blob: f944619077f4a5545e7a5b08f89d6b1b84e1467c [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 Heof392e512014-04-23 11:13:14 -0400532 if (root->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 Heof392e512014-04-23 11:13:14 -0400745 rebind_subsystems(&cgrp_dfl_root, root->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 Heof392e512014-04-23 11:13:14 -04001053 src_root->subsys_mask &= ~(1 << ssid);
1054 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1055
1056 dst_root->subsys_mask |= 1 << ssid;
1057 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001058
Tejun Heo5df36032014-03-19 10:23:54 -04001059 if (ss->bind)
1060 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001062
Tejun Heoa2dd4242014-03-19 10:23:55 -04001063 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001064 return 0;
1065}
1066
Tejun Heo2bd59d42014-02-11 11:52:49 -05001067static int cgroup_show_options(struct seq_file *seq,
1068 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001069{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001070 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001072 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073
Tejun Heob85d2042013-12-06 15:11:57 -05001074 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001075 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001076 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001077 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1078 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001079 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001081 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001082 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001083
1084 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001085 if (strlen(root->release_agent_path))
1086 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001087 spin_unlock(&release_agent_path_lock);
1088
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001089 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001090 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001091 if (strlen(root->name))
1092 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093 return 0;
1094}
1095
1096struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001097 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001098 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001099 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001100 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001101 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001102 /* User explicitly requested empty subsystem */
1103 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104};
1105
Ben Blumaae8aab2010-03-10 15:22:07 -08001106/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001107 * Convert a hierarchy specifier into a bitmask of subsystems and
1108 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1109 * array. This function takes refcounts on subsystems to be used, unless it
1110 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001111 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001112static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001113{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001114 char *token, *o = data;
1115 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001116 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001117 struct cgroup_subsys *ss;
1118 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001119
Ben Blumaae8aab2010-03-10 15:22:07 -08001120 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1121
Li Zefanf9ab5b52009-06-17 16:26:33 -07001122#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001123 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001124#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125
Paul Menagec6d57f32009-09-23 15:56:19 -07001126 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127
1128 while ((token = strsep(&o, ",")) != NULL) {
1129 if (!*token)
1130 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001131 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001132 /* Explicitly have no subsystems */
1133 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001134 continue;
1135 }
1136 if (!strcmp(token, "all")) {
1137 /* Mutually exclusive option 'all' + subsystem name */
1138 if (one_ss)
1139 return -EINVAL;
1140 all_ss = true;
1141 continue;
1142 }
Tejun Heo873fe092013-04-14 20:15:26 -07001143 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1144 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1145 continue;
1146 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001147 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001148 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001149 continue;
1150 }
1151 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001152 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001153 continue;
1154 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001155 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001156 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001157 continue;
1158 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001159 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001160 /* Specifying two release agents is forbidden */
1161 if (opts->release_agent)
1162 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001163 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001164 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001165 if (!opts->release_agent)
1166 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001167 continue;
1168 }
1169 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001170 const char *name = token + 5;
1171 /* Can't specify an empty name */
1172 if (!strlen(name))
1173 return -EINVAL;
1174 /* Must match [\w.-]+ */
1175 for (i = 0; i < strlen(name); i++) {
1176 char c = name[i];
1177 if (isalnum(c))
1178 continue;
1179 if ((c == '.') || (c == '-') || (c == '_'))
1180 continue;
1181 return -EINVAL;
1182 }
1183 /* Specifying two names is forbidden */
1184 if (opts->name)
1185 return -EINVAL;
1186 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001187 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001188 GFP_KERNEL);
1189 if (!opts->name)
1190 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001191
1192 continue;
1193 }
1194
Tejun Heo30159ec2013-06-25 11:53:37 -07001195 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001196 if (strcmp(token, ss->name))
1197 continue;
1198 if (ss->disabled)
1199 continue;
1200
1201 /* Mutually exclusive option 'all' + subsystem name */
1202 if (all_ss)
1203 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001204 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001205 one_ss = true;
1206
1207 break;
1208 }
1209 if (i == CGROUP_SUBSYS_COUNT)
1210 return -ENOENT;
1211 }
1212
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001213 /* Consistency checks */
1214
Tejun Heo873fe092013-04-14 20:15:26 -07001215 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1216 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1217
Tejun Heod3ba07c2014-02-13 06:58:38 -05001218 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1219 opts->cpuset_clone_children || opts->release_agent ||
1220 opts->name) {
1221 pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001222 return -EINVAL;
1223 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001224 } else {
1225 /*
1226 * If the 'all' option was specified select all the
1227 * subsystems, otherwise if 'none', 'name=' and a subsystem
1228 * name options were not specified, let's default to 'all'
1229 */
1230 if (all_ss || (!one_ss && !opts->none && !opts->name))
1231 for_each_subsys(ss, i)
1232 if (!ss->disabled)
1233 set_bit(i, &opts->subsys_mask);
Tejun Heo873fe092013-04-14 20:15:26 -07001234
Tejun Heoa2dd4242014-03-19 10:23:55 -04001235 /*
1236 * We either have to specify by name or by subsystems. (So
1237 * all empty hierarchies must have a name).
1238 */
1239 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001240 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001241 }
1242
Li Zefanf9ab5b52009-06-17 16:26:33 -07001243 /*
1244 * Option noprefix was introduced just for backward compatibility
1245 * with the old cpuset, so we allow noprefix only if mounting just
1246 * the cpuset subsystem.
1247 */
Tejun Heo93438622013-04-14 20:15:25 -07001248 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001249 return -EINVAL;
1250
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001251
1252 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001253 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001254 return -EINVAL;
1255
Paul Menageddbcc7e2007-10-18 23:39:30 -07001256 return 0;
1257}
1258
Tejun Heo2bd59d42014-02-11 11:52:49 -05001259static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001260{
1261 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001262 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001263 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001264 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001265
Tejun Heo873fe092013-04-14 20:15:26 -07001266 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1267 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1268 return -EINVAL;
1269 }
1270
Tejun Heoace2bee2014-02-11 11:52:47 -05001271 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001272 mutex_lock(&cgroup_mutex);
1273
1274 /* See what subsystems are wanted */
1275 ret = parse_cgroupfs_options(data, &opts);
1276 if (ret)
1277 goto out_unlock;
1278
Tejun Heof392e512014-04-23 11:13:14 -04001279 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001280 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1281 task_tgid_nr(current), current->comm);
1282
Tejun Heof392e512014-04-23 11:13:14 -04001283 added_mask = opts.subsys_mask & ~root->subsys_mask;
1284 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001285
Ben Blumcf5d5942010-03-10 15:22:09 -08001286 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001287 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001288 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001289 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1290 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1291 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001292 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001293 goto out_unlock;
1294 }
1295
Tejun Heof172e672013-06-28 17:07:30 -07001296 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001297 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001298 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001299 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001300 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001301
Tejun Heo5df36032014-03-19 10:23:54 -04001302 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001303 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001305
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001306 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001307
Tejun Heo69e943b2014-02-08 10:36:58 -05001308 if (opts.release_agent) {
1309 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001310 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001311 spin_unlock(&release_agent_path_lock);
1312 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001314 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001315 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001316 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001317 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001318 return ret;
1319}
1320
Tejun Heoafeb0f92014-02-13 06:58:39 -05001321/*
1322 * To reduce the fork() overhead for systems that are not actually using
1323 * their cgroups capability, we don't maintain the lists running through
1324 * each css_set to its tasks until we see the list actually used - in other
1325 * words after the first mount.
1326 */
1327static bool use_task_css_set_links __read_mostly;
1328
1329static void cgroup_enable_task_cg_lists(void)
1330{
1331 struct task_struct *p, *g;
1332
Tejun Heo96d365e2014-02-13 06:58:40 -05001333 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001334
1335 if (use_task_css_set_links)
1336 goto out_unlock;
1337
1338 use_task_css_set_links = true;
1339
1340 /*
1341 * We need tasklist_lock because RCU is not safe against
1342 * while_each_thread(). Besides, a forking task that has passed
1343 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1344 * is not guaranteed to have its child immediately visible in the
1345 * tasklist if we walk through it with RCU.
1346 */
1347 read_lock(&tasklist_lock);
1348 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001349 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1350 task_css_set(p) != &init_css_set);
1351
1352 /*
1353 * We should check if the process is exiting, otherwise
1354 * it will race with cgroup_exit() in that the list
1355 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001356 * Do it while holding siglock so that we don't end up
1357 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001358 */
Tejun Heof153ad12014-02-25 09:56:49 -05001359 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001360 if (!(p->flags & PF_EXITING)) {
1361 struct css_set *cset = task_css_set(p);
1362
1363 list_add(&p->cg_list, &cset->tasks);
1364 get_css_set(cset);
1365 }
Tejun Heof153ad12014-02-25 09:56:49 -05001366 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001367 } while_each_thread(g, p);
1368 read_unlock(&tasklist_lock);
1369out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001370 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001371}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001372
Paul Menagecc31edc2008-10-18 20:28:04 -07001373static void init_cgroup_housekeeping(struct cgroup *cgrp)
1374{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001375 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001376 INIT_LIST_HEAD(&cgrp->sibling);
1377 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001378 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001379 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001380 INIT_LIST_HEAD(&cgrp->pidlists);
1381 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001382 cgrp->dummy_css.cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001383}
Paul Menagec6d57f32009-09-23 15:56:19 -07001384
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001385static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001386 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001387{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001388 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001389
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001391 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001392 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001393 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001394 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001395
Paul Menagec6d57f32009-09-23 15:56:19 -07001396 root->flags = opts->flags;
1397 if (opts->release_agent)
1398 strcpy(root->release_agent_path, opts->release_agent);
1399 if (opts->name)
1400 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001401 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001402 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001403}
1404
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001405static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001406{
Tejun Heod427dfe2014-02-11 11:52:48 -05001407 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001408 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001409 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001410 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001411
Tejun Heod427dfe2014-02-11 11:52:48 -05001412 lockdep_assert_held(&cgroup_tree_mutex);
1413 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001414
Tejun Heod427dfe2014-02-11 11:52:48 -05001415 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1416 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001417 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001418 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001419
Tejun Heod427dfe2014-02-11 11:52:48 -05001420 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001421 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001422 * but that's OK - it can only be increased by someone holding
1423 * cgroup_lock, and that's us. The worst that can happen is that we
1424 * have some link structures left over
1425 */
1426 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001427 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001428 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429
Tejun Heo985ed672014-03-19 10:23:53 -04001430 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001431 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001432 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001433
Tejun Heo2bd59d42014-02-11 11:52:49 -05001434 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1435 KERNFS_ROOT_CREATE_DEACTIVATED,
1436 root_cgrp);
1437 if (IS_ERR(root->kf_root)) {
1438 ret = PTR_ERR(root->kf_root);
1439 goto exit_root_id;
1440 }
1441 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442
Tejun Heod427dfe2014-02-11 11:52:48 -05001443 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1444 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001445 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001446
Tejun Heo5df36032014-03-19 10:23:54 -04001447 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001448 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001449 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001450
Tejun Heod427dfe2014-02-11 11:52:48 -05001451 /*
1452 * There must be no failure case after here, since rebinding takes
1453 * care of subsystems' refcounts, which are explicitly dropped in
1454 * the failure exit path.
1455 */
1456 list_add(&root->root_list, &cgroup_roots);
1457 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001458
Tejun Heod427dfe2014-02-11 11:52:48 -05001459 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001460 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001461 * objects.
1462 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001463 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001464 hash_for_each(css_set_table, i, cset, hlist)
1465 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001466 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001467
Tejun Heod427dfe2014-02-11 11:52:48 -05001468 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001469 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001470
Tejun Heo2bd59d42014-02-11 11:52:49 -05001471 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001472 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001473 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001474
Tejun Heo2bd59d42014-02-11 11:52:49 -05001475destroy_root:
1476 kernfs_destroy_root(root->kf_root);
1477 root->kf_root = NULL;
1478exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001479 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001480out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001481 free_cgrp_cset_links(&tmp_links);
1482 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001483}
1484
Al Virof7e83572010-07-26 13:23:11 +04001485static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001486 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001487 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001488{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001489 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001490 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001491 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001492 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001493 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001494
1495 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001496 * The first time anyone tries to mount a cgroup, enable the list
1497 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001498 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001499 if (!use_task_css_set_links)
1500 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001501
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001502 mutex_lock(&cgroup_tree_mutex);
1503 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001504
Paul Menageddbcc7e2007-10-18 23:39:30 -07001505 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001506 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001507 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001508 goto out_unlock;
Li Zefane37a06f2014-04-17 13:53:08 +08001509retry:
Tejun Heo2bd59d42014-02-11 11:52:49 -05001510 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001511 if (!opts.subsys_mask && !opts.none && !opts.name) {
1512 cgrp_dfl_root_visible = true;
1513 root = &cgrp_dfl_root;
1514 cgroup_get(&root->cgrp);
1515 ret = 0;
1516 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001517 }
1518
Tejun Heo985ed672014-03-19 10:23:53 -04001519 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001520 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001522 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001523 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001524
Paul Menage817929e2007-10-18 23:39:36 -07001525 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001526 * If we asked for a name then it must match. Also, if
1527 * name matches but sybsys_mask doesn't, we should fail.
1528 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001529 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001530 if (opts.name) {
1531 if (strcmp(opts.name, root->name))
1532 continue;
1533 name_match = true;
1534 }
Tejun Heo31261212013-06-28 17:07:30 -07001535
1536 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001537 * If we asked for subsystems (or explicitly for no
1538 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001539 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001540 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001541 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001542 if (!name_match)
1543 continue;
1544 ret = -EBUSY;
1545 goto out_unlock;
1546 }
Tejun Heo873fe092013-04-14 20:15:26 -07001547
Tejun Heoc7ba8282013-06-29 14:06:10 -07001548 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001549 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1550 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1551 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001552 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001553 } else {
1554 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1555 }
Tejun Heo873fe092013-04-14 20:15:26 -07001556 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001557
Tejun Heo776f02f2014-02-12 09:29:50 -05001558 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001559 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001560 * ref indicate that the root is being destroyed. Wait for
1561 * destruction to complete so that the subsystems are free.
1562 * We can use wait_queue for the wait but this path is
1563 * super cold. Let's just sleep for a bit and retry.
1564 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001565 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001566 mutex_unlock(&cgroup_mutex);
1567 mutex_unlock(&cgroup_tree_mutex);
1568 msleep(10);
Li Zefane37a06f2014-04-17 13:53:08 +08001569 mutex_lock(&cgroup_tree_mutex);
1570 mutex_lock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001571 goto retry;
1572 }
1573
1574 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001575 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576 }
1577
Tejun Heo172a2c062014-03-19 10:23:53 -04001578 /*
1579 * No such thing, create a new one. name= matching without subsys
1580 * specification is allowed for already existing hierarchies but we
1581 * can't create new one without subsys specification.
1582 */
1583 if (!opts.subsys_mask && !opts.none) {
1584 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001585 goto out_unlock;
1586 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001587
Tejun Heo172a2c062014-03-19 10:23:53 -04001588 root = kzalloc(sizeof(*root), GFP_KERNEL);
1589 if (!root) {
1590 ret = -ENOMEM;
1591 goto out_unlock;
1592 }
1593
1594 init_cgroup_root(root, &opts);
1595
Tejun Heo35585572014-02-13 06:58:38 -05001596 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001597 if (ret)
1598 cgroup_free_root(root);
1599
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001600out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001601 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001602 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001603
Paul Menagec6d57f32009-09-23 15:56:19 -07001604 kfree(opts.release_agent);
1605 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001606
Tejun Heo2bd59d42014-02-11 11:52:49 -05001607 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001608 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001609
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001610 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1611 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001612 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001613 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614}
1615
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001616static void cgroup_kill_sb(struct super_block *sb)
1617{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001618 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001619 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001620
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001621 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001622 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623}
1624
1625static struct file_system_type cgroup_fs_type = {
1626 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001627 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628 .kill_sb = cgroup_kill_sb,
1629};
1630
Greg KH676db4a2010-08-05 13:53:35 -07001631static struct kobject *cgroup_kobj;
1632
Li Zefana043e3b2008-02-23 15:24:09 -08001633/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001634 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001635 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001636 * @buf: the buffer to write the path into
1637 * @buflen: the length of the buffer
1638 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001639 * Determine @task's cgroup on the first (the one with the lowest non-zero
1640 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1641 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1642 * cgroup controller callbacks.
1643 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001644 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001645 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001646char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001647{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001648 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001649 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001650 int hierarchy_id = 1;
1651 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001652
1653 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001654 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001655
Tejun Heo913ffdb2013-07-11 16:34:48 -07001656 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1657
Tejun Heo857a2be2013-04-14 20:50:08 -07001658 if (root) {
1659 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001660 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001661 } else {
1662 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001663 if (strlcpy(buf, "/", buflen) < buflen)
1664 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001665 }
1666
Tejun Heo96d365e2014-02-13 06:58:40 -05001667 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001668 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001669 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001670}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001671EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001672
Tejun Heob3dc0942014-02-25 10:04:01 -05001673/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001674struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001675 /* the src and dst cset list running through cset->mg_node */
1676 struct list_head src_csets;
1677 struct list_head dst_csets;
1678
1679 /*
1680 * Fields for cgroup_taskset_*() iteration.
1681 *
1682 * Before migration is committed, the target migration tasks are on
1683 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1684 * the csets on ->dst_csets. ->csets point to either ->src_csets
1685 * or ->dst_csets depending on whether migration is committed.
1686 *
1687 * ->cur_csets and ->cur_task point to the current task position
1688 * during iteration.
1689 */
1690 struct list_head *csets;
1691 struct css_set *cur_cset;
1692 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001693};
1694
1695/**
1696 * cgroup_taskset_first - reset taskset and return the first task
1697 * @tset: taskset of interest
1698 *
1699 * @tset iteration is initialized and the first task is returned.
1700 */
1701struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1702{
Tejun Heob3dc0942014-02-25 10:04:01 -05001703 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1704 tset->cur_task = NULL;
1705
1706 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001707}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001708
1709/**
1710 * cgroup_taskset_next - iterate to the next task in taskset
1711 * @tset: taskset of interest
1712 *
1713 * Return the next task in @tset. Iteration must have been initialized
1714 * with cgroup_taskset_first().
1715 */
1716struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1717{
Tejun Heob3dc0942014-02-25 10:04:01 -05001718 struct css_set *cset = tset->cur_cset;
1719 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001720
Tejun Heob3dc0942014-02-25 10:04:01 -05001721 while (&cset->mg_node != tset->csets) {
1722 if (!task)
1723 task = list_first_entry(&cset->mg_tasks,
1724 struct task_struct, cg_list);
1725 else
1726 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001727
Tejun Heob3dc0942014-02-25 10:04:01 -05001728 if (&task->cg_list != &cset->mg_tasks) {
1729 tset->cur_cset = cset;
1730 tset->cur_task = task;
1731 return task;
1732 }
1733
1734 cset = list_next_entry(cset, mg_node);
1735 task = NULL;
1736 }
1737
1738 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001739}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001740
1741/**
Ben Blum74a11662011-05-26 16:25:20 -07001742 * cgroup_task_migrate - move a task from one cgroup to another.
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001743 * @old_cgrp; the cgroup @tsk is being migrated from
1744 * @tsk: the task being migrated
1745 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001746 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001747 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001748 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001749static void cgroup_task_migrate(struct cgroup *old_cgrp,
1750 struct task_struct *tsk,
1751 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001752{
Tejun Heo5abb8852013-06-12 21:04:49 -07001753 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001754
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001755 lockdep_assert_held(&cgroup_mutex);
1756 lockdep_assert_held(&css_set_rwsem);
1757
Ben Blum74a11662011-05-26 16:25:20 -07001758 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001759 * We are synchronized through threadgroup_lock() against PF_EXITING
1760 * setting such that we can't race against cgroup_exit() changing the
1761 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001762 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001763 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001764 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001765
Tejun Heob3dc0942014-02-25 10:04:01 -05001766 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001767 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001768
Tejun Heo1b9aba42014-03-19 17:43:21 -04001769 /*
1770 * Use move_tail so that cgroup_taskset_first() still returns the
1771 * leader after migration. This works because cgroup_migrate()
1772 * ensures that the dst_cset of the leader is the first on the
1773 * tset's dst_csets list.
1774 */
1775 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001776
1777 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001778 * We just gained a reference on old_cset by taking it from the
1779 * task. As trading it for new_cset is protected by cgroup_mutex,
1780 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001781 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001782 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001783 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001784}
1785
Li Zefana043e3b2008-02-23 15:24:09 -08001786/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001787 * cgroup_migrate_finish - cleanup after attach
1788 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001789 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001790 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1791 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001792 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001793static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001794{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001795 struct css_set *cset, *tmp_cset;
1796
1797 lockdep_assert_held(&cgroup_mutex);
1798
1799 down_write(&css_set_rwsem);
1800 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1801 cset->mg_src_cgrp = NULL;
1802 cset->mg_dst_cset = NULL;
1803 list_del_init(&cset->mg_preload_node);
1804 put_css_set_locked(cset, false);
1805 }
1806 up_write(&css_set_rwsem);
1807}
1808
1809/**
1810 * cgroup_migrate_add_src - add a migration source css_set
1811 * @src_cset: the source css_set to add
1812 * @dst_cgrp: the destination cgroup
1813 * @preloaded_csets: list of preloaded css_sets
1814 *
1815 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1816 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1817 * up by cgroup_migrate_finish().
1818 *
1819 * This function may be called without holding threadgroup_lock even if the
1820 * target is a process. Threads may be created and destroyed but as long
1821 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1822 * the preloaded css_sets are guaranteed to cover all migrations.
1823 */
1824static void cgroup_migrate_add_src(struct css_set *src_cset,
1825 struct cgroup *dst_cgrp,
1826 struct list_head *preloaded_csets)
1827{
1828 struct cgroup *src_cgrp;
1829
1830 lockdep_assert_held(&cgroup_mutex);
1831 lockdep_assert_held(&css_set_rwsem);
1832
1833 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1834
1835 /* nothing to do if this cset already belongs to the cgroup */
1836 if (src_cgrp == dst_cgrp)
1837 return;
1838
1839 if (!list_empty(&src_cset->mg_preload_node))
1840 return;
1841
1842 WARN_ON(src_cset->mg_src_cgrp);
1843 WARN_ON(!list_empty(&src_cset->mg_tasks));
1844 WARN_ON(!list_empty(&src_cset->mg_node));
1845
1846 src_cset->mg_src_cgrp = src_cgrp;
1847 get_css_set(src_cset);
1848 list_add(&src_cset->mg_preload_node, preloaded_csets);
1849}
1850
1851/**
1852 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
1853 * @dst_cgrp: the destination cgroup
1854 * @preloaded_csets: list of preloaded source css_sets
1855 *
1856 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
1857 * have been preloaded to @preloaded_csets. This function looks up and
1858 * pins all destination css_sets, links each to its source, and put them on
1859 * @preloaded_csets.
1860 *
1861 * This function must be called after cgroup_migrate_add_src() has been
1862 * called on each migration source css_set. After migration is performed
1863 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
1864 * @preloaded_csets.
1865 */
1866static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
1867 struct list_head *preloaded_csets)
1868{
1869 LIST_HEAD(csets);
1870 struct css_set *src_cset;
1871
1872 lockdep_assert_held(&cgroup_mutex);
1873
1874 /* look up the dst cset for each src cset and link it to src */
1875 list_for_each_entry(src_cset, preloaded_csets, mg_preload_node) {
1876 struct css_set *dst_cset;
1877
1878 dst_cset = find_css_set(src_cset, dst_cgrp);
1879 if (!dst_cset)
1880 goto err;
1881
1882 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
1883 src_cset->mg_dst_cset = dst_cset;
1884
1885 if (list_empty(&dst_cset->mg_preload_node))
1886 list_add(&dst_cset->mg_preload_node, &csets);
1887 else
1888 put_css_set(dst_cset, false);
1889 }
1890
1891 list_splice(&csets, preloaded_csets);
1892 return 0;
1893err:
1894 cgroup_migrate_finish(&csets);
1895 return -ENOMEM;
1896}
1897
1898/**
1899 * cgroup_migrate - migrate a process or task to a cgroup
1900 * @cgrp: the destination cgroup
1901 * @leader: the leader of the process or the task to migrate
1902 * @threadgroup: whether @leader points to the whole process or a single task
1903 *
1904 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
1905 * process, the caller must be holding threadgroup_lock of @leader. The
1906 * caller is also responsible for invoking cgroup_migrate_add_src() and
1907 * cgroup_migrate_prepare_dst() on the targets before invoking this
1908 * function and following up with cgroup_migrate_finish().
1909 *
1910 * As long as a controller's ->can_attach() doesn't fail, this function is
1911 * guaranteed to succeed. This means that, excluding ->can_attach()
1912 * failure, when migrating multiple targets, the success or failure can be
1913 * decided for all targets by invoking group_migrate_prepare_dst() before
1914 * actually starting migrating.
1915 */
1916static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
1917 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001918{
Tejun Heob3dc0942014-02-25 10:04:01 -05001919 struct cgroup_taskset tset = {
1920 .src_csets = LIST_HEAD_INIT(tset.src_csets),
1921 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
1922 .csets = &tset.src_csets,
1923 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05001924 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05001925 struct css_set *cset, *tmp_cset;
1926 struct task_struct *task, *tmp_task;
1927 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07001928
1929 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001930 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1931 * already PF_EXITING could be freed from underneath us unless we
1932 * take an rcu_read_lock.
1933 */
Tejun Heob3dc0942014-02-25 10:04:01 -05001934 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001935 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05001936 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07001937 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05001938 /* @task either already exited or can't exit until the end */
1939 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001940 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001941
Tejun Heoeaf797a2014-02-25 10:04:03 -05001942 /* leave @task alone if post_fork() hasn't linked it yet */
1943 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08001944 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05001945
Tejun Heob3dc0942014-02-25 10:04:01 -05001946 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001947 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001948 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05001949
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001950 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04001951 * cgroup_taskset_first() must always return the leader.
1952 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001953 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04001954 list_move_tail(&task->cg_list, &cset->mg_tasks);
1955 if (list_empty(&cset->mg_node))
1956 list_add_tail(&cset->mg_node, &tset.src_csets);
1957 if (list_empty(&cset->mg_dst_cset->mg_node))
1958 list_move_tail(&cset->mg_dst_cset->mg_node,
1959 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08001960 next:
Li Zefan081aa452013-03-13 09:17:09 +08001961 if (!threadgroup)
1962 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05001963 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001964 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05001965 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07001966
Tejun Heo134d3372011-12-12 18:12:21 -08001967 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05001968 if (list_empty(&tset.src_csets))
1969 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08001970
Tejun Heo1958d2d2014-02-25 10:04:03 -05001971 /* check that we can legitimately attach to the cgroup */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001972 for_each_css(css, i, cgrp) {
1973 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05001974 ret = css->ss->can_attach(css, &tset);
1975 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001976 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07001977 goto out_cancel_attach;
1978 }
1979 }
Ben Blum74a11662011-05-26 16:25:20 -07001980 }
1981
1982 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05001983 * Now that we're guaranteed success, proceed to move all tasks to
1984 * the new cgroup. There are no failure cases after here, so this
1985 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07001986 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001987 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05001988 list_for_each_entry(cset, &tset.src_csets, mg_node) {
1989 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
1990 cgroup_task_migrate(cset->mg_src_cgrp, task,
1991 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001992 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001993 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07001994
1995 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05001996 * Migration is committed, all target tasks are now on dst_csets.
1997 * Nothing is sensitive to fork() after this point. Notify
1998 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07001999 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002000 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002001
Tejun Heo1c6727a2013-12-06 15:11:56 -05002002 for_each_css(css, i, cgrp)
2003 if (css->ss->attach)
2004 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002005
Tejun Heo9db8de32014-02-13 06:58:43 -05002006 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002007 goto out_release_tset;
2008
Ben Blum74a11662011-05-26 16:25:20 -07002009out_cancel_attach:
Tejun Heob3dc0942014-02-25 10:04:01 -05002010 for_each_css(css, i, cgrp) {
2011 if (css == failed_css)
2012 break;
2013 if (css->ss->cancel_attach)
2014 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002015 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002016out_release_tset:
2017 down_write(&css_set_rwsem);
2018 list_splice_init(&tset.dst_csets, &tset.src_csets);
2019 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002020 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002021 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002022 }
2023 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002024 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002025}
2026
Tejun Heo1958d2d2014-02-25 10:04:03 -05002027/**
2028 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2029 * @dst_cgrp: the cgroup to attach to
2030 * @leader: the task or the leader of the threadgroup to be attached
2031 * @threadgroup: attach the whole threadgroup?
2032 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002033 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002034 */
2035static int cgroup_attach_task(struct cgroup *dst_cgrp,
2036 struct task_struct *leader, bool threadgroup)
2037{
2038 LIST_HEAD(preloaded_csets);
2039 struct task_struct *task;
2040 int ret;
2041
2042 /* look up all src csets */
2043 down_read(&css_set_rwsem);
2044 rcu_read_lock();
2045 task = leader;
2046 do {
2047 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2048 &preloaded_csets);
2049 if (!threadgroup)
2050 break;
2051 } while_each_thread(leader, task);
2052 rcu_read_unlock();
2053 up_read(&css_set_rwsem);
2054
2055 /* prepare dst csets and commit */
2056 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2057 if (!ret)
2058 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2059
2060 cgroup_migrate_finish(&preloaded_csets);
2061 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002062}
2063
2064/*
2065 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002066 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002067 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002068 */
2069static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002070{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002071 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002072 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002073 int ret;
2074
Ben Blum74a11662011-05-26 16:25:20 -07002075 if (!cgroup_lock_live_group(cgrp))
2076 return -ENODEV;
2077
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002078retry_find_task:
2079 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002080 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002081 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002082 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002083 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002084 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002085 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002086 }
Ben Blum74a11662011-05-26 16:25:20 -07002087 /*
2088 * even if we're attaching all tasks in the thread group, we
2089 * only need to check permissions on one of them.
2090 */
David Howellsc69e8d92008-11-14 10:39:19 +11002091 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002092 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2093 !uid_eq(cred->euid, tcred->uid) &&
2094 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002095 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002096 ret = -EACCES;
2097 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002098 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002099 } else
2100 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002101
2102 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002103 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002104
2105 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002106 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002107 * trapped in a cpuset, or RT worker may be born in a cgroup
2108 * with no rt_runtime allocated. Just say no.
2109 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002110 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002111 ret = -EINVAL;
2112 rcu_read_unlock();
2113 goto out_unlock_cgroup;
2114 }
2115
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002116 get_task_struct(tsk);
2117 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002118
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002119 threadgroup_lock(tsk);
2120 if (threadgroup) {
2121 if (!thread_group_leader(tsk)) {
2122 /*
2123 * a race with de_thread from another thread's exec()
2124 * may strip us of our leadership, if this happens,
2125 * there is no choice but to throw this task away and
2126 * try again; this is
2127 * "double-double-toil-and-trouble-check locking".
2128 */
2129 threadgroup_unlock(tsk);
2130 put_task_struct(tsk);
2131 goto retry_find_task;
2132 }
Li Zefan081aa452013-03-13 09:17:09 +08002133 }
2134
2135 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2136
Tejun Heocd3d0952011-12-12 18:12:21 -08002137 threadgroup_unlock(tsk);
2138
Paul Menagebbcb81d2007-10-18 23:39:32 -07002139 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002140out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002141 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002142 return ret;
2143}
2144
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002145/**
2146 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2147 * @from: attach to all cgroups of a given task
2148 * @tsk: the task to be attached
2149 */
2150int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2151{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002152 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002153 int retval = 0;
2154
Tejun Heo47cfcd02013-04-07 09:29:51 -07002155 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002156 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002157 struct cgroup *from_cgrp;
2158
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002159 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002160 continue;
2161
Tejun Heo96d365e2014-02-13 06:58:40 -05002162 down_read(&css_set_rwsem);
2163 from_cgrp = task_cgroup_from_root(from, root);
2164 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002165
Li Zefan6f4b7e62013-07-31 16:18:36 +08002166 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002167 if (retval)
2168 break;
2169 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002170 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002171
2172 return retval;
2173}
2174EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2175
Tejun Heo182446d2013-08-08 20:11:24 -04002176static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2177 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002178{
Tejun Heo182446d2013-08-08 20:11:24 -04002179 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002180}
2181
Tejun Heo182446d2013-08-08 20:11:24 -04002182static int cgroup_procs_write(struct cgroup_subsys_state *css,
2183 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002184{
Tejun Heo182446d2013-08-08 20:11:24 -04002185 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002186}
2187
Tejun Heo182446d2013-08-08 20:11:24 -04002188static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
Tejun Heo4d3bb512014-03-19 10:23:54 -04002189 struct cftype *cft, char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002190{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002191 struct cgroup_root *root = css->cgroup->root;
Tejun Heo5f469902014-02-11 11:52:48 -05002192
2193 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002194 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002195 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002196 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002197 strlcpy(root->release_agent_path, buffer,
2198 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002199 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002200 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002201 return 0;
2202}
2203
Tejun Heo2da8ca82013-12-05 12:28:04 -05002204static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002205{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002206 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002207
Paul Menagee788e062008-07-25 01:46:59 -07002208 if (!cgroup_lock_live_group(cgrp))
2209 return -ENODEV;
2210 seq_puts(seq, cgrp->root->release_agent_path);
2211 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002212 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002213 return 0;
2214}
2215
Tejun Heo2da8ca82013-12-05 12:28:04 -05002216static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002217{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002218 struct cgroup *cgrp = seq_css(seq)->cgroup;
2219
2220 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002221 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002222}
2223
Tejun Heo2bd59d42014-02-11 11:52:49 -05002224static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2225 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002226{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002227 struct cgroup *cgrp = of->kn->parent->priv;
2228 struct cftype *cft = of->kn->priv;
2229 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002230 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002231
Tejun Heo2bd59d42014-02-11 11:52:49 -05002232 /*
2233 * kernfs guarantees that a file isn't deleted with operations in
2234 * flight, which means that the matching css is and stays alive and
2235 * doesn't need to be pinned. The RCU locking is not necessary
2236 * either. It's just for the convenience of using cgroup_css().
2237 */
2238 rcu_read_lock();
2239 css = cgroup_css(cgrp, cft->ss);
2240 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002241
Tejun Heoa742c592013-12-05 12:28:03 -05002242 if (cft->write_string) {
2243 ret = cft->write_string(css, cft, strstrip(buf));
2244 } else if (cft->write_u64) {
2245 unsigned long long v;
2246 ret = kstrtoull(buf, 0, &v);
2247 if (!ret)
2248 ret = cft->write_u64(css, cft, v);
2249 } else if (cft->write_s64) {
2250 long long v;
2251 ret = kstrtoll(buf, 0, &v);
2252 if (!ret)
2253 ret = cft->write_s64(css, cft, v);
2254 } else if (cft->trigger) {
2255 ret = cft->trigger(css, (unsigned int)cft->private);
2256 } else {
2257 ret = -EINVAL;
2258 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002259
Tejun Heoa742c592013-12-05 12:28:03 -05002260 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002261}
2262
Tejun Heo6612f052013-12-05 12:28:04 -05002263static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002264{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002265 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002266}
2267
2268static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2269{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002270 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002271}
2272
2273static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2274{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002275 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002276}
2277
2278static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2279{
Tejun Heo7da11272013-12-05 12:28:04 -05002280 struct cftype *cft = seq_cft(m);
2281 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002282
Tejun Heo2da8ca82013-12-05 12:28:04 -05002283 if (cft->seq_show)
2284 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002285
Tejun Heo896f5192013-12-05 12:28:04 -05002286 if (cft->read_u64)
2287 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2288 else if (cft->read_s64)
2289 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2290 else
2291 return -EINVAL;
2292 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002293}
2294
Tejun Heo2bd59d42014-02-11 11:52:49 -05002295static struct kernfs_ops cgroup_kf_single_ops = {
2296 .atomic_write_len = PAGE_SIZE,
2297 .write = cgroup_file_write,
2298 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002299};
2300
Tejun Heo2bd59d42014-02-11 11:52:49 -05002301static struct kernfs_ops cgroup_kf_ops = {
2302 .atomic_write_len = PAGE_SIZE,
2303 .write = cgroup_file_write,
2304 .seq_start = cgroup_seqfile_start,
2305 .seq_next = cgroup_seqfile_next,
2306 .seq_stop = cgroup_seqfile_stop,
2307 .seq_show = cgroup_seqfile_show,
2308};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002309
2310/*
2311 * cgroup_rename - Only allow simple rename of directories in place.
2312 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002313static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2314 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002315{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002316 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002317 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002318
Tejun Heo2bd59d42014-02-11 11:52:49 -05002319 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002320 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002321 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002322 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002323
Tejun Heo6db8e852013-06-14 11:18:22 -07002324 /*
2325 * This isn't a proper migration and its usefulness is very
2326 * limited. Disallow if sane_behavior.
2327 */
2328 if (cgroup_sane_behavior(cgrp))
2329 return -EPERM;
2330
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002331 /*
2332 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2333 * active_ref. kernfs_rename() doesn't require active_ref
2334 * protection. Break them before grabbing cgroup_tree_mutex.
2335 */
2336 kernfs_break_active_protection(new_parent);
2337 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002338
Tejun Heo2bd59d42014-02-11 11:52:49 -05002339 mutex_lock(&cgroup_tree_mutex);
2340 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002341
Tejun Heo2bd59d42014-02-11 11:52:49 -05002342 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002343
Tejun Heo2bd59d42014-02-11 11:52:49 -05002344 mutex_unlock(&cgroup_mutex);
2345 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002346
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002347 kernfs_unbreak_active_protection(kn);
2348 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002349 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002350}
2351
Tejun Heo49957f82014-04-07 16:44:47 -04002352/* set uid and gid of cgroup dirs and files to that of the creator */
2353static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2354{
2355 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2356 .ia_uid = current_fsuid(),
2357 .ia_gid = current_fsgid(), };
2358
2359 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2360 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2361 return 0;
2362
2363 return kernfs_setattr(kn, &iattr);
2364}
2365
Tejun Heo2bb566c2013-08-08 20:11:23 -04002366static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002367{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002368 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002369 struct kernfs_node *kn;
2370 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002371 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002372
Tejun Heo2bd59d42014-02-11 11:52:49 -05002373#ifdef CONFIG_DEBUG_LOCK_ALLOC
2374 key = &cft->lockdep_key;
2375#endif
2376 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2377 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2378 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002379 if (IS_ERR(kn))
2380 return PTR_ERR(kn);
2381
2382 ret = cgroup_kn_set_ugid(kn);
2383 if (ret)
2384 kernfs_remove(kn);
2385 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002386}
2387
Tejun Heob1f28d32013-06-28 16:24:10 -07002388/**
2389 * cgroup_addrm_files - add or remove files to a cgroup directory
2390 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002391 * @cfts: array of cftypes to be added
2392 * @is_add: whether to add or remove
2393 *
2394 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002395 * For removals, this function never fails. If addition fails, this
2396 * function doesn't remove files already added. The caller is responsible
2397 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002398 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002399static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2400 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002401{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002402 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002403 int ret;
2404
Tejun Heoace2bee2014-02-11 11:52:47 -05002405 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002406
2407 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002408 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002409 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2410 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002411 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2412 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002413 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2414 continue;
2415 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2416 continue;
2417
Li Zefan2739d3c2013-01-21 18:18:33 +08002418 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002419 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002420 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002421 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002422 cft->name, ret);
2423 return ret;
2424 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002425 } else {
2426 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002427 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002428 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002429 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002430}
2431
Tejun Heo21a2d342014-02-12 09:29:49 -05002432static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002433{
2434 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002435 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002436 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002437 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002438 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002439
Tejun Heo21a2d342014-02-12 09:29:49 -05002440 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002441
Li Zefane8c82d22013-06-18 18:48:37 +08002442 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002443 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002444 struct cgroup *cgrp = css->cgroup;
2445
Li Zefane8c82d22013-06-18 18:48:37 +08002446 if (cgroup_is_dead(cgrp))
2447 continue;
2448
Tejun Heo21a2d342014-02-12 09:29:49 -05002449 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002450 if (ret)
2451 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002452 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002453
2454 if (is_add && !ret)
2455 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002456 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002457}
2458
Tejun Heo2da440a2014-02-11 11:52:48 -05002459static void cgroup_exit_cftypes(struct cftype *cfts)
2460{
2461 struct cftype *cft;
2462
Tejun Heo2bd59d42014-02-11 11:52:49 -05002463 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2464 /* free copy for custom atomic_write_len, see init_cftypes() */
2465 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2466 kfree(cft->kf_ops);
2467 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002468 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002469 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002470}
2471
Tejun Heo2bd59d42014-02-11 11:52:49 -05002472static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002473{
2474 struct cftype *cft;
2475
Tejun Heo2bd59d42014-02-11 11:52:49 -05002476 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2477 struct kernfs_ops *kf_ops;
2478
Tejun Heo0adb0702014-02-12 09:29:48 -05002479 WARN_ON(cft->ss || cft->kf_ops);
2480
Tejun Heo2bd59d42014-02-11 11:52:49 -05002481 if (cft->seq_start)
2482 kf_ops = &cgroup_kf_ops;
2483 else
2484 kf_ops = &cgroup_kf_single_ops;
2485
2486 /*
2487 * Ugh... if @cft wants a custom max_write_len, we need to
2488 * make a copy of kf_ops to set its atomic_write_len.
2489 */
2490 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2491 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2492 if (!kf_ops) {
2493 cgroup_exit_cftypes(cfts);
2494 return -ENOMEM;
2495 }
2496 kf_ops->atomic_write_len = cft->max_write_len;
2497 }
2498
2499 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002500 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002501 }
2502
2503 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002504}
2505
Tejun Heo21a2d342014-02-12 09:29:49 -05002506static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2507{
2508 lockdep_assert_held(&cgroup_tree_mutex);
2509
2510 if (!cfts || !cfts[0].ss)
2511 return -ENOENT;
2512
2513 list_del(&cfts->node);
2514 cgroup_apply_cftypes(cfts, false);
2515 cgroup_exit_cftypes(cfts);
2516 return 0;
2517}
2518
Tejun Heo8e3f6542012-04-01 12:09:55 -07002519/**
Tejun Heo80b13582014-02-12 09:29:48 -05002520 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2521 * @cfts: zero-length name terminated array of cftypes
2522 *
2523 * Unregister @cfts. Files described by @cfts are removed from all
2524 * existing cgroups and all future cgroups won't have them either. This
2525 * function can be called anytime whether @cfts' subsys is attached or not.
2526 *
2527 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2528 * registered.
2529 */
2530int cgroup_rm_cftypes(struct cftype *cfts)
2531{
Tejun Heo21a2d342014-02-12 09:29:49 -05002532 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002533
Tejun Heo21a2d342014-02-12 09:29:49 -05002534 mutex_lock(&cgroup_tree_mutex);
2535 ret = cgroup_rm_cftypes_locked(cfts);
2536 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002537 return ret;
2538}
2539
2540/**
2541 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2542 * @ss: target cgroup subsystem
2543 * @cfts: zero-length name terminated array of cftypes
2544 *
2545 * Register @cfts to @ss. Files described by @cfts are created for all
2546 * existing cgroups to which @ss is attached and all future cgroups will
2547 * have them too. This function can be called anytime whether @ss is
2548 * attached or not.
2549 *
2550 * Returns 0 on successful registration, -errno on failure. Note that this
2551 * function currently returns 0 as long as @cfts registration is successful
2552 * even if some file creation attempts on existing cgroups fail.
2553 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002554int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002555{
Tejun Heo9ccece82013-06-28 16:24:11 -07002556 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002557
Li Zefandc5736e2014-02-17 10:41:50 +08002558 if (!cfts || cfts[0].name[0] == '\0')
2559 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002560
Tejun Heo2bd59d42014-02-11 11:52:49 -05002561 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002562 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05002563 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002564
Tejun Heo21a2d342014-02-12 09:29:49 -05002565 mutex_lock(&cgroup_tree_mutex);
2566
Tejun Heo0adb0702014-02-12 09:29:48 -05002567 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05002568 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002569 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05002570 cgroup_rm_cftypes_locked(cfts);
2571
2572 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002573 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002574}
Tejun Heo79578622012-04-01 12:09:56 -07002575
2576/**
Li Zefana043e3b2008-02-23 15:24:09 -08002577 * cgroup_task_count - count the number of tasks in a cgroup.
2578 * @cgrp: the cgroup in question
2579 *
2580 * Return the number of tasks in the cgroup.
2581 */
Tejun Heo07bc3562014-02-13 06:58:39 -05002582static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002583{
2584 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002585 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002586
Tejun Heo96d365e2014-02-13 06:58:40 -05002587 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07002588 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2589 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05002590 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002591 return count;
2592}
2593
Tejun Heo574bd9f2012-11-09 09:12:29 -08002594/**
Tejun Heo492eb212013-08-08 20:11:25 -04002595 * css_next_child - find the next child of a given css
2596 * @pos_css: the current position (%NULL to initiate traversal)
2597 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002598 *
Tejun Heo492eb212013-08-08 20:11:25 -04002599 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002600 * under either cgroup_mutex or RCU read lock. The only requirement is
2601 * that @parent_css and @pos_css are accessible. The next sibling is
2602 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002603 */
Tejun Heo492eb212013-08-08 20:11:25 -04002604struct cgroup_subsys_state *
2605css_next_child(struct cgroup_subsys_state *pos_css,
2606 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002607{
Tejun Heo492eb212013-08-08 20:11:25 -04002608 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2609 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002610 struct cgroup *next;
2611
Tejun Heoace2bee2014-02-11 11:52:47 -05002612 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002613
2614 /*
2615 * @pos could already have been removed. Once a cgroup is removed,
2616 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002617 * changes. As CGRP_DEAD assertion is serialized and happens
2618 * before the cgroup is taken off the ->sibling list, if we see it
2619 * unasserted, it's guaranteed that the next sibling hasn't
2620 * finished its grace period even if it's already removed, and thus
2621 * safe to dereference from this RCU critical section. If
2622 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2623 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002624 *
2625 * If @pos is dead, its next pointer can't be dereferenced;
2626 * however, as each cgroup is given a monotonically increasing
2627 * unique serial number and always appended to the sibling list,
2628 * the next one can be found by walking the parent's children until
2629 * we see a cgroup with higher serial number than @pos's. While
2630 * this path can be slower, it's taken only when either the current
2631 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002632 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002633 if (!pos) {
2634 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2635 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002636 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002637 } else {
2638 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2639 if (next->serial_nr > pos->serial_nr)
2640 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002641 }
2642
Tejun Heo492eb212013-08-08 20:11:25 -04002643 if (&next->sibling == &cgrp->children)
2644 return NULL;
2645
Tejun Heoca8bdca2013-08-26 18:40:56 -04002646 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002647}
Tejun Heo53fa5262013-05-24 10:55:38 +09002648
2649/**
Tejun Heo492eb212013-08-08 20:11:25 -04002650 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002651 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002652 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002653 *
Tejun Heo492eb212013-08-08 20:11:25 -04002654 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002655 * to visit for pre-order traversal of @root's descendants. @root is
2656 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002657 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002658 * While this function requires cgroup_mutex or RCU read locking, it
2659 * doesn't require the whole traversal to be contained in a single critical
2660 * section. This function will return the correct next descendant as long
2661 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002662 */
Tejun Heo492eb212013-08-08 20:11:25 -04002663struct cgroup_subsys_state *
2664css_next_descendant_pre(struct cgroup_subsys_state *pos,
2665 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002666{
Tejun Heo492eb212013-08-08 20:11:25 -04002667 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002668
Tejun Heoace2bee2014-02-11 11:52:47 -05002669 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002670
Tejun Heobd8815a2013-08-08 20:11:27 -04002671 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002672 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002673 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002674
2675 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002676 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002677 if (next)
2678 return next;
2679
2680 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002681 while (pos != root) {
2682 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002683 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002684 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002685 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002686 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002687
2688 return NULL;
2689}
Tejun Heo574bd9f2012-11-09 09:12:29 -08002690
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002691/**
Tejun Heo492eb212013-08-08 20:11:25 -04002692 * css_rightmost_descendant - return the rightmost descendant of a css
2693 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002694 *
Tejun Heo492eb212013-08-08 20:11:25 -04002695 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2696 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002697 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002698 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002699 * While this function requires cgroup_mutex or RCU read locking, it
2700 * doesn't require the whole traversal to be contained in a single critical
2701 * section. This function will return the correct rightmost descendant as
2702 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002703 */
Tejun Heo492eb212013-08-08 20:11:25 -04002704struct cgroup_subsys_state *
2705css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002706{
Tejun Heo492eb212013-08-08 20:11:25 -04002707 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002708
Tejun Heoace2bee2014-02-11 11:52:47 -05002709 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002710
2711 do {
2712 last = pos;
2713 /* ->prev isn't RCU safe, walk ->next till the end */
2714 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002715 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002716 pos = tmp;
2717 } while (pos);
2718
2719 return last;
2720}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002721
Tejun Heo492eb212013-08-08 20:11:25 -04002722static struct cgroup_subsys_state *
2723css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002724{
Tejun Heo492eb212013-08-08 20:11:25 -04002725 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002726
2727 do {
2728 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04002729 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002730 } while (pos);
2731
2732 return last;
2733}
2734
2735/**
Tejun Heo492eb212013-08-08 20:11:25 -04002736 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002737 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002738 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002739 *
Tejun Heo492eb212013-08-08 20:11:25 -04002740 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002741 * to visit for post-order traversal of @root's descendants. @root is
2742 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002743 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002744 * While this function requires cgroup_mutex or RCU read locking, it
2745 * doesn't require the whole traversal to be contained in a single critical
2746 * section. This function will return the correct next descendant as long
2747 * as both @pos and @cgroup are accessible and @pos is a descendant of
2748 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002749 */
Tejun Heo492eb212013-08-08 20:11:25 -04002750struct cgroup_subsys_state *
2751css_next_descendant_post(struct cgroup_subsys_state *pos,
2752 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002753{
Tejun Heo492eb212013-08-08 20:11:25 -04002754 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002755
Tejun Heoace2bee2014-02-11 11:52:47 -05002756 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002757
Tejun Heo58b79a92013-09-06 15:31:08 -04002758 /* if first iteration, visit leftmost descendant which may be @root */
2759 if (!pos)
2760 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002761
Tejun Heobd8815a2013-08-08 20:11:27 -04002762 /* if we visited @root, we're done */
2763 if (pos == root)
2764 return NULL;
2765
Tejun Heo574bd9f2012-11-09 09:12:29 -08002766 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04002767 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002768 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04002769 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002770
2771 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04002772 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002773}
Tejun Heo574bd9f2012-11-09 09:12:29 -08002774
Tejun Heo0942eee2013-08-08 20:11:26 -04002775/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002776 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04002777 * @it: the iterator to advance
2778 *
2779 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04002780 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002781static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04002782{
2783 struct list_head *l = it->cset_link;
2784 struct cgrp_cset_link *link;
2785 struct css_set *cset;
2786
2787 /* Advance to the next non-empty css_set */
2788 do {
2789 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04002790 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04002791 it->cset_link = NULL;
2792 return;
2793 }
2794 link = list_entry(l, struct cgrp_cset_link, cset_link);
2795 cset = link->cset;
Tejun Heoc7561122014-02-25 10:04:01 -05002796 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
2797
Tejun Heod5158762013-08-08 20:11:26 -04002798 it->cset_link = l;
Tejun Heoc7561122014-02-25 10:04:01 -05002799
2800 if (!list_empty(&cset->tasks))
2801 it->task = cset->tasks.next;
2802 else
2803 it->task = cset->mg_tasks.next;
Tejun Heod5158762013-08-08 20:11:26 -04002804}
2805
Tejun Heo0942eee2013-08-08 20:11:26 -04002806/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002807 * css_task_iter_start - initiate task iteration
2808 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04002809 * @it: the task iterator to use
2810 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002811 * Initiate iteration through the tasks of @css. The caller can call
2812 * css_task_iter_next() to walk through the tasks until the function
2813 * returns NULL. On completion of iteration, css_task_iter_end() must be
2814 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04002815 *
2816 * Note that this function acquires a lock which is released when the
2817 * iteration finishes. The caller can't sleep while iteration is in
2818 * progress.
2819 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002820void css_task_iter_start(struct cgroup_subsys_state *css,
2821 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05002822 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07002823{
Tejun Heo56fde9e2014-02-13 06:58:38 -05002824 /* no one should try to iterate before mounting cgroups */
2825 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002826
Tejun Heo96d365e2014-02-13 06:58:40 -05002827 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002828
Tejun Heo72ec7022013-08-08 20:11:26 -04002829 it->origin_css = css;
2830 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002831
Tejun Heo72ec7022013-08-08 20:11:26 -04002832 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002833}
2834
Tejun Heo0942eee2013-08-08 20:11:26 -04002835/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002836 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04002837 * @it: the task iterator being iterated
2838 *
2839 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04002840 * initialized via css_task_iter_start(). Returns NULL when the iteration
2841 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04002842 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002843struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002844{
2845 struct task_struct *res;
2846 struct list_head *l = it->task;
Tejun Heoc7561122014-02-25 10:04:01 -05002847 struct cgrp_cset_link *link = list_entry(it->cset_link,
2848 struct cgrp_cset_link, cset_link);
Paul Menage817929e2007-10-18 23:39:36 -07002849
2850 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07002851 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07002852 return NULL;
2853 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05002854
2855 /*
2856 * Advance iterator to find next entry. cset->tasks is consumed
2857 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
2858 * next cset.
2859 */
Paul Menage817929e2007-10-18 23:39:36 -07002860 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05002861
2862 if (l == &link->cset->tasks)
2863 l = link->cset->mg_tasks.next;
2864
2865 if (l == &link->cset->mg_tasks)
Tejun Heo72ec7022013-08-08 20:11:26 -04002866 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05002867 else
Paul Menage817929e2007-10-18 23:39:36 -07002868 it->task = l;
Tejun Heoc7561122014-02-25 10:04:01 -05002869
Paul Menage817929e2007-10-18 23:39:36 -07002870 return res;
2871}
2872
Tejun Heo0942eee2013-08-08 20:11:26 -04002873/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002874 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04002875 * @it: the task iterator to finish
2876 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002877 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04002878 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002879void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05002880 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07002881{
Tejun Heo96d365e2014-02-13 06:58:40 -05002882 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07002883}
2884
2885/**
2886 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
2887 * @to: cgroup to which the tasks will be moved
2888 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05002889 *
2890 * Locking rules between cgroup_post_fork() and the migration path
2891 * guarantee that, if a task is forking while being migrated, the new child
2892 * is guaranteed to be either visible in the source cgroup after the
2893 * parent's migration is complete or put into the target cgroup. No task
2894 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07002895 */
2896int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
2897{
Tejun Heo952aaa12014-02-25 10:04:03 -05002898 LIST_HEAD(preloaded_csets);
2899 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05002900 struct css_task_iter it;
2901 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05002902 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05002903
Tejun Heo952aaa12014-02-25 10:04:03 -05002904 mutex_lock(&cgroup_mutex);
2905
2906 /* all tasks in @from are being moved, all csets are source */
2907 down_read(&css_set_rwsem);
2908 list_for_each_entry(link, &from->cset_links, cset_link)
2909 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
2910 up_read(&css_set_rwsem);
2911
2912 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
2913 if (ret)
2914 goto out_err;
2915
2916 /*
2917 * Migrate tasks one-by-one until @form is empty. This fails iff
2918 * ->can_attach() fails.
2919 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05002920 do {
2921 css_task_iter_start(&from->dummy_css, &it);
2922 task = css_task_iter_next(&it);
2923 if (task)
2924 get_task_struct(task);
2925 css_task_iter_end(&it);
2926
2927 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05002928 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05002929 put_task_struct(task);
2930 }
2931 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05002932out_err:
2933 cgroup_migrate_finish(&preloaded_csets);
2934 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05002935 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07002936}
2937
Paul Menage817929e2007-10-18 23:39:36 -07002938/*
Ben Blum102a7752009-09-23 15:56:26 -07002939 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002940 *
2941 * Reading this file can return large amounts of data if a cgroup has
2942 * *lots* of attached tasks. So it may need several calls to read(),
2943 * but we cannot guarantee that the information we produce is correct
2944 * unless we produce it entirely atomically.
2945 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002946 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002947
Li Zefan24528252012-01-20 11:58:43 +08002948/* which pidlist file are we talking about? */
2949enum cgroup_filetype {
2950 CGROUP_FILE_PROCS,
2951 CGROUP_FILE_TASKS,
2952};
2953
2954/*
2955 * A pidlist is a list of pids that virtually represents the contents of one
2956 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
2957 * a pair (one each for procs, tasks) for each pid namespace that's relevant
2958 * to the cgroup.
2959 */
2960struct cgroup_pidlist {
2961 /*
2962 * used to find which pidlist is wanted. doesn't change as long as
2963 * this particular list stays in the list.
2964 */
2965 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
2966 /* array of xids */
2967 pid_t *list;
2968 /* how many elements the above list has */
2969 int length;
Li Zefan24528252012-01-20 11:58:43 +08002970 /* each of these stored in a list by its cgroup */
2971 struct list_head links;
2972 /* pointer to the cgroup we belong to, for list removal purposes */
2973 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05002974 /* for delayed destruction */
2975 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08002976};
2977
Paul Menagebbcb81d2007-10-18 23:39:32 -07002978/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002979 * The following two functions "fix" the issue where there are more pids
2980 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2981 * TODO: replace with a kernel-wide solution to this problem
2982 */
2983#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2984static void *pidlist_allocate(int count)
2985{
2986 if (PIDLIST_TOO_LARGE(count))
2987 return vmalloc(count * sizeof(pid_t));
2988 else
2989 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2990}
Tejun Heob1a21362013-11-29 10:42:58 -05002991
Ben Blumd1d9fd32009-09-23 15:56:28 -07002992static void pidlist_free(void *p)
2993{
2994 if (is_vmalloc_addr(p))
2995 vfree(p);
2996 else
2997 kfree(p);
2998}
Ben Blumd1d9fd32009-09-23 15:56:28 -07002999
3000/*
Tejun Heob1a21362013-11-29 10:42:58 -05003001 * Used to destroy all pidlists lingering waiting for destroy timer. None
3002 * should be left afterwards.
3003 */
3004static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3005{
3006 struct cgroup_pidlist *l, *tmp_l;
3007
3008 mutex_lock(&cgrp->pidlist_mutex);
3009 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3010 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3011 mutex_unlock(&cgrp->pidlist_mutex);
3012
3013 flush_workqueue(cgroup_pidlist_destroy_wq);
3014 BUG_ON(!list_empty(&cgrp->pidlists));
3015}
3016
3017static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3018{
3019 struct delayed_work *dwork = to_delayed_work(work);
3020 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3021 destroy_dwork);
3022 struct cgroup_pidlist *tofree = NULL;
3023
3024 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003025
3026 /*
Tejun Heo04502362013-11-29 10:42:59 -05003027 * Destroy iff we didn't get queued again. The state won't change
3028 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003029 */
Tejun Heo04502362013-11-29 10:42:59 -05003030 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003031 list_del(&l->links);
3032 pidlist_free(l->list);
3033 put_pid_ns(l->key.ns);
3034 tofree = l;
3035 }
3036
Tejun Heob1a21362013-11-29 10:42:58 -05003037 mutex_unlock(&l->owner->pidlist_mutex);
3038 kfree(tofree);
3039}
3040
3041/*
Ben Blum102a7752009-09-23 15:56:26 -07003042 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003043 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003044 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003045static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003046{
Ben Blum102a7752009-09-23 15:56:26 -07003047 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003048
3049 /*
3050 * we presume the 0th element is unique, so i starts at 1. trivial
3051 * edge cases first; no work needs to be done for either
3052 */
3053 if (length == 0 || length == 1)
3054 return length;
3055 /* src and dest walk down the list; dest counts unique elements */
3056 for (src = 1; src < length; src++) {
3057 /* find next unique element */
3058 while (list[src] == list[src-1]) {
3059 src++;
3060 if (src == length)
3061 goto after;
3062 }
3063 /* dest always points to where the next unique element goes */
3064 list[dest] = list[src];
3065 dest++;
3066 }
3067after:
Ben Blum102a7752009-09-23 15:56:26 -07003068 return dest;
3069}
3070
Tejun Heoafb2bc12013-11-29 10:42:59 -05003071/*
3072 * The two pid files - task and cgroup.procs - guaranteed that the result
3073 * is sorted, which forced this whole pidlist fiasco. As pid order is
3074 * different per namespace, each namespace needs differently sorted list,
3075 * making it impossible to use, for example, single rbtree of member tasks
3076 * sorted by task pointer. As pidlists can be fairly large, allocating one
3077 * per open file is dangerous, so cgroup had to implement shared pool of
3078 * pidlists keyed by cgroup and namespace.
3079 *
3080 * All this extra complexity was caused by the original implementation
3081 * committing to an entirely unnecessary property. In the long term, we
3082 * want to do away with it. Explicitly scramble sort order if
3083 * sane_behavior so that no such expectation exists in the new interface.
3084 *
3085 * Scrambling is done by swapping every two consecutive bits, which is
3086 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3087 */
3088static pid_t pid_fry(pid_t pid)
3089{
3090 unsigned a = pid & 0x55555555;
3091 unsigned b = pid & 0xAAAAAAAA;
3092
3093 return (a << 1) | (b >> 1);
3094}
3095
3096static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3097{
3098 if (cgroup_sane_behavior(cgrp))
3099 return pid_fry(pid);
3100 else
3101 return pid;
3102}
3103
Ben Blum102a7752009-09-23 15:56:26 -07003104static int cmppid(const void *a, const void *b)
3105{
3106 return *(pid_t *)a - *(pid_t *)b;
3107}
3108
Tejun Heoafb2bc12013-11-29 10:42:59 -05003109static int fried_cmppid(const void *a, const void *b)
3110{
3111 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3112}
3113
Ben Blum72a8cb32009-09-23 15:56:27 -07003114static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3115 enum cgroup_filetype type)
3116{
3117 struct cgroup_pidlist *l;
3118 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003119 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003120
Tejun Heoe6b81712013-11-29 10:42:59 -05003121 lockdep_assert_held(&cgrp->pidlist_mutex);
3122
3123 list_for_each_entry(l, &cgrp->pidlists, links)
3124 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003125 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003126 return NULL;
3127}
3128
Ben Blum72a8cb32009-09-23 15:56:27 -07003129/*
3130 * find the appropriate pidlist for our purpose (given procs vs tasks)
3131 * returns with the lock on that pidlist already held, and takes care
3132 * of the use count, or returns NULL with no locks held if we're out of
3133 * memory.
3134 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003135static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3136 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003137{
3138 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003139
Tejun Heoe6b81712013-11-29 10:42:59 -05003140 lockdep_assert_held(&cgrp->pidlist_mutex);
3141
3142 l = cgroup_pidlist_find(cgrp, type);
3143 if (l)
3144 return l;
3145
Ben Blum72a8cb32009-09-23 15:56:27 -07003146 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003147 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003148 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003149 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003150
Tejun Heob1a21362013-11-29 10:42:58 -05003151 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003152 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003153 /* don't need task_nsproxy() if we're looking at ourself */
3154 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003155 l->owner = cgrp;
3156 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003157 return l;
3158}
3159
3160/*
Ben Blum102a7752009-09-23 15:56:26 -07003161 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3162 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003163static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3164 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003165{
3166 pid_t *array;
3167 int length;
3168 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003169 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003170 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003171 struct cgroup_pidlist *l;
3172
Tejun Heo4bac00d2013-11-29 10:42:59 -05003173 lockdep_assert_held(&cgrp->pidlist_mutex);
3174
Ben Blum102a7752009-09-23 15:56:26 -07003175 /*
3176 * If cgroup gets more users after we read count, we won't have
3177 * enough space - tough. This race is indistinguishable to the
3178 * caller from the case that the additional cgroup users didn't
3179 * show up until sometime later on.
3180 */
3181 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003182 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003183 if (!array)
3184 return -ENOMEM;
3185 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003186 css_task_iter_start(&cgrp->dummy_css, &it);
3187 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003188 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003189 break;
Ben Blum102a7752009-09-23 15:56:26 -07003190 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003191 if (type == CGROUP_FILE_PROCS)
3192 pid = task_tgid_vnr(tsk);
3193 else
3194 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003195 if (pid > 0) /* make sure to only use valid results */
3196 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003197 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003198 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003199 length = n;
3200 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003201 if (cgroup_sane_behavior(cgrp))
3202 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3203 else
3204 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003205 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003206 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003207
Tejun Heoe6b81712013-11-29 10:42:59 -05003208 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003209 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003210 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003211 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003212 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003213 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003214
3215 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003216 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003217 l->list = array;
3218 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003219 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003220 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003221}
3222
Balbir Singh846c7bb2007-10-18 23:39:44 -07003223/**
Li Zefana043e3b2008-02-23 15:24:09 -08003224 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003225 * @stats: cgroupstats to fill information into
3226 * @dentry: A dentry entry belonging to the cgroup for which stats have
3227 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003228 *
3229 * Build and fill cgroupstats so that taskstats can export it to user
3230 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003231 */
3232int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3233{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003234 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003235 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003236 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003237 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003238
Tejun Heo2bd59d42014-02-11 11:52:49 -05003239 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3240 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3241 kernfs_type(kn) != KERNFS_DIR)
3242 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003243
Li Zefanbad34662014-02-14 16:54:28 +08003244 mutex_lock(&cgroup_mutex);
3245
Tejun Heo2bd59d42014-02-11 11:52:49 -05003246 /*
3247 * We aren't being called from kernfs and there's no guarantee on
3248 * @kn->priv's validity. For this and css_tryget_from_dir(),
3249 * @kn->priv is RCU safe. Let's do the RCU dancing.
3250 */
3251 rcu_read_lock();
3252 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003253 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003254 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003255 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003256 return -ENOENT;
3257 }
Li Zefanbad34662014-02-14 16:54:28 +08003258 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003259
Tejun Heo72ec7022013-08-08 20:11:26 -04003260 css_task_iter_start(&cgrp->dummy_css, &it);
3261 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003262 switch (tsk->state) {
3263 case TASK_RUNNING:
3264 stats->nr_running++;
3265 break;
3266 case TASK_INTERRUPTIBLE:
3267 stats->nr_sleeping++;
3268 break;
3269 case TASK_UNINTERRUPTIBLE:
3270 stats->nr_uninterruptible++;
3271 break;
3272 case TASK_STOPPED:
3273 stats->nr_stopped++;
3274 break;
3275 default:
3276 if (delayacct_is_task_waiting_on_io(tsk))
3277 stats->nr_io_wait++;
3278 break;
3279 }
3280 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003281 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003282
Li Zefanbad34662014-02-14 16:54:28 +08003283 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003284 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003285}
3286
Paul Menage8f3ff202009-09-23 15:56:25 -07003287
Paul Menagecc31edc2008-10-18 20:28:04 -07003288/*
Ben Blum102a7752009-09-23 15:56:26 -07003289 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003290 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003291 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003292 */
3293
Ben Blum102a7752009-09-23 15:56:26 -07003294static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003295{
3296 /*
3297 * Initially we receive a position value that corresponds to
3298 * one more than the last pid shown (or 0 on the first call or
3299 * after a seek to the start). Use a binary-search to find the
3300 * next pid to display, if any
3301 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003302 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003303 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003304 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003305 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003306 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003307 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003308
Tejun Heo4bac00d2013-11-29 10:42:59 -05003309 mutex_lock(&cgrp->pidlist_mutex);
3310
3311 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003312 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003313 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003314 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003315 * could already have been destroyed.
3316 */
Tejun Heo5d224442013-12-05 12:28:04 -05003317 if (of->priv)
3318 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003319
3320 /*
3321 * Either this is the first start() after open or the matching
3322 * pidlist has been destroyed inbetween. Create a new one.
3323 */
Tejun Heo5d224442013-12-05 12:28:04 -05003324 if (!of->priv) {
3325 ret = pidlist_array_load(cgrp, type,
3326 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003327 if (ret)
3328 return ERR_PTR(ret);
3329 }
Tejun Heo5d224442013-12-05 12:28:04 -05003330 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003331
Paul Menagecc31edc2008-10-18 20:28:04 -07003332 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003333 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003334
Paul Menagecc31edc2008-10-18 20:28:04 -07003335 while (index < end) {
3336 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003337 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003338 index = mid;
3339 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003340 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003341 index = mid + 1;
3342 else
3343 end = mid;
3344 }
3345 }
3346 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003347 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003348 return NULL;
3349 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003350 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003351 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003352 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003353}
3354
Ben Blum102a7752009-09-23 15:56:26 -07003355static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003356{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003357 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003358 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003359
Tejun Heo5d224442013-12-05 12:28:04 -05003360 if (l)
3361 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003362 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003363 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003364}
3365
Ben Blum102a7752009-09-23 15:56:26 -07003366static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003367{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003368 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003369 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003370 pid_t *p = v;
3371 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003372 /*
3373 * Advance to the next pid in the array. If this goes off the
3374 * end, we're done
3375 */
3376 p++;
3377 if (p >= end) {
3378 return NULL;
3379 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003380 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003381 return p;
3382 }
3383}
3384
Ben Blum102a7752009-09-23 15:56:26 -07003385static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003386{
3387 return seq_printf(s, "%d\n", *(int *)v);
3388}
3389
Ben Blum102a7752009-09-23 15:56:26 -07003390/*
3391 * seq_operations functions for iterating on pidlists through seq_file -
3392 * independent of whether it's tasks or procs
3393 */
3394static const struct seq_operations cgroup_pidlist_seq_operations = {
3395 .start = cgroup_pidlist_start,
3396 .stop = cgroup_pidlist_stop,
3397 .next = cgroup_pidlist_next,
3398 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003399};
3400
Tejun Heo182446d2013-08-08 20:11:24 -04003401static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3402 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003403{
Tejun Heo182446d2013-08-08 20:11:24 -04003404 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003405}
3406
Tejun Heo182446d2013-08-08 20:11:24 -04003407static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3408 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003409{
Tejun Heo182446d2013-08-08 20:11:24 -04003410 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003411 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003412 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003413 else
Tejun Heo182446d2013-08-08 20:11:24 -04003414 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003415 return 0;
3416}
3417
Tejun Heo182446d2013-08-08 20:11:24 -04003418static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3419 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003420{
Tejun Heo182446d2013-08-08 20:11:24 -04003421 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003422}
3423
Tejun Heo182446d2013-08-08 20:11:24 -04003424static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3425 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003426{
3427 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003428 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003429 else
Tejun Heo182446d2013-08-08 20:11:24 -04003430 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003431 return 0;
3432}
3433
Tejun Heod5c56ce2013-06-03 19:14:34 -07003434static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003435 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003436 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003437 .seq_start = cgroup_pidlist_start,
3438 .seq_next = cgroup_pidlist_next,
3439 .seq_stop = cgroup_pidlist_stop,
3440 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003441 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003442 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003443 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003444 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003445 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003446 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003447 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003448 .read_u64 = cgroup_clone_children_read,
3449 .write_u64 = cgroup_clone_children_write,
3450 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003451 {
Tejun Heo873fe092013-04-14 20:15:26 -07003452 .name = "cgroup.sane_behavior",
3453 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003454 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003455 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003456
3457 /*
3458 * Historical crazy stuff. These don't have "cgroup." prefix and
3459 * don't exist if sane_behavior. If you're depending on these, be
3460 * prepared to be burned.
3461 */
3462 {
3463 .name = "tasks",
3464 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003465 .seq_start = cgroup_pidlist_start,
3466 .seq_next = cgroup_pidlist_next,
3467 .seq_stop = cgroup_pidlist_stop,
3468 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003469 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003470 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003471 .mode = S_IRUGO | S_IWUSR,
3472 },
3473 {
3474 .name = "notify_on_release",
3475 .flags = CFTYPE_INSANE,
3476 .read_u64 = cgroup_read_notify_on_release,
3477 .write_u64 = cgroup_write_notify_on_release,
3478 },
Tejun Heo873fe092013-04-14 20:15:26 -07003479 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003480 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003481 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003482 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003483 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003484 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003485 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003486 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003487};
3488
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003489/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003490 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003491 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003492 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003493 *
3494 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003495 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003496static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003497{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003498 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003499 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003500
Tejun Heo8e3f6542012-04-01 12:09:55 -07003501 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003502 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05003503 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07003504
3505 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003506 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003507
Tejun Heo0adb0702014-02-12 09:29:48 -05003508 list_for_each_entry(cfts, &ss->cfts, node) {
3509 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003510 if (ret < 0)
3511 goto err;
3512 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003513 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003514 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003515err:
3516 cgroup_clear_dir(cgrp, subsys_mask);
3517 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003518}
3519
Tejun Heo0c21ead2013-08-13 20:22:51 -04003520/*
3521 * css destruction is four-stage process.
3522 *
3523 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3524 * Implemented in kill_css().
3525 *
3526 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3527 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3528 * by invoking offline_css(). After offlining, the base ref is put.
3529 * Implemented in css_killed_work_fn().
3530 *
3531 * 3. When the percpu_ref reaches zero, the only possible remaining
3532 * accessors are inside RCU read sections. css_release() schedules the
3533 * RCU callback.
3534 *
3535 * 4. After the grace period, the css can be freed. Implemented in
3536 * css_free_work_fn().
3537 *
3538 * It is actually hairier because both step 2 and 4 require process context
3539 * and thus involve punting to css->destroy_work adding two additional
3540 * steps to the already complex sequence.
3541 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003542static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003543{
3544 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003545 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003546 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003547
Tejun Heo0ae78e02013-08-13 11:01:54 -04003548 if (css->parent)
3549 css_put(css->parent);
3550
Tejun Heo0c21ead2013-08-13 20:22:51 -04003551 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003552 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003553}
3554
3555static void css_free_rcu_fn(struct rcu_head *rcu_head)
3556{
3557 struct cgroup_subsys_state *css =
3558 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3559
Tejun Heo0c21ead2013-08-13 20:22:51 -04003560 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003561 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003562}
3563
Tejun Heod3daf282013-06-13 19:39:16 -07003564static void css_release(struct percpu_ref *ref)
3565{
3566 struct cgroup_subsys_state *css =
3567 container_of(ref, struct cgroup_subsys_state, refcnt);
3568
Monam Agarwal01a97142014-03-24 00:17:18 +05303569 RCU_INIT_POINTER(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003570 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003571}
3572
Tejun Heo623f9262013-08-13 11:01:55 -04003573static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3574 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003575{
Paul Menagebd89aab2007-10-18 23:40:44 -07003576 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003577 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003578 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003579
Tejun Heo0ae78e02013-08-13 11:01:54 -04003580 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003581 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003582 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003583 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003584
Tejun Heoca8bdca2013-08-26 18:40:56 -04003585 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003586}
3587
Li Zefan2a4ac632013-07-31 16:16:40 +08003588/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04003589static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003590{
Tejun Heo623f9262013-08-13 11:01:55 -04003591 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08003592 int ret = 0;
3593
Tejun Heoace2bee2014-02-11 11:52:47 -05003594 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003595 lockdep_assert_held(&cgroup_mutex);
3596
Tejun Heo92fb9742012-11-19 08:13:38 -08003597 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04003598 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003599 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04003600 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04003601 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05003602 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003603 }
Tejun Heob1929db2012-11-19 08:13:38 -08003604 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003605}
3606
Li Zefan2a4ac632013-07-31 16:16:40 +08003607/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04003608static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003609{
Tejun Heo623f9262013-08-13 11:01:55 -04003610 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003611
Tejun Heoace2bee2014-02-11 11:52:47 -05003612 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003613 lockdep_assert_held(&cgroup_mutex);
3614
3615 if (!(css->flags & CSS_ONLINE))
3616 return;
3617
Li Zefand7eeac12013-03-12 15:35:59 -07003618 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04003619 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003620
Tejun Heoeb954192013-08-08 20:11:23 -04003621 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04003622 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05003623 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003624}
3625
Tejun Heoc81c925a2013-12-06 15:11:56 -05003626/**
3627 * create_css - create a cgroup_subsys_state
3628 * @cgrp: the cgroup new css will be associated with
3629 * @ss: the subsys of new css
3630 *
3631 * Create a new css associated with @cgrp - @ss pair. On success, the new
3632 * css is online and installed in @cgrp with all interface files created.
3633 * Returns 0 on success, -errno on failure.
3634 */
3635static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
3636{
3637 struct cgroup *parent = cgrp->parent;
3638 struct cgroup_subsys_state *css;
3639 int err;
3640
Tejun Heoc81c925a2013-12-06 15:11:56 -05003641 lockdep_assert_held(&cgroup_mutex);
3642
3643 css = ss->css_alloc(cgroup_css(parent, ss));
3644 if (IS_ERR(css))
3645 return PTR_ERR(css);
3646
3647 err = percpu_ref_init(&css->refcnt, css_release);
3648 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08003649 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05003650
3651 init_css(css, ss, cgrp);
3652
Tejun Heoaec25022014-02-08 10:36:58 -05003653 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003654 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08003655 goto err_free_percpu_ref;
Tejun Heoc81c925a2013-12-06 15:11:56 -05003656
3657 err = online_css(css);
3658 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08003659 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05003660
Tejun Heo59f52962014-02-11 11:52:49 -05003661 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003662 css_get(css->parent);
3663
3664 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
3665 parent->parent) {
3666 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",
3667 current->comm, current->pid, ss->name);
3668 if (!strcmp(ss->name, "memory"))
3669 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
3670 ss->warned_broken_hierarchy = true;
3671 }
3672
3673 return 0;
3674
Li Zefan3eb59ec2014-03-18 17:02:36 +08003675err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07003676 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08003677err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05003678 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08003679err_free_css:
Tejun Heoc81c925a2013-12-06 15:11:56 -05003680 ss->css_free(css);
3681 return err;
3682}
3683
Tejun Heo2bd59d42014-02-11 11:52:49 -05003684/**
Li Zefana043e3b2008-02-23 15:24:09 -08003685 * cgroup_create - create a cgroup
3686 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05003687 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05003688 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07003689 */
Tejun Heoe61734c2014-02-12 09:29:50 -05003690static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003691 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003692{
Paul Menagebd89aab2007-10-18 23:40:44 -07003693 struct cgroup *cgrp;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04003694 struct cgroup_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05003695 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003696 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003697 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003698
Tejun Heoa2dd4242014-03-19 10:23:55 -04003699 /*
3700 * XXX: The default hierarchy isn't fully implemented yet. Block
3701 * !root cgroup creation on it for now.
3702 */
3703 if (root == &cgrp_dfl_root)
3704 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003705
Tejun Heo0a950f62012-11-19 09:02:12 -08003706 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07003707 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3708 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003709 return -ENOMEM;
3710
Tejun Heoace2bee2014-02-11 11:52:47 -05003711 mutex_lock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003712
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003713 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08003714 * Only live parents can have children. Note that the liveliness
3715 * check isn't strictly necessary because cgroup_mkdir() and
3716 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
3717 * anyway so that locking is contained inside cgroup proper and we
3718 * don't get nasty surprises if we ever grow another caller.
3719 */
3720 if (!cgroup_lock_live_group(parent)) {
3721 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05003722 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08003723 }
3724
3725 /*
3726 * Temporarily set the pointer to NULL, so idr_find() won't return
3727 * a half-baked cgroup.
3728 */
3729 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
3730 if (cgrp->id < 0) {
3731 err = -ENOMEM;
3732 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08003733 }
3734
Paul Menagecc31edc2008-10-18 20:28:04 -07003735 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003736
Paul Menagebd89aab2007-10-18 23:40:44 -07003737 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003738 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07003739 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003740
Li Zefanb6abdb02008-03-04 14:28:19 -08003741 if (notify_on_release(parent))
3742 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3743
Tejun Heo2260e7f2012-11-19 08:13:38 -08003744 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
3745 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003746
Tejun Heo2bd59d42014-02-11 11:52:49 -05003747 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05003748 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003749 if (IS_ERR(kn)) {
3750 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003751 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003752 }
3753 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003754
Tejun Heo6f305582014-02-12 09:29:50 -05003755 /*
3756 * This extra ref will be put in cgroup_free_fn() and guarantees
3757 * that @cgrp->kn is always accessible.
3758 */
3759 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003760
Tejun Heo00356bd2013-06-18 11:14:22 -07003761 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09003762
Tejun Heo4e139af2012-11-19 08:13:36 -08003763 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08003764 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05003765 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05003766 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08003767
Tejun Heo0d802552013-12-06 15:11:56 -05003768 /*
3769 * @cgrp is now fully operational. If something fails after this
3770 * point, it'll be released via the normal destruction path.
3771 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003772 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
3773
Tejun Heo49957f82014-04-07 16:44:47 -04003774 err = cgroup_kn_set_ugid(kn);
3775 if (err)
3776 goto err_destroy;
3777
Tejun Heo2bb566c2013-08-08 20:11:23 -04003778 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07003779 if (err)
3780 goto err_destroy;
3781
Tejun Heo9d403e92013-12-06 15:11:56 -05003782 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05003783 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04003784 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heob85d2042013-12-06 15:11:57 -05003785 err = create_css(cgrp, ss);
3786 if (err)
3787 goto err_destroy;
3788 }
Tejun Heoa8638032012-11-09 09:12:29 -08003789 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003790
Tejun Heof392e512014-04-23 11:13:14 -04003791 cgrp->child_subsys_mask = parent->child_subsys_mask;
3792
Tejun Heo2bd59d42014-02-11 11:52:49 -05003793 kernfs_activate(kn);
3794
Paul Menageddbcc7e2007-10-18 23:39:30 -07003795 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003796 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003797
3798 return 0;
3799
Tejun Heo0a950f62012-11-19 09:02:12 -08003800err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003801 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003802err_unlock:
3803 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003804err_unlock_tree:
3805 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003806 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003807 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003808
3809err_destroy:
3810 cgroup_destroy_locked(cgrp);
3811 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003812 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003813 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003814}
3815
Tejun Heo2bd59d42014-02-11 11:52:49 -05003816static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3817 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003818{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003819 struct cgroup *parent = parent_kn->priv;
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003820 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003821
Tejun Heoe1b2dc12014-03-20 11:10:15 -04003822 /*
3823 * cgroup_create() grabs cgroup_tree_mutex which nests outside
3824 * kernfs active_ref and cgroup_create() already synchronizes
3825 * properly against removal through cgroup_lock_live_group().
3826 * Break it before calling cgroup_create().
3827 */
3828 cgroup_get(parent);
3829 kernfs_break_active_protection(parent_kn);
3830
3831 ret = cgroup_create(parent, name, mode);
3832
3833 kernfs_unbreak_active_protection(parent_kn);
3834 cgroup_put(parent);
3835 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003836}
3837
Tejun Heo223dbc32013-08-13 20:22:50 -04003838/*
3839 * This is called when the refcnt of a css is confirmed to be killed.
3840 * css_tryget() is now guaranteed to fail.
3841 */
3842static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07003843{
Tejun Heo223dbc32013-08-13 20:22:50 -04003844 struct cgroup_subsys_state *css =
3845 container_of(work, struct cgroup_subsys_state, destroy_work);
3846 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07003847
Tejun Heoace2bee2014-02-11 11:52:47 -05003848 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003849 mutex_lock(&cgroup_mutex);
3850
3851 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04003852 * css_tryget() is guaranteed to fail now. Tell subsystems to
3853 * initate destruction.
3854 */
3855 offline_css(css);
3856
3857 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003858 * If @cgrp is marked dead, it's waiting for refs of all css's to
3859 * be disabled before proceeding to the second phase of cgroup
3860 * destruction. If we are the last one, kick it off.
3861 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04003862 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04003863 cgroup_destroy_css_killed(cgrp);
3864
3865 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003866 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04003867
3868 /*
3869 * Put the css refs from kill_css(). Each css holds an extra
3870 * reference to the cgroup's dentry and cgroup removal proceeds
3871 * regardless of css refs. On the last put of each css, whenever
3872 * that may be, the extra dentry ref is put so that dentry
3873 * destruction happens only after all css's are released.
3874 */
3875 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07003876}
3877
Tejun Heo223dbc32013-08-13 20:22:50 -04003878/* css kill confirmation processing requires process context, bounce */
3879static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07003880{
3881 struct cgroup_subsys_state *css =
3882 container_of(ref, struct cgroup_subsys_state, refcnt);
3883
Tejun Heo223dbc32013-08-13 20:22:50 -04003884 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003885 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07003886}
3887
Tejun Heof392e512014-04-23 11:13:14 -04003888/**
3889 * kill_css - destroy a css
3890 * @css: css to destroy
3891 *
3892 * This function initiates destruction of @css by removing cgroup interface
3893 * files and putting its base reference. ->css_offline() will be invoked
3894 * asynchronously once css_tryget() is guaranteed to fail and when the
3895 * reference count reaches zero, @css will be released.
3896 */
3897static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04003898{
Tejun Heo94419622014-03-19 10:23:54 -04003899 lockdep_assert_held(&cgroup_tree_mutex);
3900
Tejun Heo2bd59d42014-02-11 11:52:49 -05003901 /*
3902 * This must happen before css is disassociated with its cgroup.
3903 * See seq_css() for details.
3904 */
Tejun Heoaec25022014-02-08 10:36:58 -05003905 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003906
Tejun Heoedae0c32013-08-13 20:22:51 -04003907 /*
3908 * Killing would put the base ref, but we need to keep it alive
3909 * until after ->css_offline().
3910 */
3911 css_get(css);
3912
3913 /*
3914 * cgroup core guarantees that, by the time ->css_offline() is
3915 * invoked, no new css reference will be given out via
3916 * css_tryget(). We can't simply call percpu_ref_kill() and
3917 * proceed to offlining css's because percpu_ref_kill() doesn't
3918 * guarantee that the ref is seen as killed on all CPUs on return.
3919 *
3920 * Use percpu_ref_kill_and_confirm() to get notifications as each
3921 * css is confirmed to be seen as killed on all CPUs.
3922 */
3923 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003924}
3925
3926/**
3927 * cgroup_destroy_locked - the first stage of cgroup destruction
3928 * @cgrp: cgroup to be destroyed
3929 *
3930 * css's make use of percpu refcnts whose killing latency shouldn't be
3931 * exposed to userland and are RCU protected. Also, cgroup core needs to
3932 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
3933 * invoked. To satisfy all the requirements, destruction is implemented in
3934 * the following two steps.
3935 *
3936 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
3937 * userland visible parts and start killing the percpu refcnts of
3938 * css's. Set up so that the next stage will be kicked off once all
3939 * the percpu refcnts are confirmed to be killed.
3940 *
3941 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
3942 * rest of destruction. Once all cgroup references are gone, the
3943 * cgroup is RCU-freed.
3944 *
3945 * This function implements s1. After this step, @cgrp is gone as far as
3946 * the userland is concerned and a new cgroup with the same name may be
3947 * created. As cgroup doesn't care about the names internally, this
3948 * doesn't cause any problem.
3949 */
Tejun Heo42809dd2012-11-19 08:13:37 -08003950static int cgroup_destroy_locked(struct cgroup *cgrp)
3951 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003952{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003953 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003954 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07003955 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05003956 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003957
Tejun Heoace2bee2014-02-11 11:52:47 -05003958 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003959 lockdep_assert_held(&cgroup_mutex);
3960
Tejun Heoddd69142013-06-12 21:04:54 -07003961 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05003962 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05003963 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07003964 */
Tejun Heo96d365e2014-02-13 06:58:40 -05003965 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003966 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05003967 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07003968 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003969 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08003970
Tejun Heo1a90dd52012-11-05 09:16:59 -08003971 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003972 * Make sure there's no live children. We can't test ->children
3973 * emptiness as dead children linger on it while being destroyed;
3974 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
3975 */
3976 empty = true;
3977 rcu_read_lock();
3978 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
3979 empty = cgroup_is_dead(child);
3980 if (!empty)
3981 break;
3982 }
3983 rcu_read_unlock();
3984 if (!empty)
3985 return -EBUSY;
3986
3987 /*
Tejun Heo455050d2013-06-13 19:27:41 -07003988 * Mark @cgrp dead. This prevents further task migration and child
3989 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04003990 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07003991 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04003992 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07003993 */
Tejun Heo54766d42013-06-12 21:04:53 -07003994 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08003995
Tejun Heo5d773812014-03-19 10:23:53 -04003996 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003997 * Initiate massacre of all css's. cgroup_destroy_css_killed()
3998 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05003999 * percpu refs of all css's are confirmed to be killed. This
4000 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004001 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004002 mutex_unlock(&cgroup_mutex);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004003 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004004 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004005 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004006
Tejun Heo455050d2013-06-13 19:27:41 -07004007 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4008 raw_spin_lock(&release_list_lock);
4009 if (!list_empty(&cgrp->release_list))
4010 list_del_init(&cgrp->release_list);
4011 raw_spin_unlock(&release_list_lock);
4012
4013 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004014 * If @cgrp has css's attached, the second stage of cgroup
4015 * destruction is kicked off from css_killed_work_fn() after the
4016 * refs of all attached css's are killed. If @cgrp doesn't have
4017 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004018 */
Tejun Heof20104d2013-08-13 20:22:50 -04004019 if (!cgrp->nr_css)
4020 cgroup_destroy_css_killed(cgrp);
4021
Tejun Heo2bd59d42014-02-11 11:52:49 -05004022 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05004023 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004024
Tejun Heof20104d2013-08-13 20:22:50 -04004025 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05004026 * There are two control paths which try to determine cgroup from
4027 * dentry without going through kernfs - cgroupstats_build() and
4028 * css_tryget_from_dir(). Those are supported by RCU protecting
4029 * clearing of cgrp->kn->priv backpointer, which should happen
4030 * after all files under it have been removed.
Tejun Heo455050d2013-06-13 19:27:41 -07004031 */
Tejun Heo6f305582014-02-12 09:29:50 -05004032 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004033 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004034
Tejun Heo4ac06012014-02-11 11:52:47 -05004035 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004036
Tejun Heoea15f8c2013-06-13 19:27:42 -07004037 return 0;
4038};
4039
Tejun Heod3daf282013-06-13 19:39:16 -07004040/**
Tejun Heof20104d2013-08-13 20:22:50 -04004041 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004042 * @work: cgroup->destroy_free_work
4043 *
4044 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004045 * destroyed after all css's are offlined and performs the rest of
4046 * destruction. This is the second step of destruction described in the
4047 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004048 */
Tejun Heof20104d2013-08-13 20:22:50 -04004049static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004050{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004051 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004052
Tejun Heoace2bee2014-02-11 11:52:47 -05004053 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004054 lockdep_assert_held(&cgroup_mutex);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004055
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004056 /* delete this cgroup from parent->children */
4057 list_del_rcu(&cgrp->sibling);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004058
Tejun Heo59f52962014-02-11 11:52:49 -05004059 cgroup_put(cgrp);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004060
Paul Menageddbcc7e2007-10-18 23:39:30 -07004061 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004062 check_for_release(parent);
4063}
4064
Tejun Heo2bd59d42014-02-11 11:52:49 -05004065static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004066{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004067 struct cgroup *cgrp = kn->priv;
4068 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004069
Tejun Heo2bd59d42014-02-11 11:52:49 -05004070 /*
4071 * This is self-destruction but @kn can't be removed while this
4072 * callback is in progress. Let's break active protection. Once
4073 * the protection is broken, @cgrp can be destroyed at any point.
4074 * Pin it so that it stays accessible.
4075 */
4076 cgroup_get(cgrp);
4077 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004078
Tejun Heoace2bee2014-02-11 11:52:47 -05004079 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004080 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004081
Tejun Heo2bd59d42014-02-11 11:52:49 -05004082 /*
4083 * @cgrp might already have been destroyed while we're trying to
4084 * grab the mutexes.
4085 */
4086 if (!cgroup_is_dead(cgrp))
4087 ret = cgroup_destroy_locked(cgrp);
4088
Tejun Heo42809dd2012-11-19 08:13:37 -08004089 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004090 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004091
Tejun Heo2bd59d42014-02-11 11:52:49 -05004092 kernfs_unbreak_active_protection(kn);
4093 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004094 return ret;
4095}
4096
Tejun Heo2bd59d42014-02-11 11:52:49 -05004097static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4098 .remount_fs = cgroup_remount,
4099 .show_options = cgroup_show_options,
4100 .mkdir = cgroup_mkdir,
4101 .rmdir = cgroup_rmdir,
4102 .rename = cgroup_rename,
4103};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004104
Li Zefan06a11922008-04-29 01:00:07 -07004105static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004106{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004107 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004108
4109 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004110
Tejun Heoace2bee2014-02-11 11:52:47 -05004111 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004112 mutex_lock(&cgroup_mutex);
4113
Tejun Heo0adb0702014-02-12 09:29:48 -05004114 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004115
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004116 /* Create the root cgroup state for this subsystem */
4117 ss->root = &cgrp_dfl_root;
4118 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004119 /* We don't handle early failures gracefully */
4120 BUG_ON(IS_ERR(css));
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004121 init_css(css, ss, &cgrp_dfl_root.cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004122
Li Zefane8d55fd2008-04-29 01:00:13 -07004123 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004124 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004125 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004126 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004127 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004128
4129 need_forkexit_callback |= ss->fork || ss->exit;
4130
Li Zefane8d55fd2008-04-29 01:00:13 -07004131 /* At system boot, before all subsystems have been
4132 * registered, no tasks have been forked, so we don't
4133 * need to invoke fork callbacks here. */
4134 BUG_ON(!list_empty(&init_task.tasks));
4135
Tejun Heoae7f1642013-08-13 20:22:50 -04004136 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004137
Tejun Heof392e512014-04-23 11:13:14 -04004138 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004139
Ben Blume6a11052010-03-10 15:22:09 -08004140 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004141 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004142}
4143
4144/**
Li Zefana043e3b2008-02-23 15:24:09 -08004145 * cgroup_init_early - cgroup initialization at system boot
4146 *
4147 * Initialize cgroups at system boot, and initialize any
4148 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004149 */
4150int __init cgroup_init_early(void)
4151{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004152 static struct cgroup_sb_opts __initdata opts =
4153 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004154 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004155 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004156
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004157 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004158 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004159
Tejun Heo3ed80a62014-02-08 10:36:58 -05004160 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004161 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004162 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4163 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004164 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004165 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4166 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004167
Tejun Heoaec25022014-02-08 10:36:58 -05004168 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004169 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004170
4171 if (ss->early_init)
4172 cgroup_init_subsys(ss);
4173 }
4174 return 0;
4175}
4176
4177/**
Li Zefana043e3b2008-02-23 15:24:09 -08004178 * cgroup_init - cgroup initialization
4179 *
4180 * Register cgroup filesystem and /proc file, and initialize
4181 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004182 */
4183int __init cgroup_init(void)
4184{
Tejun Heo30159ec2013-06-25 11:53:37 -07004185 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004186 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004187 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004188
Tejun Heo2bd59d42014-02-11 11:52:49 -05004189 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004190
Tejun Heo985ed672014-03-19 10:23:53 -04004191 mutex_lock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004192 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004193
Tejun Heo82fe9b02013-06-25 11:53:37 -07004194 /* Add init_css_set to the hash table */
4195 key = css_set_hash(init_css_set.subsys);
4196 hash_add(css_set_table, &init_css_set.hlist, key);
4197
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004198 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004199
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004200 mutex_unlock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04004201 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004202
Tejun Heo172a2c062014-03-19 10:23:53 -04004203 for_each_subsys(ss, ssid) {
4204 if (!ss->early_init)
4205 cgroup_init_subsys(ss);
4206
4207 /*
4208 * cftype registration needs kmalloc and can't be done
4209 * during early_init. Register base cftypes separately.
4210 */
4211 if (ss->base_cftypes)
4212 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4213 }
Greg KH676db4a2010-08-05 13:53:35 -07004214
Paul Menageddbcc7e2007-10-18 23:39:30 -07004215 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004216 if (!cgroup_kobj)
4217 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004218
4219 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004220 if (err < 0) {
4221 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004222 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004223 }
4224
4225 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004226 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004227}
Paul Menageb4f48b62007-10-18 23:39:33 -07004228
Tejun Heoe5fca242013-11-22 17:14:39 -05004229static int __init cgroup_wq_init(void)
4230{
4231 /*
4232 * There isn't much point in executing destruction path in
4233 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004234 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004235 *
4236 * We would prefer to do this in cgroup_init() above, but that
4237 * is called before init_workqueues(): so leave this until after.
4238 */
Tejun Heo1a115332014-02-12 19:06:19 -05004239 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004240 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004241
4242 /*
4243 * Used to destroy pidlists and separate to serve as flush domain.
4244 * Cap @max_active to 1 too.
4245 */
4246 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4247 0, 1);
4248 BUG_ON(!cgroup_pidlist_destroy_wq);
4249
Tejun Heoe5fca242013-11-22 17:14:39 -05004250 return 0;
4251}
4252core_initcall(cgroup_wq_init);
4253
Paul Menagea4243162007-10-18 23:39:35 -07004254/*
4255 * proc_cgroup_show()
4256 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4257 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004258 */
4259
4260/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004261int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004262{
4263 struct pid *pid;
4264 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004265 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004266 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004267 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004268
4269 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004270 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004271 if (!buf)
4272 goto out;
4273
4274 retval = -ESRCH;
4275 pid = m->private;
4276 tsk = get_pid_task(pid, PIDTYPE_PID);
4277 if (!tsk)
4278 goto out_free;
4279
4280 retval = 0;
4281
4282 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004283 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004284
Tejun Heo985ed672014-03-19 10:23:53 -04004285 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004286 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004287 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004288 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004289
Tejun Heoa2dd4242014-03-19 10:23:55 -04004290 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004291 continue;
4292
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004293 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004294 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004295 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004296 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004297 if (strlen(root->name))
4298 seq_printf(m, "%sname=%s", count ? "," : "",
4299 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004300 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004301 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004302 path = cgroup_path(cgrp, buf, PATH_MAX);
4303 if (!path) {
4304 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004305 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004306 }
4307 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004308 seq_putc(m, '\n');
4309 }
4310
4311out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004312 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004313 mutex_unlock(&cgroup_mutex);
4314 put_task_struct(tsk);
4315out_free:
4316 kfree(buf);
4317out:
4318 return retval;
4319}
4320
Paul Menagea4243162007-10-18 23:39:35 -07004321/* Display information about each subsystem and each hierarchy */
4322static int proc_cgroupstats_show(struct seq_file *m, void *v)
4323{
Tejun Heo30159ec2013-06-25 11:53:37 -07004324 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004325 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004326
Paul Menage8bab8dd2008-04-04 14:29:57 -07004327 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004328 /*
4329 * ideally we don't want subsystems moving around while we do this.
4330 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4331 * subsys/hierarchy state.
4332 */
Paul Menagea4243162007-10-18 23:39:35 -07004333 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004334
4335 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004336 seq_printf(m, "%s\t%d\t%d\t%d\n",
4337 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004338 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004339
Paul Menagea4243162007-10-18 23:39:35 -07004340 mutex_unlock(&cgroup_mutex);
4341 return 0;
4342}
4343
4344static int cgroupstats_open(struct inode *inode, struct file *file)
4345{
Al Viro9dce07f2008-03-29 03:07:28 +00004346 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004347}
4348
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004349static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004350 .open = cgroupstats_open,
4351 .read = seq_read,
4352 .llseek = seq_lseek,
4353 .release = single_release,
4354};
4355
Paul Menageb4f48b62007-10-18 23:39:33 -07004356/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004357 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004358 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004359 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004360 * A task is associated with the init_css_set until cgroup_post_fork()
4361 * attaches it to the parent's css_set. Empty cg_list indicates that
4362 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004363 */
4364void cgroup_fork(struct task_struct *child)
4365{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004366 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004367 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004368}
4369
4370/**
Li Zefana043e3b2008-02-23 15:24:09 -08004371 * cgroup_post_fork - called on a new task after adding it to the task list
4372 * @child: the task in question
4373 *
Tejun Heo5edee612012-10-16 15:03:14 -07004374 * Adds the task to the list running through its css_set if necessary and
4375 * call the subsystem fork() callbacks. Has to be after the task is
4376 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004377 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004378 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004379 */
Paul Menage817929e2007-10-18 23:39:36 -07004380void cgroup_post_fork(struct task_struct *child)
4381{
Tejun Heo30159ec2013-06-25 11:53:37 -07004382 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004383 int i;
4384
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004385 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004386 * This may race against cgroup_enable_task_cg_links(). As that
4387 * function sets use_task_css_set_links before grabbing
4388 * tasklist_lock and we just went through tasklist_lock to add
4389 * @child, it's guaranteed that either we see the set
4390 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4391 * @child during its iteration.
4392 *
4393 * If we won the race, @child is associated with %current's
4394 * css_set. Grabbing css_set_rwsem guarantees both that the
4395 * association is stable, and, on completion of the parent's
4396 * migration, @child is visible in the source of migration or
4397 * already in the destination cgroup. This guarantee is necessary
4398 * when implementing operations which need to migrate all tasks of
4399 * a cgroup to another.
4400 *
4401 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4402 * will remain in init_css_set. This is safe because all tasks are
4403 * in the init_css_set before cg_links is enabled and there's no
4404 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004405 */
Paul Menage817929e2007-10-18 23:39:36 -07004406 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004407 struct css_set *cset;
4408
Tejun Heo96d365e2014-02-13 06:58:40 -05004409 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004410 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004411 if (list_empty(&child->cg_list)) {
4412 rcu_assign_pointer(child->cgroups, cset);
4413 list_add(&child->cg_list, &cset->tasks);
4414 get_css_set(cset);
4415 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004416 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004417 }
Tejun Heo5edee612012-10-16 15:03:14 -07004418
4419 /*
4420 * Call ss->fork(). This must happen after @child is linked on
4421 * css_set; otherwise, @child might change state between ->fork()
4422 * and addition to css_set.
4423 */
4424 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004425 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004426 if (ss->fork)
4427 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004428 }
Paul Menage817929e2007-10-18 23:39:36 -07004429}
Tejun Heo5edee612012-10-16 15:03:14 -07004430
Paul Menage817929e2007-10-18 23:39:36 -07004431/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004432 * cgroup_exit - detach cgroup from exiting task
4433 * @tsk: pointer to task_struct of exiting process
4434 *
4435 * Description: Detach cgroup from @tsk and release it.
4436 *
4437 * Note that cgroups marked notify_on_release force every task in
4438 * them to take the global cgroup_mutex mutex when exiting.
4439 * This could impact scaling on very large systems. Be reluctant to
4440 * use notify_on_release cgroups where very high task exit scaling
4441 * is required on large systems.
4442 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05004443 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4444 * call cgroup_exit() while the task is still competent to handle
4445 * notify_on_release(), then leave the task attached to the root cgroup in
4446 * each hierarchy for the remainder of its exit. No need to bother with
4447 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08004448 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07004449 */
Li Zefan1ec41832014-03-28 15:22:19 +08004450void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07004451{
Tejun Heo30159ec2013-06-25 11:53:37 -07004452 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004453 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05004454 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004455 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004456
4457 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05004458 * Unlink from @tsk from its css_set. As migration path can't race
4459 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004460 */
4461 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004462 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004463 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004464 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004465 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07004466 }
4467
Paul Menageb4f48b62007-10-18 23:39:33 -07004468 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07004469 cset = task_css_set(tsk);
4470 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004471
Li Zefan1ec41832014-03-28 15:22:19 +08004472 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004473 /* see cgroup_post_fork() for details */
4474 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004475 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004476 struct cgroup_subsys_state *old_css = cset->subsys[i];
4477 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004478
Tejun Heoeb954192013-08-08 20:11:23 -04004479 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004480 }
4481 }
4482 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004483
Tejun Heoeaf797a2014-02-25 10:04:03 -05004484 if (put_cset)
4485 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07004486}
Paul Menage697f4162007-10-18 23:39:34 -07004487
Paul Menagebd89aab2007-10-18 23:40:44 -07004488static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004489{
Li Zefanf50daa72013-03-01 15:06:07 +08004490 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004491 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004492 /*
4493 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004494 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004495 * it now
4496 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004497 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004498
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004499 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004500 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004501 list_empty(&cgrp->release_list)) {
4502 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004503 need_schedule_work = 1;
4504 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004505 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004506 if (need_schedule_work)
4507 schedule_work(&release_agent_work);
4508 }
4509}
4510
Paul Menage81a6a5c2007-10-18 23:39:38 -07004511/*
4512 * Notify userspace when a cgroup is released, by running the
4513 * configured release agent with the name of the cgroup (path
4514 * relative to the root of cgroup file system) as the argument.
4515 *
4516 * Most likely, this user command will try to rmdir this cgroup.
4517 *
4518 * This races with the possibility that some other task will be
4519 * attached to this cgroup before it is removed, or that some other
4520 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4521 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4522 * unused, and this cgroup will be reprieved from its death sentence,
4523 * to continue to serve a useful existence. Next time it's released,
4524 * we will get notified again, if it still has 'notify_on_release' set.
4525 *
4526 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4527 * means only wait until the task is successfully execve()'d. The
4528 * separate release agent task is forked by call_usermodehelper(),
4529 * then control in this thread returns here, without waiting for the
4530 * release agent task. We don't bother to wait because the caller of
4531 * this routine has no use for the exit status of the release agent
4532 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004533 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004534static void cgroup_release_agent(struct work_struct *work)
4535{
4536 BUG_ON(work != &release_agent_work);
4537 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004538 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004539 while (!list_empty(&release_list)) {
4540 char *argv[3], *envp[3];
4541 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05004542 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07004543 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004544 struct cgroup,
4545 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004546 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004547 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05004548 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004549 if (!pathbuf)
4550 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05004551 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
4552 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07004553 goto continue_free;
4554 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4555 if (!agentbuf)
4556 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004557
4558 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004559 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05004560 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004561 argv[i] = NULL;
4562
4563 i = 0;
4564 /* minimal command environment */
4565 envp[i++] = "HOME=/";
4566 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4567 envp[i] = NULL;
4568
4569 /* Drop the lock while we invoke the usermode helper,
4570 * since the exec could involve hitting disk and hence
4571 * be a slow process */
4572 mutex_unlock(&cgroup_mutex);
4573 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004574 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004575 continue_free:
4576 kfree(pathbuf);
4577 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004578 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004579 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004580 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004581 mutex_unlock(&cgroup_mutex);
4582}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004583
4584static int __init cgroup_disable(char *str)
4585{
Tejun Heo30159ec2013-06-25 11:53:37 -07004586 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004587 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07004588 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004589
4590 while ((token = strsep(&str, ",")) != NULL) {
4591 if (!*token)
4592 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004593
Tejun Heo3ed80a62014-02-08 10:36:58 -05004594 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004595 if (!strcmp(token, ss->name)) {
4596 ss->disabled = 1;
4597 printk(KERN_INFO "Disabling %s control group"
4598 " subsystem\n", ss->name);
4599 break;
4600 }
4601 }
4602 }
4603 return 1;
4604}
4605__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004606
Tejun Heob77d7b62013-08-13 11:01:54 -04004607/**
Tejun Heo5a17f542014-02-11 11:52:47 -05004608 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04004609 * @dentry: directory dentry of interest
4610 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04004611 *
Tejun Heo5a17f542014-02-11 11:52:47 -05004612 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4613 * to get the corresponding css and return it. If such css doesn't exist
4614 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02004615 */
Tejun Heo5a17f542014-02-11 11:52:47 -05004616struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4617 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004618{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004619 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4620 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004621 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004622
Tejun Heo35cf0832013-08-26 18:40:56 -04004623 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004624 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4625 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004626 return ERR_PTR(-EBADF);
4627
Tejun Heo5a17f542014-02-11 11:52:47 -05004628 rcu_read_lock();
4629
Tejun Heo2bd59d42014-02-11 11:52:49 -05004630 /*
4631 * This path doesn't originate from kernfs and @kn could already
4632 * have been or be removed at any point. @kn->priv is RCU
4633 * protected for this access. See destroy_locked() for details.
4634 */
4635 cgrp = rcu_dereference(kn->priv);
4636 if (cgrp)
4637 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05004638
4639 if (!css || !css_tryget(css))
4640 css = ERR_PTR(-ENOENT);
4641
4642 rcu_read_unlock();
4643 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004644}
Stephane Eraniane5d13672011-02-14 11:20:01 +02004645
Li Zefan1cb650b2013-08-19 10:05:24 +08004646/**
4647 * css_from_id - lookup css by id
4648 * @id: the cgroup id
4649 * @ss: cgroup subsys to be looked into
4650 *
4651 * Returns the css if there's valid one with @id, otherwise returns NULL.
4652 * Should be called under rcu_read_lock().
4653 */
4654struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4655{
4656 struct cgroup *cgrp;
4657
Tejun Heoace2bee2014-02-11 11:52:47 -05004658 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08004659
4660 cgrp = idr_find(&ss->root->cgroup_idr, id);
4661 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04004662 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08004663 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004664}
4665
Paul Menagefe693432009-09-23 15:56:20 -07004666#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04004667static struct cgroup_subsys_state *
4668debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07004669{
4670 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4671
4672 if (!css)
4673 return ERR_PTR(-ENOMEM);
4674
4675 return css;
4676}
4677
Tejun Heoeb954192013-08-08 20:11:23 -04004678static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07004679{
Tejun Heoeb954192013-08-08 20:11:23 -04004680 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07004681}
4682
Tejun Heo182446d2013-08-08 20:11:24 -04004683static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
4684 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004685{
Tejun Heo182446d2013-08-08 20:11:24 -04004686 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07004687}
4688
Tejun Heo182446d2013-08-08 20:11:24 -04004689static u64 current_css_set_read(struct cgroup_subsys_state *css,
4690 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004691{
4692 return (u64)(unsigned long)current->cgroups;
4693}
4694
Tejun Heo182446d2013-08-08 20:11:24 -04004695static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08004696 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004697{
4698 u64 count;
4699
4700 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07004701 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07004702 rcu_read_unlock();
4703 return count;
4704}
4705
Tejun Heo2da8ca82013-12-05 12:28:04 -05004706static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004707{
Tejun Heo69d02062013-06-12 21:04:50 -07004708 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07004709 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05004710 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07004711
Tejun Heoe61734c2014-02-12 09:29:50 -05004712 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
4713 if (!name_buf)
4714 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07004715
Tejun Heo96d365e2014-02-13 06:58:40 -05004716 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07004717 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07004718 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07004719 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004720 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07004721
Tejun Heoa2dd4242014-03-19 10:23:55 -04004722 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004723 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04004724 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004725 }
4726 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05004727 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05004728 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004729 return 0;
4730}
4731
4732#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05004733static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004734{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004735 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07004736 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07004737
Tejun Heo96d365e2014-02-13 06:58:40 -05004738 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04004739 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004740 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004741 struct task_struct *task;
4742 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05004743
Tejun Heo5abb8852013-06-12 21:04:49 -07004744 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05004745
Tejun Heo5abb8852013-06-12 21:04:49 -07004746 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05004747 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
4748 goto overflow;
4749 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07004750 }
Tejun Heoc7561122014-02-25 10:04:01 -05004751
4752 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
4753 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
4754 goto overflow;
4755 seq_printf(seq, " task %d\n", task_pid_vnr(task));
4756 }
4757 continue;
4758 overflow:
4759 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07004760 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004761 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07004762 return 0;
4763}
4764
Tejun Heo182446d2013-08-08 20:11:24 -04004765static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004766{
Tejun Heo182446d2013-08-08 20:11:24 -04004767 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07004768}
4769
4770static struct cftype debug_files[] = {
4771 {
Paul Menagefe693432009-09-23 15:56:20 -07004772 .name = "taskcount",
4773 .read_u64 = debug_taskcount_read,
4774 },
4775
4776 {
4777 .name = "current_css_set",
4778 .read_u64 = current_css_set_read,
4779 },
4780
4781 {
4782 .name = "current_css_set_refcount",
4783 .read_u64 = current_css_set_refcount_read,
4784 },
4785
4786 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004787 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004788 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004789 },
4790
4791 {
4792 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004793 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004794 },
4795
4796 {
Paul Menagefe693432009-09-23 15:56:20 -07004797 .name = "releasable",
4798 .read_u64 = releasable_read,
4799 },
Paul Menagefe693432009-09-23 15:56:20 -07004800
Tejun Heo4baf6e32012-04-01 12:09:55 -07004801 { } /* terminate */
4802};
Paul Menagefe693432009-09-23 15:56:20 -07004803
Tejun Heo073219e2014-02-08 10:36:58 -05004804struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08004805 .css_alloc = debug_css_alloc,
4806 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07004807 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07004808};
4809#endif /* CONFIG_CGROUP_DEBUG */