blob: 0f986f7afee4a4f9da5bcd9a807a3f8b159ad6cb [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 Heof8f22e52014-04-23 11:13:16 -0400185static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss);
186static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400187static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
188 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500189static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800190
Tejun Heo95109b62013-08-08 20:11:27 -0400191/**
192 * cgroup_css - obtain a cgroup's css for the specified subsystem
193 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400194 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400195 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400196 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
197 * function must be called either under cgroup_mutex or rcu_read_lock() and
198 * the caller is responsible for pinning the returned css if it wants to
199 * keep accessing it outside the said locks. This function may return
200 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400201 */
202static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400203 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400204{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400205 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500206 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500207 lockdep_is_held(&cgroup_tree_mutex) ||
208 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400209 else
210 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400211}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700212
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400213/**
214 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
215 * @cgrp: the cgroup of interest
216 * @ss: the subsystem of interest (%NULL returns the dummy_css)
217 *
218 * Similar to cgroup_css() but returns the effctive css, which is defined
219 * as the matching css of the nearest ancestor including self which has @ss
220 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
221 * function is guaranteed to return non-NULL css.
222 */
223static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
224 struct cgroup_subsys *ss)
225{
226 lockdep_assert_held(&cgroup_mutex);
227
228 if (!ss)
229 return &cgrp->dummy_css;
230
231 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
232 return NULL;
233
234 while (cgrp->parent &&
235 !(cgrp->parent->child_subsys_mask & (1 << ss->id)))
236 cgrp = cgrp->parent;
237
238 return cgroup_css(cgrp, ss);
239}
240
Paul Menageddbcc7e2007-10-18 23:39:30 -0700241/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700242static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700243{
Tejun Heo54766d42013-06-12 21:04:53 -0700244 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700245}
246
Tejun Heo59f52962014-02-11 11:52:49 -0500247struct cgroup_subsys_state *seq_css(struct seq_file *seq)
248{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500249 struct kernfs_open_file *of = seq->private;
250 struct cgroup *cgrp = of->kn->parent->priv;
251 struct cftype *cft = seq_cft(seq);
252
253 /*
254 * This is open and unprotected implementation of cgroup_css().
255 * seq_css() is only called from a kernfs file operation which has
256 * an active reference on the file. Because all the subsystem
257 * files are drained before a css is disassociated with a cgroup,
258 * the matching css from the cgroup's subsys table is guaranteed to
259 * be and stay valid until the enclosing operation is complete.
260 */
261 if (cft->ss)
262 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
263 else
264 return &cgrp->dummy_css;
Tejun Heo59f52962014-02-11 11:52:49 -0500265}
266EXPORT_SYMBOL_GPL(seq_css);
267
Li Zefan78574cf2013-04-08 19:00:38 -0700268/**
269 * cgroup_is_descendant - test ancestry
270 * @cgrp: the cgroup to be tested
271 * @ancestor: possible ancestor of @cgrp
272 *
273 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
274 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
275 * and @ancestor are accessible.
276 */
277bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
278{
279 while (cgrp) {
280 if (cgrp == ancestor)
281 return true;
282 cgrp = cgrp->parent;
283 }
284 return false;
285}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700286
Adrian Bunke9685a02008-02-07 00:13:46 -0800287static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700288{
289 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700290 (1 << CGRP_RELEASABLE) |
291 (1 << CGRP_NOTIFY_ON_RELEASE);
292 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700293}
294
Adrian Bunke9685a02008-02-07 00:13:46 -0800295static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700296{
Paul Menagebd89aab2007-10-18 23:40:44 -0700297 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700298}
299
Tejun Heo30159ec2013-06-25 11:53:37 -0700300/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500301 * for_each_css - iterate all css's of a cgroup
302 * @css: the iteration cursor
303 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
304 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700305 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400306 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700307 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500308#define for_each_css(css, ssid, cgrp) \
309 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
310 if (!((css) = rcu_dereference_check( \
311 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500312 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500313 lockdep_is_held(&cgroup_mutex)))) { } \
314 else
315
316/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400317 * for_each_e_css - iterate all effective css's of a cgroup
318 * @css: the iteration cursor
319 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
320 * @cgrp: the target cgroup to iterate css's of
321 *
322 * Should be called under cgroup_[tree_]mutex.
323 */
324#define for_each_e_css(css, ssid, cgrp) \
325 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
326 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
327 ; \
328 else
329
330/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500331 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700332 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500333 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700334 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500335#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500336 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
337 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700338
Tejun Heo985ed672014-03-19 10:23:53 -0400339/* iterate across the hierarchies */
340#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700341 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700342
Tejun Heof8f22e52014-04-23 11:13:16 -0400343/* iterate over child cgrps, lock should be held throughout iteration */
344#define cgroup_for_each_live_child(child, cgrp) \
345 list_for_each_entry((child), &(cgrp)->children, sibling) \
346 if (({ lockdep_assert_held(&cgroup_tree_mutex); \
347 cgroup_is_dead(child); })) \
348 ; \
349 else
350
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700351/**
352 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
353 * @cgrp: the cgroup to be checked for liveness
354 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700355 * On success, returns true; the mutex should be later unlocked. On
356 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700357 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700358static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700359{
360 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700361 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700362 mutex_unlock(&cgroup_mutex);
363 return false;
364 }
365 return true;
366}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700367
Paul Menage81a6a5c2007-10-18 23:39:38 -0700368/* the list of cgroups eligible for automatic release. Protected by
369 * release_list_lock */
370static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200371static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700372static void cgroup_release_agent(struct work_struct *work);
373static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700374static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700375
Tejun Heo69d02062013-06-12 21:04:50 -0700376/*
377 * A cgroup can be associated with multiple css_sets as different tasks may
378 * belong to different cgroups on different hierarchies. In the other
379 * direction, a css_set is naturally associated with multiple cgroups.
380 * This M:N relationship is represented by the following link structure
381 * which exists for each association and allows traversing the associations
382 * from both sides.
383 */
384struct cgrp_cset_link {
385 /* the cgroup and css_set this link associates */
386 struct cgroup *cgrp;
387 struct css_set *cset;
388
389 /* list of cgrp_cset_links anchored at cgrp->cset_links */
390 struct list_head cset_link;
391
392 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
393 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700394};
395
Tejun Heo172a2c062014-03-19 10:23:53 -0400396/*
397 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700398 * hierarchies being mounted. It contains a pointer to the root state
399 * for each subsystem. Also used to anchor the list of css_sets. Not
400 * reference-counted, to improve performance when child cgroups
401 * haven't been created.
402 */
Tejun Heo172a2c062014-03-19 10:23:53 -0400403static struct css_set init_css_set = {
404 .refcount = ATOMIC_INIT(1),
405 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
406 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
407 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
408 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
409 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
410};
Paul Menage817929e2007-10-18 23:39:36 -0700411
Tejun Heo172a2c062014-03-19 10:23:53 -0400412static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700413
Tejun Heo842b5972014-04-25 18:28:02 -0400414/**
415 * cgroup_update_populated - updated populated count of a cgroup
416 * @cgrp: the target cgroup
417 * @populated: inc or dec populated count
418 *
419 * @cgrp is either getting the first task (css_set) or losing the last.
420 * Update @cgrp->populated_cnt accordingly. The count is propagated
421 * towards root so that a given cgroup's populated_cnt is zero iff the
422 * cgroup and all its descendants are empty.
423 *
424 * @cgrp's interface file "cgroup.populated" is zero if
425 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
426 * changes from or to zero, userland is notified that the content of the
427 * interface file has changed. This can be used to detect when @cgrp and
428 * its descendants become populated or empty.
429 */
430static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
431{
432 lockdep_assert_held(&css_set_rwsem);
433
434 do {
435 bool trigger;
436
437 if (populated)
438 trigger = !cgrp->populated_cnt++;
439 else
440 trigger = !--cgrp->populated_cnt;
441
442 if (!trigger)
443 break;
444
445 if (cgrp->populated_kn)
446 kernfs_notify(cgrp->populated_kn);
447 cgrp = cgrp->parent;
448 } while (cgrp);
449}
450
Paul Menage7717f7b2009-09-23 15:56:22 -0700451/*
452 * hash table for cgroup groups. This improves the performance to find
453 * an existing css_set. This hash doesn't (currently) take into
454 * account cgroups in empty hierarchies.
455 */
Li Zefan472b1052008-04-29 01:00:11 -0700456#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800457static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700458
Li Zefan0ac801f2013-01-10 11:49:27 +0800459static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700460{
Li Zefan0ac801f2013-01-10 11:49:27 +0800461 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700462 struct cgroup_subsys *ss;
463 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700464
Tejun Heo30159ec2013-06-25 11:53:37 -0700465 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800466 key += (unsigned long)css[i];
467 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700468
Li Zefan0ac801f2013-01-10 11:49:27 +0800469 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700470}
471
Tejun Heo89c55092014-02-13 06:58:40 -0500472static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700473{
Tejun Heo69d02062013-06-12 21:04:50 -0700474 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400475 struct cgroup_subsys *ss;
476 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700477
Tejun Heo89c55092014-02-13 06:58:40 -0500478 lockdep_assert_held(&css_set_rwsem);
479
480 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700481 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700482
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700483 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400484 for_each_subsys(ss, ssid)
485 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700486 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700487 css_set_count--;
488
Tejun Heo69d02062013-06-12 21:04:50 -0700489 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700490 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700491
Tejun Heo69d02062013-06-12 21:04:50 -0700492 list_del(&link->cset_link);
493 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800494
Tejun Heo96d365e2014-02-13 06:58:40 -0500495 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400496 if (list_empty(&cgrp->cset_links)) {
497 cgroup_update_populated(cgrp, false);
498 if (notify_on_release(cgrp)) {
499 if (taskexit)
500 set_bit(CGRP_RELEASABLE, &cgrp->flags);
501 check_for_release(cgrp);
502 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700503 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700504
505 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700506 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700507
Tejun Heo5abb8852013-06-12 21:04:49 -0700508 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700509}
510
Tejun Heo89c55092014-02-13 06:58:40 -0500511static void put_css_set(struct css_set *cset, bool taskexit)
512{
513 /*
514 * Ensure that the refcount doesn't hit zero while any readers
515 * can see it. Similar to atomic_dec_and_lock(), but for an
516 * rwlock
517 */
518 if (atomic_add_unless(&cset->refcount, -1, 1))
519 return;
520
521 down_write(&css_set_rwsem);
522 put_css_set_locked(cset, taskexit);
523 up_write(&css_set_rwsem);
524}
525
Paul Menage817929e2007-10-18 23:39:36 -0700526/*
527 * refcounted get/put for css_set objects
528 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700529static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700530{
Tejun Heo5abb8852013-06-12 21:04:49 -0700531 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700532}
533
Tejun Heob326f9d2013-06-24 15:21:48 -0700534/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700535 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700536 * @cset: candidate css_set being tested
537 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700538 * @new_cgrp: cgroup that's being entered by the task
539 * @template: desired set of css pointers in css_set (pre-calculated)
540 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800541 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700542 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
543 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700544static bool compare_css_sets(struct css_set *cset,
545 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700546 struct cgroup *new_cgrp,
547 struct cgroup_subsys_state *template[])
548{
549 struct list_head *l1, *l2;
550
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400551 /*
552 * On the default hierarchy, there can be csets which are
553 * associated with the same set of cgroups but different csses.
554 * Let's first ensure that csses match.
555 */
556 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700557 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700558
559 /*
560 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400561 * different cgroups in hierarchies. As different cgroups may
562 * share the same effective css, this comparison is always
563 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700564 */
Tejun Heo69d02062013-06-12 21:04:50 -0700565 l1 = &cset->cgrp_links;
566 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700567 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700568 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700569 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700570
571 l1 = l1->next;
572 l2 = l2->next;
573 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700574 if (l1 == &cset->cgrp_links) {
575 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700576 break;
577 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700578 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700579 }
580 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700581 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
582 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
583 cgrp1 = link1->cgrp;
584 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700585 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700586 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700587
588 /*
589 * If this hierarchy is the hierarchy of the cgroup
590 * that's changing, then we need to check that this
591 * css_set points to the new cgroup; if it's any other
592 * hierarchy, then this css_set should point to the
593 * same cgroup as the old css_set.
594 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700595 if (cgrp1->root == new_cgrp->root) {
596 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700597 return false;
598 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700599 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700600 return false;
601 }
602 }
603 return true;
604}
605
Tejun Heob326f9d2013-06-24 15:21:48 -0700606/**
607 * find_existing_css_set - init css array and find the matching css_set
608 * @old_cset: the css_set that we're using before the cgroup transition
609 * @cgrp: the cgroup that we're moving into
610 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700611 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700612static struct css_set *find_existing_css_set(struct css_set *old_cset,
613 struct cgroup *cgrp,
614 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700615{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400616 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700617 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700618 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800619 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700620 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700621
Ben Blumaae8aab2010-03-10 15:22:07 -0800622 /*
623 * Build the set of subsystem state objects that we want to see in the
624 * new css_set. while subsystems can change globally, the entries here
625 * won't change, so no need for locking.
626 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700627 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400628 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400629 /*
630 * @ss is in this hierarchy, so we want the
631 * effective css from @cgrp.
632 */
633 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700634 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400635 /*
636 * @ss is not in this hierarchy, so we don't want
637 * to change the css.
638 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700639 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700640 }
641 }
642
Li Zefan0ac801f2013-01-10 11:49:27 +0800643 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700644 hash_for_each_possible(css_set_table, cset, hlist, key) {
645 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700646 continue;
647
648 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700649 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700650 }
Paul Menage817929e2007-10-18 23:39:36 -0700651
652 /* No existing cgroup group matched */
653 return NULL;
654}
655
Tejun Heo69d02062013-06-12 21:04:50 -0700656static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700657{
Tejun Heo69d02062013-06-12 21:04:50 -0700658 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700659
Tejun Heo69d02062013-06-12 21:04:50 -0700660 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
661 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700662 kfree(link);
663 }
664}
665
Tejun Heo69d02062013-06-12 21:04:50 -0700666/**
667 * allocate_cgrp_cset_links - allocate cgrp_cset_links
668 * @count: the number of links to allocate
669 * @tmp_links: list_head the allocated links are put on
670 *
671 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
672 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700673 */
Tejun Heo69d02062013-06-12 21:04:50 -0700674static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700675{
Tejun Heo69d02062013-06-12 21:04:50 -0700676 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700677 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700678
679 INIT_LIST_HEAD(tmp_links);
680
Li Zefan36553432008-07-29 22:33:19 -0700681 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700682 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700683 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700684 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700685 return -ENOMEM;
686 }
Tejun Heo69d02062013-06-12 21:04:50 -0700687 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700688 }
689 return 0;
690}
691
Li Zefanc12f65d2009-01-07 18:07:42 -0800692/**
693 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700694 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700695 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800696 * @cgrp: the destination cgroup
697 */
Tejun Heo69d02062013-06-12 21:04:50 -0700698static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
699 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800700{
Tejun Heo69d02062013-06-12 21:04:50 -0700701 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800702
Tejun Heo69d02062013-06-12 21:04:50 -0700703 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400704
705 if (cgroup_on_dfl(cgrp))
706 cset->dfl_cgrp = cgrp;
707
Tejun Heo69d02062013-06-12 21:04:50 -0700708 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
709 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700710 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400711
712 if (list_empty(&cgrp->cset_links))
713 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700714 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400715
Paul Menage7717f7b2009-09-23 15:56:22 -0700716 /*
717 * Always add links to the tail of the list so that the list
718 * is sorted by order of hierarchy creation
719 */
Tejun Heo69d02062013-06-12 21:04:50 -0700720 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800721}
722
Tejun Heob326f9d2013-06-24 15:21:48 -0700723/**
724 * find_css_set - return a new css_set with one cgroup updated
725 * @old_cset: the baseline css_set
726 * @cgrp: the cgroup to be updated
727 *
728 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
729 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700730 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700731static struct css_set *find_css_set(struct css_set *old_cset,
732 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700733{
Tejun Heob326f9d2013-06-24 15:21:48 -0700734 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700735 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700736 struct list_head tmp_links;
737 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400738 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800739 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400740 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700741
Tejun Heob326f9d2013-06-24 15:21:48 -0700742 lockdep_assert_held(&cgroup_mutex);
743
Paul Menage817929e2007-10-18 23:39:36 -0700744 /* First see if we already have a cgroup group that matches
745 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500746 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700747 cset = find_existing_css_set(old_cset, cgrp, template);
748 if (cset)
749 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500750 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700751
Tejun Heo5abb8852013-06-12 21:04:49 -0700752 if (cset)
753 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700754
Tejun Heof4f4be22013-06-12 21:04:51 -0700755 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700756 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700757 return NULL;
758
Tejun Heo69d02062013-06-12 21:04:50 -0700759 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700760 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700761 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700762 return NULL;
763 }
764
Tejun Heo5abb8852013-06-12 21:04:49 -0700765 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700766 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700767 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500768 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500769 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500770 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700771 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700772
773 /* Copy the set of subsystem state objects generated in
774 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700775 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700776
Tejun Heo96d365e2014-02-13 06:58:40 -0500777 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700778 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700779 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700780 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700781
Paul Menage7717f7b2009-09-23 15:56:22 -0700782 if (c->root == cgrp->root)
783 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700784 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700785 }
Paul Menage817929e2007-10-18 23:39:36 -0700786
Tejun Heo69d02062013-06-12 21:04:50 -0700787 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700788
Paul Menage817929e2007-10-18 23:39:36 -0700789 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700790
Tejun Heo2d8f2432014-04-23 11:13:15 -0400791 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700792 key = css_set_hash(cset->subsys);
793 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700794
Tejun Heo2d8f2432014-04-23 11:13:15 -0400795 for_each_subsys(ss, ssid)
796 list_add_tail(&cset->e_cset_node[ssid],
797 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
798
Tejun Heo96d365e2014-02-13 06:58:40 -0500799 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700800
Tejun Heo5abb8852013-06-12 21:04:49 -0700801 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700802}
803
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400804static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700805{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400806 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500807
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400808 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500809}
810
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400811static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500812{
813 int id;
814
815 lockdep_assert_held(&cgroup_mutex);
816
Tejun Heo985ed672014-03-19 10:23:53 -0400817 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500818 if (id < 0)
819 return id;
820
821 root->hierarchy_id = id;
822 return 0;
823}
824
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400825static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500826{
827 lockdep_assert_held(&cgroup_mutex);
828
829 if (root->hierarchy_id) {
830 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
831 root->hierarchy_id = 0;
832 }
833}
834
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400835static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500836{
837 if (root) {
838 /* hierarhcy ID shoulid already have been released */
839 WARN_ON_ONCE(root->hierarchy_id);
840
841 idr_destroy(&root->cgroup_idr);
842 kfree(root);
843 }
844}
845
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400846static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500847{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400848 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500849 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500850
Tejun Heo2bd59d42014-02-11 11:52:49 -0500851 mutex_lock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500852 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500853
Tejun Heo776f02f2014-02-12 09:29:50 -0500854 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500855 BUG_ON(!list_empty(&cgrp->children));
856
Tejun Heof2e85d52014-02-11 11:52:49 -0500857 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400858 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500859
860 /*
861 * Release all the links from cset_links to this hierarchy's
862 * root cgroup
863 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500864 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500865
866 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
867 list_del(&link->cset_link);
868 list_del(&link->cgrp_link);
869 kfree(link);
870 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500871 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500872
873 if (!list_empty(&root->root_list)) {
874 list_del(&root->root_list);
875 cgroup_root_count--;
876 }
877
878 cgroup_exit_root_id(root);
879
880 mutex_unlock(&cgroup_mutex);
881 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500882
Tejun Heo2bd59d42014-02-11 11:52:49 -0500883 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500884 cgroup_free_root(root);
885}
886
Tejun Heoceb6a082014-02-25 10:04:02 -0500887/* look up cgroup associated with given css_set on the specified hierarchy */
888static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400889 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700890{
Paul Menage7717f7b2009-09-23 15:56:22 -0700891 struct cgroup *res = NULL;
892
Tejun Heo96d365e2014-02-13 06:58:40 -0500893 lockdep_assert_held(&cgroup_mutex);
894 lockdep_assert_held(&css_set_rwsem);
895
Tejun Heo5abb8852013-06-12 21:04:49 -0700896 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400897 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700898 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700899 struct cgrp_cset_link *link;
900
901 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700902 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700903
Paul Menage7717f7b2009-09-23 15:56:22 -0700904 if (c->root == root) {
905 res = c;
906 break;
907 }
908 }
909 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500910
Paul Menage7717f7b2009-09-23 15:56:22 -0700911 BUG_ON(!res);
912 return res;
913}
914
915/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500916 * Return the cgroup for "task" from the given hierarchy. Must be
917 * called with cgroup_mutex and css_set_rwsem held.
918 */
919static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400920 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500921{
922 /*
923 * No need to lock the task - since we hold cgroup_mutex the
924 * task can't change groups, so the only thing that can happen
925 * is that it exits and its css is set back to init_css_set.
926 */
927 return cset_cgroup_from_root(task_css_set(task), root);
928}
929
930/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700931 * A task must hold cgroup_mutex to modify cgroups.
932 *
933 * Any task can increment and decrement the count field without lock.
934 * So in general, code holding cgroup_mutex can't rely on the count
935 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800936 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700937 * means that no tasks are currently attached, therefore there is no
938 * way a task attached to that cgroup can fork (the other way to
939 * increment the count). So code holding cgroup_mutex can safely
940 * assume that if the count is zero, it will stay zero. Similarly, if
941 * a task holds cgroup_mutex on a cgroup with zero count, it
942 * knows that the cgroup won't be removed, as cgroup_rmdir()
943 * needs that mutex.
944 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700945 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
946 * (usually) take cgroup_mutex. These are the two most performance
947 * critical pieces of code here. The exception occurs on cgroup_exit(),
948 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
949 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800950 * to the release agent with the name of the cgroup (path relative to
951 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700952 *
953 * A cgroup can only be deleted if both its 'count' of using tasks
954 * is zero, and its list of 'children' cgroups is empty. Since all
955 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400956 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700957 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400958 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700959 *
960 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800961 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700962 */
963
Tejun Heo628f7cd2013-06-28 16:24:11 -0700964static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500965static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700966static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700967
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500968static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
969 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700970{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500971 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
972 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
973 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
974 cft->ss->name, cft->name);
975 else
976 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
977 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700978}
979
Tejun Heof2e85d52014-02-11 11:52:49 -0500980/**
981 * cgroup_file_mode - deduce file mode of a control file
982 * @cft: the control file in question
983 *
984 * returns cft->mode if ->mode is not 0
985 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
986 * returns S_IRUGO if it has only a read handler
987 * returns S_IWUSR if it has only a write hander
988 */
989static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +0800990{
Tejun Heof2e85d52014-02-11 11:52:49 -0500991 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +0800992
Tejun Heof2e85d52014-02-11 11:52:49 -0500993 if (cft->mode)
994 return cft->mode;
995
996 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
997 mode |= S_IRUGO;
998
999 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
1000 cft->trigger)
1001 mode |= S_IWUSR;
1002
1003 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001004}
1005
Li Zefanbe445622013-01-24 14:31:42 +08001006static void cgroup_free_fn(struct work_struct *work)
1007{
Tejun Heoea15f8c2013-06-13 19:27:42 -07001008 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001009
Tejun Heo3c9c8252014-02-12 09:29:50 -05001010 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -05001011 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +08001012
Tejun Heo776f02f2014-02-12 09:29:50 -05001013 if (cgrp->parent) {
1014 /*
1015 * We get a ref to the parent, and put the ref when this
1016 * cgroup is being freed, so it's guaranteed that the
1017 * parent won't be destroyed before its children.
1018 */
1019 cgroup_put(cgrp->parent);
1020 kernfs_put(cgrp->kn);
1021 kfree(cgrp);
1022 } else {
1023 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001024 * This is root cgroup's refcnt reaching zero, which
Tejun Heo776f02f2014-02-12 09:29:50 -05001025 * indicates that the root should be released.
1026 */
1027 cgroup_destroy_root(cgrp->root);
1028 }
Li Zefanbe445622013-01-24 14:31:42 +08001029}
1030
1031static void cgroup_free_rcu(struct rcu_head *head)
1032{
1033 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
1034
Tejun Heoea15f8c2013-06-13 19:27:42 -07001035 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05001036 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001037}
1038
Tejun Heo59f52962014-02-11 11:52:49 -05001039static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001040{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001041 WARN_ON_ONCE(cgroup_is_dead(cgrp));
1042 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
1043 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001044}
1045
Tejun Heo59f52962014-02-11 11:52:49 -05001046static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001048 if (!atomic_dec_and_test(&cgrp->refcnt))
1049 return;
Tejun Heo776f02f2014-02-12 09:29:50 -05001050 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -05001051 return;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052
Tejun Heo2bd59d42014-02-11 11:52:49 -05001053 /*
1054 * XXX: cgrp->id is only used to look up css's. As cgroup and
1055 * css's lifetimes will be decoupled, it should be made
1056 * per-subsystem and moved to css->id so that lookups are
1057 * successful until the target css is released.
1058 */
1059 mutex_lock(&cgroup_mutex);
1060 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
1061 mutex_unlock(&cgroup_mutex);
1062 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063
Tejun Heo2bd59d42014-02-11 11:52:49 -05001064 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065}
1066
Li Zefan2739d3c2013-01-21 18:18:33 +08001067static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001068{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001069 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070
Tejun Heoace2bee2014-02-11 11:52:47 -05001071 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001072 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001073}
1074
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001075/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001076 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001077 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001078 * @subsys_mask: mask of the subsystem ids whose files should be removed
1079 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07001080static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001081{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001082 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001083 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001084
Tejun Heob420ba72013-07-12 12:34:02 -07001085 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001086 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001087
1088 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001089 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001090 list_for_each_entry(cfts, &ss->cfts, node)
1091 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001092 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093}
1094
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001095static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo5df36032014-03-19 10:23:54 -04001096 unsigned long ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097{
Tejun Heo30159ec2013-06-25 11:53:37 -07001098 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001099 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100
Tejun Heoace2bee2014-02-11 11:52:47 -05001101 lockdep_assert_held(&cgroup_tree_mutex);
1102 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001103
Tejun Heo5df36032014-03-19 10:23:54 -04001104 for_each_subsys(ss, ssid) {
1105 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001107
Tejun Heo7fd8c562014-04-23 11:13:16 -04001108 /* if @ss has non-root csses attached to it, can't move */
1109 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001110 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001111
Tejun Heo5df36032014-03-19 10:23:54 -04001112 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001113 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001114 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001115 }
1116
Tejun Heoa2dd4242014-03-19 10:23:55 -04001117 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1118 if (ret) {
1119 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001120 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001121
Tejun Heoa2dd4242014-03-19 10:23:55 -04001122 /*
1123 * Rebinding back to the default root is not allowed to
1124 * fail. Using both default and non-default roots should
1125 * be rare. Moving subsystems back and forth even more so.
1126 * Just warn about it and continue.
1127 */
1128 if (cgrp_dfl_root_visible) {
1129 pr_warning("cgroup: failed to create files (%d) while rebinding 0x%lx to default root\n",
1130 ret, ss_mask);
1131 pr_warning("cgroup: you may retry by moving them to a different hierarchy and unbinding\n");
1132 }
Tejun Heo5df36032014-03-19 10:23:54 -04001133 }
Tejun Heo31261212013-06-28 17:07:30 -07001134
1135 /*
1136 * Nothing can fail from this point on. Remove files for the
1137 * removed subsystems and rebind each subsystem.
1138 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001139 mutex_unlock(&cgroup_mutex);
Tejun Heo5df36032014-03-19 10:23:54 -04001140 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001141 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001142 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo4ac06012014-02-11 11:52:47 -05001143 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001144
Tejun Heo5df36032014-03-19 10:23:54 -04001145 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001146 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001147 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001148 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001149
Tejun Heo5df36032014-03-19 10:23:54 -04001150 if (!(ss_mask & (1 << ssid)))
1151 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001152
Tejun Heo5df36032014-03-19 10:23:54 -04001153 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001154 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001155
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001156 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001157
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001158 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1159 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001160 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001161 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001162
Tejun Heo2d8f2432014-04-23 11:13:15 -04001163 down_write(&css_set_rwsem);
1164 hash_for_each(css_set_table, i, cset, hlist)
1165 list_move_tail(&cset->e_cset_node[ss->id],
1166 &dst_root->cgrp.e_csets[ss->id]);
1167 up_write(&css_set_rwsem);
1168
Tejun Heof392e512014-04-23 11:13:14 -04001169 src_root->subsys_mask &= ~(1 << ssid);
1170 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1171
Tejun Heobd53d612014-04-23 11:13:16 -04001172 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001173 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001174 if (dst_root != &cgrp_dfl_root)
1175 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001176
Tejun Heo5df36032014-03-19 10:23:54 -04001177 if (ss->bind)
1178 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001179 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001180
Tejun Heoa2dd4242014-03-19 10:23:55 -04001181 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001182 return 0;
1183}
1184
Tejun Heo2bd59d42014-02-11 11:52:49 -05001185static int cgroup_show_options(struct seq_file *seq,
1186 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001187{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001188 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001189 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001190 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001191
Tejun Heob85d2042013-12-06 15:11:57 -05001192 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001193 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001194 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001195 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1196 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001197 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001198 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001199 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001200 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001201
1202 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001203 if (strlen(root->release_agent_path))
1204 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001205 spin_unlock(&release_agent_path_lock);
1206
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001207 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001208 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001209 if (strlen(root->name))
1210 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001211 return 0;
1212}
1213
1214struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001215 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001216 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001217 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001218 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001219 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001220 /* User explicitly requested empty subsystem */
1221 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001222};
1223
Ben Blumaae8aab2010-03-10 15:22:07 -08001224/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001225 * Convert a hierarchy specifier into a bitmask of subsystems and
1226 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1227 * array. This function takes refcounts on subsystems to be used, unless it
1228 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001229 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001230static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001231{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001232 char *token, *o = data;
1233 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001234 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001235 struct cgroup_subsys *ss;
1236 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001237
Ben Blumaae8aab2010-03-10 15:22:07 -08001238 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1239
Li Zefanf9ab5b52009-06-17 16:26:33 -07001240#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001241 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001242#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001243
Paul Menagec6d57f32009-09-23 15:56:19 -07001244 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001245
1246 while ((token = strsep(&o, ",")) != NULL) {
1247 if (!*token)
1248 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001249 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001250 /* Explicitly have no subsystems */
1251 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001252 continue;
1253 }
1254 if (!strcmp(token, "all")) {
1255 /* Mutually exclusive option 'all' + subsystem name */
1256 if (one_ss)
1257 return -EINVAL;
1258 all_ss = true;
1259 continue;
1260 }
Tejun Heo873fe092013-04-14 20:15:26 -07001261 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1262 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1263 continue;
1264 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001265 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001266 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001267 continue;
1268 }
1269 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001270 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001271 continue;
1272 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001273 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001274 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001275 continue;
1276 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001277 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001278 /* Specifying two release agents is forbidden */
1279 if (opts->release_agent)
1280 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001281 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001282 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001283 if (!opts->release_agent)
1284 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001285 continue;
1286 }
1287 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001288 const char *name = token + 5;
1289 /* Can't specify an empty name */
1290 if (!strlen(name))
1291 return -EINVAL;
1292 /* Must match [\w.-]+ */
1293 for (i = 0; i < strlen(name); i++) {
1294 char c = name[i];
1295 if (isalnum(c))
1296 continue;
1297 if ((c == '.') || (c == '-') || (c == '_'))
1298 continue;
1299 return -EINVAL;
1300 }
1301 /* Specifying two names is forbidden */
1302 if (opts->name)
1303 return -EINVAL;
1304 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001305 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001306 GFP_KERNEL);
1307 if (!opts->name)
1308 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001309
1310 continue;
1311 }
1312
Tejun Heo30159ec2013-06-25 11:53:37 -07001313 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001314 if (strcmp(token, ss->name))
1315 continue;
1316 if (ss->disabled)
1317 continue;
1318
1319 /* Mutually exclusive option 'all' + subsystem name */
1320 if (all_ss)
1321 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001322 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001323 one_ss = true;
1324
1325 break;
1326 }
1327 if (i == CGROUP_SUBSYS_COUNT)
1328 return -ENOENT;
1329 }
1330
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001331 /* Consistency checks */
1332
Tejun Heo873fe092013-04-14 20:15:26 -07001333 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1334 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1335
Tejun Heod3ba07c2014-02-13 06:58:38 -05001336 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1337 opts->cpuset_clone_children || opts->release_agent ||
1338 opts->name) {
1339 pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001340 return -EINVAL;
1341 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001342 } else {
1343 /*
1344 * If the 'all' option was specified select all the
1345 * subsystems, otherwise if 'none', 'name=' and a subsystem
1346 * name options were not specified, let's default to 'all'
1347 */
1348 if (all_ss || (!one_ss && !opts->none && !opts->name))
1349 for_each_subsys(ss, i)
1350 if (!ss->disabled)
1351 set_bit(i, &opts->subsys_mask);
Tejun Heo873fe092013-04-14 20:15:26 -07001352
Tejun Heoa2dd4242014-03-19 10:23:55 -04001353 /*
1354 * We either have to specify by name or by subsystems. (So
1355 * all empty hierarchies must have a name).
1356 */
1357 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001358 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001359 }
1360
Li Zefanf9ab5b52009-06-17 16:26:33 -07001361 /*
1362 * Option noprefix was introduced just for backward compatibility
1363 * with the old cpuset, so we allow noprefix only if mounting just
1364 * the cpuset subsystem.
1365 */
Tejun Heo93438622013-04-14 20:15:25 -07001366 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001367 return -EINVAL;
1368
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001369
1370 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001371 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001372 return -EINVAL;
1373
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374 return 0;
1375}
1376
Tejun Heo2bd59d42014-02-11 11:52:49 -05001377static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378{
1379 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001380 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001381 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001382 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383
Tejun Heo873fe092013-04-14 20:15:26 -07001384 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1385 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1386 return -EINVAL;
1387 }
1388
Tejun Heoace2bee2014-02-11 11:52:47 -05001389 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390 mutex_lock(&cgroup_mutex);
1391
1392 /* See what subsystems are wanted */
1393 ret = parse_cgroupfs_options(data, &opts);
1394 if (ret)
1395 goto out_unlock;
1396
Tejun Heof392e512014-04-23 11:13:14 -04001397 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001398 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1399 task_tgid_nr(current), current->comm);
1400
Tejun Heof392e512014-04-23 11:13:14 -04001401 added_mask = opts.subsys_mask & ~root->subsys_mask;
1402 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001403
Ben Blumcf5d5942010-03-10 15:22:09 -08001404 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001405 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001406 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001407 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1408 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1409 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001410 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001411 goto out_unlock;
1412 }
1413
Tejun Heof172e672013-06-28 17:07:30 -07001414 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001415 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001416 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001417 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001418 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001419
Tejun Heo5df36032014-03-19 10:23:54 -04001420 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001421 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001422 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001423
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001424 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001425
Tejun Heo69e943b2014-02-08 10:36:58 -05001426 if (opts.release_agent) {
1427 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001428 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001429 spin_unlock(&release_agent_path_lock);
1430 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001431 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001432 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001433 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001434 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001435 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001436 return ret;
1437}
1438
Tejun Heoafeb0f92014-02-13 06:58:39 -05001439/*
1440 * To reduce the fork() overhead for systems that are not actually using
1441 * their cgroups capability, we don't maintain the lists running through
1442 * each css_set to its tasks until we see the list actually used - in other
1443 * words after the first mount.
1444 */
1445static bool use_task_css_set_links __read_mostly;
1446
1447static void cgroup_enable_task_cg_lists(void)
1448{
1449 struct task_struct *p, *g;
1450
Tejun Heo96d365e2014-02-13 06:58:40 -05001451 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001452
1453 if (use_task_css_set_links)
1454 goto out_unlock;
1455
1456 use_task_css_set_links = true;
1457
1458 /*
1459 * We need tasklist_lock because RCU is not safe against
1460 * while_each_thread(). Besides, a forking task that has passed
1461 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1462 * is not guaranteed to have its child immediately visible in the
1463 * tasklist if we walk through it with RCU.
1464 */
1465 read_lock(&tasklist_lock);
1466 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001467 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1468 task_css_set(p) != &init_css_set);
1469
1470 /*
1471 * We should check if the process is exiting, otherwise
1472 * it will race with cgroup_exit() in that the list
1473 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001474 * Do it while holding siglock so that we don't end up
1475 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001476 */
Tejun Heof153ad12014-02-25 09:56:49 -05001477 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001478 if (!(p->flags & PF_EXITING)) {
1479 struct css_set *cset = task_css_set(p);
1480
1481 list_add(&p->cg_list, &cset->tasks);
1482 get_css_set(cset);
1483 }
Tejun Heof153ad12014-02-25 09:56:49 -05001484 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001485 } while_each_thread(g, p);
1486 read_unlock(&tasklist_lock);
1487out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001488 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001489}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001490
Paul Menagecc31edc2008-10-18 20:28:04 -07001491static void init_cgroup_housekeeping(struct cgroup *cgrp)
1492{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001493 struct cgroup_subsys *ss;
1494 int ssid;
1495
Tejun Heo2bd59d42014-02-11 11:52:49 -05001496 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001497 INIT_LIST_HEAD(&cgrp->sibling);
1498 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001499 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001500 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001501 INIT_LIST_HEAD(&cgrp->pidlists);
1502 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001503 cgrp->dummy_css.cgroup = cgrp;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001504
1505 for_each_subsys(ss, ssid)
1506 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001507
1508 init_waitqueue_head(&cgrp->offline_waitq);
Paul Menagecc31edc2008-10-18 20:28:04 -07001509}
Paul Menagec6d57f32009-09-23 15:56:19 -07001510
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001511static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001512 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001514 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001515
Paul Menageddbcc7e2007-10-18 23:39:30 -07001516 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001517 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001518 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001519 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001520 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521
Paul Menagec6d57f32009-09-23 15:56:19 -07001522 root->flags = opts->flags;
1523 if (opts->release_agent)
1524 strcpy(root->release_agent_path, opts->release_agent);
1525 if (opts->name)
1526 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001527 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001528 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001529}
1530
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001531static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001532{
Tejun Heod427dfe2014-02-11 11:52:48 -05001533 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001534 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001535 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001536 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001537
Tejun Heod427dfe2014-02-11 11:52:48 -05001538 lockdep_assert_held(&cgroup_tree_mutex);
1539 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001540
Tejun Heod427dfe2014-02-11 11:52:48 -05001541 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1542 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001543 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001544 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001545
Tejun Heod427dfe2014-02-11 11:52:48 -05001546 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001547 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001548 * but that's OK - it can only be increased by someone holding
1549 * cgroup_lock, and that's us. The worst that can happen is that we
1550 * have some link structures left over
1551 */
1552 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001554 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555
Tejun Heo985ed672014-03-19 10:23:53 -04001556 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001557 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001558 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559
Tejun Heo2bd59d42014-02-11 11:52:49 -05001560 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1561 KERNFS_ROOT_CREATE_DEACTIVATED,
1562 root_cgrp);
1563 if (IS_ERR(root->kf_root)) {
1564 ret = PTR_ERR(root->kf_root);
1565 goto exit_root_id;
1566 }
1567 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568
Tejun Heod427dfe2014-02-11 11:52:48 -05001569 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1570 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001571 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001572
Tejun Heo5df36032014-03-19 10:23:54 -04001573 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001574 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001575 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001576
Tejun Heod427dfe2014-02-11 11:52:48 -05001577 /*
1578 * There must be no failure case after here, since rebinding takes
1579 * care of subsystems' refcounts, which are explicitly dropped in
1580 * the failure exit path.
1581 */
1582 list_add(&root->root_list, &cgroup_roots);
1583 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584
Tejun Heod427dfe2014-02-11 11:52:48 -05001585 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001586 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001587 * objects.
1588 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001589 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001590 hash_for_each(css_set_table, i, cset, hlist)
1591 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001592 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001593
Tejun Heod427dfe2014-02-11 11:52:48 -05001594 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001595 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001596
Tejun Heo2bd59d42014-02-11 11:52:49 -05001597 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001598 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001599 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001600
Tejun Heo2bd59d42014-02-11 11:52:49 -05001601destroy_root:
1602 kernfs_destroy_root(root->kf_root);
1603 root->kf_root = NULL;
1604exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001605 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001606out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001607 free_cgrp_cset_links(&tmp_links);
1608 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609}
1610
Al Virof7e83572010-07-26 13:23:11 +04001611static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001613 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001615 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001616 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001617 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001618 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001619 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001620
1621 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001622 * The first time anyone tries to mount a cgroup, enable the list
1623 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001624 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001625 if (!use_task_css_set_links)
1626 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001627
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001628 mutex_lock(&cgroup_tree_mutex);
1629 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001633 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001634 goto out_unlock;
Li Zefane37a06f2014-04-17 13:53:08 +08001635retry:
Tejun Heo2bd59d42014-02-11 11:52:49 -05001636 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001637 if (!opts.subsys_mask && !opts.none && !opts.name) {
1638 cgrp_dfl_root_visible = true;
1639 root = &cgrp_dfl_root;
1640 cgroup_get(&root->cgrp);
1641 ret = 0;
1642 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001643 }
1644
Tejun Heo985ed672014-03-19 10:23:53 -04001645 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001646 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001648 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001649 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001650
Paul Menage817929e2007-10-18 23:39:36 -07001651 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001652 * If we asked for a name then it must match. Also, if
1653 * name matches but sybsys_mask doesn't, we should fail.
1654 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001655 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001656 if (opts.name) {
1657 if (strcmp(opts.name, root->name))
1658 continue;
1659 name_match = true;
1660 }
Tejun Heo31261212013-06-28 17:07:30 -07001661
1662 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001663 * If we asked for subsystems (or explicitly for no
1664 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001665 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001666 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001667 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001668 if (!name_match)
1669 continue;
1670 ret = -EBUSY;
1671 goto out_unlock;
1672 }
Tejun Heo873fe092013-04-14 20:15:26 -07001673
Tejun Heoc7ba8282013-06-29 14:06:10 -07001674 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001675 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1676 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1677 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001678 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001679 } else {
1680 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1681 }
Tejun Heo873fe092013-04-14 20:15:26 -07001682 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001683
Tejun Heo776f02f2014-02-12 09:29:50 -05001684 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001685 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001686 * ref indicate that the root is being destroyed. Wait for
1687 * destruction to complete so that the subsystems are free.
1688 * We can use wait_queue for the wait but this path is
1689 * super cold. Let's just sleep for a bit and retry.
1690 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001691 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001692 mutex_unlock(&cgroup_mutex);
1693 mutex_unlock(&cgroup_tree_mutex);
1694 msleep(10);
Li Zefane37a06f2014-04-17 13:53:08 +08001695 mutex_lock(&cgroup_tree_mutex);
1696 mutex_lock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001697 goto retry;
1698 }
1699
1700 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001701 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001702 }
1703
Tejun Heo172a2c062014-03-19 10:23:53 -04001704 /*
1705 * No such thing, create a new one. name= matching without subsys
1706 * specification is allowed for already existing hierarchies but we
1707 * can't create new one without subsys specification.
1708 */
1709 if (!opts.subsys_mask && !opts.none) {
1710 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001711 goto out_unlock;
1712 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001713
Tejun Heo172a2c062014-03-19 10:23:53 -04001714 root = kzalloc(sizeof(*root), GFP_KERNEL);
1715 if (!root) {
1716 ret = -ENOMEM;
1717 goto out_unlock;
1718 }
1719
1720 init_cgroup_root(root, &opts);
1721
Tejun Heo35585572014-02-13 06:58:38 -05001722 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001723 if (ret)
1724 cgroup_free_root(root);
1725
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001726out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001727 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001728 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001729
Paul Menagec6d57f32009-09-23 15:56:19 -07001730 kfree(opts.release_agent);
1731 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001732
Tejun Heo2bd59d42014-02-11 11:52:49 -05001733 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001734 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001735
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001736 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1737 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001738 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001739 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740}
1741
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001742static void cgroup_kill_sb(struct super_block *sb)
1743{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001744 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001745 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001746
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001747 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001748 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749}
1750
1751static struct file_system_type cgroup_fs_type = {
1752 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001753 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754 .kill_sb = cgroup_kill_sb,
1755};
1756
Greg KH676db4a2010-08-05 13:53:35 -07001757static struct kobject *cgroup_kobj;
1758
Li Zefana043e3b2008-02-23 15:24:09 -08001759/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001760 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001761 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001762 * @buf: the buffer to write the path into
1763 * @buflen: the length of the buffer
1764 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001765 * Determine @task's cgroup on the first (the one with the lowest non-zero
1766 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1767 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1768 * cgroup controller callbacks.
1769 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001770 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001771 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001772char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001773{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001774 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001775 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001776 int hierarchy_id = 1;
1777 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001778
1779 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001780 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001781
Tejun Heo913ffdb2013-07-11 16:34:48 -07001782 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1783
Tejun Heo857a2be2013-04-14 20:50:08 -07001784 if (root) {
1785 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001786 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001787 } else {
1788 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001789 if (strlcpy(buf, "/", buflen) < buflen)
1790 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001791 }
1792
Tejun Heo96d365e2014-02-13 06:58:40 -05001793 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001794 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001795 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001796}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001797EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001798
Tejun Heob3dc0942014-02-25 10:04:01 -05001799/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001800struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001801 /* the src and dst cset list running through cset->mg_node */
1802 struct list_head src_csets;
1803 struct list_head dst_csets;
1804
1805 /*
1806 * Fields for cgroup_taskset_*() iteration.
1807 *
1808 * Before migration is committed, the target migration tasks are on
1809 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1810 * the csets on ->dst_csets. ->csets point to either ->src_csets
1811 * or ->dst_csets depending on whether migration is committed.
1812 *
1813 * ->cur_csets and ->cur_task point to the current task position
1814 * during iteration.
1815 */
1816 struct list_head *csets;
1817 struct css_set *cur_cset;
1818 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001819};
1820
1821/**
1822 * cgroup_taskset_first - reset taskset and return the first task
1823 * @tset: taskset of interest
1824 *
1825 * @tset iteration is initialized and the first task is returned.
1826 */
1827struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1828{
Tejun Heob3dc0942014-02-25 10:04:01 -05001829 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1830 tset->cur_task = NULL;
1831
1832 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001833}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001834
1835/**
1836 * cgroup_taskset_next - iterate to the next task in taskset
1837 * @tset: taskset of interest
1838 *
1839 * Return the next task in @tset. Iteration must have been initialized
1840 * with cgroup_taskset_first().
1841 */
1842struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1843{
Tejun Heob3dc0942014-02-25 10:04:01 -05001844 struct css_set *cset = tset->cur_cset;
1845 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001846
Tejun Heob3dc0942014-02-25 10:04:01 -05001847 while (&cset->mg_node != tset->csets) {
1848 if (!task)
1849 task = list_first_entry(&cset->mg_tasks,
1850 struct task_struct, cg_list);
1851 else
1852 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001853
Tejun Heob3dc0942014-02-25 10:04:01 -05001854 if (&task->cg_list != &cset->mg_tasks) {
1855 tset->cur_cset = cset;
1856 tset->cur_task = task;
1857 return task;
1858 }
1859
1860 cset = list_next_entry(cset, mg_node);
1861 task = NULL;
1862 }
1863
1864 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001865}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001866
1867/**
Ben Blum74a11662011-05-26 16:25:20 -07001868 * cgroup_task_migrate - move a task from one cgroup to another.
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001869 * @old_cgrp; the cgroup @tsk is being migrated from
1870 * @tsk: the task being migrated
1871 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001872 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001873 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001874 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001875static void cgroup_task_migrate(struct cgroup *old_cgrp,
1876 struct task_struct *tsk,
1877 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001878{
Tejun Heo5abb8852013-06-12 21:04:49 -07001879 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001880
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001881 lockdep_assert_held(&cgroup_mutex);
1882 lockdep_assert_held(&css_set_rwsem);
1883
Ben Blum74a11662011-05-26 16:25:20 -07001884 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001885 * We are synchronized through threadgroup_lock() against PF_EXITING
1886 * setting such that we can't race against cgroup_exit() changing the
1887 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001888 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001889 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001890 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001891
Tejun Heob3dc0942014-02-25 10:04:01 -05001892 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001893 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001894
Tejun Heo1b9aba42014-03-19 17:43:21 -04001895 /*
1896 * Use move_tail so that cgroup_taskset_first() still returns the
1897 * leader after migration. This works because cgroup_migrate()
1898 * ensures that the dst_cset of the leader is the first on the
1899 * tset's dst_csets list.
1900 */
1901 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001902
1903 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001904 * We just gained a reference on old_cset by taking it from the
1905 * task. As trading it for new_cset is protected by cgroup_mutex,
1906 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001907 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001908 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001909 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001910}
1911
Li Zefana043e3b2008-02-23 15:24:09 -08001912/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001913 * cgroup_migrate_finish - cleanup after attach
1914 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001915 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001916 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1917 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001918 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001919static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001920{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001921 struct css_set *cset, *tmp_cset;
1922
1923 lockdep_assert_held(&cgroup_mutex);
1924
1925 down_write(&css_set_rwsem);
1926 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1927 cset->mg_src_cgrp = NULL;
1928 cset->mg_dst_cset = NULL;
1929 list_del_init(&cset->mg_preload_node);
1930 put_css_set_locked(cset, false);
1931 }
1932 up_write(&css_set_rwsem);
1933}
1934
1935/**
1936 * cgroup_migrate_add_src - add a migration source css_set
1937 * @src_cset: the source css_set to add
1938 * @dst_cgrp: the destination cgroup
1939 * @preloaded_csets: list of preloaded css_sets
1940 *
1941 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1942 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1943 * up by cgroup_migrate_finish().
1944 *
1945 * This function may be called without holding threadgroup_lock even if the
1946 * target is a process. Threads may be created and destroyed but as long
1947 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1948 * the preloaded css_sets are guaranteed to cover all migrations.
1949 */
1950static void cgroup_migrate_add_src(struct css_set *src_cset,
1951 struct cgroup *dst_cgrp,
1952 struct list_head *preloaded_csets)
1953{
1954 struct cgroup *src_cgrp;
1955
1956 lockdep_assert_held(&cgroup_mutex);
1957 lockdep_assert_held(&css_set_rwsem);
1958
1959 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1960
Tejun Heo1958d2d2014-02-25 10:04:03 -05001961 if (!list_empty(&src_cset->mg_preload_node))
1962 return;
1963
1964 WARN_ON(src_cset->mg_src_cgrp);
1965 WARN_ON(!list_empty(&src_cset->mg_tasks));
1966 WARN_ON(!list_empty(&src_cset->mg_node));
1967
1968 src_cset->mg_src_cgrp = src_cgrp;
1969 get_css_set(src_cset);
1970 list_add(&src_cset->mg_preload_node, preloaded_csets);
1971}
1972
1973/**
1974 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04001975 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05001976 * @preloaded_csets: list of preloaded source css_sets
1977 *
1978 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
1979 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04001980 * pins all destination css_sets, links each to its source, and append them
1981 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
1982 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05001983 *
1984 * This function must be called after cgroup_migrate_add_src() has been
1985 * called on each migration source css_set. After migration is performed
1986 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
1987 * @preloaded_csets.
1988 */
1989static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
1990 struct list_head *preloaded_csets)
1991{
1992 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04001993 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05001994
1995 lockdep_assert_held(&cgroup_mutex);
1996
Tejun Heof8f22e52014-04-23 11:13:16 -04001997 /*
1998 * Except for the root, child_subsys_mask must be zero for a cgroup
1999 * with tasks so that child cgroups don't compete against tasks.
2000 */
2001 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && dst_cgrp->parent &&
2002 dst_cgrp->child_subsys_mask)
2003 return -EBUSY;
2004
Tejun Heo1958d2d2014-02-25 10:04:03 -05002005 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002006 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002007 struct css_set *dst_cset;
2008
Tejun Heof817de92014-04-23 11:13:16 -04002009 dst_cset = find_css_set(src_cset,
2010 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002011 if (!dst_cset)
2012 goto err;
2013
2014 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002015
2016 /*
2017 * If src cset equals dst, it's noop. Drop the src.
2018 * cgroup_migrate() will skip the cset too. Note that we
2019 * can't handle src == dst as some nodes are used by both.
2020 */
2021 if (src_cset == dst_cset) {
2022 src_cset->mg_src_cgrp = NULL;
2023 list_del_init(&src_cset->mg_preload_node);
2024 put_css_set(src_cset, false);
2025 put_css_set(dst_cset, false);
2026 continue;
2027 }
2028
Tejun Heo1958d2d2014-02-25 10:04:03 -05002029 src_cset->mg_dst_cset = dst_cset;
2030
2031 if (list_empty(&dst_cset->mg_preload_node))
2032 list_add(&dst_cset->mg_preload_node, &csets);
2033 else
2034 put_css_set(dst_cset, false);
2035 }
2036
Tejun Heof817de92014-04-23 11:13:16 -04002037 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002038 return 0;
2039err:
2040 cgroup_migrate_finish(&csets);
2041 return -ENOMEM;
2042}
2043
2044/**
2045 * cgroup_migrate - migrate a process or task to a cgroup
2046 * @cgrp: the destination cgroup
2047 * @leader: the leader of the process or the task to migrate
2048 * @threadgroup: whether @leader points to the whole process or a single task
2049 *
2050 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2051 * process, the caller must be holding threadgroup_lock of @leader. The
2052 * caller is also responsible for invoking cgroup_migrate_add_src() and
2053 * cgroup_migrate_prepare_dst() on the targets before invoking this
2054 * function and following up with cgroup_migrate_finish().
2055 *
2056 * As long as a controller's ->can_attach() doesn't fail, this function is
2057 * guaranteed to succeed. This means that, excluding ->can_attach()
2058 * failure, when migrating multiple targets, the success or failure can be
2059 * decided for all targets by invoking group_migrate_prepare_dst() before
2060 * actually starting migrating.
2061 */
2062static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2063 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002064{
Tejun Heob3dc0942014-02-25 10:04:01 -05002065 struct cgroup_taskset tset = {
2066 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2067 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2068 .csets = &tset.src_csets,
2069 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002070 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002071 struct css_set *cset, *tmp_cset;
2072 struct task_struct *task, *tmp_task;
2073 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002074
2075 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002076 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2077 * already PF_EXITING could be freed from underneath us unless we
2078 * take an rcu_read_lock.
2079 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002080 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002081 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002082 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002083 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002084 /* @task either already exited or can't exit until the end */
2085 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002086 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002087
Tejun Heoeaf797a2014-02-25 10:04:03 -05002088 /* leave @task alone if post_fork() hasn't linked it yet */
2089 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002090 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002091
Tejun Heob3dc0942014-02-25 10:04:01 -05002092 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002093 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002094 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002095
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002096 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002097 * cgroup_taskset_first() must always return the leader.
2098 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002099 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002100 list_move_tail(&task->cg_list, &cset->mg_tasks);
2101 if (list_empty(&cset->mg_node))
2102 list_add_tail(&cset->mg_node, &tset.src_csets);
2103 if (list_empty(&cset->mg_dst_cset->mg_node))
2104 list_move_tail(&cset->mg_dst_cset->mg_node,
2105 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002106 next:
Li Zefan081aa452013-03-13 09:17:09 +08002107 if (!threadgroup)
2108 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002109 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002110 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002111 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002112
Tejun Heo134d3372011-12-12 18:12:21 -08002113 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002114 if (list_empty(&tset.src_csets))
2115 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002116
Tejun Heo1958d2d2014-02-25 10:04:03 -05002117 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002118 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002119 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002120 ret = css->ss->can_attach(css, &tset);
2121 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002122 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002123 goto out_cancel_attach;
2124 }
2125 }
Ben Blum74a11662011-05-26 16:25:20 -07002126 }
2127
2128 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002129 * Now that we're guaranteed success, proceed to move all tasks to
2130 * the new cgroup. There are no failure cases after here, so this
2131 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002132 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002133 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002134 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2135 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2136 cgroup_task_migrate(cset->mg_src_cgrp, task,
2137 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002138 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002139 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002140
2141 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002142 * Migration is committed, all target tasks are now on dst_csets.
2143 * Nothing is sensitive to fork() after this point. Notify
2144 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002145 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002146 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002147
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002148 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002149 if (css->ss->attach)
2150 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002151
Tejun Heo9db8de32014-02-13 06:58:43 -05002152 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002153 goto out_release_tset;
2154
Ben Blum74a11662011-05-26 16:25:20 -07002155out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002156 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002157 if (css == failed_css)
2158 break;
2159 if (css->ss->cancel_attach)
2160 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002161 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002162out_release_tset:
2163 down_write(&css_set_rwsem);
2164 list_splice_init(&tset.dst_csets, &tset.src_csets);
2165 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002166 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002167 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002168 }
2169 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002170 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002171}
2172
Tejun Heo1958d2d2014-02-25 10:04:03 -05002173/**
2174 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2175 * @dst_cgrp: the cgroup to attach to
2176 * @leader: the task or the leader of the threadgroup to be attached
2177 * @threadgroup: attach the whole threadgroup?
2178 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002179 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002180 */
2181static int cgroup_attach_task(struct cgroup *dst_cgrp,
2182 struct task_struct *leader, bool threadgroup)
2183{
2184 LIST_HEAD(preloaded_csets);
2185 struct task_struct *task;
2186 int ret;
2187
2188 /* look up all src csets */
2189 down_read(&css_set_rwsem);
2190 rcu_read_lock();
2191 task = leader;
2192 do {
2193 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2194 &preloaded_csets);
2195 if (!threadgroup)
2196 break;
2197 } while_each_thread(leader, task);
2198 rcu_read_unlock();
2199 up_read(&css_set_rwsem);
2200
2201 /* prepare dst csets and commit */
2202 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2203 if (!ret)
2204 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2205
2206 cgroup_migrate_finish(&preloaded_csets);
2207 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002208}
2209
2210/*
2211 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002212 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002213 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002214 */
2215static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002216{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002217 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002218 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002219 int ret;
2220
Ben Blum74a11662011-05-26 16:25:20 -07002221 if (!cgroup_lock_live_group(cgrp))
2222 return -ENODEV;
2223
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002224retry_find_task:
2225 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002226 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002227 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002228 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002229 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002230 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002231 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002232 }
Ben Blum74a11662011-05-26 16:25:20 -07002233 /*
2234 * even if we're attaching all tasks in the thread group, we
2235 * only need to check permissions on one of them.
2236 */
David Howellsc69e8d92008-11-14 10:39:19 +11002237 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002238 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2239 !uid_eq(cred->euid, tcred->uid) &&
2240 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002241 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002242 ret = -EACCES;
2243 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002244 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002245 } else
2246 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002247
2248 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002249 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002250
2251 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002252 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002253 * trapped in a cpuset, or RT worker may be born in a cgroup
2254 * with no rt_runtime allocated. Just say no.
2255 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002256 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002257 ret = -EINVAL;
2258 rcu_read_unlock();
2259 goto out_unlock_cgroup;
2260 }
2261
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002262 get_task_struct(tsk);
2263 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002264
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002265 threadgroup_lock(tsk);
2266 if (threadgroup) {
2267 if (!thread_group_leader(tsk)) {
2268 /*
2269 * a race with de_thread from another thread's exec()
2270 * may strip us of our leadership, if this happens,
2271 * there is no choice but to throw this task away and
2272 * try again; this is
2273 * "double-double-toil-and-trouble-check locking".
2274 */
2275 threadgroup_unlock(tsk);
2276 put_task_struct(tsk);
2277 goto retry_find_task;
2278 }
Li Zefan081aa452013-03-13 09:17:09 +08002279 }
2280
2281 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2282
Tejun Heocd3d0952011-12-12 18:12:21 -08002283 threadgroup_unlock(tsk);
2284
Paul Menagebbcb81d2007-10-18 23:39:32 -07002285 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002286out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002287 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002288 return ret;
2289}
2290
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002291/**
2292 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2293 * @from: attach to all cgroups of a given task
2294 * @tsk: the task to be attached
2295 */
2296int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2297{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002298 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002299 int retval = 0;
2300
Tejun Heo47cfcd02013-04-07 09:29:51 -07002301 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002302 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002303 struct cgroup *from_cgrp;
2304
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002305 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002306 continue;
2307
Tejun Heo96d365e2014-02-13 06:58:40 -05002308 down_read(&css_set_rwsem);
2309 from_cgrp = task_cgroup_from_root(from, root);
2310 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002311
Li Zefan6f4b7e62013-07-31 16:18:36 +08002312 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002313 if (retval)
2314 break;
2315 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002316 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002317
2318 return retval;
2319}
2320EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2321
Tejun Heo182446d2013-08-08 20:11:24 -04002322static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2323 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002324{
Tejun Heo182446d2013-08-08 20:11:24 -04002325 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002326}
2327
Tejun Heo182446d2013-08-08 20:11:24 -04002328static int cgroup_procs_write(struct cgroup_subsys_state *css,
2329 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002330{
Tejun Heo182446d2013-08-08 20:11:24 -04002331 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002332}
2333
Tejun Heo182446d2013-08-08 20:11:24 -04002334static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
Tejun Heo4d3bb512014-03-19 10:23:54 -04002335 struct cftype *cft, char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002336{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002337 struct cgroup_root *root = css->cgroup->root;
Tejun Heo5f469902014-02-11 11:52:48 -05002338
2339 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002340 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002341 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002342 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002343 strlcpy(root->release_agent_path, buffer,
2344 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002345 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002346 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002347 return 0;
2348}
2349
Tejun Heo2da8ca82013-12-05 12:28:04 -05002350static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002351{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002352 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002353
Paul Menagee788e062008-07-25 01:46:59 -07002354 if (!cgroup_lock_live_group(cgrp))
2355 return -ENODEV;
2356 seq_puts(seq, cgrp->root->release_agent_path);
2357 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002358 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002359 return 0;
2360}
2361
Tejun Heo2da8ca82013-12-05 12:28:04 -05002362static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002363{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002364 struct cgroup *cgrp = seq_css(seq)->cgroup;
2365
2366 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002367 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002368}
2369
Tejun Heof8f22e52014-04-23 11:13:16 -04002370static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2371{
2372 struct cgroup_subsys *ss;
2373 bool printed = false;
2374 int ssid;
2375
2376 for_each_subsys(ss, ssid) {
2377 if (ss_mask & (1 << ssid)) {
2378 if (printed)
2379 seq_putc(seq, ' ');
2380 seq_printf(seq, "%s", ss->name);
2381 printed = true;
2382 }
2383 }
2384 if (printed)
2385 seq_putc(seq, '\n');
2386}
2387
2388/* show controllers which are currently attached to the default hierarchy */
2389static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2390{
2391 struct cgroup *cgrp = seq_css(seq)->cgroup;
2392
2393 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask);
2394 return 0;
2395}
2396
2397/* show controllers which are enabled from the parent */
2398static int cgroup_controllers_show(struct seq_file *seq, void *v)
2399{
2400 struct cgroup *cgrp = seq_css(seq)->cgroup;
2401
2402 cgroup_print_ss_mask(seq, cgrp->parent->child_subsys_mask);
2403 return 0;
2404}
2405
2406/* show controllers which are enabled for a given cgroup's children */
2407static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2408{
2409 struct cgroup *cgrp = seq_css(seq)->cgroup;
2410
2411 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2412 return 0;
2413}
2414
2415/**
2416 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2417 * @cgrp: root of the subtree to update csses for
2418 *
2419 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2420 * css associations need to be updated accordingly. This function looks up
2421 * all css_sets which are attached to the subtree, creates the matching
2422 * updated css_sets and migrates the tasks to the new ones.
2423 */
2424static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2425{
2426 LIST_HEAD(preloaded_csets);
2427 struct cgroup_subsys_state *css;
2428 struct css_set *src_cset;
2429 int ret;
2430
2431 lockdep_assert_held(&cgroup_tree_mutex);
2432 lockdep_assert_held(&cgroup_mutex);
2433
2434 /* look up all csses currently attached to @cgrp's subtree */
2435 down_read(&css_set_rwsem);
2436 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2437 struct cgrp_cset_link *link;
2438
2439 /* self is not affected by child_subsys_mask change */
2440 if (css->cgroup == cgrp)
2441 continue;
2442
2443 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2444 cgroup_migrate_add_src(link->cset, cgrp,
2445 &preloaded_csets);
2446 }
2447 up_read(&css_set_rwsem);
2448
2449 /* NULL dst indicates self on default hierarchy */
2450 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2451 if (ret)
2452 goto out_finish;
2453
2454 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2455 struct task_struct *last_task = NULL, *task;
2456
2457 /* src_csets precede dst_csets, break on the first dst_cset */
2458 if (!src_cset->mg_src_cgrp)
2459 break;
2460
2461 /*
2462 * All tasks in src_cset need to be migrated to the
2463 * matching dst_cset. Empty it process by process. We
2464 * walk tasks but migrate processes. The leader might even
2465 * belong to a different cset but such src_cset would also
2466 * be among the target src_csets because the default
2467 * hierarchy enforces per-process membership.
2468 */
2469 while (true) {
2470 down_read(&css_set_rwsem);
2471 task = list_first_entry_or_null(&src_cset->tasks,
2472 struct task_struct, cg_list);
2473 if (task) {
2474 task = task->group_leader;
2475 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2476 get_task_struct(task);
2477 }
2478 up_read(&css_set_rwsem);
2479
2480 if (!task)
2481 break;
2482
2483 /* guard against possible infinite loop */
2484 if (WARN(last_task == task,
2485 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2486 goto out_finish;
2487 last_task = task;
2488
2489 threadgroup_lock(task);
2490 /* raced against de_thread() from another thread? */
2491 if (!thread_group_leader(task)) {
2492 threadgroup_unlock(task);
2493 put_task_struct(task);
2494 continue;
2495 }
2496
2497 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2498
2499 threadgroup_unlock(task);
2500 put_task_struct(task);
2501
2502 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2503 goto out_finish;
2504 }
2505 }
2506
2507out_finish:
2508 cgroup_migrate_finish(&preloaded_csets);
2509 return ret;
2510}
2511
2512/* change the enabled child controllers for a cgroup in the default hierarchy */
2513static int cgroup_subtree_control_write(struct cgroup_subsys_state *dummy_css,
2514 struct cftype *cft, char *buffer)
2515{
2516 unsigned long enable_req = 0, disable_req = 0, enable, disable;
2517 struct cgroup *cgrp = dummy_css->cgroup, *child;
2518 struct cgroup_subsys *ss;
2519 char *tok, *p;
2520 int ssid, ret;
2521
2522 /*
2523 * Parse input - white space separated list of subsystem names
2524 * prefixed with either + or -.
2525 */
2526 p = buffer;
2527 while ((tok = strsep(&p, " \t\n"))) {
2528 for_each_subsys(ss, ssid) {
2529 if (ss->disabled || strcmp(tok + 1, ss->name))
2530 continue;
2531
2532 if (*tok == '+') {
2533 enable_req |= 1 << ssid;
2534 disable_req &= ~(1 << ssid);
2535 } else if (*tok == '-') {
2536 disable_req |= 1 << ssid;
2537 enable_req &= ~(1 << ssid);
2538 } else {
2539 return -EINVAL;
2540 }
2541 break;
2542 }
2543 if (ssid == CGROUP_SUBSYS_COUNT)
2544 return -EINVAL;
2545 }
2546
2547 /*
2548 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2549 * active_ref. cgroup_lock_live_group() already provides enough
2550 * protection. Ensure @cgrp stays accessible and break the
2551 * active_ref protection.
2552 */
2553 cgroup_get(cgrp);
2554 kernfs_break_active_protection(cgrp->control_kn);
2555retry:
2556 enable = enable_req;
2557 disable = disable_req;
2558
2559 mutex_lock(&cgroup_tree_mutex);
2560
2561 for_each_subsys(ss, ssid) {
2562 if (enable & (1 << ssid)) {
2563 if (cgrp->child_subsys_mask & (1 << ssid)) {
2564 enable &= ~(1 << ssid);
2565 continue;
2566 }
2567
2568 /*
2569 * Because css offlining is asynchronous, userland
2570 * might try to re-enable the same controller while
2571 * the previous instance is still around. In such
2572 * cases, wait till it's gone using offline_waitq.
2573 */
2574 cgroup_for_each_live_child(child, cgrp) {
2575 wait_queue_t wait;
2576
2577 if (!cgroup_css(child, ss))
2578 continue;
2579
2580 prepare_to_wait(&child->offline_waitq, &wait,
2581 TASK_UNINTERRUPTIBLE);
2582 mutex_unlock(&cgroup_tree_mutex);
2583 schedule();
2584 finish_wait(&child->offline_waitq, &wait);
2585 goto retry;
2586 }
2587
2588 /* unavailable or not enabled on the parent? */
2589 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2590 (cgrp->parent &&
2591 !(cgrp->parent->child_subsys_mask & (1 << ssid)))) {
2592 ret = -ENOENT;
2593 goto out_unlock_tree;
2594 }
2595 } else if (disable & (1 << ssid)) {
2596 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2597 disable &= ~(1 << ssid);
2598 continue;
2599 }
2600
2601 /* a child has it enabled? */
2602 cgroup_for_each_live_child(child, cgrp) {
2603 if (child->child_subsys_mask & (1 << ssid)) {
2604 ret = -EBUSY;
2605 goto out_unlock_tree;
2606 }
2607 }
2608 }
2609 }
2610
2611 if (!enable && !disable) {
2612 ret = 0;
2613 goto out_unlock_tree;
2614 }
2615
2616 if (!cgroup_lock_live_group(cgrp)) {
2617 ret = -ENODEV;
2618 goto out_unlock_tree;
2619 }
2620
2621 /*
2622 * Except for the root, child_subsys_mask must be zero for a cgroup
2623 * with tasks so that child cgroups don't compete against tasks.
2624 */
2625 if (enable && cgrp->parent && !list_empty(&cgrp->cset_links)) {
2626 ret = -EBUSY;
2627 goto out_unlock;
2628 }
2629
2630 /*
2631 * Create csses for enables and update child_subsys_mask. This
2632 * changes cgroup_e_css() results which in turn makes the
2633 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2634 * subtree to the updated csses.
2635 */
2636 for_each_subsys(ss, ssid) {
2637 if (!(enable & (1 << ssid)))
2638 continue;
2639
2640 cgroup_for_each_live_child(child, cgrp) {
2641 ret = create_css(child, ss);
2642 if (ret)
2643 goto err_undo_css;
2644 }
2645 }
2646
2647 cgrp->child_subsys_mask |= enable;
2648 cgrp->child_subsys_mask &= ~disable;
2649
2650 ret = cgroup_update_dfl_csses(cgrp);
2651 if (ret)
2652 goto err_undo_css;
2653
2654 /* all tasks are now migrated away from the old csses, kill them */
2655 for_each_subsys(ss, ssid) {
2656 if (!(disable & (1 << ssid)))
2657 continue;
2658
2659 cgroup_for_each_live_child(child, cgrp)
2660 kill_css(cgroup_css(child, ss));
2661 }
2662
2663 kernfs_activate(cgrp->kn);
2664 ret = 0;
2665out_unlock:
2666 mutex_unlock(&cgroup_mutex);
2667out_unlock_tree:
2668 mutex_unlock(&cgroup_tree_mutex);
2669 kernfs_unbreak_active_protection(cgrp->control_kn);
2670 cgroup_put(cgrp);
2671 return ret;
2672
2673err_undo_css:
2674 cgrp->child_subsys_mask &= ~enable;
2675 cgrp->child_subsys_mask |= disable;
2676
2677 for_each_subsys(ss, ssid) {
2678 if (!(enable & (1 << ssid)))
2679 continue;
2680
2681 cgroup_for_each_live_child(child, cgrp) {
2682 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2683 if (css)
2684 kill_css(css);
2685 }
2686 }
2687 goto out_unlock;
2688}
2689
Tejun Heo842b5972014-04-25 18:28:02 -04002690static int cgroup_populated_show(struct seq_file *seq, void *v)
2691{
2692 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2693 return 0;
2694}
2695
Tejun Heo2bd59d42014-02-11 11:52:49 -05002696static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2697 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002698{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002699 struct cgroup *cgrp = of->kn->parent->priv;
2700 struct cftype *cft = of->kn->priv;
2701 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002702 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002703
Tejun Heo2bd59d42014-02-11 11:52:49 -05002704 /*
2705 * kernfs guarantees that a file isn't deleted with operations in
2706 * flight, which means that the matching css is and stays alive and
2707 * doesn't need to be pinned. The RCU locking is not necessary
2708 * either. It's just for the convenience of using cgroup_css().
2709 */
2710 rcu_read_lock();
2711 css = cgroup_css(cgrp, cft->ss);
2712 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002713
Tejun Heoa742c592013-12-05 12:28:03 -05002714 if (cft->write_string) {
2715 ret = cft->write_string(css, cft, strstrip(buf));
2716 } else if (cft->write_u64) {
2717 unsigned long long v;
2718 ret = kstrtoull(buf, 0, &v);
2719 if (!ret)
2720 ret = cft->write_u64(css, cft, v);
2721 } else if (cft->write_s64) {
2722 long long v;
2723 ret = kstrtoll(buf, 0, &v);
2724 if (!ret)
2725 ret = cft->write_s64(css, cft, v);
2726 } else if (cft->trigger) {
2727 ret = cft->trigger(css, (unsigned int)cft->private);
2728 } else {
2729 ret = -EINVAL;
2730 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002731
Tejun Heoa742c592013-12-05 12:28:03 -05002732 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002733}
2734
Tejun Heo6612f052013-12-05 12:28:04 -05002735static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002736{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002737 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002738}
2739
2740static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2741{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002742 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002743}
2744
2745static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2746{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002747 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002748}
2749
2750static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2751{
Tejun Heo7da11272013-12-05 12:28:04 -05002752 struct cftype *cft = seq_cft(m);
2753 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002754
Tejun Heo2da8ca82013-12-05 12:28:04 -05002755 if (cft->seq_show)
2756 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002757
Tejun Heo896f5192013-12-05 12:28:04 -05002758 if (cft->read_u64)
2759 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2760 else if (cft->read_s64)
2761 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2762 else
2763 return -EINVAL;
2764 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002765}
2766
Tejun Heo2bd59d42014-02-11 11:52:49 -05002767static struct kernfs_ops cgroup_kf_single_ops = {
2768 .atomic_write_len = PAGE_SIZE,
2769 .write = cgroup_file_write,
2770 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002771};
2772
Tejun Heo2bd59d42014-02-11 11:52:49 -05002773static struct kernfs_ops cgroup_kf_ops = {
2774 .atomic_write_len = PAGE_SIZE,
2775 .write = cgroup_file_write,
2776 .seq_start = cgroup_seqfile_start,
2777 .seq_next = cgroup_seqfile_next,
2778 .seq_stop = cgroup_seqfile_stop,
2779 .seq_show = cgroup_seqfile_show,
2780};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002781
2782/*
2783 * cgroup_rename - Only allow simple rename of directories in place.
2784 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002785static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2786 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002787{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002788 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002789 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002790
Tejun Heo2bd59d42014-02-11 11:52:49 -05002791 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002792 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002793 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002794 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002795
Tejun Heo6db8e852013-06-14 11:18:22 -07002796 /*
2797 * This isn't a proper migration and its usefulness is very
2798 * limited. Disallow if sane_behavior.
2799 */
2800 if (cgroup_sane_behavior(cgrp))
2801 return -EPERM;
2802
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002803 /*
2804 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2805 * active_ref. kernfs_rename() doesn't require active_ref
2806 * protection. Break them before grabbing cgroup_tree_mutex.
2807 */
2808 kernfs_break_active_protection(new_parent);
2809 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002810
Tejun Heo2bd59d42014-02-11 11:52:49 -05002811 mutex_lock(&cgroup_tree_mutex);
2812 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002813
Tejun Heo2bd59d42014-02-11 11:52:49 -05002814 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002815
Tejun Heo2bd59d42014-02-11 11:52:49 -05002816 mutex_unlock(&cgroup_mutex);
2817 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002818
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002819 kernfs_unbreak_active_protection(kn);
2820 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002821 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002822}
2823
Tejun Heo49957f82014-04-07 16:44:47 -04002824/* set uid and gid of cgroup dirs and files to that of the creator */
2825static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2826{
2827 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2828 .ia_uid = current_fsuid(),
2829 .ia_gid = current_fsgid(), };
2830
2831 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2832 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2833 return 0;
2834
2835 return kernfs_setattr(kn, &iattr);
2836}
2837
Tejun Heo2bb566c2013-08-08 20:11:23 -04002838static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002839{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002840 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002841 struct kernfs_node *kn;
2842 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002843 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002844
Tejun Heo2bd59d42014-02-11 11:52:49 -05002845#ifdef CONFIG_DEBUG_LOCK_ALLOC
2846 key = &cft->lockdep_key;
2847#endif
2848 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2849 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2850 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002851 if (IS_ERR(kn))
2852 return PTR_ERR(kn);
2853
2854 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002855 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002856 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002857 return ret;
2858 }
2859
2860 if (cft->seq_show == cgroup_subtree_control_show)
2861 cgrp->control_kn = kn;
Tejun Heo842b5972014-04-25 18:28:02 -04002862 else if (cft->seq_show == cgroup_populated_show)
2863 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002864 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002865}
2866
Tejun Heob1f28d32013-06-28 16:24:10 -07002867/**
2868 * cgroup_addrm_files - add or remove files to a cgroup directory
2869 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002870 * @cfts: array of cftypes to be added
2871 * @is_add: whether to add or remove
2872 *
2873 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002874 * For removals, this function never fails. If addition fails, this
2875 * function doesn't remove files already added. The caller is responsible
2876 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002877 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002878static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2879 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002880{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002881 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002882 int ret;
2883
Tejun Heoace2bee2014-02-11 11:52:47 -05002884 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002885
2886 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002887 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002888 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2889 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002890 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2891 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002892 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2893 continue;
2894 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2895 continue;
2896
Li Zefan2739d3c2013-01-21 18:18:33 +08002897 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002898 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002899 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002900 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002901 cft->name, ret);
2902 return ret;
2903 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002904 } else {
2905 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002906 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002907 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002908 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002909}
2910
Tejun Heo21a2d342014-02-12 09:29:49 -05002911static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002912{
2913 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002914 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002915 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002916 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002917 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002918
Tejun Heo21a2d342014-02-12 09:29:49 -05002919 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002920
Li Zefane8c82d22013-06-18 18:48:37 +08002921 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002922 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002923 struct cgroup *cgrp = css->cgroup;
2924
Li Zefane8c82d22013-06-18 18:48:37 +08002925 if (cgroup_is_dead(cgrp))
2926 continue;
2927
Tejun Heo21a2d342014-02-12 09:29:49 -05002928 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002929 if (ret)
2930 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002931 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002932
2933 if (is_add && !ret)
2934 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002935 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002936}
2937
Tejun Heo2da440a2014-02-11 11:52:48 -05002938static void cgroup_exit_cftypes(struct cftype *cfts)
2939{
2940 struct cftype *cft;
2941
Tejun Heo2bd59d42014-02-11 11:52:49 -05002942 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2943 /* free copy for custom atomic_write_len, see init_cftypes() */
2944 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2945 kfree(cft->kf_ops);
2946 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002947 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002948 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002949}
2950
Tejun Heo2bd59d42014-02-11 11:52:49 -05002951static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002952{
2953 struct cftype *cft;
2954
Tejun Heo2bd59d42014-02-11 11:52:49 -05002955 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2956 struct kernfs_ops *kf_ops;
2957
Tejun Heo0adb0702014-02-12 09:29:48 -05002958 WARN_ON(cft->ss || cft->kf_ops);
2959
Tejun Heo2bd59d42014-02-11 11:52:49 -05002960 if (cft->seq_start)
2961 kf_ops = &cgroup_kf_ops;
2962 else
2963 kf_ops = &cgroup_kf_single_ops;
2964
2965 /*
2966 * Ugh... if @cft wants a custom max_write_len, we need to
2967 * make a copy of kf_ops to set its atomic_write_len.
2968 */
2969 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2970 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2971 if (!kf_ops) {
2972 cgroup_exit_cftypes(cfts);
2973 return -ENOMEM;
2974 }
2975 kf_ops->atomic_write_len = cft->max_write_len;
2976 }
2977
2978 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002979 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002980 }
2981
2982 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002983}
2984
Tejun Heo21a2d342014-02-12 09:29:49 -05002985static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2986{
2987 lockdep_assert_held(&cgroup_tree_mutex);
2988
2989 if (!cfts || !cfts[0].ss)
2990 return -ENOENT;
2991
2992 list_del(&cfts->node);
2993 cgroup_apply_cftypes(cfts, false);
2994 cgroup_exit_cftypes(cfts);
2995 return 0;
2996}
2997
Tejun Heo8e3f6542012-04-01 12:09:55 -07002998/**
Tejun Heo80b13582014-02-12 09:29:48 -05002999 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3000 * @cfts: zero-length name terminated array of cftypes
3001 *
3002 * Unregister @cfts. Files described by @cfts are removed from all
3003 * existing cgroups and all future cgroups won't have them either. This
3004 * function can be called anytime whether @cfts' subsys is attached or not.
3005 *
3006 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3007 * registered.
3008 */
3009int cgroup_rm_cftypes(struct cftype *cfts)
3010{
Tejun Heo21a2d342014-02-12 09:29:49 -05003011 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003012
Tejun Heo21a2d342014-02-12 09:29:49 -05003013 mutex_lock(&cgroup_tree_mutex);
3014 ret = cgroup_rm_cftypes_locked(cfts);
3015 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003016 return ret;
3017}
3018
3019/**
3020 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3021 * @ss: target cgroup subsystem
3022 * @cfts: zero-length name terminated array of cftypes
3023 *
3024 * Register @cfts to @ss. Files described by @cfts are created for all
3025 * existing cgroups to which @ss is attached and all future cgroups will
3026 * have them too. This function can be called anytime whether @ss is
3027 * attached or not.
3028 *
3029 * Returns 0 on successful registration, -errno on failure. Note that this
3030 * function currently returns 0 as long as @cfts registration is successful
3031 * even if some file creation attempts on existing cgroups fail.
3032 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003033int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003034{
Tejun Heo9ccece82013-06-28 16:24:11 -07003035 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003036
Li Zefandc5736e2014-02-17 10:41:50 +08003037 if (!cfts || cfts[0].name[0] == '\0')
3038 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003039
Tejun Heo2bd59d42014-02-11 11:52:49 -05003040 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003041 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003042 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003043
Tejun Heo21a2d342014-02-12 09:29:49 -05003044 mutex_lock(&cgroup_tree_mutex);
3045
Tejun Heo0adb0702014-02-12 09:29:48 -05003046 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003047 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003048 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003049 cgroup_rm_cftypes_locked(cfts);
3050
3051 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003052 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003053}
Tejun Heo79578622012-04-01 12:09:56 -07003054
3055/**
Li Zefana043e3b2008-02-23 15:24:09 -08003056 * cgroup_task_count - count the number of tasks in a cgroup.
3057 * @cgrp: the cgroup in question
3058 *
3059 * Return the number of tasks in the cgroup.
3060 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003061static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003062{
3063 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003064 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003065
Tejun Heo96d365e2014-02-13 06:58:40 -05003066 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003067 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3068 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003069 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003070 return count;
3071}
3072
Tejun Heo574bd9f2012-11-09 09:12:29 -08003073/**
Tejun Heo492eb212013-08-08 20:11:25 -04003074 * css_next_child - find the next child of a given css
3075 * @pos_css: the current position (%NULL to initiate traversal)
3076 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003077 *
Tejun Heo492eb212013-08-08 20:11:25 -04003078 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003079 * under either cgroup_mutex or RCU read lock. The only requirement is
3080 * that @parent_css and @pos_css are accessible. The next sibling is
3081 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003082 */
Tejun Heo492eb212013-08-08 20:11:25 -04003083struct cgroup_subsys_state *
3084css_next_child(struct cgroup_subsys_state *pos_css,
3085 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003086{
Tejun Heo492eb212013-08-08 20:11:25 -04003087 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3088 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003089 struct cgroup *next;
3090
Tejun Heoace2bee2014-02-11 11:52:47 -05003091 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003092
3093 /*
3094 * @pos could already have been removed. Once a cgroup is removed,
3095 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003096 * changes. As CGRP_DEAD assertion is serialized and happens
3097 * before the cgroup is taken off the ->sibling list, if we see it
3098 * unasserted, it's guaranteed that the next sibling hasn't
3099 * finished its grace period even if it's already removed, and thus
3100 * safe to dereference from this RCU critical section. If
3101 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3102 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003103 *
3104 * If @pos is dead, its next pointer can't be dereferenced;
3105 * however, as each cgroup is given a monotonically increasing
3106 * unique serial number and always appended to the sibling list,
3107 * the next one can be found by walking the parent's children until
3108 * we see a cgroup with higher serial number than @pos's. While
3109 * this path can be slower, it's taken only when either the current
3110 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003111 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003112 if (!pos) {
3113 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3114 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003115 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003116 } else {
3117 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3118 if (next->serial_nr > pos->serial_nr)
3119 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003120 }
3121
Tejun Heo3b281af2014-04-23 11:13:15 -04003122 /*
3123 * @next, if not pointing to the head, can be dereferenced and is
3124 * the next sibling; however, it might have @ss disabled. If so,
3125 * fast-forward to the next enabled one.
3126 */
3127 while (&next->sibling != &cgrp->children) {
3128 struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
Tejun Heo492eb212013-08-08 20:11:25 -04003129
Tejun Heo3b281af2014-04-23 11:13:15 -04003130 if (next_css)
3131 return next_css;
3132 next = list_entry_rcu(next->sibling.next, struct cgroup, sibling);
3133 }
3134 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003135}
Tejun Heo53fa5262013-05-24 10:55:38 +09003136
3137/**
Tejun Heo492eb212013-08-08 20:11:25 -04003138 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003139 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003140 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003141 *
Tejun Heo492eb212013-08-08 20:11:25 -04003142 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003143 * to visit for pre-order traversal of @root's descendants. @root is
3144 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003145 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003146 * While this function requires cgroup_mutex or RCU read locking, it
3147 * doesn't require the whole traversal to be contained in a single critical
3148 * section. This function will return the correct next descendant as long
3149 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003150 */
Tejun Heo492eb212013-08-08 20:11:25 -04003151struct cgroup_subsys_state *
3152css_next_descendant_pre(struct cgroup_subsys_state *pos,
3153 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003154{
Tejun Heo492eb212013-08-08 20:11:25 -04003155 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003156
Tejun Heoace2bee2014-02-11 11:52:47 -05003157 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003158
Tejun Heobd8815a2013-08-08 20:11:27 -04003159 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003160 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003161 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003162
3163 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003164 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003165 if (next)
3166 return next;
3167
3168 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003169 while (pos != root) {
3170 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003171 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003172 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003173 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003174 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003175
3176 return NULL;
3177}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003178
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003179/**
Tejun Heo492eb212013-08-08 20:11:25 -04003180 * css_rightmost_descendant - return the rightmost descendant of a css
3181 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003182 *
Tejun Heo492eb212013-08-08 20:11:25 -04003183 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3184 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003185 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003186 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003187 * While this function requires cgroup_mutex or RCU read locking, it
3188 * doesn't require the whole traversal to be contained in a single critical
3189 * section. This function will return the correct rightmost descendant as
3190 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003191 */
Tejun Heo492eb212013-08-08 20:11:25 -04003192struct cgroup_subsys_state *
3193css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003194{
Tejun Heo492eb212013-08-08 20:11:25 -04003195 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003196
Tejun Heoace2bee2014-02-11 11:52:47 -05003197 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003198
3199 do {
3200 last = pos;
3201 /* ->prev isn't RCU safe, walk ->next till the end */
3202 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003203 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003204 pos = tmp;
3205 } while (pos);
3206
3207 return last;
3208}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003209
Tejun Heo492eb212013-08-08 20:11:25 -04003210static struct cgroup_subsys_state *
3211css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003212{
Tejun Heo492eb212013-08-08 20:11:25 -04003213 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003214
3215 do {
3216 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003217 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003218 } while (pos);
3219
3220 return last;
3221}
3222
3223/**
Tejun Heo492eb212013-08-08 20:11:25 -04003224 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003225 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003226 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003227 *
Tejun Heo492eb212013-08-08 20:11:25 -04003228 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003229 * to visit for post-order traversal of @root's descendants. @root is
3230 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003231 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003232 * While this function requires cgroup_mutex or RCU read locking, it
3233 * doesn't require the whole traversal to be contained in a single critical
3234 * section. This function will return the correct next descendant as long
3235 * as both @pos and @cgroup are accessible and @pos is a descendant of
3236 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003237 */
Tejun Heo492eb212013-08-08 20:11:25 -04003238struct cgroup_subsys_state *
3239css_next_descendant_post(struct cgroup_subsys_state *pos,
3240 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003241{
Tejun Heo492eb212013-08-08 20:11:25 -04003242 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003243
Tejun Heoace2bee2014-02-11 11:52:47 -05003244 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003245
Tejun Heo58b79a92013-09-06 15:31:08 -04003246 /* if first iteration, visit leftmost descendant which may be @root */
3247 if (!pos)
3248 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003249
Tejun Heobd8815a2013-08-08 20:11:27 -04003250 /* if we visited @root, we're done */
3251 if (pos == root)
3252 return NULL;
3253
Tejun Heo574bd9f2012-11-09 09:12:29 -08003254 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003255 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003256 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003257 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003258
3259 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003260 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003261}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003262
Tejun Heo0942eee2013-08-08 20:11:26 -04003263/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003264 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003265 * @it: the iterator to advance
3266 *
3267 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003268 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003269static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003270{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003271 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003272 struct cgrp_cset_link *link;
3273 struct css_set *cset;
3274
3275 /* Advance to the next non-empty css_set */
3276 do {
3277 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003278 if (l == it->cset_head) {
3279 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003280 return;
3281 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003282
3283 if (it->ss) {
3284 cset = container_of(l, struct css_set,
3285 e_cset_node[it->ss->id]);
3286 } else {
3287 link = list_entry(l, struct cgrp_cset_link, cset_link);
3288 cset = link->cset;
3289 }
Tejun Heoc7561122014-02-25 10:04:01 -05003290 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3291
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003292 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003293
3294 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003295 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003296 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003297 it->task_pos = cset->mg_tasks.next;
3298
3299 it->tasks_head = &cset->tasks;
3300 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003301}
3302
Tejun Heo0942eee2013-08-08 20:11:26 -04003303/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003304 * css_task_iter_start - initiate task iteration
3305 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003306 * @it: the task iterator to use
3307 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003308 * Initiate iteration through the tasks of @css. The caller can call
3309 * css_task_iter_next() to walk through the tasks until the function
3310 * returns NULL. On completion of iteration, css_task_iter_end() must be
3311 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003312 *
3313 * Note that this function acquires a lock which is released when the
3314 * iteration finishes. The caller can't sleep while iteration is in
3315 * progress.
3316 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003317void css_task_iter_start(struct cgroup_subsys_state *css,
3318 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003319 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003320{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003321 /* no one should try to iterate before mounting cgroups */
3322 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003323
Tejun Heo96d365e2014-02-13 06:58:40 -05003324 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003325
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003326 it->ss = css->ss;
3327
3328 if (it->ss)
3329 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3330 else
3331 it->cset_pos = &css->cgroup->cset_links;
3332
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003333 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003334
Tejun Heo72ec7022013-08-08 20:11:26 -04003335 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003336}
3337
Tejun Heo0942eee2013-08-08 20:11:26 -04003338/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003339 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003340 * @it: the task iterator being iterated
3341 *
3342 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003343 * initialized via css_task_iter_start(). Returns NULL when the iteration
3344 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003345 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003346struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003347{
3348 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003349 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003350
3351 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003352 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003353 return NULL;
3354 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003355
3356 /*
3357 * Advance iterator to find next entry. cset->tasks is consumed
3358 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3359 * next cset.
3360 */
Paul Menage817929e2007-10-18 23:39:36 -07003361 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003362
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003363 if (l == it->tasks_head)
3364 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003365
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003366 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003367 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003368 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003369 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003370
Paul Menage817929e2007-10-18 23:39:36 -07003371 return res;
3372}
3373
Tejun Heo0942eee2013-08-08 20:11:26 -04003374/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003375 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003376 * @it: the task iterator to finish
3377 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003378 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003379 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003380void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003381 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003382{
Tejun Heo96d365e2014-02-13 06:58:40 -05003383 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003384}
3385
3386/**
3387 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3388 * @to: cgroup to which the tasks will be moved
3389 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003390 *
3391 * Locking rules between cgroup_post_fork() and the migration path
3392 * guarantee that, if a task is forking while being migrated, the new child
3393 * is guaranteed to be either visible in the source cgroup after the
3394 * parent's migration is complete or put into the target cgroup. No task
3395 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003396 */
3397int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3398{
Tejun Heo952aaa12014-02-25 10:04:03 -05003399 LIST_HEAD(preloaded_csets);
3400 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003401 struct css_task_iter it;
3402 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003403 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003404
Tejun Heo952aaa12014-02-25 10:04:03 -05003405 mutex_lock(&cgroup_mutex);
3406
3407 /* all tasks in @from are being moved, all csets are source */
3408 down_read(&css_set_rwsem);
3409 list_for_each_entry(link, &from->cset_links, cset_link)
3410 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3411 up_read(&css_set_rwsem);
3412
3413 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3414 if (ret)
3415 goto out_err;
3416
3417 /*
3418 * Migrate tasks one-by-one until @form is empty. This fails iff
3419 * ->can_attach() fails.
3420 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003421 do {
3422 css_task_iter_start(&from->dummy_css, &it);
3423 task = css_task_iter_next(&it);
3424 if (task)
3425 get_task_struct(task);
3426 css_task_iter_end(&it);
3427
3428 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003429 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003430 put_task_struct(task);
3431 }
3432 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003433out_err:
3434 cgroup_migrate_finish(&preloaded_csets);
3435 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003436 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003437}
3438
Paul Menage817929e2007-10-18 23:39:36 -07003439/*
Ben Blum102a7752009-09-23 15:56:26 -07003440 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003441 *
3442 * Reading this file can return large amounts of data if a cgroup has
3443 * *lots* of attached tasks. So it may need several calls to read(),
3444 * but we cannot guarantee that the information we produce is correct
3445 * unless we produce it entirely atomically.
3446 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003447 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003448
Li Zefan24528252012-01-20 11:58:43 +08003449/* which pidlist file are we talking about? */
3450enum cgroup_filetype {
3451 CGROUP_FILE_PROCS,
3452 CGROUP_FILE_TASKS,
3453};
3454
3455/*
3456 * A pidlist is a list of pids that virtually represents the contents of one
3457 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3458 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3459 * to the cgroup.
3460 */
3461struct cgroup_pidlist {
3462 /*
3463 * used to find which pidlist is wanted. doesn't change as long as
3464 * this particular list stays in the list.
3465 */
3466 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3467 /* array of xids */
3468 pid_t *list;
3469 /* how many elements the above list has */
3470 int length;
Li Zefan24528252012-01-20 11:58:43 +08003471 /* each of these stored in a list by its cgroup */
3472 struct list_head links;
3473 /* pointer to the cgroup we belong to, for list removal purposes */
3474 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003475 /* for delayed destruction */
3476 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003477};
3478
Paul Menagebbcb81d2007-10-18 23:39:32 -07003479/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003480 * The following two functions "fix" the issue where there are more pids
3481 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3482 * TODO: replace with a kernel-wide solution to this problem
3483 */
3484#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3485static void *pidlist_allocate(int count)
3486{
3487 if (PIDLIST_TOO_LARGE(count))
3488 return vmalloc(count * sizeof(pid_t));
3489 else
3490 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3491}
Tejun Heob1a21362013-11-29 10:42:58 -05003492
Ben Blumd1d9fd32009-09-23 15:56:28 -07003493static void pidlist_free(void *p)
3494{
3495 if (is_vmalloc_addr(p))
3496 vfree(p);
3497 else
3498 kfree(p);
3499}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003500
3501/*
Tejun Heob1a21362013-11-29 10:42:58 -05003502 * Used to destroy all pidlists lingering waiting for destroy timer. None
3503 * should be left afterwards.
3504 */
3505static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3506{
3507 struct cgroup_pidlist *l, *tmp_l;
3508
3509 mutex_lock(&cgrp->pidlist_mutex);
3510 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3511 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3512 mutex_unlock(&cgrp->pidlist_mutex);
3513
3514 flush_workqueue(cgroup_pidlist_destroy_wq);
3515 BUG_ON(!list_empty(&cgrp->pidlists));
3516}
3517
3518static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3519{
3520 struct delayed_work *dwork = to_delayed_work(work);
3521 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3522 destroy_dwork);
3523 struct cgroup_pidlist *tofree = NULL;
3524
3525 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003526
3527 /*
Tejun Heo04502362013-11-29 10:42:59 -05003528 * Destroy iff we didn't get queued again. The state won't change
3529 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003530 */
Tejun Heo04502362013-11-29 10:42:59 -05003531 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003532 list_del(&l->links);
3533 pidlist_free(l->list);
3534 put_pid_ns(l->key.ns);
3535 tofree = l;
3536 }
3537
Tejun Heob1a21362013-11-29 10:42:58 -05003538 mutex_unlock(&l->owner->pidlist_mutex);
3539 kfree(tofree);
3540}
3541
3542/*
Ben Blum102a7752009-09-23 15:56:26 -07003543 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003544 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003545 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003546static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003547{
Ben Blum102a7752009-09-23 15:56:26 -07003548 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003549
3550 /*
3551 * we presume the 0th element is unique, so i starts at 1. trivial
3552 * edge cases first; no work needs to be done for either
3553 */
3554 if (length == 0 || length == 1)
3555 return length;
3556 /* src and dest walk down the list; dest counts unique elements */
3557 for (src = 1; src < length; src++) {
3558 /* find next unique element */
3559 while (list[src] == list[src-1]) {
3560 src++;
3561 if (src == length)
3562 goto after;
3563 }
3564 /* dest always points to where the next unique element goes */
3565 list[dest] = list[src];
3566 dest++;
3567 }
3568after:
Ben Blum102a7752009-09-23 15:56:26 -07003569 return dest;
3570}
3571
Tejun Heoafb2bc12013-11-29 10:42:59 -05003572/*
3573 * The two pid files - task and cgroup.procs - guaranteed that the result
3574 * is sorted, which forced this whole pidlist fiasco. As pid order is
3575 * different per namespace, each namespace needs differently sorted list,
3576 * making it impossible to use, for example, single rbtree of member tasks
3577 * sorted by task pointer. As pidlists can be fairly large, allocating one
3578 * per open file is dangerous, so cgroup had to implement shared pool of
3579 * pidlists keyed by cgroup and namespace.
3580 *
3581 * All this extra complexity was caused by the original implementation
3582 * committing to an entirely unnecessary property. In the long term, we
3583 * want to do away with it. Explicitly scramble sort order if
3584 * sane_behavior so that no such expectation exists in the new interface.
3585 *
3586 * Scrambling is done by swapping every two consecutive bits, which is
3587 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3588 */
3589static pid_t pid_fry(pid_t pid)
3590{
3591 unsigned a = pid & 0x55555555;
3592 unsigned b = pid & 0xAAAAAAAA;
3593
3594 return (a << 1) | (b >> 1);
3595}
3596
3597static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3598{
3599 if (cgroup_sane_behavior(cgrp))
3600 return pid_fry(pid);
3601 else
3602 return pid;
3603}
3604
Ben Blum102a7752009-09-23 15:56:26 -07003605static int cmppid(const void *a, const void *b)
3606{
3607 return *(pid_t *)a - *(pid_t *)b;
3608}
3609
Tejun Heoafb2bc12013-11-29 10:42:59 -05003610static int fried_cmppid(const void *a, const void *b)
3611{
3612 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3613}
3614
Ben Blum72a8cb32009-09-23 15:56:27 -07003615static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3616 enum cgroup_filetype type)
3617{
3618 struct cgroup_pidlist *l;
3619 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003620 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003621
Tejun Heoe6b81712013-11-29 10:42:59 -05003622 lockdep_assert_held(&cgrp->pidlist_mutex);
3623
3624 list_for_each_entry(l, &cgrp->pidlists, links)
3625 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003626 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003627 return NULL;
3628}
3629
Ben Blum72a8cb32009-09-23 15:56:27 -07003630/*
3631 * find the appropriate pidlist for our purpose (given procs vs tasks)
3632 * returns with the lock on that pidlist already held, and takes care
3633 * of the use count, or returns NULL with no locks held if we're out of
3634 * memory.
3635 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003636static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3637 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003638{
3639 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003640
Tejun Heoe6b81712013-11-29 10:42:59 -05003641 lockdep_assert_held(&cgrp->pidlist_mutex);
3642
3643 l = cgroup_pidlist_find(cgrp, type);
3644 if (l)
3645 return l;
3646
Ben Blum72a8cb32009-09-23 15:56:27 -07003647 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003648 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003649 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003650 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003651
Tejun Heob1a21362013-11-29 10:42:58 -05003652 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003653 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003654 /* don't need task_nsproxy() if we're looking at ourself */
3655 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003656 l->owner = cgrp;
3657 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003658 return l;
3659}
3660
3661/*
Ben Blum102a7752009-09-23 15:56:26 -07003662 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3663 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003664static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3665 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003666{
3667 pid_t *array;
3668 int length;
3669 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003670 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003671 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003672 struct cgroup_pidlist *l;
3673
Tejun Heo4bac00d2013-11-29 10:42:59 -05003674 lockdep_assert_held(&cgrp->pidlist_mutex);
3675
Ben Blum102a7752009-09-23 15:56:26 -07003676 /*
3677 * If cgroup gets more users after we read count, we won't have
3678 * enough space - tough. This race is indistinguishable to the
3679 * caller from the case that the additional cgroup users didn't
3680 * show up until sometime later on.
3681 */
3682 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003683 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003684 if (!array)
3685 return -ENOMEM;
3686 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003687 css_task_iter_start(&cgrp->dummy_css, &it);
3688 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003689 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003690 break;
Ben Blum102a7752009-09-23 15:56:26 -07003691 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003692 if (type == CGROUP_FILE_PROCS)
3693 pid = task_tgid_vnr(tsk);
3694 else
3695 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003696 if (pid > 0) /* make sure to only use valid results */
3697 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003698 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003699 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003700 length = n;
3701 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003702 if (cgroup_sane_behavior(cgrp))
3703 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3704 else
3705 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003706 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003707 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003708
Tejun Heoe6b81712013-11-29 10:42:59 -05003709 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003710 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003711 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003712 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003713 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003714 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003715
3716 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003717 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003718 l->list = array;
3719 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003720 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003721 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003722}
3723
Balbir Singh846c7bb2007-10-18 23:39:44 -07003724/**
Li Zefana043e3b2008-02-23 15:24:09 -08003725 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003726 * @stats: cgroupstats to fill information into
3727 * @dentry: A dentry entry belonging to the cgroup for which stats have
3728 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003729 *
3730 * Build and fill cgroupstats so that taskstats can export it to user
3731 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003732 */
3733int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3734{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003735 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003736 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003737 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003738 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003739
Tejun Heo2bd59d42014-02-11 11:52:49 -05003740 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3741 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3742 kernfs_type(kn) != KERNFS_DIR)
3743 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003744
Li Zefanbad34662014-02-14 16:54:28 +08003745 mutex_lock(&cgroup_mutex);
3746
Tejun Heo2bd59d42014-02-11 11:52:49 -05003747 /*
3748 * We aren't being called from kernfs and there's no guarantee on
3749 * @kn->priv's validity. For this and css_tryget_from_dir(),
3750 * @kn->priv is RCU safe. Let's do the RCU dancing.
3751 */
3752 rcu_read_lock();
3753 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003754 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003755 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003756 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003757 return -ENOENT;
3758 }
Li Zefanbad34662014-02-14 16:54:28 +08003759 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003760
Tejun Heo72ec7022013-08-08 20:11:26 -04003761 css_task_iter_start(&cgrp->dummy_css, &it);
3762 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003763 switch (tsk->state) {
3764 case TASK_RUNNING:
3765 stats->nr_running++;
3766 break;
3767 case TASK_INTERRUPTIBLE:
3768 stats->nr_sleeping++;
3769 break;
3770 case TASK_UNINTERRUPTIBLE:
3771 stats->nr_uninterruptible++;
3772 break;
3773 case TASK_STOPPED:
3774 stats->nr_stopped++;
3775 break;
3776 default:
3777 if (delayacct_is_task_waiting_on_io(tsk))
3778 stats->nr_io_wait++;
3779 break;
3780 }
3781 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003782 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003783
Li Zefanbad34662014-02-14 16:54:28 +08003784 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003785 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003786}
3787
Paul Menage8f3ff202009-09-23 15:56:25 -07003788
Paul Menagecc31edc2008-10-18 20:28:04 -07003789/*
Ben Blum102a7752009-09-23 15:56:26 -07003790 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003791 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003792 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003793 */
3794
Ben Blum102a7752009-09-23 15:56:26 -07003795static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003796{
3797 /*
3798 * Initially we receive a position value that corresponds to
3799 * one more than the last pid shown (or 0 on the first call or
3800 * after a seek to the start). Use a binary-search to find the
3801 * next pid to display, if any
3802 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003803 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003804 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003805 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003806 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003807 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003808 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003809
Tejun Heo4bac00d2013-11-29 10:42:59 -05003810 mutex_lock(&cgrp->pidlist_mutex);
3811
3812 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003813 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003814 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003815 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003816 * could already have been destroyed.
3817 */
Tejun Heo5d224442013-12-05 12:28:04 -05003818 if (of->priv)
3819 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003820
3821 /*
3822 * Either this is the first start() after open or the matching
3823 * pidlist has been destroyed inbetween. Create a new one.
3824 */
Tejun Heo5d224442013-12-05 12:28:04 -05003825 if (!of->priv) {
3826 ret = pidlist_array_load(cgrp, type,
3827 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003828 if (ret)
3829 return ERR_PTR(ret);
3830 }
Tejun Heo5d224442013-12-05 12:28:04 -05003831 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003832
Paul Menagecc31edc2008-10-18 20:28:04 -07003833 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003834 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003835
Paul Menagecc31edc2008-10-18 20:28:04 -07003836 while (index < end) {
3837 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003838 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003839 index = mid;
3840 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003841 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003842 index = mid + 1;
3843 else
3844 end = mid;
3845 }
3846 }
3847 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003848 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003849 return NULL;
3850 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003851 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003852 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003853 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003854}
3855
Ben Blum102a7752009-09-23 15:56:26 -07003856static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003857{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003858 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003859 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003860
Tejun Heo5d224442013-12-05 12:28:04 -05003861 if (l)
3862 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003863 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003864 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003865}
3866
Ben Blum102a7752009-09-23 15:56:26 -07003867static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003868{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003869 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003870 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003871 pid_t *p = v;
3872 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003873 /*
3874 * Advance to the next pid in the array. If this goes off the
3875 * end, we're done
3876 */
3877 p++;
3878 if (p >= end) {
3879 return NULL;
3880 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003881 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003882 return p;
3883 }
3884}
3885
Ben Blum102a7752009-09-23 15:56:26 -07003886static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003887{
3888 return seq_printf(s, "%d\n", *(int *)v);
3889}
3890
Ben Blum102a7752009-09-23 15:56:26 -07003891/*
3892 * seq_operations functions for iterating on pidlists through seq_file -
3893 * independent of whether it's tasks or procs
3894 */
3895static const struct seq_operations cgroup_pidlist_seq_operations = {
3896 .start = cgroup_pidlist_start,
3897 .stop = cgroup_pidlist_stop,
3898 .next = cgroup_pidlist_next,
3899 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003900};
3901
Tejun Heo182446d2013-08-08 20:11:24 -04003902static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3903 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003904{
Tejun Heo182446d2013-08-08 20:11:24 -04003905 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003906}
3907
Tejun Heo182446d2013-08-08 20:11:24 -04003908static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3909 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003910{
Tejun Heo182446d2013-08-08 20:11:24 -04003911 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003912 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003913 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003914 else
Tejun Heo182446d2013-08-08 20:11:24 -04003915 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003916 return 0;
3917}
3918
Tejun Heo182446d2013-08-08 20:11:24 -04003919static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3920 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003921{
Tejun Heo182446d2013-08-08 20:11:24 -04003922 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003923}
3924
Tejun Heo182446d2013-08-08 20:11:24 -04003925static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3926 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003927{
3928 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003929 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003930 else
Tejun Heo182446d2013-08-08 20:11:24 -04003931 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003932 return 0;
3933}
3934
Tejun Heod5c56ce2013-06-03 19:14:34 -07003935static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003936 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003937 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003938 .seq_start = cgroup_pidlist_start,
3939 .seq_next = cgroup_pidlist_next,
3940 .seq_stop = cgroup_pidlist_stop,
3941 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003942 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003943 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003944 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003945 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003946 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003947 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003948 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003949 .read_u64 = cgroup_clone_children_read,
3950 .write_u64 = cgroup_clone_children_write,
3951 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003952 {
Tejun Heo873fe092013-04-14 20:15:26 -07003953 .name = "cgroup.sane_behavior",
3954 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003955 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003956 },
Tejun Heof8f22e52014-04-23 11:13:16 -04003957 {
3958 .name = "cgroup.controllers",
3959 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
3960 .seq_show = cgroup_root_controllers_show,
3961 },
3962 {
3963 .name = "cgroup.controllers",
3964 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3965 .seq_show = cgroup_controllers_show,
3966 },
3967 {
3968 .name = "cgroup.subtree_control",
3969 .flags = CFTYPE_ONLY_ON_DFL,
3970 .seq_show = cgroup_subtree_control_show,
3971 .write_string = cgroup_subtree_control_write,
3972 },
Tejun Heo842b5972014-04-25 18:28:02 -04003973 {
3974 .name = "cgroup.populated",
3975 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3976 .seq_show = cgroup_populated_show,
3977 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003978
3979 /*
3980 * Historical crazy stuff. These don't have "cgroup." prefix and
3981 * don't exist if sane_behavior. If you're depending on these, be
3982 * prepared to be burned.
3983 */
3984 {
3985 .name = "tasks",
3986 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003987 .seq_start = cgroup_pidlist_start,
3988 .seq_next = cgroup_pidlist_next,
3989 .seq_stop = cgroup_pidlist_stop,
3990 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003991 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003992 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003993 .mode = S_IRUGO | S_IWUSR,
3994 },
3995 {
3996 .name = "notify_on_release",
3997 .flags = CFTYPE_INSANE,
3998 .read_u64 = cgroup_read_notify_on_release,
3999 .write_u64 = cgroup_write_notify_on_release,
4000 },
Tejun Heo873fe092013-04-14 20:15:26 -07004001 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004002 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004003 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004004 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004005 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004006 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004007 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004008 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004009};
4010
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004011/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004012 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004013 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004014 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004015 *
4016 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004017 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004018static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004019{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004020 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004021 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004022
Tejun Heo8e3f6542012-04-01 12:09:55 -07004023 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004024 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004025 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004026
4027 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004028 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004029
Tejun Heo0adb0702014-02-12 09:29:48 -05004030 list_for_each_entry(cfts, &ss->cfts, node) {
4031 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004032 if (ret < 0)
4033 goto err;
4034 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004035 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004036 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004037err:
4038 cgroup_clear_dir(cgrp, subsys_mask);
4039 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004040}
4041
Tejun Heo0c21ead2013-08-13 20:22:51 -04004042/*
4043 * css destruction is four-stage process.
4044 *
4045 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4046 * Implemented in kill_css().
4047 *
4048 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4049 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4050 * by invoking offline_css(). After offlining, the base ref is put.
4051 * Implemented in css_killed_work_fn().
4052 *
4053 * 3. When the percpu_ref reaches zero, the only possible remaining
4054 * accessors are inside RCU read sections. css_release() schedules the
4055 * RCU callback.
4056 *
4057 * 4. After the grace period, the css can be freed. Implemented in
4058 * css_free_work_fn().
4059 *
4060 * It is actually hairier because both step 2 and 4 require process context
4061 * and thus involve punting to css->destroy_work adding two additional
4062 * steps to the already complex sequence.
4063 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004064static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004065{
4066 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004067 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004068 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004069
Tejun Heo0ae78e02013-08-13 11:01:54 -04004070 if (css->parent)
4071 css_put(css->parent);
4072
Tejun Heo0c21ead2013-08-13 20:22:51 -04004073 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004074 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004075}
4076
4077static void css_free_rcu_fn(struct rcu_head *rcu_head)
4078{
4079 struct cgroup_subsys_state *css =
4080 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4081
Tejun Heo0c21ead2013-08-13 20:22:51 -04004082 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004083 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004084}
4085
Tejun Heod3daf282013-06-13 19:39:16 -07004086static void css_release(struct percpu_ref *ref)
4087{
4088 struct cgroup_subsys_state *css =
4089 container_of(ref, struct cgroup_subsys_state, refcnt);
4090
Monam Agarwal01a97142014-03-24 00:17:18 +05304091 RCU_INIT_POINTER(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004092 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004093}
4094
Tejun Heo623f9262013-08-13 11:01:55 -04004095static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4096 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004097{
Paul Menagebd89aab2007-10-18 23:40:44 -07004098 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004099 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004100 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004101
Tejun Heo0ae78e02013-08-13 11:01:54 -04004102 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004103 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004104 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004105 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004106
Tejun Heoca8bdca2013-08-26 18:40:56 -04004107 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004108}
4109
Li Zefan2a4ac632013-07-31 16:16:40 +08004110/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004111static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004112{
Tejun Heo623f9262013-08-13 11:01:55 -04004113 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004114 int ret = 0;
4115
Tejun Heoace2bee2014-02-11 11:52:47 -05004116 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004117 lockdep_assert_held(&cgroup_mutex);
4118
Tejun Heo92fb9742012-11-19 08:13:38 -08004119 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004120 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004121 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004122 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004123 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05004124 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004125 }
Tejun Heob1929db2012-11-19 08:13:38 -08004126 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004127}
4128
Li Zefan2a4ac632013-07-31 16:16:40 +08004129/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004130static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004131{
Tejun Heo623f9262013-08-13 11:01:55 -04004132 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004133
Tejun Heoace2bee2014-02-11 11:52:47 -05004134 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004135 lockdep_assert_held(&cgroup_mutex);
4136
4137 if (!(css->flags & CSS_ONLINE))
4138 return;
4139
Li Zefand7eeac12013-03-12 15:35:59 -07004140 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004141 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004142
Tejun Heoeb954192013-08-08 20:11:23 -04004143 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004144 css->cgroup->nr_css--;
Tejun Heoe3297802014-04-23 11:13:15 -04004145 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004146
4147 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004148}
4149
Tejun Heoc81c925a2013-12-06 15:11:56 -05004150/**
4151 * create_css - create a cgroup_subsys_state
4152 * @cgrp: the cgroup new css will be associated with
4153 * @ss: the subsys of new css
4154 *
4155 * Create a new css associated with @cgrp - @ss pair. On success, the new
4156 * css is online and installed in @cgrp with all interface files created.
4157 * Returns 0 on success, -errno on failure.
4158 */
4159static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4160{
4161 struct cgroup *parent = cgrp->parent;
4162 struct cgroup_subsys_state *css;
4163 int err;
4164
Tejun Heoc81c925a2013-12-06 15:11:56 -05004165 lockdep_assert_held(&cgroup_mutex);
4166
4167 css = ss->css_alloc(cgroup_css(parent, ss));
4168 if (IS_ERR(css))
4169 return PTR_ERR(css);
4170
4171 err = percpu_ref_init(&css->refcnt, css_release);
4172 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004173 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004174
4175 init_css(css, ss, cgrp);
4176
Tejun Heoaec25022014-02-08 10:36:58 -05004177 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004178 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004179 goto err_free_percpu_ref;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004180
4181 err = online_css(css);
4182 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004183 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004184
Tejun Heo59f52962014-02-11 11:52:49 -05004185 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004186 css_get(css->parent);
4187
4188 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4189 parent->parent) {
4190 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",
4191 current->comm, current->pid, ss->name);
4192 if (!strcmp(ss->name, "memory"))
4193 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4194 ss->warned_broken_hierarchy = true;
4195 }
4196
4197 return 0;
4198
Li Zefan3eb59ec2014-03-18 17:02:36 +08004199err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004200 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004201err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004202 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004203err_free_css:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004204 ss->css_free(css);
4205 return err;
4206}
4207
Tejun Heo2bd59d42014-02-11 11:52:49 -05004208/**
Li Zefana043e3b2008-02-23 15:24:09 -08004209 * cgroup_create - create a cgroup
4210 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05004211 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05004212 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07004213 */
Tejun Heoe61734c2014-02-12 09:29:50 -05004214static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05004215 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004216{
Paul Menagebd89aab2007-10-18 23:40:44 -07004217 struct cgroup *cgrp;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004218 struct cgroup_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05004219 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004220 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004221 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004222
Tejun Heo0a950f62012-11-19 09:02:12 -08004223 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004224 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4225 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004226 return -ENOMEM;
4227
Tejun Heoace2bee2014-02-11 11:52:47 -05004228 mutex_lock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08004229
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004230 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08004231 * Only live parents can have children. Note that the liveliness
4232 * check isn't strictly necessary because cgroup_mkdir() and
4233 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4234 * anyway so that locking is contained inside cgroup proper and we
4235 * don't get nasty surprises if we ever grow another caller.
4236 */
4237 if (!cgroup_lock_live_group(parent)) {
4238 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05004239 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004240 }
4241
4242 /*
4243 * Temporarily set the pointer to NULL, so idr_find() won't return
4244 * a half-baked cgroup.
4245 */
4246 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
4247 if (cgrp->id < 0) {
4248 err = -ENOMEM;
4249 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08004250 }
4251
Paul Menagecc31edc2008-10-18 20:28:04 -07004252 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004253
Paul Menagebd89aab2007-10-18 23:40:44 -07004254 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004255 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004256 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004257
Li Zefanb6abdb02008-03-04 14:28:19 -08004258 if (notify_on_release(parent))
4259 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4260
Tejun Heo2260e7f2012-11-19 08:13:38 -08004261 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4262 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004263
Tejun Heo2bd59d42014-02-11 11:52:49 -05004264 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004265 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004266 if (IS_ERR(kn)) {
4267 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004268 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004269 }
4270 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004271
Tejun Heo6f305582014-02-12 09:29:50 -05004272 /*
4273 * This extra ref will be put in cgroup_free_fn() and guarantees
4274 * that @cgrp->kn is always accessible.
4275 */
4276 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004277
Tejun Heo00356bd2013-06-18 11:14:22 -07004278 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004279
Tejun Heo4e139af2012-11-19 08:13:36 -08004280 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004281 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004282 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004283 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004284
Tejun Heo0d802552013-12-06 15:11:56 -05004285 /*
4286 * @cgrp is now fully operational. If something fails after this
4287 * point, it'll be released via the normal destruction path.
4288 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004289 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4290
Tejun Heo49957f82014-04-07 16:44:47 -04004291 err = cgroup_kn_set_ugid(kn);
4292 if (err)
4293 goto err_destroy;
4294
Tejun Heo2bb566c2013-08-08 20:11:23 -04004295 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004296 if (err)
4297 goto err_destroy;
4298
Tejun Heo9d403e92013-12-06 15:11:56 -05004299 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004300 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004301 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heob85d2042013-12-06 15:11:57 -05004302 err = create_css(cgrp, ss);
4303 if (err)
4304 goto err_destroy;
4305 }
Tejun Heoa8638032012-11-09 09:12:29 -08004306 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004307
Tejun Heobd53d612014-04-23 11:13:16 -04004308 /*
4309 * On the default hierarchy, a child doesn't automatically inherit
4310 * child_subsys_mask from the parent. Each is configured manually.
4311 */
4312 if (!cgroup_on_dfl(cgrp))
4313 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004314
Tejun Heo2bd59d42014-02-11 11:52:49 -05004315 kernfs_activate(kn);
4316
Paul Menageddbcc7e2007-10-18 23:39:30 -07004317 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004318 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004319
4320 return 0;
4321
Tejun Heo0a950f62012-11-19 09:02:12 -08004322err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004323 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004324err_unlock:
4325 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004326err_unlock_tree:
4327 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004328 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004330
4331err_destroy:
4332 cgroup_destroy_locked(cgrp);
4333 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004334 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004335 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004336}
4337
Tejun Heo2bd59d42014-02-11 11:52:49 -05004338static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4339 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004340{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004341 struct cgroup *parent = parent_kn->priv;
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004342 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004343
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004344 /*
4345 * cgroup_create() grabs cgroup_tree_mutex which nests outside
4346 * kernfs active_ref and cgroup_create() already synchronizes
4347 * properly against removal through cgroup_lock_live_group().
4348 * Break it before calling cgroup_create().
4349 */
4350 cgroup_get(parent);
4351 kernfs_break_active_protection(parent_kn);
4352
4353 ret = cgroup_create(parent, name, mode);
4354
4355 kernfs_unbreak_active_protection(parent_kn);
4356 cgroup_put(parent);
4357 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004358}
4359
Tejun Heo223dbc32013-08-13 20:22:50 -04004360/*
4361 * This is called when the refcnt of a css is confirmed to be killed.
4362 * css_tryget() is now guaranteed to fail.
4363 */
4364static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004365{
Tejun Heo223dbc32013-08-13 20:22:50 -04004366 struct cgroup_subsys_state *css =
4367 container_of(work, struct cgroup_subsys_state, destroy_work);
4368 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004369
Tejun Heoace2bee2014-02-11 11:52:47 -05004370 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004371 mutex_lock(&cgroup_mutex);
4372
4373 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004374 * css_tryget() is guaranteed to fail now. Tell subsystems to
4375 * initate destruction.
4376 */
4377 offline_css(css);
4378
4379 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004380 * If @cgrp is marked dead, it's waiting for refs of all css's to
4381 * be disabled before proceeding to the second phase of cgroup
4382 * destruction. If we are the last one, kick it off.
4383 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004384 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004385 cgroup_destroy_css_killed(cgrp);
4386
4387 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004388 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004389
4390 /*
4391 * Put the css refs from kill_css(). Each css holds an extra
4392 * reference to the cgroup's dentry and cgroup removal proceeds
4393 * regardless of css refs. On the last put of each css, whenever
4394 * that may be, the extra dentry ref is put so that dentry
4395 * destruction happens only after all css's are released.
4396 */
4397 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004398}
4399
Tejun Heo223dbc32013-08-13 20:22:50 -04004400/* css kill confirmation processing requires process context, bounce */
4401static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004402{
4403 struct cgroup_subsys_state *css =
4404 container_of(ref, struct cgroup_subsys_state, refcnt);
4405
Tejun Heo223dbc32013-08-13 20:22:50 -04004406 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004407 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004408}
4409
Tejun Heof392e512014-04-23 11:13:14 -04004410/**
4411 * kill_css - destroy a css
4412 * @css: css to destroy
4413 *
4414 * This function initiates destruction of @css by removing cgroup interface
4415 * files and putting its base reference. ->css_offline() will be invoked
4416 * asynchronously once css_tryget() is guaranteed to fail and when the
4417 * reference count reaches zero, @css will be released.
4418 */
4419static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004420{
Tejun Heo94419622014-03-19 10:23:54 -04004421 lockdep_assert_held(&cgroup_tree_mutex);
4422
Tejun Heo2bd59d42014-02-11 11:52:49 -05004423 /*
4424 * This must happen before css is disassociated with its cgroup.
4425 * See seq_css() for details.
4426 */
Tejun Heoaec25022014-02-08 10:36:58 -05004427 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004428
Tejun Heoedae0c32013-08-13 20:22:51 -04004429 /*
4430 * Killing would put the base ref, but we need to keep it alive
4431 * until after ->css_offline().
4432 */
4433 css_get(css);
4434
4435 /*
4436 * cgroup core guarantees that, by the time ->css_offline() is
4437 * invoked, no new css reference will be given out via
4438 * css_tryget(). We can't simply call percpu_ref_kill() and
4439 * proceed to offlining css's because percpu_ref_kill() doesn't
4440 * guarantee that the ref is seen as killed on all CPUs on return.
4441 *
4442 * Use percpu_ref_kill_and_confirm() to get notifications as each
4443 * css is confirmed to be seen as killed on all CPUs.
4444 */
4445 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004446}
4447
4448/**
4449 * cgroup_destroy_locked - the first stage of cgroup destruction
4450 * @cgrp: cgroup to be destroyed
4451 *
4452 * css's make use of percpu refcnts whose killing latency shouldn't be
4453 * exposed to userland and are RCU protected. Also, cgroup core needs to
4454 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4455 * invoked. To satisfy all the requirements, destruction is implemented in
4456 * the following two steps.
4457 *
4458 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4459 * userland visible parts and start killing the percpu refcnts of
4460 * css's. Set up so that the next stage will be kicked off once all
4461 * the percpu refcnts are confirmed to be killed.
4462 *
4463 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4464 * rest of destruction. Once all cgroup references are gone, the
4465 * cgroup is RCU-freed.
4466 *
4467 * This function implements s1. After this step, @cgrp is gone as far as
4468 * the userland is concerned and a new cgroup with the same name may be
4469 * created. As cgroup doesn't care about the names internally, this
4470 * doesn't cause any problem.
4471 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004472static int cgroup_destroy_locked(struct cgroup *cgrp)
4473 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004474{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004475 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004476 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004477 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004478 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004479
Tejun Heoace2bee2014-02-11 11:52:47 -05004480 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004481 lockdep_assert_held(&cgroup_mutex);
4482
Tejun Heoddd69142013-06-12 21:04:54 -07004483 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004484 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004485 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004486 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004487 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004488 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004489 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004490 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004491 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004492
Tejun Heo1a90dd52012-11-05 09:16:59 -08004493 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004494 * Make sure there's no live children. We can't test ->children
4495 * emptiness as dead children linger on it while being destroyed;
4496 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4497 */
4498 empty = true;
4499 rcu_read_lock();
4500 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4501 empty = cgroup_is_dead(child);
4502 if (!empty)
4503 break;
4504 }
4505 rcu_read_unlock();
4506 if (!empty)
4507 return -EBUSY;
4508
4509 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004510 * Mark @cgrp dead. This prevents further task migration and child
4511 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004512 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004513 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004514 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004515 */
Tejun Heo54766d42013-06-12 21:04:53 -07004516 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004517
Tejun Heo5d773812014-03-19 10:23:53 -04004518 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004519 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4520 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004521 * percpu refs of all css's are confirmed to be killed. This
4522 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004523 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004524 mutex_unlock(&cgroup_mutex);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004525 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004526 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004527 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004528
Tejun Heo455050d2013-06-13 19:27:41 -07004529 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4530 raw_spin_lock(&release_list_lock);
4531 if (!list_empty(&cgrp->release_list))
4532 list_del_init(&cgrp->release_list);
4533 raw_spin_unlock(&release_list_lock);
4534
4535 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004536 * If @cgrp has css's attached, the second stage of cgroup
4537 * destruction is kicked off from css_killed_work_fn() after the
4538 * refs of all attached css's are killed. If @cgrp doesn't have
4539 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004540 */
Tejun Heof20104d2013-08-13 20:22:50 -04004541 if (!cgrp->nr_css)
4542 cgroup_destroy_css_killed(cgrp);
4543
Tejun Heo2bd59d42014-02-11 11:52:49 -05004544 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05004545 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004546
Tejun Heof20104d2013-08-13 20:22:50 -04004547 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05004548 * There are two control paths which try to determine cgroup from
4549 * dentry without going through kernfs - cgroupstats_build() and
4550 * css_tryget_from_dir(). Those are supported by RCU protecting
4551 * clearing of cgrp->kn->priv backpointer, which should happen
4552 * after all files under it have been removed.
Tejun Heo455050d2013-06-13 19:27:41 -07004553 */
Tejun Heo6f305582014-02-12 09:29:50 -05004554 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004555 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004556
Tejun Heo4ac06012014-02-11 11:52:47 -05004557 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004558
Tejun Heoea15f8c2013-06-13 19:27:42 -07004559 return 0;
4560};
4561
Tejun Heod3daf282013-06-13 19:39:16 -07004562/**
Tejun Heof20104d2013-08-13 20:22:50 -04004563 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004564 * @work: cgroup->destroy_free_work
4565 *
4566 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004567 * destroyed after all css's are offlined and performs the rest of
4568 * destruction. This is the second step of destruction described in the
4569 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004570 */
Tejun Heof20104d2013-08-13 20:22:50 -04004571static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004572{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004573 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004574
Tejun Heoace2bee2014-02-11 11:52:47 -05004575 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004576 lockdep_assert_held(&cgroup_mutex);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004577
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004578 /* delete this cgroup from parent->children */
4579 list_del_rcu(&cgrp->sibling);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004580
Tejun Heo59f52962014-02-11 11:52:49 -05004581 cgroup_put(cgrp);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004582
Paul Menageddbcc7e2007-10-18 23:39:30 -07004583 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004584 check_for_release(parent);
4585}
4586
Tejun Heo2bd59d42014-02-11 11:52:49 -05004587static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004588{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004589 struct cgroup *cgrp = kn->priv;
4590 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004591
Tejun Heo2bd59d42014-02-11 11:52:49 -05004592 /*
4593 * This is self-destruction but @kn can't be removed while this
4594 * callback is in progress. Let's break active protection. Once
4595 * the protection is broken, @cgrp can be destroyed at any point.
4596 * Pin it so that it stays accessible.
4597 */
4598 cgroup_get(cgrp);
4599 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004600
Tejun Heoace2bee2014-02-11 11:52:47 -05004601 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004602 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004603
Tejun Heo2bd59d42014-02-11 11:52:49 -05004604 /*
4605 * @cgrp might already have been destroyed while we're trying to
4606 * grab the mutexes.
4607 */
4608 if (!cgroup_is_dead(cgrp))
4609 ret = cgroup_destroy_locked(cgrp);
4610
Tejun Heo42809dd2012-11-19 08:13:37 -08004611 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004612 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004613
Tejun Heo2bd59d42014-02-11 11:52:49 -05004614 kernfs_unbreak_active_protection(kn);
4615 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004616 return ret;
4617}
4618
Tejun Heo2bd59d42014-02-11 11:52:49 -05004619static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4620 .remount_fs = cgroup_remount,
4621 .show_options = cgroup_show_options,
4622 .mkdir = cgroup_mkdir,
4623 .rmdir = cgroup_rmdir,
4624 .rename = cgroup_rename,
4625};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004626
Li Zefan06a11922008-04-29 01:00:07 -07004627static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004628{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004629 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004630
4631 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004632
Tejun Heoace2bee2014-02-11 11:52:47 -05004633 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004634 mutex_lock(&cgroup_mutex);
4635
Tejun Heo0adb0702014-02-12 09:29:48 -05004636 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004637
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004638 /* Create the root cgroup state for this subsystem */
4639 ss->root = &cgrp_dfl_root;
4640 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004641 /* We don't handle early failures gracefully */
4642 BUG_ON(IS_ERR(css));
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004643 init_css(css, ss, &cgrp_dfl_root.cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004644
Li Zefane8d55fd2008-04-29 01:00:13 -07004645 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004646 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004647 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004648 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004649 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004650
4651 need_forkexit_callback |= ss->fork || ss->exit;
4652
Li Zefane8d55fd2008-04-29 01:00:13 -07004653 /* At system boot, before all subsystems have been
4654 * registered, no tasks have been forked, so we don't
4655 * need to invoke fork callbacks here. */
4656 BUG_ON(!list_empty(&init_task.tasks));
4657
Tejun Heoae7f1642013-08-13 20:22:50 -04004658 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004659
Tejun Heof392e512014-04-23 11:13:14 -04004660 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004661
Ben Blume6a11052010-03-10 15:22:09 -08004662 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004663 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004664}
4665
4666/**
Li Zefana043e3b2008-02-23 15:24:09 -08004667 * cgroup_init_early - cgroup initialization at system boot
4668 *
4669 * Initialize cgroups at system boot, and initialize any
4670 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004671 */
4672int __init cgroup_init_early(void)
4673{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004674 static struct cgroup_sb_opts __initdata opts =
4675 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004676 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004677 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004678
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004679 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004680 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004681
Tejun Heo3ed80a62014-02-08 10:36:58 -05004682 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004683 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004684 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4685 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004686 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004687 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4688 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004689
Tejun Heoaec25022014-02-08 10:36:58 -05004690 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004691 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004692
4693 if (ss->early_init)
4694 cgroup_init_subsys(ss);
4695 }
4696 return 0;
4697}
4698
4699/**
Li Zefana043e3b2008-02-23 15:24:09 -08004700 * cgroup_init - cgroup initialization
4701 *
4702 * Register cgroup filesystem and /proc file, and initialize
4703 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004704 */
4705int __init cgroup_init(void)
4706{
Tejun Heo30159ec2013-06-25 11:53:37 -07004707 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004708 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004709 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004710
Tejun Heo2bd59d42014-02-11 11:52:49 -05004711 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004712
Tejun Heo985ed672014-03-19 10:23:53 -04004713 mutex_lock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004714 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004715
Tejun Heo82fe9b02013-06-25 11:53:37 -07004716 /* Add init_css_set to the hash table */
4717 key = css_set_hash(init_css_set.subsys);
4718 hash_add(css_set_table, &init_css_set.hlist, key);
4719
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004720 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004721
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004722 mutex_unlock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04004723 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004724
Tejun Heo172a2c062014-03-19 10:23:53 -04004725 for_each_subsys(ss, ssid) {
4726 if (!ss->early_init)
4727 cgroup_init_subsys(ss);
4728
Tejun Heo2d8f2432014-04-23 11:13:15 -04004729 list_add_tail(&init_css_set.e_cset_node[ssid],
4730 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4731
Tejun Heo172a2c062014-03-19 10:23:53 -04004732 /*
4733 * cftype registration needs kmalloc and can't be done
4734 * during early_init. Register base cftypes separately.
4735 */
4736 if (ss->base_cftypes)
4737 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4738 }
Greg KH676db4a2010-08-05 13:53:35 -07004739
Paul Menageddbcc7e2007-10-18 23:39:30 -07004740 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004741 if (!cgroup_kobj)
4742 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004743
4744 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004745 if (err < 0) {
4746 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004747 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004748 }
4749
4750 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004751 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004752}
Paul Menageb4f48b62007-10-18 23:39:33 -07004753
Tejun Heoe5fca242013-11-22 17:14:39 -05004754static int __init cgroup_wq_init(void)
4755{
4756 /*
4757 * There isn't much point in executing destruction path in
4758 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004759 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004760 *
4761 * We would prefer to do this in cgroup_init() above, but that
4762 * is called before init_workqueues(): so leave this until after.
4763 */
Tejun Heo1a115332014-02-12 19:06:19 -05004764 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004765 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004766
4767 /*
4768 * Used to destroy pidlists and separate to serve as flush domain.
4769 * Cap @max_active to 1 too.
4770 */
4771 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4772 0, 1);
4773 BUG_ON(!cgroup_pidlist_destroy_wq);
4774
Tejun Heoe5fca242013-11-22 17:14:39 -05004775 return 0;
4776}
4777core_initcall(cgroup_wq_init);
4778
Paul Menagea4243162007-10-18 23:39:35 -07004779/*
4780 * proc_cgroup_show()
4781 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4782 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004783 */
4784
4785/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004786int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004787{
4788 struct pid *pid;
4789 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004790 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004791 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004792 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004793
4794 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004795 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004796 if (!buf)
4797 goto out;
4798
4799 retval = -ESRCH;
4800 pid = m->private;
4801 tsk = get_pid_task(pid, PIDTYPE_PID);
4802 if (!tsk)
4803 goto out_free;
4804
4805 retval = 0;
4806
4807 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004808 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004809
Tejun Heo985ed672014-03-19 10:23:53 -04004810 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004811 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004812 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004813 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004814
Tejun Heoa2dd4242014-03-19 10:23:55 -04004815 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004816 continue;
4817
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004818 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004819 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004820 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004821 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004822 if (strlen(root->name))
4823 seq_printf(m, "%sname=%s", count ? "," : "",
4824 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004825 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004826 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004827 path = cgroup_path(cgrp, buf, PATH_MAX);
4828 if (!path) {
4829 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004830 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004831 }
4832 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004833 seq_putc(m, '\n');
4834 }
4835
4836out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004837 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004838 mutex_unlock(&cgroup_mutex);
4839 put_task_struct(tsk);
4840out_free:
4841 kfree(buf);
4842out:
4843 return retval;
4844}
4845
Paul Menagea4243162007-10-18 23:39:35 -07004846/* Display information about each subsystem and each hierarchy */
4847static int proc_cgroupstats_show(struct seq_file *m, void *v)
4848{
Tejun Heo30159ec2013-06-25 11:53:37 -07004849 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004850 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004851
Paul Menage8bab8dd2008-04-04 14:29:57 -07004852 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004853 /*
4854 * ideally we don't want subsystems moving around while we do this.
4855 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4856 * subsys/hierarchy state.
4857 */
Paul Menagea4243162007-10-18 23:39:35 -07004858 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004859
4860 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004861 seq_printf(m, "%s\t%d\t%d\t%d\n",
4862 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004863 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004864
Paul Menagea4243162007-10-18 23:39:35 -07004865 mutex_unlock(&cgroup_mutex);
4866 return 0;
4867}
4868
4869static int cgroupstats_open(struct inode *inode, struct file *file)
4870{
Al Viro9dce07f2008-03-29 03:07:28 +00004871 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004872}
4873
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004874static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004875 .open = cgroupstats_open,
4876 .read = seq_read,
4877 .llseek = seq_lseek,
4878 .release = single_release,
4879};
4880
Paul Menageb4f48b62007-10-18 23:39:33 -07004881/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004882 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004883 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004884 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004885 * A task is associated with the init_css_set until cgroup_post_fork()
4886 * attaches it to the parent's css_set. Empty cg_list indicates that
4887 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004888 */
4889void cgroup_fork(struct task_struct *child)
4890{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004891 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004892 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004893}
4894
4895/**
Li Zefana043e3b2008-02-23 15:24:09 -08004896 * cgroup_post_fork - called on a new task after adding it to the task list
4897 * @child: the task in question
4898 *
Tejun Heo5edee612012-10-16 15:03:14 -07004899 * Adds the task to the list running through its css_set if necessary and
4900 * call the subsystem fork() callbacks. Has to be after the task is
4901 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004902 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004903 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004904 */
Paul Menage817929e2007-10-18 23:39:36 -07004905void cgroup_post_fork(struct task_struct *child)
4906{
Tejun Heo30159ec2013-06-25 11:53:37 -07004907 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004908 int i;
4909
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004910 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004911 * This may race against cgroup_enable_task_cg_links(). As that
4912 * function sets use_task_css_set_links before grabbing
4913 * tasklist_lock and we just went through tasklist_lock to add
4914 * @child, it's guaranteed that either we see the set
4915 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4916 * @child during its iteration.
4917 *
4918 * If we won the race, @child is associated with %current's
4919 * css_set. Grabbing css_set_rwsem guarantees both that the
4920 * association is stable, and, on completion of the parent's
4921 * migration, @child is visible in the source of migration or
4922 * already in the destination cgroup. This guarantee is necessary
4923 * when implementing operations which need to migrate all tasks of
4924 * a cgroup to another.
4925 *
4926 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4927 * will remain in init_css_set. This is safe because all tasks are
4928 * in the init_css_set before cg_links is enabled and there's no
4929 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004930 */
Paul Menage817929e2007-10-18 23:39:36 -07004931 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004932 struct css_set *cset;
4933
Tejun Heo96d365e2014-02-13 06:58:40 -05004934 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004935 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004936 if (list_empty(&child->cg_list)) {
4937 rcu_assign_pointer(child->cgroups, cset);
4938 list_add(&child->cg_list, &cset->tasks);
4939 get_css_set(cset);
4940 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004941 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004942 }
Tejun Heo5edee612012-10-16 15:03:14 -07004943
4944 /*
4945 * Call ss->fork(). This must happen after @child is linked on
4946 * css_set; otherwise, @child might change state between ->fork()
4947 * and addition to css_set.
4948 */
4949 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004950 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004951 if (ss->fork)
4952 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004953 }
Paul Menage817929e2007-10-18 23:39:36 -07004954}
Tejun Heo5edee612012-10-16 15:03:14 -07004955
Paul Menage817929e2007-10-18 23:39:36 -07004956/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004957 * cgroup_exit - detach cgroup from exiting task
4958 * @tsk: pointer to task_struct of exiting process
4959 *
4960 * Description: Detach cgroup from @tsk and release it.
4961 *
4962 * Note that cgroups marked notify_on_release force every task in
4963 * them to take the global cgroup_mutex mutex when exiting.
4964 * This could impact scaling on very large systems. Be reluctant to
4965 * use notify_on_release cgroups where very high task exit scaling
4966 * is required on large systems.
4967 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004968 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4969 * call cgroup_exit() while the task is still competent to handle
4970 * notify_on_release(), then leave the task attached to the root cgroup in
4971 * each hierarchy for the remainder of its exit. No need to bother with
4972 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004973 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004974 */
Li Zefan1ec41832014-03-28 15:22:19 +08004975void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004976{
Tejun Heo30159ec2013-06-25 11:53:37 -07004977 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004978 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004979 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004980 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004981
4982 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05004983 * Unlink from @tsk from its css_set. As migration path can't race
4984 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004985 */
4986 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004987 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004988 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004989 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004990 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07004991 }
4992
Paul Menageb4f48b62007-10-18 23:39:33 -07004993 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07004994 cset = task_css_set(tsk);
4995 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004996
Li Zefan1ec41832014-03-28 15:22:19 +08004997 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004998 /* see cgroup_post_fork() for details */
4999 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005000 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005001 struct cgroup_subsys_state *old_css = cset->subsys[i];
5002 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005003
Tejun Heoeb954192013-08-08 20:11:23 -04005004 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005005 }
5006 }
5007 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005008
Tejun Heoeaf797a2014-02-25 10:04:03 -05005009 if (put_cset)
5010 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07005011}
Paul Menage697f4162007-10-18 23:39:34 -07005012
Paul Menagebd89aab2007-10-18 23:40:44 -07005013static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005014{
Li Zefanf50daa72013-03-01 15:06:07 +08005015 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005016 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005017 /*
5018 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005019 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005020 * it now
5021 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005022 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005023
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005024 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005025 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005026 list_empty(&cgrp->release_list)) {
5027 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005028 need_schedule_work = 1;
5029 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005030 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005031 if (need_schedule_work)
5032 schedule_work(&release_agent_work);
5033 }
5034}
5035
Paul Menage81a6a5c2007-10-18 23:39:38 -07005036/*
5037 * Notify userspace when a cgroup is released, by running the
5038 * configured release agent with the name of the cgroup (path
5039 * relative to the root of cgroup file system) as the argument.
5040 *
5041 * Most likely, this user command will try to rmdir this cgroup.
5042 *
5043 * This races with the possibility that some other task will be
5044 * attached to this cgroup before it is removed, or that some other
5045 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5046 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5047 * unused, and this cgroup will be reprieved from its death sentence,
5048 * to continue to serve a useful existence. Next time it's released,
5049 * we will get notified again, if it still has 'notify_on_release' set.
5050 *
5051 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5052 * means only wait until the task is successfully execve()'d. The
5053 * separate release agent task is forked by call_usermodehelper(),
5054 * then control in this thread returns here, without waiting for the
5055 * release agent task. We don't bother to wait because the caller of
5056 * this routine has no use for the exit status of the release agent
5057 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005058 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005059static void cgroup_release_agent(struct work_struct *work)
5060{
5061 BUG_ON(work != &release_agent_work);
5062 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005063 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005064 while (!list_empty(&release_list)) {
5065 char *argv[3], *envp[3];
5066 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005067 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005068 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005069 struct cgroup,
5070 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005071 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005072 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005073 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005074 if (!pathbuf)
5075 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005076 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5077 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005078 goto continue_free;
5079 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5080 if (!agentbuf)
5081 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005082
5083 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005084 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005085 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005086 argv[i] = NULL;
5087
5088 i = 0;
5089 /* minimal command environment */
5090 envp[i++] = "HOME=/";
5091 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5092 envp[i] = NULL;
5093
5094 /* Drop the lock while we invoke the usermode helper,
5095 * since the exec could involve hitting disk and hence
5096 * be a slow process */
5097 mutex_unlock(&cgroup_mutex);
5098 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005099 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005100 continue_free:
5101 kfree(pathbuf);
5102 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005103 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005104 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005105 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005106 mutex_unlock(&cgroup_mutex);
5107}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005108
5109static int __init cgroup_disable(char *str)
5110{
Tejun Heo30159ec2013-06-25 11:53:37 -07005111 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005112 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005113 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005114
5115 while ((token = strsep(&str, ",")) != NULL) {
5116 if (!*token)
5117 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005118
Tejun Heo3ed80a62014-02-08 10:36:58 -05005119 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005120 if (!strcmp(token, ss->name)) {
5121 ss->disabled = 1;
5122 printk(KERN_INFO "Disabling %s control group"
5123 " subsystem\n", ss->name);
5124 break;
5125 }
5126 }
5127 }
5128 return 1;
5129}
5130__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005131
Tejun Heob77d7b62013-08-13 11:01:54 -04005132/**
Tejun Heo5a17f542014-02-11 11:52:47 -05005133 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04005134 * @dentry: directory dentry of interest
5135 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005136 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005137 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5138 * to get the corresponding css and return it. If such css doesn't exist
5139 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005140 */
Tejun Heo5a17f542014-02-11 11:52:47 -05005141struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
5142 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005143{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005144 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5145 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005146 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005147
Tejun Heo35cf0832013-08-26 18:40:56 -04005148 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005149 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5150 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005151 return ERR_PTR(-EBADF);
5152
Tejun Heo5a17f542014-02-11 11:52:47 -05005153 rcu_read_lock();
5154
Tejun Heo2bd59d42014-02-11 11:52:49 -05005155 /*
5156 * This path doesn't originate from kernfs and @kn could already
5157 * have been or be removed at any point. @kn->priv is RCU
5158 * protected for this access. See destroy_locked() for details.
5159 */
5160 cgrp = rcu_dereference(kn->priv);
5161 if (cgrp)
5162 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005163
5164 if (!css || !css_tryget(css))
5165 css = ERR_PTR(-ENOENT);
5166
5167 rcu_read_unlock();
5168 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005169}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005170
Li Zefan1cb650b2013-08-19 10:05:24 +08005171/**
5172 * css_from_id - lookup css by id
5173 * @id: the cgroup id
5174 * @ss: cgroup subsys to be looked into
5175 *
5176 * Returns the css if there's valid one with @id, otherwise returns NULL.
5177 * Should be called under rcu_read_lock().
5178 */
5179struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5180{
5181 struct cgroup *cgrp;
5182
Tejun Heoace2bee2014-02-11 11:52:47 -05005183 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08005184
5185 cgrp = idr_find(&ss->root->cgroup_idr, id);
5186 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005187 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005188 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005189}
5190
Paul Menagefe693432009-09-23 15:56:20 -07005191#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005192static struct cgroup_subsys_state *
5193debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005194{
5195 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5196
5197 if (!css)
5198 return ERR_PTR(-ENOMEM);
5199
5200 return css;
5201}
5202
Tejun Heoeb954192013-08-08 20:11:23 -04005203static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005204{
Tejun Heoeb954192013-08-08 20:11:23 -04005205 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005206}
5207
Tejun Heo182446d2013-08-08 20:11:24 -04005208static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5209 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005210{
Tejun Heo182446d2013-08-08 20:11:24 -04005211 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005212}
5213
Tejun Heo182446d2013-08-08 20:11:24 -04005214static u64 current_css_set_read(struct cgroup_subsys_state *css,
5215 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005216{
5217 return (u64)(unsigned long)current->cgroups;
5218}
5219
Tejun Heo182446d2013-08-08 20:11:24 -04005220static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005221 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005222{
5223 u64 count;
5224
5225 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005226 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005227 rcu_read_unlock();
5228 return count;
5229}
5230
Tejun Heo2da8ca82013-12-05 12:28:04 -05005231static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005232{
Tejun Heo69d02062013-06-12 21:04:50 -07005233 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005234 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005235 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005236
Tejun Heoe61734c2014-02-12 09:29:50 -05005237 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5238 if (!name_buf)
5239 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005240
Tejun Heo96d365e2014-02-13 06:58:40 -05005241 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005242 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005243 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005244 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005245 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005246
Tejun Heoa2dd4242014-03-19 10:23:55 -04005247 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005248 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005249 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005250 }
5251 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005252 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005253 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005254 return 0;
5255}
5256
5257#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005258static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005259{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005260 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005261 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005262
Tejun Heo96d365e2014-02-13 06:58:40 -05005263 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005264 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005265 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005266 struct task_struct *task;
5267 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005268
Tejun Heo5abb8852013-06-12 21:04:49 -07005269 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005270
Tejun Heo5abb8852013-06-12 21:04:49 -07005271 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005272 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5273 goto overflow;
5274 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005275 }
Tejun Heoc7561122014-02-25 10:04:01 -05005276
5277 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5278 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5279 goto overflow;
5280 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5281 }
5282 continue;
5283 overflow:
5284 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005285 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005286 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005287 return 0;
5288}
5289
Tejun Heo182446d2013-08-08 20:11:24 -04005290static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005291{
Tejun Heo182446d2013-08-08 20:11:24 -04005292 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005293}
5294
5295static struct cftype debug_files[] = {
5296 {
Paul Menagefe693432009-09-23 15:56:20 -07005297 .name = "taskcount",
5298 .read_u64 = debug_taskcount_read,
5299 },
5300
5301 {
5302 .name = "current_css_set",
5303 .read_u64 = current_css_set_read,
5304 },
5305
5306 {
5307 .name = "current_css_set_refcount",
5308 .read_u64 = current_css_set_refcount_read,
5309 },
5310
5311 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005312 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005313 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005314 },
5315
5316 {
5317 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005318 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005319 },
5320
5321 {
Paul Menagefe693432009-09-23 15:56:20 -07005322 .name = "releasable",
5323 .read_u64 = releasable_read,
5324 },
Paul Menagefe693432009-09-23 15:56:20 -07005325
Tejun Heo4baf6e32012-04-01 12:09:55 -07005326 { } /* terminate */
5327};
Paul Menagefe693432009-09-23 15:56:20 -07005328
Tejun Heo073219e2014-02-08 10:36:58 -05005329struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005330 .css_alloc = debug_css_alloc,
5331 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005332 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005333};
5334#endif /* CONFIG_CGROUP_DEBUG */