blob: fede3d3f28ffaa5c998333706d5233620d0734db [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070043#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/spinlock.h>
Tejun Heo96d365e2014-02-13 06:58:40 -050045#include <linux/rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070047#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070048#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070049#include <linux/delayacct.h>
50#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080051#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070052#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070053#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070054#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020055#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050056#include <linux/delay.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070057
Arun Sharma600634972011-07-26 16:09:06 -070058#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070059
Tejun Heoe25e2cb2011-12-12 18:12:21 -080060/*
Tejun Heob1a21362013-11-29 10:42:58 -050061 * pidlists linger the following amount before being destroyed. The goal
62 * is avoiding frequent destruction in the middle of consecutive read calls
63 * Expiring in the middle is a performance problem not a correctness one.
64 * 1 sec should be enough.
65 */
66#define CGROUP_PIDLIST_DESTROY_DELAY HZ
67
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050068#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
69 MAX_CFTYPE_NAME + 2)
70
Tejun Heob1a21362013-11-29 10:42:58 -050071/*
Tejun Heoace2bee2014-02-11 11:52:47 -050072 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
73 * creation/removal and hierarchy changing operations including cgroup
74 * creation, removal, css association and controller rebinding. This outer
75 * lock is needed mainly to resolve the circular dependency between kernfs
76 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
77 */
78static DEFINE_MUTEX(cgroup_tree_mutex);
79
Tejun Heoe25e2cb2011-12-12 18:12:21 -080080/*
81 * cgroup_mutex is the master lock. Any modification to cgroup or its
82 * hierarchy must be performed while holding it.
83 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050084 * css_set_rwsem protects task->cgroups pointer, the list of css_set
85 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080086 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050087 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
88 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080089 */
Tejun Heo22194492013-04-07 09:29:51 -070090#ifdef CONFIG_PROVE_RCU
91DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050092DECLARE_RWSEM(css_set_rwsem);
93EXPORT_SYMBOL_GPL(cgroup_mutex);
94EXPORT_SYMBOL_GPL(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070095#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070096static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050097static DECLARE_RWSEM(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070098#endif
99
Tejun Heo69e943b2014-02-08 10:36:58 -0500100/*
101 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
102 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
103 */
104static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700105
Tejun Heoace2bee2014-02-11 11:52:47 -0500106#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500107 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -0500108 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500109 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500110 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500111
Ben Blumaae8aab2010-03-10 15:22:07 -0800112/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500113 * cgroup destruction makes heavy use of work items and there can be a lot
114 * of concurrent destructions. Use a separate workqueue so that cgroup
115 * destruction work items don't end up filling up max_active of system_wq
116 * which may lead to deadlock.
117 */
118static struct workqueue_struct *cgroup_destroy_wq;
119
120/*
Tejun Heob1a21362013-11-29 10:42:58 -0500121 * pidlist destructions need to be flushed on cgroup destruction. Use a
122 * separate workqueue as flush domain.
123 */
124static struct workqueue_struct *cgroup_pidlist_destroy_wq;
125
Tejun Heo3ed80a62014-02-08 10:36:58 -0500126/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500127#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500128static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700129#include <linux/cgroup_subsys.h>
130};
Tejun Heo073219e2014-02-08 10:36:58 -0500131#undef SUBSYS
132
133/* array of cgroup subsystem names */
134#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
135static const char *cgroup_subsys_name[] = {
136#include <linux/cgroup_subsys.h>
137};
138#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700139
Paul Menageddbcc7e2007-10-18 23:39:30 -0700140/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400141 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700142 * unattached - it never has more than a single cgroup, and all tasks are
143 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700144 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400145struct cgroup_root cgrp_dfl_root;
Tejun Heo9871bf92013-06-24 15:21:47 -0700146
Tejun Heoa2dd4242014-03-19 10:23:55 -0400147/*
148 * The default hierarchy always exists but is hidden until mounted for the
149 * first time. This is for backward compatibility.
150 */
151static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700152
153/* The list of hierarchy roots */
154
Tejun Heo9871bf92013-06-24 15:21:47 -0700155static LIST_HEAD(cgroup_roots);
156static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700157
Tejun Heo3417ae12014-02-08 10:37:01 -0500158/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700159static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700160
Li Zefan794611a2013-06-18 18:53:53 +0800161/*
162 * Assign a monotonically increasing serial number to cgroups. It
163 * guarantees cgroups with bigger numbers are newer than those with smaller
164 * numbers. Also, as cgroups are always appended to the parent's
165 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700166 * the ascending serial number order on the list. Protected by
167 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800168 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700169static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800170
Paul Menageddbcc7e2007-10-18 23:39:30 -0700171/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800172 * check for fork/exit handlers to call. This avoids us having to do
173 * extra work in the fork/exit path if none of the subsystems need to
174 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700175 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700176static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700177
Tejun Heo628f7cd2013-06-28 16:24:11 -0700178static struct cftype cgroup_base_files[];
179
Tejun Heo59f52962014-02-11 11:52:49 -0500180static void cgroup_put(struct cgroup *cgrp);
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400181static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo5df36032014-03-19 10:23:54 -0400182 unsigned long ss_mask);
Tejun Heof20104d2013-08-13 20:22:50 -0400183static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800184static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400185static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
186 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500187static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800188
Tejun Heo95109b62013-08-08 20:11:27 -0400189/**
190 * cgroup_css - obtain a cgroup's css for the specified subsystem
191 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400192 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400193 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400194 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
195 * function must be called either under cgroup_mutex or rcu_read_lock() and
196 * the caller is responsible for pinning the returned css if it wants to
197 * keep accessing it outside the said locks. This function may return
198 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400199 */
200static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400201 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400202{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400203 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500204 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500205 lockdep_is_held(&cgroup_tree_mutex) ||
206 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400207 else
208 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400209}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700210
Paul Menageddbcc7e2007-10-18 23:39:30 -0700211/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700212static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700213{
Tejun Heo54766d42013-06-12 21:04:53 -0700214 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700215}
216
Tejun Heo59f52962014-02-11 11:52:49 -0500217struct cgroup_subsys_state *seq_css(struct seq_file *seq)
218{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500219 struct kernfs_open_file *of = seq->private;
220 struct cgroup *cgrp = of->kn->parent->priv;
221 struct cftype *cft = seq_cft(seq);
222
223 /*
224 * This is open and unprotected implementation of cgroup_css().
225 * seq_css() is only called from a kernfs file operation which has
226 * an active reference on the file. Because all the subsystem
227 * files are drained before a css is disassociated with a cgroup,
228 * the matching css from the cgroup's subsys table is guaranteed to
229 * be and stay valid until the enclosing operation is complete.
230 */
231 if (cft->ss)
232 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
233 else
234 return &cgrp->dummy_css;
Tejun Heo59f52962014-02-11 11:52:49 -0500235}
236EXPORT_SYMBOL_GPL(seq_css);
237
Li Zefan78574cf2013-04-08 19:00:38 -0700238/**
239 * cgroup_is_descendant - test ancestry
240 * @cgrp: the cgroup to be tested
241 * @ancestor: possible ancestor of @cgrp
242 *
243 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
244 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
245 * and @ancestor are accessible.
246 */
247bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
248{
249 while (cgrp) {
250 if (cgrp == ancestor)
251 return true;
252 cgrp = cgrp->parent;
253 }
254 return false;
255}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700256
Adrian Bunke9685a02008-02-07 00:13:46 -0800257static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700258{
259 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700260 (1 << CGRP_RELEASABLE) |
261 (1 << CGRP_NOTIFY_ON_RELEASE);
262 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700263}
264
Adrian Bunke9685a02008-02-07 00:13:46 -0800265static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700266{
Paul Menagebd89aab2007-10-18 23:40:44 -0700267 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700268}
269
Tejun Heo30159ec2013-06-25 11:53:37 -0700270/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500271 * for_each_css - iterate all css's of a cgroup
272 * @css: the iteration cursor
273 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
274 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700275 *
276 * Should be called under cgroup_mutex.
277 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500278#define for_each_css(css, ssid, cgrp) \
279 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
280 if (!((css) = rcu_dereference_check( \
281 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500282 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500283 lockdep_is_held(&cgroup_mutex)))) { } \
284 else
285
286/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500287 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700288 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500289 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700290 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500291#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500292 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
293 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700294
Tejun Heo985ed672014-03-19 10:23:53 -0400295/* iterate across the hierarchies */
296#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700297 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700298
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700299/**
300 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
301 * @cgrp: the cgroup to be checked for liveness
302 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700303 * On success, returns true; the mutex should be later unlocked. On
304 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700305 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700306static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700307{
308 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700309 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700310 mutex_unlock(&cgroup_mutex);
311 return false;
312 }
313 return true;
314}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700315
Paul Menage81a6a5c2007-10-18 23:39:38 -0700316/* the list of cgroups eligible for automatic release. Protected by
317 * release_list_lock */
318static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200319static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700320static void cgroup_release_agent(struct work_struct *work);
321static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700322static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700323
Tejun Heo69d02062013-06-12 21:04:50 -0700324/*
325 * A cgroup can be associated with multiple css_sets as different tasks may
326 * belong to different cgroups on different hierarchies. In the other
327 * direction, a css_set is naturally associated with multiple cgroups.
328 * This M:N relationship is represented by the following link structure
329 * which exists for each association and allows traversing the associations
330 * from both sides.
331 */
332struct cgrp_cset_link {
333 /* the cgroup and css_set this link associates */
334 struct cgroup *cgrp;
335 struct css_set *cset;
336
337 /* list of cgrp_cset_links anchored at cgrp->cset_links */
338 struct list_head cset_link;
339
340 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
341 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700342};
343
Tejun Heo172a2c062014-03-19 10:23:53 -0400344/*
345 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700346 * hierarchies being mounted. It contains a pointer to the root state
347 * for each subsystem. Also used to anchor the list of css_sets. Not
348 * reference-counted, to improve performance when child cgroups
349 * haven't been created.
350 */
Tejun Heo172a2c062014-03-19 10:23:53 -0400351static struct css_set init_css_set = {
352 .refcount = ATOMIC_INIT(1),
353 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
354 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
355 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
356 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
357 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
358};
Paul Menage817929e2007-10-18 23:39:36 -0700359
Tejun Heo172a2c062014-03-19 10:23:53 -0400360static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700361
Paul Menage7717f7b2009-09-23 15:56:22 -0700362/*
363 * hash table for cgroup groups. This improves the performance to find
364 * an existing css_set. This hash doesn't (currently) take into
365 * account cgroups in empty hierarchies.
366 */
Li Zefan472b1052008-04-29 01:00:11 -0700367#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800368static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700369
Li Zefan0ac801f2013-01-10 11:49:27 +0800370static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700371{
Li Zefan0ac801f2013-01-10 11:49:27 +0800372 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700373 struct cgroup_subsys *ss;
374 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700375
Tejun Heo30159ec2013-06-25 11:53:37 -0700376 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800377 key += (unsigned long)css[i];
378 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700379
Li Zefan0ac801f2013-01-10 11:49:27 +0800380 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700381}
382
Tejun Heo89c55092014-02-13 06:58:40 -0500383static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700384{
Tejun Heo69d02062013-06-12 21:04:50 -0700385 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700386
Tejun Heo89c55092014-02-13 06:58:40 -0500387 lockdep_assert_held(&css_set_rwsem);
388
389 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700390 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700391
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700392 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700393 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700394 css_set_count--;
395
Tejun Heo69d02062013-06-12 21:04:50 -0700396 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700397 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700398
Tejun Heo69d02062013-06-12 21:04:50 -0700399 list_del(&link->cset_link);
400 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800401
Tejun Heo96d365e2014-02-13 06:58:40 -0500402 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700403 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700404 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700405 set_bit(CGRP_RELEASABLE, &cgrp->flags);
406 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700407 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700408
409 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700410 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700411
Tejun Heo5abb8852013-06-12 21:04:49 -0700412 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700413}
414
Tejun Heo89c55092014-02-13 06:58:40 -0500415static void put_css_set(struct css_set *cset, bool taskexit)
416{
417 /*
418 * Ensure that the refcount doesn't hit zero while any readers
419 * can see it. Similar to atomic_dec_and_lock(), but for an
420 * rwlock
421 */
422 if (atomic_add_unless(&cset->refcount, -1, 1))
423 return;
424
425 down_write(&css_set_rwsem);
426 put_css_set_locked(cset, taskexit);
427 up_write(&css_set_rwsem);
428}
429
Paul Menage817929e2007-10-18 23:39:36 -0700430/*
431 * refcounted get/put for css_set objects
432 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700433static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700434{
Tejun Heo5abb8852013-06-12 21:04:49 -0700435 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700436}
437
Tejun Heob326f9d2013-06-24 15:21:48 -0700438/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700439 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700440 * @cset: candidate css_set being tested
441 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700442 * @new_cgrp: cgroup that's being entered by the task
443 * @template: desired set of css pointers in css_set (pre-calculated)
444 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800445 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700446 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
447 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700448static bool compare_css_sets(struct css_set *cset,
449 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700450 struct cgroup *new_cgrp,
451 struct cgroup_subsys_state *template[])
452{
453 struct list_head *l1, *l2;
454
Tejun Heo5abb8852013-06-12 21:04:49 -0700455 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700456 /* Not all subsystems matched */
457 return false;
458 }
459
460 /*
461 * Compare cgroup pointers in order to distinguish between
462 * different cgroups in heirarchies with no subsystems. We
463 * could get by with just this check alone (and skip the
464 * memcmp above) but on most setups the memcmp check will
465 * avoid the need for this more expensive check on almost all
466 * candidates.
467 */
468
Tejun Heo69d02062013-06-12 21:04:50 -0700469 l1 = &cset->cgrp_links;
470 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700471 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700472 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700473 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700474
475 l1 = l1->next;
476 l2 = l2->next;
477 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700478 if (l1 == &cset->cgrp_links) {
479 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700480 break;
481 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700482 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700483 }
484 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700485 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
486 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
487 cgrp1 = link1->cgrp;
488 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700489 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700490 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700491
492 /*
493 * If this hierarchy is the hierarchy of the cgroup
494 * that's changing, then we need to check that this
495 * css_set points to the new cgroup; if it's any other
496 * hierarchy, then this css_set should point to the
497 * same cgroup as the old css_set.
498 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700499 if (cgrp1->root == new_cgrp->root) {
500 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700501 return false;
502 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700503 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700504 return false;
505 }
506 }
507 return true;
508}
509
Tejun Heob326f9d2013-06-24 15:21:48 -0700510/**
511 * find_existing_css_set - init css array and find the matching css_set
512 * @old_cset: the css_set that we're using before the cgroup transition
513 * @cgrp: the cgroup that we're moving into
514 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700515 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700516static struct css_set *find_existing_css_set(struct css_set *old_cset,
517 struct cgroup *cgrp,
518 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700519{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400520 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700521 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700522 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800523 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700524 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700525
Ben Blumaae8aab2010-03-10 15:22:07 -0800526 /*
527 * Build the set of subsystem state objects that we want to see in the
528 * new css_set. while subsystems can change globally, the entries here
529 * won't change, so no need for locking.
530 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700531 for_each_subsys(ss, i) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400532 if (root->cgrp.subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700533 /* Subsystem is in this hierarchy. So we want
534 * the subsystem state from the new
535 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400536 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700537 } else {
538 /* Subsystem is not in this hierarchy, so we
539 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700540 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700541 }
542 }
543
Li Zefan0ac801f2013-01-10 11:49:27 +0800544 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700545 hash_for_each_possible(css_set_table, cset, hlist, key) {
546 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700547 continue;
548
549 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700550 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700551 }
Paul Menage817929e2007-10-18 23:39:36 -0700552
553 /* No existing cgroup group matched */
554 return NULL;
555}
556
Tejun Heo69d02062013-06-12 21:04:50 -0700557static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700558{
Tejun Heo69d02062013-06-12 21:04:50 -0700559 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700560
Tejun Heo69d02062013-06-12 21:04:50 -0700561 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
562 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700563 kfree(link);
564 }
565}
566
Tejun Heo69d02062013-06-12 21:04:50 -0700567/**
568 * allocate_cgrp_cset_links - allocate cgrp_cset_links
569 * @count: the number of links to allocate
570 * @tmp_links: list_head the allocated links are put on
571 *
572 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
573 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700574 */
Tejun Heo69d02062013-06-12 21:04:50 -0700575static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700576{
Tejun Heo69d02062013-06-12 21:04:50 -0700577 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700578 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700579
580 INIT_LIST_HEAD(tmp_links);
581
Li Zefan36553432008-07-29 22:33:19 -0700582 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700583 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700584 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700585 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700586 return -ENOMEM;
587 }
Tejun Heo69d02062013-06-12 21:04:50 -0700588 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700589 }
590 return 0;
591}
592
Li Zefanc12f65d2009-01-07 18:07:42 -0800593/**
594 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700595 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700596 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800597 * @cgrp: the destination cgroup
598 */
Tejun Heo69d02062013-06-12 21:04:50 -0700599static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
600 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800601{
Tejun Heo69d02062013-06-12 21:04:50 -0700602 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800603
Tejun Heo69d02062013-06-12 21:04:50 -0700604 BUG_ON(list_empty(tmp_links));
605 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
606 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700607 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700608 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700609 /*
610 * Always add links to the tail of the list so that the list
611 * is sorted by order of hierarchy creation
612 */
Tejun Heo69d02062013-06-12 21:04:50 -0700613 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800614}
615
Tejun Heob326f9d2013-06-24 15:21:48 -0700616/**
617 * find_css_set - return a new css_set with one cgroup updated
618 * @old_cset: the baseline css_set
619 * @cgrp: the cgroup to be updated
620 *
621 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
622 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700623 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700624static struct css_set *find_css_set(struct css_set *old_cset,
625 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700626{
Tejun Heob326f9d2013-06-24 15:21:48 -0700627 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700628 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700629 struct list_head tmp_links;
630 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800631 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700632
Tejun Heob326f9d2013-06-24 15:21:48 -0700633 lockdep_assert_held(&cgroup_mutex);
634
Paul Menage817929e2007-10-18 23:39:36 -0700635 /* First see if we already have a cgroup group that matches
636 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500637 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700638 cset = find_existing_css_set(old_cset, cgrp, template);
639 if (cset)
640 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500641 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700642
Tejun Heo5abb8852013-06-12 21:04:49 -0700643 if (cset)
644 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700645
Tejun Heof4f4be22013-06-12 21:04:51 -0700646 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700647 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700648 return NULL;
649
Tejun Heo69d02062013-06-12 21:04:50 -0700650 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700651 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700652 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700653 return NULL;
654 }
655
Tejun Heo5abb8852013-06-12 21:04:49 -0700656 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700657 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700658 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500659 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500660 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500661 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700662 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700663
664 /* Copy the set of subsystem state objects generated in
665 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700666 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700667
Tejun Heo96d365e2014-02-13 06:58:40 -0500668 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700669 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700670 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700671 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700672
Paul Menage7717f7b2009-09-23 15:56:22 -0700673 if (c->root == cgrp->root)
674 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700675 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700676 }
Paul Menage817929e2007-10-18 23:39:36 -0700677
Tejun Heo69d02062013-06-12 21:04:50 -0700678 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700679
Paul Menage817929e2007-10-18 23:39:36 -0700680 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700681
682 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700683 key = css_set_hash(cset->subsys);
684 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700685
Tejun Heo96d365e2014-02-13 06:58:40 -0500686 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700687
Tejun Heo5abb8852013-06-12 21:04:49 -0700688 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700689}
690
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400691static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700692{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400693 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500694
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400695 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500696}
697
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400698static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500699{
700 int id;
701
702 lockdep_assert_held(&cgroup_mutex);
703
Tejun Heo985ed672014-03-19 10:23:53 -0400704 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500705 if (id < 0)
706 return id;
707
708 root->hierarchy_id = id;
709 return 0;
710}
711
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400712static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500713{
714 lockdep_assert_held(&cgroup_mutex);
715
716 if (root->hierarchy_id) {
717 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
718 root->hierarchy_id = 0;
719 }
720}
721
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400722static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500723{
724 if (root) {
725 /* hierarhcy ID shoulid already have been released */
726 WARN_ON_ONCE(root->hierarchy_id);
727
728 idr_destroy(&root->cgroup_idr);
729 kfree(root);
730 }
731}
732
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400733static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500734{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400735 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500736 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500737
Tejun Heo2bd59d42014-02-11 11:52:49 -0500738 mutex_lock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500739 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500740
Tejun Heo776f02f2014-02-12 09:29:50 -0500741 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500742 BUG_ON(!list_empty(&cgrp->children));
743
Tejun Heof2e85d52014-02-11 11:52:49 -0500744 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400745 rebind_subsystems(&cgrp_dfl_root, cgrp->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500746
747 /*
748 * Release all the links from cset_links to this hierarchy's
749 * root cgroup
750 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500751 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500752
753 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
754 list_del(&link->cset_link);
755 list_del(&link->cgrp_link);
756 kfree(link);
757 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500758 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500759
760 if (!list_empty(&root->root_list)) {
761 list_del(&root->root_list);
762 cgroup_root_count--;
763 }
764
765 cgroup_exit_root_id(root);
766
767 mutex_unlock(&cgroup_mutex);
768 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500769
Tejun Heo2bd59d42014-02-11 11:52:49 -0500770 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500771 cgroup_free_root(root);
772}
773
Tejun Heoceb6a082014-02-25 10:04:02 -0500774/* look up cgroup associated with given css_set on the specified hierarchy */
775static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400776 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700777{
Paul Menage7717f7b2009-09-23 15:56:22 -0700778 struct cgroup *res = NULL;
779
Tejun Heo96d365e2014-02-13 06:58:40 -0500780 lockdep_assert_held(&cgroup_mutex);
781 lockdep_assert_held(&css_set_rwsem);
782
Tejun Heo5abb8852013-06-12 21:04:49 -0700783 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400784 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700785 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700786 struct cgrp_cset_link *link;
787
788 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700789 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700790
Paul Menage7717f7b2009-09-23 15:56:22 -0700791 if (c->root == root) {
792 res = c;
793 break;
794 }
795 }
796 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500797
Paul Menage7717f7b2009-09-23 15:56:22 -0700798 BUG_ON(!res);
799 return res;
800}
801
802/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500803 * Return the cgroup for "task" from the given hierarchy. Must be
804 * called with cgroup_mutex and css_set_rwsem held.
805 */
806static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400807 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500808{
809 /*
810 * No need to lock the task - since we hold cgroup_mutex the
811 * task can't change groups, so the only thing that can happen
812 * is that it exits and its css is set back to init_css_set.
813 */
814 return cset_cgroup_from_root(task_css_set(task), root);
815}
816
817/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700818 * A task must hold cgroup_mutex to modify cgroups.
819 *
820 * Any task can increment and decrement the count field without lock.
821 * So in general, code holding cgroup_mutex can't rely on the count
822 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800823 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700824 * means that no tasks are currently attached, therefore there is no
825 * way a task attached to that cgroup can fork (the other way to
826 * increment the count). So code holding cgroup_mutex can safely
827 * assume that if the count is zero, it will stay zero. Similarly, if
828 * a task holds cgroup_mutex on a cgroup with zero count, it
829 * knows that the cgroup won't be removed, as cgroup_rmdir()
830 * needs that mutex.
831 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700832 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
833 * (usually) take cgroup_mutex. These are the two most performance
834 * critical pieces of code here. The exception occurs on cgroup_exit(),
835 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
836 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800837 * to the release agent with the name of the cgroup (path relative to
838 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700839 *
840 * A cgroup can only be deleted if both its 'count' of using tasks
841 * is zero, and its list of 'children' cgroups is empty. Since all
842 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400843 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700844 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400845 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700846 *
847 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800848 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700849 */
850
Tejun Heo628f7cd2013-06-28 16:24:11 -0700851static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500852static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700853static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700854
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500855static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
856 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700857{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500858 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
859 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
860 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
861 cft->ss->name, cft->name);
862 else
863 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
864 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700865}
866
Tejun Heof2e85d52014-02-11 11:52:49 -0500867/**
868 * cgroup_file_mode - deduce file mode of a control file
869 * @cft: the control file in question
870 *
871 * returns cft->mode if ->mode is not 0
872 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
873 * returns S_IRUGO if it has only a read handler
874 * returns S_IWUSR if it has only a write hander
875 */
876static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +0800877{
Tejun Heof2e85d52014-02-11 11:52:49 -0500878 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +0800879
Tejun Heof2e85d52014-02-11 11:52:49 -0500880 if (cft->mode)
881 return cft->mode;
882
883 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
884 mode |= S_IRUGO;
885
886 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
887 cft->trigger)
888 mode |= S_IWUSR;
889
890 return mode;
Li Zefan65dff752013-03-01 15:01:56 +0800891}
892
Li Zefanbe445622013-01-24 14:31:42 +0800893static void cgroup_free_fn(struct work_struct *work)
894{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700895 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800896
Tejun Heo3c9c8252014-02-12 09:29:50 -0500897 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -0500898 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800899
Tejun Heo776f02f2014-02-12 09:29:50 -0500900 if (cgrp->parent) {
901 /*
902 * We get a ref to the parent, and put the ref when this
903 * cgroup is being freed, so it's guaranteed that the
904 * parent won't be destroyed before its children.
905 */
906 cgroup_put(cgrp->parent);
907 kernfs_put(cgrp->kn);
908 kfree(cgrp);
909 } else {
910 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400911 * This is root cgroup's refcnt reaching zero, which
Tejun Heo776f02f2014-02-12 09:29:50 -0500912 * indicates that the root should be released.
913 */
914 cgroup_destroy_root(cgrp->root);
915 }
Li Zefanbe445622013-01-24 14:31:42 +0800916}
917
918static void cgroup_free_rcu(struct rcu_head *head)
919{
920 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
921
Tejun Heoea15f8c2013-06-13 19:27:42 -0700922 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500923 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800924}
925
Tejun Heo59f52962014-02-11 11:52:49 -0500926static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700927{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500928 WARN_ON_ONCE(cgroup_is_dead(cgrp));
929 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
930 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700931}
932
Tejun Heo59f52962014-02-11 11:52:49 -0500933static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700934{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500935 if (!atomic_dec_and_test(&cgrp->refcnt))
936 return;
Tejun Heo776f02f2014-02-12 09:29:50 -0500937 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -0500938 return;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700939
Tejun Heo2bd59d42014-02-11 11:52:49 -0500940 /*
941 * XXX: cgrp->id is only used to look up css's. As cgroup and
942 * css's lifetimes will be decoupled, it should be made
943 * per-subsystem and moved to css->id so that lookups are
944 * successful until the target css is released.
945 */
946 mutex_lock(&cgroup_mutex);
947 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
948 mutex_unlock(&cgroup_mutex);
949 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700950
Tejun Heo2bd59d42014-02-11 11:52:49 -0500951 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700952}
953
Li Zefan2739d3c2013-01-21 18:18:33 +0800954static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700955{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500956 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700957
Tejun Heoace2bee2014-02-11 11:52:47 -0500958 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500959 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -0700960}
961
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400962/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700963 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700964 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400965 * @subsys_mask: mask of the subsystem ids whose files should be removed
966 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700967static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700968{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400969 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700970 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700971
Tejun Heob420ba72013-07-12 12:34:02 -0700972 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -0500973 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -0700974
975 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400976 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -0500977 list_for_each_entry(cfts, &ss->cfts, node)
978 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400979 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980}
981
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400982static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo5df36032014-03-19 10:23:54 -0400983 unsigned long ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984{
Tejun Heo30159ec2013-06-25 11:53:37 -0700985 struct cgroup_subsys *ss;
Tejun Heo5df36032014-03-19 10:23:54 -0400986 int ssid, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987
Tejun Heoace2bee2014-02-11 11:52:47 -0500988 lockdep_assert_held(&cgroup_tree_mutex);
989 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -0800990
Tejun Heo5df36032014-03-19 10:23:54 -0400991 for_each_subsys(ss, ssid) {
992 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -0700994
Tejun Heo5df36032014-03-19 10:23:54 -0400995 /* if @ss is on the dummy_root, we can always move it */
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400996 if (ss->root == &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -0400997 continue;
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700998
Tejun Heo5df36032014-03-19 10:23:54 -0400999 /* if @ss has non-root cgroups attached to it, can't move */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001000 if (!list_empty(&ss->root->cgrp.children))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001001 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002
Tejun Heo5df36032014-03-19 10:23:54 -04001003 /* can't move between two non-dummy roots either */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001004 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001005 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006 }
1007
Tejun Heoa2dd4242014-03-19 10:23:55 -04001008 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1009 if (ret) {
1010 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001011 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012
Tejun Heoa2dd4242014-03-19 10:23:55 -04001013 /*
1014 * Rebinding back to the default root is not allowed to
1015 * fail. Using both default and non-default roots should
1016 * be rare. Moving subsystems back and forth even more so.
1017 * Just warn about it and continue.
1018 */
1019 if (cgrp_dfl_root_visible) {
1020 pr_warning("cgroup: failed to create files (%d) while rebinding 0x%lx to default root\n",
1021 ret, ss_mask);
1022 pr_warning("cgroup: you may retry by moving them to a different hierarchy and unbinding\n");
1023 }
Tejun Heo5df36032014-03-19 10:23:54 -04001024 }
Tejun Heo31261212013-06-28 17:07:30 -07001025
1026 /*
1027 * Nothing can fail from this point on. Remove files for the
1028 * removed subsystems and rebind each subsystem.
1029 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001030 mutex_unlock(&cgroup_mutex);
Tejun Heo5df36032014-03-19 10:23:54 -04001031 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001032 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001033 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo4ac06012014-02-11 11:52:47 -05001034 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001035
Tejun Heo5df36032014-03-19 10:23:54 -04001036 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001037 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001038 struct cgroup_subsys_state *css;
Tejun Heo30159ec2013-06-25 11:53:37 -07001039
Tejun Heo5df36032014-03-19 10:23:54 -04001040 if (!(ss_mask & (1 << ssid)))
1041 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001042
Tejun Heo5df36032014-03-19 10:23:54 -04001043 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001044 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001045
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001046 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001047
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001048 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1049 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001050 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001051 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001052
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001053 src_root->cgrp.subsys_mask &= ~(1 << ssid);
1054 dst_root->cgrp.subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001055
Tejun Heo5df36032014-03-19 10:23:54 -04001056 if (ss->bind)
1057 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059
Tejun Heoa2dd4242014-03-19 10:23:55 -04001060 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 return 0;
1062}
1063
Tejun Heo2bd59d42014-02-11 11:52:49 -05001064static int cgroup_show_options(struct seq_file *seq,
1065 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001067 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001068 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001069 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070
Tejun Heob85d2042013-12-06 15:11:57 -05001071 for_each_subsys(ss, ssid)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001072 if (root->cgrp.subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001073 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001074 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1075 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001076 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001078 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001079 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001080
1081 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001082 if (strlen(root->release_agent_path))
1083 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001084 spin_unlock(&release_agent_path_lock);
1085
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001086 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001087 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001088 if (strlen(root->name))
1089 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001090 return 0;
1091}
1092
1093struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001094 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001096 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001097 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001098 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001099 /* User explicitly requested empty subsystem */
1100 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101};
1102
Ben Blumaae8aab2010-03-10 15:22:07 -08001103/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001104 * Convert a hierarchy specifier into a bitmask of subsystems and
1105 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1106 * array. This function takes refcounts on subsystems to be used, unless it
1107 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001108 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001109static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001111 char *token, *o = data;
1112 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001113 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001114 struct cgroup_subsys *ss;
1115 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001116
Ben Blumaae8aab2010-03-10 15:22:07 -08001117 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1118
Li Zefanf9ab5b52009-06-17 16:26:33 -07001119#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001120 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001121#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001122
Paul Menagec6d57f32009-09-23 15:56:19 -07001123 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124
1125 while ((token = strsep(&o, ",")) != NULL) {
1126 if (!*token)
1127 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001128 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001129 /* Explicitly have no subsystems */
1130 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001131 continue;
1132 }
1133 if (!strcmp(token, "all")) {
1134 /* Mutually exclusive option 'all' + subsystem name */
1135 if (one_ss)
1136 return -EINVAL;
1137 all_ss = true;
1138 continue;
1139 }
Tejun Heo873fe092013-04-14 20:15:26 -07001140 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1141 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1142 continue;
1143 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001144 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001145 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001146 continue;
1147 }
1148 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001149 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001150 continue;
1151 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001152 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001153 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001154 continue;
1155 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001156 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001157 /* Specifying two release agents is forbidden */
1158 if (opts->release_agent)
1159 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001160 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001161 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001162 if (!opts->release_agent)
1163 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001164 continue;
1165 }
1166 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001167 const char *name = token + 5;
1168 /* Can't specify an empty name */
1169 if (!strlen(name))
1170 return -EINVAL;
1171 /* Must match [\w.-]+ */
1172 for (i = 0; i < strlen(name); i++) {
1173 char c = name[i];
1174 if (isalnum(c))
1175 continue;
1176 if ((c == '.') || (c == '-') || (c == '_'))
1177 continue;
1178 return -EINVAL;
1179 }
1180 /* Specifying two names is forbidden */
1181 if (opts->name)
1182 return -EINVAL;
1183 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001184 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001185 GFP_KERNEL);
1186 if (!opts->name)
1187 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001188
1189 continue;
1190 }
1191
Tejun Heo30159ec2013-06-25 11:53:37 -07001192 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001193 if (strcmp(token, ss->name))
1194 continue;
1195 if (ss->disabled)
1196 continue;
1197
1198 /* Mutually exclusive option 'all' + subsystem name */
1199 if (all_ss)
1200 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001201 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001202 one_ss = true;
1203
1204 break;
1205 }
1206 if (i == CGROUP_SUBSYS_COUNT)
1207 return -ENOENT;
1208 }
1209
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001210 /* Consistency checks */
1211
Tejun Heo873fe092013-04-14 20:15:26 -07001212 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1213 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1214
Tejun Heod3ba07c2014-02-13 06:58:38 -05001215 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1216 opts->cpuset_clone_children || opts->release_agent ||
1217 opts->name) {
1218 pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001219 return -EINVAL;
1220 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001221 } else {
1222 /*
1223 * If the 'all' option was specified select all the
1224 * subsystems, otherwise if 'none', 'name=' and a subsystem
1225 * name options were not specified, let's default to 'all'
1226 */
1227 if (all_ss || (!one_ss && !opts->none && !opts->name))
1228 for_each_subsys(ss, i)
1229 if (!ss->disabled)
1230 set_bit(i, &opts->subsys_mask);
Tejun Heo873fe092013-04-14 20:15:26 -07001231
Tejun Heoa2dd4242014-03-19 10:23:55 -04001232 /*
1233 * We either have to specify by name or by subsystems. (So
1234 * all empty hierarchies must have a name).
1235 */
1236 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001237 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001238 }
1239
Li Zefanf9ab5b52009-06-17 16:26:33 -07001240 /*
1241 * Option noprefix was introduced just for backward compatibility
1242 * with the old cpuset, so we allow noprefix only if mounting just
1243 * the cpuset subsystem.
1244 */
Tejun Heo93438622013-04-14 20:15:25 -07001245 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001246 return -EINVAL;
1247
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001248
1249 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001250 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001251 return -EINVAL;
1252
Paul Menageddbcc7e2007-10-18 23:39:30 -07001253 return 0;
1254}
1255
Tejun Heo2bd59d42014-02-11 11:52:49 -05001256static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001257{
1258 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001259 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001260 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001261 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001262
Tejun Heo873fe092013-04-14 20:15:26 -07001263 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1264 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1265 return -EINVAL;
1266 }
1267
Tejun Heoace2bee2014-02-11 11:52:47 -05001268 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001269 mutex_lock(&cgroup_mutex);
1270
1271 /* See what subsystems are wanted */
1272 ret = parse_cgroupfs_options(data, &opts);
1273 if (ret)
1274 goto out_unlock;
1275
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001276 if (opts.subsys_mask != root->cgrp.subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001277 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1278 task_tgid_nr(current), current->comm);
1279
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001280 added_mask = opts.subsys_mask & ~root->cgrp.subsys_mask;
1281 removed_mask = root->cgrp.subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001282
Ben Blumcf5d5942010-03-10 15:22:09 -08001283 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001284 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001285 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001286 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1287 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1288 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001289 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001290 goto out_unlock;
1291 }
1292
Tejun Heof172e672013-06-28 17:07:30 -07001293 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001294 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001295 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001296 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001297 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001298
Tejun Heo5df36032014-03-19 10:23:54 -04001299 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001300 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001301 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001302
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001303 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001304
Tejun Heo69e943b2014-02-08 10:36:58 -05001305 if (opts.release_agent) {
1306 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001307 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001308 spin_unlock(&release_agent_path_lock);
1309 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001310 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001311 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001312 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001314 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001315 return ret;
1316}
1317
Tejun Heoafeb0f92014-02-13 06:58:39 -05001318/*
1319 * To reduce the fork() overhead for systems that are not actually using
1320 * their cgroups capability, we don't maintain the lists running through
1321 * each css_set to its tasks until we see the list actually used - in other
1322 * words after the first mount.
1323 */
1324static bool use_task_css_set_links __read_mostly;
1325
1326static void cgroup_enable_task_cg_lists(void)
1327{
1328 struct task_struct *p, *g;
1329
Tejun Heo96d365e2014-02-13 06:58:40 -05001330 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001331
1332 if (use_task_css_set_links)
1333 goto out_unlock;
1334
1335 use_task_css_set_links = true;
1336
1337 /*
1338 * We need tasklist_lock because RCU is not safe against
1339 * while_each_thread(). Besides, a forking task that has passed
1340 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1341 * is not guaranteed to have its child immediately visible in the
1342 * tasklist if we walk through it with RCU.
1343 */
1344 read_lock(&tasklist_lock);
1345 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001346 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1347 task_css_set(p) != &init_css_set);
1348
1349 /*
1350 * We should check if the process is exiting, otherwise
1351 * it will race with cgroup_exit() in that the list
1352 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001353 * Do it while holding siglock so that we don't end up
1354 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001355 */
Tejun Heof153ad12014-02-25 09:56:49 -05001356 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001357 if (!(p->flags & PF_EXITING)) {
1358 struct css_set *cset = task_css_set(p);
1359
1360 list_add(&p->cg_list, &cset->tasks);
1361 get_css_set(cset);
1362 }
Tejun Heof153ad12014-02-25 09:56:49 -05001363 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001364 } while_each_thread(g, p);
1365 read_unlock(&tasklist_lock);
1366out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001367 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001368}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001369
Paul Menagecc31edc2008-10-18 20:28:04 -07001370static void init_cgroup_housekeeping(struct cgroup *cgrp)
1371{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001372 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001373 INIT_LIST_HEAD(&cgrp->sibling);
1374 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001375 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001376 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001377 INIT_LIST_HEAD(&cgrp->pidlists);
1378 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001379 cgrp->dummy_css.cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001380}
Paul Menagec6d57f32009-09-23 15:56:19 -07001381
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001382static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001383 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001384{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001385 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001386
Paul Menageddbcc7e2007-10-18 23:39:30 -07001387 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001388 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001389 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001390 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001391 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001392
Paul Menagec6d57f32009-09-23 15:56:19 -07001393 root->flags = opts->flags;
1394 if (opts->release_agent)
1395 strcpy(root->release_agent_path, opts->release_agent);
1396 if (opts->name)
1397 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001398 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001399 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001400}
1401
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001402static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001403{
Tejun Heod427dfe2014-02-11 11:52:48 -05001404 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001405 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001406 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001407 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001408
Tejun Heod427dfe2014-02-11 11:52:48 -05001409 lockdep_assert_held(&cgroup_tree_mutex);
1410 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001411
Tejun Heod427dfe2014-02-11 11:52:48 -05001412 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1413 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001414 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001415 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001416
Tejun Heod427dfe2014-02-11 11:52:48 -05001417 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001418 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001419 * but that's OK - it can only be increased by someone holding
1420 * cgroup_lock, and that's us. The worst that can happen is that we
1421 * have some link structures left over
1422 */
1423 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001424 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001425 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001426
Tejun Heo985ed672014-03-19 10:23:53 -04001427 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001428 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001429 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001430
Tejun Heo2bd59d42014-02-11 11:52:49 -05001431 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1432 KERNFS_ROOT_CREATE_DEACTIVATED,
1433 root_cgrp);
1434 if (IS_ERR(root->kf_root)) {
1435 ret = PTR_ERR(root->kf_root);
1436 goto exit_root_id;
1437 }
1438 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001439
Tejun Heod427dfe2014-02-11 11:52:48 -05001440 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1441 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001442 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001443
Tejun Heo5df36032014-03-19 10:23:54 -04001444 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001445 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001446 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001447
Tejun Heod427dfe2014-02-11 11:52:48 -05001448 /*
1449 * There must be no failure case after here, since rebinding takes
1450 * care of subsystems' refcounts, which are explicitly dropped in
1451 * the failure exit path.
1452 */
1453 list_add(&root->root_list, &cgroup_roots);
1454 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001455
Tejun Heod427dfe2014-02-11 11:52:48 -05001456 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001457 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001458 * objects.
1459 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001460 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001461 hash_for_each(css_set_table, i, cset, hlist)
1462 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001463 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001464
Tejun Heod427dfe2014-02-11 11:52:48 -05001465 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001466 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001467
Tejun Heo2bd59d42014-02-11 11:52:49 -05001468 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001469 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001470 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001471
Tejun Heo2bd59d42014-02-11 11:52:49 -05001472destroy_root:
1473 kernfs_destroy_root(root->kf_root);
1474 root->kf_root = NULL;
1475exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001476 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001477out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001478 free_cgrp_cset_links(&tmp_links);
1479 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001480}
1481
Al Virof7e83572010-07-26 13:23:11 +04001482static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001483 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001484 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001485{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001486 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001487 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001488 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001489 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001490
1491 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001492 * The first time anyone tries to mount a cgroup, enable the list
1493 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001494 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001495 if (!use_task_css_set_links)
1496 cgroup_enable_task_cg_lists();
Tejun Heo776f02f2014-02-12 09:29:50 -05001497retry:
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001498 mutex_lock(&cgroup_tree_mutex);
1499 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001500
Paul Menageddbcc7e2007-10-18 23:39:30 -07001501 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001502 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001503 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001504 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001505
Tejun Heo2bd59d42014-02-11 11:52:49 -05001506 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001507 if (!opts.subsys_mask && !opts.none && !opts.name) {
1508 cgrp_dfl_root_visible = true;
1509 root = &cgrp_dfl_root;
1510 cgroup_get(&root->cgrp);
1511 ret = 0;
1512 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513 }
1514
Tejun Heo985ed672014-03-19 10:23:53 -04001515 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001516 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001517
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001518 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001519 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001520
Paul Menage817929e2007-10-18 23:39:36 -07001521 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001522 * If we asked for a name then it must match. Also, if
1523 * name matches but sybsys_mask doesn't, we should fail.
1524 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001525 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001526 if (opts.name) {
1527 if (strcmp(opts.name, root->name))
1528 continue;
1529 name_match = true;
1530 }
Tejun Heo31261212013-06-28 17:07:30 -07001531
1532 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001533 * If we asked for subsystems (or explicitly for no
1534 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001535 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001536 if ((opts.subsys_mask || opts.none) &&
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001537 (opts.subsys_mask != root->cgrp.subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001538 if (!name_match)
1539 continue;
1540 ret = -EBUSY;
1541 goto out_unlock;
1542 }
Tejun Heo873fe092013-04-14 20:15:26 -07001543
Tejun Heoc7ba8282013-06-29 14:06:10 -07001544 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001545 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1546 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1547 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001548 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001549 } else {
1550 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1551 }
Tejun Heo873fe092013-04-14 20:15:26 -07001552 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001553
Tejun Heo776f02f2014-02-12 09:29:50 -05001554 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001555 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001556 * ref indicate that the root is being destroyed. Wait for
1557 * destruction to complete so that the subsystems are free.
1558 * We can use wait_queue for the wait but this path is
1559 * super cold. Let's just sleep for a bit and retry.
1560 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001561 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001562 mutex_unlock(&cgroup_mutex);
1563 mutex_unlock(&cgroup_tree_mutex);
Li Zefan6534fd62014-02-14 16:55:04 +08001564 kfree(opts.release_agent);
1565 kfree(opts.name);
Tejun Heo776f02f2014-02-12 09:29:50 -05001566 msleep(10);
1567 goto retry;
1568 }
1569
1570 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001571 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001572 }
1573
Tejun Heo172a2c062014-03-19 10:23:53 -04001574 /*
1575 * No such thing, create a new one. name= matching without subsys
1576 * specification is allowed for already existing hierarchies but we
1577 * can't create new one without subsys specification.
1578 */
1579 if (!opts.subsys_mask && !opts.none) {
1580 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001581 goto out_unlock;
1582 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583
Tejun Heo172a2c062014-03-19 10:23:53 -04001584 root = kzalloc(sizeof(*root), GFP_KERNEL);
1585 if (!root) {
1586 ret = -ENOMEM;
1587 goto out_unlock;
1588 }
1589
1590 init_cgroup_root(root, &opts);
1591
Tejun Heo35585572014-02-13 06:58:38 -05001592 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001593 if (ret)
1594 cgroup_free_root(root);
1595
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001596out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001597 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001598 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001599
Paul Menagec6d57f32009-09-23 15:56:19 -07001600 kfree(opts.release_agent);
1601 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001602
Tejun Heo2bd59d42014-02-11 11:52:49 -05001603 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001604 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001605
Linus Torvalds32d01dc2014-04-03 13:05:42 -07001606 dentry = kernfs_mount(fs_type, flags, root->kf_root, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001607 if (IS_ERR(dentry))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001608 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001609 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610}
1611
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001612static void cgroup_kill_sb(struct super_block *sb)
1613{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001614 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001615 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001616
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001617 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001618 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001619}
1620
1621static struct file_system_type cgroup_fs_type = {
1622 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001623 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624 .kill_sb = cgroup_kill_sb,
1625};
1626
Greg KH676db4a2010-08-05 13:53:35 -07001627static struct kobject *cgroup_kobj;
1628
Li Zefana043e3b2008-02-23 15:24:09 -08001629/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001630 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001631 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001632 * @buf: the buffer to write the path into
1633 * @buflen: the length of the buffer
1634 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001635 * Determine @task's cgroup on the first (the one with the lowest non-zero
1636 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1637 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1638 * cgroup controller callbacks.
1639 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001640 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001641 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001642char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001643{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001644 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001645 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001646 int hierarchy_id = 1;
1647 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001648
1649 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001650 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001651
Tejun Heo913ffdb2013-07-11 16:34:48 -07001652 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1653
Tejun Heo857a2be2013-04-14 20:50:08 -07001654 if (root) {
1655 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001656 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001657 } else {
1658 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001659 if (strlcpy(buf, "/", buflen) < buflen)
1660 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001661 }
1662
Tejun Heo96d365e2014-02-13 06:58:40 -05001663 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001664 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001665 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001666}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001667EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001668
Tejun Heob3dc0942014-02-25 10:04:01 -05001669/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001670struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001671 /* the src and dst cset list running through cset->mg_node */
1672 struct list_head src_csets;
1673 struct list_head dst_csets;
1674
1675 /*
1676 * Fields for cgroup_taskset_*() iteration.
1677 *
1678 * Before migration is committed, the target migration tasks are on
1679 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1680 * the csets on ->dst_csets. ->csets point to either ->src_csets
1681 * or ->dst_csets depending on whether migration is committed.
1682 *
1683 * ->cur_csets and ->cur_task point to the current task position
1684 * during iteration.
1685 */
1686 struct list_head *csets;
1687 struct css_set *cur_cset;
1688 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001689};
1690
1691/**
1692 * cgroup_taskset_first - reset taskset and return the first task
1693 * @tset: taskset of interest
1694 *
1695 * @tset iteration is initialized and the first task is returned.
1696 */
1697struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1698{
Tejun Heob3dc0942014-02-25 10:04:01 -05001699 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1700 tset->cur_task = NULL;
1701
1702 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001703}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001704
1705/**
1706 * cgroup_taskset_next - iterate to the next task in taskset
1707 * @tset: taskset of interest
1708 *
1709 * Return the next task in @tset. Iteration must have been initialized
1710 * with cgroup_taskset_first().
1711 */
1712struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1713{
Tejun Heob3dc0942014-02-25 10:04:01 -05001714 struct css_set *cset = tset->cur_cset;
1715 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001716
Tejun Heob3dc0942014-02-25 10:04:01 -05001717 while (&cset->mg_node != tset->csets) {
1718 if (!task)
1719 task = list_first_entry(&cset->mg_tasks,
1720 struct task_struct, cg_list);
1721 else
1722 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001723
Tejun Heob3dc0942014-02-25 10:04:01 -05001724 if (&task->cg_list != &cset->mg_tasks) {
1725 tset->cur_cset = cset;
1726 tset->cur_task = task;
1727 return task;
1728 }
1729
1730 cset = list_next_entry(cset, mg_node);
1731 task = NULL;
1732 }
1733
1734 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001735}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001736
1737/**
Ben Blum74a11662011-05-26 16:25:20 -07001738 * cgroup_task_migrate - move a task from one cgroup to another.
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001739 * @old_cgrp; the cgroup @tsk is being migrated from
1740 * @tsk: the task being migrated
1741 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001742 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001743 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001744 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001745static void cgroup_task_migrate(struct cgroup *old_cgrp,
1746 struct task_struct *tsk,
1747 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001748{
Tejun Heo5abb8852013-06-12 21:04:49 -07001749 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001750
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001751 lockdep_assert_held(&cgroup_mutex);
1752 lockdep_assert_held(&css_set_rwsem);
1753
Ben Blum74a11662011-05-26 16:25:20 -07001754 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001755 * We are synchronized through threadgroup_lock() against PF_EXITING
1756 * setting such that we can't race against cgroup_exit() changing the
1757 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001758 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001759 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001760 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001761
Tejun Heob3dc0942014-02-25 10:04:01 -05001762 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001763 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001764
Tejun Heo1b9aba42014-03-19 17:43:21 -04001765 /*
1766 * Use move_tail so that cgroup_taskset_first() still returns the
1767 * leader after migration. This works because cgroup_migrate()
1768 * ensures that the dst_cset of the leader is the first on the
1769 * tset's dst_csets list.
1770 */
1771 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001772
1773 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001774 * We just gained a reference on old_cset by taking it from the
1775 * task. As trading it for new_cset is protected by cgroup_mutex,
1776 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001777 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001778 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001779 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001780}
1781
Li Zefana043e3b2008-02-23 15:24:09 -08001782/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001783 * cgroup_migrate_finish - cleanup after attach
1784 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001785 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001786 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1787 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001788 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001789static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001790{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001791 struct css_set *cset, *tmp_cset;
1792
1793 lockdep_assert_held(&cgroup_mutex);
1794
1795 down_write(&css_set_rwsem);
1796 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1797 cset->mg_src_cgrp = NULL;
1798 cset->mg_dst_cset = NULL;
1799 list_del_init(&cset->mg_preload_node);
1800 put_css_set_locked(cset, false);
1801 }
1802 up_write(&css_set_rwsem);
1803}
1804
1805/**
1806 * cgroup_migrate_add_src - add a migration source css_set
1807 * @src_cset: the source css_set to add
1808 * @dst_cgrp: the destination cgroup
1809 * @preloaded_csets: list of preloaded css_sets
1810 *
1811 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1812 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1813 * up by cgroup_migrate_finish().
1814 *
1815 * This function may be called without holding threadgroup_lock even if the
1816 * target is a process. Threads may be created and destroyed but as long
1817 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1818 * the preloaded css_sets are guaranteed to cover all migrations.
1819 */
1820static void cgroup_migrate_add_src(struct css_set *src_cset,
1821 struct cgroup *dst_cgrp,
1822 struct list_head *preloaded_csets)
1823{
1824 struct cgroup *src_cgrp;
1825
1826 lockdep_assert_held(&cgroup_mutex);
1827 lockdep_assert_held(&css_set_rwsem);
1828
1829 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1830
1831 /* nothing to do if this cset already belongs to the cgroup */
1832 if (src_cgrp == dst_cgrp)
1833 return;
1834
1835 if (!list_empty(&src_cset->mg_preload_node))
1836 return;
1837
1838 WARN_ON(src_cset->mg_src_cgrp);
1839 WARN_ON(!list_empty(&src_cset->mg_tasks));
1840 WARN_ON(!list_empty(&src_cset->mg_node));
1841
1842 src_cset->mg_src_cgrp = src_cgrp;
1843 get_css_set(src_cset);
1844 list_add(&src_cset->mg_preload_node, preloaded_csets);
1845}
1846
1847/**
1848 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
1849 * @dst_cgrp: the destination cgroup
1850 * @preloaded_csets: list of preloaded source css_sets
1851 *
1852 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
1853 * have been preloaded to @preloaded_csets. This function looks up and
1854 * pins all destination css_sets, links each to its source, and put them on
1855 * @preloaded_csets.
1856 *
1857 * This function must be called after cgroup_migrate_add_src() has been
1858 * called on each migration source css_set. After migration is performed
1859 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
1860 * @preloaded_csets.
1861 */
1862static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
1863 struct list_head *preloaded_csets)
1864{
1865 LIST_HEAD(csets);
1866 struct css_set *src_cset;
1867
1868 lockdep_assert_held(&cgroup_mutex);
1869
1870 /* look up the dst cset for each src cset and link it to src */
1871 list_for_each_entry(src_cset, preloaded_csets, mg_preload_node) {
1872 struct css_set *dst_cset;
1873
1874 dst_cset = find_css_set(src_cset, dst_cgrp);
1875 if (!dst_cset)
1876 goto err;
1877
1878 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
1879 src_cset->mg_dst_cset = dst_cset;
1880
1881 if (list_empty(&dst_cset->mg_preload_node))
1882 list_add(&dst_cset->mg_preload_node, &csets);
1883 else
1884 put_css_set(dst_cset, false);
1885 }
1886
1887 list_splice(&csets, preloaded_csets);
1888 return 0;
1889err:
1890 cgroup_migrate_finish(&csets);
1891 return -ENOMEM;
1892}
1893
1894/**
1895 * cgroup_migrate - migrate a process or task to a cgroup
1896 * @cgrp: the destination cgroup
1897 * @leader: the leader of the process or the task to migrate
1898 * @threadgroup: whether @leader points to the whole process or a single task
1899 *
1900 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
1901 * process, the caller must be holding threadgroup_lock of @leader. The
1902 * caller is also responsible for invoking cgroup_migrate_add_src() and
1903 * cgroup_migrate_prepare_dst() on the targets before invoking this
1904 * function and following up with cgroup_migrate_finish().
1905 *
1906 * As long as a controller's ->can_attach() doesn't fail, this function is
1907 * guaranteed to succeed. This means that, excluding ->can_attach()
1908 * failure, when migrating multiple targets, the success or failure can be
1909 * decided for all targets by invoking group_migrate_prepare_dst() before
1910 * actually starting migrating.
1911 */
1912static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
1913 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001914{
Tejun Heob3dc0942014-02-25 10:04:01 -05001915 struct cgroup_taskset tset = {
1916 .src_csets = LIST_HEAD_INIT(tset.src_csets),
1917 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
1918 .csets = &tset.src_csets,
1919 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05001920 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05001921 struct css_set *cset, *tmp_cset;
1922 struct task_struct *task, *tmp_task;
1923 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07001924
1925 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001926 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1927 * already PF_EXITING could be freed from underneath us unless we
1928 * take an rcu_read_lock.
1929 */
Tejun Heob3dc0942014-02-25 10:04:01 -05001930 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001931 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05001932 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07001933 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05001934 /* @task either already exited or can't exit until the end */
1935 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001936 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001937
Tejun Heoeaf797a2014-02-25 10:04:03 -05001938 /* leave @task alone if post_fork() hasn't linked it yet */
1939 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08001940 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05001941
Tejun Heob3dc0942014-02-25 10:04:01 -05001942 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001943 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001944 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05001945
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001946 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04001947 * cgroup_taskset_first() must always return the leader.
1948 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001949 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04001950 list_move_tail(&task->cg_list, &cset->mg_tasks);
1951 if (list_empty(&cset->mg_node))
1952 list_add_tail(&cset->mg_node, &tset.src_csets);
1953 if (list_empty(&cset->mg_dst_cset->mg_node))
1954 list_move_tail(&cset->mg_dst_cset->mg_node,
1955 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08001956 next:
Li Zefan081aa452013-03-13 09:17:09 +08001957 if (!threadgroup)
1958 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05001959 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001960 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05001961 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07001962
Tejun Heo134d3372011-12-12 18:12:21 -08001963 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05001964 if (list_empty(&tset.src_csets))
1965 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08001966
Tejun Heo1958d2d2014-02-25 10:04:03 -05001967 /* check that we can legitimately attach to the cgroup */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001968 for_each_css(css, i, cgrp) {
1969 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05001970 ret = css->ss->can_attach(css, &tset);
1971 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001972 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07001973 goto out_cancel_attach;
1974 }
1975 }
Ben Blum74a11662011-05-26 16:25:20 -07001976 }
1977
1978 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05001979 * Now that we're guaranteed success, proceed to move all tasks to
1980 * the new cgroup. There are no failure cases after here, so this
1981 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07001982 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001983 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05001984 list_for_each_entry(cset, &tset.src_csets, mg_node) {
1985 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
1986 cgroup_task_migrate(cset->mg_src_cgrp, task,
1987 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001988 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001989 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07001990
1991 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05001992 * Migration is committed, all target tasks are now on dst_csets.
1993 * Nothing is sensitive to fork() after this point. Notify
1994 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07001995 */
Tejun Heob3dc0942014-02-25 10:04:01 -05001996 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07001997
Tejun Heo1c6727a2013-12-06 15:11:56 -05001998 for_each_css(css, i, cgrp)
1999 if (css->ss->attach)
2000 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002001
Tejun Heo9db8de32014-02-13 06:58:43 -05002002 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002003 goto out_release_tset;
2004
Ben Blum74a11662011-05-26 16:25:20 -07002005out_cancel_attach:
Tejun Heob3dc0942014-02-25 10:04:01 -05002006 for_each_css(css, i, cgrp) {
2007 if (css == failed_css)
2008 break;
2009 if (css->ss->cancel_attach)
2010 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002011 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002012out_release_tset:
2013 down_write(&css_set_rwsem);
2014 list_splice_init(&tset.dst_csets, &tset.src_csets);
2015 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002016 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002017 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002018 }
2019 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002020 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002021}
2022
Tejun Heo1958d2d2014-02-25 10:04:03 -05002023/**
2024 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2025 * @dst_cgrp: the cgroup to attach to
2026 * @leader: the task or the leader of the threadgroup to be attached
2027 * @threadgroup: attach the whole threadgroup?
2028 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002029 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002030 */
2031static int cgroup_attach_task(struct cgroup *dst_cgrp,
2032 struct task_struct *leader, bool threadgroup)
2033{
2034 LIST_HEAD(preloaded_csets);
2035 struct task_struct *task;
2036 int ret;
2037
2038 /* look up all src csets */
2039 down_read(&css_set_rwsem);
2040 rcu_read_lock();
2041 task = leader;
2042 do {
2043 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2044 &preloaded_csets);
2045 if (!threadgroup)
2046 break;
2047 } while_each_thread(leader, task);
2048 rcu_read_unlock();
2049 up_read(&css_set_rwsem);
2050
2051 /* prepare dst csets and commit */
2052 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2053 if (!ret)
2054 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2055
2056 cgroup_migrate_finish(&preloaded_csets);
2057 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002058}
2059
2060/*
2061 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002062 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002063 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002064 */
2065static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002066{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002067 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002068 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002069 int ret;
2070
Ben Blum74a11662011-05-26 16:25:20 -07002071 if (!cgroup_lock_live_group(cgrp))
2072 return -ENODEV;
2073
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002074retry_find_task:
2075 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002076 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002077 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002078 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002079 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002080 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002081 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002082 }
Ben Blum74a11662011-05-26 16:25:20 -07002083 /*
2084 * even if we're attaching all tasks in the thread group, we
2085 * only need to check permissions on one of them.
2086 */
David Howellsc69e8d92008-11-14 10:39:19 +11002087 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002088 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2089 !uid_eq(cred->euid, tcred->uid) &&
2090 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002091 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002092 ret = -EACCES;
2093 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002094 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002095 } else
2096 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002097
2098 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002099 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002100
2101 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002102 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002103 * trapped in a cpuset, or RT worker may be born in a cgroup
2104 * with no rt_runtime allocated. Just say no.
2105 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002106 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002107 ret = -EINVAL;
2108 rcu_read_unlock();
2109 goto out_unlock_cgroup;
2110 }
2111
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002112 get_task_struct(tsk);
2113 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002114
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002115 threadgroup_lock(tsk);
2116 if (threadgroup) {
2117 if (!thread_group_leader(tsk)) {
2118 /*
2119 * a race with de_thread from another thread's exec()
2120 * may strip us of our leadership, if this happens,
2121 * there is no choice but to throw this task away and
2122 * try again; this is
2123 * "double-double-toil-and-trouble-check locking".
2124 */
2125 threadgroup_unlock(tsk);
2126 put_task_struct(tsk);
2127 goto retry_find_task;
2128 }
Li Zefan081aa452013-03-13 09:17:09 +08002129 }
2130
2131 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2132
Tejun Heocd3d0952011-12-12 18:12:21 -08002133 threadgroup_unlock(tsk);
2134
Paul Menagebbcb81d2007-10-18 23:39:32 -07002135 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002136out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002137 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002138 return ret;
2139}
2140
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002141/**
2142 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2143 * @from: attach to all cgroups of a given task
2144 * @tsk: the task to be attached
2145 */
2146int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2147{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002148 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002149 int retval = 0;
2150
Tejun Heo47cfcd02013-04-07 09:29:51 -07002151 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002152 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002153 struct cgroup *from_cgrp;
2154
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002155 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002156 continue;
2157
Tejun Heo96d365e2014-02-13 06:58:40 -05002158 down_read(&css_set_rwsem);
2159 from_cgrp = task_cgroup_from_root(from, root);
2160 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002161
Li Zefan6f4b7e62013-07-31 16:18:36 +08002162 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002163 if (retval)
2164 break;
2165 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002166 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002167
2168 return retval;
2169}
2170EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2171
Tejun Heo182446d2013-08-08 20:11:24 -04002172static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2173 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002174{
Tejun Heo182446d2013-08-08 20:11:24 -04002175 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002176}
2177
Tejun Heo182446d2013-08-08 20:11:24 -04002178static int cgroup_procs_write(struct cgroup_subsys_state *css,
2179 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002180{
Tejun Heo182446d2013-08-08 20:11:24 -04002181 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002182}
2183
Tejun Heo182446d2013-08-08 20:11:24 -04002184static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
Tejun Heo4d3bb512014-03-19 10:23:54 -04002185 struct cftype *cft, char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002186{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002187 struct cgroup_root *root = css->cgroup->root;
Tejun Heo5f469902014-02-11 11:52:48 -05002188
2189 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002190 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002191 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002192 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002193 strlcpy(root->release_agent_path, buffer,
2194 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002195 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002196 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002197 return 0;
2198}
2199
Tejun Heo2da8ca82013-12-05 12:28:04 -05002200static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002201{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002202 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002203
Paul Menagee788e062008-07-25 01:46:59 -07002204 if (!cgroup_lock_live_group(cgrp))
2205 return -ENODEV;
2206 seq_puts(seq, cgrp->root->release_agent_path);
2207 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002208 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002209 return 0;
2210}
2211
Tejun Heo2da8ca82013-12-05 12:28:04 -05002212static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002213{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002214 struct cgroup *cgrp = seq_css(seq)->cgroup;
2215
2216 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002217 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002218}
2219
Tejun Heo2bd59d42014-02-11 11:52:49 -05002220static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2221 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002222{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002223 struct cgroup *cgrp = of->kn->parent->priv;
2224 struct cftype *cft = of->kn->priv;
2225 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002226 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002227
Tejun Heo2bd59d42014-02-11 11:52:49 -05002228 /*
2229 * kernfs guarantees that a file isn't deleted with operations in
2230 * flight, which means that the matching css is and stays alive and
2231 * doesn't need to be pinned. The RCU locking is not necessary
2232 * either. It's just for the convenience of using cgroup_css().
2233 */
2234 rcu_read_lock();
2235 css = cgroup_css(cgrp, cft->ss);
2236 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002237
Tejun Heoa742c592013-12-05 12:28:03 -05002238 if (cft->write_string) {
2239 ret = cft->write_string(css, cft, strstrip(buf));
2240 } else if (cft->write_u64) {
2241 unsigned long long v;
2242 ret = kstrtoull(buf, 0, &v);
2243 if (!ret)
2244 ret = cft->write_u64(css, cft, v);
2245 } else if (cft->write_s64) {
2246 long long v;
2247 ret = kstrtoll(buf, 0, &v);
2248 if (!ret)
2249 ret = cft->write_s64(css, cft, v);
2250 } else if (cft->trigger) {
2251 ret = cft->trigger(css, (unsigned int)cft->private);
2252 } else {
2253 ret = -EINVAL;
2254 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002255
Tejun Heoa742c592013-12-05 12:28:03 -05002256 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002257}
2258
Tejun Heo6612f052013-12-05 12:28:04 -05002259static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002260{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002261 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002262}
2263
2264static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2265{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002266 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002267}
2268
2269static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2270{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002271 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002272}
2273
2274static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2275{
Tejun Heo7da11272013-12-05 12:28:04 -05002276 struct cftype *cft = seq_cft(m);
2277 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002278
Tejun Heo2da8ca82013-12-05 12:28:04 -05002279 if (cft->seq_show)
2280 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002281
Tejun Heo896f5192013-12-05 12:28:04 -05002282 if (cft->read_u64)
2283 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2284 else if (cft->read_s64)
2285 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2286 else
2287 return -EINVAL;
2288 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002289}
2290
Tejun Heo2bd59d42014-02-11 11:52:49 -05002291static struct kernfs_ops cgroup_kf_single_ops = {
2292 .atomic_write_len = PAGE_SIZE,
2293 .write = cgroup_file_write,
2294 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002295};
2296
Tejun Heo2bd59d42014-02-11 11:52:49 -05002297static struct kernfs_ops cgroup_kf_ops = {
2298 .atomic_write_len = PAGE_SIZE,
2299 .write = cgroup_file_write,
2300 .seq_start = cgroup_seqfile_start,
2301 .seq_next = cgroup_seqfile_next,
2302 .seq_stop = cgroup_seqfile_stop,
2303 .seq_show = cgroup_seqfile_show,
2304};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002305
2306/*
2307 * cgroup_rename - Only allow simple rename of directories in place.
2308 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002309static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2310 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002311{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002312 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002313 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002314
Tejun Heo2bd59d42014-02-11 11:52:49 -05002315 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002316 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002317 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002318 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002319
Tejun Heo6db8e852013-06-14 11:18:22 -07002320 /*
2321 * This isn't a proper migration and its usefulness is very
2322 * limited. Disallow if sane_behavior.
2323 */
2324 if (cgroup_sane_behavior(cgrp))
2325 return -EPERM;
2326
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002327 /*
2328 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2329 * active_ref. kernfs_rename() doesn't require active_ref
2330 * protection. Break them before grabbing cgroup_tree_mutex.
2331 */
2332 kernfs_break_active_protection(new_parent);
2333 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002334
Tejun Heo2bd59d42014-02-11 11:52:49 -05002335 mutex_lock(&cgroup_tree_mutex);
2336 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002337
Tejun Heo2bd59d42014-02-11 11:52:49 -05002338 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002339
Tejun Heo2bd59d42014-02-11 11:52:49 -05002340 mutex_unlock(&cgroup_mutex);
2341 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002342
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002343 kernfs_unbreak_active_protection(kn);
2344 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002345 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002346}
2347
Tejun Heo2bb566c2013-08-08 20:11:23 -04002348static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002349{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002350 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002351 struct kernfs_node *kn;
2352 struct lock_class_key *key = NULL;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002353
Tejun Heo2bd59d42014-02-11 11:52:49 -05002354#ifdef CONFIG_DEBUG_LOCK_ALLOC
2355 key = &cft->lockdep_key;
2356#endif
2357 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2358 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2359 NULL, false, key);
Fengguang Wu430af8ad2014-02-13 16:42:43 -05002360 return PTR_ERR_OR_ZERO(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002361}
2362
Tejun Heob1f28d32013-06-28 16:24:10 -07002363/**
2364 * cgroup_addrm_files - add or remove files to a cgroup directory
2365 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002366 * @cfts: array of cftypes to be added
2367 * @is_add: whether to add or remove
2368 *
2369 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002370 * For removals, this function never fails. If addition fails, this
2371 * function doesn't remove files already added. The caller is responsible
2372 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002373 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002374static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2375 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002376{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002377 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002378 int ret;
2379
Tejun Heoace2bee2014-02-11 11:52:47 -05002380 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002381
2382 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002383 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002384 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2385 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002386 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2387 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002388 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2389 continue;
2390 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2391 continue;
2392
Li Zefan2739d3c2013-01-21 18:18:33 +08002393 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002394 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002395 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002396 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002397 cft->name, ret);
2398 return ret;
2399 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002400 } else {
2401 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002402 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002403 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002404 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002405}
2406
Tejun Heo21a2d342014-02-12 09:29:49 -05002407static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002408{
2409 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002410 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002411 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002412 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002413 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002414
Tejun Heo21a2d342014-02-12 09:29:49 -05002415 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002416
Tejun Heo21a2d342014-02-12 09:29:49 -05002417 /* don't bother if @ss isn't attached */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002418 if (ss->root == &cgrp_dfl_root)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002419 return 0;
Li Zefane8c82d22013-06-18 18:48:37 +08002420
Li Zefane8c82d22013-06-18 18:48:37 +08002421 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002422 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002423 struct cgroup *cgrp = css->cgroup;
2424
Li Zefane8c82d22013-06-18 18:48:37 +08002425 if (cgroup_is_dead(cgrp))
2426 continue;
2427
Tejun Heo21a2d342014-02-12 09:29:49 -05002428 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002429 if (ret)
2430 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002431 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002432
2433 if (is_add && !ret)
2434 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002435 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002436}
2437
Tejun Heo2da440a2014-02-11 11:52:48 -05002438static void cgroup_exit_cftypes(struct cftype *cfts)
2439{
2440 struct cftype *cft;
2441
Tejun Heo2bd59d42014-02-11 11:52:49 -05002442 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2443 /* free copy for custom atomic_write_len, see init_cftypes() */
2444 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2445 kfree(cft->kf_ops);
2446 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002447 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002448 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002449}
2450
Tejun Heo2bd59d42014-02-11 11:52:49 -05002451static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002452{
2453 struct cftype *cft;
2454
Tejun Heo2bd59d42014-02-11 11:52:49 -05002455 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2456 struct kernfs_ops *kf_ops;
2457
Tejun Heo0adb0702014-02-12 09:29:48 -05002458 WARN_ON(cft->ss || cft->kf_ops);
2459
Tejun Heo2bd59d42014-02-11 11:52:49 -05002460 if (cft->seq_start)
2461 kf_ops = &cgroup_kf_ops;
2462 else
2463 kf_ops = &cgroup_kf_single_ops;
2464
2465 /*
2466 * Ugh... if @cft wants a custom max_write_len, we need to
2467 * make a copy of kf_ops to set its atomic_write_len.
2468 */
2469 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2470 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2471 if (!kf_ops) {
2472 cgroup_exit_cftypes(cfts);
2473 return -ENOMEM;
2474 }
2475 kf_ops->atomic_write_len = cft->max_write_len;
2476 }
2477
2478 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002479 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002480 }
2481
2482 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002483}
2484
Tejun Heo21a2d342014-02-12 09:29:49 -05002485static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2486{
2487 lockdep_assert_held(&cgroup_tree_mutex);
2488
2489 if (!cfts || !cfts[0].ss)
2490 return -ENOENT;
2491
2492 list_del(&cfts->node);
2493 cgroup_apply_cftypes(cfts, false);
2494 cgroup_exit_cftypes(cfts);
2495 return 0;
2496}
2497
Tejun Heo8e3f6542012-04-01 12:09:55 -07002498/**
Tejun Heo80b13582014-02-12 09:29:48 -05002499 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2500 * @cfts: zero-length name terminated array of cftypes
2501 *
2502 * Unregister @cfts. Files described by @cfts are removed from all
2503 * existing cgroups and all future cgroups won't have them either. This
2504 * function can be called anytime whether @cfts' subsys is attached or not.
2505 *
2506 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2507 * registered.
2508 */
2509int cgroup_rm_cftypes(struct cftype *cfts)
2510{
Tejun Heo21a2d342014-02-12 09:29:49 -05002511 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002512
Tejun Heo21a2d342014-02-12 09:29:49 -05002513 mutex_lock(&cgroup_tree_mutex);
2514 ret = cgroup_rm_cftypes_locked(cfts);
2515 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002516 return ret;
2517}
2518
2519/**
2520 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2521 * @ss: target cgroup subsystem
2522 * @cfts: zero-length name terminated array of cftypes
2523 *
2524 * Register @cfts to @ss. Files described by @cfts are created for all
2525 * existing cgroups to which @ss is attached and all future cgroups will
2526 * have them too. This function can be called anytime whether @ss is
2527 * attached or not.
2528 *
2529 * Returns 0 on successful registration, -errno on failure. Note that this
2530 * function currently returns 0 as long as @cfts registration is successful
2531 * even if some file creation attempts on existing cgroups fail.
2532 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002533int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002534{
Tejun Heo9ccece82013-06-28 16:24:11 -07002535 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002536
Li Zefandc5736e2014-02-17 10:41:50 +08002537 if (!cfts || cfts[0].name[0] == '\0')
2538 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002539
Tejun Heo2bd59d42014-02-11 11:52:49 -05002540 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002541 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05002542 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002543
Tejun Heo21a2d342014-02-12 09:29:49 -05002544 mutex_lock(&cgroup_tree_mutex);
2545
Tejun Heo0adb0702014-02-12 09:29:48 -05002546 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05002547 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002548 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05002549 cgroup_rm_cftypes_locked(cfts);
2550
2551 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002552 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002553}
Tejun Heo79578622012-04-01 12:09:56 -07002554
2555/**
Li Zefana043e3b2008-02-23 15:24:09 -08002556 * cgroup_task_count - count the number of tasks in a cgroup.
2557 * @cgrp: the cgroup in question
2558 *
2559 * Return the number of tasks in the cgroup.
2560 */
Tejun Heo07bc3562014-02-13 06:58:39 -05002561static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002562{
2563 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002564 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002565
Tejun Heo96d365e2014-02-13 06:58:40 -05002566 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07002567 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2568 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05002569 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002570 return count;
2571}
2572
Tejun Heo574bd9f2012-11-09 09:12:29 -08002573/**
Tejun Heo492eb212013-08-08 20:11:25 -04002574 * css_next_child - find the next child of a given css
2575 * @pos_css: the current position (%NULL to initiate traversal)
2576 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002577 *
Tejun Heo492eb212013-08-08 20:11:25 -04002578 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002579 * under either cgroup_mutex or RCU read lock. The only requirement is
2580 * that @parent_css and @pos_css are accessible. The next sibling is
2581 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002582 */
Tejun Heo492eb212013-08-08 20:11:25 -04002583struct cgroup_subsys_state *
2584css_next_child(struct cgroup_subsys_state *pos_css,
2585 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002586{
Tejun Heo492eb212013-08-08 20:11:25 -04002587 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2588 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002589 struct cgroup *next;
2590
Tejun Heoace2bee2014-02-11 11:52:47 -05002591 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002592
2593 /*
2594 * @pos could already have been removed. Once a cgroup is removed,
2595 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002596 * changes. As CGRP_DEAD assertion is serialized and happens
2597 * before the cgroup is taken off the ->sibling list, if we see it
2598 * unasserted, it's guaranteed that the next sibling hasn't
2599 * finished its grace period even if it's already removed, and thus
2600 * safe to dereference from this RCU critical section. If
2601 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2602 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002603 *
2604 * If @pos is dead, its next pointer can't be dereferenced;
2605 * however, as each cgroup is given a monotonically increasing
2606 * unique serial number and always appended to the sibling list,
2607 * the next one can be found by walking the parent's children until
2608 * we see a cgroup with higher serial number than @pos's. While
2609 * this path can be slower, it's taken only when either the current
2610 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002611 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002612 if (!pos) {
2613 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2614 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002615 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002616 } else {
2617 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2618 if (next->serial_nr > pos->serial_nr)
2619 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002620 }
2621
Tejun Heo492eb212013-08-08 20:11:25 -04002622 if (&next->sibling == &cgrp->children)
2623 return NULL;
2624
Tejun Heoca8bdca2013-08-26 18:40:56 -04002625 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002626}
Tejun Heo53fa5262013-05-24 10:55:38 +09002627
2628/**
Tejun Heo492eb212013-08-08 20:11:25 -04002629 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002630 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002631 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002632 *
Tejun Heo492eb212013-08-08 20:11:25 -04002633 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002634 * to visit for pre-order traversal of @root's descendants. @root is
2635 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002636 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002637 * While this function requires cgroup_mutex or RCU read locking, it
2638 * doesn't require the whole traversal to be contained in a single critical
2639 * section. This function will return the correct next descendant as long
2640 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002641 */
Tejun Heo492eb212013-08-08 20:11:25 -04002642struct cgroup_subsys_state *
2643css_next_descendant_pre(struct cgroup_subsys_state *pos,
2644 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002645{
Tejun Heo492eb212013-08-08 20:11:25 -04002646 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002647
Tejun Heoace2bee2014-02-11 11:52:47 -05002648 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002649
Tejun Heobd8815a2013-08-08 20:11:27 -04002650 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002651 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002652 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002653
2654 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002655 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002656 if (next)
2657 return next;
2658
2659 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002660 while (pos != root) {
2661 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002662 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002663 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002664 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002665 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002666
2667 return NULL;
2668}
Tejun Heo574bd9f2012-11-09 09:12:29 -08002669
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002670/**
Tejun Heo492eb212013-08-08 20:11:25 -04002671 * css_rightmost_descendant - return the rightmost descendant of a css
2672 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002673 *
Tejun Heo492eb212013-08-08 20:11:25 -04002674 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2675 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002676 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002677 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002678 * While this function requires cgroup_mutex or RCU read locking, it
2679 * doesn't require the whole traversal to be contained in a single critical
2680 * section. This function will return the correct rightmost descendant as
2681 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002682 */
Tejun Heo492eb212013-08-08 20:11:25 -04002683struct cgroup_subsys_state *
2684css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002685{
Tejun Heo492eb212013-08-08 20:11:25 -04002686 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002687
Tejun Heoace2bee2014-02-11 11:52:47 -05002688 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002689
2690 do {
2691 last = pos;
2692 /* ->prev isn't RCU safe, walk ->next till the end */
2693 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002694 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002695 pos = tmp;
2696 } while (pos);
2697
2698 return last;
2699}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002700
Tejun Heo492eb212013-08-08 20:11:25 -04002701static struct cgroup_subsys_state *
2702css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002703{
Tejun Heo492eb212013-08-08 20:11:25 -04002704 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002705
2706 do {
2707 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04002708 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002709 } while (pos);
2710
2711 return last;
2712}
2713
2714/**
Tejun Heo492eb212013-08-08 20:11:25 -04002715 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002716 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002717 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002718 *
Tejun Heo492eb212013-08-08 20:11:25 -04002719 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002720 * to visit for post-order traversal of @root's descendants. @root is
2721 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002722 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002723 * While this function requires cgroup_mutex or RCU read locking, it
2724 * doesn't require the whole traversal to be contained in a single critical
2725 * section. This function will return the correct next descendant as long
2726 * as both @pos and @cgroup are accessible and @pos is a descendant of
2727 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002728 */
Tejun Heo492eb212013-08-08 20:11:25 -04002729struct cgroup_subsys_state *
2730css_next_descendant_post(struct cgroup_subsys_state *pos,
2731 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002732{
Tejun Heo492eb212013-08-08 20:11:25 -04002733 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002734
Tejun Heoace2bee2014-02-11 11:52:47 -05002735 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002736
Tejun Heo58b79a92013-09-06 15:31:08 -04002737 /* if first iteration, visit leftmost descendant which may be @root */
2738 if (!pos)
2739 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002740
Tejun Heobd8815a2013-08-08 20:11:27 -04002741 /* if we visited @root, we're done */
2742 if (pos == root)
2743 return NULL;
2744
Tejun Heo574bd9f2012-11-09 09:12:29 -08002745 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04002746 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002747 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04002748 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002749
2750 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04002751 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002752}
Tejun Heo574bd9f2012-11-09 09:12:29 -08002753
Tejun Heo0942eee2013-08-08 20:11:26 -04002754/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002755 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04002756 * @it: the iterator to advance
2757 *
2758 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04002759 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002760static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04002761{
2762 struct list_head *l = it->cset_link;
2763 struct cgrp_cset_link *link;
2764 struct css_set *cset;
2765
2766 /* Advance to the next non-empty css_set */
2767 do {
2768 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04002769 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04002770 it->cset_link = NULL;
2771 return;
2772 }
2773 link = list_entry(l, struct cgrp_cset_link, cset_link);
2774 cset = link->cset;
Tejun Heoc7561122014-02-25 10:04:01 -05002775 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
2776
Tejun Heod5158762013-08-08 20:11:26 -04002777 it->cset_link = l;
Tejun Heoc7561122014-02-25 10:04:01 -05002778
2779 if (!list_empty(&cset->tasks))
2780 it->task = cset->tasks.next;
2781 else
2782 it->task = cset->mg_tasks.next;
Tejun Heod5158762013-08-08 20:11:26 -04002783}
2784
Tejun Heo0942eee2013-08-08 20:11:26 -04002785/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002786 * css_task_iter_start - initiate task iteration
2787 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04002788 * @it: the task iterator to use
2789 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002790 * Initiate iteration through the tasks of @css. The caller can call
2791 * css_task_iter_next() to walk through the tasks until the function
2792 * returns NULL. On completion of iteration, css_task_iter_end() must be
2793 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04002794 *
2795 * Note that this function acquires a lock which is released when the
2796 * iteration finishes. The caller can't sleep while iteration is in
2797 * progress.
2798 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002799void css_task_iter_start(struct cgroup_subsys_state *css,
2800 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05002801 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07002802{
Tejun Heo56fde9e2014-02-13 06:58:38 -05002803 /* no one should try to iterate before mounting cgroups */
2804 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002805
Tejun Heo96d365e2014-02-13 06:58:40 -05002806 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002807
Tejun Heo72ec7022013-08-08 20:11:26 -04002808 it->origin_css = css;
2809 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002810
Tejun Heo72ec7022013-08-08 20:11:26 -04002811 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002812}
2813
Tejun Heo0942eee2013-08-08 20:11:26 -04002814/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002815 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04002816 * @it: the task iterator being iterated
2817 *
2818 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04002819 * initialized via css_task_iter_start(). Returns NULL when the iteration
2820 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04002821 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002822struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002823{
2824 struct task_struct *res;
2825 struct list_head *l = it->task;
Tejun Heoc7561122014-02-25 10:04:01 -05002826 struct cgrp_cset_link *link = list_entry(it->cset_link,
2827 struct cgrp_cset_link, cset_link);
Paul Menage817929e2007-10-18 23:39:36 -07002828
2829 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07002830 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07002831 return NULL;
2832 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05002833
2834 /*
2835 * Advance iterator to find next entry. cset->tasks is consumed
2836 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
2837 * next cset.
2838 */
Paul Menage817929e2007-10-18 23:39:36 -07002839 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05002840
2841 if (l == &link->cset->tasks)
2842 l = link->cset->mg_tasks.next;
2843
2844 if (l == &link->cset->mg_tasks)
Tejun Heo72ec7022013-08-08 20:11:26 -04002845 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05002846 else
Paul Menage817929e2007-10-18 23:39:36 -07002847 it->task = l;
Tejun Heoc7561122014-02-25 10:04:01 -05002848
Paul Menage817929e2007-10-18 23:39:36 -07002849 return res;
2850}
2851
Tejun Heo0942eee2013-08-08 20:11:26 -04002852/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002853 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04002854 * @it: the task iterator to finish
2855 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002856 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04002857 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002858void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05002859 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07002860{
Tejun Heo96d365e2014-02-13 06:58:40 -05002861 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07002862}
2863
2864/**
2865 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
2866 * @to: cgroup to which the tasks will be moved
2867 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05002868 *
2869 * Locking rules between cgroup_post_fork() and the migration path
2870 * guarantee that, if a task is forking while being migrated, the new child
2871 * is guaranteed to be either visible in the source cgroup after the
2872 * parent's migration is complete or put into the target cgroup. No task
2873 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07002874 */
2875int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
2876{
Tejun Heo952aaa12014-02-25 10:04:03 -05002877 LIST_HEAD(preloaded_csets);
2878 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05002879 struct css_task_iter it;
2880 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05002881 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05002882
Tejun Heo952aaa12014-02-25 10:04:03 -05002883 mutex_lock(&cgroup_mutex);
2884
2885 /* all tasks in @from are being moved, all csets are source */
2886 down_read(&css_set_rwsem);
2887 list_for_each_entry(link, &from->cset_links, cset_link)
2888 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
2889 up_read(&css_set_rwsem);
2890
2891 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
2892 if (ret)
2893 goto out_err;
2894
2895 /*
2896 * Migrate tasks one-by-one until @form is empty. This fails iff
2897 * ->can_attach() fails.
2898 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05002899 do {
2900 css_task_iter_start(&from->dummy_css, &it);
2901 task = css_task_iter_next(&it);
2902 if (task)
2903 get_task_struct(task);
2904 css_task_iter_end(&it);
2905
2906 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05002907 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05002908 put_task_struct(task);
2909 }
2910 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05002911out_err:
2912 cgroup_migrate_finish(&preloaded_csets);
2913 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05002914 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07002915}
2916
Paul Menage817929e2007-10-18 23:39:36 -07002917/*
Ben Blum102a7752009-09-23 15:56:26 -07002918 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002919 *
2920 * Reading this file can return large amounts of data if a cgroup has
2921 * *lots* of attached tasks. So it may need several calls to read(),
2922 * but we cannot guarantee that the information we produce is correct
2923 * unless we produce it entirely atomically.
2924 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002925 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002926
Li Zefan24528252012-01-20 11:58:43 +08002927/* which pidlist file are we talking about? */
2928enum cgroup_filetype {
2929 CGROUP_FILE_PROCS,
2930 CGROUP_FILE_TASKS,
2931};
2932
2933/*
2934 * A pidlist is a list of pids that virtually represents the contents of one
2935 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
2936 * a pair (one each for procs, tasks) for each pid namespace that's relevant
2937 * to the cgroup.
2938 */
2939struct cgroup_pidlist {
2940 /*
2941 * used to find which pidlist is wanted. doesn't change as long as
2942 * this particular list stays in the list.
2943 */
2944 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
2945 /* array of xids */
2946 pid_t *list;
2947 /* how many elements the above list has */
2948 int length;
Li Zefan24528252012-01-20 11:58:43 +08002949 /* each of these stored in a list by its cgroup */
2950 struct list_head links;
2951 /* pointer to the cgroup we belong to, for list removal purposes */
2952 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05002953 /* for delayed destruction */
2954 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08002955};
2956
Paul Menagebbcb81d2007-10-18 23:39:32 -07002957/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002958 * The following two functions "fix" the issue where there are more pids
2959 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2960 * TODO: replace with a kernel-wide solution to this problem
2961 */
2962#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2963static void *pidlist_allocate(int count)
2964{
2965 if (PIDLIST_TOO_LARGE(count))
2966 return vmalloc(count * sizeof(pid_t));
2967 else
2968 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2969}
Tejun Heob1a21362013-11-29 10:42:58 -05002970
Ben Blumd1d9fd32009-09-23 15:56:28 -07002971static void pidlist_free(void *p)
2972{
2973 if (is_vmalloc_addr(p))
2974 vfree(p);
2975 else
2976 kfree(p);
2977}
Ben Blumd1d9fd32009-09-23 15:56:28 -07002978
2979/*
Tejun Heob1a21362013-11-29 10:42:58 -05002980 * Used to destroy all pidlists lingering waiting for destroy timer. None
2981 * should be left afterwards.
2982 */
2983static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
2984{
2985 struct cgroup_pidlist *l, *tmp_l;
2986
2987 mutex_lock(&cgrp->pidlist_mutex);
2988 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
2989 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
2990 mutex_unlock(&cgrp->pidlist_mutex);
2991
2992 flush_workqueue(cgroup_pidlist_destroy_wq);
2993 BUG_ON(!list_empty(&cgrp->pidlists));
2994}
2995
2996static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
2997{
2998 struct delayed_work *dwork = to_delayed_work(work);
2999 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3000 destroy_dwork);
3001 struct cgroup_pidlist *tofree = NULL;
3002
3003 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003004
3005 /*
Tejun Heo04502362013-11-29 10:42:59 -05003006 * Destroy iff we didn't get queued again. The state won't change
3007 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003008 */
Tejun Heo04502362013-11-29 10:42:59 -05003009 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003010 list_del(&l->links);
3011 pidlist_free(l->list);
3012 put_pid_ns(l->key.ns);
3013 tofree = l;
3014 }
3015
Tejun Heob1a21362013-11-29 10:42:58 -05003016 mutex_unlock(&l->owner->pidlist_mutex);
3017 kfree(tofree);
3018}
3019
3020/*
Ben Blum102a7752009-09-23 15:56:26 -07003021 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003022 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003023 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003024static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003025{
Ben Blum102a7752009-09-23 15:56:26 -07003026 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003027
3028 /*
3029 * we presume the 0th element is unique, so i starts at 1. trivial
3030 * edge cases first; no work needs to be done for either
3031 */
3032 if (length == 0 || length == 1)
3033 return length;
3034 /* src and dest walk down the list; dest counts unique elements */
3035 for (src = 1; src < length; src++) {
3036 /* find next unique element */
3037 while (list[src] == list[src-1]) {
3038 src++;
3039 if (src == length)
3040 goto after;
3041 }
3042 /* dest always points to where the next unique element goes */
3043 list[dest] = list[src];
3044 dest++;
3045 }
3046after:
Ben Blum102a7752009-09-23 15:56:26 -07003047 return dest;
3048}
3049
Tejun Heoafb2bc12013-11-29 10:42:59 -05003050/*
3051 * The two pid files - task and cgroup.procs - guaranteed that the result
3052 * is sorted, which forced this whole pidlist fiasco. As pid order is
3053 * different per namespace, each namespace needs differently sorted list,
3054 * making it impossible to use, for example, single rbtree of member tasks
3055 * sorted by task pointer. As pidlists can be fairly large, allocating one
3056 * per open file is dangerous, so cgroup had to implement shared pool of
3057 * pidlists keyed by cgroup and namespace.
3058 *
3059 * All this extra complexity was caused by the original implementation
3060 * committing to an entirely unnecessary property. In the long term, we
3061 * want to do away with it. Explicitly scramble sort order if
3062 * sane_behavior so that no such expectation exists in the new interface.
3063 *
3064 * Scrambling is done by swapping every two consecutive bits, which is
3065 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3066 */
3067static pid_t pid_fry(pid_t pid)
3068{
3069 unsigned a = pid & 0x55555555;
3070 unsigned b = pid & 0xAAAAAAAA;
3071
3072 return (a << 1) | (b >> 1);
3073}
3074
3075static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3076{
3077 if (cgroup_sane_behavior(cgrp))
3078 return pid_fry(pid);
3079 else
3080 return pid;
3081}
3082
Ben Blum102a7752009-09-23 15:56:26 -07003083static int cmppid(const void *a, const void *b)
3084{
3085 return *(pid_t *)a - *(pid_t *)b;
3086}
3087
Tejun Heoafb2bc12013-11-29 10:42:59 -05003088static int fried_cmppid(const void *a, const void *b)
3089{
3090 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3091}
3092
Ben Blum72a8cb32009-09-23 15:56:27 -07003093static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3094 enum cgroup_filetype type)
3095{
3096 struct cgroup_pidlist *l;
3097 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003098 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003099
Tejun Heoe6b81712013-11-29 10:42:59 -05003100 lockdep_assert_held(&cgrp->pidlist_mutex);
3101
3102 list_for_each_entry(l, &cgrp->pidlists, links)
3103 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003104 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003105 return NULL;
3106}
3107
Ben Blum72a8cb32009-09-23 15:56:27 -07003108/*
3109 * find the appropriate pidlist for our purpose (given procs vs tasks)
3110 * returns with the lock on that pidlist already held, and takes care
3111 * of the use count, or returns NULL with no locks held if we're out of
3112 * memory.
3113 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003114static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3115 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003116{
3117 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003118
Tejun Heoe6b81712013-11-29 10:42:59 -05003119 lockdep_assert_held(&cgrp->pidlist_mutex);
3120
3121 l = cgroup_pidlist_find(cgrp, type);
3122 if (l)
3123 return l;
3124
Ben Blum72a8cb32009-09-23 15:56:27 -07003125 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003126 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003127 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003128 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003129
Tejun Heob1a21362013-11-29 10:42:58 -05003130 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003131 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003132 /* don't need task_nsproxy() if we're looking at ourself */
3133 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003134 l->owner = cgrp;
3135 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003136 return l;
3137}
3138
3139/*
Ben Blum102a7752009-09-23 15:56:26 -07003140 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3141 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003142static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3143 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003144{
3145 pid_t *array;
3146 int length;
3147 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003148 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003149 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003150 struct cgroup_pidlist *l;
3151
Tejun Heo4bac00d2013-11-29 10:42:59 -05003152 lockdep_assert_held(&cgrp->pidlist_mutex);
3153
Ben Blum102a7752009-09-23 15:56:26 -07003154 /*
3155 * If cgroup gets more users after we read count, we won't have
3156 * enough space - tough. This race is indistinguishable to the
3157 * caller from the case that the additional cgroup users didn't
3158 * show up until sometime later on.
3159 */
3160 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003161 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003162 if (!array)
3163 return -ENOMEM;
3164 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003165 css_task_iter_start(&cgrp->dummy_css, &it);
3166 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003167 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003168 break;
Ben Blum102a7752009-09-23 15:56:26 -07003169 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003170 if (type == CGROUP_FILE_PROCS)
3171 pid = task_tgid_vnr(tsk);
3172 else
3173 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003174 if (pid > 0) /* make sure to only use valid results */
3175 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003176 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003177 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003178 length = n;
3179 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003180 if (cgroup_sane_behavior(cgrp))
3181 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3182 else
3183 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003184 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003185 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003186
Tejun Heoe6b81712013-11-29 10:42:59 -05003187 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003188 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003189 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003190 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003191 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003192 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003193
3194 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003195 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003196 l->list = array;
3197 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003198 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003199 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003200}
3201
Balbir Singh846c7bb2007-10-18 23:39:44 -07003202/**
Li Zefana043e3b2008-02-23 15:24:09 -08003203 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003204 * @stats: cgroupstats to fill information into
3205 * @dentry: A dentry entry belonging to the cgroup for which stats have
3206 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003207 *
3208 * Build and fill cgroupstats so that taskstats can export it to user
3209 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003210 */
3211int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3212{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003213 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003214 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003215 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003216 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003217
Tejun Heo2bd59d42014-02-11 11:52:49 -05003218 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3219 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3220 kernfs_type(kn) != KERNFS_DIR)
3221 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003222
Li Zefanbad34662014-02-14 16:54:28 +08003223 mutex_lock(&cgroup_mutex);
3224
Tejun Heo2bd59d42014-02-11 11:52:49 -05003225 /*
3226 * We aren't being called from kernfs and there's no guarantee on
3227 * @kn->priv's validity. For this and css_tryget_from_dir(),
3228 * @kn->priv is RCU safe. Let's do the RCU dancing.
3229 */
3230 rcu_read_lock();
3231 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003232 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003233 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003234 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003235 return -ENOENT;
3236 }
Li Zefanbad34662014-02-14 16:54:28 +08003237 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003238
Tejun Heo72ec7022013-08-08 20:11:26 -04003239 css_task_iter_start(&cgrp->dummy_css, &it);
3240 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003241 switch (tsk->state) {
3242 case TASK_RUNNING:
3243 stats->nr_running++;
3244 break;
3245 case TASK_INTERRUPTIBLE:
3246 stats->nr_sleeping++;
3247 break;
3248 case TASK_UNINTERRUPTIBLE:
3249 stats->nr_uninterruptible++;
3250 break;
3251 case TASK_STOPPED:
3252 stats->nr_stopped++;
3253 break;
3254 default:
3255 if (delayacct_is_task_waiting_on_io(tsk))
3256 stats->nr_io_wait++;
3257 break;
3258 }
3259 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003260 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003261
Li Zefanbad34662014-02-14 16:54:28 +08003262 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003263 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003264}
3265
Paul Menage8f3ff202009-09-23 15:56:25 -07003266
Paul Menagecc31edc2008-10-18 20:28:04 -07003267/*
Ben Blum102a7752009-09-23 15:56:26 -07003268 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003269 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003270 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003271 */
3272
Ben Blum102a7752009-09-23 15:56:26 -07003273static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003274{
3275 /*
3276 * Initially we receive a position value that corresponds to
3277 * one more than the last pid shown (or 0 on the first call or
3278 * after a seek to the start). Use a binary-search to find the
3279 * next pid to display, if any
3280 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003281 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003282 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003283 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003284 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003285 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003286 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003287
Tejun Heo4bac00d2013-11-29 10:42:59 -05003288 mutex_lock(&cgrp->pidlist_mutex);
3289
3290 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003291 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003292 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003293 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003294 * could already have been destroyed.
3295 */
Tejun Heo5d224442013-12-05 12:28:04 -05003296 if (of->priv)
3297 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003298
3299 /*
3300 * Either this is the first start() after open or the matching
3301 * pidlist has been destroyed inbetween. Create a new one.
3302 */
Tejun Heo5d224442013-12-05 12:28:04 -05003303 if (!of->priv) {
3304 ret = pidlist_array_load(cgrp, type,
3305 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003306 if (ret)
3307 return ERR_PTR(ret);
3308 }
Tejun Heo5d224442013-12-05 12:28:04 -05003309 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003310
Paul Menagecc31edc2008-10-18 20:28:04 -07003311 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003312 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003313
Paul Menagecc31edc2008-10-18 20:28:04 -07003314 while (index < end) {
3315 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003316 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003317 index = mid;
3318 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003319 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003320 index = mid + 1;
3321 else
3322 end = mid;
3323 }
3324 }
3325 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003326 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003327 return NULL;
3328 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003329 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003330 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003331 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003332}
3333
Ben Blum102a7752009-09-23 15:56:26 -07003334static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003335{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003336 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003337 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003338
Tejun Heo5d224442013-12-05 12:28:04 -05003339 if (l)
3340 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003341 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003342 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003343}
3344
Ben Blum102a7752009-09-23 15:56:26 -07003345static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003346{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003347 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003348 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003349 pid_t *p = v;
3350 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003351 /*
3352 * Advance to the next pid in the array. If this goes off the
3353 * end, we're done
3354 */
3355 p++;
3356 if (p >= end) {
3357 return NULL;
3358 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003359 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003360 return p;
3361 }
3362}
3363
Ben Blum102a7752009-09-23 15:56:26 -07003364static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003365{
3366 return seq_printf(s, "%d\n", *(int *)v);
3367}
3368
Ben Blum102a7752009-09-23 15:56:26 -07003369/*
3370 * seq_operations functions for iterating on pidlists through seq_file -
3371 * independent of whether it's tasks or procs
3372 */
3373static const struct seq_operations cgroup_pidlist_seq_operations = {
3374 .start = cgroup_pidlist_start,
3375 .stop = cgroup_pidlist_stop,
3376 .next = cgroup_pidlist_next,
3377 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003378};
3379
Tejun Heo182446d2013-08-08 20:11:24 -04003380static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3381 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003382{
Tejun Heo182446d2013-08-08 20:11:24 -04003383 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003384}
3385
Tejun Heo182446d2013-08-08 20:11:24 -04003386static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3387 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003388{
Tejun Heo182446d2013-08-08 20:11:24 -04003389 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003390 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003391 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003392 else
Tejun Heo182446d2013-08-08 20:11:24 -04003393 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003394 return 0;
3395}
3396
Tejun Heo182446d2013-08-08 20:11:24 -04003397static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3398 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003399{
Tejun Heo182446d2013-08-08 20:11:24 -04003400 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003401}
3402
Tejun Heo182446d2013-08-08 20:11:24 -04003403static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3404 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003405{
3406 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003407 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003408 else
Tejun Heo182446d2013-08-08 20:11:24 -04003409 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003410 return 0;
3411}
3412
Tejun Heod5c56ce2013-06-03 19:14:34 -07003413static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003414 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003415 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003416 .seq_start = cgroup_pidlist_start,
3417 .seq_next = cgroup_pidlist_next,
3418 .seq_stop = cgroup_pidlist_stop,
3419 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003420 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003421 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003422 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003423 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003424 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003425 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003426 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003427 .read_u64 = cgroup_clone_children_read,
3428 .write_u64 = cgroup_clone_children_write,
3429 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003430 {
Tejun Heo873fe092013-04-14 20:15:26 -07003431 .name = "cgroup.sane_behavior",
3432 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003433 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003434 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003435
3436 /*
3437 * Historical crazy stuff. These don't have "cgroup." prefix and
3438 * don't exist if sane_behavior. If you're depending on these, be
3439 * prepared to be burned.
3440 */
3441 {
3442 .name = "tasks",
3443 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003444 .seq_start = cgroup_pidlist_start,
3445 .seq_next = cgroup_pidlist_next,
3446 .seq_stop = cgroup_pidlist_stop,
3447 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003448 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003449 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003450 .mode = S_IRUGO | S_IWUSR,
3451 },
3452 {
3453 .name = "notify_on_release",
3454 .flags = CFTYPE_INSANE,
3455 .read_u64 = cgroup_read_notify_on_release,
3456 .write_u64 = cgroup_write_notify_on_release,
3457 },
Tejun Heo873fe092013-04-14 20:15:26 -07003458 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003459 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003460 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003461 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003462 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003463 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003464 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003465 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003466};
3467
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003468/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003469 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003470 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003471 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003472 *
3473 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003474 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003475static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003476{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003477 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003478 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003479
Tejun Heo8e3f6542012-04-01 12:09:55 -07003480 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003481 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05003482 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07003483
3484 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003485 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003486
Tejun Heo0adb0702014-02-12 09:29:48 -05003487 list_for_each_entry(cfts, &ss->cfts, node) {
3488 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003489 if (ret < 0)
3490 goto err;
3491 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003492 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003493 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003494err:
3495 cgroup_clear_dir(cgrp, subsys_mask);
3496 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003497}
3498
Tejun Heo0c21ead2013-08-13 20:22:51 -04003499/*
3500 * css destruction is four-stage process.
3501 *
3502 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3503 * Implemented in kill_css().
3504 *
3505 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3506 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3507 * by invoking offline_css(). After offlining, the base ref is put.
3508 * Implemented in css_killed_work_fn().
3509 *
3510 * 3. When the percpu_ref reaches zero, the only possible remaining
3511 * accessors are inside RCU read sections. css_release() schedules the
3512 * RCU callback.
3513 *
3514 * 4. After the grace period, the css can be freed. Implemented in
3515 * css_free_work_fn().
3516 *
3517 * It is actually hairier because both step 2 and 4 require process context
3518 * and thus involve punting to css->destroy_work adding two additional
3519 * steps to the already complex sequence.
3520 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003521static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003522{
3523 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003524 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003525 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003526
Tejun Heo0ae78e02013-08-13 11:01:54 -04003527 if (css->parent)
3528 css_put(css->parent);
3529
Tejun Heo0c21ead2013-08-13 20:22:51 -04003530 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003531 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003532}
3533
3534static void css_free_rcu_fn(struct rcu_head *rcu_head)
3535{
3536 struct cgroup_subsys_state *css =
3537 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3538
Tejun Heo0c21ead2013-08-13 20:22:51 -04003539 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003540 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003541}
3542
Tejun Heod3daf282013-06-13 19:39:16 -07003543static void css_release(struct percpu_ref *ref)
3544{
3545 struct cgroup_subsys_state *css =
3546 container_of(ref, struct cgroup_subsys_state, refcnt);
3547
Monam Agarwal01a97142014-03-24 00:17:18 +05303548 RCU_INIT_POINTER(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003549 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003550}
3551
Tejun Heo623f9262013-08-13 11:01:55 -04003552static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3553 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003554{
Paul Menagebd89aab2007-10-18 23:40:44 -07003555 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003556 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003557 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003558
Tejun Heo0ae78e02013-08-13 11:01:54 -04003559 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003560 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003561 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003562 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003563
Tejun Heoca8bdca2013-08-26 18:40:56 -04003564 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003565}
3566
Li Zefan2a4ac632013-07-31 16:16:40 +08003567/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04003568static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003569{
Tejun Heo623f9262013-08-13 11:01:55 -04003570 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08003571 int ret = 0;
3572
Tejun Heoace2bee2014-02-11 11:52:47 -05003573 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003574 lockdep_assert_held(&cgroup_mutex);
3575
Tejun Heo92fb9742012-11-19 08:13:38 -08003576 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04003577 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003578 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04003579 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04003580 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05003581 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003582 }
Tejun Heob1929db2012-11-19 08:13:38 -08003583 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003584}
3585
Li Zefan2a4ac632013-07-31 16:16:40 +08003586/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04003587static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003588{
Tejun Heo623f9262013-08-13 11:01:55 -04003589 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003590
Tejun Heoace2bee2014-02-11 11:52:47 -05003591 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003592 lockdep_assert_held(&cgroup_mutex);
3593
3594 if (!(css->flags & CSS_ONLINE))
3595 return;
3596
Li Zefand7eeac12013-03-12 15:35:59 -07003597 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04003598 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003599
Tejun Heoeb954192013-08-08 20:11:23 -04003600 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04003601 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05003602 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003603}
3604
Tejun Heoc81c925a2013-12-06 15:11:56 -05003605/**
3606 * create_css - create a cgroup_subsys_state
3607 * @cgrp: the cgroup new css will be associated with
3608 * @ss: the subsys of new css
3609 *
3610 * Create a new css associated with @cgrp - @ss pair. On success, the new
3611 * css is online and installed in @cgrp with all interface files created.
3612 * Returns 0 on success, -errno on failure.
3613 */
3614static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
3615{
3616 struct cgroup *parent = cgrp->parent;
3617 struct cgroup_subsys_state *css;
3618 int err;
3619
Tejun Heoc81c925a2013-12-06 15:11:56 -05003620 lockdep_assert_held(&cgroup_mutex);
3621
3622 css = ss->css_alloc(cgroup_css(parent, ss));
3623 if (IS_ERR(css))
3624 return PTR_ERR(css);
3625
3626 err = percpu_ref_init(&css->refcnt, css_release);
3627 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08003628 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05003629
3630 init_css(css, ss, cgrp);
3631
Tejun Heoaec25022014-02-08 10:36:58 -05003632 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003633 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08003634 goto err_free_percpu_ref;
Tejun Heoc81c925a2013-12-06 15:11:56 -05003635
3636 err = online_css(css);
3637 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08003638 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05003639
Tejun Heo59f52962014-02-11 11:52:49 -05003640 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003641 css_get(css->parent);
3642
Tejun Heo94419622014-03-19 10:23:54 -04003643 cgrp->subsys_mask |= 1 << ss->id;
3644
Tejun Heoc81c925a2013-12-06 15:11:56 -05003645 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
3646 parent->parent) {
3647 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",
3648 current->comm, current->pid, ss->name);
3649 if (!strcmp(ss->name, "memory"))
3650 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
3651 ss->warned_broken_hierarchy = true;
3652 }
3653
3654 return 0;
3655
Li Zefan3eb59ec2014-03-18 17:02:36 +08003656err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07003657 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08003658err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05003659 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08003660err_free_css:
Tejun Heoc81c925a2013-12-06 15:11:56 -05003661 ss->css_free(css);
3662 return err;
3663}
3664
Tejun Heo2bd59d42014-02-11 11:52:49 -05003665/**
Li Zefana043e3b2008-02-23 15:24:09 -08003666 * cgroup_create - create a cgroup
3667 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05003668 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05003669 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07003670 */
Tejun Heoe61734c2014-02-12 09:29:50 -05003671static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003672 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003673{
Paul Menagebd89aab2007-10-18 23:40:44 -07003674 struct cgroup *cgrp;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003675 struct cgroup_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05003676 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003677 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003678 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003679
Tejun Heoa2dd4242014-03-19 10:23:55 -04003680 /*
3681 * XXX: The default hierarchy isn't fully implemented yet. Block
3682 * !root cgroup creation on it for now.
3683 */
3684 if (root == &cgrp_dfl_root)
3685 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003686
Tejun Heo0a950f62012-11-19 09:02:12 -08003687 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07003688 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3689 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003690 return -ENOMEM;
3691
Tejun Heoace2bee2014-02-11 11:52:47 -05003692 mutex_lock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003693
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003694 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08003695 * Only live parents can have children. Note that the liveliness
3696 * check isn't strictly necessary because cgroup_mkdir() and
3697 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
3698 * anyway so that locking is contained inside cgroup proper and we
3699 * don't get nasty surprises if we ever grow another caller.
3700 */
3701 if (!cgroup_lock_live_group(parent)) {
3702 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05003703 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08003704 }
3705
3706 /*
3707 * Temporarily set the pointer to NULL, so idr_find() won't return
3708 * a half-baked cgroup.
3709 */
3710 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
3711 if (cgrp->id < 0) {
3712 err = -ENOMEM;
3713 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08003714 }
3715
Paul Menagecc31edc2008-10-18 20:28:04 -07003716 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003717
Paul Menagebd89aab2007-10-18 23:40:44 -07003718 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003719 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07003720 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003721
Li Zefanb6abdb02008-03-04 14:28:19 -08003722 if (notify_on_release(parent))
3723 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3724
Tejun Heo2260e7f2012-11-19 08:13:38 -08003725 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
3726 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003727
Tejun Heo2bd59d42014-02-11 11:52:49 -05003728 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05003729 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003730 if (IS_ERR(kn)) {
3731 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003732 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003733 }
3734 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003735
Tejun Heo6f305582014-02-12 09:29:50 -05003736 /*
3737 * This extra ref will be put in cgroup_free_fn() and guarantees
3738 * that @cgrp->kn is always accessible.
3739 */
3740 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003741
Tejun Heo00356bd2013-06-18 11:14:22 -07003742 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09003743
Tejun Heo4e139af2012-11-19 08:13:36 -08003744 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08003745 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05003746 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05003747 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08003748
Tejun Heo0d802552013-12-06 15:11:56 -05003749 /*
3750 * @cgrp is now fully operational. If something fails after this
3751 * point, it'll be released via the normal destruction path.
3752 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003753 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
3754
Tejun Heo2bb566c2013-08-08 20:11:23 -04003755 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07003756 if (err)
3757 goto err_destroy;
3758
Tejun Heo9d403e92013-12-06 15:11:56 -05003759 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05003760 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003761 if (root->cgrp.subsys_mask & (1 << ssid)) {
Tejun Heob85d2042013-12-06 15:11:57 -05003762 err = create_css(cgrp, ss);
3763 if (err)
3764 goto err_destroy;
3765 }
Tejun Heoa8638032012-11-09 09:12:29 -08003766 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003767
Tejun Heo2bd59d42014-02-11 11:52:49 -05003768 kernfs_activate(kn);
3769
Paul Menageddbcc7e2007-10-18 23:39:30 -07003770 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003771 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003772
3773 return 0;
3774
Tejun Heo0a950f62012-11-19 09:02:12 -08003775err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003776 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003777err_unlock:
3778 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003779err_unlock_tree:
3780 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003781 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003782 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003783
3784err_destroy:
3785 cgroup_destroy_locked(cgrp);
3786 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003787 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003788 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003789}
3790
Tejun Heo2bd59d42014-02-11 11:52:49 -05003791static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3792 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003793{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003794 struct cgroup *parent = parent_kn->priv;
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003795 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003796
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003797 /*
3798 * cgroup_create() grabs cgroup_tree_mutex which nests outside
3799 * kernfs active_ref and cgroup_create() already synchronizes
3800 * properly against removal through cgroup_lock_live_group().
3801 * Break it before calling cgroup_create().
3802 */
3803 cgroup_get(parent);
3804 kernfs_break_active_protection(parent_kn);
3805
3806 ret = cgroup_create(parent, name, mode);
3807
3808 kernfs_unbreak_active_protection(parent_kn);
3809 cgroup_put(parent);
3810 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003811}
3812
Tejun Heo223dbc32013-08-13 20:22:50 -04003813/*
3814 * This is called when the refcnt of a css is confirmed to be killed.
3815 * css_tryget() is now guaranteed to fail.
3816 */
3817static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07003818{
Tejun Heo223dbc32013-08-13 20:22:50 -04003819 struct cgroup_subsys_state *css =
3820 container_of(work, struct cgroup_subsys_state, destroy_work);
3821 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07003822
Tejun Heoace2bee2014-02-11 11:52:47 -05003823 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003824 mutex_lock(&cgroup_mutex);
3825
3826 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04003827 * css_tryget() is guaranteed to fail now. Tell subsystems to
3828 * initate destruction.
3829 */
3830 offline_css(css);
3831
3832 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003833 * If @cgrp is marked dead, it's waiting for refs of all css's to
3834 * be disabled before proceeding to the second phase of cgroup
3835 * destruction. If we are the last one, kick it off.
3836 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04003837 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04003838 cgroup_destroy_css_killed(cgrp);
3839
3840 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003841 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04003842
3843 /*
3844 * Put the css refs from kill_css(). Each css holds an extra
3845 * reference to the cgroup's dentry and cgroup removal proceeds
3846 * regardless of css refs. On the last put of each css, whenever
3847 * that may be, the extra dentry ref is put so that dentry
3848 * destruction happens only after all css's are released.
3849 */
3850 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07003851}
3852
Tejun Heo223dbc32013-08-13 20:22:50 -04003853/* css kill confirmation processing requires process context, bounce */
3854static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07003855{
3856 struct cgroup_subsys_state *css =
3857 container_of(ref, struct cgroup_subsys_state, refcnt);
3858
Tejun Heo223dbc32013-08-13 20:22:50 -04003859 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003860 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07003861}
3862
Tejun Heo94419622014-03-19 10:23:54 -04003863static void __kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04003864{
Tejun Heo94419622014-03-19 10:23:54 -04003865 lockdep_assert_held(&cgroup_tree_mutex);
3866
Tejun Heo2bd59d42014-02-11 11:52:49 -05003867 /*
3868 * This must happen before css is disassociated with its cgroup.
3869 * See seq_css() for details.
3870 */
Tejun Heoaec25022014-02-08 10:36:58 -05003871 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003872
Tejun Heoedae0c32013-08-13 20:22:51 -04003873 /*
3874 * Killing would put the base ref, but we need to keep it alive
3875 * until after ->css_offline().
3876 */
3877 css_get(css);
3878
3879 /*
3880 * cgroup core guarantees that, by the time ->css_offline() is
3881 * invoked, no new css reference will be given out via
3882 * css_tryget(). We can't simply call percpu_ref_kill() and
3883 * proceed to offlining css's because percpu_ref_kill() doesn't
3884 * guarantee that the ref is seen as killed on all CPUs on return.
3885 *
3886 * Use percpu_ref_kill_and_confirm() to get notifications as each
3887 * css is confirmed to be seen as killed on all CPUs.
3888 */
3889 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003890}
3891
3892/**
Tejun Heo94419622014-03-19 10:23:54 -04003893 * kill_css - destroy a css
3894 * @css: css to destroy
3895 *
3896 * This function initiates destruction of @css by removing cgroup interface
3897 * files and putting its base reference. ->css_offline() will be invoked
3898 * asynchronously once css_tryget() is guaranteed to fail and when the
3899 * reference count reaches zero, @css will be released.
3900 */
3901static void kill_css(struct cgroup_subsys_state *css)
3902{
3903 struct cgroup *cgrp = css->cgroup;
3904
3905 lockdep_assert_held(&cgroup_tree_mutex);
3906
3907 /* if already killed, noop */
3908 if (cgrp->subsys_mask & (1 << css->ss->id)) {
3909 cgrp->subsys_mask &= ~(1 << css->ss->id);
3910 __kill_css(css);
3911 }
3912}
3913
3914/**
Tejun Heod3daf282013-06-13 19:39:16 -07003915 * cgroup_destroy_locked - the first stage of cgroup destruction
3916 * @cgrp: cgroup to be destroyed
3917 *
3918 * css's make use of percpu refcnts whose killing latency shouldn't be
3919 * exposed to userland and are RCU protected. Also, cgroup core needs to
3920 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
3921 * invoked. To satisfy all the requirements, destruction is implemented in
3922 * the following two steps.
3923 *
3924 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
3925 * userland visible parts and start killing the percpu refcnts of
3926 * css's. Set up so that the next stage will be kicked off once all
3927 * the percpu refcnts are confirmed to be killed.
3928 *
3929 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
3930 * rest of destruction. Once all cgroup references are gone, the
3931 * cgroup is RCU-freed.
3932 *
3933 * This function implements s1. After this step, @cgrp is gone as far as
3934 * the userland is concerned and a new cgroup with the same name may be
3935 * created. As cgroup doesn't care about the names internally, this
3936 * doesn't cause any problem.
3937 */
Tejun Heo42809dd2012-11-19 08:13:37 -08003938static int cgroup_destroy_locked(struct cgroup *cgrp)
3939 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003940{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003941 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003942 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07003943 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05003944 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003945
Tejun Heoace2bee2014-02-11 11:52:47 -05003946 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003947 lockdep_assert_held(&cgroup_mutex);
3948
Tejun Heoddd69142013-06-12 21:04:54 -07003949 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05003950 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05003951 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07003952 */
Tejun Heo96d365e2014-02-13 06:58:40 -05003953 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003954 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05003955 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07003956 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003957 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08003958
Tejun Heo1a90dd52012-11-05 09:16:59 -08003959 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003960 * Make sure there's no live children. We can't test ->children
3961 * emptiness as dead children linger on it while being destroyed;
3962 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
3963 */
3964 empty = true;
3965 rcu_read_lock();
3966 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
3967 empty = cgroup_is_dead(child);
3968 if (!empty)
3969 break;
3970 }
3971 rcu_read_unlock();
3972 if (!empty)
3973 return -EBUSY;
3974
3975 /*
Tejun Heo455050d2013-06-13 19:27:41 -07003976 * Mark @cgrp dead. This prevents further task migration and child
3977 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04003978 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07003979 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04003980 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07003981 */
Tejun Heo54766d42013-06-12 21:04:53 -07003982 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08003983
Tejun Heo5d773812014-03-19 10:23:53 -04003984 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003985 * Initiate massacre of all css's. cgroup_destroy_css_killed()
3986 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05003987 * percpu refs of all css's are confirmed to be killed. This
3988 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08003989 */
Tejun Heo4ac06012014-02-11 11:52:47 -05003990 mutex_unlock(&cgroup_mutex);
Tejun Heo1a90dd52012-11-05 09:16:59 -08003991 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07003992 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05003993 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003994
Tejun Heo455050d2013-06-13 19:27:41 -07003995 /* CGRP_DEAD is set, remove from ->release_list for the last time */
3996 raw_spin_lock(&release_list_lock);
3997 if (!list_empty(&cgrp->release_list))
3998 list_del_init(&cgrp->release_list);
3999 raw_spin_unlock(&release_list_lock);
4000
4001 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004002 * If @cgrp has css's attached, the second stage of cgroup
4003 * destruction is kicked off from css_killed_work_fn() after the
4004 * refs of all attached css's are killed. If @cgrp doesn't have
4005 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004006 */
Tejun Heof20104d2013-08-13 20:22:50 -04004007 if (!cgrp->nr_css)
4008 cgroup_destroy_css_killed(cgrp);
4009
Tejun Heo2bd59d42014-02-11 11:52:49 -05004010 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05004011 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004012
Tejun Heof20104d2013-08-13 20:22:50 -04004013 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05004014 * There are two control paths which try to determine cgroup from
4015 * dentry without going through kernfs - cgroupstats_build() and
4016 * css_tryget_from_dir(). Those are supported by RCU protecting
4017 * clearing of cgrp->kn->priv backpointer, which should happen
4018 * after all files under it have been removed.
Tejun Heo455050d2013-06-13 19:27:41 -07004019 */
Tejun Heo6f305582014-02-12 09:29:50 -05004020 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004021 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004022
Tejun Heo4ac06012014-02-11 11:52:47 -05004023 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004024
Tejun Heoea15f8c2013-06-13 19:27:42 -07004025 return 0;
4026};
4027
Tejun Heod3daf282013-06-13 19:39:16 -07004028/**
Tejun Heof20104d2013-08-13 20:22:50 -04004029 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004030 * @work: cgroup->destroy_free_work
4031 *
4032 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004033 * destroyed after all css's are offlined and performs the rest of
4034 * destruction. This is the second step of destruction described in the
4035 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004036 */
Tejun Heof20104d2013-08-13 20:22:50 -04004037static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004038{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004039 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004040
Tejun Heoace2bee2014-02-11 11:52:47 -05004041 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004042 lockdep_assert_held(&cgroup_mutex);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004043
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004044 /* delete this cgroup from parent->children */
4045 list_del_rcu(&cgrp->sibling);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004046
Tejun Heo59f52962014-02-11 11:52:49 -05004047 cgroup_put(cgrp);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004048
Paul Menageddbcc7e2007-10-18 23:39:30 -07004049 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004050 check_for_release(parent);
4051}
4052
Tejun Heo2bd59d42014-02-11 11:52:49 -05004053static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004054{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004055 struct cgroup *cgrp = kn->priv;
4056 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004057
Tejun Heo2bd59d42014-02-11 11:52:49 -05004058 /*
4059 * This is self-destruction but @kn can't be removed while this
4060 * callback is in progress. Let's break active protection. Once
4061 * the protection is broken, @cgrp can be destroyed at any point.
4062 * Pin it so that it stays accessible.
4063 */
4064 cgroup_get(cgrp);
4065 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004066
Tejun Heoace2bee2014-02-11 11:52:47 -05004067 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004068 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004069
Tejun Heo2bd59d42014-02-11 11:52:49 -05004070 /*
4071 * @cgrp might already have been destroyed while we're trying to
4072 * grab the mutexes.
4073 */
4074 if (!cgroup_is_dead(cgrp))
4075 ret = cgroup_destroy_locked(cgrp);
4076
Tejun Heo42809dd2012-11-19 08:13:37 -08004077 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004078 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004079
Tejun Heo2bd59d42014-02-11 11:52:49 -05004080 kernfs_unbreak_active_protection(kn);
4081 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004082 return ret;
4083}
4084
Tejun Heo2bd59d42014-02-11 11:52:49 -05004085static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4086 .remount_fs = cgroup_remount,
4087 .show_options = cgroup_show_options,
4088 .mkdir = cgroup_mkdir,
4089 .rmdir = cgroup_rmdir,
4090 .rename = cgroup_rename,
4091};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004092
Li Zefan06a11922008-04-29 01:00:07 -07004093static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004094{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004095 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004096
4097 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004098
Tejun Heoace2bee2014-02-11 11:52:47 -05004099 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004100 mutex_lock(&cgroup_mutex);
4101
Tejun Heo0adb0702014-02-12 09:29:48 -05004102 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004103
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004104 /* Create the root cgroup state for this subsystem */
4105 ss->root = &cgrp_dfl_root;
4106 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004107 /* We don't handle early failures gracefully */
4108 BUG_ON(IS_ERR(css));
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004109 init_css(css, ss, &cgrp_dfl_root.cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004110
Li Zefane8d55fd2008-04-29 01:00:13 -07004111 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004112 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004113 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004114 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004115 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004116
4117 need_forkexit_callback |= ss->fork || ss->exit;
4118
Li Zefane8d55fd2008-04-29 01:00:13 -07004119 /* At system boot, before all subsystems have been
4120 * registered, no tasks have been forked, so we don't
4121 * need to invoke fork callbacks here. */
4122 BUG_ON(!list_empty(&init_task.tasks));
4123
Tejun Heoae7f1642013-08-13 20:22:50 -04004124 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004125
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004126 cgrp_dfl_root.cgrp.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004127
Ben Blume6a11052010-03-10 15:22:09 -08004128 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004129 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004130}
4131
4132/**
Li Zefana043e3b2008-02-23 15:24:09 -08004133 * cgroup_init_early - cgroup initialization at system boot
4134 *
4135 * Initialize cgroups at system boot, and initialize any
4136 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004137 */
4138int __init cgroup_init_early(void)
4139{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004140 static struct cgroup_sb_opts __initdata opts =
4141 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004142 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004143 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004144
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004145 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004146 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004147
Tejun Heo3ed80a62014-02-08 10:36:58 -05004148 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004149 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004150 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4151 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004152 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004153 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4154 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004155
Tejun Heoaec25022014-02-08 10:36:58 -05004156 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004157 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004158
4159 if (ss->early_init)
4160 cgroup_init_subsys(ss);
4161 }
4162 return 0;
4163}
4164
4165/**
Li Zefana043e3b2008-02-23 15:24:09 -08004166 * cgroup_init - cgroup initialization
4167 *
4168 * Register cgroup filesystem and /proc file, and initialize
4169 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004170 */
4171int __init cgroup_init(void)
4172{
Tejun Heo30159ec2013-06-25 11:53:37 -07004173 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004174 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004175 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004176
Tejun Heo2bd59d42014-02-11 11:52:49 -05004177 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004178
Tejun Heo985ed672014-03-19 10:23:53 -04004179 mutex_lock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004180 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004181
Tejun Heo82fe9b02013-06-25 11:53:37 -07004182 /* Add init_css_set to the hash table */
4183 key = css_set_hash(init_css_set.subsys);
4184 hash_add(css_set_table, &init_css_set.hlist, key);
4185
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004186 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004187
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004188 mutex_unlock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04004189 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004190
Tejun Heo172a2c062014-03-19 10:23:53 -04004191 for_each_subsys(ss, ssid) {
4192 if (!ss->early_init)
4193 cgroup_init_subsys(ss);
4194
4195 /*
4196 * cftype registration needs kmalloc and can't be done
4197 * during early_init. Register base cftypes separately.
4198 */
4199 if (ss->base_cftypes)
4200 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4201 }
Greg KH676db4a2010-08-05 13:53:35 -07004202
Paul Menageddbcc7e2007-10-18 23:39:30 -07004203 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004204 if (!cgroup_kobj)
4205 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004206
4207 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004208 if (err < 0) {
4209 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004210 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004211 }
4212
4213 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004214 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004215}
Paul Menageb4f48b62007-10-18 23:39:33 -07004216
Tejun Heoe5fca242013-11-22 17:14:39 -05004217static int __init cgroup_wq_init(void)
4218{
4219 /*
4220 * There isn't much point in executing destruction path in
4221 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004222 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004223 *
4224 * We would prefer to do this in cgroup_init() above, but that
4225 * is called before init_workqueues(): so leave this until after.
4226 */
Tejun Heo1a115332014-02-12 19:06:19 -05004227 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004228 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004229
4230 /*
4231 * Used to destroy pidlists and separate to serve as flush domain.
4232 * Cap @max_active to 1 too.
4233 */
4234 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4235 0, 1);
4236 BUG_ON(!cgroup_pidlist_destroy_wq);
4237
Tejun Heoe5fca242013-11-22 17:14:39 -05004238 return 0;
4239}
4240core_initcall(cgroup_wq_init);
4241
Paul Menagea4243162007-10-18 23:39:35 -07004242/*
4243 * proc_cgroup_show()
4244 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4245 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004246 */
4247
4248/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004249int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004250{
4251 struct pid *pid;
4252 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004253 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004254 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004255 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004256
4257 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004258 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004259 if (!buf)
4260 goto out;
4261
4262 retval = -ESRCH;
4263 pid = m->private;
4264 tsk = get_pid_task(pid, PIDTYPE_PID);
4265 if (!tsk)
4266 goto out_free;
4267
4268 retval = 0;
4269
4270 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004271 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004272
Tejun Heo985ed672014-03-19 10:23:53 -04004273 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004274 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004275 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004276 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004277
Tejun Heoa2dd4242014-03-19 10:23:55 -04004278 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004279 continue;
4280
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004281 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004282 for_each_subsys(ss, ssid)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004283 if (root->cgrp.subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004284 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004285 if (strlen(root->name))
4286 seq_printf(m, "%sname=%s", count ? "," : "",
4287 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004288 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004289 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004290 path = cgroup_path(cgrp, buf, PATH_MAX);
4291 if (!path) {
4292 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004293 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004294 }
4295 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004296 seq_putc(m, '\n');
4297 }
4298
4299out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004300 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004301 mutex_unlock(&cgroup_mutex);
4302 put_task_struct(tsk);
4303out_free:
4304 kfree(buf);
4305out:
4306 return retval;
4307}
4308
Paul Menagea4243162007-10-18 23:39:35 -07004309/* Display information about each subsystem and each hierarchy */
4310static int proc_cgroupstats_show(struct seq_file *m, void *v)
4311{
Tejun Heo30159ec2013-06-25 11:53:37 -07004312 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004313 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004314
Paul Menage8bab8dd2008-04-04 14:29:57 -07004315 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004316 /*
4317 * ideally we don't want subsystems moving around while we do this.
4318 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4319 * subsys/hierarchy state.
4320 */
Paul Menagea4243162007-10-18 23:39:35 -07004321 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004322
4323 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004324 seq_printf(m, "%s\t%d\t%d\t%d\n",
4325 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004326 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004327
Paul Menagea4243162007-10-18 23:39:35 -07004328 mutex_unlock(&cgroup_mutex);
4329 return 0;
4330}
4331
4332static int cgroupstats_open(struct inode *inode, struct file *file)
4333{
Al Viro9dce07f2008-03-29 03:07:28 +00004334 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004335}
4336
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004337static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004338 .open = cgroupstats_open,
4339 .read = seq_read,
4340 .llseek = seq_lseek,
4341 .release = single_release,
4342};
4343
Paul Menageb4f48b62007-10-18 23:39:33 -07004344/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004345 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004346 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004347 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004348 * A task is associated with the init_css_set until cgroup_post_fork()
4349 * attaches it to the parent's css_set. Empty cg_list indicates that
4350 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004351 */
4352void cgroup_fork(struct task_struct *child)
4353{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004354 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004355 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004356}
4357
4358/**
Li Zefana043e3b2008-02-23 15:24:09 -08004359 * cgroup_post_fork - called on a new task after adding it to the task list
4360 * @child: the task in question
4361 *
Tejun Heo5edee612012-10-16 15:03:14 -07004362 * Adds the task to the list running through its css_set if necessary and
4363 * call the subsystem fork() callbacks. Has to be after the task is
4364 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004365 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004366 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004367 */
Paul Menage817929e2007-10-18 23:39:36 -07004368void cgroup_post_fork(struct task_struct *child)
4369{
Tejun Heo30159ec2013-06-25 11:53:37 -07004370 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004371 int i;
4372
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004373 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004374 * This may race against cgroup_enable_task_cg_links(). As that
4375 * function sets use_task_css_set_links before grabbing
4376 * tasklist_lock and we just went through tasklist_lock to add
4377 * @child, it's guaranteed that either we see the set
4378 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4379 * @child during its iteration.
4380 *
4381 * If we won the race, @child is associated with %current's
4382 * css_set. Grabbing css_set_rwsem guarantees both that the
4383 * association is stable, and, on completion of the parent's
4384 * migration, @child is visible in the source of migration or
4385 * already in the destination cgroup. This guarantee is necessary
4386 * when implementing operations which need to migrate all tasks of
4387 * a cgroup to another.
4388 *
4389 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4390 * will remain in init_css_set. This is safe because all tasks are
4391 * in the init_css_set before cg_links is enabled and there's no
4392 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004393 */
Paul Menage817929e2007-10-18 23:39:36 -07004394 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004395 struct css_set *cset;
4396
Tejun Heo96d365e2014-02-13 06:58:40 -05004397 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004398 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004399 if (list_empty(&child->cg_list)) {
4400 rcu_assign_pointer(child->cgroups, cset);
4401 list_add(&child->cg_list, &cset->tasks);
4402 get_css_set(cset);
4403 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004404 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004405 }
Tejun Heo5edee612012-10-16 15:03:14 -07004406
4407 /*
4408 * Call ss->fork(). This must happen after @child is linked on
4409 * css_set; otherwise, @child might change state between ->fork()
4410 * and addition to css_set.
4411 */
4412 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004413 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004414 if (ss->fork)
4415 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004416 }
Paul Menage817929e2007-10-18 23:39:36 -07004417}
Tejun Heo5edee612012-10-16 15:03:14 -07004418
Paul Menage817929e2007-10-18 23:39:36 -07004419/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004420 * cgroup_exit - detach cgroup from exiting task
4421 * @tsk: pointer to task_struct of exiting process
4422 *
4423 * Description: Detach cgroup from @tsk and release it.
4424 *
4425 * Note that cgroups marked notify_on_release force every task in
4426 * them to take the global cgroup_mutex mutex when exiting.
4427 * This could impact scaling on very large systems. Be reluctant to
4428 * use notify_on_release cgroups where very high task exit scaling
4429 * is required on large systems.
4430 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004431 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4432 * call cgroup_exit() while the task is still competent to handle
4433 * notify_on_release(), then leave the task attached to the root cgroup in
4434 * each hierarchy for the remainder of its exit. No need to bother with
4435 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004436 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004437 */
Li Zefan1ec41832014-03-28 15:22:19 +08004438void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004439{
Tejun Heo30159ec2013-06-25 11:53:37 -07004440 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004441 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004442 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004443 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004444
4445 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05004446 * Unlink from @tsk from its css_set. As migration path can't race
4447 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004448 */
4449 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004450 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004451 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004452 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004453 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07004454 }
4455
Paul Menageb4f48b62007-10-18 23:39:33 -07004456 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07004457 cset = task_css_set(tsk);
4458 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004459
Li Zefan1ec41832014-03-28 15:22:19 +08004460 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004461 /* see cgroup_post_fork() for details */
4462 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004463 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004464 struct cgroup_subsys_state *old_css = cset->subsys[i];
4465 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004466
Tejun Heoeb954192013-08-08 20:11:23 -04004467 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004468 }
4469 }
4470 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004471
Tejun Heoeaf797a2014-02-25 10:04:03 -05004472 if (put_cset)
4473 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07004474}
Paul Menage697f4162007-10-18 23:39:34 -07004475
Paul Menagebd89aab2007-10-18 23:40:44 -07004476static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004477{
Li Zefanf50daa72013-03-01 15:06:07 +08004478 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004479 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004480 /*
4481 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004482 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004483 * it now
4484 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004485 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004486
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004487 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004488 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004489 list_empty(&cgrp->release_list)) {
4490 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004491 need_schedule_work = 1;
4492 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004493 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004494 if (need_schedule_work)
4495 schedule_work(&release_agent_work);
4496 }
4497}
4498
Paul Menage81a6a5c2007-10-18 23:39:38 -07004499/*
4500 * Notify userspace when a cgroup is released, by running the
4501 * configured release agent with the name of the cgroup (path
4502 * relative to the root of cgroup file system) as the argument.
4503 *
4504 * Most likely, this user command will try to rmdir this cgroup.
4505 *
4506 * This races with the possibility that some other task will be
4507 * attached to this cgroup before it is removed, or that some other
4508 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4509 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4510 * unused, and this cgroup will be reprieved from its death sentence,
4511 * to continue to serve a useful existence. Next time it's released,
4512 * we will get notified again, if it still has 'notify_on_release' set.
4513 *
4514 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4515 * means only wait until the task is successfully execve()'d. The
4516 * separate release agent task is forked by call_usermodehelper(),
4517 * then control in this thread returns here, without waiting for the
4518 * release agent task. We don't bother to wait because the caller of
4519 * this routine has no use for the exit status of the release agent
4520 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004521 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004522static void cgroup_release_agent(struct work_struct *work)
4523{
4524 BUG_ON(work != &release_agent_work);
4525 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004526 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004527 while (!list_empty(&release_list)) {
4528 char *argv[3], *envp[3];
4529 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05004530 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07004531 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004532 struct cgroup,
4533 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004534 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004535 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05004536 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004537 if (!pathbuf)
4538 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05004539 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
4540 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07004541 goto continue_free;
4542 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4543 if (!agentbuf)
4544 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004545
4546 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004547 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05004548 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004549 argv[i] = NULL;
4550
4551 i = 0;
4552 /* minimal command environment */
4553 envp[i++] = "HOME=/";
4554 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4555 envp[i] = NULL;
4556
4557 /* Drop the lock while we invoke the usermode helper,
4558 * since the exec could involve hitting disk and hence
4559 * be a slow process */
4560 mutex_unlock(&cgroup_mutex);
4561 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004562 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004563 continue_free:
4564 kfree(pathbuf);
4565 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004566 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004567 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004568 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004569 mutex_unlock(&cgroup_mutex);
4570}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004571
4572static int __init cgroup_disable(char *str)
4573{
Tejun Heo30159ec2013-06-25 11:53:37 -07004574 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004575 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07004576 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004577
4578 while ((token = strsep(&str, ",")) != NULL) {
4579 if (!*token)
4580 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004581
Tejun Heo3ed80a62014-02-08 10:36:58 -05004582 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004583 if (!strcmp(token, ss->name)) {
4584 ss->disabled = 1;
4585 printk(KERN_INFO "Disabling %s control group"
4586 " subsystem\n", ss->name);
4587 break;
4588 }
4589 }
4590 }
4591 return 1;
4592}
4593__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004594
Tejun Heob77d7b62013-08-13 11:01:54 -04004595/**
Tejun Heo5a17f542014-02-11 11:52:47 -05004596 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04004597 * @dentry: directory dentry of interest
4598 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04004599 *
Tejun Heo5a17f542014-02-11 11:52:47 -05004600 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4601 * to get the corresponding css and return it. If such css doesn't exist
4602 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02004603 */
Tejun Heo5a17f542014-02-11 11:52:47 -05004604struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4605 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004606{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004607 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4608 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004609 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004610
Tejun Heo35cf0832013-08-26 18:40:56 -04004611 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004612 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4613 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004614 return ERR_PTR(-EBADF);
4615
Tejun Heo5a17f542014-02-11 11:52:47 -05004616 rcu_read_lock();
4617
Tejun Heo2bd59d42014-02-11 11:52:49 -05004618 /*
4619 * This path doesn't originate from kernfs and @kn could already
4620 * have been or be removed at any point. @kn->priv is RCU
4621 * protected for this access. See destroy_locked() for details.
4622 */
4623 cgrp = rcu_dereference(kn->priv);
4624 if (cgrp)
4625 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05004626
4627 if (!css || !css_tryget(css))
4628 css = ERR_PTR(-ENOENT);
4629
4630 rcu_read_unlock();
4631 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004632}
Stephane Eraniane5d13672011-02-14 11:20:01 +02004633
Li Zefan1cb650b2013-08-19 10:05:24 +08004634/**
4635 * css_from_id - lookup css by id
4636 * @id: the cgroup id
4637 * @ss: cgroup subsys to be looked into
4638 *
4639 * Returns the css if there's valid one with @id, otherwise returns NULL.
4640 * Should be called under rcu_read_lock().
4641 */
4642struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4643{
4644 struct cgroup *cgrp;
4645
Tejun Heoace2bee2014-02-11 11:52:47 -05004646 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08004647
4648 cgrp = idr_find(&ss->root->cgroup_idr, id);
4649 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04004650 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08004651 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004652}
4653
Paul Menagefe693432009-09-23 15:56:20 -07004654#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04004655static struct cgroup_subsys_state *
4656debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07004657{
4658 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4659
4660 if (!css)
4661 return ERR_PTR(-ENOMEM);
4662
4663 return css;
4664}
4665
Tejun Heoeb954192013-08-08 20:11:23 -04004666static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07004667{
Tejun Heoeb954192013-08-08 20:11:23 -04004668 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07004669}
4670
Tejun Heo182446d2013-08-08 20:11:24 -04004671static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
4672 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004673{
Tejun Heo182446d2013-08-08 20:11:24 -04004674 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07004675}
4676
Tejun Heo182446d2013-08-08 20:11:24 -04004677static u64 current_css_set_read(struct cgroup_subsys_state *css,
4678 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004679{
4680 return (u64)(unsigned long)current->cgroups;
4681}
4682
Tejun Heo182446d2013-08-08 20:11:24 -04004683static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08004684 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004685{
4686 u64 count;
4687
4688 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07004689 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07004690 rcu_read_unlock();
4691 return count;
4692}
4693
Tejun Heo2da8ca82013-12-05 12:28:04 -05004694static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004695{
Tejun Heo69d02062013-06-12 21:04:50 -07004696 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07004697 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05004698 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07004699
Tejun Heoe61734c2014-02-12 09:29:50 -05004700 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
4701 if (!name_buf)
4702 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07004703
Tejun Heo96d365e2014-02-13 06:58:40 -05004704 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07004705 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07004706 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07004707 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004708 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07004709
Tejun Heoa2dd4242014-03-19 10:23:55 -04004710 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004711 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04004712 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004713 }
4714 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05004715 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05004716 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004717 return 0;
4718}
4719
4720#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05004721static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004722{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004723 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07004724 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07004725
Tejun Heo96d365e2014-02-13 06:58:40 -05004726 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04004727 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004728 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004729 struct task_struct *task;
4730 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05004731
Tejun Heo5abb8852013-06-12 21:04:49 -07004732 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05004733
Tejun Heo5abb8852013-06-12 21:04:49 -07004734 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05004735 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
4736 goto overflow;
4737 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07004738 }
Tejun Heoc7561122014-02-25 10:04:01 -05004739
4740 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
4741 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
4742 goto overflow;
4743 seq_printf(seq, " task %d\n", task_pid_vnr(task));
4744 }
4745 continue;
4746 overflow:
4747 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07004748 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004749 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07004750 return 0;
4751}
4752
Tejun Heo182446d2013-08-08 20:11:24 -04004753static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004754{
Tejun Heo182446d2013-08-08 20:11:24 -04004755 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07004756}
4757
4758static struct cftype debug_files[] = {
4759 {
Paul Menagefe693432009-09-23 15:56:20 -07004760 .name = "taskcount",
4761 .read_u64 = debug_taskcount_read,
4762 },
4763
4764 {
4765 .name = "current_css_set",
4766 .read_u64 = current_css_set_read,
4767 },
4768
4769 {
4770 .name = "current_css_set_refcount",
4771 .read_u64 = current_css_set_refcount_read,
4772 },
4773
4774 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004775 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004776 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004777 },
4778
4779 {
4780 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004781 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004782 },
4783
4784 {
Paul Menagefe693432009-09-23 15:56:20 -07004785 .name = "releasable",
4786 .read_u64 = releasable_read,
4787 },
Paul Menagefe693432009-09-23 15:56:20 -07004788
Tejun Heo4baf6e32012-04-01 12:09:55 -07004789 { } /* terminate */
4790};
Paul Menagefe693432009-09-23 15:56:20 -07004791
Tejun Heo073219e2014-02-08 10:36:58 -05004792struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08004793 .css_alloc = debug_css_alloc,
4794 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07004795 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07004796};
4797#endif /* CONFIG_CGROUP_DEBUG */