blob: 3fa0463e74bb7cefd1420e39c3740a4eba8a194a [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/*
103 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
104 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
105 */
106static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700107
Tejun Heoace2bee2014-02-11 11:52:47 -0500108#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500109 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -0500110 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500111 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500112 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500113
Ben Blumaae8aab2010-03-10 15:22:07 -0800114/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500115 * cgroup destruction makes heavy use of work items and there can be a lot
116 * of concurrent destructions. Use a separate workqueue so that cgroup
117 * destruction work items don't end up filling up max_active of system_wq
118 * which may lead to deadlock.
119 */
120static struct workqueue_struct *cgroup_destroy_wq;
121
122/*
Tejun Heob1a21362013-11-29 10:42:58 -0500123 * pidlist destructions need to be flushed on cgroup destruction. Use a
124 * separate workqueue as flush domain.
125 */
126static struct workqueue_struct *cgroup_pidlist_destroy_wq;
127
Tejun Heo3ed80a62014-02-08 10:36:58 -0500128/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500129#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500130static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700131#include <linux/cgroup_subsys.h>
132};
Tejun Heo073219e2014-02-08 10:36:58 -0500133#undef SUBSYS
134
135/* array of cgroup subsystem names */
136#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
137static const char *cgroup_subsys_name[] = {
138#include <linux/cgroup_subsys.h>
139};
140#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700141
Paul Menageddbcc7e2007-10-18 23:39:30 -0700142/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400143 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700144 * unattached - it never has more than a single cgroup, and all tasks are
145 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700146 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400147struct cgroup_root cgrp_dfl_root;
Tejun Heo9871bf92013-06-24 15:21:47 -0700148
Tejun Heoa2dd4242014-03-19 10:23:55 -0400149/*
150 * The default hierarchy always exists but is hidden until mounted for the
151 * first time. This is for backward compatibility.
152 */
153static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700154
155/* The list of hierarchy roots */
156
Tejun Heo9871bf92013-06-24 15:21:47 -0700157static LIST_HEAD(cgroup_roots);
158static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700159
Tejun Heo3417ae12014-02-08 10:37:01 -0500160/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700161static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700162
Li Zefan794611a2013-06-18 18:53:53 +0800163/*
164 * Assign a monotonically increasing serial number to cgroups. It
165 * guarantees cgroups with bigger numbers are newer than those with smaller
166 * numbers. Also, as cgroups are always appended to the parent's
167 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700168 * the ascending serial number order on the list. Protected by
169 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800170 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700171static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800172
Paul Menageddbcc7e2007-10-18 23:39:30 -0700173/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800174 * check for fork/exit handlers to call. This avoids us having to do
175 * extra work in the fork/exit path if none of the subsystems need to
176 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700177 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700178static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700179
Tejun Heo628f7cd2013-06-28 16:24:11 -0700180static struct cftype cgroup_base_files[];
181
Tejun Heo59f52962014-02-11 11:52:49 -0500182static void cgroup_put(struct cgroup *cgrp);
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400183static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo69dfa002014-05-04 15:09:13 -0400184 unsigned int ss_mask);
Tejun Heof20104d2013-08-13 20:22:50 -0400185static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800186static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -0400187static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss);
188static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400189static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
190 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500191static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800192
Tejun Heo95109b62013-08-08 20:11:27 -0400193/**
194 * cgroup_css - obtain a cgroup's css for the specified subsystem
195 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400196 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400197 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400198 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
199 * function must be called either under cgroup_mutex or rcu_read_lock() and
200 * the caller is responsible for pinning the returned css if it wants to
201 * keep accessing it outside the said locks. This function may return
202 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400203 */
204static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400205 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400206{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400207 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500208 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500209 lockdep_is_held(&cgroup_tree_mutex) ||
210 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400211 else
212 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400213}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700214
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400215/**
216 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
217 * @cgrp: the cgroup of interest
218 * @ss: the subsystem of interest (%NULL returns the dummy_css)
219 *
220 * Similar to cgroup_css() but returns the effctive css, which is defined
221 * as the matching css of the nearest ancestor including self which has @ss
222 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
223 * function is guaranteed to return non-NULL css.
224 */
225static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
226 struct cgroup_subsys *ss)
227{
228 lockdep_assert_held(&cgroup_mutex);
229
230 if (!ss)
231 return &cgrp->dummy_css;
232
233 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
234 return NULL;
235
236 while (cgrp->parent &&
237 !(cgrp->parent->child_subsys_mask & (1 << ss->id)))
238 cgrp = cgrp->parent;
239
240 return cgroup_css(cgrp, ss);
241}
242
Paul Menageddbcc7e2007-10-18 23:39:30 -0700243/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700244static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700245{
Tejun Heo54766d42013-06-12 21:04:53 -0700246 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700247}
248
Tejun Heo59f52962014-02-11 11:52:49 -0500249struct cgroup_subsys_state *seq_css(struct seq_file *seq)
250{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500251 struct kernfs_open_file *of = seq->private;
252 struct cgroup *cgrp = of->kn->parent->priv;
253 struct cftype *cft = seq_cft(seq);
254
255 /*
256 * This is open and unprotected implementation of cgroup_css().
257 * seq_css() is only called from a kernfs file operation which has
258 * an active reference on the file. Because all the subsystem
259 * files are drained before a css is disassociated with a cgroup,
260 * the matching css from the cgroup's subsys table is guaranteed to
261 * be and stay valid until the enclosing operation is complete.
262 */
263 if (cft->ss)
264 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
265 else
266 return &cgrp->dummy_css;
Tejun Heo59f52962014-02-11 11:52:49 -0500267}
268EXPORT_SYMBOL_GPL(seq_css);
269
Li Zefan78574cf2013-04-08 19:00:38 -0700270/**
271 * cgroup_is_descendant - test ancestry
272 * @cgrp: the cgroup to be tested
273 * @ancestor: possible ancestor of @cgrp
274 *
275 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
276 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
277 * and @ancestor are accessible.
278 */
279bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
280{
281 while (cgrp) {
282 if (cgrp == ancestor)
283 return true;
284 cgrp = cgrp->parent;
285 }
286 return false;
287}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700288
Adrian Bunke9685a02008-02-07 00:13:46 -0800289static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700290{
291 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700292 (1 << CGRP_RELEASABLE) |
293 (1 << CGRP_NOTIFY_ON_RELEASE);
294 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700295}
296
Adrian Bunke9685a02008-02-07 00:13:46 -0800297static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700298{
Paul Menagebd89aab2007-10-18 23:40:44 -0700299 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700300}
301
Tejun Heo30159ec2013-06-25 11:53:37 -0700302/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500303 * for_each_css - iterate all css's of a cgroup
304 * @css: the iteration cursor
305 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
306 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700307 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400308 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700309 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500310#define for_each_css(css, ssid, cgrp) \
311 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
312 if (!((css) = rcu_dereference_check( \
313 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500314 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500315 lockdep_is_held(&cgroup_mutex)))) { } \
316 else
317
318/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400319 * for_each_e_css - iterate all effective css's of a cgroup
320 * @css: the iteration cursor
321 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
322 * @cgrp: the target cgroup to iterate css's of
323 *
324 * Should be called under cgroup_[tree_]mutex.
325 */
326#define for_each_e_css(css, ssid, cgrp) \
327 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
328 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
329 ; \
330 else
331
332/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500333 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700334 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500335 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700336 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500337#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500338 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
339 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700340
Tejun Heo985ed672014-03-19 10:23:53 -0400341/* iterate across the hierarchies */
342#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700343 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700344
Tejun Heof8f22e52014-04-23 11:13:16 -0400345/* iterate over child cgrps, lock should be held throughout iteration */
346#define cgroup_for_each_live_child(child, cgrp) \
347 list_for_each_entry((child), &(cgrp)->children, sibling) \
348 if (({ lockdep_assert_held(&cgroup_tree_mutex); \
349 cgroup_is_dead(child); })) \
350 ; \
351 else
352
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700353/**
354 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
355 * @cgrp: the cgroup to be checked for liveness
356 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700357 * On success, returns true; the mutex should be later unlocked. On
358 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700359 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700360static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700361{
362 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700363 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700364 mutex_unlock(&cgroup_mutex);
365 return false;
366 }
367 return true;
368}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700369
Paul Menage81a6a5c2007-10-18 23:39:38 -0700370/* the list of cgroups eligible for automatic release. Protected by
371 * release_list_lock */
372static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200373static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700374static void cgroup_release_agent(struct work_struct *work);
375static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700376static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700377
Tejun Heo69d02062013-06-12 21:04:50 -0700378/*
379 * A cgroup can be associated with multiple css_sets as different tasks may
380 * belong to different cgroups on different hierarchies. In the other
381 * direction, a css_set is naturally associated with multiple cgroups.
382 * This M:N relationship is represented by the following link structure
383 * which exists for each association and allows traversing the associations
384 * from both sides.
385 */
386struct cgrp_cset_link {
387 /* the cgroup and css_set this link associates */
388 struct cgroup *cgrp;
389 struct css_set *cset;
390
391 /* list of cgrp_cset_links anchored at cgrp->cset_links */
392 struct list_head cset_link;
393
394 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
395 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700396};
397
Tejun Heo172a2c062014-03-19 10:23:53 -0400398/*
399 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700400 * hierarchies being mounted. It contains a pointer to the root state
401 * for each subsystem. Also used to anchor the list of css_sets. Not
402 * reference-counted, to improve performance when child cgroups
403 * haven't been created.
404 */
Tejun Heo172a2c062014-03-19 10:23:53 -0400405static struct css_set init_css_set = {
406 .refcount = ATOMIC_INIT(1),
407 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
408 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
409 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
410 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
411 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
412};
Paul Menage817929e2007-10-18 23:39:36 -0700413
Tejun Heo172a2c062014-03-19 10:23:53 -0400414static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700415
Tejun Heo842b5972014-04-25 18:28:02 -0400416/**
417 * cgroup_update_populated - updated populated count of a cgroup
418 * @cgrp: the target cgroup
419 * @populated: inc or dec populated count
420 *
421 * @cgrp is either getting the first task (css_set) or losing the last.
422 * Update @cgrp->populated_cnt accordingly. The count is propagated
423 * towards root so that a given cgroup's populated_cnt is zero iff the
424 * cgroup and all its descendants are empty.
425 *
426 * @cgrp's interface file "cgroup.populated" is zero if
427 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
428 * changes from or to zero, userland is notified that the content of the
429 * interface file has changed. This can be used to detect when @cgrp and
430 * its descendants become populated or empty.
431 */
432static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
433{
434 lockdep_assert_held(&css_set_rwsem);
435
436 do {
437 bool trigger;
438
439 if (populated)
440 trigger = !cgrp->populated_cnt++;
441 else
442 trigger = !--cgrp->populated_cnt;
443
444 if (!trigger)
445 break;
446
447 if (cgrp->populated_kn)
448 kernfs_notify(cgrp->populated_kn);
449 cgrp = cgrp->parent;
450 } while (cgrp);
451}
452
Paul Menage7717f7b2009-09-23 15:56:22 -0700453/*
454 * hash table for cgroup groups. This improves the performance to find
455 * an existing css_set. This hash doesn't (currently) take into
456 * account cgroups in empty hierarchies.
457 */
Li Zefan472b1052008-04-29 01:00:11 -0700458#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800459static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700460
Li Zefan0ac801f2013-01-10 11:49:27 +0800461static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700462{
Li Zefan0ac801f2013-01-10 11:49:27 +0800463 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700464 struct cgroup_subsys *ss;
465 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700466
Tejun Heo30159ec2013-06-25 11:53:37 -0700467 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800468 key += (unsigned long)css[i];
469 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700470
Li Zefan0ac801f2013-01-10 11:49:27 +0800471 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700472}
473
Tejun Heo89c55092014-02-13 06:58:40 -0500474static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700475{
Tejun Heo69d02062013-06-12 21:04:50 -0700476 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400477 struct cgroup_subsys *ss;
478 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700479
Tejun Heo89c55092014-02-13 06:58:40 -0500480 lockdep_assert_held(&css_set_rwsem);
481
482 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700483 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700484
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700485 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400486 for_each_subsys(ss, ssid)
487 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700488 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700489 css_set_count--;
490
Tejun Heo69d02062013-06-12 21:04:50 -0700491 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700492 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700493
Tejun Heo69d02062013-06-12 21:04:50 -0700494 list_del(&link->cset_link);
495 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800496
Tejun Heo96d365e2014-02-13 06:58:40 -0500497 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400498 if (list_empty(&cgrp->cset_links)) {
499 cgroup_update_populated(cgrp, false);
500 if (notify_on_release(cgrp)) {
501 if (taskexit)
502 set_bit(CGRP_RELEASABLE, &cgrp->flags);
503 check_for_release(cgrp);
504 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700505 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700506
507 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700508 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700509
Tejun Heo5abb8852013-06-12 21:04:49 -0700510 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700511}
512
Tejun Heo89c55092014-02-13 06:58:40 -0500513static void put_css_set(struct css_set *cset, bool taskexit)
514{
515 /*
516 * Ensure that the refcount doesn't hit zero while any readers
517 * can see it. Similar to atomic_dec_and_lock(), but for an
518 * rwlock
519 */
520 if (atomic_add_unless(&cset->refcount, -1, 1))
521 return;
522
523 down_write(&css_set_rwsem);
524 put_css_set_locked(cset, taskexit);
525 up_write(&css_set_rwsem);
526}
527
Paul Menage817929e2007-10-18 23:39:36 -0700528/*
529 * refcounted get/put for css_set objects
530 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700531static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700532{
Tejun Heo5abb8852013-06-12 21:04:49 -0700533 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700534}
535
Tejun Heob326f9d2013-06-24 15:21:48 -0700536/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700537 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700538 * @cset: candidate css_set being tested
539 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700540 * @new_cgrp: cgroup that's being entered by the task
541 * @template: desired set of css pointers in css_set (pre-calculated)
542 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800543 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700544 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
545 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700546static bool compare_css_sets(struct css_set *cset,
547 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700548 struct cgroup *new_cgrp,
549 struct cgroup_subsys_state *template[])
550{
551 struct list_head *l1, *l2;
552
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400553 /*
554 * On the default hierarchy, there can be csets which are
555 * associated with the same set of cgroups but different csses.
556 * Let's first ensure that csses match.
557 */
558 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700559 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700560
561 /*
562 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400563 * different cgroups in hierarchies. As different cgroups may
564 * share the same effective css, this comparison is always
565 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700566 */
Tejun Heo69d02062013-06-12 21:04:50 -0700567 l1 = &cset->cgrp_links;
568 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700569 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700570 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700571 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700572
573 l1 = l1->next;
574 l2 = l2->next;
575 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700576 if (l1 == &cset->cgrp_links) {
577 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700578 break;
579 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700580 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700581 }
582 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700583 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
584 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
585 cgrp1 = link1->cgrp;
586 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700587 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700588 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700589
590 /*
591 * If this hierarchy is the hierarchy of the cgroup
592 * that's changing, then we need to check that this
593 * css_set points to the new cgroup; if it's any other
594 * hierarchy, then this css_set should point to the
595 * same cgroup as the old css_set.
596 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700597 if (cgrp1->root == new_cgrp->root) {
598 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700599 return false;
600 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700601 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700602 return false;
603 }
604 }
605 return true;
606}
607
Tejun Heob326f9d2013-06-24 15:21:48 -0700608/**
609 * find_existing_css_set - init css array and find the matching css_set
610 * @old_cset: the css_set that we're using before the cgroup transition
611 * @cgrp: the cgroup that we're moving into
612 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700613 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700614static struct css_set *find_existing_css_set(struct css_set *old_cset,
615 struct cgroup *cgrp,
616 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700617{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400618 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700619 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700620 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800621 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700622 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700623
Ben Blumaae8aab2010-03-10 15:22:07 -0800624 /*
625 * Build the set of subsystem state objects that we want to see in the
626 * new css_set. while subsystems can change globally, the entries here
627 * won't change, so no need for locking.
628 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700629 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400630 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400631 /*
632 * @ss is in this hierarchy, so we want the
633 * effective css from @cgrp.
634 */
635 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700636 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400637 /*
638 * @ss is not in this hierarchy, so we don't want
639 * to change the css.
640 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700641 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700642 }
643 }
644
Li Zefan0ac801f2013-01-10 11:49:27 +0800645 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700646 hash_for_each_possible(css_set_table, cset, hlist, key) {
647 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700648 continue;
649
650 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700651 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700652 }
Paul Menage817929e2007-10-18 23:39:36 -0700653
654 /* No existing cgroup group matched */
655 return NULL;
656}
657
Tejun Heo69d02062013-06-12 21:04:50 -0700658static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700659{
Tejun Heo69d02062013-06-12 21:04:50 -0700660 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700661
Tejun Heo69d02062013-06-12 21:04:50 -0700662 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
663 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700664 kfree(link);
665 }
666}
667
Tejun Heo69d02062013-06-12 21:04:50 -0700668/**
669 * allocate_cgrp_cset_links - allocate cgrp_cset_links
670 * @count: the number of links to allocate
671 * @tmp_links: list_head the allocated links are put on
672 *
673 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
674 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700675 */
Tejun Heo69d02062013-06-12 21:04:50 -0700676static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700677{
Tejun Heo69d02062013-06-12 21:04:50 -0700678 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700679 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700680
681 INIT_LIST_HEAD(tmp_links);
682
Li Zefan36553432008-07-29 22:33:19 -0700683 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700684 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700685 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700686 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700687 return -ENOMEM;
688 }
Tejun Heo69d02062013-06-12 21:04:50 -0700689 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700690 }
691 return 0;
692}
693
Li Zefanc12f65d2009-01-07 18:07:42 -0800694/**
695 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700696 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700697 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800698 * @cgrp: the destination cgroup
699 */
Tejun Heo69d02062013-06-12 21:04:50 -0700700static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
701 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800702{
Tejun Heo69d02062013-06-12 21:04:50 -0700703 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800704
Tejun Heo69d02062013-06-12 21:04:50 -0700705 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400706
707 if (cgroup_on_dfl(cgrp))
708 cset->dfl_cgrp = cgrp;
709
Tejun Heo69d02062013-06-12 21:04:50 -0700710 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
711 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700712 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400713
714 if (list_empty(&cgrp->cset_links))
715 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700716 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400717
Paul Menage7717f7b2009-09-23 15:56:22 -0700718 /*
719 * Always add links to the tail of the list so that the list
720 * is sorted by order of hierarchy creation
721 */
Tejun Heo69d02062013-06-12 21:04:50 -0700722 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800723}
724
Tejun Heob326f9d2013-06-24 15:21:48 -0700725/**
726 * find_css_set - return a new css_set with one cgroup updated
727 * @old_cset: the baseline css_set
728 * @cgrp: the cgroup to be updated
729 *
730 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
731 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700732 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700733static struct css_set *find_css_set(struct css_set *old_cset,
734 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700735{
Tejun Heob326f9d2013-06-24 15:21:48 -0700736 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700737 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700738 struct list_head tmp_links;
739 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400740 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800741 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400742 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700743
Tejun Heob326f9d2013-06-24 15:21:48 -0700744 lockdep_assert_held(&cgroup_mutex);
745
Paul Menage817929e2007-10-18 23:39:36 -0700746 /* First see if we already have a cgroup group that matches
747 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500748 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700749 cset = find_existing_css_set(old_cset, cgrp, template);
750 if (cset)
751 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500752 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700753
Tejun Heo5abb8852013-06-12 21:04:49 -0700754 if (cset)
755 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700756
Tejun Heof4f4be22013-06-12 21:04:51 -0700757 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700758 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700759 return NULL;
760
Tejun Heo69d02062013-06-12 21:04:50 -0700761 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700762 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700763 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700764 return NULL;
765 }
766
Tejun Heo5abb8852013-06-12 21:04:49 -0700767 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700768 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700769 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500770 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500771 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500772 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700773 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700774
775 /* Copy the set of subsystem state objects generated in
776 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700777 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700778
Tejun Heo96d365e2014-02-13 06:58:40 -0500779 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700780 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700781 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700782 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700783
Paul Menage7717f7b2009-09-23 15:56:22 -0700784 if (c->root == cgrp->root)
785 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700786 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700787 }
Paul Menage817929e2007-10-18 23:39:36 -0700788
Tejun Heo69d02062013-06-12 21:04:50 -0700789 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700790
Paul Menage817929e2007-10-18 23:39:36 -0700791 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700792
Tejun Heo2d8f2432014-04-23 11:13:15 -0400793 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700794 key = css_set_hash(cset->subsys);
795 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700796
Tejun Heo2d8f2432014-04-23 11:13:15 -0400797 for_each_subsys(ss, ssid)
798 list_add_tail(&cset->e_cset_node[ssid],
799 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
800
Tejun Heo96d365e2014-02-13 06:58:40 -0500801 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700802
Tejun Heo5abb8852013-06-12 21:04:49 -0700803 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700804}
805
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400806static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700807{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400808 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500809
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400810 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500811}
812
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400813static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500814{
815 int id;
816
817 lockdep_assert_held(&cgroup_mutex);
818
Tejun Heo985ed672014-03-19 10:23:53 -0400819 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500820 if (id < 0)
821 return id;
822
823 root->hierarchy_id = id;
824 return 0;
825}
826
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400827static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500828{
829 lockdep_assert_held(&cgroup_mutex);
830
831 if (root->hierarchy_id) {
832 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
833 root->hierarchy_id = 0;
834 }
835}
836
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400837static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500838{
839 if (root) {
840 /* hierarhcy ID shoulid already have been released */
841 WARN_ON_ONCE(root->hierarchy_id);
842
843 idr_destroy(&root->cgroup_idr);
844 kfree(root);
845 }
846}
847
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400848static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500849{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400850 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500851 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500852
Tejun Heo2bd59d42014-02-11 11:52:49 -0500853 mutex_lock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500854 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500855
Tejun Heo776f02f2014-02-12 09:29:50 -0500856 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500857 BUG_ON(!list_empty(&cgrp->children));
858
Tejun Heof2e85d52014-02-11 11:52:49 -0500859 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400860 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500861
862 /*
863 * Release all the links from cset_links to this hierarchy's
864 * root cgroup
865 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500866 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500867
868 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
869 list_del(&link->cset_link);
870 list_del(&link->cgrp_link);
871 kfree(link);
872 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500873 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500874
875 if (!list_empty(&root->root_list)) {
876 list_del(&root->root_list);
877 cgroup_root_count--;
878 }
879
880 cgroup_exit_root_id(root);
881
882 mutex_unlock(&cgroup_mutex);
883 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500884
Tejun Heo2bd59d42014-02-11 11:52:49 -0500885 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500886 cgroup_free_root(root);
887}
888
Tejun Heoceb6a082014-02-25 10:04:02 -0500889/* look up cgroup associated with given css_set on the specified hierarchy */
890static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400891 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700892{
Paul Menage7717f7b2009-09-23 15:56:22 -0700893 struct cgroup *res = NULL;
894
Tejun Heo96d365e2014-02-13 06:58:40 -0500895 lockdep_assert_held(&cgroup_mutex);
896 lockdep_assert_held(&css_set_rwsem);
897
Tejun Heo5abb8852013-06-12 21:04:49 -0700898 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400899 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700900 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700901 struct cgrp_cset_link *link;
902
903 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700904 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700905
Paul Menage7717f7b2009-09-23 15:56:22 -0700906 if (c->root == root) {
907 res = c;
908 break;
909 }
910 }
911 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500912
Paul Menage7717f7b2009-09-23 15:56:22 -0700913 BUG_ON(!res);
914 return res;
915}
916
917/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500918 * Return the cgroup for "task" from the given hierarchy. Must be
919 * called with cgroup_mutex and css_set_rwsem held.
920 */
921static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400922 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500923{
924 /*
925 * No need to lock the task - since we hold cgroup_mutex the
926 * task can't change groups, so the only thing that can happen
927 * is that it exits and its css is set back to init_css_set.
928 */
929 return cset_cgroup_from_root(task_css_set(task), root);
930}
931
932/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700933 * A task must hold cgroup_mutex to modify cgroups.
934 *
935 * Any task can increment and decrement the count field without lock.
936 * So in general, code holding cgroup_mutex can't rely on the count
937 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800938 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700939 * means that no tasks are currently attached, therefore there is no
940 * way a task attached to that cgroup can fork (the other way to
941 * increment the count). So code holding cgroup_mutex can safely
942 * assume that if the count is zero, it will stay zero. Similarly, if
943 * a task holds cgroup_mutex on a cgroup with zero count, it
944 * knows that the cgroup won't be removed, as cgroup_rmdir()
945 * needs that mutex.
946 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700947 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
948 * (usually) take cgroup_mutex. These are the two most performance
949 * critical pieces of code here. The exception occurs on cgroup_exit(),
950 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
951 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800952 * to the release agent with the name of the cgroup (path relative to
953 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700954 *
955 * A cgroup can only be deleted if both its 'count' of using tasks
956 * is zero, and its list of 'children' cgroups is empty. Since all
957 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400958 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700959 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400960 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700961 *
962 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800963 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700964 */
965
Tejun Heo69dfa002014-05-04 15:09:13 -0400966static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500967static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700968static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700969
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500970static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
971 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500973 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
974 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
975 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
976 cft->ss->name, cft->name);
977 else
978 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
979 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980}
981
Tejun Heof2e85d52014-02-11 11:52:49 -0500982/**
983 * cgroup_file_mode - deduce file mode of a control file
984 * @cft: the control file in question
985 *
986 * returns cft->mode if ->mode is not 0
987 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
988 * returns S_IRUGO if it has only a read handler
989 * returns S_IWUSR if it has only a write hander
990 */
991static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +0800992{
Tejun Heof2e85d52014-02-11 11:52:49 -0500993 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +0800994
Tejun Heof2e85d52014-02-11 11:52:49 -0500995 if (cft->mode)
996 return cft->mode;
997
998 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
999 mode |= S_IRUGO;
1000
1001 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
1002 cft->trigger)
1003 mode |= S_IWUSR;
1004
1005 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001006}
1007
Li Zefanbe445622013-01-24 14:31:42 +08001008static void cgroup_free_fn(struct work_struct *work)
1009{
Tejun Heoea15f8c2013-06-13 19:27:42 -07001010 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001011
Tejun Heo3c9c8252014-02-12 09:29:50 -05001012 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -05001013 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +08001014
Tejun Heo776f02f2014-02-12 09:29:50 -05001015 if (cgrp->parent) {
1016 /*
1017 * We get a ref to the parent, and put the ref when this
1018 * cgroup is being freed, so it's guaranteed that the
1019 * parent won't be destroyed before its children.
1020 */
1021 cgroup_put(cgrp->parent);
1022 kernfs_put(cgrp->kn);
1023 kfree(cgrp);
1024 } else {
1025 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001026 * This is root cgroup's refcnt reaching zero, which
Tejun Heo776f02f2014-02-12 09:29:50 -05001027 * indicates that the root should be released.
1028 */
1029 cgroup_destroy_root(cgrp->root);
1030 }
Li Zefanbe445622013-01-24 14:31:42 +08001031}
1032
1033static void cgroup_free_rcu(struct rcu_head *head)
1034{
1035 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
1036
Tejun Heoea15f8c2013-06-13 19:27:42 -07001037 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05001038 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001039}
1040
Tejun Heo59f52962014-02-11 11:52:49 -05001041static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001042{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001043 WARN_ON_ONCE(cgroup_is_dead(cgrp));
1044 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
1045 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046}
1047
Tejun Heo59f52962014-02-11 11:52:49 -05001048static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001050 if (!atomic_dec_and_test(&cgrp->refcnt))
1051 return;
Tejun Heo776f02f2014-02-12 09:29:50 -05001052 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -05001053 return;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054
Tejun Heo2bd59d42014-02-11 11:52:49 -05001055 /*
1056 * XXX: cgrp->id is only used to look up css's. As cgroup and
1057 * css's lifetimes will be decoupled, it should be made
1058 * per-subsystem and moved to css->id so that lookups are
1059 * successful until the target css is released.
1060 */
1061 mutex_lock(&cgroup_mutex);
1062 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
1063 mutex_unlock(&cgroup_mutex);
1064 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065
Tejun Heo2bd59d42014-02-11 11:52:49 -05001066 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067}
1068
Li Zefan2739d3c2013-01-21 18:18:33 +08001069static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001071 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072
Tejun Heoace2bee2014-02-11 11:52:47 -05001073 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001074 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001075}
1076
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001077/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001078 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001079 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001080 * @subsys_mask: mask of the subsystem ids whose files should be removed
1081 */
Tejun Heo69dfa002014-05-04 15:09:13 -04001082static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001083{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001084 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001085 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001086
Tejun Heob420ba72013-07-12 12:34:02 -07001087 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001088 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001089
Tejun Heo69dfa002014-05-04 15:09:13 -04001090 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001091 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001092 list_for_each_entry(cfts, &ss->cfts, node)
1093 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001094 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095}
1096
Tejun Heo69dfa002014-05-04 15:09:13 -04001097static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001098{
Tejun Heo30159ec2013-06-25 11:53:37 -07001099 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001100 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101
Tejun Heoace2bee2014-02-11 11:52:47 -05001102 lockdep_assert_held(&cgroup_tree_mutex);
1103 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001104
Tejun Heo5df36032014-03-19 10:23:54 -04001105 for_each_subsys(ss, ssid) {
1106 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001108
Tejun Heo7fd8c562014-04-23 11:13:16 -04001109 /* if @ss has non-root csses attached to it, can't move */
1110 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001111 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001112
Tejun Heo5df36032014-03-19 10:23:54 -04001113 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001114 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001115 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001116 }
1117
Tejun Heoa2dd4242014-03-19 10:23:55 -04001118 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1119 if (ret) {
1120 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001121 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001122
Tejun Heoa2dd4242014-03-19 10:23:55 -04001123 /*
1124 * Rebinding back to the default root is not allowed to
1125 * fail. Using both default and non-default roots should
1126 * be rare. Moving subsystems back and forth even more so.
1127 * Just warn about it and continue.
1128 */
1129 if (cgrp_dfl_root_visible) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001130 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001131 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001132 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001133 }
Tejun Heo5df36032014-03-19 10:23:54 -04001134 }
Tejun Heo31261212013-06-28 17:07:30 -07001135
1136 /*
1137 * Nothing can fail from this point on. Remove files for the
1138 * removed subsystems and rebind each subsystem.
1139 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001140 mutex_unlock(&cgroup_mutex);
Tejun Heo5df36032014-03-19 10:23:54 -04001141 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001142 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001143 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo4ac06012014-02-11 11:52:47 -05001144 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001145
Tejun Heo5df36032014-03-19 10:23:54 -04001146 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001147 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001148 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001149 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001150
Tejun Heo5df36032014-03-19 10:23:54 -04001151 if (!(ss_mask & (1 << ssid)))
1152 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001153
Tejun Heo5df36032014-03-19 10:23:54 -04001154 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001155 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001156
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001157 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001158
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001159 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1160 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001161 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001162 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001163
Tejun Heo2d8f2432014-04-23 11:13:15 -04001164 down_write(&css_set_rwsem);
1165 hash_for_each(css_set_table, i, cset, hlist)
1166 list_move_tail(&cset->e_cset_node[ss->id],
1167 &dst_root->cgrp.e_csets[ss->id]);
1168 up_write(&css_set_rwsem);
1169
Tejun Heof392e512014-04-23 11:13:14 -04001170 src_root->subsys_mask &= ~(1 << ssid);
1171 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1172
Tejun Heobd53d612014-04-23 11:13:16 -04001173 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001174 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001175 if (dst_root != &cgrp_dfl_root)
1176 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001177
Tejun Heo5df36032014-03-19 10:23:54 -04001178 if (ss->bind)
1179 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001180 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001181
Tejun Heoa2dd4242014-03-19 10:23:55 -04001182 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001183 return 0;
1184}
1185
Tejun Heo2bd59d42014-02-11 11:52:49 -05001186static int cgroup_show_options(struct seq_file *seq,
1187 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001188{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001189 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001190 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001191 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001192
Tejun Heob85d2042013-12-06 15:11:57 -05001193 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001194 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001195 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001196 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1197 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001198 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001199 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001200 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001201 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001202
1203 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001204 if (strlen(root->release_agent_path))
1205 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001206 spin_unlock(&release_agent_path_lock);
1207
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001208 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001209 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001210 if (strlen(root->name))
1211 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001212 return 0;
1213}
1214
1215struct cgroup_sb_opts {
Tejun Heo69dfa002014-05-04 15:09:13 -04001216 unsigned int subsys_mask;
1217 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001218 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001219 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001220 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001221 /* User explicitly requested empty subsystem */
1222 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001223};
1224
Ben Blumcf5d5942010-03-10 15:22:09 -08001225static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001226{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001227 char *token, *o = data;
1228 bool all_ss = false, one_ss = false;
Tejun Heo69dfa002014-05-04 15:09:13 -04001229 unsigned int mask = -1U;
Tejun Heo30159ec2013-06-25 11:53:37 -07001230 struct cgroup_subsys *ss;
1231 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001232
1233#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001234 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001235#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001236
Paul Menagec6d57f32009-09-23 15:56:19 -07001237 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001238
1239 while ((token = strsep(&o, ",")) != NULL) {
1240 if (!*token)
1241 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001242 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001243 /* Explicitly have no subsystems */
1244 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001245 continue;
1246 }
1247 if (!strcmp(token, "all")) {
1248 /* Mutually exclusive option 'all' + subsystem name */
1249 if (one_ss)
1250 return -EINVAL;
1251 all_ss = true;
1252 continue;
1253 }
Tejun Heo873fe092013-04-14 20:15:26 -07001254 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1255 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1256 continue;
1257 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001258 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001259 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001260 continue;
1261 }
1262 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001263 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001264 continue;
1265 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001266 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001267 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001268 continue;
1269 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001270 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001271 /* Specifying two release agents is forbidden */
1272 if (opts->release_agent)
1273 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001274 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001275 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001276 if (!opts->release_agent)
1277 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001278 continue;
1279 }
1280 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001281 const char *name = token + 5;
1282 /* Can't specify an empty name */
1283 if (!strlen(name))
1284 return -EINVAL;
1285 /* Must match [\w.-]+ */
1286 for (i = 0; i < strlen(name); i++) {
1287 char c = name[i];
1288 if (isalnum(c))
1289 continue;
1290 if ((c == '.') || (c == '-') || (c == '_'))
1291 continue;
1292 return -EINVAL;
1293 }
1294 /* Specifying two names is forbidden */
1295 if (opts->name)
1296 return -EINVAL;
1297 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001298 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001299 GFP_KERNEL);
1300 if (!opts->name)
1301 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001302
1303 continue;
1304 }
1305
Tejun Heo30159ec2013-06-25 11:53:37 -07001306 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001307 if (strcmp(token, ss->name))
1308 continue;
1309 if (ss->disabled)
1310 continue;
1311
1312 /* Mutually exclusive option 'all' + subsystem name */
1313 if (all_ss)
1314 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001315 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001316 one_ss = true;
1317
1318 break;
1319 }
1320 if (i == CGROUP_SUBSYS_COUNT)
1321 return -ENOENT;
1322 }
1323
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001324 /* Consistency checks */
1325
Tejun Heo873fe092013-04-14 20:15:26 -07001326 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001327 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 -07001328
Tejun Heod3ba07c2014-02-13 06:58:38 -05001329 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1330 opts->cpuset_clone_children || opts->release_agent ||
1331 opts->name) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001332 pr_err("sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001333 return -EINVAL;
1334 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001335 } else {
1336 /*
1337 * If the 'all' option was specified select all the
1338 * subsystems, otherwise if 'none', 'name=' and a subsystem
1339 * name options were not specified, let's default to 'all'
1340 */
1341 if (all_ss || (!one_ss && !opts->none && !opts->name))
1342 for_each_subsys(ss, i)
1343 if (!ss->disabled)
Tejun Heo69dfa002014-05-04 15:09:13 -04001344 opts->subsys_mask |= (1 << i);
Tejun Heo873fe092013-04-14 20:15:26 -07001345
Tejun Heoa2dd4242014-03-19 10:23:55 -04001346 /*
1347 * We either have to specify by name or by subsystems. (So
1348 * all empty hierarchies must have a name).
1349 */
1350 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001351 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001352 }
1353
Li Zefanf9ab5b52009-06-17 16:26:33 -07001354 /*
1355 * Option noprefix was introduced just for backward compatibility
1356 * with the old cpuset, so we allow noprefix only if mounting just
1357 * the cpuset subsystem.
1358 */
Tejun Heo93438622013-04-14 20:15:25 -07001359 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001360 return -EINVAL;
1361
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001362
1363 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001364 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001365 return -EINVAL;
1366
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367 return 0;
1368}
1369
Tejun Heo2bd59d42014-02-11 11:52:49 -05001370static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371{
1372 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001373 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374 struct cgroup_sb_opts opts;
Tejun Heo69dfa002014-05-04 15:09:13 -04001375 unsigned int added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001376
Tejun Heo873fe092013-04-14 20:15:26 -07001377 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001378 pr_err("sane_behavior: remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001379 return -EINVAL;
1380 }
1381
Tejun Heoace2bee2014-02-11 11:52:47 -05001382 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383 mutex_lock(&cgroup_mutex);
1384
1385 /* See what subsystems are wanted */
1386 ret = parse_cgroupfs_options(data, &opts);
1387 if (ret)
1388 goto out_unlock;
1389
Tejun Heof392e512014-04-23 11:13:14 -04001390 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001391 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001392 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001393
Tejun Heof392e512014-04-23 11:13:14 -04001394 added_mask = opts.subsys_mask & ~root->subsys_mask;
1395 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001396
Ben Blumcf5d5942010-03-10 15:22:09 -08001397 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001398 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001399 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001400 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001401 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1402 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001403 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001404 goto out_unlock;
1405 }
1406
Tejun Heof172e672013-06-28 17:07:30 -07001407 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001408 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001409 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001410 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001411 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001412
Tejun Heo5df36032014-03-19 10:23:54 -04001413 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001414 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001415 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001416
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001417 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001418
Tejun Heo69e943b2014-02-08 10:36:58 -05001419 if (opts.release_agent) {
1420 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001421 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001422 spin_unlock(&release_agent_path_lock);
1423 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001424 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001425 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001426 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001427 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001428 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429 return ret;
1430}
1431
Tejun Heoafeb0f92014-02-13 06:58:39 -05001432/*
1433 * To reduce the fork() overhead for systems that are not actually using
1434 * their cgroups capability, we don't maintain the lists running through
1435 * each css_set to its tasks until we see the list actually used - in other
1436 * words after the first mount.
1437 */
1438static bool use_task_css_set_links __read_mostly;
1439
1440static void cgroup_enable_task_cg_lists(void)
1441{
1442 struct task_struct *p, *g;
1443
Tejun Heo96d365e2014-02-13 06:58:40 -05001444 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001445
1446 if (use_task_css_set_links)
1447 goto out_unlock;
1448
1449 use_task_css_set_links = true;
1450
1451 /*
1452 * We need tasklist_lock because RCU is not safe against
1453 * while_each_thread(). Besides, a forking task that has passed
1454 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1455 * is not guaranteed to have its child immediately visible in the
1456 * tasklist if we walk through it with RCU.
1457 */
1458 read_lock(&tasklist_lock);
1459 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001460 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1461 task_css_set(p) != &init_css_set);
1462
1463 /*
1464 * We should check if the process is exiting, otherwise
1465 * it will race with cgroup_exit() in that the list
1466 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001467 * Do it while holding siglock so that we don't end up
1468 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001469 */
Tejun Heof153ad12014-02-25 09:56:49 -05001470 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001471 if (!(p->flags & PF_EXITING)) {
1472 struct css_set *cset = task_css_set(p);
1473
1474 list_add(&p->cg_list, &cset->tasks);
1475 get_css_set(cset);
1476 }
Tejun Heof153ad12014-02-25 09:56:49 -05001477 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001478 } while_each_thread(g, p);
1479 read_unlock(&tasklist_lock);
1480out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001481 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001482}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001483
Paul Menagecc31edc2008-10-18 20:28:04 -07001484static void init_cgroup_housekeeping(struct cgroup *cgrp)
1485{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001486 struct cgroup_subsys *ss;
1487 int ssid;
1488
Tejun Heo2bd59d42014-02-11 11:52:49 -05001489 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001490 INIT_LIST_HEAD(&cgrp->sibling);
1491 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001492 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001493 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001494 INIT_LIST_HEAD(&cgrp->pidlists);
1495 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001496 cgrp->dummy_css.cgroup = cgrp;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001497
1498 for_each_subsys(ss, ssid)
1499 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001500
1501 init_waitqueue_head(&cgrp->offline_waitq);
Paul Menagecc31edc2008-10-18 20:28:04 -07001502}
Paul Menagec6d57f32009-09-23 15:56:19 -07001503
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001504static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001505 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001506{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001507 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001508
Paul Menageddbcc7e2007-10-18 23:39:30 -07001509 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001510 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001511 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001512 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001513 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514
Paul Menagec6d57f32009-09-23 15:56:19 -07001515 root->flags = opts->flags;
1516 if (opts->release_agent)
1517 strcpy(root->release_agent_path, opts->release_agent);
1518 if (opts->name)
1519 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001520 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001521 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001522}
1523
Tejun Heo69dfa002014-05-04 15:09:13 -04001524static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001525{
Tejun Heod427dfe2014-02-11 11:52:48 -05001526 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001527 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001528 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001529 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001530
Tejun Heod427dfe2014-02-11 11:52:48 -05001531 lockdep_assert_held(&cgroup_tree_mutex);
1532 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001533
Tejun Heo7d699dd2014-05-04 15:09:13 -04001534 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
Tejun Heod427dfe2014-02-11 11:52:48 -05001535 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001536 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001537 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001538
Tejun Heod427dfe2014-02-11 11:52:48 -05001539 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001540 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001541 * but that's OK - it can only be increased by someone holding
1542 * cgroup_lock, and that's us. The worst that can happen is that we
1543 * have some link structures left over
1544 */
1545 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001547 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548
Tejun Heo985ed672014-03-19 10:23:53 -04001549 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001550 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001551 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552
Tejun Heo2bd59d42014-02-11 11:52:49 -05001553 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1554 KERNFS_ROOT_CREATE_DEACTIVATED,
1555 root_cgrp);
1556 if (IS_ERR(root->kf_root)) {
1557 ret = PTR_ERR(root->kf_root);
1558 goto exit_root_id;
1559 }
1560 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561
Tejun Heod427dfe2014-02-11 11:52:48 -05001562 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1563 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001564 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565
Tejun Heo5df36032014-03-19 10:23:54 -04001566 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001567 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001568 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001569
Tejun Heod427dfe2014-02-11 11:52:48 -05001570 /*
1571 * There must be no failure case after here, since rebinding takes
1572 * care of subsystems' refcounts, which are explicitly dropped in
1573 * the failure exit path.
1574 */
1575 list_add(&root->root_list, &cgroup_roots);
1576 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577
Tejun Heod427dfe2014-02-11 11:52:48 -05001578 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001579 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001580 * objects.
1581 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001582 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001583 hash_for_each(css_set_table, i, cset, hlist)
1584 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001585 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586
Tejun Heod427dfe2014-02-11 11:52:48 -05001587 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001588 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001589
Tejun Heo2bd59d42014-02-11 11:52:49 -05001590 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001591 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001592 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001593
Tejun Heo2bd59d42014-02-11 11:52:49 -05001594destroy_root:
1595 kernfs_destroy_root(root->kf_root);
1596 root->kf_root = NULL;
1597exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001598 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001599out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001600 free_cgrp_cset_links(&tmp_links);
1601 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001602}
1603
Al Virof7e83572010-07-26 13:23:11 +04001604static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001606 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001608 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001610 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001611 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001612 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001613
1614 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001615 * The first time anyone tries to mount a cgroup, enable the list
1616 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001617 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001618 if (!use_task_css_set_links)
1619 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001620
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001621 mutex_lock(&cgroup_tree_mutex);
1622 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001625 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001627 goto out_unlock;
Li Zefane37a06f2014-04-17 13:53:08 +08001628retry:
Tejun Heo2bd59d42014-02-11 11:52:49 -05001629 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001630 if (!opts.subsys_mask && !opts.none && !opts.name) {
1631 cgrp_dfl_root_visible = true;
1632 root = &cgrp_dfl_root;
1633 cgroup_get(&root->cgrp);
1634 ret = 0;
1635 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636 }
1637
Tejun Heo985ed672014-03-19 10:23:53 -04001638 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001639 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001640
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001641 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001642 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001643
Paul Menage817929e2007-10-18 23:39:36 -07001644 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001645 * If we asked for a name then it must match. Also, if
1646 * name matches but sybsys_mask doesn't, we should fail.
1647 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001648 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001649 if (opts.name) {
1650 if (strcmp(opts.name, root->name))
1651 continue;
1652 name_match = true;
1653 }
Tejun Heo31261212013-06-28 17:07:30 -07001654
1655 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001656 * If we asked for subsystems (or explicitly for no
1657 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001658 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001659 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001660 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001661 if (!name_match)
1662 continue;
1663 ret = -EBUSY;
1664 goto out_unlock;
1665 }
Tejun Heo873fe092013-04-14 20:15:26 -07001666
Tejun Heoc7ba8282013-06-29 14:06:10 -07001667 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001668 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001669 pr_err("sane_behavior: new mount options should match the existing superblock\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001670 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001671 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001672 } else {
Joe Perchesed3d2612014-04-25 18:28:03 -04001673 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001674 }
Tejun Heo873fe092013-04-14 20:15:26 -07001675 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001676
Tejun Heo776f02f2014-02-12 09:29:50 -05001677 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001678 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001679 * ref indicate that the root is being destroyed. Wait for
1680 * destruction to complete so that the subsystems are free.
1681 * We can use wait_queue for the wait but this path is
1682 * super cold. Let's just sleep for a bit and retry.
1683 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001684 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001685 mutex_unlock(&cgroup_mutex);
1686 mutex_unlock(&cgroup_tree_mutex);
1687 msleep(10);
Li Zefane37a06f2014-04-17 13:53:08 +08001688 mutex_lock(&cgroup_tree_mutex);
1689 mutex_lock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001690 goto retry;
1691 }
1692
1693 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001694 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001695 }
1696
Tejun Heo172a2c062014-03-19 10:23:53 -04001697 /*
1698 * No such thing, create a new one. name= matching without subsys
1699 * specification is allowed for already existing hierarchies but we
1700 * can't create new one without subsys specification.
1701 */
1702 if (!opts.subsys_mask && !opts.none) {
1703 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001704 goto out_unlock;
1705 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706
Tejun Heo172a2c062014-03-19 10:23:53 -04001707 root = kzalloc(sizeof(*root), GFP_KERNEL);
1708 if (!root) {
1709 ret = -ENOMEM;
1710 goto out_unlock;
1711 }
1712
1713 init_cgroup_root(root, &opts);
1714
Tejun Heo35585572014-02-13 06:58:38 -05001715 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001716 if (ret)
1717 cgroup_free_root(root);
1718
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001719out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001720 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001721 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001722
Paul Menagec6d57f32009-09-23 15:56:19 -07001723 kfree(opts.release_agent);
1724 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001725
Tejun Heo2bd59d42014-02-11 11:52:49 -05001726 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001727 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001728
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001729 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1730 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001731 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001732 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733}
1734
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001735static void cgroup_kill_sb(struct super_block *sb)
1736{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001737 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001738 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001740 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001741 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001742}
1743
1744static struct file_system_type cgroup_fs_type = {
1745 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001746 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001747 .kill_sb = cgroup_kill_sb,
1748};
1749
Greg KH676db4a2010-08-05 13:53:35 -07001750static struct kobject *cgroup_kobj;
1751
Li Zefana043e3b2008-02-23 15:24:09 -08001752/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001753 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001754 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001755 * @buf: the buffer to write the path into
1756 * @buflen: the length of the buffer
1757 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001758 * Determine @task's cgroup on the first (the one with the lowest non-zero
1759 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1760 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1761 * cgroup controller callbacks.
1762 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001763 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001764 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001765char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001766{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001767 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001768 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001769 int hierarchy_id = 1;
1770 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001771
1772 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001773 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001774
Tejun Heo913ffdb2013-07-11 16:34:48 -07001775 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1776
Tejun Heo857a2be2013-04-14 20:50:08 -07001777 if (root) {
1778 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001779 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001780 } else {
1781 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001782 if (strlcpy(buf, "/", buflen) < buflen)
1783 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001784 }
1785
Tejun Heo96d365e2014-02-13 06:58:40 -05001786 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001787 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001788 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001789}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001790EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001791
Tejun Heob3dc0942014-02-25 10:04:01 -05001792/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001793struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001794 /* the src and dst cset list running through cset->mg_node */
1795 struct list_head src_csets;
1796 struct list_head dst_csets;
1797
1798 /*
1799 * Fields for cgroup_taskset_*() iteration.
1800 *
1801 * Before migration is committed, the target migration tasks are on
1802 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1803 * the csets on ->dst_csets. ->csets point to either ->src_csets
1804 * or ->dst_csets depending on whether migration is committed.
1805 *
1806 * ->cur_csets and ->cur_task point to the current task position
1807 * during iteration.
1808 */
1809 struct list_head *csets;
1810 struct css_set *cur_cset;
1811 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001812};
1813
1814/**
1815 * cgroup_taskset_first - reset taskset and return the first task
1816 * @tset: taskset of interest
1817 *
1818 * @tset iteration is initialized and the first task is returned.
1819 */
1820struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1821{
Tejun Heob3dc0942014-02-25 10:04:01 -05001822 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1823 tset->cur_task = NULL;
1824
1825 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001826}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001827
1828/**
1829 * cgroup_taskset_next - iterate to the next task in taskset
1830 * @tset: taskset of interest
1831 *
1832 * Return the next task in @tset. Iteration must have been initialized
1833 * with cgroup_taskset_first().
1834 */
1835struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1836{
Tejun Heob3dc0942014-02-25 10:04:01 -05001837 struct css_set *cset = tset->cur_cset;
1838 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001839
Tejun Heob3dc0942014-02-25 10:04:01 -05001840 while (&cset->mg_node != tset->csets) {
1841 if (!task)
1842 task = list_first_entry(&cset->mg_tasks,
1843 struct task_struct, cg_list);
1844 else
1845 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001846
Tejun Heob3dc0942014-02-25 10:04:01 -05001847 if (&task->cg_list != &cset->mg_tasks) {
1848 tset->cur_cset = cset;
1849 tset->cur_task = task;
1850 return task;
1851 }
1852
1853 cset = list_next_entry(cset, mg_node);
1854 task = NULL;
1855 }
1856
1857 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001858}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001859
1860/**
Ben Blum74a11662011-05-26 16:25:20 -07001861 * cgroup_task_migrate - move a task from one cgroup to another.
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001862 * @old_cgrp; the cgroup @tsk is being migrated from
1863 * @tsk: the task being migrated
1864 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001865 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001866 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001867 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001868static void cgroup_task_migrate(struct cgroup *old_cgrp,
1869 struct task_struct *tsk,
1870 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001871{
Tejun Heo5abb8852013-06-12 21:04:49 -07001872 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001873
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001874 lockdep_assert_held(&cgroup_mutex);
1875 lockdep_assert_held(&css_set_rwsem);
1876
Ben Blum74a11662011-05-26 16:25:20 -07001877 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001878 * We are synchronized through threadgroup_lock() against PF_EXITING
1879 * setting such that we can't race against cgroup_exit() changing the
1880 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001881 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001882 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001883 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001884
Tejun Heob3dc0942014-02-25 10:04:01 -05001885 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001886 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001887
Tejun Heo1b9aba42014-03-19 17:43:21 -04001888 /*
1889 * Use move_tail so that cgroup_taskset_first() still returns the
1890 * leader after migration. This works because cgroup_migrate()
1891 * ensures that the dst_cset of the leader is the first on the
1892 * tset's dst_csets list.
1893 */
1894 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001895
1896 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001897 * We just gained a reference on old_cset by taking it from the
1898 * task. As trading it for new_cset is protected by cgroup_mutex,
1899 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001900 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001901 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001902 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001903}
1904
Li Zefana043e3b2008-02-23 15:24:09 -08001905/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001906 * cgroup_migrate_finish - cleanup after attach
1907 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001908 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001909 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1910 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001911 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001912static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001913{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001914 struct css_set *cset, *tmp_cset;
1915
1916 lockdep_assert_held(&cgroup_mutex);
1917
1918 down_write(&css_set_rwsem);
1919 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1920 cset->mg_src_cgrp = NULL;
1921 cset->mg_dst_cset = NULL;
1922 list_del_init(&cset->mg_preload_node);
1923 put_css_set_locked(cset, false);
1924 }
1925 up_write(&css_set_rwsem);
1926}
1927
1928/**
1929 * cgroup_migrate_add_src - add a migration source css_set
1930 * @src_cset: the source css_set to add
1931 * @dst_cgrp: the destination cgroup
1932 * @preloaded_csets: list of preloaded css_sets
1933 *
1934 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1935 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1936 * up by cgroup_migrate_finish().
1937 *
1938 * This function may be called without holding threadgroup_lock even if the
1939 * target is a process. Threads may be created and destroyed but as long
1940 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1941 * the preloaded css_sets are guaranteed to cover all migrations.
1942 */
1943static void cgroup_migrate_add_src(struct css_set *src_cset,
1944 struct cgroup *dst_cgrp,
1945 struct list_head *preloaded_csets)
1946{
1947 struct cgroup *src_cgrp;
1948
1949 lockdep_assert_held(&cgroup_mutex);
1950 lockdep_assert_held(&css_set_rwsem);
1951
1952 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1953
Tejun Heo1958d2d2014-02-25 10:04:03 -05001954 if (!list_empty(&src_cset->mg_preload_node))
1955 return;
1956
1957 WARN_ON(src_cset->mg_src_cgrp);
1958 WARN_ON(!list_empty(&src_cset->mg_tasks));
1959 WARN_ON(!list_empty(&src_cset->mg_node));
1960
1961 src_cset->mg_src_cgrp = src_cgrp;
1962 get_css_set(src_cset);
1963 list_add(&src_cset->mg_preload_node, preloaded_csets);
1964}
1965
1966/**
1967 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04001968 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05001969 * @preloaded_csets: list of preloaded source css_sets
1970 *
1971 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
1972 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04001973 * pins all destination css_sets, links each to its source, and append them
1974 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
1975 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05001976 *
1977 * This function must be called after cgroup_migrate_add_src() has been
1978 * called on each migration source css_set. After migration is performed
1979 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
1980 * @preloaded_csets.
1981 */
1982static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
1983 struct list_head *preloaded_csets)
1984{
1985 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04001986 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05001987
1988 lockdep_assert_held(&cgroup_mutex);
1989
Tejun Heof8f22e52014-04-23 11:13:16 -04001990 /*
1991 * Except for the root, child_subsys_mask must be zero for a cgroup
1992 * with tasks so that child cgroups don't compete against tasks.
1993 */
1994 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && dst_cgrp->parent &&
1995 dst_cgrp->child_subsys_mask)
1996 return -EBUSY;
1997
Tejun Heo1958d2d2014-02-25 10:04:03 -05001998 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04001999 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002000 struct css_set *dst_cset;
2001
Tejun Heof817de92014-04-23 11:13:16 -04002002 dst_cset = find_css_set(src_cset,
2003 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002004 if (!dst_cset)
2005 goto err;
2006
2007 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002008
2009 /*
2010 * If src cset equals dst, it's noop. Drop the src.
2011 * cgroup_migrate() will skip the cset too. Note that we
2012 * can't handle src == dst as some nodes are used by both.
2013 */
2014 if (src_cset == dst_cset) {
2015 src_cset->mg_src_cgrp = NULL;
2016 list_del_init(&src_cset->mg_preload_node);
2017 put_css_set(src_cset, false);
2018 put_css_set(dst_cset, false);
2019 continue;
2020 }
2021
Tejun Heo1958d2d2014-02-25 10:04:03 -05002022 src_cset->mg_dst_cset = dst_cset;
2023
2024 if (list_empty(&dst_cset->mg_preload_node))
2025 list_add(&dst_cset->mg_preload_node, &csets);
2026 else
2027 put_css_set(dst_cset, false);
2028 }
2029
Tejun Heof817de92014-04-23 11:13:16 -04002030 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002031 return 0;
2032err:
2033 cgroup_migrate_finish(&csets);
2034 return -ENOMEM;
2035}
2036
2037/**
2038 * cgroup_migrate - migrate a process or task to a cgroup
2039 * @cgrp: the destination cgroup
2040 * @leader: the leader of the process or the task to migrate
2041 * @threadgroup: whether @leader points to the whole process or a single task
2042 *
2043 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2044 * process, the caller must be holding threadgroup_lock of @leader. The
2045 * caller is also responsible for invoking cgroup_migrate_add_src() and
2046 * cgroup_migrate_prepare_dst() on the targets before invoking this
2047 * function and following up with cgroup_migrate_finish().
2048 *
2049 * As long as a controller's ->can_attach() doesn't fail, this function is
2050 * guaranteed to succeed. This means that, excluding ->can_attach()
2051 * failure, when migrating multiple targets, the success or failure can be
2052 * decided for all targets by invoking group_migrate_prepare_dst() before
2053 * actually starting migrating.
2054 */
2055static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2056 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002057{
Tejun Heob3dc0942014-02-25 10:04:01 -05002058 struct cgroup_taskset tset = {
2059 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2060 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2061 .csets = &tset.src_csets,
2062 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002063 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002064 struct css_set *cset, *tmp_cset;
2065 struct task_struct *task, *tmp_task;
2066 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002067
2068 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002069 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2070 * already PF_EXITING could be freed from underneath us unless we
2071 * take an rcu_read_lock.
2072 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002073 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002074 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002075 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002076 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002077 /* @task either already exited or can't exit until the end */
2078 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002079 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002080
Tejun Heoeaf797a2014-02-25 10:04:03 -05002081 /* leave @task alone if post_fork() hasn't linked it yet */
2082 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002083 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002084
Tejun Heob3dc0942014-02-25 10:04:01 -05002085 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002086 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002087 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002088
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002089 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002090 * cgroup_taskset_first() must always return the leader.
2091 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002092 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002093 list_move_tail(&task->cg_list, &cset->mg_tasks);
2094 if (list_empty(&cset->mg_node))
2095 list_add_tail(&cset->mg_node, &tset.src_csets);
2096 if (list_empty(&cset->mg_dst_cset->mg_node))
2097 list_move_tail(&cset->mg_dst_cset->mg_node,
2098 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002099 next:
Li Zefan081aa452013-03-13 09:17:09 +08002100 if (!threadgroup)
2101 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002102 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002103 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002104 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002105
Tejun Heo134d3372011-12-12 18:12:21 -08002106 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002107 if (list_empty(&tset.src_csets))
2108 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002109
Tejun Heo1958d2d2014-02-25 10:04:03 -05002110 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002111 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002112 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002113 ret = css->ss->can_attach(css, &tset);
2114 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002115 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002116 goto out_cancel_attach;
2117 }
2118 }
Ben Blum74a11662011-05-26 16:25:20 -07002119 }
2120
2121 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002122 * Now that we're guaranteed success, proceed to move all tasks to
2123 * the new cgroup. There are no failure cases after here, so this
2124 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002125 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002126 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002127 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2128 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2129 cgroup_task_migrate(cset->mg_src_cgrp, task,
2130 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002131 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002132 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002133
2134 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002135 * Migration is committed, all target tasks are now on dst_csets.
2136 * Nothing is sensitive to fork() after this point. Notify
2137 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002138 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002139 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002140
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002141 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002142 if (css->ss->attach)
2143 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002144
Tejun Heo9db8de32014-02-13 06:58:43 -05002145 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002146 goto out_release_tset;
2147
Ben Blum74a11662011-05-26 16:25:20 -07002148out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002149 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002150 if (css == failed_css)
2151 break;
2152 if (css->ss->cancel_attach)
2153 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002154 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002155out_release_tset:
2156 down_write(&css_set_rwsem);
2157 list_splice_init(&tset.dst_csets, &tset.src_csets);
2158 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002159 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002160 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002161 }
2162 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002163 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002164}
2165
Tejun Heo1958d2d2014-02-25 10:04:03 -05002166/**
2167 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2168 * @dst_cgrp: the cgroup to attach to
2169 * @leader: the task or the leader of the threadgroup to be attached
2170 * @threadgroup: attach the whole threadgroup?
2171 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002172 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002173 */
2174static int cgroup_attach_task(struct cgroup *dst_cgrp,
2175 struct task_struct *leader, bool threadgroup)
2176{
2177 LIST_HEAD(preloaded_csets);
2178 struct task_struct *task;
2179 int ret;
2180
2181 /* look up all src csets */
2182 down_read(&css_set_rwsem);
2183 rcu_read_lock();
2184 task = leader;
2185 do {
2186 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2187 &preloaded_csets);
2188 if (!threadgroup)
2189 break;
2190 } while_each_thread(leader, task);
2191 rcu_read_unlock();
2192 up_read(&css_set_rwsem);
2193
2194 /* prepare dst csets and commit */
2195 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2196 if (!ret)
2197 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2198
2199 cgroup_migrate_finish(&preloaded_csets);
2200 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002201}
2202
2203/*
2204 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002205 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002206 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002207 */
2208static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002209{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002210 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002211 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002212 int ret;
2213
Ben Blum74a11662011-05-26 16:25:20 -07002214 if (!cgroup_lock_live_group(cgrp))
2215 return -ENODEV;
2216
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002217retry_find_task:
2218 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002219 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002220 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002221 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002222 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002223 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002224 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002225 }
Ben Blum74a11662011-05-26 16:25:20 -07002226 /*
2227 * even if we're attaching all tasks in the thread group, we
2228 * only need to check permissions on one of them.
2229 */
David Howellsc69e8d92008-11-14 10:39:19 +11002230 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002231 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2232 !uid_eq(cred->euid, tcred->uid) &&
2233 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002234 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002235 ret = -EACCES;
2236 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002237 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002238 } else
2239 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002240
2241 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002242 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002243
2244 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002245 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002246 * trapped in a cpuset, or RT worker may be born in a cgroup
2247 * with no rt_runtime allocated. Just say no.
2248 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002249 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002250 ret = -EINVAL;
2251 rcu_read_unlock();
2252 goto out_unlock_cgroup;
2253 }
2254
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002255 get_task_struct(tsk);
2256 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002257
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002258 threadgroup_lock(tsk);
2259 if (threadgroup) {
2260 if (!thread_group_leader(tsk)) {
2261 /*
2262 * a race with de_thread from another thread's exec()
2263 * may strip us of our leadership, if this happens,
2264 * there is no choice but to throw this task away and
2265 * try again; this is
2266 * "double-double-toil-and-trouble-check locking".
2267 */
2268 threadgroup_unlock(tsk);
2269 put_task_struct(tsk);
2270 goto retry_find_task;
2271 }
Li Zefan081aa452013-03-13 09:17:09 +08002272 }
2273
2274 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2275
Tejun Heocd3d0952011-12-12 18:12:21 -08002276 threadgroup_unlock(tsk);
2277
Paul Menagebbcb81d2007-10-18 23:39:32 -07002278 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002279out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002280 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002281 return ret;
2282}
2283
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002284/**
2285 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2286 * @from: attach to all cgroups of a given task
2287 * @tsk: the task to be attached
2288 */
2289int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2290{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002291 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002292 int retval = 0;
2293
Tejun Heo47cfcd02013-04-07 09:29:51 -07002294 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002295 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002296 struct cgroup *from_cgrp;
2297
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002298 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002299 continue;
2300
Tejun Heo96d365e2014-02-13 06:58:40 -05002301 down_read(&css_set_rwsem);
2302 from_cgrp = task_cgroup_from_root(from, root);
2303 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002304
Li Zefan6f4b7e62013-07-31 16:18:36 +08002305 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002306 if (retval)
2307 break;
2308 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002309 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002310
2311 return retval;
2312}
2313EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2314
Tejun Heo182446d2013-08-08 20:11:24 -04002315static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2316 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002317{
Tejun Heo182446d2013-08-08 20:11:24 -04002318 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002319}
2320
Tejun Heo182446d2013-08-08 20:11:24 -04002321static int cgroup_procs_write(struct cgroup_subsys_state *css,
2322 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002323{
Tejun Heo182446d2013-08-08 20:11:24 -04002324 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002325}
2326
Tejun Heo182446d2013-08-08 20:11:24 -04002327static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
Tejun Heo4d3bb512014-03-19 10:23:54 -04002328 struct cftype *cft, char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002329{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002330 struct cgroup_root *root = css->cgroup->root;
Tejun Heo5f469902014-02-11 11:52:48 -05002331
2332 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002333 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002334 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002335 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002336 strlcpy(root->release_agent_path, buffer,
2337 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002338 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002339 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002340 return 0;
2341}
2342
Tejun Heo2da8ca82013-12-05 12:28:04 -05002343static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002344{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002345 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002346
Paul Menagee788e062008-07-25 01:46:59 -07002347 if (!cgroup_lock_live_group(cgrp))
2348 return -ENODEV;
2349 seq_puts(seq, cgrp->root->release_agent_path);
2350 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002351 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002352 return 0;
2353}
2354
Tejun Heo2da8ca82013-12-05 12:28:04 -05002355static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002356{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002357 struct cgroup *cgrp = seq_css(seq)->cgroup;
2358
2359 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002360 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002361}
2362
Tejun Heof8f22e52014-04-23 11:13:16 -04002363static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2364{
2365 struct cgroup_subsys *ss;
2366 bool printed = false;
2367 int ssid;
2368
2369 for_each_subsys(ss, ssid) {
2370 if (ss_mask & (1 << ssid)) {
2371 if (printed)
2372 seq_putc(seq, ' ');
2373 seq_printf(seq, "%s", ss->name);
2374 printed = true;
2375 }
2376 }
2377 if (printed)
2378 seq_putc(seq, '\n');
2379}
2380
2381/* show controllers which are currently attached to the default hierarchy */
2382static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2383{
2384 struct cgroup *cgrp = seq_css(seq)->cgroup;
2385
2386 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask);
2387 return 0;
2388}
2389
2390/* show controllers which are enabled from the parent */
2391static int cgroup_controllers_show(struct seq_file *seq, void *v)
2392{
2393 struct cgroup *cgrp = seq_css(seq)->cgroup;
2394
2395 cgroup_print_ss_mask(seq, cgrp->parent->child_subsys_mask);
2396 return 0;
2397}
2398
2399/* show controllers which are enabled for a given cgroup's children */
2400static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2401{
2402 struct cgroup *cgrp = seq_css(seq)->cgroup;
2403
2404 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2405 return 0;
2406}
2407
2408/**
2409 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2410 * @cgrp: root of the subtree to update csses for
2411 *
2412 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2413 * css associations need to be updated accordingly. This function looks up
2414 * all css_sets which are attached to the subtree, creates the matching
2415 * updated css_sets and migrates the tasks to the new ones.
2416 */
2417static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2418{
2419 LIST_HEAD(preloaded_csets);
2420 struct cgroup_subsys_state *css;
2421 struct css_set *src_cset;
2422 int ret;
2423
2424 lockdep_assert_held(&cgroup_tree_mutex);
2425 lockdep_assert_held(&cgroup_mutex);
2426
2427 /* look up all csses currently attached to @cgrp's subtree */
2428 down_read(&css_set_rwsem);
2429 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2430 struct cgrp_cset_link *link;
2431
2432 /* self is not affected by child_subsys_mask change */
2433 if (css->cgroup == cgrp)
2434 continue;
2435
2436 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2437 cgroup_migrate_add_src(link->cset, cgrp,
2438 &preloaded_csets);
2439 }
2440 up_read(&css_set_rwsem);
2441
2442 /* NULL dst indicates self on default hierarchy */
2443 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2444 if (ret)
2445 goto out_finish;
2446
2447 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2448 struct task_struct *last_task = NULL, *task;
2449
2450 /* src_csets precede dst_csets, break on the first dst_cset */
2451 if (!src_cset->mg_src_cgrp)
2452 break;
2453
2454 /*
2455 * All tasks in src_cset need to be migrated to the
2456 * matching dst_cset. Empty it process by process. We
2457 * walk tasks but migrate processes. The leader might even
2458 * belong to a different cset but such src_cset would also
2459 * be among the target src_csets because the default
2460 * hierarchy enforces per-process membership.
2461 */
2462 while (true) {
2463 down_read(&css_set_rwsem);
2464 task = list_first_entry_or_null(&src_cset->tasks,
2465 struct task_struct, cg_list);
2466 if (task) {
2467 task = task->group_leader;
2468 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2469 get_task_struct(task);
2470 }
2471 up_read(&css_set_rwsem);
2472
2473 if (!task)
2474 break;
2475
2476 /* guard against possible infinite loop */
2477 if (WARN(last_task == task,
2478 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2479 goto out_finish;
2480 last_task = task;
2481
2482 threadgroup_lock(task);
2483 /* raced against de_thread() from another thread? */
2484 if (!thread_group_leader(task)) {
2485 threadgroup_unlock(task);
2486 put_task_struct(task);
2487 continue;
2488 }
2489
2490 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2491
2492 threadgroup_unlock(task);
2493 put_task_struct(task);
2494
2495 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2496 goto out_finish;
2497 }
2498 }
2499
2500out_finish:
2501 cgroup_migrate_finish(&preloaded_csets);
2502 return ret;
2503}
2504
2505/* change the enabled child controllers for a cgroup in the default hierarchy */
2506static int cgroup_subtree_control_write(struct cgroup_subsys_state *dummy_css,
2507 struct cftype *cft, char *buffer)
2508{
Tejun Heo69dfa002014-05-04 15:09:13 -04002509 unsigned int enable_req = 0, disable_req = 0, enable, disable;
Tejun Heof8f22e52014-04-23 11:13:16 -04002510 struct cgroup *cgrp = dummy_css->cgroup, *child;
2511 struct cgroup_subsys *ss;
2512 char *tok, *p;
2513 int ssid, ret;
2514
2515 /*
2516 * Parse input - white space separated list of subsystem names
2517 * prefixed with either + or -.
2518 */
2519 p = buffer;
2520 while ((tok = strsep(&p, " \t\n"))) {
2521 for_each_subsys(ss, ssid) {
2522 if (ss->disabled || strcmp(tok + 1, ss->name))
2523 continue;
2524
2525 if (*tok == '+') {
2526 enable_req |= 1 << ssid;
2527 disable_req &= ~(1 << ssid);
2528 } else if (*tok == '-') {
2529 disable_req |= 1 << ssid;
2530 enable_req &= ~(1 << ssid);
2531 } else {
2532 return -EINVAL;
2533 }
2534 break;
2535 }
2536 if (ssid == CGROUP_SUBSYS_COUNT)
2537 return -EINVAL;
2538 }
2539
2540 /*
2541 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2542 * active_ref. cgroup_lock_live_group() already provides enough
2543 * protection. Ensure @cgrp stays accessible and break the
2544 * active_ref protection.
2545 */
2546 cgroup_get(cgrp);
2547 kernfs_break_active_protection(cgrp->control_kn);
2548retry:
2549 enable = enable_req;
2550 disable = disable_req;
2551
2552 mutex_lock(&cgroup_tree_mutex);
2553
2554 for_each_subsys(ss, ssid) {
2555 if (enable & (1 << ssid)) {
2556 if (cgrp->child_subsys_mask & (1 << ssid)) {
2557 enable &= ~(1 << ssid);
2558 continue;
2559 }
2560
2561 /*
2562 * Because css offlining is asynchronous, userland
2563 * might try to re-enable the same controller while
2564 * the previous instance is still around. In such
2565 * cases, wait till it's gone using offline_waitq.
2566 */
2567 cgroup_for_each_live_child(child, cgrp) {
2568 wait_queue_t wait;
2569
2570 if (!cgroup_css(child, ss))
2571 continue;
2572
2573 prepare_to_wait(&child->offline_waitq, &wait,
2574 TASK_UNINTERRUPTIBLE);
2575 mutex_unlock(&cgroup_tree_mutex);
2576 schedule();
2577 finish_wait(&child->offline_waitq, &wait);
2578 goto retry;
2579 }
2580
2581 /* unavailable or not enabled on the parent? */
2582 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2583 (cgrp->parent &&
2584 !(cgrp->parent->child_subsys_mask & (1 << ssid)))) {
2585 ret = -ENOENT;
2586 goto out_unlock_tree;
2587 }
2588 } else if (disable & (1 << ssid)) {
2589 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2590 disable &= ~(1 << ssid);
2591 continue;
2592 }
2593
2594 /* a child has it enabled? */
2595 cgroup_for_each_live_child(child, cgrp) {
2596 if (child->child_subsys_mask & (1 << ssid)) {
2597 ret = -EBUSY;
2598 goto out_unlock_tree;
2599 }
2600 }
2601 }
2602 }
2603
2604 if (!enable && !disable) {
2605 ret = 0;
2606 goto out_unlock_tree;
2607 }
2608
2609 if (!cgroup_lock_live_group(cgrp)) {
2610 ret = -ENODEV;
2611 goto out_unlock_tree;
2612 }
2613
2614 /*
2615 * Except for the root, child_subsys_mask must be zero for a cgroup
2616 * with tasks so that child cgroups don't compete against tasks.
2617 */
2618 if (enable && cgrp->parent && !list_empty(&cgrp->cset_links)) {
2619 ret = -EBUSY;
2620 goto out_unlock;
2621 }
2622
2623 /*
2624 * Create csses for enables and update child_subsys_mask. This
2625 * changes cgroup_e_css() results which in turn makes the
2626 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2627 * subtree to the updated csses.
2628 */
2629 for_each_subsys(ss, ssid) {
2630 if (!(enable & (1 << ssid)))
2631 continue;
2632
2633 cgroup_for_each_live_child(child, cgrp) {
2634 ret = create_css(child, ss);
2635 if (ret)
2636 goto err_undo_css;
2637 }
2638 }
2639
2640 cgrp->child_subsys_mask |= enable;
2641 cgrp->child_subsys_mask &= ~disable;
2642
2643 ret = cgroup_update_dfl_csses(cgrp);
2644 if (ret)
2645 goto err_undo_css;
2646
2647 /* all tasks are now migrated away from the old csses, kill them */
2648 for_each_subsys(ss, ssid) {
2649 if (!(disable & (1 << ssid)))
2650 continue;
2651
2652 cgroup_for_each_live_child(child, cgrp)
2653 kill_css(cgroup_css(child, ss));
2654 }
2655
2656 kernfs_activate(cgrp->kn);
2657 ret = 0;
2658out_unlock:
2659 mutex_unlock(&cgroup_mutex);
2660out_unlock_tree:
2661 mutex_unlock(&cgroup_tree_mutex);
2662 kernfs_unbreak_active_protection(cgrp->control_kn);
2663 cgroup_put(cgrp);
2664 return ret;
2665
2666err_undo_css:
2667 cgrp->child_subsys_mask &= ~enable;
2668 cgrp->child_subsys_mask |= disable;
2669
2670 for_each_subsys(ss, ssid) {
2671 if (!(enable & (1 << ssid)))
2672 continue;
2673
2674 cgroup_for_each_live_child(child, cgrp) {
2675 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2676 if (css)
2677 kill_css(css);
2678 }
2679 }
2680 goto out_unlock;
2681}
2682
Tejun Heo842b5972014-04-25 18:28:02 -04002683static int cgroup_populated_show(struct seq_file *seq, void *v)
2684{
2685 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2686 return 0;
2687}
2688
Tejun Heo2bd59d42014-02-11 11:52:49 -05002689static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2690 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002691{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002692 struct cgroup *cgrp = of->kn->parent->priv;
2693 struct cftype *cft = of->kn->priv;
2694 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002695 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002696
Tejun Heo2bd59d42014-02-11 11:52:49 -05002697 /*
2698 * kernfs guarantees that a file isn't deleted with operations in
2699 * flight, which means that the matching css is and stays alive and
2700 * doesn't need to be pinned. The RCU locking is not necessary
2701 * either. It's just for the convenience of using cgroup_css().
2702 */
2703 rcu_read_lock();
2704 css = cgroup_css(cgrp, cft->ss);
2705 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002706
Tejun Heoa742c592013-12-05 12:28:03 -05002707 if (cft->write_string) {
2708 ret = cft->write_string(css, cft, strstrip(buf));
2709 } else if (cft->write_u64) {
2710 unsigned long long v;
2711 ret = kstrtoull(buf, 0, &v);
2712 if (!ret)
2713 ret = cft->write_u64(css, cft, v);
2714 } else if (cft->write_s64) {
2715 long long v;
2716 ret = kstrtoll(buf, 0, &v);
2717 if (!ret)
2718 ret = cft->write_s64(css, cft, v);
2719 } else if (cft->trigger) {
2720 ret = cft->trigger(css, (unsigned int)cft->private);
2721 } else {
2722 ret = -EINVAL;
2723 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002724
Tejun Heoa742c592013-12-05 12:28:03 -05002725 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002726}
2727
Tejun Heo6612f052013-12-05 12:28:04 -05002728static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002729{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002730 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002731}
2732
2733static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2734{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002735 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002736}
2737
2738static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2739{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002740 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002741}
2742
2743static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2744{
Tejun Heo7da11272013-12-05 12:28:04 -05002745 struct cftype *cft = seq_cft(m);
2746 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002747
Tejun Heo2da8ca82013-12-05 12:28:04 -05002748 if (cft->seq_show)
2749 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002750
Tejun Heo896f5192013-12-05 12:28:04 -05002751 if (cft->read_u64)
2752 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2753 else if (cft->read_s64)
2754 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2755 else
2756 return -EINVAL;
2757 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002758}
2759
Tejun Heo2bd59d42014-02-11 11:52:49 -05002760static struct kernfs_ops cgroup_kf_single_ops = {
2761 .atomic_write_len = PAGE_SIZE,
2762 .write = cgroup_file_write,
2763 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002764};
2765
Tejun Heo2bd59d42014-02-11 11:52:49 -05002766static struct kernfs_ops cgroup_kf_ops = {
2767 .atomic_write_len = PAGE_SIZE,
2768 .write = cgroup_file_write,
2769 .seq_start = cgroup_seqfile_start,
2770 .seq_next = cgroup_seqfile_next,
2771 .seq_stop = cgroup_seqfile_stop,
2772 .seq_show = cgroup_seqfile_show,
2773};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002774
2775/*
2776 * cgroup_rename - Only allow simple rename of directories in place.
2777 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002778static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2779 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002780{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002781 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002782 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002783
Tejun Heo2bd59d42014-02-11 11:52:49 -05002784 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002785 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002786 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002787 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002788
Tejun Heo6db8e852013-06-14 11:18:22 -07002789 /*
2790 * This isn't a proper migration and its usefulness is very
2791 * limited. Disallow if sane_behavior.
2792 */
2793 if (cgroup_sane_behavior(cgrp))
2794 return -EPERM;
2795
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002796 /*
2797 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2798 * active_ref. kernfs_rename() doesn't require active_ref
2799 * protection. Break them before grabbing cgroup_tree_mutex.
2800 */
2801 kernfs_break_active_protection(new_parent);
2802 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002803
Tejun Heo2bd59d42014-02-11 11:52:49 -05002804 mutex_lock(&cgroup_tree_mutex);
2805 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002806
Tejun Heo2bd59d42014-02-11 11:52:49 -05002807 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002808
Tejun Heo2bd59d42014-02-11 11:52:49 -05002809 mutex_unlock(&cgroup_mutex);
2810 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002811
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002812 kernfs_unbreak_active_protection(kn);
2813 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002814 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002815}
2816
Tejun Heo49957f82014-04-07 16:44:47 -04002817/* set uid and gid of cgroup dirs and files to that of the creator */
2818static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2819{
2820 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2821 .ia_uid = current_fsuid(),
2822 .ia_gid = current_fsgid(), };
2823
2824 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2825 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2826 return 0;
2827
2828 return kernfs_setattr(kn, &iattr);
2829}
2830
Tejun Heo2bb566c2013-08-08 20:11:23 -04002831static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002832{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002833 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002834 struct kernfs_node *kn;
2835 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002836 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002837
Tejun Heo2bd59d42014-02-11 11:52:49 -05002838#ifdef CONFIG_DEBUG_LOCK_ALLOC
2839 key = &cft->lockdep_key;
2840#endif
2841 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2842 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2843 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002844 if (IS_ERR(kn))
2845 return PTR_ERR(kn);
2846
2847 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002848 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002849 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002850 return ret;
2851 }
2852
2853 if (cft->seq_show == cgroup_subtree_control_show)
2854 cgrp->control_kn = kn;
Tejun Heo842b5972014-04-25 18:28:02 -04002855 else if (cft->seq_show == cgroup_populated_show)
2856 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002857 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002858}
2859
Tejun Heob1f28d32013-06-28 16:24:10 -07002860/**
2861 * cgroup_addrm_files - add or remove files to a cgroup directory
2862 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002863 * @cfts: array of cftypes to be added
2864 * @is_add: whether to add or remove
2865 *
2866 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002867 * For removals, this function never fails. If addition fails, this
2868 * function doesn't remove files already added. The caller is responsible
2869 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002870 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002871static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2872 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002873{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002874 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002875 int ret;
2876
Tejun Heoace2bee2014-02-11 11:52:47 -05002877 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002878
2879 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002880 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002881 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2882 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002883 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2884 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002885 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2886 continue;
2887 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2888 continue;
2889
Li Zefan2739d3c2013-01-21 18:18:33 +08002890 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002891 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002892 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04002893 pr_warn("%s: failed to add %s, err=%d\n",
2894 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07002895 return ret;
2896 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002897 } else {
2898 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002899 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002900 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002901 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002902}
2903
Tejun Heo21a2d342014-02-12 09:29:49 -05002904static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002905{
2906 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002907 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002908 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002909 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002910 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002911
Tejun Heo21a2d342014-02-12 09:29:49 -05002912 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002913
Li Zefane8c82d22013-06-18 18:48:37 +08002914 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002915 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002916 struct cgroup *cgrp = css->cgroup;
2917
Li Zefane8c82d22013-06-18 18:48:37 +08002918 if (cgroup_is_dead(cgrp))
2919 continue;
2920
Tejun Heo21a2d342014-02-12 09:29:49 -05002921 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002922 if (ret)
2923 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002924 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002925
2926 if (is_add && !ret)
2927 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002928 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002929}
2930
Tejun Heo2da440a2014-02-11 11:52:48 -05002931static void cgroup_exit_cftypes(struct cftype *cfts)
2932{
2933 struct cftype *cft;
2934
Tejun Heo2bd59d42014-02-11 11:52:49 -05002935 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2936 /* free copy for custom atomic_write_len, see init_cftypes() */
2937 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2938 kfree(cft->kf_ops);
2939 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002940 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002941 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002942}
2943
Tejun Heo2bd59d42014-02-11 11:52:49 -05002944static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002945{
2946 struct cftype *cft;
2947
Tejun Heo2bd59d42014-02-11 11:52:49 -05002948 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2949 struct kernfs_ops *kf_ops;
2950
Tejun Heo0adb0702014-02-12 09:29:48 -05002951 WARN_ON(cft->ss || cft->kf_ops);
2952
Tejun Heo2bd59d42014-02-11 11:52:49 -05002953 if (cft->seq_start)
2954 kf_ops = &cgroup_kf_ops;
2955 else
2956 kf_ops = &cgroup_kf_single_ops;
2957
2958 /*
2959 * Ugh... if @cft wants a custom max_write_len, we need to
2960 * make a copy of kf_ops to set its atomic_write_len.
2961 */
2962 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2963 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2964 if (!kf_ops) {
2965 cgroup_exit_cftypes(cfts);
2966 return -ENOMEM;
2967 }
2968 kf_ops->atomic_write_len = cft->max_write_len;
2969 }
2970
2971 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002972 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002973 }
2974
2975 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002976}
2977
Tejun Heo21a2d342014-02-12 09:29:49 -05002978static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2979{
2980 lockdep_assert_held(&cgroup_tree_mutex);
2981
2982 if (!cfts || !cfts[0].ss)
2983 return -ENOENT;
2984
2985 list_del(&cfts->node);
2986 cgroup_apply_cftypes(cfts, false);
2987 cgroup_exit_cftypes(cfts);
2988 return 0;
2989}
2990
Tejun Heo8e3f6542012-04-01 12:09:55 -07002991/**
Tejun Heo80b13582014-02-12 09:29:48 -05002992 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2993 * @cfts: zero-length name terminated array of cftypes
2994 *
2995 * Unregister @cfts. Files described by @cfts are removed from all
2996 * existing cgroups and all future cgroups won't have them either. This
2997 * function can be called anytime whether @cfts' subsys is attached or not.
2998 *
2999 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3000 * registered.
3001 */
3002int cgroup_rm_cftypes(struct cftype *cfts)
3003{
Tejun Heo21a2d342014-02-12 09:29:49 -05003004 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003005
Tejun Heo21a2d342014-02-12 09:29:49 -05003006 mutex_lock(&cgroup_tree_mutex);
3007 ret = cgroup_rm_cftypes_locked(cfts);
3008 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003009 return ret;
3010}
3011
3012/**
3013 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3014 * @ss: target cgroup subsystem
3015 * @cfts: zero-length name terminated array of cftypes
3016 *
3017 * Register @cfts to @ss. Files described by @cfts are created for all
3018 * existing cgroups to which @ss is attached and all future cgroups will
3019 * have them too. This function can be called anytime whether @ss is
3020 * attached or not.
3021 *
3022 * Returns 0 on successful registration, -errno on failure. Note that this
3023 * function currently returns 0 as long as @cfts registration is successful
3024 * even if some file creation attempts on existing cgroups fail.
3025 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003026int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003027{
Tejun Heo9ccece82013-06-28 16:24:11 -07003028 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003029
Li Zefandc5736e2014-02-17 10:41:50 +08003030 if (!cfts || cfts[0].name[0] == '\0')
3031 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003032
Tejun Heo2bd59d42014-02-11 11:52:49 -05003033 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003034 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003035 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003036
Tejun Heo21a2d342014-02-12 09:29:49 -05003037 mutex_lock(&cgroup_tree_mutex);
3038
Tejun Heo0adb0702014-02-12 09:29:48 -05003039 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003040 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003041 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003042 cgroup_rm_cftypes_locked(cfts);
3043
3044 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003045 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003046}
Tejun Heo79578622012-04-01 12:09:56 -07003047
3048/**
Li Zefana043e3b2008-02-23 15:24:09 -08003049 * cgroup_task_count - count the number of tasks in a cgroup.
3050 * @cgrp: the cgroup in question
3051 *
3052 * Return the number of tasks in the cgroup.
3053 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003054static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003055{
3056 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003057 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003058
Tejun Heo96d365e2014-02-13 06:58:40 -05003059 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003060 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3061 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003062 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003063 return count;
3064}
3065
Tejun Heo574bd9f2012-11-09 09:12:29 -08003066/**
Tejun Heo492eb212013-08-08 20:11:25 -04003067 * css_next_child - find the next child of a given css
3068 * @pos_css: the current position (%NULL to initiate traversal)
3069 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003070 *
Tejun Heo492eb212013-08-08 20:11:25 -04003071 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003072 * under either cgroup_mutex or RCU read lock. The only requirement is
3073 * that @parent_css and @pos_css are accessible. The next sibling is
3074 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003075 */
Tejun Heo492eb212013-08-08 20:11:25 -04003076struct cgroup_subsys_state *
3077css_next_child(struct cgroup_subsys_state *pos_css,
3078 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003079{
Tejun Heo492eb212013-08-08 20:11:25 -04003080 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3081 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003082 struct cgroup *next;
3083
Tejun Heoace2bee2014-02-11 11:52:47 -05003084 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003085
3086 /*
3087 * @pos could already have been removed. Once a cgroup is removed,
3088 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003089 * changes. As CGRP_DEAD assertion is serialized and happens
3090 * before the cgroup is taken off the ->sibling list, if we see it
3091 * unasserted, it's guaranteed that the next sibling hasn't
3092 * finished its grace period even if it's already removed, and thus
3093 * safe to dereference from this RCU critical section. If
3094 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3095 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003096 *
3097 * If @pos is dead, its next pointer can't be dereferenced;
3098 * however, as each cgroup is given a monotonically increasing
3099 * unique serial number and always appended to the sibling list,
3100 * the next one can be found by walking the parent's children until
3101 * we see a cgroup with higher serial number than @pos's. While
3102 * this path can be slower, it's taken only when either the current
3103 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003104 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003105 if (!pos) {
3106 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3107 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003108 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003109 } else {
3110 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3111 if (next->serial_nr > pos->serial_nr)
3112 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003113 }
3114
Tejun Heo3b281af2014-04-23 11:13:15 -04003115 /*
3116 * @next, if not pointing to the head, can be dereferenced and is
3117 * the next sibling; however, it might have @ss disabled. If so,
3118 * fast-forward to the next enabled one.
3119 */
3120 while (&next->sibling != &cgrp->children) {
3121 struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
Tejun Heo492eb212013-08-08 20:11:25 -04003122
Tejun Heo3b281af2014-04-23 11:13:15 -04003123 if (next_css)
3124 return next_css;
3125 next = list_entry_rcu(next->sibling.next, struct cgroup, sibling);
3126 }
3127 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003128}
Tejun Heo53fa5262013-05-24 10:55:38 +09003129
3130/**
Tejun Heo492eb212013-08-08 20:11:25 -04003131 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003132 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003133 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003134 *
Tejun Heo492eb212013-08-08 20:11:25 -04003135 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003136 * to visit for pre-order traversal of @root's descendants. @root is
3137 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003138 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003139 * While this function requires cgroup_mutex or RCU read locking, it
3140 * doesn't require the whole traversal to be contained in a single critical
3141 * section. This function will return the correct next descendant as long
3142 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003143 */
Tejun Heo492eb212013-08-08 20:11:25 -04003144struct cgroup_subsys_state *
3145css_next_descendant_pre(struct cgroup_subsys_state *pos,
3146 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003147{
Tejun Heo492eb212013-08-08 20:11:25 -04003148 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003149
Tejun Heoace2bee2014-02-11 11:52:47 -05003150 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003151
Tejun Heobd8815a2013-08-08 20:11:27 -04003152 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003153 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003154 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003155
3156 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003157 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003158 if (next)
3159 return next;
3160
3161 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003162 while (pos != root) {
3163 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003164 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003165 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003166 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003167 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003168
3169 return NULL;
3170}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003171
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003172/**
Tejun Heo492eb212013-08-08 20:11:25 -04003173 * css_rightmost_descendant - return the rightmost descendant of a css
3174 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003175 *
Tejun Heo492eb212013-08-08 20:11:25 -04003176 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3177 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003178 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003179 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003180 * While this function requires cgroup_mutex or RCU read locking, it
3181 * doesn't require the whole traversal to be contained in a single critical
3182 * section. This function will return the correct rightmost descendant as
3183 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003184 */
Tejun Heo492eb212013-08-08 20:11:25 -04003185struct cgroup_subsys_state *
3186css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003187{
Tejun Heo492eb212013-08-08 20:11:25 -04003188 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003189
Tejun Heoace2bee2014-02-11 11:52:47 -05003190 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003191
3192 do {
3193 last = pos;
3194 /* ->prev isn't RCU safe, walk ->next till the end */
3195 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003196 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003197 pos = tmp;
3198 } while (pos);
3199
3200 return last;
3201}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003202
Tejun Heo492eb212013-08-08 20:11:25 -04003203static struct cgroup_subsys_state *
3204css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003205{
Tejun Heo492eb212013-08-08 20:11:25 -04003206 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003207
3208 do {
3209 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003210 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003211 } while (pos);
3212
3213 return last;
3214}
3215
3216/**
Tejun Heo492eb212013-08-08 20:11:25 -04003217 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003218 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003219 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003220 *
Tejun Heo492eb212013-08-08 20:11:25 -04003221 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003222 * to visit for post-order traversal of @root's descendants. @root is
3223 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003224 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003225 * While this function requires cgroup_mutex or RCU read locking, it
3226 * doesn't require the whole traversal to be contained in a single critical
3227 * section. This function will return the correct next descendant as long
3228 * as both @pos and @cgroup are accessible and @pos is a descendant of
3229 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003230 */
Tejun Heo492eb212013-08-08 20:11:25 -04003231struct cgroup_subsys_state *
3232css_next_descendant_post(struct cgroup_subsys_state *pos,
3233 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003234{
Tejun Heo492eb212013-08-08 20:11:25 -04003235 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003236
Tejun Heoace2bee2014-02-11 11:52:47 -05003237 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003238
Tejun Heo58b79a92013-09-06 15:31:08 -04003239 /* if first iteration, visit leftmost descendant which may be @root */
3240 if (!pos)
3241 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003242
Tejun Heobd8815a2013-08-08 20:11:27 -04003243 /* if we visited @root, we're done */
3244 if (pos == root)
3245 return NULL;
3246
Tejun Heo574bd9f2012-11-09 09:12:29 -08003247 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003248 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003249 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003250 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003251
3252 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003253 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003254}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003255
Tejun Heo0942eee2013-08-08 20:11:26 -04003256/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003257 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003258 * @it: the iterator to advance
3259 *
3260 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003261 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003262static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003263{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003264 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003265 struct cgrp_cset_link *link;
3266 struct css_set *cset;
3267
3268 /* Advance to the next non-empty css_set */
3269 do {
3270 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003271 if (l == it->cset_head) {
3272 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003273 return;
3274 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003275
3276 if (it->ss) {
3277 cset = container_of(l, struct css_set,
3278 e_cset_node[it->ss->id]);
3279 } else {
3280 link = list_entry(l, struct cgrp_cset_link, cset_link);
3281 cset = link->cset;
3282 }
Tejun Heoc7561122014-02-25 10:04:01 -05003283 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3284
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003285 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003286
3287 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003288 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003289 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003290 it->task_pos = cset->mg_tasks.next;
3291
3292 it->tasks_head = &cset->tasks;
3293 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003294}
3295
Tejun Heo0942eee2013-08-08 20:11:26 -04003296/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003297 * css_task_iter_start - initiate task iteration
3298 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003299 * @it: the task iterator to use
3300 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003301 * Initiate iteration through the tasks of @css. The caller can call
3302 * css_task_iter_next() to walk through the tasks until the function
3303 * returns NULL. On completion of iteration, css_task_iter_end() must be
3304 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003305 *
3306 * Note that this function acquires a lock which is released when the
3307 * iteration finishes. The caller can't sleep while iteration is in
3308 * progress.
3309 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003310void css_task_iter_start(struct cgroup_subsys_state *css,
3311 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003312 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003313{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003314 /* no one should try to iterate before mounting cgroups */
3315 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003316
Tejun Heo96d365e2014-02-13 06:58:40 -05003317 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003318
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003319 it->ss = css->ss;
3320
3321 if (it->ss)
3322 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3323 else
3324 it->cset_pos = &css->cgroup->cset_links;
3325
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003326 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003327
Tejun Heo72ec7022013-08-08 20:11:26 -04003328 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003329}
3330
Tejun Heo0942eee2013-08-08 20:11:26 -04003331/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003332 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003333 * @it: the task iterator being iterated
3334 *
3335 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003336 * initialized via css_task_iter_start(). Returns NULL when the iteration
3337 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003338 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003339struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003340{
3341 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003342 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003343
3344 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003345 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003346 return NULL;
3347 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003348
3349 /*
3350 * Advance iterator to find next entry. cset->tasks is consumed
3351 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3352 * next cset.
3353 */
Paul Menage817929e2007-10-18 23:39:36 -07003354 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003355
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003356 if (l == it->tasks_head)
3357 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003358
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003359 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003360 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003361 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003362 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003363
Paul Menage817929e2007-10-18 23:39:36 -07003364 return res;
3365}
3366
Tejun Heo0942eee2013-08-08 20:11:26 -04003367/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003368 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003369 * @it: the task iterator to finish
3370 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003371 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003372 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003373void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003374 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003375{
Tejun Heo96d365e2014-02-13 06:58:40 -05003376 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003377}
3378
3379/**
3380 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3381 * @to: cgroup to which the tasks will be moved
3382 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003383 *
3384 * Locking rules between cgroup_post_fork() and the migration path
3385 * guarantee that, if a task is forking while being migrated, the new child
3386 * is guaranteed to be either visible in the source cgroup after the
3387 * parent's migration is complete or put into the target cgroup. No task
3388 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003389 */
3390int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3391{
Tejun Heo952aaa12014-02-25 10:04:03 -05003392 LIST_HEAD(preloaded_csets);
3393 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003394 struct css_task_iter it;
3395 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003396 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003397
Tejun Heo952aaa12014-02-25 10:04:03 -05003398 mutex_lock(&cgroup_mutex);
3399
3400 /* all tasks in @from are being moved, all csets are source */
3401 down_read(&css_set_rwsem);
3402 list_for_each_entry(link, &from->cset_links, cset_link)
3403 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3404 up_read(&css_set_rwsem);
3405
3406 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3407 if (ret)
3408 goto out_err;
3409
3410 /*
3411 * Migrate tasks one-by-one until @form is empty. This fails iff
3412 * ->can_attach() fails.
3413 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003414 do {
3415 css_task_iter_start(&from->dummy_css, &it);
3416 task = css_task_iter_next(&it);
3417 if (task)
3418 get_task_struct(task);
3419 css_task_iter_end(&it);
3420
3421 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003422 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003423 put_task_struct(task);
3424 }
3425 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003426out_err:
3427 cgroup_migrate_finish(&preloaded_csets);
3428 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003429 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003430}
3431
Paul Menage817929e2007-10-18 23:39:36 -07003432/*
Ben Blum102a7752009-09-23 15:56:26 -07003433 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003434 *
3435 * Reading this file can return large amounts of data if a cgroup has
3436 * *lots* of attached tasks. So it may need several calls to read(),
3437 * but we cannot guarantee that the information we produce is correct
3438 * unless we produce it entirely atomically.
3439 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003440 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003441
Li Zefan24528252012-01-20 11:58:43 +08003442/* which pidlist file are we talking about? */
3443enum cgroup_filetype {
3444 CGROUP_FILE_PROCS,
3445 CGROUP_FILE_TASKS,
3446};
3447
3448/*
3449 * A pidlist is a list of pids that virtually represents the contents of one
3450 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3451 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3452 * to the cgroup.
3453 */
3454struct cgroup_pidlist {
3455 /*
3456 * used to find which pidlist is wanted. doesn't change as long as
3457 * this particular list stays in the list.
3458 */
3459 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3460 /* array of xids */
3461 pid_t *list;
3462 /* how many elements the above list has */
3463 int length;
Li Zefan24528252012-01-20 11:58:43 +08003464 /* each of these stored in a list by its cgroup */
3465 struct list_head links;
3466 /* pointer to the cgroup we belong to, for list removal purposes */
3467 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003468 /* for delayed destruction */
3469 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003470};
3471
Paul Menagebbcb81d2007-10-18 23:39:32 -07003472/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003473 * The following two functions "fix" the issue where there are more pids
3474 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3475 * TODO: replace with a kernel-wide solution to this problem
3476 */
3477#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3478static void *pidlist_allocate(int count)
3479{
3480 if (PIDLIST_TOO_LARGE(count))
3481 return vmalloc(count * sizeof(pid_t));
3482 else
3483 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3484}
Tejun Heob1a21362013-11-29 10:42:58 -05003485
Ben Blumd1d9fd32009-09-23 15:56:28 -07003486static void pidlist_free(void *p)
3487{
3488 if (is_vmalloc_addr(p))
3489 vfree(p);
3490 else
3491 kfree(p);
3492}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003493
3494/*
Tejun Heob1a21362013-11-29 10:42:58 -05003495 * Used to destroy all pidlists lingering waiting for destroy timer. None
3496 * should be left afterwards.
3497 */
3498static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3499{
3500 struct cgroup_pidlist *l, *tmp_l;
3501
3502 mutex_lock(&cgrp->pidlist_mutex);
3503 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3504 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3505 mutex_unlock(&cgrp->pidlist_mutex);
3506
3507 flush_workqueue(cgroup_pidlist_destroy_wq);
3508 BUG_ON(!list_empty(&cgrp->pidlists));
3509}
3510
3511static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3512{
3513 struct delayed_work *dwork = to_delayed_work(work);
3514 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3515 destroy_dwork);
3516 struct cgroup_pidlist *tofree = NULL;
3517
3518 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003519
3520 /*
Tejun Heo04502362013-11-29 10:42:59 -05003521 * Destroy iff we didn't get queued again. The state won't change
3522 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003523 */
Tejun Heo04502362013-11-29 10:42:59 -05003524 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003525 list_del(&l->links);
3526 pidlist_free(l->list);
3527 put_pid_ns(l->key.ns);
3528 tofree = l;
3529 }
3530
Tejun Heob1a21362013-11-29 10:42:58 -05003531 mutex_unlock(&l->owner->pidlist_mutex);
3532 kfree(tofree);
3533}
3534
3535/*
Ben Blum102a7752009-09-23 15:56:26 -07003536 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003537 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003538 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003539static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003540{
Ben Blum102a7752009-09-23 15:56:26 -07003541 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003542
3543 /*
3544 * we presume the 0th element is unique, so i starts at 1. trivial
3545 * edge cases first; no work needs to be done for either
3546 */
3547 if (length == 0 || length == 1)
3548 return length;
3549 /* src and dest walk down the list; dest counts unique elements */
3550 for (src = 1; src < length; src++) {
3551 /* find next unique element */
3552 while (list[src] == list[src-1]) {
3553 src++;
3554 if (src == length)
3555 goto after;
3556 }
3557 /* dest always points to where the next unique element goes */
3558 list[dest] = list[src];
3559 dest++;
3560 }
3561after:
Ben Blum102a7752009-09-23 15:56:26 -07003562 return dest;
3563}
3564
Tejun Heoafb2bc12013-11-29 10:42:59 -05003565/*
3566 * The two pid files - task and cgroup.procs - guaranteed that the result
3567 * is sorted, which forced this whole pidlist fiasco. As pid order is
3568 * different per namespace, each namespace needs differently sorted list,
3569 * making it impossible to use, for example, single rbtree of member tasks
3570 * sorted by task pointer. As pidlists can be fairly large, allocating one
3571 * per open file is dangerous, so cgroup had to implement shared pool of
3572 * pidlists keyed by cgroup and namespace.
3573 *
3574 * All this extra complexity was caused by the original implementation
3575 * committing to an entirely unnecessary property. In the long term, we
3576 * want to do away with it. Explicitly scramble sort order if
3577 * sane_behavior so that no such expectation exists in the new interface.
3578 *
3579 * Scrambling is done by swapping every two consecutive bits, which is
3580 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3581 */
3582static pid_t pid_fry(pid_t pid)
3583{
3584 unsigned a = pid & 0x55555555;
3585 unsigned b = pid & 0xAAAAAAAA;
3586
3587 return (a << 1) | (b >> 1);
3588}
3589
3590static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3591{
3592 if (cgroup_sane_behavior(cgrp))
3593 return pid_fry(pid);
3594 else
3595 return pid;
3596}
3597
Ben Blum102a7752009-09-23 15:56:26 -07003598static int cmppid(const void *a, const void *b)
3599{
3600 return *(pid_t *)a - *(pid_t *)b;
3601}
3602
Tejun Heoafb2bc12013-11-29 10:42:59 -05003603static int fried_cmppid(const void *a, const void *b)
3604{
3605 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3606}
3607
Ben Blum72a8cb32009-09-23 15:56:27 -07003608static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3609 enum cgroup_filetype type)
3610{
3611 struct cgroup_pidlist *l;
3612 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003613 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003614
Tejun Heoe6b81712013-11-29 10:42:59 -05003615 lockdep_assert_held(&cgrp->pidlist_mutex);
3616
3617 list_for_each_entry(l, &cgrp->pidlists, links)
3618 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003619 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003620 return NULL;
3621}
3622
Ben Blum72a8cb32009-09-23 15:56:27 -07003623/*
3624 * find the appropriate pidlist for our purpose (given procs vs tasks)
3625 * returns with the lock on that pidlist already held, and takes care
3626 * of the use count, or returns NULL with no locks held if we're out of
3627 * memory.
3628 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003629static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3630 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003631{
3632 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003633
Tejun Heoe6b81712013-11-29 10:42:59 -05003634 lockdep_assert_held(&cgrp->pidlist_mutex);
3635
3636 l = cgroup_pidlist_find(cgrp, type);
3637 if (l)
3638 return l;
3639
Ben Blum72a8cb32009-09-23 15:56:27 -07003640 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003641 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003642 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003643 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003644
Tejun Heob1a21362013-11-29 10:42:58 -05003645 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003646 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003647 /* don't need task_nsproxy() if we're looking at ourself */
3648 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003649 l->owner = cgrp;
3650 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003651 return l;
3652}
3653
3654/*
Ben Blum102a7752009-09-23 15:56:26 -07003655 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3656 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003657static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3658 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003659{
3660 pid_t *array;
3661 int length;
3662 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003663 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003664 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003665 struct cgroup_pidlist *l;
3666
Tejun Heo4bac00d2013-11-29 10:42:59 -05003667 lockdep_assert_held(&cgrp->pidlist_mutex);
3668
Ben Blum102a7752009-09-23 15:56:26 -07003669 /*
3670 * If cgroup gets more users after we read count, we won't have
3671 * enough space - tough. This race is indistinguishable to the
3672 * caller from the case that the additional cgroup users didn't
3673 * show up until sometime later on.
3674 */
3675 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003676 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003677 if (!array)
3678 return -ENOMEM;
3679 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003680 css_task_iter_start(&cgrp->dummy_css, &it);
3681 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003682 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003683 break;
Ben Blum102a7752009-09-23 15:56:26 -07003684 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003685 if (type == CGROUP_FILE_PROCS)
3686 pid = task_tgid_vnr(tsk);
3687 else
3688 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003689 if (pid > 0) /* make sure to only use valid results */
3690 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003691 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003692 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003693 length = n;
3694 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003695 if (cgroup_sane_behavior(cgrp))
3696 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3697 else
3698 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003699 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003700 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003701
Tejun Heoe6b81712013-11-29 10:42:59 -05003702 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003703 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003704 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003705 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003706 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003707 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003708
3709 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003710 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003711 l->list = array;
3712 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003713 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003714 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003715}
3716
Balbir Singh846c7bb2007-10-18 23:39:44 -07003717/**
Li Zefana043e3b2008-02-23 15:24:09 -08003718 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003719 * @stats: cgroupstats to fill information into
3720 * @dentry: A dentry entry belonging to the cgroup for which stats have
3721 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003722 *
3723 * Build and fill cgroupstats so that taskstats can export it to user
3724 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003725 */
3726int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3727{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003728 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003729 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003730 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003731 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003732
Tejun Heo2bd59d42014-02-11 11:52:49 -05003733 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3734 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3735 kernfs_type(kn) != KERNFS_DIR)
3736 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003737
Li Zefanbad34662014-02-14 16:54:28 +08003738 mutex_lock(&cgroup_mutex);
3739
Tejun Heo2bd59d42014-02-11 11:52:49 -05003740 /*
3741 * We aren't being called from kernfs and there's no guarantee on
3742 * @kn->priv's validity. For this and css_tryget_from_dir(),
3743 * @kn->priv is RCU safe. Let's do the RCU dancing.
3744 */
3745 rcu_read_lock();
3746 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003747 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003748 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003749 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003750 return -ENOENT;
3751 }
Li Zefanbad34662014-02-14 16:54:28 +08003752 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003753
Tejun Heo72ec7022013-08-08 20:11:26 -04003754 css_task_iter_start(&cgrp->dummy_css, &it);
3755 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003756 switch (tsk->state) {
3757 case TASK_RUNNING:
3758 stats->nr_running++;
3759 break;
3760 case TASK_INTERRUPTIBLE:
3761 stats->nr_sleeping++;
3762 break;
3763 case TASK_UNINTERRUPTIBLE:
3764 stats->nr_uninterruptible++;
3765 break;
3766 case TASK_STOPPED:
3767 stats->nr_stopped++;
3768 break;
3769 default:
3770 if (delayacct_is_task_waiting_on_io(tsk))
3771 stats->nr_io_wait++;
3772 break;
3773 }
3774 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003775 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003776
Li Zefanbad34662014-02-14 16:54:28 +08003777 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003778 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003779}
3780
Paul Menage8f3ff202009-09-23 15:56:25 -07003781
Paul Menagecc31edc2008-10-18 20:28:04 -07003782/*
Ben Blum102a7752009-09-23 15:56:26 -07003783 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003784 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003785 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003786 */
3787
Ben Blum102a7752009-09-23 15:56:26 -07003788static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003789{
3790 /*
3791 * Initially we receive a position value that corresponds to
3792 * one more than the last pid shown (or 0 on the first call or
3793 * after a seek to the start). Use a binary-search to find the
3794 * next pid to display, if any
3795 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003796 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003797 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003798 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003799 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003800 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003801 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003802
Tejun Heo4bac00d2013-11-29 10:42:59 -05003803 mutex_lock(&cgrp->pidlist_mutex);
3804
3805 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003806 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003807 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003808 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003809 * could already have been destroyed.
3810 */
Tejun Heo5d224442013-12-05 12:28:04 -05003811 if (of->priv)
3812 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003813
3814 /*
3815 * Either this is the first start() after open or the matching
3816 * pidlist has been destroyed inbetween. Create a new one.
3817 */
Tejun Heo5d224442013-12-05 12:28:04 -05003818 if (!of->priv) {
3819 ret = pidlist_array_load(cgrp, type,
3820 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003821 if (ret)
3822 return ERR_PTR(ret);
3823 }
Tejun Heo5d224442013-12-05 12:28:04 -05003824 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003825
Paul Menagecc31edc2008-10-18 20:28:04 -07003826 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003827 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003828
Paul Menagecc31edc2008-10-18 20:28:04 -07003829 while (index < end) {
3830 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003831 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003832 index = mid;
3833 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003834 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003835 index = mid + 1;
3836 else
3837 end = mid;
3838 }
3839 }
3840 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003841 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003842 return NULL;
3843 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003844 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003845 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003846 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003847}
3848
Ben Blum102a7752009-09-23 15:56:26 -07003849static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003850{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003851 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003852 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003853
Tejun Heo5d224442013-12-05 12:28:04 -05003854 if (l)
3855 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003856 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003857 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003858}
3859
Ben Blum102a7752009-09-23 15:56:26 -07003860static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003861{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003862 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003863 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003864 pid_t *p = v;
3865 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003866 /*
3867 * Advance to the next pid in the array. If this goes off the
3868 * end, we're done
3869 */
3870 p++;
3871 if (p >= end) {
3872 return NULL;
3873 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003874 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003875 return p;
3876 }
3877}
3878
Ben Blum102a7752009-09-23 15:56:26 -07003879static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003880{
3881 return seq_printf(s, "%d\n", *(int *)v);
3882}
3883
Tejun Heo182446d2013-08-08 20:11:24 -04003884static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3885 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003886{
Tejun Heo182446d2013-08-08 20:11:24 -04003887 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003888}
3889
Tejun Heo182446d2013-08-08 20:11:24 -04003890static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3891 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003892{
Tejun Heo182446d2013-08-08 20:11:24 -04003893 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003894 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003895 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003896 else
Tejun Heo182446d2013-08-08 20:11:24 -04003897 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003898 return 0;
3899}
3900
Tejun Heo182446d2013-08-08 20:11:24 -04003901static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3902 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003903{
Tejun Heo182446d2013-08-08 20:11:24 -04003904 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003905}
3906
Tejun Heo182446d2013-08-08 20:11:24 -04003907static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3908 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003909{
3910 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003911 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003912 else
Tejun Heo182446d2013-08-08 20:11:24 -04003913 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003914 return 0;
3915}
3916
Tejun Heod5c56ce2013-06-03 19:14:34 -07003917static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003918 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003919 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003920 .seq_start = cgroup_pidlist_start,
3921 .seq_next = cgroup_pidlist_next,
3922 .seq_stop = cgroup_pidlist_stop,
3923 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003924 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003925 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003926 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003927 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003928 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003929 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003930 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003931 .read_u64 = cgroup_clone_children_read,
3932 .write_u64 = cgroup_clone_children_write,
3933 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003934 {
Tejun Heo873fe092013-04-14 20:15:26 -07003935 .name = "cgroup.sane_behavior",
3936 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003937 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003938 },
Tejun Heof8f22e52014-04-23 11:13:16 -04003939 {
3940 .name = "cgroup.controllers",
3941 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
3942 .seq_show = cgroup_root_controllers_show,
3943 },
3944 {
3945 .name = "cgroup.controllers",
3946 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3947 .seq_show = cgroup_controllers_show,
3948 },
3949 {
3950 .name = "cgroup.subtree_control",
3951 .flags = CFTYPE_ONLY_ON_DFL,
3952 .seq_show = cgroup_subtree_control_show,
3953 .write_string = cgroup_subtree_control_write,
3954 },
Tejun Heo842b5972014-04-25 18:28:02 -04003955 {
3956 .name = "cgroup.populated",
3957 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3958 .seq_show = cgroup_populated_show,
3959 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003960
3961 /*
3962 * Historical crazy stuff. These don't have "cgroup." prefix and
3963 * don't exist if sane_behavior. If you're depending on these, be
3964 * prepared to be burned.
3965 */
3966 {
3967 .name = "tasks",
3968 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003969 .seq_start = cgroup_pidlist_start,
3970 .seq_next = cgroup_pidlist_next,
3971 .seq_stop = cgroup_pidlist_stop,
3972 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003973 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003974 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003975 .mode = S_IRUGO | S_IWUSR,
3976 },
3977 {
3978 .name = "notify_on_release",
3979 .flags = CFTYPE_INSANE,
3980 .read_u64 = cgroup_read_notify_on_release,
3981 .write_u64 = cgroup_write_notify_on_release,
3982 },
Tejun Heo873fe092013-04-14 20:15:26 -07003983 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003984 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003985 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003986 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003987 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003988 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003989 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003990 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003991};
3992
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003993/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003994 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003995 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003996 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003997 *
3998 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003999 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004000static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004001{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004002 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004003 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004004
Tejun Heo8e3f6542012-04-01 12:09:55 -07004005 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004006 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004007 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004008
Tejun Heo69dfa002014-05-04 15:09:13 -04004009 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004010 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004011
Tejun Heo0adb0702014-02-12 09:29:48 -05004012 list_for_each_entry(cfts, &ss->cfts, node) {
4013 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004014 if (ret < 0)
4015 goto err;
4016 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004017 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004018 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004019err:
4020 cgroup_clear_dir(cgrp, subsys_mask);
4021 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004022}
4023
Tejun Heo0c21ead2013-08-13 20:22:51 -04004024/*
4025 * css destruction is four-stage process.
4026 *
4027 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4028 * Implemented in kill_css().
4029 *
4030 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4031 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4032 * by invoking offline_css(). After offlining, the base ref is put.
4033 * Implemented in css_killed_work_fn().
4034 *
4035 * 3. When the percpu_ref reaches zero, the only possible remaining
4036 * accessors are inside RCU read sections. css_release() schedules the
4037 * RCU callback.
4038 *
4039 * 4. After the grace period, the css can be freed. Implemented in
4040 * css_free_work_fn().
4041 *
4042 * It is actually hairier because both step 2 and 4 require process context
4043 * and thus involve punting to css->destroy_work adding two additional
4044 * steps to the already complex sequence.
4045 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004046static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004047{
4048 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004049 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004050 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004051
Tejun Heo0ae78e02013-08-13 11:01:54 -04004052 if (css->parent)
4053 css_put(css->parent);
4054
Tejun Heo0c21ead2013-08-13 20:22:51 -04004055 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004056 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004057}
4058
4059static void css_free_rcu_fn(struct rcu_head *rcu_head)
4060{
4061 struct cgroup_subsys_state *css =
4062 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4063
Tejun Heo0c21ead2013-08-13 20:22:51 -04004064 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004065 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004066}
4067
Tejun Heod3daf282013-06-13 19:39:16 -07004068static void css_release(struct percpu_ref *ref)
4069{
4070 struct cgroup_subsys_state *css =
4071 container_of(ref, struct cgroup_subsys_state, refcnt);
4072
Monam Agarwal01a97142014-03-24 00:17:18 +05304073 RCU_INIT_POINTER(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004074 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004075}
4076
Tejun Heo623f9262013-08-13 11:01:55 -04004077static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4078 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004079{
Paul Menagebd89aab2007-10-18 23:40:44 -07004080 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004081 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004082 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004083
Tejun Heo0ae78e02013-08-13 11:01:54 -04004084 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004085 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004086 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004087 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004088
Tejun Heoca8bdca2013-08-26 18:40:56 -04004089 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004090}
4091
Li Zefan2a4ac632013-07-31 16:16:40 +08004092/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004093static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004094{
Tejun Heo623f9262013-08-13 11:01:55 -04004095 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004096 int ret = 0;
4097
Tejun Heoace2bee2014-02-11 11:52:47 -05004098 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004099 lockdep_assert_held(&cgroup_mutex);
4100
Tejun Heo92fb9742012-11-19 08:13:38 -08004101 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004102 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004103 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004104 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004105 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05004106 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004107 }
Tejun Heob1929db2012-11-19 08:13:38 -08004108 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004109}
4110
Li Zefan2a4ac632013-07-31 16:16:40 +08004111/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004112static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004113{
Tejun Heo623f9262013-08-13 11:01:55 -04004114 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004115
Tejun Heoace2bee2014-02-11 11:52:47 -05004116 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004117 lockdep_assert_held(&cgroup_mutex);
4118
4119 if (!(css->flags & CSS_ONLINE))
4120 return;
4121
Li Zefand7eeac12013-03-12 15:35:59 -07004122 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004123 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004124
Tejun Heoeb954192013-08-08 20:11:23 -04004125 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004126 css->cgroup->nr_css--;
Tejun Heoe3297802014-04-23 11:13:15 -04004127 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004128
4129 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004130}
4131
Tejun Heoc81c925a2013-12-06 15:11:56 -05004132/**
4133 * create_css - create a cgroup_subsys_state
4134 * @cgrp: the cgroup new css will be associated with
4135 * @ss: the subsys of new css
4136 *
4137 * Create a new css associated with @cgrp - @ss pair. On success, the new
4138 * css is online and installed in @cgrp with all interface files created.
4139 * Returns 0 on success, -errno on failure.
4140 */
4141static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4142{
4143 struct cgroup *parent = cgrp->parent;
4144 struct cgroup_subsys_state *css;
4145 int err;
4146
Tejun Heoc81c925a2013-12-06 15:11:56 -05004147 lockdep_assert_held(&cgroup_mutex);
4148
4149 css = ss->css_alloc(cgroup_css(parent, ss));
4150 if (IS_ERR(css))
4151 return PTR_ERR(css);
4152
4153 err = percpu_ref_init(&css->refcnt, css_release);
4154 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004155 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004156
4157 init_css(css, ss, cgrp);
4158
Tejun Heoaec25022014-02-08 10:36:58 -05004159 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004160 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004161 goto err_free_percpu_ref;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004162
4163 err = online_css(css);
4164 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004165 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004166
Tejun Heo59f52962014-02-11 11:52:49 -05004167 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004168 css_get(css->parent);
4169
4170 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4171 parent->parent) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004172 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 -04004173 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004174 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004175 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004176 ss->warned_broken_hierarchy = true;
4177 }
4178
4179 return 0;
4180
Li Zefan3eb59ec2014-03-18 17:02:36 +08004181err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004182 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004183err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004184 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004185err_free_css:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004186 ss->css_free(css);
4187 return err;
4188}
4189
Tejun Heo2bd59d42014-02-11 11:52:49 -05004190/**
Li Zefana043e3b2008-02-23 15:24:09 -08004191 * cgroup_create - create a cgroup
4192 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05004193 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05004194 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07004195 */
Tejun Heoe61734c2014-02-12 09:29:50 -05004196static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05004197 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004198{
Paul Menagebd89aab2007-10-18 23:40:44 -07004199 struct cgroup *cgrp;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004200 struct cgroup_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05004201 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004202 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004203 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004204
Tejun Heo0a950f62012-11-19 09:02:12 -08004205 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004206 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4207 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004208 return -ENOMEM;
4209
Tejun Heoace2bee2014-02-11 11:52:47 -05004210 mutex_lock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08004211
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004212 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08004213 * Only live parents can have children. Note that the liveliness
4214 * check isn't strictly necessary because cgroup_mkdir() and
4215 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4216 * anyway so that locking is contained inside cgroup proper and we
4217 * don't get nasty surprises if we ever grow another caller.
4218 */
4219 if (!cgroup_lock_live_group(parent)) {
4220 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05004221 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004222 }
4223
4224 /*
4225 * Temporarily set the pointer to NULL, so idr_find() won't return
4226 * a half-baked cgroup.
4227 */
Tejun Heo7d699dd2014-05-04 15:09:13 -04004228 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004229 if (cgrp->id < 0) {
4230 err = -ENOMEM;
4231 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08004232 }
4233
Paul Menagecc31edc2008-10-18 20:28:04 -07004234 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004235
Paul Menagebd89aab2007-10-18 23:40:44 -07004236 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004237 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004238 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004239
Li Zefanb6abdb02008-03-04 14:28:19 -08004240 if (notify_on_release(parent))
4241 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4242
Tejun Heo2260e7f2012-11-19 08:13:38 -08004243 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4244 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004245
Tejun Heo2bd59d42014-02-11 11:52:49 -05004246 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004247 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004248 if (IS_ERR(kn)) {
4249 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004250 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004251 }
4252 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004253
Tejun Heo6f305582014-02-12 09:29:50 -05004254 /*
4255 * This extra ref will be put in cgroup_free_fn() and guarantees
4256 * that @cgrp->kn is always accessible.
4257 */
4258 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004259
Tejun Heo00356bd2013-06-18 11:14:22 -07004260 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004261
Tejun Heo4e139af2012-11-19 08:13:36 -08004262 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004263 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004264 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004265 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004266
Tejun Heo0d802552013-12-06 15:11:56 -05004267 /*
4268 * @cgrp is now fully operational. If something fails after this
4269 * point, it'll be released via the normal destruction path.
4270 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004271 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4272
Tejun Heo49957f82014-04-07 16:44:47 -04004273 err = cgroup_kn_set_ugid(kn);
4274 if (err)
4275 goto err_destroy;
4276
Tejun Heo2bb566c2013-08-08 20:11:23 -04004277 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004278 if (err)
4279 goto err_destroy;
4280
Tejun Heo9d403e92013-12-06 15:11:56 -05004281 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004282 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004283 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heob85d2042013-12-06 15:11:57 -05004284 err = create_css(cgrp, ss);
4285 if (err)
4286 goto err_destroy;
4287 }
Tejun Heoa8638032012-11-09 09:12:29 -08004288 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004289
Tejun Heobd53d612014-04-23 11:13:16 -04004290 /*
4291 * On the default hierarchy, a child doesn't automatically inherit
4292 * child_subsys_mask from the parent. Each is configured manually.
4293 */
4294 if (!cgroup_on_dfl(cgrp))
4295 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004296
Tejun Heo2bd59d42014-02-11 11:52:49 -05004297 kernfs_activate(kn);
4298
Paul Menageddbcc7e2007-10-18 23:39:30 -07004299 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004300 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004301
4302 return 0;
4303
Tejun Heo0a950f62012-11-19 09:02:12 -08004304err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004305 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004306err_unlock:
4307 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004308err_unlock_tree:
4309 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004310 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004311 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004312
4313err_destroy:
4314 cgroup_destroy_locked(cgrp);
4315 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004316 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004317 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004318}
4319
Tejun Heo2bd59d42014-02-11 11:52:49 -05004320static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4321 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004322{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004323 struct cgroup *parent = parent_kn->priv;
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004324 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004325
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004326 /*
4327 * cgroup_create() grabs cgroup_tree_mutex which nests outside
4328 * kernfs active_ref and cgroup_create() already synchronizes
4329 * properly against removal through cgroup_lock_live_group().
4330 * Break it before calling cgroup_create().
4331 */
4332 cgroup_get(parent);
4333 kernfs_break_active_protection(parent_kn);
4334
4335 ret = cgroup_create(parent, name, mode);
4336
4337 kernfs_unbreak_active_protection(parent_kn);
4338 cgroup_put(parent);
4339 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004340}
4341
Tejun Heo223dbc32013-08-13 20:22:50 -04004342/*
4343 * This is called when the refcnt of a css is confirmed to be killed.
4344 * css_tryget() is now guaranteed to fail.
4345 */
4346static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004347{
Tejun Heo223dbc32013-08-13 20:22:50 -04004348 struct cgroup_subsys_state *css =
4349 container_of(work, struct cgroup_subsys_state, destroy_work);
4350 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004351
Tejun Heoace2bee2014-02-11 11:52:47 -05004352 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004353 mutex_lock(&cgroup_mutex);
4354
4355 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004356 * css_tryget() is guaranteed to fail now. Tell subsystems to
4357 * initate destruction.
4358 */
4359 offline_css(css);
4360
4361 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004362 * If @cgrp is marked dead, it's waiting for refs of all css's to
4363 * be disabled before proceeding to the second phase of cgroup
4364 * destruction. If we are the last one, kick it off.
4365 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004366 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004367 cgroup_destroy_css_killed(cgrp);
4368
4369 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004370 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004371
4372 /*
4373 * Put the css refs from kill_css(). Each css holds an extra
4374 * reference to the cgroup's dentry and cgroup removal proceeds
4375 * regardless of css refs. On the last put of each css, whenever
4376 * that may be, the extra dentry ref is put so that dentry
4377 * destruction happens only after all css's are released.
4378 */
4379 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004380}
4381
Tejun Heo223dbc32013-08-13 20:22:50 -04004382/* css kill confirmation processing requires process context, bounce */
4383static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004384{
4385 struct cgroup_subsys_state *css =
4386 container_of(ref, struct cgroup_subsys_state, refcnt);
4387
Tejun Heo223dbc32013-08-13 20:22:50 -04004388 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004389 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004390}
4391
Tejun Heof392e512014-04-23 11:13:14 -04004392/**
4393 * kill_css - destroy a css
4394 * @css: css to destroy
4395 *
4396 * This function initiates destruction of @css by removing cgroup interface
4397 * files and putting its base reference. ->css_offline() will be invoked
4398 * asynchronously once css_tryget() is guaranteed to fail and when the
4399 * reference count reaches zero, @css will be released.
4400 */
4401static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004402{
Tejun Heo94419622014-03-19 10:23:54 -04004403 lockdep_assert_held(&cgroup_tree_mutex);
4404
Tejun Heo2bd59d42014-02-11 11:52:49 -05004405 /*
4406 * This must happen before css is disassociated with its cgroup.
4407 * See seq_css() for details.
4408 */
Tejun Heoaec25022014-02-08 10:36:58 -05004409 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004410
Tejun Heoedae0c32013-08-13 20:22:51 -04004411 /*
4412 * Killing would put the base ref, but we need to keep it alive
4413 * until after ->css_offline().
4414 */
4415 css_get(css);
4416
4417 /*
4418 * cgroup core guarantees that, by the time ->css_offline() is
4419 * invoked, no new css reference will be given out via
4420 * css_tryget(). We can't simply call percpu_ref_kill() and
4421 * proceed to offlining css's because percpu_ref_kill() doesn't
4422 * guarantee that the ref is seen as killed on all CPUs on return.
4423 *
4424 * Use percpu_ref_kill_and_confirm() to get notifications as each
4425 * css is confirmed to be seen as killed on all CPUs.
4426 */
4427 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004428}
4429
4430/**
4431 * cgroup_destroy_locked - the first stage of cgroup destruction
4432 * @cgrp: cgroup to be destroyed
4433 *
4434 * css's make use of percpu refcnts whose killing latency shouldn't be
4435 * exposed to userland and are RCU protected. Also, cgroup core needs to
4436 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4437 * invoked. To satisfy all the requirements, destruction is implemented in
4438 * the following two steps.
4439 *
4440 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4441 * userland visible parts and start killing the percpu refcnts of
4442 * css's. Set up so that the next stage will be kicked off once all
4443 * the percpu refcnts are confirmed to be killed.
4444 *
4445 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4446 * rest of destruction. Once all cgroup references are gone, the
4447 * cgroup is RCU-freed.
4448 *
4449 * This function implements s1. After this step, @cgrp is gone as far as
4450 * the userland is concerned and a new cgroup with the same name may be
4451 * created. As cgroup doesn't care about the names internally, this
4452 * doesn't cause any problem.
4453 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004454static int cgroup_destroy_locked(struct cgroup *cgrp)
4455 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004456{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004457 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004458 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004459 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004460 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004461
Tejun Heoace2bee2014-02-11 11:52:47 -05004462 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004463 lockdep_assert_held(&cgroup_mutex);
4464
Tejun Heoddd69142013-06-12 21:04:54 -07004465 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004466 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004467 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004468 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004469 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004470 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004471 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004472 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004473 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004474
Tejun Heo1a90dd52012-11-05 09:16:59 -08004475 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004476 * Make sure there's no live children. We can't test ->children
4477 * emptiness as dead children linger on it while being destroyed;
4478 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4479 */
4480 empty = true;
4481 rcu_read_lock();
4482 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4483 empty = cgroup_is_dead(child);
4484 if (!empty)
4485 break;
4486 }
4487 rcu_read_unlock();
4488 if (!empty)
4489 return -EBUSY;
4490
4491 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004492 * Mark @cgrp dead. This prevents further task migration and child
4493 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004494 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004495 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004496 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004497 */
Tejun Heo54766d42013-06-12 21:04:53 -07004498 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004499
Tejun Heo5d773812014-03-19 10:23:53 -04004500 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004501 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4502 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004503 * percpu refs of all css's are confirmed to be killed. This
4504 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004505 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004506 mutex_unlock(&cgroup_mutex);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004507 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004508 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004509 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004510
Tejun Heo455050d2013-06-13 19:27:41 -07004511 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4512 raw_spin_lock(&release_list_lock);
4513 if (!list_empty(&cgrp->release_list))
4514 list_del_init(&cgrp->release_list);
4515 raw_spin_unlock(&release_list_lock);
4516
4517 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004518 * If @cgrp has css's attached, the second stage of cgroup
4519 * destruction is kicked off from css_killed_work_fn() after the
4520 * refs of all attached css's are killed. If @cgrp doesn't have
4521 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004522 */
Tejun Heof20104d2013-08-13 20:22:50 -04004523 if (!cgrp->nr_css)
4524 cgroup_destroy_css_killed(cgrp);
4525
Tejun Heo2bd59d42014-02-11 11:52:49 -05004526 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05004527 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004528
Tejun Heof20104d2013-08-13 20:22:50 -04004529 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05004530 * There are two control paths which try to determine cgroup from
4531 * dentry without going through kernfs - cgroupstats_build() and
4532 * css_tryget_from_dir(). Those are supported by RCU protecting
4533 * clearing of cgrp->kn->priv backpointer, which should happen
4534 * after all files under it have been removed.
Tejun Heo455050d2013-06-13 19:27:41 -07004535 */
Tejun Heo6f305582014-02-12 09:29:50 -05004536 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004537 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004538
Tejun Heo4ac06012014-02-11 11:52:47 -05004539 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004540
Tejun Heoea15f8c2013-06-13 19:27:42 -07004541 return 0;
4542};
4543
Tejun Heod3daf282013-06-13 19:39:16 -07004544/**
Tejun Heof20104d2013-08-13 20:22:50 -04004545 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004546 * @work: cgroup->destroy_free_work
4547 *
4548 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004549 * destroyed after all css's are offlined and performs the rest of
4550 * destruction. This is the second step of destruction described in the
4551 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004552 */
Tejun Heof20104d2013-08-13 20:22:50 -04004553static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004554{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004555 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004556
Tejun Heoace2bee2014-02-11 11:52:47 -05004557 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004558 lockdep_assert_held(&cgroup_mutex);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004559
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004560 /* delete this cgroup from parent->children */
4561 list_del_rcu(&cgrp->sibling);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004562
Tejun Heo59f52962014-02-11 11:52:49 -05004563 cgroup_put(cgrp);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004564
Paul Menageddbcc7e2007-10-18 23:39:30 -07004565 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004566 check_for_release(parent);
4567}
4568
Tejun Heo2bd59d42014-02-11 11:52:49 -05004569static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004570{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004571 struct cgroup *cgrp = kn->priv;
4572 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004573
Tejun Heo2bd59d42014-02-11 11:52:49 -05004574 /*
4575 * This is self-destruction but @kn can't be removed while this
4576 * callback is in progress. Let's break active protection. Once
4577 * the protection is broken, @cgrp can be destroyed at any point.
4578 * Pin it so that it stays accessible.
4579 */
4580 cgroup_get(cgrp);
4581 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004582
Tejun Heoace2bee2014-02-11 11:52:47 -05004583 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004584 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004585
Tejun Heo2bd59d42014-02-11 11:52:49 -05004586 /*
4587 * @cgrp might already have been destroyed while we're trying to
4588 * grab the mutexes.
4589 */
4590 if (!cgroup_is_dead(cgrp))
4591 ret = cgroup_destroy_locked(cgrp);
4592
Tejun Heo42809dd2012-11-19 08:13:37 -08004593 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004594 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004595
Tejun Heo2bd59d42014-02-11 11:52:49 -05004596 kernfs_unbreak_active_protection(kn);
4597 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004598 return ret;
4599}
4600
Tejun Heo2bd59d42014-02-11 11:52:49 -05004601static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4602 .remount_fs = cgroup_remount,
4603 .show_options = cgroup_show_options,
4604 .mkdir = cgroup_mkdir,
4605 .rmdir = cgroup_rmdir,
4606 .rename = cgroup_rename,
4607};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004608
Li Zefan06a11922008-04-29 01:00:07 -07004609static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004610{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004611 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004612
4613 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004614
Tejun Heoace2bee2014-02-11 11:52:47 -05004615 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004616 mutex_lock(&cgroup_mutex);
4617
Tejun Heo0adb0702014-02-12 09:29:48 -05004618 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004619
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004620 /* Create the root cgroup state for this subsystem */
4621 ss->root = &cgrp_dfl_root;
4622 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004623 /* We don't handle early failures gracefully */
4624 BUG_ON(IS_ERR(css));
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004625 init_css(css, ss, &cgrp_dfl_root.cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004626
Li Zefane8d55fd2008-04-29 01:00:13 -07004627 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004628 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004629 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004630 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004631 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004632
4633 need_forkexit_callback |= ss->fork || ss->exit;
4634
Li Zefane8d55fd2008-04-29 01:00:13 -07004635 /* At system boot, before all subsystems have been
4636 * registered, no tasks have been forked, so we don't
4637 * need to invoke fork callbacks here. */
4638 BUG_ON(!list_empty(&init_task.tasks));
4639
Tejun Heoae7f1642013-08-13 20:22:50 -04004640 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004641
Tejun Heof392e512014-04-23 11:13:14 -04004642 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004643
Ben Blume6a11052010-03-10 15:22:09 -08004644 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004645 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004646}
4647
4648/**
Li Zefana043e3b2008-02-23 15:24:09 -08004649 * cgroup_init_early - cgroup initialization at system boot
4650 *
4651 * Initialize cgroups at system boot, and initialize any
4652 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004653 */
4654int __init cgroup_init_early(void)
4655{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004656 static struct cgroup_sb_opts __initdata opts =
4657 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004658 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004659 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004660
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004661 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004662 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004663
Tejun Heo3ed80a62014-02-08 10:36:58 -05004664 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004665 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004666 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4667 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004668 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004669 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4670 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004671
Tejun Heoaec25022014-02-08 10:36:58 -05004672 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004673 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004674
4675 if (ss->early_init)
4676 cgroup_init_subsys(ss);
4677 }
4678 return 0;
4679}
4680
4681/**
Li Zefana043e3b2008-02-23 15:24:09 -08004682 * cgroup_init - cgroup initialization
4683 *
4684 * Register cgroup filesystem and /proc file, and initialize
4685 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004686 */
4687int __init cgroup_init(void)
4688{
Tejun Heo30159ec2013-06-25 11:53:37 -07004689 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004690 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004691 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004692
Tejun Heo2bd59d42014-02-11 11:52:49 -05004693 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004694
Tejun Heo985ed672014-03-19 10:23:53 -04004695 mutex_lock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004696 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004697
Tejun Heo82fe9b02013-06-25 11:53:37 -07004698 /* Add init_css_set to the hash table */
4699 key = css_set_hash(init_css_set.subsys);
4700 hash_add(css_set_table, &init_css_set.hlist, key);
4701
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004702 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004703
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004704 mutex_unlock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04004705 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004706
Tejun Heo172a2c062014-03-19 10:23:53 -04004707 for_each_subsys(ss, ssid) {
4708 if (!ss->early_init)
4709 cgroup_init_subsys(ss);
4710
Tejun Heo2d8f2432014-04-23 11:13:15 -04004711 list_add_tail(&init_css_set.e_cset_node[ssid],
4712 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4713
Tejun Heo172a2c062014-03-19 10:23:53 -04004714 /*
4715 * cftype registration needs kmalloc and can't be done
4716 * during early_init. Register base cftypes separately.
4717 */
4718 if (ss->base_cftypes)
4719 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4720 }
Greg KH676db4a2010-08-05 13:53:35 -07004721
Paul Menageddbcc7e2007-10-18 23:39:30 -07004722 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004723 if (!cgroup_kobj)
4724 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004725
4726 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004727 if (err < 0) {
4728 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004729 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004730 }
4731
4732 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004733 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004734}
Paul Menageb4f48b62007-10-18 23:39:33 -07004735
Tejun Heoe5fca242013-11-22 17:14:39 -05004736static int __init cgroup_wq_init(void)
4737{
4738 /*
4739 * There isn't much point in executing destruction path in
4740 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004741 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004742 *
4743 * We would prefer to do this in cgroup_init() above, but that
4744 * is called before init_workqueues(): so leave this until after.
4745 */
Tejun Heo1a115332014-02-12 19:06:19 -05004746 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004747 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004748
4749 /*
4750 * Used to destroy pidlists and separate to serve as flush domain.
4751 * Cap @max_active to 1 too.
4752 */
4753 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4754 0, 1);
4755 BUG_ON(!cgroup_pidlist_destroy_wq);
4756
Tejun Heoe5fca242013-11-22 17:14:39 -05004757 return 0;
4758}
4759core_initcall(cgroup_wq_init);
4760
Paul Menagea4243162007-10-18 23:39:35 -07004761/*
4762 * proc_cgroup_show()
4763 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4764 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004765 */
4766
4767/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004768int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004769{
4770 struct pid *pid;
4771 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004772 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004773 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004774 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004775
4776 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004777 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004778 if (!buf)
4779 goto out;
4780
4781 retval = -ESRCH;
4782 pid = m->private;
4783 tsk = get_pid_task(pid, PIDTYPE_PID);
4784 if (!tsk)
4785 goto out_free;
4786
4787 retval = 0;
4788
4789 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004790 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004791
Tejun Heo985ed672014-03-19 10:23:53 -04004792 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004793 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004794 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004795 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004796
Tejun Heoa2dd4242014-03-19 10:23:55 -04004797 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004798 continue;
4799
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004800 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004801 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004802 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004803 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004804 if (strlen(root->name))
4805 seq_printf(m, "%sname=%s", count ? "," : "",
4806 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004807 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004808 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004809 path = cgroup_path(cgrp, buf, PATH_MAX);
4810 if (!path) {
4811 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004812 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004813 }
4814 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004815 seq_putc(m, '\n');
4816 }
4817
4818out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004819 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004820 mutex_unlock(&cgroup_mutex);
4821 put_task_struct(tsk);
4822out_free:
4823 kfree(buf);
4824out:
4825 return retval;
4826}
4827
Paul Menagea4243162007-10-18 23:39:35 -07004828/* Display information about each subsystem and each hierarchy */
4829static int proc_cgroupstats_show(struct seq_file *m, void *v)
4830{
Tejun Heo30159ec2013-06-25 11:53:37 -07004831 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004832 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004833
Paul Menage8bab8dd2008-04-04 14:29:57 -07004834 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004835 /*
4836 * ideally we don't want subsystems moving around while we do this.
4837 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4838 * subsys/hierarchy state.
4839 */
Paul Menagea4243162007-10-18 23:39:35 -07004840 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004841
4842 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004843 seq_printf(m, "%s\t%d\t%d\t%d\n",
4844 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004845 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004846
Paul Menagea4243162007-10-18 23:39:35 -07004847 mutex_unlock(&cgroup_mutex);
4848 return 0;
4849}
4850
4851static int cgroupstats_open(struct inode *inode, struct file *file)
4852{
Al Viro9dce07f2008-03-29 03:07:28 +00004853 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004854}
4855
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004856static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004857 .open = cgroupstats_open,
4858 .read = seq_read,
4859 .llseek = seq_lseek,
4860 .release = single_release,
4861};
4862
Paul Menageb4f48b62007-10-18 23:39:33 -07004863/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004864 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004865 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004866 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004867 * A task is associated with the init_css_set until cgroup_post_fork()
4868 * attaches it to the parent's css_set. Empty cg_list indicates that
4869 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004870 */
4871void cgroup_fork(struct task_struct *child)
4872{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004873 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004874 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004875}
4876
4877/**
Li Zefana043e3b2008-02-23 15:24:09 -08004878 * cgroup_post_fork - called on a new task after adding it to the task list
4879 * @child: the task in question
4880 *
Tejun Heo5edee612012-10-16 15:03:14 -07004881 * Adds the task to the list running through its css_set if necessary and
4882 * call the subsystem fork() callbacks. Has to be after the task is
4883 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004884 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004885 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004886 */
Paul Menage817929e2007-10-18 23:39:36 -07004887void cgroup_post_fork(struct task_struct *child)
4888{
Tejun Heo30159ec2013-06-25 11:53:37 -07004889 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004890 int i;
4891
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004892 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004893 * This may race against cgroup_enable_task_cg_links(). As that
4894 * function sets use_task_css_set_links before grabbing
4895 * tasklist_lock and we just went through tasklist_lock to add
4896 * @child, it's guaranteed that either we see the set
4897 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4898 * @child during its iteration.
4899 *
4900 * If we won the race, @child is associated with %current's
4901 * css_set. Grabbing css_set_rwsem guarantees both that the
4902 * association is stable, and, on completion of the parent's
4903 * migration, @child is visible in the source of migration or
4904 * already in the destination cgroup. This guarantee is necessary
4905 * when implementing operations which need to migrate all tasks of
4906 * a cgroup to another.
4907 *
4908 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4909 * will remain in init_css_set. This is safe because all tasks are
4910 * in the init_css_set before cg_links is enabled and there's no
4911 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004912 */
Paul Menage817929e2007-10-18 23:39:36 -07004913 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004914 struct css_set *cset;
4915
Tejun Heo96d365e2014-02-13 06:58:40 -05004916 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004917 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004918 if (list_empty(&child->cg_list)) {
4919 rcu_assign_pointer(child->cgroups, cset);
4920 list_add(&child->cg_list, &cset->tasks);
4921 get_css_set(cset);
4922 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004923 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004924 }
Tejun Heo5edee612012-10-16 15:03:14 -07004925
4926 /*
4927 * Call ss->fork(). This must happen after @child is linked on
4928 * css_set; otherwise, @child might change state between ->fork()
4929 * and addition to css_set.
4930 */
4931 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004932 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004933 if (ss->fork)
4934 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004935 }
Paul Menage817929e2007-10-18 23:39:36 -07004936}
Tejun Heo5edee612012-10-16 15:03:14 -07004937
Paul Menage817929e2007-10-18 23:39:36 -07004938/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004939 * cgroup_exit - detach cgroup from exiting task
4940 * @tsk: pointer to task_struct of exiting process
4941 *
4942 * Description: Detach cgroup from @tsk and release it.
4943 *
4944 * Note that cgroups marked notify_on_release force every task in
4945 * them to take the global cgroup_mutex mutex when exiting.
4946 * This could impact scaling on very large systems. Be reluctant to
4947 * use notify_on_release cgroups where very high task exit scaling
4948 * is required on large systems.
4949 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004950 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4951 * call cgroup_exit() while the task is still competent to handle
4952 * notify_on_release(), then leave the task attached to the root cgroup in
4953 * each hierarchy for the remainder of its exit. No need to bother with
4954 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004955 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004956 */
Li Zefan1ec41832014-03-28 15:22:19 +08004957void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004958{
Tejun Heo30159ec2013-06-25 11:53:37 -07004959 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004960 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004961 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004962 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004963
4964 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05004965 * Unlink from @tsk from its css_set. As migration path can't race
4966 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004967 */
4968 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004969 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004970 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004971 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004972 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07004973 }
4974
Paul Menageb4f48b62007-10-18 23:39:33 -07004975 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07004976 cset = task_css_set(tsk);
4977 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004978
Li Zefan1ec41832014-03-28 15:22:19 +08004979 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004980 /* see cgroup_post_fork() for details */
4981 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004982 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004983 struct cgroup_subsys_state *old_css = cset->subsys[i];
4984 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004985
Tejun Heoeb954192013-08-08 20:11:23 -04004986 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004987 }
4988 }
4989 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004990
Tejun Heoeaf797a2014-02-25 10:04:03 -05004991 if (put_cset)
4992 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07004993}
Paul Menage697f4162007-10-18 23:39:34 -07004994
Paul Menagebd89aab2007-10-18 23:40:44 -07004995static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004996{
Li Zefanf50daa72013-03-01 15:06:07 +08004997 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004998 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004999 /*
5000 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005001 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005002 * it now
5003 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005004 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005005
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005006 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005007 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005008 list_empty(&cgrp->release_list)) {
5009 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005010 need_schedule_work = 1;
5011 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005012 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005013 if (need_schedule_work)
5014 schedule_work(&release_agent_work);
5015 }
5016}
5017
Paul Menage81a6a5c2007-10-18 23:39:38 -07005018/*
5019 * Notify userspace when a cgroup is released, by running the
5020 * configured release agent with the name of the cgroup (path
5021 * relative to the root of cgroup file system) as the argument.
5022 *
5023 * Most likely, this user command will try to rmdir this cgroup.
5024 *
5025 * This races with the possibility that some other task will be
5026 * attached to this cgroup before it is removed, or that some other
5027 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5028 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5029 * unused, and this cgroup will be reprieved from its death sentence,
5030 * to continue to serve a useful existence. Next time it's released,
5031 * we will get notified again, if it still has 'notify_on_release' set.
5032 *
5033 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5034 * means only wait until the task is successfully execve()'d. The
5035 * separate release agent task is forked by call_usermodehelper(),
5036 * then control in this thread returns here, without waiting for the
5037 * release agent task. We don't bother to wait because the caller of
5038 * this routine has no use for the exit status of the release agent
5039 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005040 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005041static void cgroup_release_agent(struct work_struct *work)
5042{
5043 BUG_ON(work != &release_agent_work);
5044 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005045 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005046 while (!list_empty(&release_list)) {
5047 char *argv[3], *envp[3];
5048 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005049 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005050 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005051 struct cgroup,
5052 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005053 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005054 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005055 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005056 if (!pathbuf)
5057 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005058 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5059 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005060 goto continue_free;
5061 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5062 if (!agentbuf)
5063 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005064
5065 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005066 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005067 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005068 argv[i] = NULL;
5069
5070 i = 0;
5071 /* minimal command environment */
5072 envp[i++] = "HOME=/";
5073 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5074 envp[i] = NULL;
5075
5076 /* Drop the lock while we invoke the usermode helper,
5077 * since the exec could involve hitting disk and hence
5078 * be a slow process */
5079 mutex_unlock(&cgroup_mutex);
5080 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005081 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005082 continue_free:
5083 kfree(pathbuf);
5084 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005085 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005086 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005087 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005088 mutex_unlock(&cgroup_mutex);
5089}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005090
5091static int __init cgroup_disable(char *str)
5092{
Tejun Heo30159ec2013-06-25 11:53:37 -07005093 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005094 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005095 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005096
5097 while ((token = strsep(&str, ",")) != NULL) {
5098 if (!*token)
5099 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005100
Tejun Heo3ed80a62014-02-08 10:36:58 -05005101 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005102 if (!strcmp(token, ss->name)) {
5103 ss->disabled = 1;
5104 printk(KERN_INFO "Disabling %s control group"
5105 " subsystem\n", ss->name);
5106 break;
5107 }
5108 }
5109 }
5110 return 1;
5111}
5112__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005113
Tejun Heob77d7b62013-08-13 11:01:54 -04005114/**
Tejun Heo5a17f542014-02-11 11:52:47 -05005115 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04005116 * @dentry: directory dentry of interest
5117 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005118 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005119 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5120 * to get the corresponding css and return it. If such css doesn't exist
5121 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005122 */
Tejun Heo5a17f542014-02-11 11:52:47 -05005123struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
5124 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005125{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005126 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5127 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005128 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005129
Tejun Heo35cf0832013-08-26 18:40:56 -04005130 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005131 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5132 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005133 return ERR_PTR(-EBADF);
5134
Tejun Heo5a17f542014-02-11 11:52:47 -05005135 rcu_read_lock();
5136
Tejun Heo2bd59d42014-02-11 11:52:49 -05005137 /*
5138 * This path doesn't originate from kernfs and @kn could already
5139 * have been or be removed at any point. @kn->priv is RCU
5140 * protected for this access. See destroy_locked() for details.
5141 */
5142 cgrp = rcu_dereference(kn->priv);
5143 if (cgrp)
5144 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005145
5146 if (!css || !css_tryget(css))
5147 css = ERR_PTR(-ENOENT);
5148
5149 rcu_read_unlock();
5150 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005151}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005152
Li Zefan1cb650b2013-08-19 10:05:24 +08005153/**
5154 * css_from_id - lookup css by id
5155 * @id: the cgroup id
5156 * @ss: cgroup subsys to be looked into
5157 *
5158 * Returns the css if there's valid one with @id, otherwise returns NULL.
5159 * Should be called under rcu_read_lock().
5160 */
5161struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5162{
5163 struct cgroup *cgrp;
5164
Tejun Heoace2bee2014-02-11 11:52:47 -05005165 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08005166
5167 cgrp = idr_find(&ss->root->cgroup_idr, id);
5168 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005169 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005170 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005171}
5172
Paul Menagefe693432009-09-23 15:56:20 -07005173#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005174static struct cgroup_subsys_state *
5175debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005176{
5177 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5178
5179 if (!css)
5180 return ERR_PTR(-ENOMEM);
5181
5182 return css;
5183}
5184
Tejun Heoeb954192013-08-08 20:11:23 -04005185static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005186{
Tejun Heoeb954192013-08-08 20:11:23 -04005187 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005188}
5189
Tejun Heo182446d2013-08-08 20:11:24 -04005190static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5191 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005192{
Tejun Heo182446d2013-08-08 20:11:24 -04005193 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005194}
5195
Tejun Heo182446d2013-08-08 20:11:24 -04005196static u64 current_css_set_read(struct cgroup_subsys_state *css,
5197 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005198{
5199 return (u64)(unsigned long)current->cgroups;
5200}
5201
Tejun Heo182446d2013-08-08 20:11:24 -04005202static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005203 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005204{
5205 u64 count;
5206
5207 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005208 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005209 rcu_read_unlock();
5210 return count;
5211}
5212
Tejun Heo2da8ca82013-12-05 12:28:04 -05005213static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005214{
Tejun Heo69d02062013-06-12 21:04:50 -07005215 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005216 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005217 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005218
Tejun Heoe61734c2014-02-12 09:29:50 -05005219 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5220 if (!name_buf)
5221 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005222
Tejun Heo96d365e2014-02-13 06:58:40 -05005223 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005224 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005225 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005226 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005227 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005228
Tejun Heoa2dd4242014-03-19 10:23:55 -04005229 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005230 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005231 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005232 }
5233 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005234 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005235 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005236 return 0;
5237}
5238
5239#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005240static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005241{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005242 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005243 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005244
Tejun Heo96d365e2014-02-13 06:58:40 -05005245 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005246 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005247 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005248 struct task_struct *task;
5249 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005250
Tejun Heo5abb8852013-06-12 21:04:49 -07005251 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005252
Tejun Heo5abb8852013-06-12 21:04:49 -07005253 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005254 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5255 goto overflow;
5256 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005257 }
Tejun Heoc7561122014-02-25 10:04:01 -05005258
5259 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5260 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5261 goto overflow;
5262 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5263 }
5264 continue;
5265 overflow:
5266 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005267 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005268 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005269 return 0;
5270}
5271
Tejun Heo182446d2013-08-08 20:11:24 -04005272static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005273{
Tejun Heo182446d2013-08-08 20:11:24 -04005274 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005275}
5276
5277static struct cftype debug_files[] = {
5278 {
Paul Menagefe693432009-09-23 15:56:20 -07005279 .name = "taskcount",
5280 .read_u64 = debug_taskcount_read,
5281 },
5282
5283 {
5284 .name = "current_css_set",
5285 .read_u64 = current_css_set_read,
5286 },
5287
5288 {
5289 .name = "current_css_set_refcount",
5290 .read_u64 = current_css_set_refcount_read,
5291 },
5292
5293 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005294 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005295 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005296 },
5297
5298 {
5299 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005300 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005301 },
5302
5303 {
Paul Menagefe693432009-09-23 15:56:20 -07005304 .name = "releasable",
5305 .read_u64 = releasable_read,
5306 },
Paul Menagefe693432009-09-23 15:56:20 -07005307
Tejun Heo4baf6e32012-04-01 12:09:55 -07005308 { } /* terminate */
5309};
Paul Menagefe693432009-09-23 15:56:20 -07005310
Tejun Heo073219e2014-02-08 10:36:58 -05005311struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005312 .css_alloc = debug_css_alloc,
5313 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005314 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005315};
5316#endif /* CONFIG_CGROUP_DEBUG */