blob: 4eb2dd1bb5b1cf28da9fc85fc9a0a7a1472ed744 [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
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070043#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/spinlock.h>
Tejun Heo96d365e2014-02-13 06:58:40 -050045#include <linux/rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070047#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070048#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070049#include <linux/delayacct.h>
50#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080051#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070052#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070053#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070054#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020055#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050056#include <linux/delay.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070057
Arun Sharma600634972011-07-26 16:09:06 -070058#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070059
Tejun Heoe25e2cb2011-12-12 18:12:21 -080060/*
Tejun Heob1a21362013-11-29 10:42:58 -050061 * pidlists linger the following amount before being destroyed. The goal
62 * is avoiding frequent destruction in the middle of consecutive read calls
63 * Expiring in the middle is a performance problem not a correctness one.
64 * 1 sec should be enough.
65 */
66#define CGROUP_PIDLIST_DESTROY_DELAY HZ
67
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050068#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
69 MAX_CFTYPE_NAME + 2)
70
Tejun Heob1a21362013-11-29 10:42:58 -050071/*
Tejun Heoace2bee2014-02-11 11:52:47 -050072 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
73 * creation/removal and hierarchy changing operations including cgroup
74 * creation, removal, css association and controller rebinding. This outer
75 * lock is needed mainly to resolve the circular dependency between kernfs
76 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
77 */
78static DEFINE_MUTEX(cgroup_tree_mutex);
79
Tejun Heoe25e2cb2011-12-12 18:12:21 -080080/*
81 * cgroup_mutex is the master lock. Any modification to cgroup or its
82 * hierarchy must be performed while holding it.
83 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050084 * css_set_rwsem protects task->cgroups pointer, the list of css_set
85 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080086 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050087 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
88 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080089 */
Tejun Heo22194492013-04-07 09:29:51 -070090#ifdef CONFIG_PROVE_RCU
91DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050092DECLARE_RWSEM(css_set_rwsem);
93EXPORT_SYMBOL_GPL(cgroup_mutex);
94EXPORT_SYMBOL_GPL(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070095#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070096static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050097static DECLARE_RWSEM(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070098#endif
99
Tejun Heo69e943b2014-02-08 10:36:58 -0500100/*
101 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
102 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
103 */
104static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700105
Tejun Heoace2bee2014-02-11 11:52:47 -0500106#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500107 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -0500108 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500109 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500110 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500111
Ben Blumaae8aab2010-03-10 15:22:07 -0800112/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500113 * cgroup destruction makes heavy use of work items and there can be a lot
114 * of concurrent destructions. Use a separate workqueue so that cgroup
115 * destruction work items don't end up filling up max_active of system_wq
116 * which may lead to deadlock.
117 */
118static struct workqueue_struct *cgroup_destroy_wq;
119
120/*
Tejun Heob1a21362013-11-29 10:42:58 -0500121 * pidlist destructions need to be flushed on cgroup destruction. Use a
122 * separate workqueue as flush domain.
123 */
124static struct workqueue_struct *cgroup_pidlist_destroy_wq;
125
Tejun Heo3ed80a62014-02-08 10:36:58 -0500126/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500127#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500128static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700129#include <linux/cgroup_subsys.h>
130};
Tejun Heo073219e2014-02-08 10:36:58 -0500131#undef SUBSYS
132
133/* array of cgroup subsystem names */
134#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
135static const char *cgroup_subsys_name[] = {
136#include <linux/cgroup_subsys.h>
137};
138#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700139
Paul Menageddbcc7e2007-10-18 23:39:30 -0700140/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400141 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700142 * unattached - it never has more than a single cgroup, and all tasks are
143 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700144 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400145struct cgroup_root cgrp_dfl_root;
Tejun Heo9871bf92013-06-24 15:21:47 -0700146
Tejun Heoa2dd4242014-03-19 10:23:55 -0400147/*
148 * The default hierarchy always exists but is hidden until mounted for the
149 * first time. This is for backward compatibility.
150 */
151static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700152
153/* The list of hierarchy roots */
154
Tejun Heo9871bf92013-06-24 15:21:47 -0700155static LIST_HEAD(cgroup_roots);
156static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700157
Tejun Heo3417ae12014-02-08 10:37:01 -0500158/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700159static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700160
Li Zefan794611a2013-06-18 18:53:53 +0800161/*
162 * Assign a monotonically increasing serial number to cgroups. It
163 * guarantees cgroups with bigger numbers are newer than those with smaller
164 * numbers. Also, as cgroups are always appended to the parent's
165 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700166 * the ascending serial number order on the list. Protected by
167 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800168 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700169static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800170
Paul Menageddbcc7e2007-10-18 23:39:30 -0700171/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800172 * check for fork/exit handlers to call. This avoids us having to do
173 * extra work in the fork/exit path if none of the subsystems need to
174 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700175 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700176static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700177
Tejun Heo628f7cd2013-06-28 16:24:11 -0700178static struct cftype cgroup_base_files[];
179
Tejun Heo59f52962014-02-11 11:52:49 -0500180static void cgroup_put(struct cgroup *cgrp);
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400181static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo5df36032014-03-19 10:23:54 -0400182 unsigned long ss_mask);
Tejun Heof20104d2013-08-13 20:22:50 -0400183static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800184static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400185static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
186 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500187static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800188
Tejun Heo95109b62013-08-08 20:11:27 -0400189/**
190 * cgroup_css - obtain a cgroup's css for the specified subsystem
191 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400192 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400193 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400194 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
195 * function must be called either under cgroup_mutex or rcu_read_lock() and
196 * the caller is responsible for pinning the returned css if it wants to
197 * keep accessing it outside the said locks. This function may return
198 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400199 */
200static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400201 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400202{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400203 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500204 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500205 lockdep_is_held(&cgroup_tree_mutex) ||
206 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400207 else
208 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400209}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700210
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400211/**
212 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
213 * @cgrp: the cgroup of interest
214 * @ss: the subsystem of interest (%NULL returns the dummy_css)
215 *
216 * Similar to cgroup_css() but returns the effctive css, which is defined
217 * as the matching css of the nearest ancestor including self which has @ss
218 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
219 * function is guaranteed to return non-NULL css.
220 */
221static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
222 struct cgroup_subsys *ss)
223{
224 lockdep_assert_held(&cgroup_mutex);
225
226 if (!ss)
227 return &cgrp->dummy_css;
228
229 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
230 return NULL;
231
232 while (cgrp->parent &&
233 !(cgrp->parent->child_subsys_mask & (1 << ss->id)))
234 cgrp = cgrp->parent;
235
236 return cgroup_css(cgrp, ss);
237}
238
Paul Menageddbcc7e2007-10-18 23:39:30 -0700239/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700240static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700241{
Tejun Heo54766d42013-06-12 21:04:53 -0700242 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700243}
244
Tejun Heo59f52962014-02-11 11:52:49 -0500245struct cgroup_subsys_state *seq_css(struct seq_file *seq)
246{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500247 struct kernfs_open_file *of = seq->private;
248 struct cgroup *cgrp = of->kn->parent->priv;
249 struct cftype *cft = seq_cft(seq);
250
251 /*
252 * This is open and unprotected implementation of cgroup_css().
253 * seq_css() is only called from a kernfs file operation which has
254 * an active reference on the file. Because all the subsystem
255 * files are drained before a css is disassociated with a cgroup,
256 * the matching css from the cgroup's subsys table is guaranteed to
257 * be and stay valid until the enclosing operation is complete.
258 */
259 if (cft->ss)
260 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
261 else
262 return &cgrp->dummy_css;
Tejun Heo59f52962014-02-11 11:52:49 -0500263}
264EXPORT_SYMBOL_GPL(seq_css);
265
Li Zefan78574cf2013-04-08 19:00:38 -0700266/**
267 * cgroup_is_descendant - test ancestry
268 * @cgrp: the cgroup to be tested
269 * @ancestor: possible ancestor of @cgrp
270 *
271 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
272 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
273 * and @ancestor are accessible.
274 */
275bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
276{
277 while (cgrp) {
278 if (cgrp == ancestor)
279 return true;
280 cgrp = cgrp->parent;
281 }
282 return false;
283}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700284
Adrian Bunke9685a02008-02-07 00:13:46 -0800285static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700286{
287 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700288 (1 << CGRP_RELEASABLE) |
289 (1 << CGRP_NOTIFY_ON_RELEASE);
290 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700291}
292
Adrian Bunke9685a02008-02-07 00:13:46 -0800293static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700294{
Paul Menagebd89aab2007-10-18 23:40:44 -0700295 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700296}
297
Tejun Heo30159ec2013-06-25 11:53:37 -0700298/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500299 * for_each_css - iterate all css's of a cgroup
300 * @css: the iteration cursor
301 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
302 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700303 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400304 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700305 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500306#define for_each_css(css, ssid, cgrp) \
307 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
308 if (!((css) = rcu_dereference_check( \
309 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500310 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500311 lockdep_is_held(&cgroup_mutex)))) { } \
312 else
313
314/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400315 * for_each_e_css - iterate all effective css's of a cgroup
316 * @css: the iteration cursor
317 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
318 * @cgrp: the target cgroup to iterate css's of
319 *
320 * Should be called under cgroup_[tree_]mutex.
321 */
322#define for_each_e_css(css, ssid, cgrp) \
323 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
324 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
325 ; \
326 else
327
328/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500329 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700330 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500331 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700332 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500333#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500334 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
335 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700336
Tejun Heo985ed672014-03-19 10:23:53 -0400337/* iterate across the hierarchies */
338#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700339 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700340
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700341/**
342 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
343 * @cgrp: the cgroup to be checked for liveness
344 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700345 * On success, returns true; the mutex should be later unlocked. On
346 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700347 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700348static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700349{
350 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700351 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700352 mutex_unlock(&cgroup_mutex);
353 return false;
354 }
355 return true;
356}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700357
Paul Menage81a6a5c2007-10-18 23:39:38 -0700358/* the list of cgroups eligible for automatic release. Protected by
359 * release_list_lock */
360static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200361static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700362static void cgroup_release_agent(struct work_struct *work);
363static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700364static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700365
Tejun Heo69d02062013-06-12 21:04:50 -0700366/*
367 * A cgroup can be associated with multiple css_sets as different tasks may
368 * belong to different cgroups on different hierarchies. In the other
369 * direction, a css_set is naturally associated with multiple cgroups.
370 * This M:N relationship is represented by the following link structure
371 * which exists for each association and allows traversing the associations
372 * from both sides.
373 */
374struct cgrp_cset_link {
375 /* the cgroup and css_set this link associates */
376 struct cgroup *cgrp;
377 struct css_set *cset;
378
379 /* list of cgrp_cset_links anchored at cgrp->cset_links */
380 struct list_head cset_link;
381
382 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
383 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700384};
385
Tejun Heo172a2c062014-03-19 10:23:53 -0400386/*
387 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700388 * hierarchies being mounted. It contains a pointer to the root state
389 * for each subsystem. Also used to anchor the list of css_sets. Not
390 * reference-counted, to improve performance when child cgroups
391 * haven't been created.
392 */
Tejun Heo172a2c062014-03-19 10:23:53 -0400393static struct css_set init_css_set = {
394 .refcount = ATOMIC_INIT(1),
395 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
396 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
397 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
398 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
399 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
400};
Paul Menage817929e2007-10-18 23:39:36 -0700401
Tejun Heo172a2c062014-03-19 10:23:53 -0400402static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700403
Paul Menage7717f7b2009-09-23 15:56:22 -0700404/*
405 * hash table for cgroup groups. This improves the performance to find
406 * an existing css_set. This hash doesn't (currently) take into
407 * account cgroups in empty hierarchies.
408 */
Li Zefan472b1052008-04-29 01:00:11 -0700409#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800410static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700411
Li Zefan0ac801f2013-01-10 11:49:27 +0800412static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700413{
Li Zefan0ac801f2013-01-10 11:49:27 +0800414 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700415 struct cgroup_subsys *ss;
416 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700417
Tejun Heo30159ec2013-06-25 11:53:37 -0700418 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800419 key += (unsigned long)css[i];
420 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700421
Li Zefan0ac801f2013-01-10 11:49:27 +0800422 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700423}
424
Tejun Heo89c55092014-02-13 06:58:40 -0500425static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700426{
Tejun Heo69d02062013-06-12 21:04:50 -0700427 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700428
Tejun Heo89c55092014-02-13 06:58:40 -0500429 lockdep_assert_held(&css_set_rwsem);
430
431 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700432 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700433
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700434 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700435 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700436 css_set_count--;
437
Tejun Heo69d02062013-06-12 21:04:50 -0700438 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700439 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700440
Tejun Heo69d02062013-06-12 21:04:50 -0700441 list_del(&link->cset_link);
442 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800443
Tejun Heo96d365e2014-02-13 06:58:40 -0500444 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700445 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700446 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700447 set_bit(CGRP_RELEASABLE, &cgrp->flags);
448 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700449 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700450
451 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700452 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700453
Tejun Heo5abb8852013-06-12 21:04:49 -0700454 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700455}
456
Tejun Heo89c55092014-02-13 06:58:40 -0500457static void put_css_set(struct css_set *cset, bool taskexit)
458{
459 /*
460 * Ensure that the refcount doesn't hit zero while any readers
461 * can see it. Similar to atomic_dec_and_lock(), but for an
462 * rwlock
463 */
464 if (atomic_add_unless(&cset->refcount, -1, 1))
465 return;
466
467 down_write(&css_set_rwsem);
468 put_css_set_locked(cset, taskexit);
469 up_write(&css_set_rwsem);
470}
471
Paul Menage817929e2007-10-18 23:39:36 -0700472/*
473 * refcounted get/put for css_set objects
474 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700475static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700476{
Tejun Heo5abb8852013-06-12 21:04:49 -0700477 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700478}
479
Tejun Heob326f9d2013-06-24 15:21:48 -0700480/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700481 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700482 * @cset: candidate css_set being tested
483 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700484 * @new_cgrp: cgroup that's being entered by the task
485 * @template: desired set of css pointers in css_set (pre-calculated)
486 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800487 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700488 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
489 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700490static bool compare_css_sets(struct css_set *cset,
491 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700492 struct cgroup *new_cgrp,
493 struct cgroup_subsys_state *template[])
494{
495 struct list_head *l1, *l2;
496
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400497 /*
498 * On the default hierarchy, there can be csets which are
499 * associated with the same set of cgroups but different csses.
500 * Let's first ensure that csses match.
501 */
502 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700503 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700504
505 /*
506 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400507 * different cgroups in hierarchies. As different cgroups may
508 * share the same effective css, this comparison is always
509 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700510 */
Tejun Heo69d02062013-06-12 21:04:50 -0700511 l1 = &cset->cgrp_links;
512 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700513 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700514 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700515 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700516
517 l1 = l1->next;
518 l2 = l2->next;
519 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700520 if (l1 == &cset->cgrp_links) {
521 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700522 break;
523 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700524 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700525 }
526 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700527 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
528 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
529 cgrp1 = link1->cgrp;
530 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700531 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700532 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700533
534 /*
535 * If this hierarchy is the hierarchy of the cgroup
536 * that's changing, then we need to check that this
537 * css_set points to the new cgroup; if it's any other
538 * hierarchy, then this css_set should point to the
539 * same cgroup as the old css_set.
540 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700541 if (cgrp1->root == new_cgrp->root) {
542 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700543 return false;
544 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700545 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700546 return false;
547 }
548 }
549 return true;
550}
551
Tejun Heob326f9d2013-06-24 15:21:48 -0700552/**
553 * find_existing_css_set - init css array and find the matching css_set
554 * @old_cset: the css_set that we're using before the cgroup transition
555 * @cgrp: the cgroup that we're moving into
556 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700557 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700558static struct css_set *find_existing_css_set(struct css_set *old_cset,
559 struct cgroup *cgrp,
560 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700561{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400562 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700563 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700564 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800565 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700566 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700567
Ben Blumaae8aab2010-03-10 15:22:07 -0800568 /*
569 * Build the set of subsystem state objects that we want to see in the
570 * new css_set. while subsystems can change globally, the entries here
571 * won't change, so no need for locking.
572 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700573 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400574 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400575 /*
576 * @ss is in this hierarchy, so we want the
577 * effective css from @cgrp.
578 */
579 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700580 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400581 /*
582 * @ss is not in this hierarchy, so we don't want
583 * to change the css.
584 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700585 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700586 }
587 }
588
Li Zefan0ac801f2013-01-10 11:49:27 +0800589 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700590 hash_for_each_possible(css_set_table, cset, hlist, key) {
591 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700592 continue;
593
594 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700595 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700596 }
Paul Menage817929e2007-10-18 23:39:36 -0700597
598 /* No existing cgroup group matched */
599 return NULL;
600}
601
Tejun Heo69d02062013-06-12 21:04:50 -0700602static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700603{
Tejun Heo69d02062013-06-12 21:04:50 -0700604 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700605
Tejun Heo69d02062013-06-12 21:04:50 -0700606 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
607 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700608 kfree(link);
609 }
610}
611
Tejun Heo69d02062013-06-12 21:04:50 -0700612/**
613 * allocate_cgrp_cset_links - allocate cgrp_cset_links
614 * @count: the number of links to allocate
615 * @tmp_links: list_head the allocated links are put on
616 *
617 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
618 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700619 */
Tejun Heo69d02062013-06-12 21:04:50 -0700620static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700621{
Tejun Heo69d02062013-06-12 21:04:50 -0700622 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700623 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700624
625 INIT_LIST_HEAD(tmp_links);
626
Li Zefan36553432008-07-29 22:33:19 -0700627 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700628 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700629 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700630 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700631 return -ENOMEM;
632 }
Tejun Heo69d02062013-06-12 21:04:50 -0700633 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700634 }
635 return 0;
636}
637
Li Zefanc12f65d2009-01-07 18:07:42 -0800638/**
639 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700640 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700641 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800642 * @cgrp: the destination cgroup
643 */
Tejun Heo69d02062013-06-12 21:04:50 -0700644static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
645 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800646{
Tejun Heo69d02062013-06-12 21:04:50 -0700647 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800648
Tejun Heo69d02062013-06-12 21:04:50 -0700649 BUG_ON(list_empty(tmp_links));
650 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
651 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700652 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700653 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700654 /*
655 * Always add links to the tail of the list so that the list
656 * is sorted by order of hierarchy creation
657 */
Tejun Heo69d02062013-06-12 21:04:50 -0700658 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800659}
660
Tejun Heob326f9d2013-06-24 15:21:48 -0700661/**
662 * find_css_set - return a new css_set with one cgroup updated
663 * @old_cset: the baseline css_set
664 * @cgrp: the cgroup to be updated
665 *
666 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
667 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700668 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700669static struct css_set *find_css_set(struct css_set *old_cset,
670 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700671{
Tejun Heob326f9d2013-06-24 15:21:48 -0700672 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700673 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700674 struct list_head tmp_links;
675 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800676 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700677
Tejun Heob326f9d2013-06-24 15:21:48 -0700678 lockdep_assert_held(&cgroup_mutex);
679
Paul Menage817929e2007-10-18 23:39:36 -0700680 /* First see if we already have a cgroup group that matches
681 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500682 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700683 cset = find_existing_css_set(old_cset, cgrp, template);
684 if (cset)
685 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500686 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700687
Tejun Heo5abb8852013-06-12 21:04:49 -0700688 if (cset)
689 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700690
Tejun Heof4f4be22013-06-12 21:04:51 -0700691 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700692 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700693 return NULL;
694
Tejun Heo69d02062013-06-12 21:04:50 -0700695 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700696 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700697 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700698 return NULL;
699 }
700
Tejun Heo5abb8852013-06-12 21:04:49 -0700701 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700702 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700703 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500704 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500705 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500706 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700707 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700708
709 /* Copy the set of subsystem state objects generated in
710 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700711 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700712
Tejun Heo96d365e2014-02-13 06:58:40 -0500713 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700714 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700715 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700716 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700717
Paul Menage7717f7b2009-09-23 15:56:22 -0700718 if (c->root == cgrp->root)
719 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700720 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700721 }
Paul Menage817929e2007-10-18 23:39:36 -0700722
Tejun Heo69d02062013-06-12 21:04:50 -0700723 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700724
Paul Menage817929e2007-10-18 23:39:36 -0700725 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700726
727 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700728 key = css_set_hash(cset->subsys);
729 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700730
Tejun Heo96d365e2014-02-13 06:58:40 -0500731 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700732
Tejun Heo5abb8852013-06-12 21:04:49 -0700733 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700734}
735
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400736static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700737{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400738 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500739
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400740 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500741}
742
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400743static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500744{
745 int id;
746
747 lockdep_assert_held(&cgroup_mutex);
748
Tejun Heo985ed672014-03-19 10:23:53 -0400749 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500750 if (id < 0)
751 return id;
752
753 root->hierarchy_id = id;
754 return 0;
755}
756
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400757static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500758{
759 lockdep_assert_held(&cgroup_mutex);
760
761 if (root->hierarchy_id) {
762 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
763 root->hierarchy_id = 0;
764 }
765}
766
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400767static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500768{
769 if (root) {
770 /* hierarhcy ID shoulid already have been released */
771 WARN_ON_ONCE(root->hierarchy_id);
772
773 idr_destroy(&root->cgroup_idr);
774 kfree(root);
775 }
776}
777
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400778static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500779{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400780 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500781 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500782
Tejun Heo2bd59d42014-02-11 11:52:49 -0500783 mutex_lock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500784 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500785
Tejun Heo776f02f2014-02-12 09:29:50 -0500786 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500787 BUG_ON(!list_empty(&cgrp->children));
788
Tejun Heof2e85d52014-02-11 11:52:49 -0500789 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400790 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500791
792 /*
793 * Release all the links from cset_links to this hierarchy's
794 * root cgroup
795 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500796 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500797
798 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
799 list_del(&link->cset_link);
800 list_del(&link->cgrp_link);
801 kfree(link);
802 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500803 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500804
805 if (!list_empty(&root->root_list)) {
806 list_del(&root->root_list);
807 cgroup_root_count--;
808 }
809
810 cgroup_exit_root_id(root);
811
812 mutex_unlock(&cgroup_mutex);
813 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500814
Tejun Heo2bd59d42014-02-11 11:52:49 -0500815 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500816 cgroup_free_root(root);
817}
818
Tejun Heoceb6a082014-02-25 10:04:02 -0500819/* look up cgroup associated with given css_set on the specified hierarchy */
820static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400821 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700822{
Paul Menage7717f7b2009-09-23 15:56:22 -0700823 struct cgroup *res = NULL;
824
Tejun Heo96d365e2014-02-13 06:58:40 -0500825 lockdep_assert_held(&cgroup_mutex);
826 lockdep_assert_held(&css_set_rwsem);
827
Tejun Heo5abb8852013-06-12 21:04:49 -0700828 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400829 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700830 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700831 struct cgrp_cset_link *link;
832
833 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700834 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700835
Paul Menage7717f7b2009-09-23 15:56:22 -0700836 if (c->root == root) {
837 res = c;
838 break;
839 }
840 }
841 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500842
Paul Menage7717f7b2009-09-23 15:56:22 -0700843 BUG_ON(!res);
844 return res;
845}
846
847/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500848 * Return the cgroup for "task" from the given hierarchy. Must be
849 * called with cgroup_mutex and css_set_rwsem held.
850 */
851static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400852 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500853{
854 /*
855 * No need to lock the task - since we hold cgroup_mutex the
856 * task can't change groups, so the only thing that can happen
857 * is that it exits and its css is set back to init_css_set.
858 */
859 return cset_cgroup_from_root(task_css_set(task), root);
860}
861
862/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700863 * A task must hold cgroup_mutex to modify cgroups.
864 *
865 * Any task can increment and decrement the count field without lock.
866 * So in general, code holding cgroup_mutex can't rely on the count
867 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800868 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700869 * means that no tasks are currently attached, therefore there is no
870 * way a task attached to that cgroup can fork (the other way to
871 * increment the count). So code holding cgroup_mutex can safely
872 * assume that if the count is zero, it will stay zero. Similarly, if
873 * a task holds cgroup_mutex on a cgroup with zero count, it
874 * knows that the cgroup won't be removed, as cgroup_rmdir()
875 * needs that mutex.
876 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700877 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
878 * (usually) take cgroup_mutex. These are the two most performance
879 * critical pieces of code here. The exception occurs on cgroup_exit(),
880 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
881 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800882 * to the release agent with the name of the cgroup (path relative to
883 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700884 *
885 * A cgroup can only be deleted if both its 'count' of using tasks
886 * is zero, and its list of 'children' cgroups is empty. Since all
887 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400888 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700889 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400890 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700891 *
892 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800893 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700894 */
895
Tejun Heo628f7cd2013-06-28 16:24:11 -0700896static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500897static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700898static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700899
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500900static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
901 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700902{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500903 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
904 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
905 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
906 cft->ss->name, cft->name);
907 else
908 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
909 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700910}
911
Tejun Heof2e85d52014-02-11 11:52:49 -0500912/**
913 * cgroup_file_mode - deduce file mode of a control file
914 * @cft: the control file in question
915 *
916 * returns cft->mode if ->mode is not 0
917 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
918 * returns S_IRUGO if it has only a read handler
919 * returns S_IWUSR if it has only a write hander
920 */
921static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +0800922{
Tejun Heof2e85d52014-02-11 11:52:49 -0500923 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +0800924
Tejun Heof2e85d52014-02-11 11:52:49 -0500925 if (cft->mode)
926 return cft->mode;
927
928 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
929 mode |= S_IRUGO;
930
931 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
932 cft->trigger)
933 mode |= S_IWUSR;
934
935 return mode;
Li Zefan65dff752013-03-01 15:01:56 +0800936}
937
Li Zefanbe445622013-01-24 14:31:42 +0800938static void cgroup_free_fn(struct work_struct *work)
939{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700940 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800941
Tejun Heo3c9c8252014-02-12 09:29:50 -0500942 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -0500943 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800944
Tejun Heo776f02f2014-02-12 09:29:50 -0500945 if (cgrp->parent) {
946 /*
947 * We get a ref to the parent, and put the ref when this
948 * cgroup is being freed, so it's guaranteed that the
949 * parent won't be destroyed before its children.
950 */
951 cgroup_put(cgrp->parent);
952 kernfs_put(cgrp->kn);
953 kfree(cgrp);
954 } else {
955 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400956 * This is root cgroup's refcnt reaching zero, which
Tejun Heo776f02f2014-02-12 09:29:50 -0500957 * indicates that the root should be released.
958 */
959 cgroup_destroy_root(cgrp->root);
960 }
Li Zefanbe445622013-01-24 14:31:42 +0800961}
962
963static void cgroup_free_rcu(struct rcu_head *head)
964{
965 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
966
Tejun Heoea15f8c2013-06-13 19:27:42 -0700967 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500968 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800969}
970
Tejun Heo59f52962014-02-11 11:52:49 -0500971static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500973 WARN_ON_ONCE(cgroup_is_dead(cgrp));
974 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
975 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976}
977
Tejun Heo59f52962014-02-11 11:52:49 -0500978static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700979{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500980 if (!atomic_dec_and_test(&cgrp->refcnt))
981 return;
Tejun Heo776f02f2014-02-12 09:29:50 -0500982 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -0500983 return;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984
Tejun Heo2bd59d42014-02-11 11:52:49 -0500985 /*
986 * XXX: cgrp->id is only used to look up css's. As cgroup and
987 * css's lifetimes will be decoupled, it should be made
988 * per-subsystem and moved to css->id so that lookups are
989 * successful until the target css is released.
990 */
991 mutex_lock(&cgroup_mutex);
992 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
993 mutex_unlock(&cgroup_mutex);
994 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995
Tejun Heo2bd59d42014-02-11 11:52:49 -0500996 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997}
998
Li Zefan2739d3c2013-01-21 18:18:33 +0800999static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001001 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002
Tejun Heoace2bee2014-02-11 11:52:47 -05001003 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001004 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001005}
1006
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001007/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001008 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001009 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001010 * @subsys_mask: mask of the subsystem ids whose files should be removed
1011 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07001012static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001013{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001014 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001015 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001016
Tejun Heob420ba72013-07-12 12:34:02 -07001017 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001018 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001019
1020 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001021 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001022 list_for_each_entry(cfts, &ss->cfts, node)
1023 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001024 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025}
1026
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001027static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo5df36032014-03-19 10:23:54 -04001028 unsigned long ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029{
Tejun Heo30159ec2013-06-25 11:53:37 -07001030 struct cgroup_subsys *ss;
Tejun Heo5df36032014-03-19 10:23:54 -04001031 int ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032
Tejun Heoace2bee2014-02-11 11:52:47 -05001033 lockdep_assert_held(&cgroup_tree_mutex);
1034 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001035
Tejun Heo5df36032014-03-19 10:23:54 -04001036 for_each_subsys(ss, ssid) {
1037 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001038 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001039
Tejun Heo5df36032014-03-19 10:23:54 -04001040 /* if @ss is on the dummy_root, we can always move it */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001041 if (ss->root == &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001042 continue;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001043
Tejun Heo5df36032014-03-19 10:23:54 -04001044 /* if @ss has non-root cgroups attached to it, can't move */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001045 if (!list_empty(&ss->root->cgrp.children))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001046 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047
Tejun Heo5df36032014-03-19 10:23:54 -04001048 /* can't move between two non-dummy roots either */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001049 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001050 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 }
1052
Tejun Heoa2dd4242014-03-19 10:23:55 -04001053 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1054 if (ret) {
1055 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001056 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057
Tejun Heoa2dd4242014-03-19 10:23:55 -04001058 /*
1059 * Rebinding back to the default root is not allowed to
1060 * fail. Using both default and non-default roots should
1061 * be rare. Moving subsystems back and forth even more so.
1062 * Just warn about it and continue.
1063 */
1064 if (cgrp_dfl_root_visible) {
1065 pr_warning("cgroup: failed to create files (%d) while rebinding 0x%lx to default root\n",
1066 ret, ss_mask);
1067 pr_warning("cgroup: you may retry by moving them to a different hierarchy and unbinding\n");
1068 }
Tejun Heo5df36032014-03-19 10:23:54 -04001069 }
Tejun Heo31261212013-06-28 17:07:30 -07001070
1071 /*
1072 * Nothing can fail from this point on. Remove files for the
1073 * removed subsystems and rebind each subsystem.
1074 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001075 mutex_unlock(&cgroup_mutex);
Tejun Heo5df36032014-03-19 10:23:54 -04001076 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001077 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001078 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo4ac06012014-02-11 11:52:47 -05001079 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001080
Tejun Heo5df36032014-03-19 10:23:54 -04001081 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001082 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001083 struct cgroup_subsys_state *css;
Tejun Heo30159ec2013-06-25 11:53:37 -07001084
Tejun Heo5df36032014-03-19 10:23:54 -04001085 if (!(ss_mask & (1 << ssid)))
1086 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001087
Tejun Heo5df36032014-03-19 10:23:54 -04001088 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001089 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001090
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001091 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001092
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001093 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1094 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001095 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001096 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001097
Tejun Heof392e512014-04-23 11:13:14 -04001098 src_root->subsys_mask &= ~(1 << ssid);
1099 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1100
1101 dst_root->subsys_mask |= 1 << ssid;
1102 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001103
Tejun Heo5df36032014-03-19 10:23:54 -04001104 if (ss->bind)
1105 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107
Tejun Heoa2dd4242014-03-19 10:23:55 -04001108 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109 return 0;
1110}
1111
Tejun Heo2bd59d42014-02-11 11:52:49 -05001112static int cgroup_show_options(struct seq_file *seq,
1113 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001115 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001116 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001117 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118
Tejun Heob85d2042013-12-06 15:11:57 -05001119 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001120 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001121 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001122 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1123 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001124 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001126 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001127 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001128
1129 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001130 if (strlen(root->release_agent_path))
1131 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001132 spin_unlock(&release_agent_path_lock);
1133
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001134 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001135 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001136 if (strlen(root->name))
1137 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001138 return 0;
1139}
1140
1141struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001142 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001143 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001144 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001145 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001146 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001147 /* User explicitly requested empty subsystem */
1148 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001149};
1150
Ben Blumaae8aab2010-03-10 15:22:07 -08001151/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001152 * Convert a hierarchy specifier into a bitmask of subsystems and
1153 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1154 * array. This function takes refcounts on subsystems to be used, unless it
1155 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001156 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001157static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001158{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001159 char *token, *o = data;
1160 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001161 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001162 struct cgroup_subsys *ss;
1163 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001164
Ben Blumaae8aab2010-03-10 15:22:07 -08001165 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1166
Li Zefanf9ab5b52009-06-17 16:26:33 -07001167#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001168 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001169#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001170
Paul Menagec6d57f32009-09-23 15:56:19 -07001171 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001172
1173 while ((token = strsep(&o, ",")) != NULL) {
1174 if (!*token)
1175 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001176 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001177 /* Explicitly have no subsystems */
1178 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001179 continue;
1180 }
1181 if (!strcmp(token, "all")) {
1182 /* Mutually exclusive option 'all' + subsystem name */
1183 if (one_ss)
1184 return -EINVAL;
1185 all_ss = true;
1186 continue;
1187 }
Tejun Heo873fe092013-04-14 20:15:26 -07001188 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1189 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1190 continue;
1191 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001192 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001193 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001194 continue;
1195 }
1196 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001197 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001198 continue;
1199 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001200 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001201 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001202 continue;
1203 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001204 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001205 /* Specifying two release agents is forbidden */
1206 if (opts->release_agent)
1207 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001208 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001209 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001210 if (!opts->release_agent)
1211 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001212 continue;
1213 }
1214 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001215 const char *name = token + 5;
1216 /* Can't specify an empty name */
1217 if (!strlen(name))
1218 return -EINVAL;
1219 /* Must match [\w.-]+ */
1220 for (i = 0; i < strlen(name); i++) {
1221 char c = name[i];
1222 if (isalnum(c))
1223 continue;
1224 if ((c == '.') || (c == '-') || (c == '_'))
1225 continue;
1226 return -EINVAL;
1227 }
1228 /* Specifying two names is forbidden */
1229 if (opts->name)
1230 return -EINVAL;
1231 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001232 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001233 GFP_KERNEL);
1234 if (!opts->name)
1235 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001236
1237 continue;
1238 }
1239
Tejun Heo30159ec2013-06-25 11:53:37 -07001240 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001241 if (strcmp(token, ss->name))
1242 continue;
1243 if (ss->disabled)
1244 continue;
1245
1246 /* Mutually exclusive option 'all' + subsystem name */
1247 if (all_ss)
1248 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001249 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001250 one_ss = true;
1251
1252 break;
1253 }
1254 if (i == CGROUP_SUBSYS_COUNT)
1255 return -ENOENT;
1256 }
1257
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001258 /* Consistency checks */
1259
Tejun Heo873fe092013-04-14 20:15:26 -07001260 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1261 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1262
Tejun Heod3ba07c2014-02-13 06:58:38 -05001263 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1264 opts->cpuset_clone_children || opts->release_agent ||
1265 opts->name) {
1266 pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001267 return -EINVAL;
1268 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001269 } else {
1270 /*
1271 * If the 'all' option was specified select all the
1272 * subsystems, otherwise if 'none', 'name=' and a subsystem
1273 * name options were not specified, let's default to 'all'
1274 */
1275 if (all_ss || (!one_ss && !opts->none && !opts->name))
1276 for_each_subsys(ss, i)
1277 if (!ss->disabled)
1278 set_bit(i, &opts->subsys_mask);
Tejun Heo873fe092013-04-14 20:15:26 -07001279
Tejun Heoa2dd4242014-03-19 10:23:55 -04001280 /*
1281 * We either have to specify by name or by subsystems. (So
1282 * all empty hierarchies must have a name).
1283 */
1284 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001285 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001286 }
1287
Li Zefanf9ab5b52009-06-17 16:26:33 -07001288 /*
1289 * Option noprefix was introduced just for backward compatibility
1290 * with the old cpuset, so we allow noprefix only if mounting just
1291 * the cpuset subsystem.
1292 */
Tejun Heo93438622013-04-14 20:15:25 -07001293 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001294 return -EINVAL;
1295
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001296
1297 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001298 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001299 return -EINVAL;
1300
Paul Menageddbcc7e2007-10-18 23:39:30 -07001301 return 0;
1302}
1303
Tejun Heo2bd59d42014-02-11 11:52:49 -05001304static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001305{
1306 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001307 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001308 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001309 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001310
Tejun Heo873fe092013-04-14 20:15:26 -07001311 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1312 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1313 return -EINVAL;
1314 }
1315
Tejun Heoace2bee2014-02-11 11:52:47 -05001316 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001317 mutex_lock(&cgroup_mutex);
1318
1319 /* See what subsystems are wanted */
1320 ret = parse_cgroupfs_options(data, &opts);
1321 if (ret)
1322 goto out_unlock;
1323
Tejun Heof392e512014-04-23 11:13:14 -04001324 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001325 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1326 task_tgid_nr(current), current->comm);
1327
Tejun Heof392e512014-04-23 11:13:14 -04001328 added_mask = opts.subsys_mask & ~root->subsys_mask;
1329 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001330
Ben Blumcf5d5942010-03-10 15:22:09 -08001331 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001332 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001333 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001334 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1335 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1336 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001337 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001338 goto out_unlock;
1339 }
1340
Tejun Heof172e672013-06-28 17:07:30 -07001341 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001342 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001343 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001344 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001345 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346
Tejun Heo5df36032014-03-19 10:23:54 -04001347 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001348 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001349 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001350
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001351 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001352
Tejun Heo69e943b2014-02-08 10:36:58 -05001353 if (opts.release_agent) {
1354 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001355 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001356 spin_unlock(&release_agent_path_lock);
1357 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001358 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001359 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001360 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001361 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001362 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001363 return ret;
1364}
1365
Tejun Heoafeb0f92014-02-13 06:58:39 -05001366/*
1367 * To reduce the fork() overhead for systems that are not actually using
1368 * their cgroups capability, we don't maintain the lists running through
1369 * each css_set to its tasks until we see the list actually used - in other
1370 * words after the first mount.
1371 */
1372static bool use_task_css_set_links __read_mostly;
1373
1374static void cgroup_enable_task_cg_lists(void)
1375{
1376 struct task_struct *p, *g;
1377
Tejun Heo96d365e2014-02-13 06:58:40 -05001378 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001379
1380 if (use_task_css_set_links)
1381 goto out_unlock;
1382
1383 use_task_css_set_links = true;
1384
1385 /*
1386 * We need tasklist_lock because RCU is not safe against
1387 * while_each_thread(). Besides, a forking task that has passed
1388 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1389 * is not guaranteed to have its child immediately visible in the
1390 * tasklist if we walk through it with RCU.
1391 */
1392 read_lock(&tasklist_lock);
1393 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001394 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1395 task_css_set(p) != &init_css_set);
1396
1397 /*
1398 * We should check if the process is exiting, otherwise
1399 * it will race with cgroup_exit() in that the list
1400 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001401 * Do it while holding siglock so that we don't end up
1402 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001403 */
Tejun Heof153ad12014-02-25 09:56:49 -05001404 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001405 if (!(p->flags & PF_EXITING)) {
1406 struct css_set *cset = task_css_set(p);
1407
1408 list_add(&p->cg_list, &cset->tasks);
1409 get_css_set(cset);
1410 }
Tejun Heof153ad12014-02-25 09:56:49 -05001411 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001412 } while_each_thread(g, p);
1413 read_unlock(&tasklist_lock);
1414out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001415 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001416}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001417
Paul Menagecc31edc2008-10-18 20:28:04 -07001418static void init_cgroup_housekeeping(struct cgroup *cgrp)
1419{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001420 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001421 INIT_LIST_HEAD(&cgrp->sibling);
1422 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001423 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001424 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001425 INIT_LIST_HEAD(&cgrp->pidlists);
1426 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001427 cgrp->dummy_css.cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001428}
Paul Menagec6d57f32009-09-23 15:56:19 -07001429
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001430static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001431 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001432{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001433 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001434
Paul Menageddbcc7e2007-10-18 23:39:30 -07001435 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001436 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001437 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001438 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001439 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001440
Paul Menagec6d57f32009-09-23 15:56:19 -07001441 root->flags = opts->flags;
1442 if (opts->release_agent)
1443 strcpy(root->release_agent_path, opts->release_agent);
1444 if (opts->name)
1445 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001446 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001447 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001448}
1449
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001450static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001451{
Tejun Heod427dfe2014-02-11 11:52:48 -05001452 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001453 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001454 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001455 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001456
Tejun Heod427dfe2014-02-11 11:52:48 -05001457 lockdep_assert_held(&cgroup_tree_mutex);
1458 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001459
Tejun Heod427dfe2014-02-11 11:52:48 -05001460 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1461 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001462 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001463 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001464
Tejun Heod427dfe2014-02-11 11:52:48 -05001465 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001466 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001467 * but that's OK - it can only be increased by someone holding
1468 * cgroup_lock, and that's us. The worst that can happen is that we
1469 * have some link structures left over
1470 */
1471 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001472 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001473 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001474
Tejun Heo985ed672014-03-19 10:23:53 -04001475 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001476 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001477 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478
Tejun Heo2bd59d42014-02-11 11:52:49 -05001479 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1480 KERNFS_ROOT_CREATE_DEACTIVATED,
1481 root_cgrp);
1482 if (IS_ERR(root->kf_root)) {
1483 ret = PTR_ERR(root->kf_root);
1484 goto exit_root_id;
1485 }
1486 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001487
Tejun Heod427dfe2014-02-11 11:52:48 -05001488 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1489 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001490 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001491
Tejun Heo5df36032014-03-19 10:23:54 -04001492 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001493 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001494 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001495
Tejun Heod427dfe2014-02-11 11:52:48 -05001496 /*
1497 * There must be no failure case after here, since rebinding takes
1498 * care of subsystems' refcounts, which are explicitly dropped in
1499 * the failure exit path.
1500 */
1501 list_add(&root->root_list, &cgroup_roots);
1502 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001503
Tejun Heod427dfe2014-02-11 11:52:48 -05001504 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001505 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001506 * objects.
1507 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001508 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001509 hash_for_each(css_set_table, i, cset, hlist)
1510 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001511 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512
Tejun Heod427dfe2014-02-11 11:52:48 -05001513 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001514 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001515
Tejun Heo2bd59d42014-02-11 11:52:49 -05001516 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001517 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001518 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001519
Tejun Heo2bd59d42014-02-11 11:52:49 -05001520destroy_root:
1521 kernfs_destroy_root(root->kf_root);
1522 root->kf_root = NULL;
1523exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001524 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001525out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001526 free_cgrp_cset_links(&tmp_links);
1527 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528}
1529
Al Virof7e83572010-07-26 13:23:11 +04001530static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001532 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001534 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001535 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001536 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001537 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001538 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001539
1540 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001541 * The first time anyone tries to mount a cgroup, enable the list
1542 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001543 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001544 if (!use_task_css_set_links)
1545 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001546
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001547 mutex_lock(&cgroup_tree_mutex);
1548 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549
Paul Menageddbcc7e2007-10-18 23:39:30 -07001550 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001553 goto out_unlock;
Li Zefane37a06f2014-04-17 13:53:08 +08001554retry:
Tejun Heo2bd59d42014-02-11 11:52:49 -05001555 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001556 if (!opts.subsys_mask && !opts.none && !opts.name) {
1557 cgrp_dfl_root_visible = true;
1558 root = &cgrp_dfl_root;
1559 cgroup_get(&root->cgrp);
1560 ret = 0;
1561 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562 }
1563
Tejun Heo985ed672014-03-19 10:23:53 -04001564 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001565 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001567 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001568 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001569
Paul Menage817929e2007-10-18 23:39:36 -07001570 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001571 * If we asked for a name then it must match. Also, if
1572 * name matches but sybsys_mask doesn't, we should fail.
1573 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001574 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001575 if (opts.name) {
1576 if (strcmp(opts.name, root->name))
1577 continue;
1578 name_match = true;
1579 }
Tejun Heo31261212013-06-28 17:07:30 -07001580
1581 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001582 * If we asked for subsystems (or explicitly for no
1583 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001584 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001585 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001586 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001587 if (!name_match)
1588 continue;
1589 ret = -EBUSY;
1590 goto out_unlock;
1591 }
Tejun Heo873fe092013-04-14 20:15:26 -07001592
Tejun Heoc7ba8282013-06-29 14:06:10 -07001593 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001594 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1595 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1596 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001597 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001598 } else {
1599 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1600 }
Tejun Heo873fe092013-04-14 20:15:26 -07001601 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001602
Tejun Heo776f02f2014-02-12 09:29:50 -05001603 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001604 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001605 * ref indicate that the root is being destroyed. Wait for
1606 * destruction to complete so that the subsystems are free.
1607 * We can use wait_queue for the wait but this path is
1608 * super cold. Let's just sleep for a bit and retry.
1609 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001610 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001611 mutex_unlock(&cgroup_mutex);
1612 mutex_unlock(&cgroup_tree_mutex);
1613 msleep(10);
Li Zefane37a06f2014-04-17 13:53:08 +08001614 mutex_lock(&cgroup_tree_mutex);
1615 mutex_lock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001616 goto retry;
1617 }
1618
1619 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001620 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621 }
1622
Tejun Heo172a2c062014-03-19 10:23:53 -04001623 /*
1624 * No such thing, create a new one. name= matching without subsys
1625 * specification is allowed for already existing hierarchies but we
1626 * can't create new one without subsys specification.
1627 */
1628 if (!opts.subsys_mask && !opts.none) {
1629 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001630 goto out_unlock;
1631 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632
Tejun Heo172a2c062014-03-19 10:23:53 -04001633 root = kzalloc(sizeof(*root), GFP_KERNEL);
1634 if (!root) {
1635 ret = -ENOMEM;
1636 goto out_unlock;
1637 }
1638
1639 init_cgroup_root(root, &opts);
1640
Tejun Heo35585572014-02-13 06:58:38 -05001641 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001642 if (ret)
1643 cgroup_free_root(root);
1644
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001645out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001646 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001647 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001648
Paul Menagec6d57f32009-09-23 15:56:19 -07001649 kfree(opts.release_agent);
1650 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001651
Tejun Heo2bd59d42014-02-11 11:52:49 -05001652 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001653 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001654
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001655 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1656 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001657 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001658 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659}
1660
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001661static void cgroup_kill_sb(struct super_block *sb)
1662{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001663 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001664 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001666 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001667 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001668}
1669
1670static struct file_system_type cgroup_fs_type = {
1671 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001672 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673 .kill_sb = cgroup_kill_sb,
1674};
1675
Greg KH676db4a2010-08-05 13:53:35 -07001676static struct kobject *cgroup_kobj;
1677
Li Zefana043e3b2008-02-23 15:24:09 -08001678/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001679 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001680 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001681 * @buf: the buffer to write the path into
1682 * @buflen: the length of the buffer
1683 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001684 * Determine @task's cgroup on the first (the one with the lowest non-zero
1685 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1686 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1687 * cgroup controller callbacks.
1688 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001689 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001690 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001691char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001692{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001693 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001694 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001695 int hierarchy_id = 1;
1696 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001697
1698 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001699 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001700
Tejun Heo913ffdb2013-07-11 16:34:48 -07001701 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1702
Tejun Heo857a2be2013-04-14 20:50:08 -07001703 if (root) {
1704 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001705 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001706 } else {
1707 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001708 if (strlcpy(buf, "/", buflen) < buflen)
1709 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001710 }
1711
Tejun Heo96d365e2014-02-13 06:58:40 -05001712 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001713 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001714 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001715}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001716EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001717
Tejun Heob3dc0942014-02-25 10:04:01 -05001718/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001719struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001720 /* the src and dst cset list running through cset->mg_node */
1721 struct list_head src_csets;
1722 struct list_head dst_csets;
1723
1724 /*
1725 * Fields for cgroup_taskset_*() iteration.
1726 *
1727 * Before migration is committed, the target migration tasks are on
1728 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1729 * the csets on ->dst_csets. ->csets point to either ->src_csets
1730 * or ->dst_csets depending on whether migration is committed.
1731 *
1732 * ->cur_csets and ->cur_task point to the current task position
1733 * during iteration.
1734 */
1735 struct list_head *csets;
1736 struct css_set *cur_cset;
1737 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001738};
1739
1740/**
1741 * cgroup_taskset_first - reset taskset and return the first task
1742 * @tset: taskset of interest
1743 *
1744 * @tset iteration is initialized and the first task is returned.
1745 */
1746struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1747{
Tejun Heob3dc0942014-02-25 10:04:01 -05001748 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1749 tset->cur_task = NULL;
1750
1751 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001752}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001753
1754/**
1755 * cgroup_taskset_next - iterate to the next task in taskset
1756 * @tset: taskset of interest
1757 *
1758 * Return the next task in @tset. Iteration must have been initialized
1759 * with cgroup_taskset_first().
1760 */
1761struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1762{
Tejun Heob3dc0942014-02-25 10:04:01 -05001763 struct css_set *cset = tset->cur_cset;
1764 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001765
Tejun Heob3dc0942014-02-25 10:04:01 -05001766 while (&cset->mg_node != tset->csets) {
1767 if (!task)
1768 task = list_first_entry(&cset->mg_tasks,
1769 struct task_struct, cg_list);
1770 else
1771 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001772
Tejun Heob3dc0942014-02-25 10:04:01 -05001773 if (&task->cg_list != &cset->mg_tasks) {
1774 tset->cur_cset = cset;
1775 tset->cur_task = task;
1776 return task;
1777 }
1778
1779 cset = list_next_entry(cset, mg_node);
1780 task = NULL;
1781 }
1782
1783 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001784}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001785
1786/**
Ben Blum74a11662011-05-26 16:25:20 -07001787 * cgroup_task_migrate - move a task from one cgroup to another.
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001788 * @old_cgrp; the cgroup @tsk is being migrated from
1789 * @tsk: the task being migrated
1790 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001791 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001792 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001793 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001794static void cgroup_task_migrate(struct cgroup *old_cgrp,
1795 struct task_struct *tsk,
1796 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001797{
Tejun Heo5abb8852013-06-12 21:04:49 -07001798 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001799
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001800 lockdep_assert_held(&cgroup_mutex);
1801 lockdep_assert_held(&css_set_rwsem);
1802
Ben Blum74a11662011-05-26 16:25:20 -07001803 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001804 * We are synchronized through threadgroup_lock() against PF_EXITING
1805 * setting such that we can't race against cgroup_exit() changing the
1806 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001807 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001808 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001809 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001810
Tejun Heob3dc0942014-02-25 10:04:01 -05001811 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001812 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001813
Tejun Heo1b9aba42014-03-19 17:43:21 -04001814 /*
1815 * Use move_tail so that cgroup_taskset_first() still returns the
1816 * leader after migration. This works because cgroup_migrate()
1817 * ensures that the dst_cset of the leader is the first on the
1818 * tset's dst_csets list.
1819 */
1820 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001821
1822 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001823 * We just gained a reference on old_cset by taking it from the
1824 * task. As trading it for new_cset is protected by cgroup_mutex,
1825 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001826 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001827 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001828 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001829}
1830
Li Zefana043e3b2008-02-23 15:24:09 -08001831/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001832 * cgroup_migrate_finish - cleanup after attach
1833 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001834 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001835 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1836 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001837 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001838static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001839{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001840 struct css_set *cset, *tmp_cset;
1841
1842 lockdep_assert_held(&cgroup_mutex);
1843
1844 down_write(&css_set_rwsem);
1845 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1846 cset->mg_src_cgrp = NULL;
1847 cset->mg_dst_cset = NULL;
1848 list_del_init(&cset->mg_preload_node);
1849 put_css_set_locked(cset, false);
1850 }
1851 up_write(&css_set_rwsem);
1852}
1853
1854/**
1855 * cgroup_migrate_add_src - add a migration source css_set
1856 * @src_cset: the source css_set to add
1857 * @dst_cgrp: the destination cgroup
1858 * @preloaded_csets: list of preloaded css_sets
1859 *
1860 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1861 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1862 * up by cgroup_migrate_finish().
1863 *
1864 * This function may be called without holding threadgroup_lock even if the
1865 * target is a process. Threads may be created and destroyed but as long
1866 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1867 * the preloaded css_sets are guaranteed to cover all migrations.
1868 */
1869static void cgroup_migrate_add_src(struct css_set *src_cset,
1870 struct cgroup *dst_cgrp,
1871 struct list_head *preloaded_csets)
1872{
1873 struct cgroup *src_cgrp;
1874
1875 lockdep_assert_held(&cgroup_mutex);
1876 lockdep_assert_held(&css_set_rwsem);
1877
1878 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1879
1880 /* nothing to do if this cset already belongs to the cgroup */
1881 if (src_cgrp == dst_cgrp)
1882 return;
1883
1884 if (!list_empty(&src_cset->mg_preload_node))
1885 return;
1886
1887 WARN_ON(src_cset->mg_src_cgrp);
1888 WARN_ON(!list_empty(&src_cset->mg_tasks));
1889 WARN_ON(!list_empty(&src_cset->mg_node));
1890
1891 src_cset->mg_src_cgrp = src_cgrp;
1892 get_css_set(src_cset);
1893 list_add(&src_cset->mg_preload_node, preloaded_csets);
1894}
1895
1896/**
1897 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
1898 * @dst_cgrp: the destination cgroup
1899 * @preloaded_csets: list of preloaded source css_sets
1900 *
1901 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
1902 * have been preloaded to @preloaded_csets. This function looks up and
1903 * pins all destination css_sets, links each to its source, and put them on
1904 * @preloaded_csets.
1905 *
1906 * This function must be called after cgroup_migrate_add_src() has been
1907 * called on each migration source css_set. After migration is performed
1908 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
1909 * @preloaded_csets.
1910 */
1911static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
1912 struct list_head *preloaded_csets)
1913{
1914 LIST_HEAD(csets);
1915 struct css_set *src_cset;
1916
1917 lockdep_assert_held(&cgroup_mutex);
1918
1919 /* look up the dst cset for each src cset and link it to src */
1920 list_for_each_entry(src_cset, preloaded_csets, mg_preload_node) {
1921 struct css_set *dst_cset;
1922
1923 dst_cset = find_css_set(src_cset, dst_cgrp);
1924 if (!dst_cset)
1925 goto err;
1926
1927 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
1928 src_cset->mg_dst_cset = dst_cset;
1929
1930 if (list_empty(&dst_cset->mg_preload_node))
1931 list_add(&dst_cset->mg_preload_node, &csets);
1932 else
1933 put_css_set(dst_cset, false);
1934 }
1935
1936 list_splice(&csets, preloaded_csets);
1937 return 0;
1938err:
1939 cgroup_migrate_finish(&csets);
1940 return -ENOMEM;
1941}
1942
1943/**
1944 * cgroup_migrate - migrate a process or task to a cgroup
1945 * @cgrp: the destination cgroup
1946 * @leader: the leader of the process or the task to migrate
1947 * @threadgroup: whether @leader points to the whole process or a single task
1948 *
1949 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
1950 * process, the caller must be holding threadgroup_lock of @leader. The
1951 * caller is also responsible for invoking cgroup_migrate_add_src() and
1952 * cgroup_migrate_prepare_dst() on the targets before invoking this
1953 * function and following up with cgroup_migrate_finish().
1954 *
1955 * As long as a controller's ->can_attach() doesn't fail, this function is
1956 * guaranteed to succeed. This means that, excluding ->can_attach()
1957 * failure, when migrating multiple targets, the success or failure can be
1958 * decided for all targets by invoking group_migrate_prepare_dst() before
1959 * actually starting migrating.
1960 */
1961static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
1962 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001963{
Tejun Heob3dc0942014-02-25 10:04:01 -05001964 struct cgroup_taskset tset = {
1965 .src_csets = LIST_HEAD_INIT(tset.src_csets),
1966 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
1967 .csets = &tset.src_csets,
1968 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05001969 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05001970 struct css_set *cset, *tmp_cset;
1971 struct task_struct *task, *tmp_task;
1972 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07001973
1974 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001975 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1976 * already PF_EXITING could be freed from underneath us unless we
1977 * take an rcu_read_lock.
1978 */
Tejun Heob3dc0942014-02-25 10:04:01 -05001979 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001980 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05001981 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07001982 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05001983 /* @task either already exited or can't exit until the end */
1984 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001985 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001986
Tejun Heoeaf797a2014-02-25 10:04:03 -05001987 /* leave @task alone if post_fork() hasn't linked it yet */
1988 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08001989 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05001990
Tejun Heob3dc0942014-02-25 10:04:01 -05001991 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001992 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001993 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05001994
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001995 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04001996 * cgroup_taskset_first() must always return the leader.
1997 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001998 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04001999 list_move_tail(&task->cg_list, &cset->mg_tasks);
2000 if (list_empty(&cset->mg_node))
2001 list_add_tail(&cset->mg_node, &tset.src_csets);
2002 if (list_empty(&cset->mg_dst_cset->mg_node))
2003 list_move_tail(&cset->mg_dst_cset->mg_node,
2004 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002005 next:
Li Zefan081aa452013-03-13 09:17:09 +08002006 if (!threadgroup)
2007 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002008 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002009 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002010 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002011
Tejun Heo134d3372011-12-12 18:12:21 -08002012 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002013 if (list_empty(&tset.src_csets))
2014 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002015
Tejun Heo1958d2d2014-02-25 10:04:03 -05002016 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002017 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002018 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002019 ret = css->ss->can_attach(css, &tset);
2020 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002021 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002022 goto out_cancel_attach;
2023 }
2024 }
Ben Blum74a11662011-05-26 16:25:20 -07002025 }
2026
2027 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002028 * Now that we're guaranteed success, proceed to move all tasks to
2029 * the new cgroup. There are no failure cases after here, so this
2030 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002031 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002032 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002033 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2034 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2035 cgroup_task_migrate(cset->mg_src_cgrp, task,
2036 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002037 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002038 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002039
2040 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002041 * Migration is committed, all target tasks are now on dst_csets.
2042 * Nothing is sensitive to fork() after this point. Notify
2043 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002044 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002045 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002046
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002047 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002048 if (css->ss->attach)
2049 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002050
Tejun Heo9db8de32014-02-13 06:58:43 -05002051 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002052 goto out_release_tset;
2053
Ben Blum74a11662011-05-26 16:25:20 -07002054out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002055 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002056 if (css == failed_css)
2057 break;
2058 if (css->ss->cancel_attach)
2059 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002060 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002061out_release_tset:
2062 down_write(&css_set_rwsem);
2063 list_splice_init(&tset.dst_csets, &tset.src_csets);
2064 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002065 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002066 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002067 }
2068 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002069 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002070}
2071
Tejun Heo1958d2d2014-02-25 10:04:03 -05002072/**
2073 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2074 * @dst_cgrp: the cgroup to attach to
2075 * @leader: the task or the leader of the threadgroup to be attached
2076 * @threadgroup: attach the whole threadgroup?
2077 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002078 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002079 */
2080static int cgroup_attach_task(struct cgroup *dst_cgrp,
2081 struct task_struct *leader, bool threadgroup)
2082{
2083 LIST_HEAD(preloaded_csets);
2084 struct task_struct *task;
2085 int ret;
2086
2087 /* look up all src csets */
2088 down_read(&css_set_rwsem);
2089 rcu_read_lock();
2090 task = leader;
2091 do {
2092 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2093 &preloaded_csets);
2094 if (!threadgroup)
2095 break;
2096 } while_each_thread(leader, task);
2097 rcu_read_unlock();
2098 up_read(&css_set_rwsem);
2099
2100 /* prepare dst csets and commit */
2101 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2102 if (!ret)
2103 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2104
2105 cgroup_migrate_finish(&preloaded_csets);
2106 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002107}
2108
2109/*
2110 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002111 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002112 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002113 */
2114static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002115{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002116 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002117 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002118 int ret;
2119
Ben Blum74a11662011-05-26 16:25:20 -07002120 if (!cgroup_lock_live_group(cgrp))
2121 return -ENODEV;
2122
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002123retry_find_task:
2124 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002125 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002126 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002127 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002128 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002129 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002130 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002131 }
Ben Blum74a11662011-05-26 16:25:20 -07002132 /*
2133 * even if we're attaching all tasks in the thread group, we
2134 * only need to check permissions on one of them.
2135 */
David Howellsc69e8d92008-11-14 10:39:19 +11002136 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002137 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2138 !uid_eq(cred->euid, tcred->uid) &&
2139 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002140 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002141 ret = -EACCES;
2142 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002143 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002144 } else
2145 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002146
2147 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002148 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002149
2150 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002151 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002152 * trapped in a cpuset, or RT worker may be born in a cgroup
2153 * with no rt_runtime allocated. Just say no.
2154 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002155 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002156 ret = -EINVAL;
2157 rcu_read_unlock();
2158 goto out_unlock_cgroup;
2159 }
2160
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002161 get_task_struct(tsk);
2162 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002163
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002164 threadgroup_lock(tsk);
2165 if (threadgroup) {
2166 if (!thread_group_leader(tsk)) {
2167 /*
2168 * a race with de_thread from another thread's exec()
2169 * may strip us of our leadership, if this happens,
2170 * there is no choice but to throw this task away and
2171 * try again; this is
2172 * "double-double-toil-and-trouble-check locking".
2173 */
2174 threadgroup_unlock(tsk);
2175 put_task_struct(tsk);
2176 goto retry_find_task;
2177 }
Li Zefan081aa452013-03-13 09:17:09 +08002178 }
2179
2180 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2181
Tejun Heocd3d0952011-12-12 18:12:21 -08002182 threadgroup_unlock(tsk);
2183
Paul Menagebbcb81d2007-10-18 23:39:32 -07002184 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002185out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002186 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002187 return ret;
2188}
2189
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002190/**
2191 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2192 * @from: attach to all cgroups of a given task
2193 * @tsk: the task to be attached
2194 */
2195int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2196{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002197 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002198 int retval = 0;
2199
Tejun Heo47cfcd02013-04-07 09:29:51 -07002200 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002201 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002202 struct cgroup *from_cgrp;
2203
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002204 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002205 continue;
2206
Tejun Heo96d365e2014-02-13 06:58:40 -05002207 down_read(&css_set_rwsem);
2208 from_cgrp = task_cgroup_from_root(from, root);
2209 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002210
Li Zefan6f4b7e62013-07-31 16:18:36 +08002211 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002212 if (retval)
2213 break;
2214 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002215 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002216
2217 return retval;
2218}
2219EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2220
Tejun Heo182446d2013-08-08 20:11:24 -04002221static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2222 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002223{
Tejun Heo182446d2013-08-08 20:11:24 -04002224 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002225}
2226
Tejun Heo182446d2013-08-08 20:11:24 -04002227static int cgroup_procs_write(struct cgroup_subsys_state *css,
2228 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002229{
Tejun Heo182446d2013-08-08 20:11:24 -04002230 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002231}
2232
Tejun Heo182446d2013-08-08 20:11:24 -04002233static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
Tejun Heo4d3bb512014-03-19 10:23:54 -04002234 struct cftype *cft, char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002235{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002236 struct cgroup_root *root = css->cgroup->root;
Tejun Heo5f469902014-02-11 11:52:48 -05002237
2238 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002239 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002240 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002241 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002242 strlcpy(root->release_agent_path, buffer,
2243 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002244 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002245 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002246 return 0;
2247}
2248
Tejun Heo2da8ca82013-12-05 12:28:04 -05002249static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002250{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002251 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002252
Paul Menagee788e062008-07-25 01:46:59 -07002253 if (!cgroup_lock_live_group(cgrp))
2254 return -ENODEV;
2255 seq_puts(seq, cgrp->root->release_agent_path);
2256 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002257 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002258 return 0;
2259}
2260
Tejun Heo2da8ca82013-12-05 12:28:04 -05002261static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002262{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002263 struct cgroup *cgrp = seq_css(seq)->cgroup;
2264
2265 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002266 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002267}
2268
Tejun Heo2bd59d42014-02-11 11:52:49 -05002269static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2270 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002271{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002272 struct cgroup *cgrp = of->kn->parent->priv;
2273 struct cftype *cft = of->kn->priv;
2274 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002275 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002276
Tejun Heo2bd59d42014-02-11 11:52:49 -05002277 /*
2278 * kernfs guarantees that a file isn't deleted with operations in
2279 * flight, which means that the matching css is and stays alive and
2280 * doesn't need to be pinned. The RCU locking is not necessary
2281 * either. It's just for the convenience of using cgroup_css().
2282 */
2283 rcu_read_lock();
2284 css = cgroup_css(cgrp, cft->ss);
2285 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002286
Tejun Heoa742c592013-12-05 12:28:03 -05002287 if (cft->write_string) {
2288 ret = cft->write_string(css, cft, strstrip(buf));
2289 } else if (cft->write_u64) {
2290 unsigned long long v;
2291 ret = kstrtoull(buf, 0, &v);
2292 if (!ret)
2293 ret = cft->write_u64(css, cft, v);
2294 } else if (cft->write_s64) {
2295 long long v;
2296 ret = kstrtoll(buf, 0, &v);
2297 if (!ret)
2298 ret = cft->write_s64(css, cft, v);
2299 } else if (cft->trigger) {
2300 ret = cft->trigger(css, (unsigned int)cft->private);
2301 } else {
2302 ret = -EINVAL;
2303 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002304
Tejun Heoa742c592013-12-05 12:28:03 -05002305 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002306}
2307
Tejun Heo6612f052013-12-05 12:28:04 -05002308static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002309{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002310 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002311}
2312
2313static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2314{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002315 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002316}
2317
2318static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2319{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002320 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002321}
2322
2323static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2324{
Tejun Heo7da11272013-12-05 12:28:04 -05002325 struct cftype *cft = seq_cft(m);
2326 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002327
Tejun Heo2da8ca82013-12-05 12:28:04 -05002328 if (cft->seq_show)
2329 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002330
Tejun Heo896f5192013-12-05 12:28:04 -05002331 if (cft->read_u64)
2332 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2333 else if (cft->read_s64)
2334 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2335 else
2336 return -EINVAL;
2337 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002338}
2339
Tejun Heo2bd59d42014-02-11 11:52:49 -05002340static struct kernfs_ops cgroup_kf_single_ops = {
2341 .atomic_write_len = PAGE_SIZE,
2342 .write = cgroup_file_write,
2343 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002344};
2345
Tejun Heo2bd59d42014-02-11 11:52:49 -05002346static struct kernfs_ops cgroup_kf_ops = {
2347 .atomic_write_len = PAGE_SIZE,
2348 .write = cgroup_file_write,
2349 .seq_start = cgroup_seqfile_start,
2350 .seq_next = cgroup_seqfile_next,
2351 .seq_stop = cgroup_seqfile_stop,
2352 .seq_show = cgroup_seqfile_show,
2353};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002354
2355/*
2356 * cgroup_rename - Only allow simple rename of directories in place.
2357 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002358static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2359 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002360{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002361 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002362 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002363
Tejun Heo2bd59d42014-02-11 11:52:49 -05002364 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002365 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002366 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002367 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002368
Tejun Heo6db8e852013-06-14 11:18:22 -07002369 /*
2370 * This isn't a proper migration and its usefulness is very
2371 * limited. Disallow if sane_behavior.
2372 */
2373 if (cgroup_sane_behavior(cgrp))
2374 return -EPERM;
2375
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002376 /*
2377 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2378 * active_ref. kernfs_rename() doesn't require active_ref
2379 * protection. Break them before grabbing cgroup_tree_mutex.
2380 */
2381 kernfs_break_active_protection(new_parent);
2382 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002383
Tejun Heo2bd59d42014-02-11 11:52:49 -05002384 mutex_lock(&cgroup_tree_mutex);
2385 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002386
Tejun Heo2bd59d42014-02-11 11:52:49 -05002387 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002388
Tejun Heo2bd59d42014-02-11 11:52:49 -05002389 mutex_unlock(&cgroup_mutex);
2390 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002391
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002392 kernfs_unbreak_active_protection(kn);
2393 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002394 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002395}
2396
Tejun Heo49957f82014-04-07 16:44:47 -04002397/* set uid and gid of cgroup dirs and files to that of the creator */
2398static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2399{
2400 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2401 .ia_uid = current_fsuid(),
2402 .ia_gid = current_fsgid(), };
2403
2404 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2405 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2406 return 0;
2407
2408 return kernfs_setattr(kn, &iattr);
2409}
2410
Tejun Heo2bb566c2013-08-08 20:11:23 -04002411static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002412{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002413 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002414 struct kernfs_node *kn;
2415 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002416 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002417
Tejun Heo2bd59d42014-02-11 11:52:49 -05002418#ifdef CONFIG_DEBUG_LOCK_ALLOC
2419 key = &cft->lockdep_key;
2420#endif
2421 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2422 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2423 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002424 if (IS_ERR(kn))
2425 return PTR_ERR(kn);
2426
2427 ret = cgroup_kn_set_ugid(kn);
2428 if (ret)
2429 kernfs_remove(kn);
2430 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002431}
2432
Tejun Heob1f28d32013-06-28 16:24:10 -07002433/**
2434 * cgroup_addrm_files - add or remove files to a cgroup directory
2435 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002436 * @cfts: array of cftypes to be added
2437 * @is_add: whether to add or remove
2438 *
2439 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002440 * For removals, this function never fails. If addition fails, this
2441 * function doesn't remove files already added. The caller is responsible
2442 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002443 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002444static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2445 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002446{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002447 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002448 int ret;
2449
Tejun Heoace2bee2014-02-11 11:52:47 -05002450 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002451
2452 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002453 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002454 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2455 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002456 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2457 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002458 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2459 continue;
2460 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2461 continue;
2462
Li Zefan2739d3c2013-01-21 18:18:33 +08002463 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002464 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002465 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002466 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002467 cft->name, ret);
2468 return ret;
2469 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002470 } else {
2471 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002472 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002473 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002474 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002475}
2476
Tejun Heo21a2d342014-02-12 09:29:49 -05002477static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002478{
2479 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002480 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002481 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002482 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002483 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002484
Tejun Heo21a2d342014-02-12 09:29:49 -05002485 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002486
Li Zefane8c82d22013-06-18 18:48:37 +08002487 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002488 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002489 struct cgroup *cgrp = css->cgroup;
2490
Li Zefane8c82d22013-06-18 18:48:37 +08002491 if (cgroup_is_dead(cgrp))
2492 continue;
2493
Tejun Heo21a2d342014-02-12 09:29:49 -05002494 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002495 if (ret)
2496 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002497 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002498
2499 if (is_add && !ret)
2500 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002501 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002502}
2503
Tejun Heo2da440a2014-02-11 11:52:48 -05002504static void cgroup_exit_cftypes(struct cftype *cfts)
2505{
2506 struct cftype *cft;
2507
Tejun Heo2bd59d42014-02-11 11:52:49 -05002508 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2509 /* free copy for custom atomic_write_len, see init_cftypes() */
2510 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2511 kfree(cft->kf_ops);
2512 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002513 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002514 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002515}
2516
Tejun Heo2bd59d42014-02-11 11:52:49 -05002517static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002518{
2519 struct cftype *cft;
2520
Tejun Heo2bd59d42014-02-11 11:52:49 -05002521 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2522 struct kernfs_ops *kf_ops;
2523
Tejun Heo0adb0702014-02-12 09:29:48 -05002524 WARN_ON(cft->ss || cft->kf_ops);
2525
Tejun Heo2bd59d42014-02-11 11:52:49 -05002526 if (cft->seq_start)
2527 kf_ops = &cgroup_kf_ops;
2528 else
2529 kf_ops = &cgroup_kf_single_ops;
2530
2531 /*
2532 * Ugh... if @cft wants a custom max_write_len, we need to
2533 * make a copy of kf_ops to set its atomic_write_len.
2534 */
2535 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2536 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2537 if (!kf_ops) {
2538 cgroup_exit_cftypes(cfts);
2539 return -ENOMEM;
2540 }
2541 kf_ops->atomic_write_len = cft->max_write_len;
2542 }
2543
2544 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002545 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002546 }
2547
2548 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002549}
2550
Tejun Heo21a2d342014-02-12 09:29:49 -05002551static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2552{
2553 lockdep_assert_held(&cgroup_tree_mutex);
2554
2555 if (!cfts || !cfts[0].ss)
2556 return -ENOENT;
2557
2558 list_del(&cfts->node);
2559 cgroup_apply_cftypes(cfts, false);
2560 cgroup_exit_cftypes(cfts);
2561 return 0;
2562}
2563
Tejun Heo8e3f6542012-04-01 12:09:55 -07002564/**
Tejun Heo80b13582014-02-12 09:29:48 -05002565 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2566 * @cfts: zero-length name terminated array of cftypes
2567 *
2568 * Unregister @cfts. Files described by @cfts are removed from all
2569 * existing cgroups and all future cgroups won't have them either. This
2570 * function can be called anytime whether @cfts' subsys is attached or not.
2571 *
2572 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2573 * registered.
2574 */
2575int cgroup_rm_cftypes(struct cftype *cfts)
2576{
Tejun Heo21a2d342014-02-12 09:29:49 -05002577 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002578
Tejun Heo21a2d342014-02-12 09:29:49 -05002579 mutex_lock(&cgroup_tree_mutex);
2580 ret = cgroup_rm_cftypes_locked(cfts);
2581 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002582 return ret;
2583}
2584
2585/**
2586 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2587 * @ss: target cgroup subsystem
2588 * @cfts: zero-length name terminated array of cftypes
2589 *
2590 * Register @cfts to @ss. Files described by @cfts are created for all
2591 * existing cgroups to which @ss is attached and all future cgroups will
2592 * have them too. This function can be called anytime whether @ss is
2593 * attached or not.
2594 *
2595 * Returns 0 on successful registration, -errno on failure. Note that this
2596 * function currently returns 0 as long as @cfts registration is successful
2597 * even if some file creation attempts on existing cgroups fail.
2598 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002599int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002600{
Tejun Heo9ccece82013-06-28 16:24:11 -07002601 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002602
Li Zefandc5736e2014-02-17 10:41:50 +08002603 if (!cfts || cfts[0].name[0] == '\0')
2604 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002605
Tejun Heo2bd59d42014-02-11 11:52:49 -05002606 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002607 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05002608 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002609
Tejun Heo21a2d342014-02-12 09:29:49 -05002610 mutex_lock(&cgroup_tree_mutex);
2611
Tejun Heo0adb0702014-02-12 09:29:48 -05002612 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05002613 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002614 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05002615 cgroup_rm_cftypes_locked(cfts);
2616
2617 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002618 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002619}
Tejun Heo79578622012-04-01 12:09:56 -07002620
2621/**
Li Zefana043e3b2008-02-23 15:24:09 -08002622 * cgroup_task_count - count the number of tasks in a cgroup.
2623 * @cgrp: the cgroup in question
2624 *
2625 * Return the number of tasks in the cgroup.
2626 */
Tejun Heo07bc3562014-02-13 06:58:39 -05002627static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002628{
2629 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002630 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002631
Tejun Heo96d365e2014-02-13 06:58:40 -05002632 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07002633 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2634 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05002635 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002636 return count;
2637}
2638
Tejun Heo574bd9f2012-11-09 09:12:29 -08002639/**
Tejun Heo492eb212013-08-08 20:11:25 -04002640 * css_next_child - find the next child of a given css
2641 * @pos_css: the current position (%NULL to initiate traversal)
2642 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002643 *
Tejun Heo492eb212013-08-08 20:11:25 -04002644 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002645 * under either cgroup_mutex or RCU read lock. The only requirement is
2646 * that @parent_css and @pos_css are accessible. The next sibling is
2647 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002648 */
Tejun Heo492eb212013-08-08 20:11:25 -04002649struct cgroup_subsys_state *
2650css_next_child(struct cgroup_subsys_state *pos_css,
2651 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002652{
Tejun Heo492eb212013-08-08 20:11:25 -04002653 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2654 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002655 struct cgroup *next;
2656
Tejun Heoace2bee2014-02-11 11:52:47 -05002657 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002658
2659 /*
2660 * @pos could already have been removed. Once a cgroup is removed,
2661 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002662 * changes. As CGRP_DEAD assertion is serialized and happens
2663 * before the cgroup is taken off the ->sibling list, if we see it
2664 * unasserted, it's guaranteed that the next sibling hasn't
2665 * finished its grace period even if it's already removed, and thus
2666 * safe to dereference from this RCU critical section. If
2667 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2668 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002669 *
2670 * If @pos is dead, its next pointer can't be dereferenced;
2671 * however, as each cgroup is given a monotonically increasing
2672 * unique serial number and always appended to the sibling list,
2673 * the next one can be found by walking the parent's children until
2674 * we see a cgroup with higher serial number than @pos's. While
2675 * this path can be slower, it's taken only when either the current
2676 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002677 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002678 if (!pos) {
2679 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2680 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002681 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002682 } else {
2683 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2684 if (next->serial_nr > pos->serial_nr)
2685 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002686 }
2687
Tejun Heo492eb212013-08-08 20:11:25 -04002688 if (&next->sibling == &cgrp->children)
2689 return NULL;
2690
Tejun Heoca8bdca2013-08-26 18:40:56 -04002691 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002692}
Tejun Heo53fa5262013-05-24 10:55:38 +09002693
2694/**
Tejun Heo492eb212013-08-08 20:11:25 -04002695 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002696 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002697 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002698 *
Tejun Heo492eb212013-08-08 20:11:25 -04002699 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002700 * to visit for pre-order traversal of @root's descendants. @root is
2701 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002702 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002703 * While this function requires cgroup_mutex or RCU read locking, it
2704 * doesn't require the whole traversal to be contained in a single critical
2705 * section. This function will return the correct next descendant as long
2706 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002707 */
Tejun Heo492eb212013-08-08 20:11:25 -04002708struct cgroup_subsys_state *
2709css_next_descendant_pre(struct cgroup_subsys_state *pos,
2710 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002711{
Tejun Heo492eb212013-08-08 20:11:25 -04002712 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002713
Tejun Heoace2bee2014-02-11 11:52:47 -05002714 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002715
Tejun Heobd8815a2013-08-08 20:11:27 -04002716 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002717 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002718 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002719
2720 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002721 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002722 if (next)
2723 return next;
2724
2725 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002726 while (pos != root) {
2727 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002728 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002729 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002730 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002731 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002732
2733 return NULL;
2734}
Tejun Heo574bd9f2012-11-09 09:12:29 -08002735
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002736/**
Tejun Heo492eb212013-08-08 20:11:25 -04002737 * css_rightmost_descendant - return the rightmost descendant of a css
2738 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002739 *
Tejun Heo492eb212013-08-08 20:11:25 -04002740 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2741 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002742 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002743 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002744 * While this function requires cgroup_mutex or RCU read locking, it
2745 * doesn't require the whole traversal to be contained in a single critical
2746 * section. This function will return the correct rightmost descendant as
2747 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002748 */
Tejun Heo492eb212013-08-08 20:11:25 -04002749struct cgroup_subsys_state *
2750css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002751{
Tejun Heo492eb212013-08-08 20:11:25 -04002752 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002753
Tejun Heoace2bee2014-02-11 11:52:47 -05002754 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002755
2756 do {
2757 last = pos;
2758 /* ->prev isn't RCU safe, walk ->next till the end */
2759 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002760 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002761 pos = tmp;
2762 } while (pos);
2763
2764 return last;
2765}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002766
Tejun Heo492eb212013-08-08 20:11:25 -04002767static struct cgroup_subsys_state *
2768css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002769{
Tejun Heo492eb212013-08-08 20:11:25 -04002770 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002771
2772 do {
2773 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04002774 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002775 } while (pos);
2776
2777 return last;
2778}
2779
2780/**
Tejun Heo492eb212013-08-08 20:11:25 -04002781 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002782 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002783 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002784 *
Tejun Heo492eb212013-08-08 20:11:25 -04002785 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002786 * to visit for post-order traversal of @root's descendants. @root is
2787 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002788 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002789 * While this function requires cgroup_mutex or RCU read locking, it
2790 * doesn't require the whole traversal to be contained in a single critical
2791 * section. This function will return the correct next descendant as long
2792 * as both @pos and @cgroup are accessible and @pos is a descendant of
2793 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002794 */
Tejun Heo492eb212013-08-08 20:11:25 -04002795struct cgroup_subsys_state *
2796css_next_descendant_post(struct cgroup_subsys_state *pos,
2797 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002798{
Tejun Heo492eb212013-08-08 20:11:25 -04002799 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002800
Tejun Heoace2bee2014-02-11 11:52:47 -05002801 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002802
Tejun Heo58b79a92013-09-06 15:31:08 -04002803 /* if first iteration, visit leftmost descendant which may be @root */
2804 if (!pos)
2805 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002806
Tejun Heobd8815a2013-08-08 20:11:27 -04002807 /* if we visited @root, we're done */
2808 if (pos == root)
2809 return NULL;
2810
Tejun Heo574bd9f2012-11-09 09:12:29 -08002811 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04002812 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002813 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04002814 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002815
2816 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04002817 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002818}
Tejun Heo574bd9f2012-11-09 09:12:29 -08002819
Tejun Heo0942eee2013-08-08 20:11:26 -04002820/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002821 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04002822 * @it: the iterator to advance
2823 *
2824 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04002825 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002826static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04002827{
2828 struct list_head *l = it->cset_link;
2829 struct cgrp_cset_link *link;
2830 struct css_set *cset;
2831
2832 /* Advance to the next non-empty css_set */
2833 do {
2834 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04002835 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04002836 it->cset_link = NULL;
2837 return;
2838 }
2839 link = list_entry(l, struct cgrp_cset_link, cset_link);
2840 cset = link->cset;
Tejun Heoc7561122014-02-25 10:04:01 -05002841 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
2842
Tejun Heod5158762013-08-08 20:11:26 -04002843 it->cset_link = l;
Tejun Heoc7561122014-02-25 10:04:01 -05002844
2845 if (!list_empty(&cset->tasks))
2846 it->task = cset->tasks.next;
2847 else
2848 it->task = cset->mg_tasks.next;
Tejun Heod5158762013-08-08 20:11:26 -04002849}
2850
Tejun Heo0942eee2013-08-08 20:11:26 -04002851/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002852 * css_task_iter_start - initiate task iteration
2853 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04002854 * @it: the task iterator to use
2855 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002856 * Initiate iteration through the tasks of @css. The caller can call
2857 * css_task_iter_next() to walk through the tasks until the function
2858 * returns NULL. On completion of iteration, css_task_iter_end() must be
2859 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04002860 *
2861 * Note that this function acquires a lock which is released when the
2862 * iteration finishes. The caller can't sleep while iteration is in
2863 * progress.
2864 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002865void css_task_iter_start(struct cgroup_subsys_state *css,
2866 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05002867 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07002868{
Tejun Heo56fde9e2014-02-13 06:58:38 -05002869 /* no one should try to iterate before mounting cgroups */
2870 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002871
Tejun Heo96d365e2014-02-13 06:58:40 -05002872 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002873
Tejun Heo72ec7022013-08-08 20:11:26 -04002874 it->origin_css = css;
2875 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002876
Tejun Heo72ec7022013-08-08 20:11:26 -04002877 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002878}
2879
Tejun Heo0942eee2013-08-08 20:11:26 -04002880/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002881 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04002882 * @it: the task iterator being iterated
2883 *
2884 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04002885 * initialized via css_task_iter_start(). Returns NULL when the iteration
2886 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04002887 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002888struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002889{
2890 struct task_struct *res;
2891 struct list_head *l = it->task;
Tejun Heoc7561122014-02-25 10:04:01 -05002892 struct cgrp_cset_link *link = list_entry(it->cset_link,
2893 struct cgrp_cset_link, cset_link);
Paul Menage817929e2007-10-18 23:39:36 -07002894
2895 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07002896 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07002897 return NULL;
2898 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05002899
2900 /*
2901 * Advance iterator to find next entry. cset->tasks is consumed
2902 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
2903 * next cset.
2904 */
Paul Menage817929e2007-10-18 23:39:36 -07002905 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05002906
2907 if (l == &link->cset->tasks)
2908 l = link->cset->mg_tasks.next;
2909
2910 if (l == &link->cset->mg_tasks)
Tejun Heo72ec7022013-08-08 20:11:26 -04002911 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05002912 else
Paul Menage817929e2007-10-18 23:39:36 -07002913 it->task = l;
Tejun Heoc7561122014-02-25 10:04:01 -05002914
Paul Menage817929e2007-10-18 23:39:36 -07002915 return res;
2916}
2917
Tejun Heo0942eee2013-08-08 20:11:26 -04002918/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002919 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04002920 * @it: the task iterator to finish
2921 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002922 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04002923 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002924void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05002925 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07002926{
Tejun Heo96d365e2014-02-13 06:58:40 -05002927 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07002928}
2929
2930/**
2931 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
2932 * @to: cgroup to which the tasks will be moved
2933 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05002934 *
2935 * Locking rules between cgroup_post_fork() and the migration path
2936 * guarantee that, if a task is forking while being migrated, the new child
2937 * is guaranteed to be either visible in the source cgroup after the
2938 * parent's migration is complete or put into the target cgroup. No task
2939 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07002940 */
2941int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
2942{
Tejun Heo952aaa12014-02-25 10:04:03 -05002943 LIST_HEAD(preloaded_csets);
2944 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05002945 struct css_task_iter it;
2946 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05002947 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05002948
Tejun Heo952aaa12014-02-25 10:04:03 -05002949 mutex_lock(&cgroup_mutex);
2950
2951 /* all tasks in @from are being moved, all csets are source */
2952 down_read(&css_set_rwsem);
2953 list_for_each_entry(link, &from->cset_links, cset_link)
2954 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
2955 up_read(&css_set_rwsem);
2956
2957 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
2958 if (ret)
2959 goto out_err;
2960
2961 /*
2962 * Migrate tasks one-by-one until @form is empty. This fails iff
2963 * ->can_attach() fails.
2964 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05002965 do {
2966 css_task_iter_start(&from->dummy_css, &it);
2967 task = css_task_iter_next(&it);
2968 if (task)
2969 get_task_struct(task);
2970 css_task_iter_end(&it);
2971
2972 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05002973 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05002974 put_task_struct(task);
2975 }
2976 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05002977out_err:
2978 cgroup_migrate_finish(&preloaded_csets);
2979 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05002980 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07002981}
2982
Paul Menage817929e2007-10-18 23:39:36 -07002983/*
Ben Blum102a7752009-09-23 15:56:26 -07002984 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002985 *
2986 * Reading this file can return large amounts of data if a cgroup has
2987 * *lots* of attached tasks. So it may need several calls to read(),
2988 * but we cannot guarantee that the information we produce is correct
2989 * unless we produce it entirely atomically.
2990 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002991 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002992
Li Zefan24528252012-01-20 11:58:43 +08002993/* which pidlist file are we talking about? */
2994enum cgroup_filetype {
2995 CGROUP_FILE_PROCS,
2996 CGROUP_FILE_TASKS,
2997};
2998
2999/*
3000 * A pidlist is a list of pids that virtually represents the contents of one
3001 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3002 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3003 * to the cgroup.
3004 */
3005struct cgroup_pidlist {
3006 /*
3007 * used to find which pidlist is wanted. doesn't change as long as
3008 * this particular list stays in the list.
3009 */
3010 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3011 /* array of xids */
3012 pid_t *list;
3013 /* how many elements the above list has */
3014 int length;
Li Zefan24528252012-01-20 11:58:43 +08003015 /* each of these stored in a list by its cgroup */
3016 struct list_head links;
3017 /* pointer to the cgroup we belong to, for list removal purposes */
3018 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003019 /* for delayed destruction */
3020 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003021};
3022
Paul Menagebbcb81d2007-10-18 23:39:32 -07003023/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003024 * The following two functions "fix" the issue where there are more pids
3025 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3026 * TODO: replace with a kernel-wide solution to this problem
3027 */
3028#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3029static void *pidlist_allocate(int count)
3030{
3031 if (PIDLIST_TOO_LARGE(count))
3032 return vmalloc(count * sizeof(pid_t));
3033 else
3034 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3035}
Tejun Heob1a21362013-11-29 10:42:58 -05003036
Ben Blumd1d9fd32009-09-23 15:56:28 -07003037static void pidlist_free(void *p)
3038{
3039 if (is_vmalloc_addr(p))
3040 vfree(p);
3041 else
3042 kfree(p);
3043}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003044
3045/*
Tejun Heob1a21362013-11-29 10:42:58 -05003046 * Used to destroy all pidlists lingering waiting for destroy timer. None
3047 * should be left afterwards.
3048 */
3049static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3050{
3051 struct cgroup_pidlist *l, *tmp_l;
3052
3053 mutex_lock(&cgrp->pidlist_mutex);
3054 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3055 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3056 mutex_unlock(&cgrp->pidlist_mutex);
3057
3058 flush_workqueue(cgroup_pidlist_destroy_wq);
3059 BUG_ON(!list_empty(&cgrp->pidlists));
3060}
3061
3062static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3063{
3064 struct delayed_work *dwork = to_delayed_work(work);
3065 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3066 destroy_dwork);
3067 struct cgroup_pidlist *tofree = NULL;
3068
3069 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003070
3071 /*
Tejun Heo04502362013-11-29 10:42:59 -05003072 * Destroy iff we didn't get queued again. The state won't change
3073 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003074 */
Tejun Heo04502362013-11-29 10:42:59 -05003075 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003076 list_del(&l->links);
3077 pidlist_free(l->list);
3078 put_pid_ns(l->key.ns);
3079 tofree = l;
3080 }
3081
Tejun Heob1a21362013-11-29 10:42:58 -05003082 mutex_unlock(&l->owner->pidlist_mutex);
3083 kfree(tofree);
3084}
3085
3086/*
Ben Blum102a7752009-09-23 15:56:26 -07003087 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003088 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003089 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003090static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003091{
Ben Blum102a7752009-09-23 15:56:26 -07003092 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003093
3094 /*
3095 * we presume the 0th element is unique, so i starts at 1. trivial
3096 * edge cases first; no work needs to be done for either
3097 */
3098 if (length == 0 || length == 1)
3099 return length;
3100 /* src and dest walk down the list; dest counts unique elements */
3101 for (src = 1; src < length; src++) {
3102 /* find next unique element */
3103 while (list[src] == list[src-1]) {
3104 src++;
3105 if (src == length)
3106 goto after;
3107 }
3108 /* dest always points to where the next unique element goes */
3109 list[dest] = list[src];
3110 dest++;
3111 }
3112after:
Ben Blum102a7752009-09-23 15:56:26 -07003113 return dest;
3114}
3115
Tejun Heoafb2bc12013-11-29 10:42:59 -05003116/*
3117 * The two pid files - task and cgroup.procs - guaranteed that the result
3118 * is sorted, which forced this whole pidlist fiasco. As pid order is
3119 * different per namespace, each namespace needs differently sorted list,
3120 * making it impossible to use, for example, single rbtree of member tasks
3121 * sorted by task pointer. As pidlists can be fairly large, allocating one
3122 * per open file is dangerous, so cgroup had to implement shared pool of
3123 * pidlists keyed by cgroup and namespace.
3124 *
3125 * All this extra complexity was caused by the original implementation
3126 * committing to an entirely unnecessary property. In the long term, we
3127 * want to do away with it. Explicitly scramble sort order if
3128 * sane_behavior so that no such expectation exists in the new interface.
3129 *
3130 * Scrambling is done by swapping every two consecutive bits, which is
3131 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3132 */
3133static pid_t pid_fry(pid_t pid)
3134{
3135 unsigned a = pid & 0x55555555;
3136 unsigned b = pid & 0xAAAAAAAA;
3137
3138 return (a << 1) | (b >> 1);
3139}
3140
3141static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3142{
3143 if (cgroup_sane_behavior(cgrp))
3144 return pid_fry(pid);
3145 else
3146 return pid;
3147}
3148
Ben Blum102a7752009-09-23 15:56:26 -07003149static int cmppid(const void *a, const void *b)
3150{
3151 return *(pid_t *)a - *(pid_t *)b;
3152}
3153
Tejun Heoafb2bc12013-11-29 10:42:59 -05003154static int fried_cmppid(const void *a, const void *b)
3155{
3156 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3157}
3158
Ben Blum72a8cb32009-09-23 15:56:27 -07003159static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3160 enum cgroup_filetype type)
3161{
3162 struct cgroup_pidlist *l;
3163 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003164 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003165
Tejun Heoe6b81712013-11-29 10:42:59 -05003166 lockdep_assert_held(&cgrp->pidlist_mutex);
3167
3168 list_for_each_entry(l, &cgrp->pidlists, links)
3169 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003170 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003171 return NULL;
3172}
3173
Ben Blum72a8cb32009-09-23 15:56:27 -07003174/*
3175 * find the appropriate pidlist for our purpose (given procs vs tasks)
3176 * returns with the lock on that pidlist already held, and takes care
3177 * of the use count, or returns NULL with no locks held if we're out of
3178 * memory.
3179 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003180static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3181 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003182{
3183 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003184
Tejun Heoe6b81712013-11-29 10:42:59 -05003185 lockdep_assert_held(&cgrp->pidlist_mutex);
3186
3187 l = cgroup_pidlist_find(cgrp, type);
3188 if (l)
3189 return l;
3190
Ben Blum72a8cb32009-09-23 15:56:27 -07003191 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003192 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003193 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003194 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003195
Tejun Heob1a21362013-11-29 10:42:58 -05003196 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003197 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003198 /* don't need task_nsproxy() if we're looking at ourself */
3199 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003200 l->owner = cgrp;
3201 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003202 return l;
3203}
3204
3205/*
Ben Blum102a7752009-09-23 15:56:26 -07003206 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3207 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003208static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3209 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003210{
3211 pid_t *array;
3212 int length;
3213 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003214 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003215 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003216 struct cgroup_pidlist *l;
3217
Tejun Heo4bac00d2013-11-29 10:42:59 -05003218 lockdep_assert_held(&cgrp->pidlist_mutex);
3219
Ben Blum102a7752009-09-23 15:56:26 -07003220 /*
3221 * If cgroup gets more users after we read count, we won't have
3222 * enough space - tough. This race is indistinguishable to the
3223 * caller from the case that the additional cgroup users didn't
3224 * show up until sometime later on.
3225 */
3226 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003227 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003228 if (!array)
3229 return -ENOMEM;
3230 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003231 css_task_iter_start(&cgrp->dummy_css, &it);
3232 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003233 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003234 break;
Ben Blum102a7752009-09-23 15:56:26 -07003235 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003236 if (type == CGROUP_FILE_PROCS)
3237 pid = task_tgid_vnr(tsk);
3238 else
3239 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003240 if (pid > 0) /* make sure to only use valid results */
3241 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003242 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003243 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003244 length = n;
3245 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003246 if (cgroup_sane_behavior(cgrp))
3247 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3248 else
3249 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003250 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003251 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003252
Tejun Heoe6b81712013-11-29 10:42:59 -05003253 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003254 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003255 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003256 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003257 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003258 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003259
3260 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003261 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003262 l->list = array;
3263 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003264 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003265 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003266}
3267
Balbir Singh846c7bb2007-10-18 23:39:44 -07003268/**
Li Zefana043e3b2008-02-23 15:24:09 -08003269 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003270 * @stats: cgroupstats to fill information into
3271 * @dentry: A dentry entry belonging to the cgroup for which stats have
3272 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003273 *
3274 * Build and fill cgroupstats so that taskstats can export it to user
3275 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003276 */
3277int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3278{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003279 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003280 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003281 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003282 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003283
Tejun Heo2bd59d42014-02-11 11:52:49 -05003284 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3285 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3286 kernfs_type(kn) != KERNFS_DIR)
3287 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003288
Li Zefanbad34662014-02-14 16:54:28 +08003289 mutex_lock(&cgroup_mutex);
3290
Tejun Heo2bd59d42014-02-11 11:52:49 -05003291 /*
3292 * We aren't being called from kernfs and there's no guarantee on
3293 * @kn->priv's validity. For this and css_tryget_from_dir(),
3294 * @kn->priv is RCU safe. Let's do the RCU dancing.
3295 */
3296 rcu_read_lock();
3297 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003298 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003299 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003300 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003301 return -ENOENT;
3302 }
Li Zefanbad34662014-02-14 16:54:28 +08003303 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003304
Tejun Heo72ec7022013-08-08 20:11:26 -04003305 css_task_iter_start(&cgrp->dummy_css, &it);
3306 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003307 switch (tsk->state) {
3308 case TASK_RUNNING:
3309 stats->nr_running++;
3310 break;
3311 case TASK_INTERRUPTIBLE:
3312 stats->nr_sleeping++;
3313 break;
3314 case TASK_UNINTERRUPTIBLE:
3315 stats->nr_uninterruptible++;
3316 break;
3317 case TASK_STOPPED:
3318 stats->nr_stopped++;
3319 break;
3320 default:
3321 if (delayacct_is_task_waiting_on_io(tsk))
3322 stats->nr_io_wait++;
3323 break;
3324 }
3325 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003326 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003327
Li Zefanbad34662014-02-14 16:54:28 +08003328 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003329 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003330}
3331
Paul Menage8f3ff202009-09-23 15:56:25 -07003332
Paul Menagecc31edc2008-10-18 20:28:04 -07003333/*
Ben Blum102a7752009-09-23 15:56:26 -07003334 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003335 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003336 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003337 */
3338
Ben Blum102a7752009-09-23 15:56:26 -07003339static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003340{
3341 /*
3342 * Initially we receive a position value that corresponds to
3343 * one more than the last pid shown (or 0 on the first call or
3344 * after a seek to the start). Use a binary-search to find the
3345 * next pid to display, if any
3346 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003347 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003348 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003349 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003350 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003351 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003352 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003353
Tejun Heo4bac00d2013-11-29 10:42:59 -05003354 mutex_lock(&cgrp->pidlist_mutex);
3355
3356 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003357 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003358 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003359 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003360 * could already have been destroyed.
3361 */
Tejun Heo5d224442013-12-05 12:28:04 -05003362 if (of->priv)
3363 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003364
3365 /*
3366 * Either this is the first start() after open or the matching
3367 * pidlist has been destroyed inbetween. Create a new one.
3368 */
Tejun Heo5d224442013-12-05 12:28:04 -05003369 if (!of->priv) {
3370 ret = pidlist_array_load(cgrp, type,
3371 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003372 if (ret)
3373 return ERR_PTR(ret);
3374 }
Tejun Heo5d224442013-12-05 12:28:04 -05003375 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003376
Paul Menagecc31edc2008-10-18 20:28:04 -07003377 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003378 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003379
Paul Menagecc31edc2008-10-18 20:28:04 -07003380 while (index < end) {
3381 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003382 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003383 index = mid;
3384 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003385 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003386 index = mid + 1;
3387 else
3388 end = mid;
3389 }
3390 }
3391 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003392 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003393 return NULL;
3394 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003395 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003396 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003397 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003398}
3399
Ben Blum102a7752009-09-23 15:56:26 -07003400static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003401{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003402 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003403 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003404
Tejun Heo5d224442013-12-05 12:28:04 -05003405 if (l)
3406 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003407 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003408 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003409}
3410
Ben Blum102a7752009-09-23 15:56:26 -07003411static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003412{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003413 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003414 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003415 pid_t *p = v;
3416 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003417 /*
3418 * Advance to the next pid in the array. If this goes off the
3419 * end, we're done
3420 */
3421 p++;
3422 if (p >= end) {
3423 return NULL;
3424 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003425 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003426 return p;
3427 }
3428}
3429
Ben Blum102a7752009-09-23 15:56:26 -07003430static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003431{
3432 return seq_printf(s, "%d\n", *(int *)v);
3433}
3434
Ben Blum102a7752009-09-23 15:56:26 -07003435/*
3436 * seq_operations functions for iterating on pidlists through seq_file -
3437 * independent of whether it's tasks or procs
3438 */
3439static const struct seq_operations cgroup_pidlist_seq_operations = {
3440 .start = cgroup_pidlist_start,
3441 .stop = cgroup_pidlist_stop,
3442 .next = cgroup_pidlist_next,
3443 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003444};
3445
Tejun Heo182446d2013-08-08 20:11:24 -04003446static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3447 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003448{
Tejun Heo182446d2013-08-08 20:11:24 -04003449 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003450}
3451
Tejun Heo182446d2013-08-08 20:11:24 -04003452static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3453 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003454{
Tejun Heo182446d2013-08-08 20:11:24 -04003455 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003456 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003457 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003458 else
Tejun Heo182446d2013-08-08 20:11:24 -04003459 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003460 return 0;
3461}
3462
Tejun Heo182446d2013-08-08 20:11:24 -04003463static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3464 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003465{
Tejun Heo182446d2013-08-08 20:11:24 -04003466 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003467}
3468
Tejun Heo182446d2013-08-08 20:11:24 -04003469static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3470 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003471{
3472 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003473 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003474 else
Tejun Heo182446d2013-08-08 20:11:24 -04003475 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003476 return 0;
3477}
3478
Tejun Heod5c56ce2013-06-03 19:14:34 -07003479static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003480 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003481 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003482 .seq_start = cgroup_pidlist_start,
3483 .seq_next = cgroup_pidlist_next,
3484 .seq_stop = cgroup_pidlist_stop,
3485 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003486 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003487 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003488 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003489 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003490 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003491 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003492 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003493 .read_u64 = cgroup_clone_children_read,
3494 .write_u64 = cgroup_clone_children_write,
3495 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003496 {
Tejun Heo873fe092013-04-14 20:15:26 -07003497 .name = "cgroup.sane_behavior",
3498 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003499 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003500 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003501
3502 /*
3503 * Historical crazy stuff. These don't have "cgroup." prefix and
3504 * don't exist if sane_behavior. If you're depending on these, be
3505 * prepared to be burned.
3506 */
3507 {
3508 .name = "tasks",
3509 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003510 .seq_start = cgroup_pidlist_start,
3511 .seq_next = cgroup_pidlist_next,
3512 .seq_stop = cgroup_pidlist_stop,
3513 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003514 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003515 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003516 .mode = S_IRUGO | S_IWUSR,
3517 },
3518 {
3519 .name = "notify_on_release",
3520 .flags = CFTYPE_INSANE,
3521 .read_u64 = cgroup_read_notify_on_release,
3522 .write_u64 = cgroup_write_notify_on_release,
3523 },
Tejun Heo873fe092013-04-14 20:15:26 -07003524 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003525 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003526 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003527 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003528 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003529 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003530 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003531 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003532};
3533
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003534/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003535 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003536 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003537 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003538 *
3539 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003540 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003541static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003542{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003543 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003544 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003545
Tejun Heo8e3f6542012-04-01 12:09:55 -07003546 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003547 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05003548 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07003549
3550 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003551 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003552
Tejun Heo0adb0702014-02-12 09:29:48 -05003553 list_for_each_entry(cfts, &ss->cfts, node) {
3554 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003555 if (ret < 0)
3556 goto err;
3557 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003558 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003559 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003560err:
3561 cgroup_clear_dir(cgrp, subsys_mask);
3562 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003563}
3564
Tejun Heo0c21ead2013-08-13 20:22:51 -04003565/*
3566 * css destruction is four-stage process.
3567 *
3568 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3569 * Implemented in kill_css().
3570 *
3571 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3572 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3573 * by invoking offline_css(). After offlining, the base ref is put.
3574 * Implemented in css_killed_work_fn().
3575 *
3576 * 3. When the percpu_ref reaches zero, the only possible remaining
3577 * accessors are inside RCU read sections. css_release() schedules the
3578 * RCU callback.
3579 *
3580 * 4. After the grace period, the css can be freed. Implemented in
3581 * css_free_work_fn().
3582 *
3583 * It is actually hairier because both step 2 and 4 require process context
3584 * and thus involve punting to css->destroy_work adding two additional
3585 * steps to the already complex sequence.
3586 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003587static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003588{
3589 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003590 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003591 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003592
Tejun Heo0ae78e02013-08-13 11:01:54 -04003593 if (css->parent)
3594 css_put(css->parent);
3595
Tejun Heo0c21ead2013-08-13 20:22:51 -04003596 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003597 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003598}
3599
3600static void css_free_rcu_fn(struct rcu_head *rcu_head)
3601{
3602 struct cgroup_subsys_state *css =
3603 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3604
Tejun Heo0c21ead2013-08-13 20:22:51 -04003605 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003606 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003607}
3608
Tejun Heod3daf282013-06-13 19:39:16 -07003609static void css_release(struct percpu_ref *ref)
3610{
3611 struct cgroup_subsys_state *css =
3612 container_of(ref, struct cgroup_subsys_state, refcnt);
3613
Monam Agarwal01a97142014-03-24 00:17:18 +05303614 RCU_INIT_POINTER(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003615 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003616}
3617
Tejun Heo623f9262013-08-13 11:01:55 -04003618static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3619 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003620{
Paul Menagebd89aab2007-10-18 23:40:44 -07003621 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003622 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003623 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003624
Tejun Heo0ae78e02013-08-13 11:01:54 -04003625 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003626 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003627 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003628 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003629
Tejun Heoca8bdca2013-08-26 18:40:56 -04003630 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003631}
3632
Li Zefan2a4ac632013-07-31 16:16:40 +08003633/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04003634static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003635{
Tejun Heo623f9262013-08-13 11:01:55 -04003636 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08003637 int ret = 0;
3638
Tejun Heoace2bee2014-02-11 11:52:47 -05003639 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003640 lockdep_assert_held(&cgroup_mutex);
3641
Tejun Heo92fb9742012-11-19 08:13:38 -08003642 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04003643 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003644 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04003645 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04003646 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05003647 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003648 }
Tejun Heob1929db2012-11-19 08:13:38 -08003649 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003650}
3651
Li Zefan2a4ac632013-07-31 16:16:40 +08003652/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04003653static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003654{
Tejun Heo623f9262013-08-13 11:01:55 -04003655 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003656
Tejun Heoace2bee2014-02-11 11:52:47 -05003657 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003658 lockdep_assert_held(&cgroup_mutex);
3659
3660 if (!(css->flags & CSS_ONLINE))
3661 return;
3662
Li Zefand7eeac12013-03-12 15:35:59 -07003663 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04003664 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003665
Tejun Heoeb954192013-08-08 20:11:23 -04003666 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04003667 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05003668 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003669}
3670
Tejun Heoc81c925a2013-12-06 15:11:56 -05003671/**
3672 * create_css - create a cgroup_subsys_state
3673 * @cgrp: the cgroup new css will be associated with
3674 * @ss: the subsys of new css
3675 *
3676 * Create a new css associated with @cgrp - @ss pair. On success, the new
3677 * css is online and installed in @cgrp with all interface files created.
3678 * Returns 0 on success, -errno on failure.
3679 */
3680static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
3681{
3682 struct cgroup *parent = cgrp->parent;
3683 struct cgroup_subsys_state *css;
3684 int err;
3685
Tejun Heoc81c925a2013-12-06 15:11:56 -05003686 lockdep_assert_held(&cgroup_mutex);
3687
3688 css = ss->css_alloc(cgroup_css(parent, ss));
3689 if (IS_ERR(css))
3690 return PTR_ERR(css);
3691
3692 err = percpu_ref_init(&css->refcnt, css_release);
3693 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08003694 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05003695
3696 init_css(css, ss, cgrp);
3697
Tejun Heoaec25022014-02-08 10:36:58 -05003698 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003699 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08003700 goto err_free_percpu_ref;
Tejun Heoc81c925a2013-12-06 15:11:56 -05003701
3702 err = online_css(css);
3703 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08003704 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05003705
Tejun Heo59f52962014-02-11 11:52:49 -05003706 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003707 css_get(css->parent);
3708
3709 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
3710 parent->parent) {
3711 pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
3712 current->comm, current->pid, ss->name);
3713 if (!strcmp(ss->name, "memory"))
3714 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
3715 ss->warned_broken_hierarchy = true;
3716 }
3717
3718 return 0;
3719
Li Zefan3eb59ec2014-03-18 17:02:36 +08003720err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07003721 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08003722err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05003723 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08003724err_free_css:
Tejun Heoc81c925a2013-12-06 15:11:56 -05003725 ss->css_free(css);
3726 return err;
3727}
3728
Tejun Heo2bd59d42014-02-11 11:52:49 -05003729/**
Li Zefana043e3b2008-02-23 15:24:09 -08003730 * cgroup_create - create a cgroup
3731 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05003732 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05003733 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07003734 */
Tejun Heoe61734c2014-02-12 09:29:50 -05003735static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003736 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003737{
Paul Menagebd89aab2007-10-18 23:40:44 -07003738 struct cgroup *cgrp;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003739 struct cgroup_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05003740 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003741 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003742 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003743
Tejun Heoa2dd4242014-03-19 10:23:55 -04003744 /*
3745 * XXX: The default hierarchy isn't fully implemented yet. Block
3746 * !root cgroup creation on it for now.
3747 */
3748 if (root == &cgrp_dfl_root)
3749 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003750
Tejun Heo0a950f62012-11-19 09:02:12 -08003751 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07003752 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3753 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003754 return -ENOMEM;
3755
Tejun Heoace2bee2014-02-11 11:52:47 -05003756 mutex_lock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003757
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003758 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08003759 * Only live parents can have children. Note that the liveliness
3760 * check isn't strictly necessary because cgroup_mkdir() and
3761 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
3762 * anyway so that locking is contained inside cgroup proper and we
3763 * don't get nasty surprises if we ever grow another caller.
3764 */
3765 if (!cgroup_lock_live_group(parent)) {
3766 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05003767 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08003768 }
3769
3770 /*
3771 * Temporarily set the pointer to NULL, so idr_find() won't return
3772 * a half-baked cgroup.
3773 */
3774 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
3775 if (cgrp->id < 0) {
3776 err = -ENOMEM;
3777 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08003778 }
3779
Paul Menagecc31edc2008-10-18 20:28:04 -07003780 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003781
Paul Menagebd89aab2007-10-18 23:40:44 -07003782 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003783 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07003784 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003785
Li Zefanb6abdb02008-03-04 14:28:19 -08003786 if (notify_on_release(parent))
3787 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3788
Tejun Heo2260e7f2012-11-19 08:13:38 -08003789 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
3790 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003791
Tejun Heo2bd59d42014-02-11 11:52:49 -05003792 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05003793 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003794 if (IS_ERR(kn)) {
3795 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003796 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003797 }
3798 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003799
Tejun Heo6f305582014-02-12 09:29:50 -05003800 /*
3801 * This extra ref will be put in cgroup_free_fn() and guarantees
3802 * that @cgrp->kn is always accessible.
3803 */
3804 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003805
Tejun Heo00356bd2013-06-18 11:14:22 -07003806 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09003807
Tejun Heo4e139af2012-11-19 08:13:36 -08003808 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08003809 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05003810 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05003811 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08003812
Tejun Heo0d802552013-12-06 15:11:56 -05003813 /*
3814 * @cgrp is now fully operational. If something fails after this
3815 * point, it'll be released via the normal destruction path.
3816 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003817 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
3818
Tejun Heo49957f82014-04-07 16:44:47 -04003819 err = cgroup_kn_set_ugid(kn);
3820 if (err)
3821 goto err_destroy;
3822
Tejun Heo2bb566c2013-08-08 20:11:23 -04003823 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07003824 if (err)
3825 goto err_destroy;
3826
Tejun Heo9d403e92013-12-06 15:11:56 -05003827 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05003828 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04003829 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heob85d2042013-12-06 15:11:57 -05003830 err = create_css(cgrp, ss);
3831 if (err)
3832 goto err_destroy;
3833 }
Tejun Heoa8638032012-11-09 09:12:29 -08003834 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003835
Tejun Heof392e512014-04-23 11:13:14 -04003836 cgrp->child_subsys_mask = parent->child_subsys_mask;
3837
Tejun Heo2bd59d42014-02-11 11:52:49 -05003838 kernfs_activate(kn);
3839
Paul Menageddbcc7e2007-10-18 23:39:30 -07003840 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003841 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003842
3843 return 0;
3844
Tejun Heo0a950f62012-11-19 09:02:12 -08003845err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003846 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003847err_unlock:
3848 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003849err_unlock_tree:
3850 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003851 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003852 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003853
3854err_destroy:
3855 cgroup_destroy_locked(cgrp);
3856 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003857 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003858 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003859}
3860
Tejun Heo2bd59d42014-02-11 11:52:49 -05003861static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3862 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003863{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003864 struct cgroup *parent = parent_kn->priv;
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003865 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003866
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003867 /*
3868 * cgroup_create() grabs cgroup_tree_mutex which nests outside
3869 * kernfs active_ref and cgroup_create() already synchronizes
3870 * properly against removal through cgroup_lock_live_group().
3871 * Break it before calling cgroup_create().
3872 */
3873 cgroup_get(parent);
3874 kernfs_break_active_protection(parent_kn);
3875
3876 ret = cgroup_create(parent, name, mode);
3877
3878 kernfs_unbreak_active_protection(parent_kn);
3879 cgroup_put(parent);
3880 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003881}
3882
Tejun Heo223dbc32013-08-13 20:22:50 -04003883/*
3884 * This is called when the refcnt of a css is confirmed to be killed.
3885 * css_tryget() is now guaranteed to fail.
3886 */
3887static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07003888{
Tejun Heo223dbc32013-08-13 20:22:50 -04003889 struct cgroup_subsys_state *css =
3890 container_of(work, struct cgroup_subsys_state, destroy_work);
3891 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07003892
Tejun Heoace2bee2014-02-11 11:52:47 -05003893 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003894 mutex_lock(&cgroup_mutex);
3895
3896 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04003897 * css_tryget() is guaranteed to fail now. Tell subsystems to
3898 * initate destruction.
3899 */
3900 offline_css(css);
3901
3902 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003903 * If @cgrp is marked dead, it's waiting for refs of all css's to
3904 * be disabled before proceeding to the second phase of cgroup
3905 * destruction. If we are the last one, kick it off.
3906 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04003907 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04003908 cgroup_destroy_css_killed(cgrp);
3909
3910 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003911 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04003912
3913 /*
3914 * Put the css refs from kill_css(). Each css holds an extra
3915 * reference to the cgroup's dentry and cgroup removal proceeds
3916 * regardless of css refs. On the last put of each css, whenever
3917 * that may be, the extra dentry ref is put so that dentry
3918 * destruction happens only after all css's are released.
3919 */
3920 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07003921}
3922
Tejun Heo223dbc32013-08-13 20:22:50 -04003923/* css kill confirmation processing requires process context, bounce */
3924static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07003925{
3926 struct cgroup_subsys_state *css =
3927 container_of(ref, struct cgroup_subsys_state, refcnt);
3928
Tejun Heo223dbc32013-08-13 20:22:50 -04003929 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003930 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07003931}
3932
Tejun Heof392e512014-04-23 11:13:14 -04003933/**
3934 * kill_css - destroy a css
3935 * @css: css to destroy
3936 *
3937 * This function initiates destruction of @css by removing cgroup interface
3938 * files and putting its base reference. ->css_offline() will be invoked
3939 * asynchronously once css_tryget() is guaranteed to fail and when the
3940 * reference count reaches zero, @css will be released.
3941 */
3942static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04003943{
Tejun Heo94419622014-03-19 10:23:54 -04003944 lockdep_assert_held(&cgroup_tree_mutex);
3945
Tejun Heo2bd59d42014-02-11 11:52:49 -05003946 /*
3947 * This must happen before css is disassociated with its cgroup.
3948 * See seq_css() for details.
3949 */
Tejun Heoaec25022014-02-08 10:36:58 -05003950 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003951
Tejun Heoedae0c32013-08-13 20:22:51 -04003952 /*
3953 * Killing would put the base ref, but we need to keep it alive
3954 * until after ->css_offline().
3955 */
3956 css_get(css);
3957
3958 /*
3959 * cgroup core guarantees that, by the time ->css_offline() is
3960 * invoked, no new css reference will be given out via
3961 * css_tryget(). We can't simply call percpu_ref_kill() and
3962 * proceed to offlining css's because percpu_ref_kill() doesn't
3963 * guarantee that the ref is seen as killed on all CPUs on return.
3964 *
3965 * Use percpu_ref_kill_and_confirm() to get notifications as each
3966 * css is confirmed to be seen as killed on all CPUs.
3967 */
3968 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003969}
3970
3971/**
3972 * cgroup_destroy_locked - the first stage of cgroup destruction
3973 * @cgrp: cgroup to be destroyed
3974 *
3975 * css's make use of percpu refcnts whose killing latency shouldn't be
3976 * exposed to userland and are RCU protected. Also, cgroup core needs to
3977 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
3978 * invoked. To satisfy all the requirements, destruction is implemented in
3979 * the following two steps.
3980 *
3981 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
3982 * userland visible parts and start killing the percpu refcnts of
3983 * css's. Set up so that the next stage will be kicked off once all
3984 * the percpu refcnts are confirmed to be killed.
3985 *
3986 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
3987 * rest of destruction. Once all cgroup references are gone, the
3988 * cgroup is RCU-freed.
3989 *
3990 * This function implements s1. After this step, @cgrp is gone as far as
3991 * the userland is concerned and a new cgroup with the same name may be
3992 * created. As cgroup doesn't care about the names internally, this
3993 * doesn't cause any problem.
3994 */
Tejun Heo42809dd2012-11-19 08:13:37 -08003995static int cgroup_destroy_locked(struct cgroup *cgrp)
3996 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003997{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003998 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003999 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004000 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004001 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004002
Tejun Heoace2bee2014-02-11 11:52:47 -05004003 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004004 lockdep_assert_held(&cgroup_mutex);
4005
Tejun Heoddd69142013-06-12 21:04:54 -07004006 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004007 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004008 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004009 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004010 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004011 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004012 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004013 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004014 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004015
Tejun Heo1a90dd52012-11-05 09:16:59 -08004016 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004017 * Make sure there's no live children. We can't test ->children
4018 * emptiness as dead children linger on it while being destroyed;
4019 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4020 */
4021 empty = true;
4022 rcu_read_lock();
4023 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4024 empty = cgroup_is_dead(child);
4025 if (!empty)
4026 break;
4027 }
4028 rcu_read_unlock();
4029 if (!empty)
4030 return -EBUSY;
4031
4032 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004033 * Mark @cgrp dead. This prevents further task migration and child
4034 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004035 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004036 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004037 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004038 */
Tejun Heo54766d42013-06-12 21:04:53 -07004039 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004040
Tejun Heo5d773812014-03-19 10:23:53 -04004041 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004042 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4043 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004044 * percpu refs of all css's are confirmed to be killed. This
4045 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004046 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004047 mutex_unlock(&cgroup_mutex);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004048 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004049 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004050 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004051
Tejun Heo455050d2013-06-13 19:27:41 -07004052 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4053 raw_spin_lock(&release_list_lock);
4054 if (!list_empty(&cgrp->release_list))
4055 list_del_init(&cgrp->release_list);
4056 raw_spin_unlock(&release_list_lock);
4057
4058 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004059 * If @cgrp has css's attached, the second stage of cgroup
4060 * destruction is kicked off from css_killed_work_fn() after the
4061 * refs of all attached css's are killed. If @cgrp doesn't have
4062 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004063 */
Tejun Heof20104d2013-08-13 20:22:50 -04004064 if (!cgrp->nr_css)
4065 cgroup_destroy_css_killed(cgrp);
4066
Tejun Heo2bd59d42014-02-11 11:52:49 -05004067 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05004068 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004069
Tejun Heof20104d2013-08-13 20:22:50 -04004070 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05004071 * There are two control paths which try to determine cgroup from
4072 * dentry without going through kernfs - cgroupstats_build() and
4073 * css_tryget_from_dir(). Those are supported by RCU protecting
4074 * clearing of cgrp->kn->priv backpointer, which should happen
4075 * after all files under it have been removed.
Tejun Heo455050d2013-06-13 19:27:41 -07004076 */
Tejun Heo6f305582014-02-12 09:29:50 -05004077 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004078 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004079
Tejun Heo4ac06012014-02-11 11:52:47 -05004080 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004081
Tejun Heoea15f8c2013-06-13 19:27:42 -07004082 return 0;
4083};
4084
Tejun Heod3daf282013-06-13 19:39:16 -07004085/**
Tejun Heof20104d2013-08-13 20:22:50 -04004086 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004087 * @work: cgroup->destroy_free_work
4088 *
4089 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004090 * destroyed after all css's are offlined and performs the rest of
4091 * destruction. This is the second step of destruction described in the
4092 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004093 */
Tejun Heof20104d2013-08-13 20:22:50 -04004094static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004095{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004096 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004097
Tejun Heoace2bee2014-02-11 11:52:47 -05004098 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004099 lockdep_assert_held(&cgroup_mutex);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004100
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004101 /* delete this cgroup from parent->children */
4102 list_del_rcu(&cgrp->sibling);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004103
Tejun Heo59f52962014-02-11 11:52:49 -05004104 cgroup_put(cgrp);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004105
Paul Menageddbcc7e2007-10-18 23:39:30 -07004106 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004107 check_for_release(parent);
4108}
4109
Tejun Heo2bd59d42014-02-11 11:52:49 -05004110static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004111{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004112 struct cgroup *cgrp = kn->priv;
4113 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004114
Tejun Heo2bd59d42014-02-11 11:52:49 -05004115 /*
4116 * This is self-destruction but @kn can't be removed while this
4117 * callback is in progress. Let's break active protection. Once
4118 * the protection is broken, @cgrp can be destroyed at any point.
4119 * Pin it so that it stays accessible.
4120 */
4121 cgroup_get(cgrp);
4122 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004123
Tejun Heoace2bee2014-02-11 11:52:47 -05004124 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004125 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004126
Tejun Heo2bd59d42014-02-11 11:52:49 -05004127 /*
4128 * @cgrp might already have been destroyed while we're trying to
4129 * grab the mutexes.
4130 */
4131 if (!cgroup_is_dead(cgrp))
4132 ret = cgroup_destroy_locked(cgrp);
4133
Tejun Heo42809dd2012-11-19 08:13:37 -08004134 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004135 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004136
Tejun Heo2bd59d42014-02-11 11:52:49 -05004137 kernfs_unbreak_active_protection(kn);
4138 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004139 return ret;
4140}
4141
Tejun Heo2bd59d42014-02-11 11:52:49 -05004142static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4143 .remount_fs = cgroup_remount,
4144 .show_options = cgroup_show_options,
4145 .mkdir = cgroup_mkdir,
4146 .rmdir = cgroup_rmdir,
4147 .rename = cgroup_rename,
4148};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004149
Li Zefan06a11922008-04-29 01:00:07 -07004150static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004151{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004152 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004153
4154 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004155
Tejun Heoace2bee2014-02-11 11:52:47 -05004156 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004157 mutex_lock(&cgroup_mutex);
4158
Tejun Heo0adb0702014-02-12 09:29:48 -05004159 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004160
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004161 /* Create the root cgroup state for this subsystem */
4162 ss->root = &cgrp_dfl_root;
4163 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004164 /* We don't handle early failures gracefully */
4165 BUG_ON(IS_ERR(css));
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004166 init_css(css, ss, &cgrp_dfl_root.cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004167
Li Zefane8d55fd2008-04-29 01:00:13 -07004168 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004169 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004170 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004171 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004172 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004173
4174 need_forkexit_callback |= ss->fork || ss->exit;
4175
Li Zefane8d55fd2008-04-29 01:00:13 -07004176 /* At system boot, before all subsystems have been
4177 * registered, no tasks have been forked, so we don't
4178 * need to invoke fork callbacks here. */
4179 BUG_ON(!list_empty(&init_task.tasks));
4180
Tejun Heoae7f1642013-08-13 20:22:50 -04004181 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004182
Tejun Heof392e512014-04-23 11:13:14 -04004183 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004184
Ben Blume6a11052010-03-10 15:22:09 -08004185 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004186 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004187}
4188
4189/**
Li Zefana043e3b2008-02-23 15:24:09 -08004190 * cgroup_init_early - cgroup initialization at system boot
4191 *
4192 * Initialize cgroups at system boot, and initialize any
4193 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004194 */
4195int __init cgroup_init_early(void)
4196{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004197 static struct cgroup_sb_opts __initdata opts =
4198 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004199 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004200 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004201
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004202 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004203 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004204
Tejun Heo3ed80a62014-02-08 10:36:58 -05004205 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004206 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004207 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4208 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004209 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004210 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4211 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004212
Tejun Heoaec25022014-02-08 10:36:58 -05004213 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004214 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004215
4216 if (ss->early_init)
4217 cgroup_init_subsys(ss);
4218 }
4219 return 0;
4220}
4221
4222/**
Li Zefana043e3b2008-02-23 15:24:09 -08004223 * cgroup_init - cgroup initialization
4224 *
4225 * Register cgroup filesystem and /proc file, and initialize
4226 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004227 */
4228int __init cgroup_init(void)
4229{
Tejun Heo30159ec2013-06-25 11:53:37 -07004230 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004231 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004232 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004233
Tejun Heo2bd59d42014-02-11 11:52:49 -05004234 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004235
Tejun Heo985ed672014-03-19 10:23:53 -04004236 mutex_lock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004237 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004238
Tejun Heo82fe9b02013-06-25 11:53:37 -07004239 /* Add init_css_set to the hash table */
4240 key = css_set_hash(init_css_set.subsys);
4241 hash_add(css_set_table, &init_css_set.hlist, key);
4242
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004243 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004244
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004245 mutex_unlock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04004246 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004247
Tejun Heo172a2c062014-03-19 10:23:53 -04004248 for_each_subsys(ss, ssid) {
4249 if (!ss->early_init)
4250 cgroup_init_subsys(ss);
4251
4252 /*
4253 * cftype registration needs kmalloc and can't be done
4254 * during early_init. Register base cftypes separately.
4255 */
4256 if (ss->base_cftypes)
4257 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4258 }
Greg KH676db4a2010-08-05 13:53:35 -07004259
Paul Menageddbcc7e2007-10-18 23:39:30 -07004260 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004261 if (!cgroup_kobj)
4262 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004263
4264 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004265 if (err < 0) {
4266 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004267 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004268 }
4269
4270 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004271 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004272}
Paul Menageb4f48b62007-10-18 23:39:33 -07004273
Tejun Heoe5fca242013-11-22 17:14:39 -05004274static int __init cgroup_wq_init(void)
4275{
4276 /*
4277 * There isn't much point in executing destruction path in
4278 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004279 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004280 *
4281 * We would prefer to do this in cgroup_init() above, but that
4282 * is called before init_workqueues(): so leave this until after.
4283 */
Tejun Heo1a115332014-02-12 19:06:19 -05004284 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004285 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004286
4287 /*
4288 * Used to destroy pidlists and separate to serve as flush domain.
4289 * Cap @max_active to 1 too.
4290 */
4291 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4292 0, 1);
4293 BUG_ON(!cgroup_pidlist_destroy_wq);
4294
Tejun Heoe5fca242013-11-22 17:14:39 -05004295 return 0;
4296}
4297core_initcall(cgroup_wq_init);
4298
Paul Menagea4243162007-10-18 23:39:35 -07004299/*
4300 * proc_cgroup_show()
4301 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4302 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004303 */
4304
4305/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004306int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004307{
4308 struct pid *pid;
4309 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004310 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004311 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004312 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004313
4314 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004315 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004316 if (!buf)
4317 goto out;
4318
4319 retval = -ESRCH;
4320 pid = m->private;
4321 tsk = get_pid_task(pid, PIDTYPE_PID);
4322 if (!tsk)
4323 goto out_free;
4324
4325 retval = 0;
4326
4327 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004328 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004329
Tejun Heo985ed672014-03-19 10:23:53 -04004330 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004331 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004332 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004333 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004334
Tejun Heoa2dd4242014-03-19 10:23:55 -04004335 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004336 continue;
4337
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004338 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004339 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004340 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004341 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004342 if (strlen(root->name))
4343 seq_printf(m, "%sname=%s", count ? "," : "",
4344 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004345 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004346 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004347 path = cgroup_path(cgrp, buf, PATH_MAX);
4348 if (!path) {
4349 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004350 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004351 }
4352 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004353 seq_putc(m, '\n');
4354 }
4355
4356out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004357 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004358 mutex_unlock(&cgroup_mutex);
4359 put_task_struct(tsk);
4360out_free:
4361 kfree(buf);
4362out:
4363 return retval;
4364}
4365
Paul Menagea4243162007-10-18 23:39:35 -07004366/* Display information about each subsystem and each hierarchy */
4367static int proc_cgroupstats_show(struct seq_file *m, void *v)
4368{
Tejun Heo30159ec2013-06-25 11:53:37 -07004369 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004370 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004371
Paul Menage8bab8dd2008-04-04 14:29:57 -07004372 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004373 /*
4374 * ideally we don't want subsystems moving around while we do this.
4375 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4376 * subsys/hierarchy state.
4377 */
Paul Menagea4243162007-10-18 23:39:35 -07004378 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004379
4380 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004381 seq_printf(m, "%s\t%d\t%d\t%d\n",
4382 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004383 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004384
Paul Menagea4243162007-10-18 23:39:35 -07004385 mutex_unlock(&cgroup_mutex);
4386 return 0;
4387}
4388
4389static int cgroupstats_open(struct inode *inode, struct file *file)
4390{
Al Viro9dce07f2008-03-29 03:07:28 +00004391 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004392}
4393
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004394static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004395 .open = cgroupstats_open,
4396 .read = seq_read,
4397 .llseek = seq_lseek,
4398 .release = single_release,
4399};
4400
Paul Menageb4f48b62007-10-18 23:39:33 -07004401/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004402 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004403 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004404 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004405 * A task is associated with the init_css_set until cgroup_post_fork()
4406 * attaches it to the parent's css_set. Empty cg_list indicates that
4407 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004408 */
4409void cgroup_fork(struct task_struct *child)
4410{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004411 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004412 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004413}
4414
4415/**
Li Zefana043e3b2008-02-23 15:24:09 -08004416 * cgroup_post_fork - called on a new task after adding it to the task list
4417 * @child: the task in question
4418 *
Tejun Heo5edee612012-10-16 15:03:14 -07004419 * Adds the task to the list running through its css_set if necessary and
4420 * call the subsystem fork() callbacks. Has to be after the task is
4421 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004422 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004423 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004424 */
Paul Menage817929e2007-10-18 23:39:36 -07004425void cgroup_post_fork(struct task_struct *child)
4426{
Tejun Heo30159ec2013-06-25 11:53:37 -07004427 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004428 int i;
4429
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004430 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004431 * This may race against cgroup_enable_task_cg_links(). As that
4432 * function sets use_task_css_set_links before grabbing
4433 * tasklist_lock and we just went through tasklist_lock to add
4434 * @child, it's guaranteed that either we see the set
4435 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4436 * @child during its iteration.
4437 *
4438 * If we won the race, @child is associated with %current's
4439 * css_set. Grabbing css_set_rwsem guarantees both that the
4440 * association is stable, and, on completion of the parent's
4441 * migration, @child is visible in the source of migration or
4442 * already in the destination cgroup. This guarantee is necessary
4443 * when implementing operations which need to migrate all tasks of
4444 * a cgroup to another.
4445 *
4446 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4447 * will remain in init_css_set. This is safe because all tasks are
4448 * in the init_css_set before cg_links is enabled and there's no
4449 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004450 */
Paul Menage817929e2007-10-18 23:39:36 -07004451 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004452 struct css_set *cset;
4453
Tejun Heo96d365e2014-02-13 06:58:40 -05004454 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004455 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004456 if (list_empty(&child->cg_list)) {
4457 rcu_assign_pointer(child->cgroups, cset);
4458 list_add(&child->cg_list, &cset->tasks);
4459 get_css_set(cset);
4460 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004461 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004462 }
Tejun Heo5edee612012-10-16 15:03:14 -07004463
4464 /*
4465 * Call ss->fork(). This must happen after @child is linked on
4466 * css_set; otherwise, @child might change state between ->fork()
4467 * and addition to css_set.
4468 */
4469 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004470 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004471 if (ss->fork)
4472 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004473 }
Paul Menage817929e2007-10-18 23:39:36 -07004474}
Tejun Heo5edee612012-10-16 15:03:14 -07004475
Paul Menage817929e2007-10-18 23:39:36 -07004476/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004477 * cgroup_exit - detach cgroup from exiting task
4478 * @tsk: pointer to task_struct of exiting process
4479 *
4480 * Description: Detach cgroup from @tsk and release it.
4481 *
4482 * Note that cgroups marked notify_on_release force every task in
4483 * them to take the global cgroup_mutex mutex when exiting.
4484 * This could impact scaling on very large systems. Be reluctant to
4485 * use notify_on_release cgroups where very high task exit scaling
4486 * is required on large systems.
4487 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004488 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4489 * call cgroup_exit() while the task is still competent to handle
4490 * notify_on_release(), then leave the task attached to the root cgroup in
4491 * each hierarchy for the remainder of its exit. No need to bother with
4492 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004493 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004494 */
Li Zefan1ec41832014-03-28 15:22:19 +08004495void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004496{
Tejun Heo30159ec2013-06-25 11:53:37 -07004497 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004498 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004499 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004500 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004501
4502 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05004503 * Unlink from @tsk from its css_set. As migration path can't race
4504 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004505 */
4506 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004507 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004508 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004509 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004510 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07004511 }
4512
Paul Menageb4f48b62007-10-18 23:39:33 -07004513 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07004514 cset = task_css_set(tsk);
4515 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004516
Li Zefan1ec41832014-03-28 15:22:19 +08004517 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004518 /* see cgroup_post_fork() for details */
4519 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004520 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004521 struct cgroup_subsys_state *old_css = cset->subsys[i];
4522 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004523
Tejun Heoeb954192013-08-08 20:11:23 -04004524 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004525 }
4526 }
4527 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004528
Tejun Heoeaf797a2014-02-25 10:04:03 -05004529 if (put_cset)
4530 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07004531}
Paul Menage697f4162007-10-18 23:39:34 -07004532
Paul Menagebd89aab2007-10-18 23:40:44 -07004533static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004534{
Li Zefanf50daa72013-03-01 15:06:07 +08004535 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004536 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004537 /*
4538 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004539 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004540 * it now
4541 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004542 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004543
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004544 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004545 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004546 list_empty(&cgrp->release_list)) {
4547 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004548 need_schedule_work = 1;
4549 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004550 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004551 if (need_schedule_work)
4552 schedule_work(&release_agent_work);
4553 }
4554}
4555
Paul Menage81a6a5c2007-10-18 23:39:38 -07004556/*
4557 * Notify userspace when a cgroup is released, by running the
4558 * configured release agent with the name of the cgroup (path
4559 * relative to the root of cgroup file system) as the argument.
4560 *
4561 * Most likely, this user command will try to rmdir this cgroup.
4562 *
4563 * This races with the possibility that some other task will be
4564 * attached to this cgroup before it is removed, or that some other
4565 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4566 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4567 * unused, and this cgroup will be reprieved from its death sentence,
4568 * to continue to serve a useful existence. Next time it's released,
4569 * we will get notified again, if it still has 'notify_on_release' set.
4570 *
4571 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4572 * means only wait until the task is successfully execve()'d. The
4573 * separate release agent task is forked by call_usermodehelper(),
4574 * then control in this thread returns here, without waiting for the
4575 * release agent task. We don't bother to wait because the caller of
4576 * this routine has no use for the exit status of the release agent
4577 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004578 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004579static void cgroup_release_agent(struct work_struct *work)
4580{
4581 BUG_ON(work != &release_agent_work);
4582 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004583 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004584 while (!list_empty(&release_list)) {
4585 char *argv[3], *envp[3];
4586 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05004587 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07004588 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004589 struct cgroup,
4590 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004591 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004592 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05004593 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004594 if (!pathbuf)
4595 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05004596 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
4597 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07004598 goto continue_free;
4599 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4600 if (!agentbuf)
4601 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004602
4603 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004604 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05004605 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004606 argv[i] = NULL;
4607
4608 i = 0;
4609 /* minimal command environment */
4610 envp[i++] = "HOME=/";
4611 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4612 envp[i] = NULL;
4613
4614 /* Drop the lock while we invoke the usermode helper,
4615 * since the exec could involve hitting disk and hence
4616 * be a slow process */
4617 mutex_unlock(&cgroup_mutex);
4618 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004619 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004620 continue_free:
4621 kfree(pathbuf);
4622 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004623 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004624 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004625 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004626 mutex_unlock(&cgroup_mutex);
4627}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004628
4629static int __init cgroup_disable(char *str)
4630{
Tejun Heo30159ec2013-06-25 11:53:37 -07004631 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004632 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07004633 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004634
4635 while ((token = strsep(&str, ",")) != NULL) {
4636 if (!*token)
4637 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004638
Tejun Heo3ed80a62014-02-08 10:36:58 -05004639 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004640 if (!strcmp(token, ss->name)) {
4641 ss->disabled = 1;
4642 printk(KERN_INFO "Disabling %s control group"
4643 " subsystem\n", ss->name);
4644 break;
4645 }
4646 }
4647 }
4648 return 1;
4649}
4650__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004651
Tejun Heob77d7b62013-08-13 11:01:54 -04004652/**
Tejun Heo5a17f542014-02-11 11:52:47 -05004653 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04004654 * @dentry: directory dentry of interest
4655 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04004656 *
Tejun Heo5a17f542014-02-11 11:52:47 -05004657 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4658 * to get the corresponding css and return it. If such css doesn't exist
4659 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02004660 */
Tejun Heo5a17f542014-02-11 11:52:47 -05004661struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4662 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004663{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004664 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4665 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004666 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004667
Tejun Heo35cf0832013-08-26 18:40:56 -04004668 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004669 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4670 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004671 return ERR_PTR(-EBADF);
4672
Tejun Heo5a17f542014-02-11 11:52:47 -05004673 rcu_read_lock();
4674
Tejun Heo2bd59d42014-02-11 11:52:49 -05004675 /*
4676 * This path doesn't originate from kernfs and @kn could already
4677 * have been or be removed at any point. @kn->priv is RCU
4678 * protected for this access. See destroy_locked() for details.
4679 */
4680 cgrp = rcu_dereference(kn->priv);
4681 if (cgrp)
4682 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05004683
4684 if (!css || !css_tryget(css))
4685 css = ERR_PTR(-ENOENT);
4686
4687 rcu_read_unlock();
4688 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004689}
Stephane Eraniane5d13672011-02-14 11:20:01 +02004690
Li Zefan1cb650b2013-08-19 10:05:24 +08004691/**
4692 * css_from_id - lookup css by id
4693 * @id: the cgroup id
4694 * @ss: cgroup subsys to be looked into
4695 *
4696 * Returns the css if there's valid one with @id, otherwise returns NULL.
4697 * Should be called under rcu_read_lock().
4698 */
4699struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4700{
4701 struct cgroup *cgrp;
4702
Tejun Heoace2bee2014-02-11 11:52:47 -05004703 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08004704
4705 cgrp = idr_find(&ss->root->cgroup_idr, id);
4706 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04004707 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08004708 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004709}
4710
Paul Menagefe693432009-09-23 15:56:20 -07004711#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04004712static struct cgroup_subsys_state *
4713debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07004714{
4715 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4716
4717 if (!css)
4718 return ERR_PTR(-ENOMEM);
4719
4720 return css;
4721}
4722
Tejun Heoeb954192013-08-08 20:11:23 -04004723static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07004724{
Tejun Heoeb954192013-08-08 20:11:23 -04004725 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07004726}
4727
Tejun Heo182446d2013-08-08 20:11:24 -04004728static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
4729 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004730{
Tejun Heo182446d2013-08-08 20:11:24 -04004731 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07004732}
4733
Tejun Heo182446d2013-08-08 20:11:24 -04004734static u64 current_css_set_read(struct cgroup_subsys_state *css,
4735 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004736{
4737 return (u64)(unsigned long)current->cgroups;
4738}
4739
Tejun Heo182446d2013-08-08 20:11:24 -04004740static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08004741 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004742{
4743 u64 count;
4744
4745 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07004746 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07004747 rcu_read_unlock();
4748 return count;
4749}
4750
Tejun Heo2da8ca82013-12-05 12:28:04 -05004751static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004752{
Tejun Heo69d02062013-06-12 21:04:50 -07004753 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07004754 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05004755 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07004756
Tejun Heoe61734c2014-02-12 09:29:50 -05004757 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
4758 if (!name_buf)
4759 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07004760
Tejun Heo96d365e2014-02-13 06:58:40 -05004761 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07004762 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07004763 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07004764 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004765 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07004766
Tejun Heoa2dd4242014-03-19 10:23:55 -04004767 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004768 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04004769 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004770 }
4771 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05004772 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05004773 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004774 return 0;
4775}
4776
4777#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05004778static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004779{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004780 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07004781 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07004782
Tejun Heo96d365e2014-02-13 06:58:40 -05004783 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04004784 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004785 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004786 struct task_struct *task;
4787 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05004788
Tejun Heo5abb8852013-06-12 21:04:49 -07004789 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05004790
Tejun Heo5abb8852013-06-12 21:04:49 -07004791 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05004792 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
4793 goto overflow;
4794 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07004795 }
Tejun Heoc7561122014-02-25 10:04:01 -05004796
4797 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
4798 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
4799 goto overflow;
4800 seq_printf(seq, " task %d\n", task_pid_vnr(task));
4801 }
4802 continue;
4803 overflow:
4804 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07004805 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004806 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07004807 return 0;
4808}
4809
Tejun Heo182446d2013-08-08 20:11:24 -04004810static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004811{
Tejun Heo182446d2013-08-08 20:11:24 -04004812 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07004813}
4814
4815static struct cftype debug_files[] = {
4816 {
Paul Menagefe693432009-09-23 15:56:20 -07004817 .name = "taskcount",
4818 .read_u64 = debug_taskcount_read,
4819 },
4820
4821 {
4822 .name = "current_css_set",
4823 .read_u64 = current_css_set_read,
4824 },
4825
4826 {
4827 .name = "current_css_set_refcount",
4828 .read_u64 = current_css_set_refcount_read,
4829 },
4830
4831 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004832 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004833 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004834 },
4835
4836 {
4837 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004838 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004839 },
4840
4841 {
Paul Menagefe693432009-09-23 15:56:20 -07004842 .name = "releasable",
4843 .read_u64 = releasable_read,
4844 },
Paul Menagefe693432009-09-23 15:56:20 -07004845
Tejun Heo4baf6e32012-04-01 12:09:55 -07004846 { } /* terminate */
4847};
Paul Menagefe693432009-09-23 15:56:20 -07004848
Tejun Heo073219e2014-02-08 10:36:58 -05004849struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08004850 .css_alloc = debug_css_alloc,
4851 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07004852 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07004853};
4854#endif /* CONFIG_CGROUP_DEBUG */