blob: 2a4f88db3205ad114a3eabccd05033285d7848b7 [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 Heo2d8f2432014-04-23 11:13:15 -0400428 struct cgroup_subsys *ss;
429 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700430
Tejun Heo89c55092014-02-13 06:58:40 -0500431 lockdep_assert_held(&css_set_rwsem);
432
433 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700434 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700435
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700436 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400437 for_each_subsys(ss, ssid)
438 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700439 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700440 css_set_count--;
441
Tejun Heo69d02062013-06-12 21:04:50 -0700442 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700443 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700444
Tejun Heo69d02062013-06-12 21:04:50 -0700445 list_del(&link->cset_link);
446 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800447
Tejun Heo96d365e2014-02-13 06:58:40 -0500448 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700449 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700450 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700451 set_bit(CGRP_RELEASABLE, &cgrp->flags);
452 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700453 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700454
455 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700456 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700457
Tejun Heo5abb8852013-06-12 21:04:49 -0700458 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700459}
460
Tejun Heo89c55092014-02-13 06:58:40 -0500461static void put_css_set(struct css_set *cset, bool taskexit)
462{
463 /*
464 * Ensure that the refcount doesn't hit zero while any readers
465 * can see it. Similar to atomic_dec_and_lock(), but for an
466 * rwlock
467 */
468 if (atomic_add_unless(&cset->refcount, -1, 1))
469 return;
470
471 down_write(&css_set_rwsem);
472 put_css_set_locked(cset, taskexit);
473 up_write(&css_set_rwsem);
474}
475
Paul Menage817929e2007-10-18 23:39:36 -0700476/*
477 * refcounted get/put for css_set objects
478 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700479static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700480{
Tejun Heo5abb8852013-06-12 21:04:49 -0700481 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700482}
483
Tejun Heob326f9d2013-06-24 15:21:48 -0700484/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700485 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700486 * @cset: candidate css_set being tested
487 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700488 * @new_cgrp: cgroup that's being entered by the task
489 * @template: desired set of css pointers in css_set (pre-calculated)
490 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800491 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700492 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
493 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700494static bool compare_css_sets(struct css_set *cset,
495 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700496 struct cgroup *new_cgrp,
497 struct cgroup_subsys_state *template[])
498{
499 struct list_head *l1, *l2;
500
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400501 /*
502 * On the default hierarchy, there can be csets which are
503 * associated with the same set of cgroups but different csses.
504 * Let's first ensure that csses match.
505 */
506 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700507 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700508
509 /*
510 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400511 * different cgroups in hierarchies. As different cgroups may
512 * share the same effective css, this comparison is always
513 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700514 */
Tejun Heo69d02062013-06-12 21:04:50 -0700515 l1 = &cset->cgrp_links;
516 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700517 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700518 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700519 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700520
521 l1 = l1->next;
522 l2 = l2->next;
523 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700524 if (l1 == &cset->cgrp_links) {
525 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700526 break;
527 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700528 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700529 }
530 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700531 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
532 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
533 cgrp1 = link1->cgrp;
534 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700535 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700536 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700537
538 /*
539 * If this hierarchy is the hierarchy of the cgroup
540 * that's changing, then we need to check that this
541 * css_set points to the new cgroup; if it's any other
542 * hierarchy, then this css_set should point to the
543 * same cgroup as the old css_set.
544 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700545 if (cgrp1->root == new_cgrp->root) {
546 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700547 return false;
548 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700549 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700550 return false;
551 }
552 }
553 return true;
554}
555
Tejun Heob326f9d2013-06-24 15:21:48 -0700556/**
557 * find_existing_css_set - init css array and find the matching css_set
558 * @old_cset: the css_set that we're using before the cgroup transition
559 * @cgrp: the cgroup that we're moving into
560 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700561 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700562static struct css_set *find_existing_css_set(struct css_set *old_cset,
563 struct cgroup *cgrp,
564 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700565{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400566 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700567 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700568 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800569 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700570 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700571
Ben Blumaae8aab2010-03-10 15:22:07 -0800572 /*
573 * Build the set of subsystem state objects that we want to see in the
574 * new css_set. while subsystems can change globally, the entries here
575 * won't change, so no need for locking.
576 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700577 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400578 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400579 /*
580 * @ss is in this hierarchy, so we want the
581 * effective css from @cgrp.
582 */
583 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700584 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400585 /*
586 * @ss is not in this hierarchy, so we don't want
587 * to change the css.
588 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700589 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700590 }
591 }
592
Li Zefan0ac801f2013-01-10 11:49:27 +0800593 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700594 hash_for_each_possible(css_set_table, cset, hlist, key) {
595 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700596 continue;
597
598 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700599 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700600 }
Paul Menage817929e2007-10-18 23:39:36 -0700601
602 /* No existing cgroup group matched */
603 return NULL;
604}
605
Tejun Heo69d02062013-06-12 21:04:50 -0700606static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700607{
Tejun Heo69d02062013-06-12 21:04:50 -0700608 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700609
Tejun Heo69d02062013-06-12 21:04:50 -0700610 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
611 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700612 kfree(link);
613 }
614}
615
Tejun Heo69d02062013-06-12 21:04:50 -0700616/**
617 * allocate_cgrp_cset_links - allocate cgrp_cset_links
618 * @count: the number of links to allocate
619 * @tmp_links: list_head the allocated links are put on
620 *
621 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
622 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700623 */
Tejun Heo69d02062013-06-12 21:04:50 -0700624static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700625{
Tejun Heo69d02062013-06-12 21:04:50 -0700626 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700627 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700628
629 INIT_LIST_HEAD(tmp_links);
630
Li Zefan36553432008-07-29 22:33:19 -0700631 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700632 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700633 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700634 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700635 return -ENOMEM;
636 }
Tejun Heo69d02062013-06-12 21:04:50 -0700637 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700638 }
639 return 0;
640}
641
Li Zefanc12f65d2009-01-07 18:07:42 -0800642/**
643 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700644 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700645 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800646 * @cgrp: the destination cgroup
647 */
Tejun Heo69d02062013-06-12 21:04:50 -0700648static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
649 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800650{
Tejun Heo69d02062013-06-12 21:04:50 -0700651 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800652
Tejun Heo69d02062013-06-12 21:04:50 -0700653 BUG_ON(list_empty(tmp_links));
654 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
655 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700656 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700657 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700658 /*
659 * Always add links to the tail of the list so that the list
660 * is sorted by order of hierarchy creation
661 */
Tejun Heo69d02062013-06-12 21:04:50 -0700662 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800663}
664
Tejun Heob326f9d2013-06-24 15:21:48 -0700665/**
666 * find_css_set - return a new css_set with one cgroup updated
667 * @old_cset: the baseline css_set
668 * @cgrp: the cgroup to be updated
669 *
670 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
671 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700672 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700673static struct css_set *find_css_set(struct css_set *old_cset,
674 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700675{
Tejun Heob326f9d2013-06-24 15:21:48 -0700676 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700677 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700678 struct list_head tmp_links;
679 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400680 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800681 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400682 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700683
Tejun Heob326f9d2013-06-24 15:21:48 -0700684 lockdep_assert_held(&cgroup_mutex);
685
Paul Menage817929e2007-10-18 23:39:36 -0700686 /* First see if we already have a cgroup group that matches
687 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500688 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700689 cset = find_existing_css_set(old_cset, cgrp, template);
690 if (cset)
691 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500692 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700693
Tejun Heo5abb8852013-06-12 21:04:49 -0700694 if (cset)
695 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700696
Tejun Heof4f4be22013-06-12 21:04:51 -0700697 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700698 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700699 return NULL;
700
Tejun Heo69d02062013-06-12 21:04:50 -0700701 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700702 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700703 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700704 return NULL;
705 }
706
Tejun Heo5abb8852013-06-12 21:04:49 -0700707 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700708 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700709 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500710 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500711 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500712 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700713 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700714
715 /* Copy the set of subsystem state objects generated in
716 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700717 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700718
Tejun Heo96d365e2014-02-13 06:58:40 -0500719 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700720 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700721 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700722 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700723
Paul Menage7717f7b2009-09-23 15:56:22 -0700724 if (c->root == cgrp->root)
725 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700726 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700727 }
Paul Menage817929e2007-10-18 23:39:36 -0700728
Tejun Heo69d02062013-06-12 21:04:50 -0700729 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700730
Paul Menage817929e2007-10-18 23:39:36 -0700731 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700732
Tejun Heo2d8f2432014-04-23 11:13:15 -0400733 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700734 key = css_set_hash(cset->subsys);
735 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700736
Tejun Heo2d8f2432014-04-23 11:13:15 -0400737 for_each_subsys(ss, ssid)
738 list_add_tail(&cset->e_cset_node[ssid],
739 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
740
Tejun Heo96d365e2014-02-13 06:58:40 -0500741 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700742
Tejun Heo5abb8852013-06-12 21:04:49 -0700743 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700744}
745
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400746static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700747{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400748 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500749
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400750 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500751}
752
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400753static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500754{
755 int id;
756
757 lockdep_assert_held(&cgroup_mutex);
758
Tejun Heo985ed672014-03-19 10:23:53 -0400759 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500760 if (id < 0)
761 return id;
762
763 root->hierarchy_id = id;
764 return 0;
765}
766
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400767static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500768{
769 lockdep_assert_held(&cgroup_mutex);
770
771 if (root->hierarchy_id) {
772 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
773 root->hierarchy_id = 0;
774 }
775}
776
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400777static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500778{
779 if (root) {
780 /* hierarhcy ID shoulid already have been released */
781 WARN_ON_ONCE(root->hierarchy_id);
782
783 idr_destroy(&root->cgroup_idr);
784 kfree(root);
785 }
786}
787
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400788static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500789{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400790 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500791 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500792
Tejun Heo2bd59d42014-02-11 11:52:49 -0500793 mutex_lock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500794 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500795
Tejun Heo776f02f2014-02-12 09:29:50 -0500796 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500797 BUG_ON(!list_empty(&cgrp->children));
798
Tejun Heof2e85d52014-02-11 11:52:49 -0500799 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400800 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500801
802 /*
803 * Release all the links from cset_links to this hierarchy's
804 * root cgroup
805 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500806 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500807
808 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
809 list_del(&link->cset_link);
810 list_del(&link->cgrp_link);
811 kfree(link);
812 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500813 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500814
815 if (!list_empty(&root->root_list)) {
816 list_del(&root->root_list);
817 cgroup_root_count--;
818 }
819
820 cgroup_exit_root_id(root);
821
822 mutex_unlock(&cgroup_mutex);
823 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500824
Tejun Heo2bd59d42014-02-11 11:52:49 -0500825 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500826 cgroup_free_root(root);
827}
828
Tejun Heoceb6a082014-02-25 10:04:02 -0500829/* look up cgroup associated with given css_set on the specified hierarchy */
830static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400831 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700832{
Paul Menage7717f7b2009-09-23 15:56:22 -0700833 struct cgroup *res = NULL;
834
Tejun Heo96d365e2014-02-13 06:58:40 -0500835 lockdep_assert_held(&cgroup_mutex);
836 lockdep_assert_held(&css_set_rwsem);
837
Tejun Heo5abb8852013-06-12 21:04:49 -0700838 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400839 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700840 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700841 struct cgrp_cset_link *link;
842
843 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700844 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700845
Paul Menage7717f7b2009-09-23 15:56:22 -0700846 if (c->root == root) {
847 res = c;
848 break;
849 }
850 }
851 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500852
Paul Menage7717f7b2009-09-23 15:56:22 -0700853 BUG_ON(!res);
854 return res;
855}
856
857/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500858 * Return the cgroup for "task" from the given hierarchy. Must be
859 * called with cgroup_mutex and css_set_rwsem held.
860 */
861static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400862 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500863{
864 /*
865 * No need to lock the task - since we hold cgroup_mutex the
866 * task can't change groups, so the only thing that can happen
867 * is that it exits and its css is set back to init_css_set.
868 */
869 return cset_cgroup_from_root(task_css_set(task), root);
870}
871
872/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700873 * A task must hold cgroup_mutex to modify cgroups.
874 *
875 * Any task can increment and decrement the count field without lock.
876 * So in general, code holding cgroup_mutex can't rely on the count
877 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800878 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700879 * means that no tasks are currently attached, therefore there is no
880 * way a task attached to that cgroup can fork (the other way to
881 * increment the count). So code holding cgroup_mutex can safely
882 * assume that if the count is zero, it will stay zero. Similarly, if
883 * a task holds cgroup_mutex on a cgroup with zero count, it
884 * knows that the cgroup won't be removed, as cgroup_rmdir()
885 * needs that mutex.
886 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700887 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
888 * (usually) take cgroup_mutex. These are the two most performance
889 * critical pieces of code here. The exception occurs on cgroup_exit(),
890 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
891 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800892 * to the release agent with the name of the cgroup (path relative to
893 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700894 *
895 * A cgroup can only be deleted if both its 'count' of using tasks
896 * is zero, and its list of 'children' cgroups is empty. Since all
897 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400898 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700899 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400900 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700901 *
902 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800903 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700904 */
905
Tejun Heo628f7cd2013-06-28 16:24:11 -0700906static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500907static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700908static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700909
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500910static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
911 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700912{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500913 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
914 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
915 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
916 cft->ss->name, cft->name);
917 else
918 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
919 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700920}
921
Tejun Heof2e85d52014-02-11 11:52:49 -0500922/**
923 * cgroup_file_mode - deduce file mode of a control file
924 * @cft: the control file in question
925 *
926 * returns cft->mode if ->mode is not 0
927 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
928 * returns S_IRUGO if it has only a read handler
929 * returns S_IWUSR if it has only a write hander
930 */
931static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +0800932{
Tejun Heof2e85d52014-02-11 11:52:49 -0500933 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +0800934
Tejun Heof2e85d52014-02-11 11:52:49 -0500935 if (cft->mode)
936 return cft->mode;
937
938 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
939 mode |= S_IRUGO;
940
941 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
942 cft->trigger)
943 mode |= S_IWUSR;
944
945 return mode;
Li Zefan65dff752013-03-01 15:01:56 +0800946}
947
Li Zefanbe445622013-01-24 14:31:42 +0800948static void cgroup_free_fn(struct work_struct *work)
949{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700950 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800951
Tejun Heo3c9c8252014-02-12 09:29:50 -0500952 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -0500953 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800954
Tejun Heo776f02f2014-02-12 09:29:50 -0500955 if (cgrp->parent) {
956 /*
957 * We get a ref to the parent, and put the ref when this
958 * cgroup is being freed, so it's guaranteed that the
959 * parent won't be destroyed before its children.
960 */
961 cgroup_put(cgrp->parent);
962 kernfs_put(cgrp->kn);
963 kfree(cgrp);
964 } else {
965 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400966 * This is root cgroup's refcnt reaching zero, which
Tejun Heo776f02f2014-02-12 09:29:50 -0500967 * indicates that the root should be released.
968 */
969 cgroup_destroy_root(cgrp->root);
970 }
Li Zefanbe445622013-01-24 14:31:42 +0800971}
972
973static void cgroup_free_rcu(struct rcu_head *head)
974{
975 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
976
Tejun Heoea15f8c2013-06-13 19:27:42 -0700977 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500978 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800979}
980
Tejun Heo59f52962014-02-11 11:52:49 -0500981static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700982{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500983 WARN_ON_ONCE(cgroup_is_dead(cgrp));
984 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
985 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986}
987
Tejun Heo59f52962014-02-11 11:52:49 -0500988static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700989{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500990 if (!atomic_dec_and_test(&cgrp->refcnt))
991 return;
Tejun Heo776f02f2014-02-12 09:29:50 -0500992 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -0500993 return;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994
Tejun Heo2bd59d42014-02-11 11:52:49 -0500995 /*
996 * XXX: cgrp->id is only used to look up css's. As cgroup and
997 * css's lifetimes will be decoupled, it should be made
998 * per-subsystem and moved to css->id so that lookups are
999 * successful until the target css is released.
1000 */
1001 mutex_lock(&cgroup_mutex);
1002 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
1003 mutex_unlock(&cgroup_mutex);
1004 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001005
Tejun Heo2bd59d42014-02-11 11:52:49 -05001006 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007}
1008
Li Zefan2739d3c2013-01-21 18:18:33 +08001009static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001011 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012
Tejun Heoace2bee2014-02-11 11:52:47 -05001013 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001014 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001015}
1016
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001017/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001018 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001019 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001020 * @subsys_mask: mask of the subsystem ids whose files should be removed
1021 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07001022static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001023{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001024 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001025 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001026
Tejun Heob420ba72013-07-12 12:34:02 -07001027 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001028 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001029
1030 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001031 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001032 list_for_each_entry(cfts, &ss->cfts, node)
1033 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001034 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035}
1036
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001037static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo5df36032014-03-19 10:23:54 -04001038 unsigned long ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039{
Tejun Heo30159ec2013-06-25 11:53:37 -07001040 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001041 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001042
Tejun Heoace2bee2014-02-11 11:52:47 -05001043 lockdep_assert_held(&cgroup_tree_mutex);
1044 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001045
Tejun Heo5df36032014-03-19 10:23:54 -04001046 for_each_subsys(ss, ssid) {
1047 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001049
Tejun Heo5df36032014-03-19 10:23:54 -04001050 /* if @ss is on the dummy_root, we can always move it */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001051 if (ss->root == &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001052 continue;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001053
Tejun Heo5df36032014-03-19 10:23:54 -04001054 /* if @ss has non-root cgroups attached to it, can't move */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001055 if (!list_empty(&ss->root->cgrp.children))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001056 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057
Tejun Heo5df36032014-03-19 10:23:54 -04001058 /* can't move between two non-dummy roots either */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001059 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001060 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 }
1062
Tejun Heoa2dd4242014-03-19 10:23:55 -04001063 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1064 if (ret) {
1065 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001066 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067
Tejun Heoa2dd4242014-03-19 10:23:55 -04001068 /*
1069 * Rebinding back to the default root is not allowed to
1070 * fail. Using both default and non-default roots should
1071 * be rare. Moving subsystems back and forth even more so.
1072 * Just warn about it and continue.
1073 */
1074 if (cgrp_dfl_root_visible) {
1075 pr_warning("cgroup: failed to create files (%d) while rebinding 0x%lx to default root\n",
1076 ret, ss_mask);
1077 pr_warning("cgroup: you may retry by moving them to a different hierarchy and unbinding\n");
1078 }
Tejun Heo5df36032014-03-19 10:23:54 -04001079 }
Tejun Heo31261212013-06-28 17:07:30 -07001080
1081 /*
1082 * Nothing can fail from this point on. Remove files for the
1083 * removed subsystems and rebind each subsystem.
1084 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001085 mutex_unlock(&cgroup_mutex);
Tejun Heo5df36032014-03-19 10:23:54 -04001086 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001087 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001088 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo4ac06012014-02-11 11:52:47 -05001089 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001090
Tejun Heo5df36032014-03-19 10:23:54 -04001091 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001092 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001093 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001094 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001095
Tejun Heo5df36032014-03-19 10:23:54 -04001096 if (!(ss_mask & (1 << ssid)))
1097 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001098
Tejun Heo5df36032014-03-19 10:23:54 -04001099 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001100 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001101
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001102 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001103
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001104 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1105 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001106 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001107 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001108
Tejun Heo2d8f2432014-04-23 11:13:15 -04001109 down_write(&css_set_rwsem);
1110 hash_for_each(css_set_table, i, cset, hlist)
1111 list_move_tail(&cset->e_cset_node[ss->id],
1112 &dst_root->cgrp.e_csets[ss->id]);
1113 up_write(&css_set_rwsem);
1114
Tejun Heof392e512014-04-23 11:13:14 -04001115 src_root->subsys_mask &= ~(1 << ssid);
1116 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1117
Tejun Heobd53d612014-04-23 11:13:16 -04001118 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001119 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001120 if (dst_root != &cgrp_dfl_root)
1121 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001122
Tejun Heo5df36032014-03-19 10:23:54 -04001123 if (ss->bind)
1124 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001126
Tejun Heoa2dd4242014-03-19 10:23:55 -04001127 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128 return 0;
1129}
1130
Tejun Heo2bd59d42014-02-11 11:52:49 -05001131static int cgroup_show_options(struct seq_file *seq,
1132 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001134 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001135 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001136 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137
Tejun Heob85d2042013-12-06 15:11:57 -05001138 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001139 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001140 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001141 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1142 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001143 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001144 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001145 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001146 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001147
1148 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001149 if (strlen(root->release_agent_path))
1150 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001151 spin_unlock(&release_agent_path_lock);
1152
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001153 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001154 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001155 if (strlen(root->name))
1156 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001157 return 0;
1158}
1159
1160struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001161 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001162 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001163 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001164 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001165 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001166 /* User explicitly requested empty subsystem */
1167 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001168};
1169
Ben Blumaae8aab2010-03-10 15:22:07 -08001170/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001171 * Convert a hierarchy specifier into a bitmask of subsystems and
1172 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1173 * array. This function takes refcounts on subsystems to be used, unless it
1174 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001175 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001176static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001177{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001178 char *token, *o = data;
1179 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001180 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001181 struct cgroup_subsys *ss;
1182 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001183
Ben Blumaae8aab2010-03-10 15:22:07 -08001184 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1185
Li Zefanf9ab5b52009-06-17 16:26:33 -07001186#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001187 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001188#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001189
Paul Menagec6d57f32009-09-23 15:56:19 -07001190 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001191
1192 while ((token = strsep(&o, ",")) != NULL) {
1193 if (!*token)
1194 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001195 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001196 /* Explicitly have no subsystems */
1197 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001198 continue;
1199 }
1200 if (!strcmp(token, "all")) {
1201 /* Mutually exclusive option 'all' + subsystem name */
1202 if (one_ss)
1203 return -EINVAL;
1204 all_ss = true;
1205 continue;
1206 }
Tejun Heo873fe092013-04-14 20:15:26 -07001207 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1208 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1209 continue;
1210 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001211 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001212 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001213 continue;
1214 }
1215 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001216 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001217 continue;
1218 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001219 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001220 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001221 continue;
1222 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001223 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001224 /* Specifying two release agents is forbidden */
1225 if (opts->release_agent)
1226 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001227 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001228 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001229 if (!opts->release_agent)
1230 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001231 continue;
1232 }
1233 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001234 const char *name = token + 5;
1235 /* Can't specify an empty name */
1236 if (!strlen(name))
1237 return -EINVAL;
1238 /* Must match [\w.-]+ */
1239 for (i = 0; i < strlen(name); i++) {
1240 char c = name[i];
1241 if (isalnum(c))
1242 continue;
1243 if ((c == '.') || (c == '-') || (c == '_'))
1244 continue;
1245 return -EINVAL;
1246 }
1247 /* Specifying two names is forbidden */
1248 if (opts->name)
1249 return -EINVAL;
1250 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001251 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001252 GFP_KERNEL);
1253 if (!opts->name)
1254 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001255
1256 continue;
1257 }
1258
Tejun Heo30159ec2013-06-25 11:53:37 -07001259 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001260 if (strcmp(token, ss->name))
1261 continue;
1262 if (ss->disabled)
1263 continue;
1264
1265 /* Mutually exclusive option 'all' + subsystem name */
1266 if (all_ss)
1267 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001268 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001269 one_ss = true;
1270
1271 break;
1272 }
1273 if (i == CGROUP_SUBSYS_COUNT)
1274 return -ENOENT;
1275 }
1276
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001277 /* Consistency checks */
1278
Tejun Heo873fe092013-04-14 20:15:26 -07001279 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1280 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1281
Tejun Heod3ba07c2014-02-13 06:58:38 -05001282 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1283 opts->cpuset_clone_children || opts->release_agent ||
1284 opts->name) {
1285 pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001286 return -EINVAL;
1287 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001288 } else {
1289 /*
1290 * If the 'all' option was specified select all the
1291 * subsystems, otherwise if 'none', 'name=' and a subsystem
1292 * name options were not specified, let's default to 'all'
1293 */
1294 if (all_ss || (!one_ss && !opts->none && !opts->name))
1295 for_each_subsys(ss, i)
1296 if (!ss->disabled)
1297 set_bit(i, &opts->subsys_mask);
Tejun Heo873fe092013-04-14 20:15:26 -07001298
Tejun Heoa2dd4242014-03-19 10:23:55 -04001299 /*
1300 * We either have to specify by name or by subsystems. (So
1301 * all empty hierarchies must have a name).
1302 */
1303 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001304 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001305 }
1306
Li Zefanf9ab5b52009-06-17 16:26:33 -07001307 /*
1308 * Option noprefix was introduced just for backward compatibility
1309 * with the old cpuset, so we allow noprefix only if mounting just
1310 * the cpuset subsystem.
1311 */
Tejun Heo93438622013-04-14 20:15:25 -07001312 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001313 return -EINVAL;
1314
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001315
1316 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001317 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001318 return -EINVAL;
1319
Paul Menageddbcc7e2007-10-18 23:39:30 -07001320 return 0;
1321}
1322
Tejun Heo2bd59d42014-02-11 11:52:49 -05001323static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324{
1325 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001326 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001327 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001328 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001329
Tejun Heo873fe092013-04-14 20:15:26 -07001330 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1331 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1332 return -EINVAL;
1333 }
1334
Tejun Heoace2bee2014-02-11 11:52:47 -05001335 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001336 mutex_lock(&cgroup_mutex);
1337
1338 /* See what subsystems are wanted */
1339 ret = parse_cgroupfs_options(data, &opts);
1340 if (ret)
1341 goto out_unlock;
1342
Tejun Heof392e512014-04-23 11:13:14 -04001343 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001344 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1345 task_tgid_nr(current), current->comm);
1346
Tejun Heof392e512014-04-23 11:13:14 -04001347 added_mask = opts.subsys_mask & ~root->subsys_mask;
1348 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001349
Ben Blumcf5d5942010-03-10 15:22:09 -08001350 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001351 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001352 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001353 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1354 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1355 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001356 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001357 goto out_unlock;
1358 }
1359
Tejun Heof172e672013-06-28 17:07:30 -07001360 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001361 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001362 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001363 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001364 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365
Tejun Heo5df36032014-03-19 10:23:54 -04001366 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001367 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001369
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001370 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001371
Tejun Heo69e943b2014-02-08 10:36:58 -05001372 if (opts.release_agent) {
1373 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001374 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001375 spin_unlock(&release_agent_path_lock);
1376 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001377 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001378 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001379 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001381 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001382 return ret;
1383}
1384
Tejun Heoafeb0f92014-02-13 06:58:39 -05001385/*
1386 * To reduce the fork() overhead for systems that are not actually using
1387 * their cgroups capability, we don't maintain the lists running through
1388 * each css_set to its tasks until we see the list actually used - in other
1389 * words after the first mount.
1390 */
1391static bool use_task_css_set_links __read_mostly;
1392
1393static void cgroup_enable_task_cg_lists(void)
1394{
1395 struct task_struct *p, *g;
1396
Tejun Heo96d365e2014-02-13 06:58:40 -05001397 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001398
1399 if (use_task_css_set_links)
1400 goto out_unlock;
1401
1402 use_task_css_set_links = true;
1403
1404 /*
1405 * We need tasklist_lock because RCU is not safe against
1406 * while_each_thread(). Besides, a forking task that has passed
1407 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1408 * is not guaranteed to have its child immediately visible in the
1409 * tasklist if we walk through it with RCU.
1410 */
1411 read_lock(&tasklist_lock);
1412 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001413 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1414 task_css_set(p) != &init_css_set);
1415
1416 /*
1417 * We should check if the process is exiting, otherwise
1418 * it will race with cgroup_exit() in that the list
1419 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001420 * Do it while holding siglock so that we don't end up
1421 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001422 */
Tejun Heof153ad12014-02-25 09:56:49 -05001423 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001424 if (!(p->flags & PF_EXITING)) {
1425 struct css_set *cset = task_css_set(p);
1426
1427 list_add(&p->cg_list, &cset->tasks);
1428 get_css_set(cset);
1429 }
Tejun Heof153ad12014-02-25 09:56:49 -05001430 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001431 } while_each_thread(g, p);
1432 read_unlock(&tasklist_lock);
1433out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001434 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001435}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001436
Paul Menagecc31edc2008-10-18 20:28:04 -07001437static void init_cgroup_housekeeping(struct cgroup *cgrp)
1438{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001439 struct cgroup_subsys *ss;
1440 int ssid;
1441
Tejun Heo2bd59d42014-02-11 11:52:49 -05001442 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001443 INIT_LIST_HEAD(&cgrp->sibling);
1444 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001445 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001446 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001447 INIT_LIST_HEAD(&cgrp->pidlists);
1448 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001449 cgrp->dummy_css.cgroup = cgrp;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001450
1451 for_each_subsys(ss, ssid)
1452 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Paul Menagecc31edc2008-10-18 20:28:04 -07001453}
Paul Menagec6d57f32009-09-23 15:56:19 -07001454
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001455static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001456 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001457{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001458 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001459
Paul Menageddbcc7e2007-10-18 23:39:30 -07001460 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001461 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001462 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001463 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee82013-07-31 09:50:50 +08001464 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001465
Paul Menagec6d57f32009-09-23 15:56:19 -07001466 root->flags = opts->flags;
1467 if (opts->release_agent)
1468 strcpy(root->release_agent_path, opts->release_agent);
1469 if (opts->name)
1470 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001471 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001472 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001473}
1474
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001475static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001476{
Tejun Heod427dfe2014-02-11 11:52:48 -05001477 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001478 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001479 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001480 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001481
Tejun Heod427dfe2014-02-11 11:52:48 -05001482 lockdep_assert_held(&cgroup_tree_mutex);
1483 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001484
Tejun Heod427dfe2014-02-11 11:52:48 -05001485 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1486 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001487 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001488 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001489
Tejun Heod427dfe2014-02-11 11:52:48 -05001490 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001491 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001492 * but that's OK - it can only be increased by someone holding
1493 * cgroup_lock, and that's us. The worst that can happen is that we
1494 * have some link structures left over
1495 */
1496 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001497 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001498 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001499
Tejun Heo985ed672014-03-19 10:23:53 -04001500 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001501 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001502 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001503
Tejun Heo2bd59d42014-02-11 11:52:49 -05001504 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1505 KERNFS_ROOT_CREATE_DEACTIVATED,
1506 root_cgrp);
1507 if (IS_ERR(root->kf_root)) {
1508 ret = PTR_ERR(root->kf_root);
1509 goto exit_root_id;
1510 }
1511 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512
Tejun Heod427dfe2014-02-11 11:52:48 -05001513 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1514 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001515 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001516
Tejun Heo5df36032014-03-19 10:23:54 -04001517 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001518 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001519 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001520
Tejun Heod427dfe2014-02-11 11:52:48 -05001521 /*
1522 * There must be no failure case after here, since rebinding takes
1523 * care of subsystems' refcounts, which are explicitly dropped in
1524 * the failure exit path.
1525 */
1526 list_add(&root->root_list, &cgroup_roots);
1527 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528
Tejun Heod427dfe2014-02-11 11:52:48 -05001529 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001530 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001531 * objects.
1532 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001533 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001534 hash_for_each(css_set_table, i, cset, hlist)
1535 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001536 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537
Tejun Heod427dfe2014-02-11 11:52:48 -05001538 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001539 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001540
Tejun Heo2bd59d42014-02-11 11:52:49 -05001541 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001542 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001543 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001544
Tejun Heo2bd59d42014-02-11 11:52:49 -05001545destroy_root:
1546 kernfs_destroy_root(root->kf_root);
1547 root->kf_root = NULL;
1548exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001549 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001550out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001551 free_cgrp_cset_links(&tmp_links);
1552 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553}
1554
Al Virof7e83572010-07-26 13:23:11 +04001555static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001557 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001559 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001561 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001562 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001563 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001564
1565 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001566 * The first time anyone tries to mount a cgroup, enable the list
1567 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001568 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001569 if (!use_task_css_set_links)
1570 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001571
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001572 mutex_lock(&cgroup_tree_mutex);
1573 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001578 goto out_unlock;
Li Zefane37a06f2014-04-17 13:53:08 +08001579retry:
Tejun Heo2bd59d42014-02-11 11:52:49 -05001580 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001581 if (!opts.subsys_mask && !opts.none && !opts.name) {
1582 cgrp_dfl_root_visible = true;
1583 root = &cgrp_dfl_root;
1584 cgroup_get(&root->cgrp);
1585 ret = 0;
1586 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001587 }
1588
Tejun Heo985ed672014-03-19 10:23:53 -04001589 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001590 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001591
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001592 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001593 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001594
Paul Menage817929e2007-10-18 23:39:36 -07001595 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001596 * If we asked for a name then it must match. Also, if
1597 * name matches but sybsys_mask doesn't, we should fail.
1598 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001599 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001600 if (opts.name) {
1601 if (strcmp(opts.name, root->name))
1602 continue;
1603 name_match = true;
1604 }
Tejun Heo31261212013-06-28 17:07:30 -07001605
1606 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001607 * If we asked for subsystems (or explicitly for no
1608 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001609 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001610 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001611 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001612 if (!name_match)
1613 continue;
1614 ret = -EBUSY;
1615 goto out_unlock;
1616 }
Tejun Heo873fe092013-04-14 20:15:26 -07001617
Tejun Heoc7ba8282013-06-29 14:06:10 -07001618 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001619 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1620 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1621 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001622 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001623 } else {
1624 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1625 }
Tejun Heo873fe092013-04-14 20:15:26 -07001626 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001627
Tejun Heo776f02f2014-02-12 09:29:50 -05001628 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001629 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001630 * ref indicate that the root is being destroyed. Wait for
1631 * destruction to complete so that the subsystems are free.
1632 * We can use wait_queue for the wait but this path is
1633 * super cold. Let's just sleep for a bit and retry.
1634 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001635 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001636 mutex_unlock(&cgroup_mutex);
1637 mutex_unlock(&cgroup_tree_mutex);
1638 msleep(10);
Li Zefane37a06f2014-04-17 13:53:08 +08001639 mutex_lock(&cgroup_tree_mutex);
1640 mutex_lock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001641 goto retry;
1642 }
1643
1644 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001645 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001646 }
1647
Tejun Heo172a2c062014-03-19 10:23:53 -04001648 /*
1649 * No such thing, create a new one. name= matching without subsys
1650 * specification is allowed for already existing hierarchies but we
1651 * can't create new one without subsys specification.
1652 */
1653 if (!opts.subsys_mask && !opts.none) {
1654 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001655 goto out_unlock;
1656 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657
Tejun Heo172a2c062014-03-19 10:23:53 -04001658 root = kzalloc(sizeof(*root), GFP_KERNEL);
1659 if (!root) {
1660 ret = -ENOMEM;
1661 goto out_unlock;
1662 }
1663
1664 init_cgroup_root(root, &opts);
1665
Tejun Heo35585572014-02-13 06:58:38 -05001666 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001667 if (ret)
1668 cgroup_free_root(root);
1669
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001670out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001671 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001672 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001673
Paul Menagec6d57f32009-09-23 15:56:19 -07001674 kfree(opts.release_agent);
1675 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001676
Tejun Heo2bd59d42014-02-11 11:52:49 -05001677 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001678 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001679
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001680 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1681 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001682 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001683 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684}
1685
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001686static void cgroup_kill_sb(struct super_block *sb)
1687{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001688 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001689 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001690
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001691 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001692 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693}
1694
1695static struct file_system_type cgroup_fs_type = {
1696 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001697 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698 .kill_sb = cgroup_kill_sb,
1699};
1700
Greg KH676db4a2010-08-05 13:53:35 -07001701static struct kobject *cgroup_kobj;
1702
Li Zefana043e3b2008-02-23 15:24:09 -08001703/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001704 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001705 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001706 * @buf: the buffer to write the path into
1707 * @buflen: the length of the buffer
1708 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001709 * Determine @task's cgroup on the first (the one with the lowest non-zero
1710 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1711 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1712 * cgroup controller callbacks.
1713 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001714 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001715 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001716char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001717{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001718 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001719 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001720 int hierarchy_id = 1;
1721 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001722
1723 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001724 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001725
Tejun Heo913ffdb2013-07-11 16:34:48 -07001726 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1727
Tejun Heo857a2be2013-04-14 20:50:08 -07001728 if (root) {
1729 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001730 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001731 } else {
1732 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001733 if (strlcpy(buf, "/", buflen) < buflen)
1734 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001735 }
1736
Tejun Heo96d365e2014-02-13 06:58:40 -05001737 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001738 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001739 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001740}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001741EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001742
Tejun Heob3dc0942014-02-25 10:04:01 -05001743/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001744struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001745 /* the src and dst cset list running through cset->mg_node */
1746 struct list_head src_csets;
1747 struct list_head dst_csets;
1748
1749 /*
1750 * Fields for cgroup_taskset_*() iteration.
1751 *
1752 * Before migration is committed, the target migration tasks are on
1753 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1754 * the csets on ->dst_csets. ->csets point to either ->src_csets
1755 * or ->dst_csets depending on whether migration is committed.
1756 *
1757 * ->cur_csets and ->cur_task point to the current task position
1758 * during iteration.
1759 */
1760 struct list_head *csets;
1761 struct css_set *cur_cset;
1762 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001763};
1764
1765/**
1766 * cgroup_taskset_first - reset taskset and return the first task
1767 * @tset: taskset of interest
1768 *
1769 * @tset iteration is initialized and the first task is returned.
1770 */
1771struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1772{
Tejun Heob3dc0942014-02-25 10:04:01 -05001773 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1774 tset->cur_task = NULL;
1775
1776 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001777}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001778
1779/**
1780 * cgroup_taskset_next - iterate to the next task in taskset
1781 * @tset: taskset of interest
1782 *
1783 * Return the next task in @tset. Iteration must have been initialized
1784 * with cgroup_taskset_first().
1785 */
1786struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1787{
Tejun Heob3dc0942014-02-25 10:04:01 -05001788 struct css_set *cset = tset->cur_cset;
1789 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001790
Tejun Heob3dc0942014-02-25 10:04:01 -05001791 while (&cset->mg_node != tset->csets) {
1792 if (!task)
1793 task = list_first_entry(&cset->mg_tasks,
1794 struct task_struct, cg_list);
1795 else
1796 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001797
Tejun Heob3dc0942014-02-25 10:04:01 -05001798 if (&task->cg_list != &cset->mg_tasks) {
1799 tset->cur_cset = cset;
1800 tset->cur_task = task;
1801 return task;
1802 }
1803
1804 cset = list_next_entry(cset, mg_node);
1805 task = NULL;
1806 }
1807
1808 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001809}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001810
1811/**
Ben Blum74a11662011-05-26 16:25:20 -07001812 * cgroup_task_migrate - move a task from one cgroup to another.
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001813 * @old_cgrp; the cgroup @tsk is being migrated from
1814 * @tsk: the task being migrated
1815 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001816 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001817 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001818 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001819static void cgroup_task_migrate(struct cgroup *old_cgrp,
1820 struct task_struct *tsk,
1821 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001822{
Tejun Heo5abb8852013-06-12 21:04:49 -07001823 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001824
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001825 lockdep_assert_held(&cgroup_mutex);
1826 lockdep_assert_held(&css_set_rwsem);
1827
Ben Blum74a11662011-05-26 16:25:20 -07001828 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001829 * We are synchronized through threadgroup_lock() against PF_EXITING
1830 * setting such that we can't race against cgroup_exit() changing the
1831 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001832 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001833 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001834 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001835
Tejun Heob3dc0942014-02-25 10:04:01 -05001836 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001837 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001838
Tejun Heo1b9aba42014-03-19 17:43:21 -04001839 /*
1840 * Use move_tail so that cgroup_taskset_first() still returns the
1841 * leader after migration. This works because cgroup_migrate()
1842 * ensures that the dst_cset of the leader is the first on the
1843 * tset's dst_csets list.
1844 */
1845 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001846
1847 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001848 * We just gained a reference on old_cset by taking it from the
1849 * task. As trading it for new_cset is protected by cgroup_mutex,
1850 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001851 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001852 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001853 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001854}
1855
Li Zefana043e3b2008-02-23 15:24:09 -08001856/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001857 * cgroup_migrate_finish - cleanup after attach
1858 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001859 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001860 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1861 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001862 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001863static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001864{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001865 struct css_set *cset, *tmp_cset;
1866
1867 lockdep_assert_held(&cgroup_mutex);
1868
1869 down_write(&css_set_rwsem);
1870 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1871 cset->mg_src_cgrp = NULL;
1872 cset->mg_dst_cset = NULL;
1873 list_del_init(&cset->mg_preload_node);
1874 put_css_set_locked(cset, false);
1875 }
1876 up_write(&css_set_rwsem);
1877}
1878
1879/**
1880 * cgroup_migrate_add_src - add a migration source css_set
1881 * @src_cset: the source css_set to add
1882 * @dst_cgrp: the destination cgroup
1883 * @preloaded_csets: list of preloaded css_sets
1884 *
1885 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1886 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1887 * up by cgroup_migrate_finish().
1888 *
1889 * This function may be called without holding threadgroup_lock even if the
1890 * target is a process. Threads may be created and destroyed but as long
1891 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1892 * the preloaded css_sets are guaranteed to cover all migrations.
1893 */
1894static void cgroup_migrate_add_src(struct css_set *src_cset,
1895 struct cgroup *dst_cgrp,
1896 struct list_head *preloaded_csets)
1897{
1898 struct cgroup *src_cgrp;
1899
1900 lockdep_assert_held(&cgroup_mutex);
1901 lockdep_assert_held(&css_set_rwsem);
1902
1903 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1904
1905 /* nothing to do if this cset already belongs to the cgroup */
1906 if (src_cgrp == dst_cgrp)
1907 return;
1908
1909 if (!list_empty(&src_cset->mg_preload_node))
1910 return;
1911
1912 WARN_ON(src_cset->mg_src_cgrp);
1913 WARN_ON(!list_empty(&src_cset->mg_tasks));
1914 WARN_ON(!list_empty(&src_cset->mg_node));
1915
1916 src_cset->mg_src_cgrp = src_cgrp;
1917 get_css_set(src_cset);
1918 list_add(&src_cset->mg_preload_node, preloaded_csets);
1919}
1920
1921/**
1922 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
1923 * @dst_cgrp: the destination cgroup
1924 * @preloaded_csets: list of preloaded source css_sets
1925 *
1926 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
1927 * have been preloaded to @preloaded_csets. This function looks up and
1928 * pins all destination css_sets, links each to its source, and put them on
1929 * @preloaded_csets.
1930 *
1931 * This function must be called after cgroup_migrate_add_src() has been
1932 * called on each migration source css_set. After migration is performed
1933 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
1934 * @preloaded_csets.
1935 */
1936static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
1937 struct list_head *preloaded_csets)
1938{
1939 LIST_HEAD(csets);
1940 struct css_set *src_cset;
1941
1942 lockdep_assert_held(&cgroup_mutex);
1943
1944 /* look up the dst cset for each src cset and link it to src */
1945 list_for_each_entry(src_cset, preloaded_csets, mg_preload_node) {
1946 struct css_set *dst_cset;
1947
1948 dst_cset = find_css_set(src_cset, dst_cgrp);
1949 if (!dst_cset)
1950 goto err;
1951
1952 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
1953 src_cset->mg_dst_cset = dst_cset;
1954
1955 if (list_empty(&dst_cset->mg_preload_node))
1956 list_add(&dst_cset->mg_preload_node, &csets);
1957 else
1958 put_css_set(dst_cset, false);
1959 }
1960
1961 list_splice(&csets, preloaded_csets);
1962 return 0;
1963err:
1964 cgroup_migrate_finish(&csets);
1965 return -ENOMEM;
1966}
1967
1968/**
1969 * cgroup_migrate - migrate a process or task to a cgroup
1970 * @cgrp: the destination cgroup
1971 * @leader: the leader of the process or the task to migrate
1972 * @threadgroup: whether @leader points to the whole process or a single task
1973 *
1974 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
1975 * process, the caller must be holding threadgroup_lock of @leader. The
1976 * caller is also responsible for invoking cgroup_migrate_add_src() and
1977 * cgroup_migrate_prepare_dst() on the targets before invoking this
1978 * function and following up with cgroup_migrate_finish().
1979 *
1980 * As long as a controller's ->can_attach() doesn't fail, this function is
1981 * guaranteed to succeed. This means that, excluding ->can_attach()
1982 * failure, when migrating multiple targets, the success or failure can be
1983 * decided for all targets by invoking group_migrate_prepare_dst() before
1984 * actually starting migrating.
1985 */
1986static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
1987 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001988{
Tejun Heob3dc0942014-02-25 10:04:01 -05001989 struct cgroup_taskset tset = {
1990 .src_csets = LIST_HEAD_INIT(tset.src_csets),
1991 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
1992 .csets = &tset.src_csets,
1993 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05001994 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05001995 struct css_set *cset, *tmp_cset;
1996 struct task_struct *task, *tmp_task;
1997 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07001998
1999 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002000 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2001 * already PF_EXITING could be freed from underneath us unless we
2002 * take an rcu_read_lock.
2003 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002004 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002005 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002006 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002007 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002008 /* @task either already exited or can't exit until the end */
2009 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002010 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002011
Tejun Heoeaf797a2014-02-25 10:04:03 -05002012 /* leave @task alone if post_fork() hasn't linked it yet */
2013 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002014 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002015
Tejun Heob3dc0942014-02-25 10:04:01 -05002016 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002017 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002018 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002019
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002020 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002021 * cgroup_taskset_first() must always return the leader.
2022 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002023 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002024 list_move_tail(&task->cg_list, &cset->mg_tasks);
2025 if (list_empty(&cset->mg_node))
2026 list_add_tail(&cset->mg_node, &tset.src_csets);
2027 if (list_empty(&cset->mg_dst_cset->mg_node))
2028 list_move_tail(&cset->mg_dst_cset->mg_node,
2029 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002030 next:
Li Zefan081aa452013-03-13 09:17:09 +08002031 if (!threadgroup)
2032 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002033 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002034 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002035 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002036
Tejun Heo134d3372011-12-12 18:12:21 -08002037 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002038 if (list_empty(&tset.src_csets))
2039 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002040
Tejun Heo1958d2d2014-02-25 10:04:03 -05002041 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002042 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002043 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002044 ret = css->ss->can_attach(css, &tset);
2045 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002046 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002047 goto out_cancel_attach;
2048 }
2049 }
Ben Blum74a11662011-05-26 16:25:20 -07002050 }
2051
2052 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002053 * Now that we're guaranteed success, proceed to move all tasks to
2054 * the new cgroup. There are no failure cases after here, so this
2055 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002056 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002057 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002058 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2059 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2060 cgroup_task_migrate(cset->mg_src_cgrp, task,
2061 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002062 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002063 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002064
2065 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002066 * Migration is committed, all target tasks are now on dst_csets.
2067 * Nothing is sensitive to fork() after this point. Notify
2068 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002069 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002070 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002071
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002072 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002073 if (css->ss->attach)
2074 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002075
Tejun Heo9db8de32014-02-13 06:58:43 -05002076 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002077 goto out_release_tset;
2078
Ben Blum74a11662011-05-26 16:25:20 -07002079out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002080 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002081 if (css == failed_css)
2082 break;
2083 if (css->ss->cancel_attach)
2084 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002085 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002086out_release_tset:
2087 down_write(&css_set_rwsem);
2088 list_splice_init(&tset.dst_csets, &tset.src_csets);
2089 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002090 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002091 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002092 }
2093 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002094 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002095}
2096
Tejun Heo1958d2d2014-02-25 10:04:03 -05002097/**
2098 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2099 * @dst_cgrp: the cgroup to attach to
2100 * @leader: the task or the leader of the threadgroup to be attached
2101 * @threadgroup: attach the whole threadgroup?
2102 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002103 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002104 */
2105static int cgroup_attach_task(struct cgroup *dst_cgrp,
2106 struct task_struct *leader, bool threadgroup)
2107{
2108 LIST_HEAD(preloaded_csets);
2109 struct task_struct *task;
2110 int ret;
2111
2112 /* look up all src csets */
2113 down_read(&css_set_rwsem);
2114 rcu_read_lock();
2115 task = leader;
2116 do {
2117 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2118 &preloaded_csets);
2119 if (!threadgroup)
2120 break;
2121 } while_each_thread(leader, task);
2122 rcu_read_unlock();
2123 up_read(&css_set_rwsem);
2124
2125 /* prepare dst csets and commit */
2126 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2127 if (!ret)
2128 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2129
2130 cgroup_migrate_finish(&preloaded_csets);
2131 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002132}
2133
2134/*
2135 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002136 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002137 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002138 */
2139static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002140{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002141 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002142 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002143 int ret;
2144
Ben Blum74a11662011-05-26 16:25:20 -07002145 if (!cgroup_lock_live_group(cgrp))
2146 return -ENODEV;
2147
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002148retry_find_task:
2149 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002150 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002151 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002152 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002153 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002154 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002155 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002156 }
Ben Blum74a11662011-05-26 16:25:20 -07002157 /*
2158 * even if we're attaching all tasks in the thread group, we
2159 * only need to check permissions on one of them.
2160 */
David Howellsc69e8d92008-11-14 10:39:19 +11002161 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002162 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2163 !uid_eq(cred->euid, tcred->uid) &&
2164 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002165 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002166 ret = -EACCES;
2167 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002168 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002169 } else
2170 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002171
2172 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002173 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002174
2175 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002176 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002177 * trapped in a cpuset, or RT worker may be born in a cgroup
2178 * with no rt_runtime allocated. Just say no.
2179 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002180 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002181 ret = -EINVAL;
2182 rcu_read_unlock();
2183 goto out_unlock_cgroup;
2184 }
2185
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002186 get_task_struct(tsk);
2187 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002188
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002189 threadgroup_lock(tsk);
2190 if (threadgroup) {
2191 if (!thread_group_leader(tsk)) {
2192 /*
2193 * a race with de_thread from another thread's exec()
2194 * may strip us of our leadership, if this happens,
2195 * there is no choice but to throw this task away and
2196 * try again; this is
2197 * "double-double-toil-and-trouble-check locking".
2198 */
2199 threadgroup_unlock(tsk);
2200 put_task_struct(tsk);
2201 goto retry_find_task;
2202 }
Li Zefan081aa452013-03-13 09:17:09 +08002203 }
2204
2205 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2206
Tejun Heocd3d0952011-12-12 18:12:21 -08002207 threadgroup_unlock(tsk);
2208
Paul Menagebbcb81d2007-10-18 23:39:32 -07002209 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002210out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002211 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002212 return ret;
2213}
2214
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002215/**
2216 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2217 * @from: attach to all cgroups of a given task
2218 * @tsk: the task to be attached
2219 */
2220int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2221{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002222 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002223 int retval = 0;
2224
Tejun Heo47cfcd02013-04-07 09:29:51 -07002225 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002226 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002227 struct cgroup *from_cgrp;
2228
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002229 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002230 continue;
2231
Tejun Heo96d365e2014-02-13 06:58:40 -05002232 down_read(&css_set_rwsem);
2233 from_cgrp = task_cgroup_from_root(from, root);
2234 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002235
Li Zefan6f4b7e62013-07-31 16:18:36 +08002236 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002237 if (retval)
2238 break;
2239 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002240 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002241
2242 return retval;
2243}
2244EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2245
Tejun Heo182446d2013-08-08 20:11:24 -04002246static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2247 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002248{
Tejun Heo182446d2013-08-08 20:11:24 -04002249 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002250}
2251
Tejun Heo182446d2013-08-08 20:11:24 -04002252static int cgroup_procs_write(struct cgroup_subsys_state *css,
2253 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002254{
Tejun Heo182446d2013-08-08 20:11:24 -04002255 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002256}
2257
Tejun Heo182446d2013-08-08 20:11:24 -04002258static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
Tejun Heo4d3bb512014-03-19 10:23:54 -04002259 struct cftype *cft, char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002260{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002261 struct cgroup_root *root = css->cgroup->root;
Tejun Heo5f469902014-02-11 11:52:48 -05002262
2263 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002264 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002265 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002266 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002267 strlcpy(root->release_agent_path, buffer,
2268 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002269 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002270 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002271 return 0;
2272}
2273
Tejun Heo2da8ca82013-12-05 12:28:04 -05002274static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002275{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002276 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002277
Paul Menagee788e062008-07-25 01:46:59 -07002278 if (!cgroup_lock_live_group(cgrp))
2279 return -ENODEV;
2280 seq_puts(seq, cgrp->root->release_agent_path);
2281 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002282 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002283 return 0;
2284}
2285
Tejun Heo2da8ca82013-12-05 12:28:04 -05002286static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002287{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002288 struct cgroup *cgrp = seq_css(seq)->cgroup;
2289
2290 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002291 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002292}
2293
Tejun Heo2bd59d42014-02-11 11:52:49 -05002294static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2295 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002296{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002297 struct cgroup *cgrp = of->kn->parent->priv;
2298 struct cftype *cft = of->kn->priv;
2299 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002300 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002301
Tejun Heo2bd59d42014-02-11 11:52:49 -05002302 /*
2303 * kernfs guarantees that a file isn't deleted with operations in
2304 * flight, which means that the matching css is and stays alive and
2305 * doesn't need to be pinned. The RCU locking is not necessary
2306 * either. It's just for the convenience of using cgroup_css().
2307 */
2308 rcu_read_lock();
2309 css = cgroup_css(cgrp, cft->ss);
2310 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002311
Tejun Heoa742c592013-12-05 12:28:03 -05002312 if (cft->write_string) {
2313 ret = cft->write_string(css, cft, strstrip(buf));
2314 } else if (cft->write_u64) {
2315 unsigned long long v;
2316 ret = kstrtoull(buf, 0, &v);
2317 if (!ret)
2318 ret = cft->write_u64(css, cft, v);
2319 } else if (cft->write_s64) {
2320 long long v;
2321 ret = kstrtoll(buf, 0, &v);
2322 if (!ret)
2323 ret = cft->write_s64(css, cft, v);
2324 } else if (cft->trigger) {
2325 ret = cft->trigger(css, (unsigned int)cft->private);
2326 } else {
2327 ret = -EINVAL;
2328 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002329
Tejun Heoa742c592013-12-05 12:28:03 -05002330 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002331}
2332
Tejun Heo6612f052013-12-05 12:28:04 -05002333static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002334{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002335 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002336}
2337
2338static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2339{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002340 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002341}
2342
2343static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2344{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002345 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002346}
2347
2348static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2349{
Tejun Heo7da11272013-12-05 12:28:04 -05002350 struct cftype *cft = seq_cft(m);
2351 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002352
Tejun Heo2da8ca82013-12-05 12:28:04 -05002353 if (cft->seq_show)
2354 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002355
Tejun Heo896f5192013-12-05 12:28:04 -05002356 if (cft->read_u64)
2357 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2358 else if (cft->read_s64)
2359 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2360 else
2361 return -EINVAL;
2362 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002363}
2364
Tejun Heo2bd59d42014-02-11 11:52:49 -05002365static struct kernfs_ops cgroup_kf_single_ops = {
2366 .atomic_write_len = PAGE_SIZE,
2367 .write = cgroup_file_write,
2368 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002369};
2370
Tejun Heo2bd59d42014-02-11 11:52:49 -05002371static struct kernfs_ops cgroup_kf_ops = {
2372 .atomic_write_len = PAGE_SIZE,
2373 .write = cgroup_file_write,
2374 .seq_start = cgroup_seqfile_start,
2375 .seq_next = cgroup_seqfile_next,
2376 .seq_stop = cgroup_seqfile_stop,
2377 .seq_show = cgroup_seqfile_show,
2378};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002379
2380/*
2381 * cgroup_rename - Only allow simple rename of directories in place.
2382 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002383static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2384 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002385{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002386 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002387 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002388
Tejun Heo2bd59d42014-02-11 11:52:49 -05002389 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002391 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002393
Tejun Heo6db8e852013-06-14 11:18:22 -07002394 /*
2395 * This isn't a proper migration and its usefulness is very
2396 * limited. Disallow if sane_behavior.
2397 */
2398 if (cgroup_sane_behavior(cgrp))
2399 return -EPERM;
2400
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002401 /*
2402 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2403 * active_ref. kernfs_rename() doesn't require active_ref
2404 * protection. Break them before grabbing cgroup_tree_mutex.
2405 */
2406 kernfs_break_active_protection(new_parent);
2407 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002408
Tejun Heo2bd59d42014-02-11 11:52:49 -05002409 mutex_lock(&cgroup_tree_mutex);
2410 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002411
Tejun Heo2bd59d42014-02-11 11:52:49 -05002412 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002413
Tejun Heo2bd59d42014-02-11 11:52:49 -05002414 mutex_unlock(&cgroup_mutex);
2415 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002416
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002417 kernfs_unbreak_active_protection(kn);
2418 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002419 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002420}
2421
Tejun Heo49957f82014-04-07 16:44:47 -04002422/* set uid and gid of cgroup dirs and files to that of the creator */
2423static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2424{
2425 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2426 .ia_uid = current_fsuid(),
2427 .ia_gid = current_fsgid(), };
2428
2429 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2430 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2431 return 0;
2432
2433 return kernfs_setattr(kn, &iattr);
2434}
2435
Tejun Heo2bb566c2013-08-08 20:11:23 -04002436static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002437{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002438 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002439 struct kernfs_node *kn;
2440 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002441 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002442
Tejun Heo2bd59d42014-02-11 11:52:49 -05002443#ifdef CONFIG_DEBUG_LOCK_ALLOC
2444 key = &cft->lockdep_key;
2445#endif
2446 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2447 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2448 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002449 if (IS_ERR(kn))
2450 return PTR_ERR(kn);
2451
2452 ret = cgroup_kn_set_ugid(kn);
2453 if (ret)
2454 kernfs_remove(kn);
2455 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002456}
2457
Tejun Heob1f28d32013-06-28 16:24:10 -07002458/**
2459 * cgroup_addrm_files - add or remove files to a cgroup directory
2460 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002461 * @cfts: array of cftypes to be added
2462 * @is_add: whether to add or remove
2463 *
2464 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002465 * For removals, this function never fails. If addition fails, this
2466 * function doesn't remove files already added. The caller is responsible
2467 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002468 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002469static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2470 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002471{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002472 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002473 int ret;
2474
Tejun Heoace2bee2014-02-11 11:52:47 -05002475 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002476
2477 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002478 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002479 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2480 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002481 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2482 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002483 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2484 continue;
2485 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2486 continue;
2487
Li Zefan2739d3c2013-01-21 18:18:33 +08002488 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002489 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002490 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002491 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002492 cft->name, ret);
2493 return ret;
2494 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002495 } else {
2496 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002497 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002498 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002499 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002500}
2501
Tejun Heo21a2d3432014-02-12 09:29:49 -05002502static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002503{
2504 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002505 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002506 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002507 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002508 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002509
Tejun Heo21a2d3432014-02-12 09:29:49 -05002510 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002511
Li Zefane8c82d22013-06-18 18:48:37 +08002512 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002513 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002514 struct cgroup *cgrp = css->cgroup;
2515
Li Zefane8c82d22013-06-18 18:48:37 +08002516 if (cgroup_is_dead(cgrp))
2517 continue;
2518
Tejun Heo21a2d3432014-02-12 09:29:49 -05002519 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002520 if (ret)
2521 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002522 }
Tejun Heo21a2d3432014-02-12 09:29:49 -05002523
2524 if (is_add && !ret)
2525 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002526 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002527}
2528
Tejun Heo2da440a2014-02-11 11:52:48 -05002529static void cgroup_exit_cftypes(struct cftype *cfts)
2530{
2531 struct cftype *cft;
2532
Tejun Heo2bd59d42014-02-11 11:52:49 -05002533 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2534 /* free copy for custom atomic_write_len, see init_cftypes() */
2535 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2536 kfree(cft->kf_ops);
2537 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002538 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002539 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002540}
2541
Tejun Heo2bd59d42014-02-11 11:52:49 -05002542static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002543{
2544 struct cftype *cft;
2545
Tejun Heo2bd59d42014-02-11 11:52:49 -05002546 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2547 struct kernfs_ops *kf_ops;
2548
Tejun Heo0adb0702014-02-12 09:29:48 -05002549 WARN_ON(cft->ss || cft->kf_ops);
2550
Tejun Heo2bd59d42014-02-11 11:52:49 -05002551 if (cft->seq_start)
2552 kf_ops = &cgroup_kf_ops;
2553 else
2554 kf_ops = &cgroup_kf_single_ops;
2555
2556 /*
2557 * Ugh... if @cft wants a custom max_write_len, we need to
2558 * make a copy of kf_ops to set its atomic_write_len.
2559 */
2560 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2561 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2562 if (!kf_ops) {
2563 cgroup_exit_cftypes(cfts);
2564 return -ENOMEM;
2565 }
2566 kf_ops->atomic_write_len = cft->max_write_len;
2567 }
2568
2569 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002570 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002571 }
2572
2573 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002574}
2575
Tejun Heo21a2d3432014-02-12 09:29:49 -05002576static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2577{
2578 lockdep_assert_held(&cgroup_tree_mutex);
2579
2580 if (!cfts || !cfts[0].ss)
2581 return -ENOENT;
2582
2583 list_del(&cfts->node);
2584 cgroup_apply_cftypes(cfts, false);
2585 cgroup_exit_cftypes(cfts);
2586 return 0;
2587}
2588
Tejun Heo8e3f6542012-04-01 12:09:55 -07002589/**
Tejun Heo80b13582014-02-12 09:29:48 -05002590 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2591 * @cfts: zero-length name terminated array of cftypes
2592 *
2593 * Unregister @cfts. Files described by @cfts are removed from all
2594 * existing cgroups and all future cgroups won't have them either. This
2595 * function can be called anytime whether @cfts' subsys is attached or not.
2596 *
2597 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2598 * registered.
2599 */
2600int cgroup_rm_cftypes(struct cftype *cfts)
2601{
Tejun Heo21a2d3432014-02-12 09:29:49 -05002602 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002603
Tejun Heo21a2d3432014-02-12 09:29:49 -05002604 mutex_lock(&cgroup_tree_mutex);
2605 ret = cgroup_rm_cftypes_locked(cfts);
2606 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002607 return ret;
2608}
2609
2610/**
2611 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2612 * @ss: target cgroup subsystem
2613 * @cfts: zero-length name terminated array of cftypes
2614 *
2615 * Register @cfts to @ss. Files described by @cfts are created for all
2616 * existing cgroups to which @ss is attached and all future cgroups will
2617 * have them too. This function can be called anytime whether @ss is
2618 * attached or not.
2619 *
2620 * Returns 0 on successful registration, -errno on failure. Note that this
2621 * function currently returns 0 as long as @cfts registration is successful
2622 * even if some file creation attempts on existing cgroups fail.
2623 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002624int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002625{
Tejun Heo9ccece82013-06-28 16:24:11 -07002626 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002627
Li Zefandc5736e2014-02-17 10:41:50 +08002628 if (!cfts || cfts[0].name[0] == '\0')
2629 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002630
Tejun Heo2bd59d42014-02-11 11:52:49 -05002631 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002632 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05002633 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002634
Tejun Heo21a2d3432014-02-12 09:29:49 -05002635 mutex_lock(&cgroup_tree_mutex);
2636
Tejun Heo0adb0702014-02-12 09:29:48 -05002637 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d3432014-02-12 09:29:49 -05002638 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002639 if (ret)
Tejun Heo21a2d3432014-02-12 09:29:49 -05002640 cgroup_rm_cftypes_locked(cfts);
2641
2642 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002643 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002644}
Tejun Heo79578622012-04-01 12:09:56 -07002645
2646/**
Li Zefana043e3b2008-02-23 15:24:09 -08002647 * cgroup_task_count - count the number of tasks in a cgroup.
2648 * @cgrp: the cgroup in question
2649 *
2650 * Return the number of tasks in the cgroup.
2651 */
Tejun Heo07bc3562014-02-13 06:58:39 -05002652static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002653{
2654 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002655 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002656
Tejun Heo96d365e2014-02-13 06:58:40 -05002657 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07002658 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2659 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05002660 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002661 return count;
2662}
2663
Tejun Heo574bd9f2012-11-09 09:12:29 -08002664/**
Tejun Heo492eb212013-08-08 20:11:25 -04002665 * css_next_child - find the next child of a given css
2666 * @pos_css: the current position (%NULL to initiate traversal)
2667 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002668 *
Tejun Heo492eb212013-08-08 20:11:25 -04002669 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002670 * under either cgroup_mutex or RCU read lock. The only requirement is
2671 * that @parent_css and @pos_css are accessible. The next sibling is
2672 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002673 */
Tejun Heo492eb212013-08-08 20:11:25 -04002674struct cgroup_subsys_state *
2675css_next_child(struct cgroup_subsys_state *pos_css,
2676 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002677{
Tejun Heo492eb212013-08-08 20:11:25 -04002678 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2679 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002680 struct cgroup *next;
2681
Tejun Heoace2bee2014-02-11 11:52:47 -05002682 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002683
2684 /*
2685 * @pos could already have been removed. Once a cgroup is removed,
2686 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002687 * changes. As CGRP_DEAD assertion is serialized and happens
2688 * before the cgroup is taken off the ->sibling list, if we see it
2689 * unasserted, it's guaranteed that the next sibling hasn't
2690 * finished its grace period even if it's already removed, and thus
2691 * safe to dereference from this RCU critical section. If
2692 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2693 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002694 *
2695 * If @pos is dead, its next pointer can't be dereferenced;
2696 * however, as each cgroup is given a monotonically increasing
2697 * unique serial number and always appended to the sibling list,
2698 * the next one can be found by walking the parent's children until
2699 * we see a cgroup with higher serial number than @pos's. While
2700 * this path can be slower, it's taken only when either the current
2701 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002702 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002703 if (!pos) {
2704 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2705 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002706 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002707 } else {
2708 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2709 if (next->serial_nr > pos->serial_nr)
2710 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002711 }
2712
Tejun Heo3b281af2014-04-23 11:13:15 -04002713 /*
2714 * @next, if not pointing to the head, can be dereferenced and is
2715 * the next sibling; however, it might have @ss disabled. If so,
2716 * fast-forward to the next enabled one.
2717 */
2718 while (&next->sibling != &cgrp->children) {
2719 struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
Tejun Heo492eb212013-08-08 20:11:25 -04002720
Tejun Heo3b281af2014-04-23 11:13:15 -04002721 if (next_css)
2722 return next_css;
2723 next = list_entry_rcu(next->sibling.next, struct cgroup, sibling);
2724 }
2725 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09002726}
Tejun Heo53fa5262013-05-24 10:55:38 +09002727
2728/**
Tejun Heo492eb212013-08-08 20:11:25 -04002729 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002730 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002731 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002732 *
Tejun Heo492eb212013-08-08 20:11:25 -04002733 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002734 * to visit for pre-order traversal of @root's descendants. @root is
2735 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002736 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002737 * While this function requires cgroup_mutex or RCU read locking, it
2738 * doesn't require the whole traversal to be contained in a single critical
2739 * section. This function will return the correct next descendant as long
2740 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002741 */
Tejun Heo492eb212013-08-08 20:11:25 -04002742struct cgroup_subsys_state *
2743css_next_descendant_pre(struct cgroup_subsys_state *pos,
2744 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002745{
Tejun Heo492eb212013-08-08 20:11:25 -04002746 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002747
Tejun Heoace2bee2014-02-11 11:52:47 -05002748 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002749
Tejun Heobd8815a2013-08-08 20:11:27 -04002750 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002751 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002752 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002753
2754 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002755 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002756 if (next)
2757 return next;
2758
2759 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002760 while (pos != root) {
2761 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002762 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002763 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002764 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002765 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002766
2767 return NULL;
2768}
Tejun Heo574bd9f2012-11-09 09:12:29 -08002769
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002770/**
Tejun Heo492eb212013-08-08 20:11:25 -04002771 * css_rightmost_descendant - return the rightmost descendant of a css
2772 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002773 *
Tejun Heo492eb212013-08-08 20:11:25 -04002774 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2775 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002776 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002777 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002778 * While this function requires cgroup_mutex or RCU read locking, it
2779 * doesn't require the whole traversal to be contained in a single critical
2780 * section. This function will return the correct rightmost descendant as
2781 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002782 */
Tejun Heo492eb212013-08-08 20:11:25 -04002783struct cgroup_subsys_state *
2784css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002785{
Tejun Heo492eb212013-08-08 20:11:25 -04002786 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002787
Tejun Heoace2bee2014-02-11 11:52:47 -05002788 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002789
2790 do {
2791 last = pos;
2792 /* ->prev isn't RCU safe, walk ->next till the end */
2793 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002794 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002795 pos = tmp;
2796 } while (pos);
2797
2798 return last;
2799}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002800
Tejun Heo492eb212013-08-08 20:11:25 -04002801static struct cgroup_subsys_state *
2802css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002803{
Tejun Heo492eb212013-08-08 20:11:25 -04002804 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002805
2806 do {
2807 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04002808 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002809 } while (pos);
2810
2811 return last;
2812}
2813
2814/**
Tejun Heo492eb212013-08-08 20:11:25 -04002815 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002816 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002817 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002818 *
Tejun Heo492eb212013-08-08 20:11:25 -04002819 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002820 * to visit for post-order traversal of @root's descendants. @root is
2821 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002822 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002823 * While this function requires cgroup_mutex or RCU read locking, it
2824 * doesn't require the whole traversal to be contained in a single critical
2825 * section. This function will return the correct next descendant as long
2826 * as both @pos and @cgroup are accessible and @pos is a descendant of
2827 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002828 */
Tejun Heo492eb212013-08-08 20:11:25 -04002829struct cgroup_subsys_state *
2830css_next_descendant_post(struct cgroup_subsys_state *pos,
2831 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002832{
Tejun Heo492eb212013-08-08 20:11:25 -04002833 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002834
Tejun Heoace2bee2014-02-11 11:52:47 -05002835 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002836
Tejun Heo58b79a92013-09-06 15:31:08 -04002837 /* if first iteration, visit leftmost descendant which may be @root */
2838 if (!pos)
2839 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002840
Tejun Heobd8815a2013-08-08 20:11:27 -04002841 /* if we visited @root, we're done */
2842 if (pos == root)
2843 return NULL;
2844
Tejun Heo574bd9f2012-11-09 09:12:29 -08002845 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04002846 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002847 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04002848 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002849
2850 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04002851 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002852}
Tejun Heo574bd9f2012-11-09 09:12:29 -08002853
Tejun Heo0942eee2013-08-08 20:11:26 -04002854/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002855 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04002856 * @it: the iterator to advance
2857 *
2858 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04002859 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002860static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04002861{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04002862 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04002863 struct cgrp_cset_link *link;
2864 struct css_set *cset;
2865
2866 /* Advance to the next non-empty css_set */
2867 do {
2868 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04002869 if (l == it->cset_head) {
2870 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04002871 return;
2872 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04002873
2874 if (it->ss) {
2875 cset = container_of(l, struct css_set,
2876 e_cset_node[it->ss->id]);
2877 } else {
2878 link = list_entry(l, struct cgrp_cset_link, cset_link);
2879 cset = link->cset;
2880 }
Tejun Heoc7561122014-02-25 10:04:01 -05002881 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
2882
Tejun Heo0f0a2b42014-04-23 11:13:15 -04002883 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05002884
2885 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04002886 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05002887 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04002888 it->task_pos = cset->mg_tasks.next;
2889
2890 it->tasks_head = &cset->tasks;
2891 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04002892}
2893
Tejun Heo0942eee2013-08-08 20:11:26 -04002894/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002895 * css_task_iter_start - initiate task iteration
2896 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04002897 * @it: the task iterator to use
2898 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002899 * Initiate iteration through the tasks of @css. The caller can call
2900 * css_task_iter_next() to walk through the tasks until the function
2901 * returns NULL. On completion of iteration, css_task_iter_end() must be
2902 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04002903 *
2904 * Note that this function acquires a lock which is released when the
2905 * iteration finishes. The caller can't sleep while iteration is in
2906 * progress.
2907 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002908void css_task_iter_start(struct cgroup_subsys_state *css,
2909 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05002910 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07002911{
Tejun Heo56fde9e2014-02-13 06:58:38 -05002912 /* no one should try to iterate before mounting cgroups */
2913 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002914
Tejun Heo96d365e2014-02-13 06:58:40 -05002915 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002916
Tejun Heo3ebb2b62014-04-23 11:13:15 -04002917 it->ss = css->ss;
2918
2919 if (it->ss)
2920 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
2921 else
2922 it->cset_pos = &css->cgroup->cset_links;
2923
Tejun Heo0f0a2b42014-04-23 11:13:15 -04002924 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002925
Tejun Heo72ec7022013-08-08 20:11:26 -04002926 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002927}
2928
Tejun Heo0942eee2013-08-08 20:11:26 -04002929/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002930 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04002931 * @it: the task iterator being iterated
2932 *
2933 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04002934 * initialized via css_task_iter_start(). Returns NULL when the iteration
2935 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04002936 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002937struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002938{
2939 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04002940 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07002941
2942 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04002943 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07002944 return NULL;
2945 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05002946
2947 /*
2948 * Advance iterator to find next entry. cset->tasks is consumed
2949 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
2950 * next cset.
2951 */
Paul Menage817929e2007-10-18 23:39:36 -07002952 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05002953
Tejun Heo0f0a2b42014-04-23 11:13:15 -04002954 if (l == it->tasks_head)
2955 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05002956
Tejun Heo0f0a2b42014-04-23 11:13:15 -04002957 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04002958 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05002959 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04002960 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05002961
Paul Menage817929e2007-10-18 23:39:36 -07002962 return res;
2963}
2964
Tejun Heo0942eee2013-08-08 20:11:26 -04002965/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002966 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04002967 * @it: the task iterator to finish
2968 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002969 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04002970 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002971void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05002972 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07002973{
Tejun Heo96d365e2014-02-13 06:58:40 -05002974 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07002975}
2976
2977/**
2978 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
2979 * @to: cgroup to which the tasks will be moved
2980 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05002981 *
2982 * Locking rules between cgroup_post_fork() and the migration path
2983 * guarantee that, if a task is forking while being migrated, the new child
2984 * is guaranteed to be either visible in the source cgroup after the
2985 * parent's migration is complete or put into the target cgroup. No task
2986 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07002987 */
2988int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
2989{
Tejun Heo952aaa12014-02-25 10:04:03 -05002990 LIST_HEAD(preloaded_csets);
2991 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05002992 struct css_task_iter it;
2993 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05002994 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05002995
Tejun Heo952aaa12014-02-25 10:04:03 -05002996 mutex_lock(&cgroup_mutex);
2997
2998 /* all tasks in @from are being moved, all csets are source */
2999 down_read(&css_set_rwsem);
3000 list_for_each_entry(link, &from->cset_links, cset_link)
3001 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3002 up_read(&css_set_rwsem);
3003
3004 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3005 if (ret)
3006 goto out_err;
3007
3008 /*
3009 * Migrate tasks one-by-one until @form is empty. This fails iff
3010 * ->can_attach() fails.
3011 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003012 do {
3013 css_task_iter_start(&from->dummy_css, &it);
3014 task = css_task_iter_next(&it);
3015 if (task)
3016 get_task_struct(task);
3017 css_task_iter_end(&it);
3018
3019 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003020 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003021 put_task_struct(task);
3022 }
3023 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003024out_err:
3025 cgroup_migrate_finish(&preloaded_csets);
3026 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003027 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003028}
3029
Paul Menage817929e2007-10-18 23:39:36 -07003030/*
Ben Blum102a7752009-09-23 15:56:26 -07003031 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003032 *
3033 * Reading this file can return large amounts of data if a cgroup has
3034 * *lots* of attached tasks. So it may need several calls to read(),
3035 * but we cannot guarantee that the information we produce is correct
3036 * unless we produce it entirely atomically.
3037 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003038 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003039
Li Zefan24528252012-01-20 11:58:43 +08003040/* which pidlist file are we talking about? */
3041enum cgroup_filetype {
3042 CGROUP_FILE_PROCS,
3043 CGROUP_FILE_TASKS,
3044};
3045
3046/*
3047 * A pidlist is a list of pids that virtually represents the contents of one
3048 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3049 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3050 * to the cgroup.
3051 */
3052struct cgroup_pidlist {
3053 /*
3054 * used to find which pidlist is wanted. doesn't change as long as
3055 * this particular list stays in the list.
3056 */
3057 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3058 /* array of xids */
3059 pid_t *list;
3060 /* how many elements the above list has */
3061 int length;
Li Zefan24528252012-01-20 11:58:43 +08003062 /* each of these stored in a list by its cgroup */
3063 struct list_head links;
3064 /* pointer to the cgroup we belong to, for list removal purposes */
3065 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003066 /* for delayed destruction */
3067 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003068};
3069
Paul Menagebbcb81d2007-10-18 23:39:32 -07003070/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003071 * The following two functions "fix" the issue where there are more pids
3072 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3073 * TODO: replace with a kernel-wide solution to this problem
3074 */
3075#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3076static void *pidlist_allocate(int count)
3077{
3078 if (PIDLIST_TOO_LARGE(count))
3079 return vmalloc(count * sizeof(pid_t));
3080 else
3081 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3082}
Tejun Heob1a21362013-11-29 10:42:58 -05003083
Ben Blumd1d9fd32009-09-23 15:56:28 -07003084static void pidlist_free(void *p)
3085{
3086 if (is_vmalloc_addr(p))
3087 vfree(p);
3088 else
3089 kfree(p);
3090}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003091
3092/*
Tejun Heob1a21362013-11-29 10:42:58 -05003093 * Used to destroy all pidlists lingering waiting for destroy timer. None
3094 * should be left afterwards.
3095 */
3096static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3097{
3098 struct cgroup_pidlist *l, *tmp_l;
3099
3100 mutex_lock(&cgrp->pidlist_mutex);
3101 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3102 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3103 mutex_unlock(&cgrp->pidlist_mutex);
3104
3105 flush_workqueue(cgroup_pidlist_destroy_wq);
3106 BUG_ON(!list_empty(&cgrp->pidlists));
3107}
3108
3109static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3110{
3111 struct delayed_work *dwork = to_delayed_work(work);
3112 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3113 destroy_dwork);
3114 struct cgroup_pidlist *tofree = NULL;
3115
3116 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003117
3118 /*
Tejun Heo04502362013-11-29 10:42:59 -05003119 * Destroy iff we didn't get queued again. The state won't change
3120 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003121 */
Tejun Heo04502362013-11-29 10:42:59 -05003122 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003123 list_del(&l->links);
3124 pidlist_free(l->list);
3125 put_pid_ns(l->key.ns);
3126 tofree = l;
3127 }
3128
Tejun Heob1a21362013-11-29 10:42:58 -05003129 mutex_unlock(&l->owner->pidlist_mutex);
3130 kfree(tofree);
3131}
3132
3133/*
Ben Blum102a7752009-09-23 15:56:26 -07003134 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003135 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003136 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003137static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003138{
Ben Blum102a7752009-09-23 15:56:26 -07003139 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003140
3141 /*
3142 * we presume the 0th element is unique, so i starts at 1. trivial
3143 * edge cases first; no work needs to be done for either
3144 */
3145 if (length == 0 || length == 1)
3146 return length;
3147 /* src and dest walk down the list; dest counts unique elements */
3148 for (src = 1; src < length; src++) {
3149 /* find next unique element */
3150 while (list[src] == list[src-1]) {
3151 src++;
3152 if (src == length)
3153 goto after;
3154 }
3155 /* dest always points to where the next unique element goes */
3156 list[dest] = list[src];
3157 dest++;
3158 }
3159after:
Ben Blum102a7752009-09-23 15:56:26 -07003160 return dest;
3161}
3162
Tejun Heoafb2bc12013-11-29 10:42:59 -05003163/*
3164 * The two pid files - task and cgroup.procs - guaranteed that the result
3165 * is sorted, which forced this whole pidlist fiasco. As pid order is
3166 * different per namespace, each namespace needs differently sorted list,
3167 * making it impossible to use, for example, single rbtree of member tasks
3168 * sorted by task pointer. As pidlists can be fairly large, allocating one
3169 * per open file is dangerous, so cgroup had to implement shared pool of
3170 * pidlists keyed by cgroup and namespace.
3171 *
3172 * All this extra complexity was caused by the original implementation
3173 * committing to an entirely unnecessary property. In the long term, we
3174 * want to do away with it. Explicitly scramble sort order if
3175 * sane_behavior so that no such expectation exists in the new interface.
3176 *
3177 * Scrambling is done by swapping every two consecutive bits, which is
3178 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3179 */
3180static pid_t pid_fry(pid_t pid)
3181{
3182 unsigned a = pid & 0x55555555;
3183 unsigned b = pid & 0xAAAAAAAA;
3184
3185 return (a << 1) | (b >> 1);
3186}
3187
3188static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3189{
3190 if (cgroup_sane_behavior(cgrp))
3191 return pid_fry(pid);
3192 else
3193 return pid;
3194}
3195
Ben Blum102a7752009-09-23 15:56:26 -07003196static int cmppid(const void *a, const void *b)
3197{
3198 return *(pid_t *)a - *(pid_t *)b;
3199}
3200
Tejun Heoafb2bc12013-11-29 10:42:59 -05003201static int fried_cmppid(const void *a, const void *b)
3202{
3203 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3204}
3205
Ben Blum72a8cb32009-09-23 15:56:27 -07003206static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3207 enum cgroup_filetype type)
3208{
3209 struct cgroup_pidlist *l;
3210 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003211 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003212
Tejun Heoe6b81712013-11-29 10:42:59 -05003213 lockdep_assert_held(&cgrp->pidlist_mutex);
3214
3215 list_for_each_entry(l, &cgrp->pidlists, links)
3216 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003217 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003218 return NULL;
3219}
3220
Ben Blum72a8cb32009-09-23 15:56:27 -07003221/*
3222 * find the appropriate pidlist for our purpose (given procs vs tasks)
3223 * returns with the lock on that pidlist already held, and takes care
3224 * of the use count, or returns NULL with no locks held if we're out of
3225 * memory.
3226 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003227static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3228 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003229{
3230 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003231
Tejun Heoe6b81712013-11-29 10:42:59 -05003232 lockdep_assert_held(&cgrp->pidlist_mutex);
3233
3234 l = cgroup_pidlist_find(cgrp, type);
3235 if (l)
3236 return l;
3237
Ben Blum72a8cb32009-09-23 15:56:27 -07003238 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003239 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003240 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003241 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003242
Tejun Heob1a21362013-11-29 10:42:58 -05003243 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003244 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003245 /* don't need task_nsproxy() if we're looking at ourself */
3246 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003247 l->owner = cgrp;
3248 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003249 return l;
3250}
3251
3252/*
Ben Blum102a7752009-09-23 15:56:26 -07003253 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3254 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003255static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3256 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003257{
3258 pid_t *array;
3259 int length;
3260 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003261 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003262 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003263 struct cgroup_pidlist *l;
3264
Tejun Heo4bac00d2013-11-29 10:42:59 -05003265 lockdep_assert_held(&cgrp->pidlist_mutex);
3266
Ben Blum102a7752009-09-23 15:56:26 -07003267 /*
3268 * If cgroup gets more users after we read count, we won't have
3269 * enough space - tough. This race is indistinguishable to the
3270 * caller from the case that the additional cgroup users didn't
3271 * show up until sometime later on.
3272 */
3273 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003274 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003275 if (!array)
3276 return -ENOMEM;
3277 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003278 css_task_iter_start(&cgrp->dummy_css, &it);
3279 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003280 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003281 break;
Ben Blum102a7752009-09-23 15:56:26 -07003282 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003283 if (type == CGROUP_FILE_PROCS)
3284 pid = task_tgid_vnr(tsk);
3285 else
3286 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003287 if (pid > 0) /* make sure to only use valid results */
3288 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003289 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003290 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003291 length = n;
3292 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003293 if (cgroup_sane_behavior(cgrp))
3294 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3295 else
3296 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003297 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003298 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003299
Tejun Heoe6b81712013-11-29 10:42:59 -05003300 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003301 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003302 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003303 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003304 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003305 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003306
3307 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003308 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003309 l->list = array;
3310 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003311 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003312 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003313}
3314
Balbir Singh846c7bb2007-10-18 23:39:44 -07003315/**
Li Zefana043e3b2008-02-23 15:24:09 -08003316 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003317 * @stats: cgroupstats to fill information into
3318 * @dentry: A dentry entry belonging to the cgroup for which stats have
3319 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003320 *
3321 * Build and fill cgroupstats so that taskstats can export it to user
3322 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003323 */
3324int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3325{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003326 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003327 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003328 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003329 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003330
Tejun Heo2bd59d42014-02-11 11:52:49 -05003331 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3332 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3333 kernfs_type(kn) != KERNFS_DIR)
3334 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003335
Li Zefanbad34662014-02-14 16:54:28 +08003336 mutex_lock(&cgroup_mutex);
3337
Tejun Heo2bd59d42014-02-11 11:52:49 -05003338 /*
3339 * We aren't being called from kernfs and there's no guarantee on
3340 * @kn->priv's validity. For this and css_tryget_from_dir(),
3341 * @kn->priv is RCU safe. Let's do the RCU dancing.
3342 */
3343 rcu_read_lock();
3344 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003345 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003346 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003347 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003348 return -ENOENT;
3349 }
Li Zefanbad34662014-02-14 16:54:28 +08003350 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003351
Tejun Heo72ec7022013-08-08 20:11:26 -04003352 css_task_iter_start(&cgrp->dummy_css, &it);
3353 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003354 switch (tsk->state) {
3355 case TASK_RUNNING:
3356 stats->nr_running++;
3357 break;
3358 case TASK_INTERRUPTIBLE:
3359 stats->nr_sleeping++;
3360 break;
3361 case TASK_UNINTERRUPTIBLE:
3362 stats->nr_uninterruptible++;
3363 break;
3364 case TASK_STOPPED:
3365 stats->nr_stopped++;
3366 break;
3367 default:
3368 if (delayacct_is_task_waiting_on_io(tsk))
3369 stats->nr_io_wait++;
3370 break;
3371 }
3372 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003373 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003374
Li Zefanbad34662014-02-14 16:54:28 +08003375 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003376 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003377}
3378
Paul Menage8f3ff202009-09-23 15:56:25 -07003379
Paul Menagecc31edc2008-10-18 20:28:04 -07003380/*
Ben Blum102a7752009-09-23 15:56:26 -07003381 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003382 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003383 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003384 */
3385
Ben Blum102a7752009-09-23 15:56:26 -07003386static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003387{
3388 /*
3389 * Initially we receive a position value that corresponds to
3390 * one more than the last pid shown (or 0 on the first call or
3391 * after a seek to the start). Use a binary-search to find the
3392 * next pid to display, if any
3393 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003394 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003395 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003396 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003397 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003398 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003399 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003400
Tejun Heo4bac00d2013-11-29 10:42:59 -05003401 mutex_lock(&cgrp->pidlist_mutex);
3402
3403 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003404 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003405 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003406 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003407 * could already have been destroyed.
3408 */
Tejun Heo5d224442013-12-05 12:28:04 -05003409 if (of->priv)
3410 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003411
3412 /*
3413 * Either this is the first start() after open or the matching
3414 * pidlist has been destroyed inbetween. Create a new one.
3415 */
Tejun Heo5d224442013-12-05 12:28:04 -05003416 if (!of->priv) {
3417 ret = pidlist_array_load(cgrp, type,
3418 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003419 if (ret)
3420 return ERR_PTR(ret);
3421 }
Tejun Heo5d224442013-12-05 12:28:04 -05003422 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003423
Paul Menagecc31edc2008-10-18 20:28:04 -07003424 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003425 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003426
Paul Menagecc31edc2008-10-18 20:28:04 -07003427 while (index < end) {
3428 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003429 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003430 index = mid;
3431 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003432 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003433 index = mid + 1;
3434 else
3435 end = mid;
3436 }
3437 }
3438 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003439 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003440 return NULL;
3441 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003442 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003443 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003444 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003445}
3446
Ben Blum102a7752009-09-23 15:56:26 -07003447static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003448{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003449 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003450 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003451
Tejun Heo5d224442013-12-05 12:28:04 -05003452 if (l)
3453 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003454 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003455 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003456}
3457
Ben Blum102a7752009-09-23 15:56:26 -07003458static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003459{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003460 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003461 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003462 pid_t *p = v;
3463 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003464 /*
3465 * Advance to the next pid in the array. If this goes off the
3466 * end, we're done
3467 */
3468 p++;
3469 if (p >= end) {
3470 return NULL;
3471 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003472 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003473 return p;
3474 }
3475}
3476
Ben Blum102a7752009-09-23 15:56:26 -07003477static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003478{
3479 return seq_printf(s, "%d\n", *(int *)v);
3480}
3481
Ben Blum102a7752009-09-23 15:56:26 -07003482/*
3483 * seq_operations functions for iterating on pidlists through seq_file -
3484 * independent of whether it's tasks or procs
3485 */
3486static const struct seq_operations cgroup_pidlist_seq_operations = {
3487 .start = cgroup_pidlist_start,
3488 .stop = cgroup_pidlist_stop,
3489 .next = cgroup_pidlist_next,
3490 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003491};
3492
Tejun Heo182446d2013-08-08 20:11:24 -04003493static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3494 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003495{
Tejun Heo182446d2013-08-08 20:11:24 -04003496 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003497}
3498
Tejun Heo182446d2013-08-08 20:11:24 -04003499static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3500 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003501{
Tejun Heo182446d2013-08-08 20:11:24 -04003502 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003503 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003504 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003505 else
Tejun Heo182446d2013-08-08 20:11:24 -04003506 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003507 return 0;
3508}
3509
Tejun Heo182446d2013-08-08 20:11:24 -04003510static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3511 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003512{
Tejun Heo182446d2013-08-08 20:11:24 -04003513 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003514}
3515
Tejun Heo182446d2013-08-08 20:11:24 -04003516static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3517 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003518{
3519 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003520 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003521 else
Tejun Heo182446d2013-08-08 20:11:24 -04003522 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003523 return 0;
3524}
3525
Tejun Heod5c56ce2013-06-03 19:14:34 -07003526static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003527 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003528 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003529 .seq_start = cgroup_pidlist_start,
3530 .seq_next = cgroup_pidlist_next,
3531 .seq_stop = cgroup_pidlist_stop,
3532 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003533 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003534 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003535 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003536 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003537 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003538 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003539 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003540 .read_u64 = cgroup_clone_children_read,
3541 .write_u64 = cgroup_clone_children_write,
3542 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003543 {
Tejun Heo873fe092013-04-14 20:15:26 -07003544 .name = "cgroup.sane_behavior",
3545 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003546 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003547 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003548
3549 /*
3550 * Historical crazy stuff. These don't have "cgroup." prefix and
3551 * don't exist if sane_behavior. If you're depending on these, be
3552 * prepared to be burned.
3553 */
3554 {
3555 .name = "tasks",
3556 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003557 .seq_start = cgroup_pidlist_start,
3558 .seq_next = cgroup_pidlist_next,
3559 .seq_stop = cgroup_pidlist_stop,
3560 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003561 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003562 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003563 .mode = S_IRUGO | S_IWUSR,
3564 },
3565 {
3566 .name = "notify_on_release",
3567 .flags = CFTYPE_INSANE,
3568 .read_u64 = cgroup_read_notify_on_release,
3569 .write_u64 = cgroup_write_notify_on_release,
3570 },
Tejun Heo873fe092013-04-14 20:15:26 -07003571 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003572 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003573 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003574 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003575 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003576 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003577 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003578 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003579};
3580
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003581/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003582 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003583 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003584 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003585 *
3586 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003587 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003588static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003589{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003590 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003591 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003592
Tejun Heo8e3f6542012-04-01 12:09:55 -07003593 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003594 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05003595 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07003596
3597 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003598 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003599
Tejun Heo0adb0702014-02-12 09:29:48 -05003600 list_for_each_entry(cfts, &ss->cfts, node) {
3601 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003602 if (ret < 0)
3603 goto err;
3604 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003605 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003606 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003607err:
3608 cgroup_clear_dir(cgrp, subsys_mask);
3609 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003610}
3611
Tejun Heo0c21ead2013-08-13 20:22:51 -04003612/*
3613 * css destruction is four-stage process.
3614 *
3615 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3616 * Implemented in kill_css().
3617 *
3618 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3619 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3620 * by invoking offline_css(). After offlining, the base ref is put.
3621 * Implemented in css_killed_work_fn().
3622 *
3623 * 3. When the percpu_ref reaches zero, the only possible remaining
3624 * accessors are inside RCU read sections. css_release() schedules the
3625 * RCU callback.
3626 *
3627 * 4. After the grace period, the css can be freed. Implemented in
3628 * css_free_work_fn().
3629 *
3630 * It is actually hairier because both step 2 and 4 require process context
3631 * and thus involve punting to css->destroy_work adding two additional
3632 * steps to the already complex sequence.
3633 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003634static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003635{
3636 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003637 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003638 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003639
Tejun Heo0ae78e02013-08-13 11:01:54 -04003640 if (css->parent)
3641 css_put(css->parent);
3642
Tejun Heo0c21ead2013-08-13 20:22:51 -04003643 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003644 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003645}
3646
3647static void css_free_rcu_fn(struct rcu_head *rcu_head)
3648{
3649 struct cgroup_subsys_state *css =
3650 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3651
Tejun Heo0c21ead2013-08-13 20:22:51 -04003652 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003653 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003654}
3655
Tejun Heod3daf282013-06-13 19:39:16 -07003656static void css_release(struct percpu_ref *ref)
3657{
3658 struct cgroup_subsys_state *css =
3659 container_of(ref, struct cgroup_subsys_state, refcnt);
3660
Monam Agarwal01a97142014-03-24 00:17:18 +05303661 RCU_INIT_POINTER(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003662 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003663}
3664
Tejun Heo623f9262013-08-13 11:01:55 -04003665static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3666 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003667{
Paul Menagebd89aab2007-10-18 23:40:44 -07003668 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003669 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003670 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003671
Tejun Heo0ae78e02013-08-13 11:01:54 -04003672 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003673 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003674 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003675 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003676
Tejun Heoca8bdca2013-08-26 18:40:56 -04003677 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003678}
3679
Li Zefan2a4ac632013-07-31 16:16:40 +08003680/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04003681static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003682{
Tejun Heo623f9262013-08-13 11:01:55 -04003683 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08003684 int ret = 0;
3685
Tejun Heoace2bee2014-02-11 11:52:47 -05003686 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003687 lockdep_assert_held(&cgroup_mutex);
3688
Tejun Heo92fb9742012-11-19 08:13:38 -08003689 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04003690 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003691 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04003692 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04003693 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05003694 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003695 }
Tejun Heob1929db2012-11-19 08:13:38 -08003696 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003697}
3698
Li Zefan2a4ac632013-07-31 16:16:40 +08003699/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04003700static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003701{
Tejun Heo623f9262013-08-13 11:01:55 -04003702 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003703
Tejun Heoace2bee2014-02-11 11:52:47 -05003704 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003705 lockdep_assert_held(&cgroup_mutex);
3706
3707 if (!(css->flags & CSS_ONLINE))
3708 return;
3709
Li Zefand7eeac12013-03-12 15:35:59 -07003710 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04003711 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003712
Tejun Heoeb954192013-08-08 20:11:23 -04003713 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04003714 css->cgroup->nr_css--;
Tejun Heoe3297802014-04-23 11:13:15 -04003715 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003716}
3717
Tejun Heoc81c925a2013-12-06 15:11:56 -05003718/**
3719 * create_css - create a cgroup_subsys_state
3720 * @cgrp: the cgroup new css will be associated with
3721 * @ss: the subsys of new css
3722 *
3723 * Create a new css associated with @cgrp - @ss pair. On success, the new
3724 * css is online and installed in @cgrp with all interface files created.
3725 * Returns 0 on success, -errno on failure.
3726 */
3727static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
3728{
3729 struct cgroup *parent = cgrp->parent;
3730 struct cgroup_subsys_state *css;
3731 int err;
3732
Tejun Heoc81c925a2013-12-06 15:11:56 -05003733 lockdep_assert_held(&cgroup_mutex);
3734
3735 css = ss->css_alloc(cgroup_css(parent, ss));
3736 if (IS_ERR(css))
3737 return PTR_ERR(css);
3738
3739 err = percpu_ref_init(&css->refcnt, css_release);
3740 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08003741 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05003742
3743 init_css(css, ss, cgrp);
3744
Tejun Heoaec25022014-02-08 10:36:58 -05003745 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003746 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08003747 goto err_free_percpu_ref;
Tejun Heoc81c925a2013-12-06 15:11:56 -05003748
3749 err = online_css(css);
3750 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08003751 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05003752
Tejun Heo59f52962014-02-11 11:52:49 -05003753 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003754 css_get(css->parent);
3755
3756 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
3757 parent->parent) {
3758 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",
3759 current->comm, current->pid, ss->name);
3760 if (!strcmp(ss->name, "memory"))
3761 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
3762 ss->warned_broken_hierarchy = true;
3763 }
3764
3765 return 0;
3766
Li Zefan3eb59ec2014-03-18 17:02:36 +08003767err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07003768 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08003769err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05003770 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08003771err_free_css:
Tejun Heoc81c925a2013-12-06 15:11:56 -05003772 ss->css_free(css);
3773 return err;
3774}
3775
Tejun Heo2bd59d42014-02-11 11:52:49 -05003776/**
Li Zefana043e3b2008-02-23 15:24:09 -08003777 * cgroup_create - create a cgroup
3778 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05003779 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05003780 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07003781 */
Tejun Heoe61734c2014-02-12 09:29:50 -05003782static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003783 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003784{
Paul Menagebd89aab2007-10-18 23:40:44 -07003785 struct cgroup *cgrp;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003786 struct cgroup_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05003787 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003788 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003789 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003790
Tejun Heo0a950f62012-11-19 09:02:12 -08003791 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07003792 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3793 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003794 return -ENOMEM;
3795
Tejun Heoace2bee2014-02-11 11:52:47 -05003796 mutex_lock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003797
Li Zefan4e96ee82013-07-31 09:50:50 +08003798 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08003799 * Only live parents can have children. Note that the liveliness
3800 * check isn't strictly necessary because cgroup_mkdir() and
3801 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
3802 * anyway so that locking is contained inside cgroup proper and we
3803 * don't get nasty surprises if we ever grow another caller.
3804 */
3805 if (!cgroup_lock_live_group(parent)) {
3806 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05003807 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08003808 }
3809
3810 /*
3811 * Temporarily set the pointer to NULL, so idr_find() won't return
3812 * a half-baked cgroup.
3813 */
3814 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
3815 if (cgrp->id < 0) {
3816 err = -ENOMEM;
3817 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08003818 }
3819
Paul Menagecc31edc2008-10-18 20:28:04 -07003820 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003821
Paul Menagebd89aab2007-10-18 23:40:44 -07003822 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003823 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07003824 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003825
Li Zefanb6abdb02008-03-04 14:28:19 -08003826 if (notify_on_release(parent))
3827 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3828
Tejun Heo2260e7f2012-11-19 08:13:38 -08003829 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
3830 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003831
Tejun Heo2bd59d42014-02-11 11:52:49 -05003832 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05003833 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003834 if (IS_ERR(kn)) {
3835 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003836 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003837 }
3838 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003839
Tejun Heo6f305582014-02-12 09:29:50 -05003840 /*
3841 * This extra ref will be put in cgroup_free_fn() and guarantees
3842 * that @cgrp->kn is always accessible.
3843 */
3844 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003845
Tejun Heo00356bd2013-06-18 11:14:22 -07003846 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09003847
Tejun Heo4e139af2012-11-19 08:13:36 -08003848 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08003849 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05003850 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05003851 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08003852
Tejun Heo0d802552013-12-06 15:11:56 -05003853 /*
3854 * @cgrp is now fully operational. If something fails after this
3855 * point, it'll be released via the normal destruction path.
3856 */
Li Zefan4e96ee82013-07-31 09:50:50 +08003857 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
3858
Tejun Heo49957f82014-04-07 16:44:47 -04003859 err = cgroup_kn_set_ugid(kn);
3860 if (err)
3861 goto err_destroy;
3862
Tejun Heo2bb566c2013-08-08 20:11:23 -04003863 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07003864 if (err)
3865 goto err_destroy;
3866
Tejun Heo9d403e92013-12-06 15:11:56 -05003867 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05003868 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04003869 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heob85d2042013-12-06 15:11:57 -05003870 err = create_css(cgrp, ss);
3871 if (err)
3872 goto err_destroy;
3873 }
Tejun Heoa8638032012-11-09 09:12:29 -08003874 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003875
Tejun Heobd53d612014-04-23 11:13:16 -04003876 /*
3877 * On the default hierarchy, a child doesn't automatically inherit
3878 * child_subsys_mask from the parent. Each is configured manually.
3879 */
3880 if (!cgroup_on_dfl(cgrp))
3881 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04003882
Tejun Heo2bd59d42014-02-11 11:52:49 -05003883 kernfs_activate(kn);
3884
Paul Menageddbcc7e2007-10-18 23:39:30 -07003885 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003886 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003887
3888 return 0;
3889
Tejun Heo0a950f62012-11-19 09:02:12 -08003890err_free_id:
Li Zefan4e96ee82013-07-31 09:50:50 +08003891 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003892err_unlock:
3893 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003894err_unlock_tree:
3895 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003896 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003897 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08003898
3899err_destroy:
3900 cgroup_destroy_locked(cgrp);
3901 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003902 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08003903 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003904}
3905
Tejun Heo2bd59d42014-02-11 11:52:49 -05003906static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3907 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003908{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003909 struct cgroup *parent = parent_kn->priv;
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003910 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003911
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003912 /*
3913 * cgroup_create() grabs cgroup_tree_mutex which nests outside
3914 * kernfs active_ref and cgroup_create() already synchronizes
3915 * properly against removal through cgroup_lock_live_group().
3916 * Break it before calling cgroup_create().
3917 */
3918 cgroup_get(parent);
3919 kernfs_break_active_protection(parent_kn);
3920
3921 ret = cgroup_create(parent, name, mode);
3922
3923 kernfs_unbreak_active_protection(parent_kn);
3924 cgroup_put(parent);
3925 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003926}
3927
Tejun Heo223dbc32013-08-13 20:22:50 -04003928/*
3929 * This is called when the refcnt of a css is confirmed to be killed.
3930 * css_tryget() is now guaranteed to fail.
3931 */
3932static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07003933{
Tejun Heo223dbc32013-08-13 20:22:50 -04003934 struct cgroup_subsys_state *css =
3935 container_of(work, struct cgroup_subsys_state, destroy_work);
3936 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07003937
Tejun Heoace2bee2014-02-11 11:52:47 -05003938 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003939 mutex_lock(&cgroup_mutex);
3940
3941 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04003942 * css_tryget() is guaranteed to fail now. Tell subsystems to
3943 * initate destruction.
3944 */
3945 offline_css(css);
3946
3947 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003948 * If @cgrp is marked dead, it's waiting for refs of all css's to
3949 * be disabled before proceeding to the second phase of cgroup
3950 * destruction. If we are the last one, kick it off.
3951 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04003952 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04003953 cgroup_destroy_css_killed(cgrp);
3954
3955 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003956 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04003957
3958 /*
3959 * Put the css refs from kill_css(). Each css holds an extra
3960 * reference to the cgroup's dentry and cgroup removal proceeds
3961 * regardless of css refs. On the last put of each css, whenever
3962 * that may be, the extra dentry ref is put so that dentry
3963 * destruction happens only after all css's are released.
3964 */
3965 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07003966}
3967
Tejun Heo223dbc32013-08-13 20:22:50 -04003968/* css kill confirmation processing requires process context, bounce */
3969static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07003970{
3971 struct cgroup_subsys_state *css =
3972 container_of(ref, struct cgroup_subsys_state, refcnt);
3973
Tejun Heo223dbc32013-08-13 20:22:50 -04003974 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003975 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07003976}
3977
Tejun Heof392e512014-04-23 11:13:14 -04003978/**
3979 * kill_css - destroy a css
3980 * @css: css to destroy
3981 *
3982 * This function initiates destruction of @css by removing cgroup interface
3983 * files and putting its base reference. ->css_offline() will be invoked
3984 * asynchronously once css_tryget() is guaranteed to fail and when the
3985 * reference count reaches zero, @css will be released.
3986 */
3987static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04003988{
Tejun Heo94419622014-03-19 10:23:54 -04003989 lockdep_assert_held(&cgroup_tree_mutex);
3990
Tejun Heo2bd59d42014-02-11 11:52:49 -05003991 /*
3992 * This must happen before css is disassociated with its cgroup.
3993 * See seq_css() for details.
3994 */
Tejun Heoaec25022014-02-08 10:36:58 -05003995 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003996
Tejun Heoedae0c32013-08-13 20:22:51 -04003997 /*
3998 * Killing would put the base ref, but we need to keep it alive
3999 * until after ->css_offline().
4000 */
4001 css_get(css);
4002
4003 /*
4004 * cgroup core guarantees that, by the time ->css_offline() is
4005 * invoked, no new css reference will be given out via
4006 * css_tryget(). We can't simply call percpu_ref_kill() and
4007 * proceed to offlining css's because percpu_ref_kill() doesn't
4008 * guarantee that the ref is seen as killed on all CPUs on return.
4009 *
4010 * Use percpu_ref_kill_and_confirm() to get notifications as each
4011 * css is confirmed to be seen as killed on all CPUs.
4012 */
4013 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004014}
4015
4016/**
4017 * cgroup_destroy_locked - the first stage of cgroup destruction
4018 * @cgrp: cgroup to be destroyed
4019 *
4020 * css's make use of percpu refcnts whose killing latency shouldn't be
4021 * exposed to userland and are RCU protected. Also, cgroup core needs to
4022 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4023 * invoked. To satisfy all the requirements, destruction is implemented in
4024 * the following two steps.
4025 *
4026 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4027 * userland visible parts and start killing the percpu refcnts of
4028 * css's. Set up so that the next stage will be kicked off once all
4029 * the percpu refcnts are confirmed to be killed.
4030 *
4031 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4032 * rest of destruction. Once all cgroup references are gone, the
4033 * cgroup is RCU-freed.
4034 *
4035 * This function implements s1. After this step, @cgrp is gone as far as
4036 * the userland is concerned and a new cgroup with the same name may be
4037 * created. As cgroup doesn't care about the names internally, this
4038 * doesn't cause any problem.
4039 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004040static int cgroup_destroy_locked(struct cgroup *cgrp)
4041 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004042{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004043 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004044 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004045 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004046 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004047
Tejun Heoace2bee2014-02-11 11:52:47 -05004048 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004049 lockdep_assert_held(&cgroup_mutex);
4050
Tejun Heoddd69142013-06-12 21:04:54 -07004051 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004052 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004053 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004054 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004055 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004056 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004057 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004058 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004059 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004060
Tejun Heo1a90dd52012-11-05 09:16:59 -08004061 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004062 * Make sure there's no live children. We can't test ->children
4063 * emptiness as dead children linger on it while being destroyed;
4064 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4065 */
4066 empty = true;
4067 rcu_read_lock();
4068 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4069 empty = cgroup_is_dead(child);
4070 if (!empty)
4071 break;
4072 }
4073 rcu_read_unlock();
4074 if (!empty)
4075 return -EBUSY;
4076
4077 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004078 * Mark @cgrp dead. This prevents further task migration and child
4079 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004080 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004081 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004082 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004083 */
Tejun Heo54766d42013-06-12 21:04:53 -07004084 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004085
Tejun Heo5d773812014-03-19 10:23:53 -04004086 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004087 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4088 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004089 * percpu refs of all css's are confirmed to be killed. This
4090 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004091 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004092 mutex_unlock(&cgroup_mutex);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004093 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004094 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004095 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004096
Tejun Heo455050d2013-06-13 19:27:41 -07004097 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4098 raw_spin_lock(&release_list_lock);
4099 if (!list_empty(&cgrp->release_list))
4100 list_del_init(&cgrp->release_list);
4101 raw_spin_unlock(&release_list_lock);
4102
4103 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004104 * If @cgrp has css's attached, the second stage of cgroup
4105 * destruction is kicked off from css_killed_work_fn() after the
4106 * refs of all attached css's are killed. If @cgrp doesn't have
4107 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004108 */
Tejun Heof20104d2013-08-13 20:22:50 -04004109 if (!cgrp->nr_css)
4110 cgroup_destroy_css_killed(cgrp);
4111
Tejun Heo2bd59d42014-02-11 11:52:49 -05004112 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05004113 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004114
Tejun Heof20104d2013-08-13 20:22:50 -04004115 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05004116 * There are two control paths which try to determine cgroup from
4117 * dentry without going through kernfs - cgroupstats_build() and
4118 * css_tryget_from_dir(). Those are supported by RCU protecting
4119 * clearing of cgrp->kn->priv backpointer, which should happen
4120 * after all files under it have been removed.
Tejun Heo455050d2013-06-13 19:27:41 -07004121 */
Tejun Heo6f305582014-02-12 09:29:50 -05004122 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004123 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004124
Tejun Heo4ac06012014-02-11 11:52:47 -05004125 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004126
Tejun Heoea15f8c2013-06-13 19:27:42 -07004127 return 0;
4128};
4129
Tejun Heod3daf282013-06-13 19:39:16 -07004130/**
Tejun Heof20104d2013-08-13 20:22:50 -04004131 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004132 * @work: cgroup->destroy_free_work
4133 *
4134 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004135 * destroyed after all css's are offlined and performs the rest of
4136 * destruction. This is the second step of destruction described in the
4137 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004138 */
Tejun Heof20104d2013-08-13 20:22:50 -04004139static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004140{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004141 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004142
Tejun Heoace2bee2014-02-11 11:52:47 -05004143 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004144 lockdep_assert_held(&cgroup_mutex);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004145
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004146 /* delete this cgroup from parent->children */
4147 list_del_rcu(&cgrp->sibling);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004148
Tejun Heo59f52962014-02-11 11:52:49 -05004149 cgroup_put(cgrp);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004150
Paul Menageddbcc7e2007-10-18 23:39:30 -07004151 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004152 check_for_release(parent);
4153}
4154
Tejun Heo2bd59d42014-02-11 11:52:49 -05004155static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004156{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004157 struct cgroup *cgrp = kn->priv;
4158 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004159
Tejun Heo2bd59d42014-02-11 11:52:49 -05004160 /*
4161 * This is self-destruction but @kn can't be removed while this
4162 * callback is in progress. Let's break active protection. Once
4163 * the protection is broken, @cgrp can be destroyed at any point.
4164 * Pin it so that it stays accessible.
4165 */
4166 cgroup_get(cgrp);
4167 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004168
Tejun Heoace2bee2014-02-11 11:52:47 -05004169 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004170 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004171
Tejun Heo2bd59d42014-02-11 11:52:49 -05004172 /*
4173 * @cgrp might already have been destroyed while we're trying to
4174 * grab the mutexes.
4175 */
4176 if (!cgroup_is_dead(cgrp))
4177 ret = cgroup_destroy_locked(cgrp);
4178
Tejun Heo42809dd2012-11-19 08:13:37 -08004179 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004180 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004181
Tejun Heo2bd59d42014-02-11 11:52:49 -05004182 kernfs_unbreak_active_protection(kn);
4183 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004184 return ret;
4185}
4186
Tejun Heo2bd59d42014-02-11 11:52:49 -05004187static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4188 .remount_fs = cgroup_remount,
4189 .show_options = cgroup_show_options,
4190 .mkdir = cgroup_mkdir,
4191 .rmdir = cgroup_rmdir,
4192 .rename = cgroup_rename,
4193};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004194
Li Zefan06a11922008-04-29 01:00:07 -07004195static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004196{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004197 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004198
4199 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004200
Tejun Heoace2bee2014-02-11 11:52:47 -05004201 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004202 mutex_lock(&cgroup_mutex);
4203
Tejun Heo0adb0702014-02-12 09:29:48 -05004204 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004205
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004206 /* Create the root cgroup state for this subsystem */
4207 ss->root = &cgrp_dfl_root;
4208 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004209 /* We don't handle early failures gracefully */
4210 BUG_ON(IS_ERR(css));
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004211 init_css(css, ss, &cgrp_dfl_root.cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004212
Li Zefane8d55fd2008-04-29 01:00:13 -07004213 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004214 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004215 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004216 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004217 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004218
4219 need_forkexit_callback |= ss->fork || ss->exit;
4220
Li Zefane8d55fd2008-04-29 01:00:13 -07004221 /* At system boot, before all subsystems have been
4222 * registered, no tasks have been forked, so we don't
4223 * need to invoke fork callbacks here. */
4224 BUG_ON(!list_empty(&init_task.tasks));
4225
Tejun Heoae7f1642013-08-13 20:22:50 -04004226 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004227
Tejun Heof392e512014-04-23 11:13:14 -04004228 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004229
Ben Blume6a11052010-03-10 15:22:09 -08004230 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004231 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004232}
4233
4234/**
Li Zefana043e3b2008-02-23 15:24:09 -08004235 * cgroup_init_early - cgroup initialization at system boot
4236 *
4237 * Initialize cgroups at system boot, and initialize any
4238 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004239 */
4240int __init cgroup_init_early(void)
4241{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004242 static struct cgroup_sb_opts __initdata opts =
4243 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004244 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004245 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004246
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004247 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004248 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004249
Tejun Heo3ed80a62014-02-08 10:36:58 -05004250 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004251 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004252 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4253 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004254 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004255 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4256 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004257
Tejun Heoaec25022014-02-08 10:36:58 -05004258 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004259 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004260
4261 if (ss->early_init)
4262 cgroup_init_subsys(ss);
4263 }
4264 return 0;
4265}
4266
4267/**
Li Zefana043e3b2008-02-23 15:24:09 -08004268 * cgroup_init - cgroup initialization
4269 *
4270 * Register cgroup filesystem and /proc file, and initialize
4271 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004272 */
4273int __init cgroup_init(void)
4274{
Tejun Heo30159ec2013-06-25 11:53:37 -07004275 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004276 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004277 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004278
Tejun Heo2bd59d42014-02-11 11:52:49 -05004279 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004280
Tejun Heo985ed672014-03-19 10:23:53 -04004281 mutex_lock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004282 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004283
Tejun Heo82fe9b02013-06-25 11:53:37 -07004284 /* Add init_css_set to the hash table */
4285 key = css_set_hash(init_css_set.subsys);
4286 hash_add(css_set_table, &init_css_set.hlist, key);
4287
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004288 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004289
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004290 mutex_unlock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04004291 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004292
Tejun Heo172a2c062014-03-19 10:23:53 -04004293 for_each_subsys(ss, ssid) {
4294 if (!ss->early_init)
4295 cgroup_init_subsys(ss);
4296
Tejun Heo2d8f2432014-04-23 11:13:15 -04004297 list_add_tail(&init_css_set.e_cset_node[ssid],
4298 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4299
Tejun Heo172a2c062014-03-19 10:23:53 -04004300 /*
4301 * cftype registration needs kmalloc and can't be done
4302 * during early_init. Register base cftypes separately.
4303 */
4304 if (ss->base_cftypes)
4305 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4306 }
Greg KH676db4a2010-08-05 13:53:35 -07004307
Paul Menageddbcc7e2007-10-18 23:39:30 -07004308 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004309 if (!cgroup_kobj)
4310 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004311
4312 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004313 if (err < 0) {
4314 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004315 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004316 }
4317
4318 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004319 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004320}
Paul Menageb4f48b62007-10-18 23:39:33 -07004321
Tejun Heoe5fca242013-11-22 17:14:39 -05004322static int __init cgroup_wq_init(void)
4323{
4324 /*
4325 * There isn't much point in executing destruction path in
4326 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004327 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004328 *
4329 * We would prefer to do this in cgroup_init() above, but that
4330 * is called before init_workqueues(): so leave this until after.
4331 */
Tejun Heo1a115332014-02-12 19:06:19 -05004332 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004333 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004334
4335 /*
4336 * Used to destroy pidlists and separate to serve as flush domain.
4337 * Cap @max_active to 1 too.
4338 */
4339 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4340 0, 1);
4341 BUG_ON(!cgroup_pidlist_destroy_wq);
4342
Tejun Heoe5fca242013-11-22 17:14:39 -05004343 return 0;
4344}
4345core_initcall(cgroup_wq_init);
4346
Paul Menagea4243162007-10-18 23:39:35 -07004347/*
4348 * proc_cgroup_show()
4349 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4350 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004351 */
4352
4353/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004354int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004355{
4356 struct pid *pid;
4357 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004358 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004359 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004360 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004361
4362 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004363 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004364 if (!buf)
4365 goto out;
4366
4367 retval = -ESRCH;
4368 pid = m->private;
4369 tsk = get_pid_task(pid, PIDTYPE_PID);
4370 if (!tsk)
4371 goto out_free;
4372
4373 retval = 0;
4374
4375 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004376 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004377
Tejun Heo985ed672014-03-19 10:23:53 -04004378 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004379 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004380 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004381 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004382
Tejun Heoa2dd4242014-03-19 10:23:55 -04004383 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004384 continue;
4385
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004386 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004387 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004388 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004389 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004390 if (strlen(root->name))
4391 seq_printf(m, "%sname=%s", count ? "," : "",
4392 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004393 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004394 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004395 path = cgroup_path(cgrp, buf, PATH_MAX);
4396 if (!path) {
4397 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004398 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004399 }
4400 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004401 seq_putc(m, '\n');
4402 }
4403
4404out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004405 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004406 mutex_unlock(&cgroup_mutex);
4407 put_task_struct(tsk);
4408out_free:
4409 kfree(buf);
4410out:
4411 return retval;
4412}
4413
Paul Menagea4243162007-10-18 23:39:35 -07004414/* Display information about each subsystem and each hierarchy */
4415static int proc_cgroupstats_show(struct seq_file *m, void *v)
4416{
Tejun Heo30159ec2013-06-25 11:53:37 -07004417 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004418 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004419
Paul Menage8bab8dd2008-04-04 14:29:57 -07004420 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004421 /*
4422 * ideally we don't want subsystems moving around while we do this.
4423 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4424 * subsys/hierarchy state.
4425 */
Paul Menagea4243162007-10-18 23:39:35 -07004426 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004427
4428 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004429 seq_printf(m, "%s\t%d\t%d\t%d\n",
4430 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004431 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004432
Paul Menagea4243162007-10-18 23:39:35 -07004433 mutex_unlock(&cgroup_mutex);
4434 return 0;
4435}
4436
4437static int cgroupstats_open(struct inode *inode, struct file *file)
4438{
Al Viro9dce07f2008-03-29 03:07:28 +00004439 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004440}
4441
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004442static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004443 .open = cgroupstats_open,
4444 .read = seq_read,
4445 .llseek = seq_lseek,
4446 .release = single_release,
4447};
4448
Paul Menageb4f48b62007-10-18 23:39:33 -07004449/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004450 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004451 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004452 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004453 * A task is associated with the init_css_set until cgroup_post_fork()
4454 * attaches it to the parent's css_set. Empty cg_list indicates that
4455 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004456 */
4457void cgroup_fork(struct task_struct *child)
4458{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004459 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004460 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004461}
4462
4463/**
Li Zefana043e3b2008-02-23 15:24:09 -08004464 * cgroup_post_fork - called on a new task after adding it to the task list
4465 * @child: the task in question
4466 *
Tejun Heo5edee612012-10-16 15:03:14 -07004467 * Adds the task to the list running through its css_set if necessary and
4468 * call the subsystem fork() callbacks. Has to be after the task is
4469 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004470 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004471 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004472 */
Paul Menage817929e2007-10-18 23:39:36 -07004473void cgroup_post_fork(struct task_struct *child)
4474{
Tejun Heo30159ec2013-06-25 11:53:37 -07004475 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004476 int i;
4477
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004478 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004479 * This may race against cgroup_enable_task_cg_links(). As that
4480 * function sets use_task_css_set_links before grabbing
4481 * tasklist_lock and we just went through tasklist_lock to add
4482 * @child, it's guaranteed that either we see the set
4483 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4484 * @child during its iteration.
4485 *
4486 * If we won the race, @child is associated with %current's
4487 * css_set. Grabbing css_set_rwsem guarantees both that the
4488 * association is stable, and, on completion of the parent's
4489 * migration, @child is visible in the source of migration or
4490 * already in the destination cgroup. This guarantee is necessary
4491 * when implementing operations which need to migrate all tasks of
4492 * a cgroup to another.
4493 *
4494 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4495 * will remain in init_css_set. This is safe because all tasks are
4496 * in the init_css_set before cg_links is enabled and there's no
4497 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004498 */
Paul Menage817929e2007-10-18 23:39:36 -07004499 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004500 struct css_set *cset;
4501
Tejun Heo96d365e2014-02-13 06:58:40 -05004502 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004503 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004504 if (list_empty(&child->cg_list)) {
4505 rcu_assign_pointer(child->cgroups, cset);
4506 list_add(&child->cg_list, &cset->tasks);
4507 get_css_set(cset);
4508 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004509 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004510 }
Tejun Heo5edee612012-10-16 15:03:14 -07004511
4512 /*
4513 * Call ss->fork(). This must happen after @child is linked on
4514 * css_set; otherwise, @child might change state between ->fork()
4515 * and addition to css_set.
4516 */
4517 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004518 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004519 if (ss->fork)
4520 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004521 }
Paul Menage817929e2007-10-18 23:39:36 -07004522}
Tejun Heo5edee612012-10-16 15:03:14 -07004523
Paul Menage817929e2007-10-18 23:39:36 -07004524/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004525 * cgroup_exit - detach cgroup from exiting task
4526 * @tsk: pointer to task_struct of exiting process
4527 *
4528 * Description: Detach cgroup from @tsk and release it.
4529 *
4530 * Note that cgroups marked notify_on_release force every task in
4531 * them to take the global cgroup_mutex mutex when exiting.
4532 * This could impact scaling on very large systems. Be reluctant to
4533 * use notify_on_release cgroups where very high task exit scaling
4534 * is required on large systems.
4535 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004536 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4537 * call cgroup_exit() while the task is still competent to handle
4538 * notify_on_release(), then leave the task attached to the root cgroup in
4539 * each hierarchy for the remainder of its exit. No need to bother with
4540 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004541 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004542 */
Li Zefan1ec41832014-03-28 15:22:19 +08004543void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004544{
Tejun Heo30159ec2013-06-25 11:53:37 -07004545 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004546 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004547 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004548 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004549
4550 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05004551 * Unlink from @tsk from its css_set. As migration path can't race
4552 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004553 */
4554 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004555 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004556 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004557 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004558 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07004559 }
4560
Paul Menageb4f48b62007-10-18 23:39:33 -07004561 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07004562 cset = task_css_set(tsk);
4563 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004564
Li Zefan1ec41832014-03-28 15:22:19 +08004565 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004566 /* see cgroup_post_fork() for details */
4567 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004568 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004569 struct cgroup_subsys_state *old_css = cset->subsys[i];
4570 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004571
Tejun Heoeb954192013-08-08 20:11:23 -04004572 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004573 }
4574 }
4575 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004576
Tejun Heoeaf797a2014-02-25 10:04:03 -05004577 if (put_cset)
4578 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07004579}
Paul Menage697f4162007-10-18 23:39:34 -07004580
Paul Menagebd89aab2007-10-18 23:40:44 -07004581static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004582{
Li Zefanf50daa72013-03-01 15:06:07 +08004583 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004584 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004585 /*
4586 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004587 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004588 * it now
4589 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004590 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004591
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004592 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004593 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004594 list_empty(&cgrp->release_list)) {
4595 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004596 need_schedule_work = 1;
4597 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004598 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004599 if (need_schedule_work)
4600 schedule_work(&release_agent_work);
4601 }
4602}
4603
Paul Menage81a6a5c2007-10-18 23:39:38 -07004604/*
4605 * Notify userspace when a cgroup is released, by running the
4606 * configured release agent with the name of the cgroup (path
4607 * relative to the root of cgroup file system) as the argument.
4608 *
4609 * Most likely, this user command will try to rmdir this cgroup.
4610 *
4611 * This races with the possibility that some other task will be
4612 * attached to this cgroup before it is removed, or that some other
4613 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4614 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4615 * unused, and this cgroup will be reprieved from its death sentence,
4616 * to continue to serve a useful existence. Next time it's released,
4617 * we will get notified again, if it still has 'notify_on_release' set.
4618 *
4619 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4620 * means only wait until the task is successfully execve()'d. The
4621 * separate release agent task is forked by call_usermodehelper(),
4622 * then control in this thread returns here, without waiting for the
4623 * release agent task. We don't bother to wait because the caller of
4624 * this routine has no use for the exit status of the release agent
4625 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004626 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004627static void cgroup_release_agent(struct work_struct *work)
4628{
4629 BUG_ON(work != &release_agent_work);
4630 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004631 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004632 while (!list_empty(&release_list)) {
4633 char *argv[3], *envp[3];
4634 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05004635 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07004636 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004637 struct cgroup,
4638 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004639 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004640 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05004641 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004642 if (!pathbuf)
4643 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05004644 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
4645 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07004646 goto continue_free;
4647 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4648 if (!agentbuf)
4649 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004650
4651 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004652 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05004653 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004654 argv[i] = NULL;
4655
4656 i = 0;
4657 /* minimal command environment */
4658 envp[i++] = "HOME=/";
4659 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4660 envp[i] = NULL;
4661
4662 /* Drop the lock while we invoke the usermode helper,
4663 * since the exec could involve hitting disk and hence
4664 * be a slow process */
4665 mutex_unlock(&cgroup_mutex);
4666 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004667 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004668 continue_free:
4669 kfree(pathbuf);
4670 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004671 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004672 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004673 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004674 mutex_unlock(&cgroup_mutex);
4675}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004676
4677static int __init cgroup_disable(char *str)
4678{
Tejun Heo30159ec2013-06-25 11:53:37 -07004679 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004680 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07004681 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004682
4683 while ((token = strsep(&str, ",")) != NULL) {
4684 if (!*token)
4685 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004686
Tejun Heo3ed80a62014-02-08 10:36:58 -05004687 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004688 if (!strcmp(token, ss->name)) {
4689 ss->disabled = 1;
4690 printk(KERN_INFO "Disabling %s control group"
4691 " subsystem\n", ss->name);
4692 break;
4693 }
4694 }
4695 }
4696 return 1;
4697}
4698__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004699
Tejun Heob77d7b62013-08-13 11:01:54 -04004700/**
Tejun Heo5a17f542014-02-11 11:52:47 -05004701 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04004702 * @dentry: directory dentry of interest
4703 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04004704 *
Tejun Heo5a17f542014-02-11 11:52:47 -05004705 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4706 * to get the corresponding css and return it. If such css doesn't exist
4707 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02004708 */
Tejun Heo5a17f542014-02-11 11:52:47 -05004709struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4710 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004711{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004712 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4713 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004714 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004715
Tejun Heo35cf0832013-08-26 18:40:56 -04004716 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004717 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4718 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004719 return ERR_PTR(-EBADF);
4720
Tejun Heo5a17f542014-02-11 11:52:47 -05004721 rcu_read_lock();
4722
Tejun Heo2bd59d42014-02-11 11:52:49 -05004723 /*
4724 * This path doesn't originate from kernfs and @kn could already
4725 * have been or be removed at any point. @kn->priv is RCU
4726 * protected for this access. See destroy_locked() for details.
4727 */
4728 cgrp = rcu_dereference(kn->priv);
4729 if (cgrp)
4730 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05004731
4732 if (!css || !css_tryget(css))
4733 css = ERR_PTR(-ENOENT);
4734
4735 rcu_read_unlock();
4736 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004737}
Stephane Eraniane5d13672011-02-14 11:20:01 +02004738
Li Zefan1cb650b2013-08-19 10:05:24 +08004739/**
4740 * css_from_id - lookup css by id
4741 * @id: the cgroup id
4742 * @ss: cgroup subsys to be looked into
4743 *
4744 * Returns the css if there's valid one with @id, otherwise returns NULL.
4745 * Should be called under rcu_read_lock().
4746 */
4747struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4748{
4749 struct cgroup *cgrp;
4750
Tejun Heoace2bee2014-02-11 11:52:47 -05004751 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08004752
4753 cgrp = idr_find(&ss->root->cgroup_idr, id);
4754 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04004755 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08004756 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004757}
4758
Paul Menagefe693432009-09-23 15:56:20 -07004759#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04004760static struct cgroup_subsys_state *
4761debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07004762{
4763 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4764
4765 if (!css)
4766 return ERR_PTR(-ENOMEM);
4767
4768 return css;
4769}
4770
Tejun Heoeb954192013-08-08 20:11:23 -04004771static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07004772{
Tejun Heoeb954192013-08-08 20:11:23 -04004773 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07004774}
4775
Tejun Heo182446d2013-08-08 20:11:24 -04004776static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
4777 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004778{
Tejun Heo182446d2013-08-08 20:11:24 -04004779 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07004780}
4781
Tejun Heo182446d2013-08-08 20:11:24 -04004782static u64 current_css_set_read(struct cgroup_subsys_state *css,
4783 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004784{
4785 return (u64)(unsigned long)current->cgroups;
4786}
4787
Tejun Heo182446d2013-08-08 20:11:24 -04004788static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08004789 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004790{
4791 u64 count;
4792
4793 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07004794 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07004795 rcu_read_unlock();
4796 return count;
4797}
4798
Tejun Heo2da8ca82013-12-05 12:28:04 -05004799static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004800{
Tejun Heo69d02062013-06-12 21:04:50 -07004801 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07004802 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05004803 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07004804
Tejun Heoe61734c2014-02-12 09:29:50 -05004805 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
4806 if (!name_buf)
4807 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07004808
Tejun Heo96d365e2014-02-13 06:58:40 -05004809 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07004810 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07004811 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07004812 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004813 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07004814
Tejun Heoa2dd4242014-03-19 10:23:55 -04004815 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004816 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04004817 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004818 }
4819 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05004820 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05004821 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004822 return 0;
4823}
4824
4825#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05004826static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004827{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004828 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07004829 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07004830
Tejun Heo96d365e2014-02-13 06:58:40 -05004831 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04004832 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004833 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004834 struct task_struct *task;
4835 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05004836
Tejun Heo5abb8852013-06-12 21:04:49 -07004837 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05004838
Tejun Heo5abb8852013-06-12 21:04:49 -07004839 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05004840 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
4841 goto overflow;
4842 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07004843 }
Tejun Heoc7561122014-02-25 10:04:01 -05004844
4845 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
4846 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
4847 goto overflow;
4848 seq_printf(seq, " task %d\n", task_pid_vnr(task));
4849 }
4850 continue;
4851 overflow:
4852 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07004853 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004854 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07004855 return 0;
4856}
4857
Tejun Heo182446d2013-08-08 20:11:24 -04004858static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004859{
Tejun Heo182446d2013-08-08 20:11:24 -04004860 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07004861}
4862
4863static struct cftype debug_files[] = {
4864 {
Paul Menagefe693432009-09-23 15:56:20 -07004865 .name = "taskcount",
4866 .read_u64 = debug_taskcount_read,
4867 },
4868
4869 {
4870 .name = "current_css_set",
4871 .read_u64 = current_css_set_read,
4872 },
4873
4874 {
4875 .name = "current_css_set_refcount",
4876 .read_u64 = current_css_set_refcount_read,
4877 },
4878
4879 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004880 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004881 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004882 },
4883
4884 {
4885 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004886 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004887 },
4888
4889 {
Paul Menagefe693432009-09-23 15:56:20 -07004890 .name = "releasable",
4891 .read_u64 = releasable_read,
4892 },
Paul Menagefe693432009-09-23 15:56:20 -07004893
Tejun Heo4baf6e32012-04-01 12:09:55 -07004894 { } /* terminate */
4895};
Paul Menagefe693432009-09-23 15:56:20 -07004896
Tejun Heo073219e2014-02-08 10:36:58 -05004897struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08004898 .css_alloc = debug_css_alloc,
4899 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07004900 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07004901};
4902#endif /* CONFIG_CGROUP_DEBUG */