blob: cda614da40cfc5c74e442991a03d9291ad3fa8b2 [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>
45#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070046#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070047#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070048#include <linux/delayacct.h>
49#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080050#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070051#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070052#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070053#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Li Zefan081aa452013-03-13 09:17:09 +080054#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020055#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070056
Arun Sharma600634972011-07-26 16:09:06 -070057#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070058
Tejun Heoe25e2cb2011-12-12 18:12:21 -080059/*
Tejun Heob1a21362013-11-29 10:42:58 -050060 * pidlists linger the following amount before being destroyed. The goal
61 * is avoiding frequent destruction in the middle of consecutive read calls
62 * Expiring in the middle is a performance problem not a correctness one.
63 * 1 sec should be enough.
64 */
65#define CGROUP_PIDLIST_DESTROY_DELAY HZ
66
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050067#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
68 MAX_CFTYPE_NAME + 2)
69
Tejun Heob1a21362013-11-29 10:42:58 -050070/*
Tejun Heoace2bee2014-02-11 11:52:47 -050071 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
72 * creation/removal and hierarchy changing operations including cgroup
73 * creation, removal, css association and controller rebinding. This outer
74 * lock is needed mainly to resolve the circular dependency between kernfs
75 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
76 */
77static DEFINE_MUTEX(cgroup_tree_mutex);
78
79/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080080 * cgroup_mutex is the master lock. Any modification to cgroup or its
81 * hierarchy must be performed while holding it.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080082 */
Tejun Heo22194492013-04-07 09:29:51 -070083#ifdef CONFIG_PROVE_RCU
84DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040085EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070086#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070087static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070088#endif
89
Tejun Heo69e943b2014-02-08 10:36:58 -050090/*
91 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
92 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
93 */
94static DEFINE_SPINLOCK(release_agent_path_lock);
95
Tejun Heoace2bee2014-02-11 11:52:47 -050096#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -050097 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -050098 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -050099 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500100 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo87fb54f2013-12-06 15:11:55 -0500101
Ben Blumaae8aab2010-03-10 15:22:07 -0800102/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500103 * cgroup destruction makes heavy use of work items and there can be a lot
104 * of concurrent destructions. Use a separate workqueue so that cgroup
105 * destruction work items don't end up filling up max_active of system_wq
106 * which may lead to deadlock.
107 */
108static struct workqueue_struct *cgroup_destroy_wq;
109
110/*
Tejun Heob1a21362013-11-29 10:42:58 -0500111 * pidlist destructions need to be flushed on cgroup destruction. Use a
112 * separate workqueue as flush domain.
113 */
114static struct workqueue_struct *cgroup_pidlist_destroy_wq;
115
Tejun Heo3ed80a62014-02-08 10:36:58 -0500116/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500117#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500118static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700119#include <linux/cgroup_subsys.h>
120};
Tejun Heo073219e2014-02-08 10:36:58 -0500121#undef SUBSYS
122
123/* array of cgroup subsystem names */
124#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
125static const char *cgroup_subsys_name[] = {
126#include <linux/cgroup_subsys.h>
127};
128#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700129
Paul Menageddbcc7e2007-10-18 23:39:30 -0700130/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700131 * The dummy hierarchy, reserved for the subsystems that are otherwise
132 * unattached - it never has more than a single cgroup, and all tasks are
133 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700134 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700135static struct cgroupfs_root cgroup_dummy_root;
136
137/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
138static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700139
140/* The list of hierarchy roots */
141
Tejun Heo9871bf92013-06-24 15:21:47 -0700142static LIST_HEAD(cgroup_roots);
143static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700144
Tejun Heo3417ae12014-02-08 10:37:01 -0500145/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700146static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700147
Li Zefan65dff752013-03-01 15:01:56 +0800148static struct cgroup_name root_cgroup_name = { .name = "/" };
149
Li Zefan794611a2013-06-18 18:53:53 +0800150/*
151 * Assign a monotonically increasing serial number to cgroups. It
152 * guarantees cgroups with bigger numbers are newer than those with smaller
153 * numbers. Also, as cgroups are always appended to the parent's
154 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700155 * the ascending serial number order on the list. Protected by
156 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800157 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700158static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800159
Paul Menageddbcc7e2007-10-18 23:39:30 -0700160/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800161 * check for fork/exit handlers to call. This avoids us having to do
162 * extra work in the fork/exit path if none of the subsystems need to
163 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700164 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700165static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700166
Tejun Heo628f7cd2013-06-28 16:24:11 -0700167static struct cftype cgroup_base_files[];
168
Tejun Heo59f52962014-02-11 11:52:49 -0500169static void cgroup_put(struct cgroup *cgrp);
Tejun Heof2e85d52014-02-11 11:52:49 -0500170static int rebind_subsystems(struct cgroupfs_root *root,
171 unsigned long added_mask, unsigned removed_mask);
Tejun Heof20104d2013-08-13 20:22:50 -0400172static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800173static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400174static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
175 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500176static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800177
Tejun Heo95109b62013-08-08 20:11:27 -0400178/**
179 * cgroup_css - obtain a cgroup's css for the specified subsystem
180 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400181 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400182 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400183 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
184 * function must be called either under cgroup_mutex or rcu_read_lock() and
185 * the caller is responsible for pinning the returned css if it wants to
186 * keep accessing it outside the said locks. This function may return
187 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400188 */
189static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400190 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400191{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400192 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500193 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500194 lockdep_is_held(&cgroup_tree_mutex) ||
195 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400196 else
197 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400198}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700199
Paul Menageddbcc7e2007-10-18 23:39:30 -0700200/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700201static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700202{
Tejun Heo54766d42013-06-12 21:04:53 -0700203 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700204}
205
Tejun Heo59f52962014-02-11 11:52:49 -0500206struct cgroup_subsys_state *seq_css(struct seq_file *seq)
207{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500208 struct kernfs_open_file *of = seq->private;
209 struct cgroup *cgrp = of->kn->parent->priv;
210 struct cftype *cft = seq_cft(seq);
211
212 /*
213 * This is open and unprotected implementation of cgroup_css().
214 * seq_css() is only called from a kernfs file operation which has
215 * an active reference on the file. Because all the subsystem
216 * files are drained before a css is disassociated with a cgroup,
217 * the matching css from the cgroup's subsys table is guaranteed to
218 * be and stay valid until the enclosing operation is complete.
219 */
220 if (cft->ss)
221 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
222 else
223 return &cgrp->dummy_css;
Tejun Heo59f52962014-02-11 11:52:49 -0500224}
225EXPORT_SYMBOL_GPL(seq_css);
226
Li Zefan78574cf2013-04-08 19:00:38 -0700227/**
228 * cgroup_is_descendant - test ancestry
229 * @cgrp: the cgroup to be tested
230 * @ancestor: possible ancestor of @cgrp
231 *
232 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
233 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
234 * and @ancestor are accessible.
235 */
236bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
237{
238 while (cgrp) {
239 if (cgrp == ancestor)
240 return true;
241 cgrp = cgrp->parent;
242 }
243 return false;
244}
245EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700246
Adrian Bunke9685a02008-02-07 00:13:46 -0800247static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700248{
249 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700250 (1 << CGRP_RELEASABLE) |
251 (1 << CGRP_NOTIFY_ON_RELEASE);
252 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700253}
254
Adrian Bunke9685a02008-02-07 00:13:46 -0800255static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700256{
Paul Menagebd89aab2007-10-18 23:40:44 -0700257 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700258}
259
Tejun Heo30159ec2013-06-25 11:53:37 -0700260/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500261 * for_each_css - iterate all css's of a cgroup
262 * @css: the iteration cursor
263 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
264 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700265 *
266 * Should be called under cgroup_mutex.
267 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500268#define for_each_css(css, ssid, cgrp) \
269 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
270 if (!((css) = rcu_dereference_check( \
271 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500272 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500273 lockdep_is_held(&cgroup_mutex)))) { } \
274 else
275
276/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500277 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700278 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500279 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700280 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500281#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500282 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
283 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700284
Tejun Heo5549c492013-06-24 15:21:48 -0700285/* iterate across the active hierarchies */
286#define for_each_active_root(root) \
287 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700288
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700289/**
290 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
291 * @cgrp: the cgroup to be checked for liveness
292 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700293 * On success, returns true; the mutex should be later unlocked. On
294 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700295 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700296static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700297{
298 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700299 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700300 mutex_unlock(&cgroup_mutex);
301 return false;
302 }
303 return true;
304}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700305
Paul Menage81a6a5c2007-10-18 23:39:38 -0700306/* the list of cgroups eligible for automatic release. Protected by
307 * release_list_lock */
308static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200309static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700310static void cgroup_release_agent(struct work_struct *work);
311static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700312static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700313
Tejun Heo69d02062013-06-12 21:04:50 -0700314/*
315 * A cgroup can be associated with multiple css_sets as different tasks may
316 * belong to different cgroups on different hierarchies. In the other
317 * direction, a css_set is naturally associated with multiple cgroups.
318 * This M:N relationship is represented by the following link structure
319 * which exists for each association and allows traversing the associations
320 * from both sides.
321 */
322struct cgrp_cset_link {
323 /* the cgroup and css_set this link associates */
324 struct cgroup *cgrp;
325 struct css_set *cset;
326
327 /* list of cgrp_cset_links anchored at cgrp->cset_links */
328 struct list_head cset_link;
329
330 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
331 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700332};
333
334/* The default css_set - used by init and its children prior to any
335 * hierarchies being mounted. It contains a pointer to the root state
336 * for each subsystem. Also used to anchor the list of css_sets. Not
337 * reference-counted, to improve performance when child cgroups
338 * haven't been created.
339 */
340
341static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700342static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700343
Tejun Heo0942eee2013-08-08 20:11:26 -0400344/*
345 * css_set_lock protects the list of css_set objects, and the chain of
346 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400347 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400348 */
Paul Menage817929e2007-10-18 23:39:36 -0700349static DEFINE_RWLOCK(css_set_lock);
350static int css_set_count;
351
Paul Menage7717f7b2009-09-23 15:56:22 -0700352/*
353 * hash table for cgroup groups. This improves the performance to find
354 * an existing css_set. This hash doesn't (currently) take into
355 * account cgroups in empty hierarchies.
356 */
Li Zefan472b1052008-04-29 01:00:11 -0700357#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800358static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700359
Li Zefan0ac801f2013-01-10 11:49:27 +0800360static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700361{
Li Zefan0ac801f2013-01-10 11:49:27 +0800362 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700363 struct cgroup_subsys *ss;
364 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700365
Tejun Heo30159ec2013-06-25 11:53:37 -0700366 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800367 key += (unsigned long)css[i];
368 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700369
Li Zefan0ac801f2013-01-10 11:49:27 +0800370 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700371}
372
Tejun Heo0942eee2013-08-08 20:11:26 -0400373/*
374 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400375 * until after the first call to css_task_iter_start(). This reduces the
376 * fork()/exit() overhead for people who have cgroups compiled into their
377 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400378 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700379static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700380
Tejun Heo5abb8852013-06-12 21:04:49 -0700381static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700382{
Tejun Heo69d02062013-06-12 21:04:50 -0700383 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700384
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700385 /*
386 * Ensure that the refcount doesn't hit zero while any readers
387 * can see it. Similar to atomic_dec_and_lock(), but for an
388 * rwlock
389 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700390 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700391 return;
392 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700393 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700394 write_unlock(&css_set_lock);
395 return;
396 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700397
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700398 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700399 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700400 css_set_count--;
401
Tejun Heo69d02062013-06-12 21:04:50 -0700402 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700403 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700404
Tejun Heo69d02062013-06-12 21:04:50 -0700405 list_del(&link->cset_link);
406 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800407
Tejun Heoddd69142013-06-12 21:04:54 -0700408 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700409 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700410 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700411 set_bit(CGRP_RELEASABLE, &cgrp->flags);
412 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700413 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700414
415 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700416 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700417
418 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700419 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700420}
421
422/*
423 * refcounted get/put for css_set objects
424 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700425static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700426{
Tejun Heo5abb8852013-06-12 21:04:49 -0700427 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700428}
429
Tejun Heo5abb8852013-06-12 21:04:49 -0700430static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700431{
Tejun Heo5abb8852013-06-12 21:04:49 -0700432 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700433}
434
Tejun Heo5abb8852013-06-12 21:04:49 -0700435static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700436{
Tejun Heo5abb8852013-06-12 21:04:49 -0700437 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700438}
439
Tejun Heob326f9d2013-06-24 15:21:48 -0700440/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700441 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700442 * @cset: candidate css_set being tested
443 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700444 * @new_cgrp: cgroup that's being entered by the task
445 * @template: desired set of css pointers in css_set (pre-calculated)
446 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800447 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700448 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
449 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700450static bool compare_css_sets(struct css_set *cset,
451 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700452 struct cgroup *new_cgrp,
453 struct cgroup_subsys_state *template[])
454{
455 struct list_head *l1, *l2;
456
Tejun Heo5abb8852013-06-12 21:04:49 -0700457 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700458 /* Not all subsystems matched */
459 return false;
460 }
461
462 /*
463 * Compare cgroup pointers in order to distinguish between
464 * different cgroups in heirarchies with no subsystems. We
465 * could get by with just this check alone (and skip the
466 * memcmp above) but on most setups the memcmp check will
467 * avoid the need for this more expensive check on almost all
468 * candidates.
469 */
470
Tejun Heo69d02062013-06-12 21:04:50 -0700471 l1 = &cset->cgrp_links;
472 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700473 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700474 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700475 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700476
477 l1 = l1->next;
478 l2 = l2->next;
479 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700480 if (l1 == &cset->cgrp_links) {
481 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700482 break;
483 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700484 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700485 }
486 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700487 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
488 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
489 cgrp1 = link1->cgrp;
490 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700491 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700492 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700493
494 /*
495 * If this hierarchy is the hierarchy of the cgroup
496 * that's changing, then we need to check that this
497 * css_set points to the new cgroup; if it's any other
498 * hierarchy, then this css_set should point to the
499 * same cgroup as the old css_set.
500 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700501 if (cgrp1->root == new_cgrp->root) {
502 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700503 return false;
504 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700505 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700506 return false;
507 }
508 }
509 return true;
510}
511
Tejun Heob326f9d2013-06-24 15:21:48 -0700512/**
513 * find_existing_css_set - init css array and find the matching css_set
514 * @old_cset: the css_set that we're using before the cgroup transition
515 * @cgrp: the cgroup that we're moving into
516 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700517 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700518static struct css_set *find_existing_css_set(struct css_set *old_cset,
519 struct cgroup *cgrp,
520 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700521{
Paul Menagebd89aab2007-10-18 23:40:44 -0700522 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700523 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700524 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800525 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700526 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700527
Ben Blumaae8aab2010-03-10 15:22:07 -0800528 /*
529 * Build the set of subsystem state objects that we want to see in the
530 * new css_set. while subsystems can change globally, the entries here
531 * won't change, so no need for locking.
532 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700533 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400534 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700535 /* Subsystem is in this hierarchy. So we want
536 * the subsystem state from the new
537 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400538 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700539 } else {
540 /* Subsystem is not in this hierarchy, so we
541 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700542 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700543 }
544 }
545
Li Zefan0ac801f2013-01-10 11:49:27 +0800546 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700547 hash_for_each_possible(css_set_table, cset, hlist, key) {
548 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700549 continue;
550
551 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700552 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700553 }
Paul Menage817929e2007-10-18 23:39:36 -0700554
555 /* No existing cgroup group matched */
556 return NULL;
557}
558
Tejun Heo69d02062013-06-12 21:04:50 -0700559static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700560{
Tejun Heo69d02062013-06-12 21:04:50 -0700561 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700562
Tejun Heo69d02062013-06-12 21:04:50 -0700563 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
564 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700565 kfree(link);
566 }
567}
568
Tejun Heo69d02062013-06-12 21:04:50 -0700569/**
570 * allocate_cgrp_cset_links - allocate cgrp_cset_links
571 * @count: the number of links to allocate
572 * @tmp_links: list_head the allocated links are put on
573 *
574 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
575 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700576 */
Tejun Heo69d02062013-06-12 21:04:50 -0700577static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700578{
Tejun Heo69d02062013-06-12 21:04:50 -0700579 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700580 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700581
582 INIT_LIST_HEAD(tmp_links);
583
Li Zefan36553432008-07-29 22:33:19 -0700584 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700585 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700586 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700587 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700588 return -ENOMEM;
589 }
Tejun Heo69d02062013-06-12 21:04:50 -0700590 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700591 }
592 return 0;
593}
594
Li Zefanc12f65d2009-01-07 18:07:42 -0800595/**
596 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700597 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700598 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800599 * @cgrp: the destination cgroup
600 */
Tejun Heo69d02062013-06-12 21:04:50 -0700601static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
602 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800603{
Tejun Heo69d02062013-06-12 21:04:50 -0700604 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800605
Tejun Heo69d02062013-06-12 21:04:50 -0700606 BUG_ON(list_empty(tmp_links));
607 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
608 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700609 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700610 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700611 /*
612 * Always add links to the tail of the list so that the list
613 * is sorted by order of hierarchy creation
614 */
Tejun Heo69d02062013-06-12 21:04:50 -0700615 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800616}
617
Tejun Heob326f9d2013-06-24 15:21:48 -0700618/**
619 * find_css_set - return a new css_set with one cgroup updated
620 * @old_cset: the baseline css_set
621 * @cgrp: the cgroup to be updated
622 *
623 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
624 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700625 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700626static struct css_set *find_css_set(struct css_set *old_cset,
627 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700628{
Tejun Heob326f9d2013-06-24 15:21:48 -0700629 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700630 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700631 struct list_head tmp_links;
632 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800633 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700634
Tejun Heob326f9d2013-06-24 15:21:48 -0700635 lockdep_assert_held(&cgroup_mutex);
636
Paul Menage817929e2007-10-18 23:39:36 -0700637 /* First see if we already have a cgroup group that matches
638 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700639 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700640 cset = find_existing_css_set(old_cset, cgrp, template);
641 if (cset)
642 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700643 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700644
Tejun Heo5abb8852013-06-12 21:04:49 -0700645 if (cset)
646 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700647
Tejun Heof4f4be22013-06-12 21:04:51 -0700648 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700649 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700650 return NULL;
651
Tejun Heo69d02062013-06-12 21:04:50 -0700652 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700653 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700654 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700655 return NULL;
656 }
657
Tejun Heo5abb8852013-06-12 21:04:49 -0700658 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700659 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700660 INIT_LIST_HEAD(&cset->tasks);
661 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700662
663 /* Copy the set of subsystem state objects generated in
664 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700665 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700666
667 write_lock(&css_set_lock);
668 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700669 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700670 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700671
Paul Menage7717f7b2009-09-23 15:56:22 -0700672 if (c->root == cgrp->root)
673 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700674 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700675 }
Paul Menage817929e2007-10-18 23:39:36 -0700676
Tejun Heo69d02062013-06-12 21:04:50 -0700677 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700678
Paul Menage817929e2007-10-18 23:39:36 -0700679 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700680
681 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700682 key = css_set_hash(cset->subsys);
683 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700684
Paul Menage817929e2007-10-18 23:39:36 -0700685 write_unlock(&css_set_lock);
686
Tejun Heo5abb8852013-06-12 21:04:49 -0700687 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700688}
689
Tejun Heo2bd59d42014-02-11 11:52:49 -0500690static struct cgroupfs_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
691{
692 struct cgroup *top_cgrp = kf_root->kn->priv;
693
694 return top_cgrp->root;
695}
696
Tejun Heof2e85d52014-02-11 11:52:49 -0500697static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
698{
699 int id;
700
701 lockdep_assert_held(&cgroup_mutex);
702
703 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
704 GFP_KERNEL);
705 if (id < 0)
706 return id;
707
708 root->hierarchy_id = id;
709 return 0;
710}
711
712static void cgroup_exit_root_id(struct cgroupfs_root *root)
713{
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
722static void cgroup_free_root(struct cgroupfs_root *root)
723{
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 Heo59f52962014-02-11 11:52:49 -0500733static void cgroup_get_root(struct cgroupfs_root *root)
734{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500735 /*
736 * The caller must ensure that @root is alive, which can be
737 * achieved by holding a ref on one of the member cgroups or
738 * following a registered reference to @root while holding
739 * cgroup_tree_mutex.
740 */
741 WARN_ON_ONCE(atomic_read(&root->refcnt) <= 0);
742 atomic_inc(&root->refcnt);
Tejun Heo59f52962014-02-11 11:52:49 -0500743}
744
745static void cgroup_put_root(struct cgroupfs_root *root)
746{
Tejun Heof2e85d52014-02-11 11:52:49 -0500747 struct cgroup *cgrp = &root->top_cgroup;
748 struct cgrp_cset_link *link, *tmp_link;
749 int ret;
750
Tejun Heo2bd59d42014-02-11 11:52:49 -0500751 /*
752 * @root's refcnt reaching zero and its deregistration should be
753 * atomic w.r.t. cgroup_tree_mutex. This ensures that
754 * cgroup_get_root() is safe to invoke if @root is registered.
755 */
756 mutex_lock(&cgroup_tree_mutex);
757 if (!atomic_dec_and_test(&root->refcnt)) {
758 mutex_unlock(&cgroup_tree_mutex);
759 return;
760 }
761 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500762
763 BUG_ON(root->number_of_cgroups != 1);
764 BUG_ON(!list_empty(&cgrp->children));
765
Tejun Heof2e85d52014-02-11 11:52:49 -0500766 /* Rebind all subsystems back to the default hierarchy */
767 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
768 ret = rebind_subsystems(root, 0, root->subsys_mask);
769 /* Shouldn't be able to fail ... */
770 BUG_ON(ret);
771 }
772
773 /*
774 * Release all the links from cset_links to this hierarchy's
775 * root cgroup
776 */
777 write_lock(&css_set_lock);
778
779 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
780 list_del(&link->cset_link);
781 list_del(&link->cgrp_link);
782 kfree(link);
783 }
784 write_unlock(&css_set_lock);
785
786 if (!list_empty(&root->root_list)) {
787 list_del(&root->root_list);
788 cgroup_root_count--;
789 }
790
791 cgroup_exit_root_id(root);
792
793 mutex_unlock(&cgroup_mutex);
794 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500795
Tejun Heo2bd59d42014-02-11 11:52:49 -0500796 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500797 cgroup_free_root(root);
798}
799
Paul Menageddbcc7e2007-10-18 23:39:30 -0700800/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700801 * Return the cgroup for "task" from the given hierarchy. Must be
802 * called with cgroup_mutex held.
803 */
804static struct cgroup *task_cgroup_from_root(struct task_struct *task,
805 struct cgroupfs_root *root)
806{
Tejun Heo5abb8852013-06-12 21:04:49 -0700807 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700808 struct cgroup *res = NULL;
809
810 BUG_ON(!mutex_is_locked(&cgroup_mutex));
811 read_lock(&css_set_lock);
812 /*
813 * No need to lock the task - since we hold cgroup_mutex the
814 * task can't change groups, so the only thing that can happen
815 * is that it exits and its css is set back to init_css_set.
816 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700817 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700818 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700819 res = &root->top_cgroup;
820 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700821 struct cgrp_cset_link *link;
822
823 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700824 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700825
Paul Menage7717f7b2009-09-23 15:56:22 -0700826 if (c->root == root) {
827 res = c;
828 break;
829 }
830 }
831 }
832 read_unlock(&css_set_lock);
833 BUG_ON(!res);
834 return res;
835}
836
837/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700838 * There is one global cgroup mutex. We also require taking
839 * task_lock() when dereferencing a task's cgroup subsys pointers.
840 * See "The task_lock() exception", at the end of this comment.
841 *
842 * A task must hold cgroup_mutex to modify cgroups.
843 *
844 * Any task can increment and decrement the count field without lock.
845 * So in general, code holding cgroup_mutex can't rely on the count
846 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800847 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700848 * means that no tasks are currently attached, therefore there is no
849 * way a task attached to that cgroup can fork (the other way to
850 * increment the count). So code holding cgroup_mutex can safely
851 * assume that if the count is zero, it will stay zero. Similarly, if
852 * a task holds cgroup_mutex on a cgroup with zero count, it
853 * knows that the cgroup won't be removed, as cgroup_rmdir()
854 * needs that mutex.
855 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700856 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
857 * (usually) take cgroup_mutex. These are the two most performance
858 * critical pieces of code here. The exception occurs on cgroup_exit(),
859 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
860 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800861 * to the release agent with the name of the cgroup (path relative to
862 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700863 *
864 * A cgroup can only be deleted if both its 'count' of using tasks
865 * is zero, and its list of 'children' cgroups is empty. Since all
866 * tasks in the system use _some_ cgroup, and since there is always at
867 * least one task in the system (init, pid == 1), therefore, top_cgroup
868 * always has either children cgroups and/or using tasks. So we don't
869 * need a special hack to ensure that top_cgroup cannot be deleted.
870 *
871 * The task_lock() exception
872 *
873 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800874 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800875 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700876 * several performance critical places that need to reference
877 * task->cgroup without the expense of grabbing a system global
878 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800879 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700880 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
881 * the task_struct routinely used for such matters.
882 *
883 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800884 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700885 */
886
Tejun Heo628f7cd2013-06-28 16:24:11 -0700887static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500888static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700889static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700890
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500891static struct cgroup_name *cgroup_alloc_name(const char *name_str)
Li Zefan65dff752013-03-01 15:01:56 +0800892{
893 struct cgroup_name *name;
894
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500895 name = kmalloc(sizeof(*name) + strlen(name_str) + 1, GFP_KERNEL);
Li Zefan65dff752013-03-01 15:01:56 +0800896 if (!name)
897 return NULL;
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500898 strcpy(name->name, name_str);
Li Zefan65dff752013-03-01 15:01:56 +0800899 return name;
900}
901
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500902static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
903 char *buf)
904{
905 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
906 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
907 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
908 cft->ss->name, cft->name);
909 else
910 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
911 return buf;
912}
913
Tejun Heof2e85d52014-02-11 11:52:49 -0500914/**
915 * cgroup_file_mode - deduce file mode of a control file
916 * @cft: the control file in question
917 *
918 * returns cft->mode if ->mode is not 0
919 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
920 * returns S_IRUGO if it has only a read handler
921 * returns S_IWUSR if it has only a write hander
922 */
923static umode_t cgroup_file_mode(const struct cftype *cft)
924{
925 umode_t mode = 0;
926
927 if (cft->mode)
928 return cft->mode;
929
930 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
931 mode |= S_IRUGO;
932
933 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
934 cft->trigger)
935 mode |= S_IWUSR;
936
937 return mode;
938}
939
Li Zefanbe445622013-01-24 14:31:42 +0800940static void cgroup_free_fn(struct work_struct *work)
941{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700942 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800943
944 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800945 cgrp->root->number_of_cgroups--;
946 mutex_unlock(&cgroup_mutex);
947
948 /*
Tejun Heo59f52962014-02-11 11:52:49 -0500949 * We get a ref to the parent, and put the ref when this cgroup is
950 * being freed, so it's guaranteed that the parent won't be
951 * destroyed before its children.
Li Zefan415cf072013-04-08 14:35:02 +0800952 */
Tejun Heo59f52962014-02-11 11:52:49 -0500953 cgroup_put(cgrp->parent);
Li Zefan415cf072013-04-08 14:35:02 +0800954
Tejun Heo59f52962014-02-11 11:52:49 -0500955 /* put the root reference that we took when we created the cgroup */
956 cgroup_put_root(cgrp->root);
Li Zefanbe445622013-01-24 14:31:42 +0800957
Tejun Heob1a21362013-11-29 10:42:58 -0500958 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800959
Li Zefan65dff752013-03-01 15:01:56 +0800960 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800961 kfree(cgrp);
962}
963
964static void cgroup_free_rcu(struct rcu_head *head)
965{
966 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
967
Tejun Heoea15f8c2013-06-13 19:27:42 -0700968 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500969 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800970}
971
Tejun Heo59f52962014-02-11 11:52:49 -0500972static void cgroup_get(struct cgroup *cgrp)
973{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500974 WARN_ON_ONCE(cgroup_is_dead(cgrp));
975 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
976 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700977}
978
Tejun Heo59f52962014-02-11 11:52:49 -0500979static void cgroup_put(struct cgroup *cgrp)
980{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500981 if (!atomic_dec_and_test(&cgrp->refcnt))
982 return;
983 if (WARN_ON_ONCE(!cgroup_is_dead(cgrp)))
984 return;
Tejun Heo59f52962014-02-11 11:52:49 -0500985
Tejun Heo2bd59d42014-02-11 11:52:49 -0500986 /*
987 * XXX: cgrp->id is only used to look up css's. As cgroup and
988 * css's lifetimes will be decoupled, it should be made
989 * per-subsystem and moved to css->id so that lookups are
990 * successful until the target css is released.
991 */
992 mutex_lock(&cgroup_mutex);
993 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
994 mutex_unlock(&cgroup_mutex);
995 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700996
Tejun Heo2bd59d42014-02-11 11:52:49 -0500997 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998}
999
Li Zefan2739d3c2013-01-21 18:18:33 +08001000static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001002 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003
Tejun Heoace2bee2014-02-11 11:52:47 -05001004 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001005 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001006}
1007
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001008/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001009 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001010 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001011 * @subsys_mask: mask of the subsystem ids whose files should be removed
1012 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07001013static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001014{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001015 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001016 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001017
Tejun Heob420ba72013-07-12 12:34:02 -07001018 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001019 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07001020
1021 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001022 continue;
1023 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -04001024 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001025 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026}
1027
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001029 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030{
Paul Menagebd89aab2007-10-18 23:40:44 -07001031 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001032 struct cgroup_subsys *ss;
Tejun Heo31261212013-06-28 17:07:30 -07001033 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001034
Tejun Heoace2bee2014-02-11 11:52:47 -05001035 lockdep_assert_held(&cgroup_tree_mutex);
1036 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001037
Paul Menageddbcc7e2007-10-18 23:39:30 -07001038 /* Check that any added subsystems are currently free */
Tejun Heo3ed80a62014-02-08 10:36:58 -05001039 for_each_subsys(ss, i)
1040 if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
1041 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001042
Tejun Heo31261212013-06-28 17:07:30 -07001043 ret = cgroup_populate_dir(cgrp, added_mask);
1044 if (ret)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001045 return ret;
Tejun Heo31261212013-06-28 17:07:30 -07001046
1047 /*
1048 * Nothing can fail from this point on. Remove files for the
1049 * removed subsystems and rebind each subsystem.
1050 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001051 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001052 cgroup_clear_dir(cgrp, removed_mask);
Tejun Heo4ac06012014-02-11 11:52:47 -05001053 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001054
Tejun Heo30159ec2013-06-25 11:53:37 -07001055 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001057
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001058 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001060 BUG_ON(cgroup_css(cgrp, ss));
1061 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1062 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001063
Tejun Heo73e80ed2013-08-13 11:01:55 -04001064 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001065 cgroup_css(cgroup_dummy_top, ss));
1066 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001067
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001068 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001069 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001070 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001071
Ben Blumcf5d5942010-03-10 15:22:09 -08001072 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001073 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001074 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001076 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1077 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001078
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001080 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001081
Tejun Heoca8bdca2013-08-26 18:40:56 -04001082 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001083 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1084
Tejun Heo9871bf92013-06-24 15:21:47 -07001085 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001086 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 }
1088 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089
Tejun Heo1672d042013-06-25 18:04:54 -07001090 /*
1091 * Mark @root has finished binding subsystems. @root->subsys_mask
1092 * now matches the bound subsystems.
1093 */
1094 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001095 kernfs_activate(cgrp->kn);
Tejun Heo1672d042013-06-25 18:04:54 -07001096
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097 return 0;
1098}
1099
Tejun Heo2bd59d42014-02-11 11:52:49 -05001100static int cgroup_show_options(struct seq_file *seq,
1101 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001103 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001105 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106
Tejun Heob85d2042013-12-06 15:11:57 -05001107 for_each_subsys(ss, ssid)
1108 if (root->subsys_mask & (1 << ssid))
1109 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001110 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1111 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001112 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001113 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001114 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001115 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001116
1117 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001118 if (strlen(root->release_agent_path))
1119 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001120 spin_unlock(&release_agent_path_lock);
1121
Tejun Heo2260e7f2012-11-19 08:13:38 -08001122 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001123 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001124 if (strlen(root->name))
1125 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001126 return 0;
1127}
1128
1129struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001130 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001132 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001133 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001134 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001135 /* User explicitly requested empty subsystem */
1136 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137};
1138
Ben Blumaae8aab2010-03-10 15:22:07 -08001139/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001140 * Convert a hierarchy specifier into a bitmask of subsystems and
1141 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1142 * array. This function takes refcounts on subsystems to be used, unless it
1143 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001144 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001145static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001146{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001147 char *token, *o = data;
1148 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001149 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001150 struct cgroup_subsys *ss;
1151 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001152
Ben Blumaae8aab2010-03-10 15:22:07 -08001153 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1154
Li Zefanf9ab5b52009-06-17 16:26:33 -07001155#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001156 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001157#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001158
Paul Menagec6d57f32009-09-23 15:56:19 -07001159 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001160
1161 while ((token = strsep(&o, ",")) != NULL) {
1162 if (!*token)
1163 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001164 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001165 /* Explicitly have no subsystems */
1166 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001167 continue;
1168 }
1169 if (!strcmp(token, "all")) {
1170 /* Mutually exclusive option 'all' + subsystem name */
1171 if (one_ss)
1172 return -EINVAL;
1173 all_ss = true;
1174 continue;
1175 }
Tejun Heo873fe092013-04-14 20:15:26 -07001176 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1177 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1178 continue;
1179 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001180 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001181 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001182 continue;
1183 }
1184 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001185 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001186 continue;
1187 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001188 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001189 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001190 continue;
1191 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001192 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001193 /* Specifying two release agents is forbidden */
1194 if (opts->release_agent)
1195 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001196 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001197 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001198 if (!opts->release_agent)
1199 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001200 continue;
1201 }
1202 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001203 const char *name = token + 5;
1204 /* Can't specify an empty name */
1205 if (!strlen(name))
1206 return -EINVAL;
1207 /* Must match [\w.-]+ */
1208 for (i = 0; i < strlen(name); i++) {
1209 char c = name[i];
1210 if (isalnum(c))
1211 continue;
1212 if ((c == '.') || (c == '-') || (c == '_'))
1213 continue;
1214 return -EINVAL;
1215 }
1216 /* Specifying two names is forbidden */
1217 if (opts->name)
1218 return -EINVAL;
1219 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001220 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001221 GFP_KERNEL);
1222 if (!opts->name)
1223 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001224
1225 continue;
1226 }
1227
Tejun Heo30159ec2013-06-25 11:53:37 -07001228 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001229 if (strcmp(token, ss->name))
1230 continue;
1231 if (ss->disabled)
1232 continue;
1233
1234 /* Mutually exclusive option 'all' + subsystem name */
1235 if (all_ss)
1236 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001237 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001238 one_ss = true;
1239
1240 break;
1241 }
1242 if (i == CGROUP_SUBSYS_COUNT)
1243 return -ENOENT;
1244 }
1245
1246 /*
1247 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001248 * otherwise if 'none', 'name=' and a subsystem name options
1249 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001250 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001251 if (all_ss || (!one_ss && !opts->none && !opts->name))
1252 for_each_subsys(ss, i)
1253 if (!ss->disabled)
1254 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001255
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001256 /* Consistency checks */
1257
Tejun Heo873fe092013-04-14 20:15:26 -07001258 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1259 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1260
1261 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1262 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1263 return -EINVAL;
1264 }
1265
1266 if (opts->cpuset_clone_children) {
1267 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1268 return -EINVAL;
1269 }
1270 }
1271
Li Zefanf9ab5b52009-06-17 16:26:33 -07001272 /*
1273 * Option noprefix was introduced just for backward compatibility
1274 * with the old cpuset, so we allow noprefix only if mounting just
1275 * the cpuset subsystem.
1276 */
Tejun Heo93438622013-04-14 20:15:25 -07001277 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001278 return -EINVAL;
1279
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001280
1281 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001282 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001283 return -EINVAL;
1284
1285 /*
1286 * We either have to specify by name or by subsystems. (So all
1287 * empty hierarchies must have a name).
1288 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001289 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001290 return -EINVAL;
1291
1292 return 0;
1293}
1294
Tejun Heo2bd59d42014-02-11 11:52:49 -05001295static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001296{
1297 int ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001298 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001299 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001300 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001301
Tejun Heo873fe092013-04-14 20:15:26 -07001302 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1303 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1304 return -EINVAL;
1305 }
1306
Tejun Heoace2bee2014-02-11 11:52:47 -05001307 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001308 mutex_lock(&cgroup_mutex);
1309
1310 /* See what subsystems are wanted */
1311 ret = parse_cgroupfs_options(data, &opts);
1312 if (ret)
1313 goto out_unlock;
1314
Tejun Heoa8a648c2013-06-24 15:21:47 -07001315 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001316 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1317 task_tgid_nr(current), current->comm);
1318
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001319 added_mask = opts.subsys_mask & ~root->subsys_mask;
1320 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001321
Ben Blumcf5d5942010-03-10 15:22:09 -08001322 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001323 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001324 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001325 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1326 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1327 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001329 goto out_unlock;
1330 }
1331
Tejun Heof172e672013-06-28 17:07:30 -07001332 /* remounting is not allowed for populated hierarchies */
1333 if (root->number_of_cgroups > 1) {
1334 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001335 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001336 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001337
Paul Menageddbcc7e2007-10-18 23:39:30 -07001338 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001339 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001341
Tejun Heo69e943b2014-02-08 10:36:58 -05001342 if (opts.release_agent) {
1343 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001344 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001345 spin_unlock(&release_agent_path_lock);
1346 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001347 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001348 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001349 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001350 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001351 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001352 return ret;
1353}
1354
Paul Menagecc31edc2008-10-18 20:28:04 -07001355static void init_cgroup_housekeeping(struct cgroup *cgrp)
1356{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001357 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001358 INIT_LIST_HEAD(&cgrp->sibling);
1359 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001360 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001361 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001362 INIT_LIST_HEAD(&cgrp->pidlists);
1363 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001364 cgrp->dummy_css.cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001365}
Paul Menagec6d57f32009-09-23 15:56:19 -07001366
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367static void init_cgroup_root(struct cgroupfs_root *root)
1368{
Paul Menagebd89aab2007-10-18 23:40:44 -07001369 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001370
Tejun Heo2bd59d42014-02-11 11:52:49 -05001371 atomic_set(&root->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001372 INIT_LIST_HEAD(&root->root_list);
1373 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001374 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001375 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001376 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001377 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378}
1379
Paul Menagec6d57f32009-09-23 15:56:19 -07001380static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1381{
1382 struct cgroupfs_root *root;
1383
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001384 if (!opts->subsys_mask && !opts->none)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001385 return ERR_PTR(-EINVAL);
Paul Menagec6d57f32009-09-23 15:56:19 -07001386
1387 root = kzalloc(sizeof(*root), GFP_KERNEL);
1388 if (!root)
1389 return ERR_PTR(-ENOMEM);
1390
1391 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001392
Tejun Heo1672d042013-06-25 18:04:54 -07001393 /*
1394 * We need to set @root->subsys_mask now so that @root can be
1395 * matched by cgroup_test_super() before it finishes
1396 * initialization; otherwise, competing mounts with the same
1397 * options may try to bind the same subsystems instead of waiting
1398 * for the first one leading to unexpected mount errors.
1399 * SUBSYS_BOUND will be set once actual binding is complete.
1400 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001401 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001402 root->flags = opts->flags;
1403 if (opts->release_agent)
1404 strcpy(root->release_agent_path, opts->release_agent);
1405 if (opts->name)
1406 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001407 if (opts->cpuset_clone_children)
1408 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001409 return root;
1410}
1411
Tejun Heod427dfe2014-02-11 11:52:48 -05001412static int cgroup_setup_root(struct cgroupfs_root *root)
1413{
1414 LIST_HEAD(tmp_links);
Tejun Heod427dfe2014-02-11 11:52:48 -05001415 struct cgroup *root_cgrp = &root->top_cgroup;
Tejun Heod427dfe2014-02-11 11:52:48 -05001416 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001417 int i, ret;
1418
1419 lockdep_assert_held(&cgroup_tree_mutex);
1420 lockdep_assert_held(&cgroup_mutex);
Tejun Heod427dfe2014-02-11 11:52:48 -05001421
1422 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1423 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001424 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001425 root_cgrp->id = ret;
1426
Tejun Heod427dfe2014-02-11 11:52:48 -05001427 /*
1428 * We're accessing css_set_count without locking css_set_lock here,
1429 * but that's OK - it can only be increased by someone holding
1430 * cgroup_lock, and that's us. The worst that can happen is that we
1431 * have some link structures left over
1432 */
1433 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1434 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001435 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001436
1437 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1438 ret = cgroup_init_root_id(root, 2, 0);
1439 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001440 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001441
Tejun Heo2bd59d42014-02-11 11:52:49 -05001442 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1443 KERNFS_ROOT_CREATE_DEACTIVATED,
1444 root_cgrp);
1445 if (IS_ERR(root->kf_root)) {
1446 ret = PTR_ERR(root->kf_root);
1447 goto exit_root_id;
1448 }
1449 root_cgrp->kn = root->kf_root->kn;
Tejun Heod427dfe2014-02-11 11:52:48 -05001450
1451 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1452 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001453 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001454
1455 ret = rebind_subsystems(root, root->subsys_mask, 0);
1456 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001457 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001458
1459 /*
1460 * There must be no failure case after here, since rebinding takes
1461 * care of subsystems' refcounts, which are explicitly dropped in
1462 * the failure exit path.
1463 */
1464 list_add(&root->root_list, &cgroup_roots);
1465 cgroup_root_count++;
1466
1467 /*
1468 * Link the top cgroup in this hierarchy into all the css_set
1469 * objects.
1470 */
1471 write_lock(&css_set_lock);
1472 hash_for_each(css_set_table, i, cset, hlist)
1473 link_css_set(&tmp_links, cset, root_cgrp);
1474 write_unlock(&css_set_lock);
1475
1476 BUG_ON(!list_empty(&root_cgrp->children));
1477 BUG_ON(root->number_of_cgroups != 1);
1478
Tejun Heo2bd59d42014-02-11 11:52:49 -05001479 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001480 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001481 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001482
Tejun Heo2bd59d42014-02-11 11:52:49 -05001483destroy_root:
1484 kernfs_destroy_root(root->kf_root);
1485 root->kf_root = NULL;
1486exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001487 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001488out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001489 free_cgrp_cset_links(&tmp_links);
1490 return ret;
1491}
1492
Al Virof7e83572010-07-26 13:23:11 +04001493static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001494 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001495 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001496{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001497 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001498 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001499 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001500 int ret;
1501
1502 mutex_lock(&cgroup_tree_mutex);
1503 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001504
1505 /* First find the desired set of subsystems */
1506 ret = parse_cgroupfs_options(data, &opts);
Paul Menagec6d57f32009-09-23 15:56:19 -07001507 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001508 goto out_unlock;
Paul Menagec6d57f32009-09-23 15:56:19 -07001509
Tejun Heo2bd59d42014-02-11 11:52:49 -05001510 /* look for a matching existing root */
1511 for_each_active_root(root) {
1512 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513
Paul Menagec6d57f32009-09-23 15:56:19 -07001514 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001515 * If we asked for a name then it must match. Also, if
1516 * name matches but sybsys_mask doesn't, we should fail.
1517 * Remember whether name matched.
Paul Menagec6d57f32009-09-23 15:56:19 -07001518 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001519 if (opts.name) {
1520 if (strcmp(opts.name, root->name))
1521 continue;
1522 name_match = true;
1523 }
1524
1525 /*
1526 * If we asked for subsystems (or explicitly for no
1527 * subsystems) then they must match.
1528 */
1529 if ((opts.subsys_mask || opts.none) &&
1530 (opts.subsys_mask != root->subsys_mask)) {
1531 if (!name_match)
1532 continue;
1533 ret = -EBUSY;
1534 goto out_unlock;
1535 }
Tejun Heo873fe092013-04-14 20:15:26 -07001536
Tejun Heoc7ba8282013-06-29 14:06:10 -07001537 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001538 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1539 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1540 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001541 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001542 } else {
1543 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1544 }
Tejun Heo873fe092013-04-14 20:15:26 -07001545 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001546
1547 cgroup_get_root(root);
1548 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549 }
1550
Tejun Heo2bd59d42014-02-11 11:52:49 -05001551 /* no such thing, create a new one */
1552 root = cgroup_root_from_opts(&opts);
1553 if (IS_ERR(root)) {
1554 ret = PTR_ERR(root);
1555 goto out_unlock;
1556 }
1557
1558 ret = cgroup_setup_root(root);
1559 if (ret)
1560 cgroup_free_root(root);
1561
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001562out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001563 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001564 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001565
Paul Menagec6d57f32009-09-23 15:56:19 -07001566 kfree(opts.release_agent);
1567 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001568
Tejun Heo2bd59d42014-02-11 11:52:49 -05001569 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001570 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001571
1572 dentry = kernfs_mount(fs_type, flags, root->kf_root);
1573 if (IS_ERR(dentry))
1574 cgroup_put_root(root);
1575 return dentry;
1576}
1577
1578static void cgroup_kill_sb(struct super_block *sb)
1579{
1580 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
1581 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
1582
1583 cgroup_put_root(root);
1584 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001585}
1586
Paul Menageddbcc7e2007-10-18 23:39:30 -07001587static struct file_system_type cgroup_fs_type = {
1588 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001589 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001590 .kill_sb = cgroup_kill_sb,
1591};
1592
Greg KH676db4a2010-08-05 13:53:35 -07001593static struct kobject *cgroup_kobj;
1594
Li Zefana043e3b2008-02-23 15:24:09 -08001595/**
1596 * cgroup_path - generate the path of a cgroup
1597 * @cgrp: the cgroup in question
1598 * @buf: the buffer to write the path into
1599 * @buflen: the length of the buffer
1600 *
Li Zefan65dff752013-03-01 15:01:56 +08001601 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1602 *
1603 * We can't generate cgroup path using dentry->d_name, as accessing
1604 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1605 * inode's i_mutex, while on the other hand cgroup_path() can be called
1606 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001608int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609{
Li Zefan65dff752013-03-01 15:01:56 +08001610 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001611 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001612
Tejun Heoda1f2962013-04-14 10:32:19 -07001613 if (!cgrp->parent) {
1614 if (strlcpy(buf, "/", buflen) >= buflen)
1615 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001616 return 0;
1617 }
1618
Tao Ma316eb662012-11-08 21:36:38 +08001619 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001620 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001621
Li Zefan65dff752013-03-01 15:01:56 +08001622 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001623 do {
Li Zefan65dff752013-03-01 15:01:56 +08001624 const char *name = cgroup_name(cgrp);
1625 int len;
1626
1627 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001629 goto out;
1630 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001631
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001633 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001634 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001635
1636 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001637 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001638 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001639 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001640out:
1641 rcu_read_unlock();
1642 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001643}
Ben Blum67523c42010-03-10 15:22:11 -08001644EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001645
Tejun Heo857a2be2013-04-14 20:50:08 -07001646/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001647 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001648 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001649 * @buf: the buffer to write the path into
1650 * @buflen: the length of the buffer
1651 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001652 * Determine @task's cgroup on the first (the one with the lowest non-zero
1653 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1654 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1655 * cgroup controller callbacks.
1656 *
1657 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001658 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001659int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001660{
1661 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001662 struct cgroup *cgrp;
1663 int hierarchy_id = 1, ret = 0;
1664
1665 if (buflen < 2)
1666 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001667
1668 mutex_lock(&cgroup_mutex);
1669
Tejun Heo913ffdb2013-07-11 16:34:48 -07001670 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1671
Tejun Heo857a2be2013-04-14 20:50:08 -07001672 if (root) {
1673 cgrp = task_cgroup_from_root(task, root);
1674 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001675 } else {
1676 /* if no hierarchy exists, everyone is in "/" */
1677 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001678 }
1679
1680 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001681 return ret;
1682}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001683EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001684
Ben Blum74a11662011-05-26 16:25:20 -07001685/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001686 * Control Group taskset
1687 */
Tejun Heo134d3372011-12-12 18:12:21 -08001688struct task_and_cgroup {
1689 struct task_struct *task;
1690 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001691 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001692};
1693
Tejun Heo2f7ee562011-12-12 18:12:21 -08001694struct cgroup_taskset {
1695 struct task_and_cgroup single;
1696 struct flex_array *tc_array;
1697 int tc_array_len;
1698 int idx;
1699 struct cgroup *cur_cgrp;
1700};
1701
1702/**
1703 * cgroup_taskset_first - reset taskset and return the first task
1704 * @tset: taskset of interest
1705 *
1706 * @tset iteration is initialized and the first task is returned.
1707 */
1708struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1709{
1710 if (tset->tc_array) {
1711 tset->idx = 0;
1712 return cgroup_taskset_next(tset);
1713 } else {
1714 tset->cur_cgrp = tset->single.cgrp;
1715 return tset->single.task;
1716 }
1717}
1718EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1719
1720/**
1721 * cgroup_taskset_next - iterate to the next task in taskset
1722 * @tset: taskset of interest
1723 *
1724 * Return the next task in @tset. Iteration must have been initialized
1725 * with cgroup_taskset_first().
1726 */
1727struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1728{
1729 struct task_and_cgroup *tc;
1730
1731 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1732 return NULL;
1733
1734 tc = flex_array_get(tset->tc_array, tset->idx++);
1735 tset->cur_cgrp = tc->cgrp;
1736 return tc->task;
1737}
1738EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1739
1740/**
Tejun Heod99c8722013-08-08 20:11:27 -04001741 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001742 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001743 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001744 *
Tejun Heod99c8722013-08-08 20:11:27 -04001745 * Return the css for the current (last returned) task of @tset for
1746 * subsystem specified by @subsys_id. This function must be preceded by
1747 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001748 */
Tejun Heod99c8722013-08-08 20:11:27 -04001749struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1750 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001751{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001752 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001753}
Tejun Heod99c8722013-08-08 20:11:27 -04001754EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001755
1756/**
1757 * cgroup_taskset_size - return the number of tasks in taskset
1758 * @tset: taskset of interest
1759 */
1760int cgroup_taskset_size(struct cgroup_taskset *tset)
1761{
1762 return tset->tc_array ? tset->tc_array_len : 1;
1763}
1764EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1765
1766
Ben Blum74a11662011-05-26 16:25:20 -07001767/*
1768 * cgroup_task_migrate - move a task from one cgroup to another.
1769 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001770 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001771 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001772static void cgroup_task_migrate(struct cgroup *old_cgrp,
1773 struct task_struct *tsk,
1774 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001775{
Tejun Heo5abb8852013-06-12 21:04:49 -07001776 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001777
1778 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001779 * We are synchronized through threadgroup_lock() against PF_EXITING
1780 * setting such that we can't race against cgroup_exit() changing the
1781 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001782 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001783 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001784 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001785
Ben Blum74a11662011-05-26 16:25:20 -07001786 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001787 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001788 task_unlock(tsk);
1789
1790 /* Update the css_set linked lists if we're using them */
1791 write_lock(&css_set_lock);
1792 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001793 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001794 write_unlock(&css_set_lock);
1795
1796 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001797 * We just gained a reference on old_cset by taking it from the
1798 * task. As trading it for new_cset is protected by cgroup_mutex,
1799 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001800 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001801 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1802 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001803}
1804
Li Zefana043e3b2008-02-23 15:24:09 -08001805/**
Li Zefan081aa452013-03-13 09:17:09 +08001806 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001807 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001808 * @tsk: the task or the leader of the threadgroup to be attached
1809 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001810 *
Tejun Heo257058a2011-12-12 18:12:21 -08001811 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001812 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001813 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001814static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1815 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001816{
1817 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001818 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001819 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001820 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001821 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001822 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001823 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001824 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001825
1826 /*
1827 * step 0: in order to do expensive, possibly blocking operations for
1828 * every thread, we cannot iterate the thread group list, since it needs
1829 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001830 * group - group_rwsem prevents new threads from appearing, and if
1831 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001832 */
Li Zefan081aa452013-03-13 09:17:09 +08001833 if (threadgroup)
1834 group_size = get_nr_threads(tsk);
1835 else
1836 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001837 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001838 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001839 if (!group)
1840 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001841 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001842 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001843 if (retval)
1844 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001845
Ben Blum74a11662011-05-26 16:25:20 -07001846 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001847 /*
1848 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1849 * already PF_EXITING could be freed from underneath us unless we
1850 * take an rcu_read_lock.
1851 */
1852 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001853 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001854 struct task_and_cgroup ent;
1855
Tejun Heocd3d0952011-12-12 18:12:21 -08001856 /* @tsk either already exited or can't exit until the end */
1857 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001858 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001859
Ben Blum74a11662011-05-26 16:25:20 -07001860 /* as per above, nr_threads may decrease, but not increase. */
1861 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001862 ent.task = tsk;
1863 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001864 /* nothing to do if this task is already in the cgroup */
1865 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001866 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001867 /*
1868 * saying GFP_ATOMIC has no effect here because we did prealloc
1869 * earlier, but it's good form to communicate our expectations.
1870 */
Tejun Heo134d3372011-12-12 18:12:21 -08001871 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07001872 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07001873 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08001874 next:
Li Zefan081aa452013-03-13 09:17:09 +08001875 if (!threadgroup)
1876 break;
Ben Blum74a11662011-05-26 16:25:20 -07001877 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001878 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07001879 /* remember the number of threads in the array for later. */
1880 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001881 tset.tc_array = group;
1882 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001883
Tejun Heo134d3372011-12-12 18:12:21 -08001884 /* methods shouldn't be called if no task is actually migrating */
1885 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001886 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08001887 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08001888
Ben Blum74a11662011-05-26 16:25:20 -07001889 /*
1890 * step 1: check that we can legitimately attach to the cgroup.
1891 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001892 for_each_css(css, i, cgrp) {
1893 if (css->ss->can_attach) {
1894 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001895 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001896 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07001897 goto out_cancel_attach;
1898 }
1899 }
Ben Blum74a11662011-05-26 16:25:20 -07001900 }
1901
1902 /*
1903 * step 2: make sure css_sets exist for all threads to be migrated.
1904 * we use find_css_set, which allocates a new one if necessary.
1905 */
Ben Blum74a11662011-05-26 16:25:20 -07001906 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07001907 struct css_set *old_cset;
1908
Tejun Heo134d3372011-12-12 18:12:21 -08001909 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001910 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001911 tc->cset = find_css_set(old_cset, cgrp);
1912 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001913 retval = -ENOMEM;
1914 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07001915 }
1916 }
1917
1918 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001919 * step 3: now that we're guaranteed success wrt the css_sets,
1920 * proceed to move all tasks to the new cgroup. There are no
1921 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07001922 */
Ben Blum74a11662011-05-26 16:25:20 -07001923 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08001924 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001925 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07001926 }
1927 /* nothing is sensitive to fork() after this point. */
1928
1929 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001930 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07001931 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001932 for_each_css(css, i, cgrp)
1933 if (css->ss->attach)
1934 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001935
1936 /*
1937 * step 5: success! and cleanup
1938 */
Ben Blum74a11662011-05-26 16:25:20 -07001939 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001940out_put_css_set_refs:
1941 if (retval) {
1942 for (i = 0; i < group_size; i++) {
1943 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001944 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001945 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001946 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001947 }
Ben Blum74a11662011-05-26 16:25:20 -07001948 }
1949out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07001950 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001951 for_each_css(css, i, cgrp) {
1952 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07001953 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001954 if (css->ss->cancel_attach)
1955 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001956 }
1957 }
Ben Blum74a11662011-05-26 16:25:20 -07001958out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07001959 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07001960 return retval;
1961}
1962
1963/*
1964 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08001965 * function to attach either it or all tasks in its threadgroup. Will lock
1966 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07001967 */
1968static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001969{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001970 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001971 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001972 int ret;
1973
Ben Blum74a11662011-05-26 16:25:20 -07001974 if (!cgroup_lock_live_group(cgrp))
1975 return -ENODEV;
1976
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001977retry_find_task:
1978 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001979 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001980 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07001981 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07001982 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001983 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001984 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001985 }
Ben Blum74a11662011-05-26 16:25:20 -07001986 /*
1987 * even if we're attaching all tasks in the thread group, we
1988 * only need to check permissions on one of them.
1989 */
David Howellsc69e8d92008-11-14 10:39:19 +11001990 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07001991 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
1992 !uid_eq(cred->euid, tcred->uid) &&
1993 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001994 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001995 ret = -EACCES;
1996 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001997 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001998 } else
1999 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002000
2001 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002002 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002003
2004 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002005 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002006 * trapped in a cpuset, or RT worker may be born in a cgroup
2007 * with no rt_runtime allocated. Just say no.
2008 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002009 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002010 ret = -EINVAL;
2011 rcu_read_unlock();
2012 goto out_unlock_cgroup;
2013 }
2014
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002015 get_task_struct(tsk);
2016 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002017
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002018 threadgroup_lock(tsk);
2019 if (threadgroup) {
2020 if (!thread_group_leader(tsk)) {
2021 /*
2022 * a race with de_thread from another thread's exec()
2023 * may strip us of our leadership, if this happens,
2024 * there is no choice but to throw this task away and
2025 * try again; this is
2026 * "double-double-toil-and-trouble-check locking".
2027 */
2028 threadgroup_unlock(tsk);
2029 put_task_struct(tsk);
2030 goto retry_find_task;
2031 }
Li Zefan081aa452013-03-13 09:17:09 +08002032 }
2033
2034 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2035
Tejun Heocd3d0952011-12-12 18:12:21 -08002036 threadgroup_unlock(tsk);
2037
Paul Menagebbcb81d2007-10-18 23:39:32 -07002038 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002039out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002040 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002041 return ret;
2042}
2043
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002044/**
2045 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2046 * @from: attach to all cgroups of a given task
2047 * @tsk: the task to be attached
2048 */
2049int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2050{
2051 struct cgroupfs_root *root;
2052 int retval = 0;
2053
Tejun Heo47cfcd02013-04-07 09:29:51 -07002054 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002055 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002056 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002057
Li Zefan6f4b7e62013-07-31 16:18:36 +08002058 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002059 if (retval)
2060 break;
2061 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002062 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002063
2064 return retval;
2065}
2066EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2067
Tejun Heo182446d2013-08-08 20:11:24 -04002068static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2069 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002070{
Tejun Heo182446d2013-08-08 20:11:24 -04002071 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002072}
2073
Tejun Heo182446d2013-08-08 20:11:24 -04002074static int cgroup_procs_write(struct cgroup_subsys_state *css,
2075 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002076{
Tejun Heo182446d2013-08-08 20:11:24 -04002077 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002078}
2079
Tejun Heo182446d2013-08-08 20:11:24 -04002080static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2081 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002082{
Tejun Heo5f469902014-02-11 11:52:48 -05002083 struct cgroupfs_root *root = css->cgroup->root;
2084
2085 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002086 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002087 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002088 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002089 strlcpy(root->release_agent_path, buffer,
2090 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002091 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002092 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002093 return 0;
2094}
2095
Tejun Heo2da8ca82013-12-05 12:28:04 -05002096static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002097{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002098 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002099
Paul Menagee788e062008-07-25 01:46:59 -07002100 if (!cgroup_lock_live_group(cgrp))
2101 return -ENODEV;
2102 seq_puts(seq, cgrp->root->release_agent_path);
2103 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002104 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002105 return 0;
2106}
2107
Tejun Heo2da8ca82013-12-05 12:28:04 -05002108static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002109{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002110 struct cgroup *cgrp = seq_css(seq)->cgroup;
2111
2112 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002113 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002114}
2115
Tejun Heo2bd59d42014-02-11 11:52:49 -05002116static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2117 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002118{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002119 struct cgroup *cgrp = of->kn->parent->priv;
2120 struct cftype *cft = of->kn->priv;
2121 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002122 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002123
Tejun Heo2bd59d42014-02-11 11:52:49 -05002124 /*
2125 * kernfs guarantees that a file isn't deleted with operations in
2126 * flight, which means that the matching css is and stays alive and
2127 * doesn't need to be pinned. The RCU locking is not necessary
2128 * either. It's just for the convenience of using cgroup_css().
2129 */
2130 rcu_read_lock();
2131 css = cgroup_css(cgrp, cft->ss);
2132 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002133
Tejun Heoa742c592013-12-05 12:28:03 -05002134 if (cft->write_string) {
2135 ret = cft->write_string(css, cft, strstrip(buf));
2136 } else if (cft->write_u64) {
2137 unsigned long long v;
2138 ret = kstrtoull(buf, 0, &v);
2139 if (!ret)
2140 ret = cft->write_u64(css, cft, v);
2141 } else if (cft->write_s64) {
2142 long long v;
2143 ret = kstrtoll(buf, 0, &v);
2144 if (!ret)
2145 ret = cft->write_s64(css, cft, v);
2146 } else if (cft->trigger) {
2147 ret = cft->trigger(css, (unsigned int)cft->private);
2148 } else {
2149 ret = -EINVAL;
2150 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002151
Tejun Heoa742c592013-12-05 12:28:03 -05002152 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002153}
2154
Tejun Heo6612f052013-12-05 12:28:04 -05002155static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002156{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002157 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002158}
2159
2160static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2161{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002162 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002163}
2164
2165static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2166{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002167 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002168}
2169
2170static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2171{
Tejun Heo7da11272013-12-05 12:28:04 -05002172 struct cftype *cft = seq_cft(m);
2173 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002174
Tejun Heo2da8ca82013-12-05 12:28:04 -05002175 if (cft->seq_show)
2176 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002177
Tejun Heo896f5192013-12-05 12:28:04 -05002178 if (cft->read_u64)
2179 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2180 else if (cft->read_s64)
2181 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2182 else
2183 return -EINVAL;
2184 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002185}
2186
Tejun Heo2bd59d42014-02-11 11:52:49 -05002187static struct kernfs_ops cgroup_kf_single_ops = {
2188 .atomic_write_len = PAGE_SIZE,
2189 .write = cgroup_file_write,
2190 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002191};
2192
Tejun Heo2bd59d42014-02-11 11:52:49 -05002193static struct kernfs_ops cgroup_kf_ops = {
2194 .atomic_write_len = PAGE_SIZE,
2195 .write = cgroup_file_write,
2196 .seq_start = cgroup_seqfile_start,
2197 .seq_next = cgroup_seqfile_next,
2198 .seq_stop = cgroup_seqfile_stop,
2199 .seq_show = cgroup_seqfile_show,
2200};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002201
2202/*
2203 * cgroup_rename - Only allow simple rename of directories in place.
2204 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002205static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2206 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002207{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002208 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002209 struct cgroup_name *name, *old_name;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002210 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002211
Tejun Heo2bd59d42014-02-11 11:52:49 -05002212 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002213 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002214 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002215 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002216
Tejun Heo6db8e852013-06-14 11:18:22 -07002217 /*
2218 * This isn't a proper migration and its usefulness is very
2219 * limited. Disallow if sane_behavior.
2220 */
2221 if (cgroup_sane_behavior(cgrp))
2222 return -EPERM;
2223
Tejun Heo2bd59d42014-02-11 11:52:49 -05002224 name = cgroup_alloc_name(new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002225 if (!name)
2226 return -ENOMEM;
2227
Tejun Heo2bd59d42014-02-11 11:52:49 -05002228 mutex_lock(&cgroup_tree_mutex);
2229 mutex_lock(&cgroup_mutex);
2230
2231 ret = kernfs_rename(kn, new_parent, new_name_str);
2232 if (!ret) {
2233 old_name = rcu_dereference_protected(cgrp->name, true);
2234 rcu_assign_pointer(cgrp->name, name);
2235 } else {
2236 old_name = name;
Li Zefan65dff752013-03-01 15:01:56 +08002237 }
2238
Tejun Heo2bd59d42014-02-11 11:52:49 -05002239 mutex_unlock(&cgroup_mutex);
2240 mutex_unlock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002241
2242 kfree_rcu(old_name, rcu_head);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002243 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002244}
2245
Tejun Heo2bb566c2013-08-08 20:11:23 -04002246static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002247{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002248 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002249 struct kernfs_node *kn;
2250 struct lock_class_key *key = NULL;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002251
Tejun Heo2bd59d42014-02-11 11:52:49 -05002252#ifdef CONFIG_DEBUG_LOCK_ALLOC
2253 key = &cft->lockdep_key;
2254#endif
2255 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2256 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2257 NULL, false, key);
2258 if (IS_ERR(kn))
2259 return PTR_ERR(kn);
2260 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002261}
2262
Tejun Heob1f28d32013-06-28 16:24:10 -07002263/**
2264 * cgroup_addrm_files - add or remove files to a cgroup directory
2265 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002266 * @cfts: array of cftypes to be added
2267 * @is_add: whether to add or remove
2268 *
2269 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002270 * For removals, this function never fails. If addition fails, this
2271 * function doesn't remove files already added. The caller is responsible
2272 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002273 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002274static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2275 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002276{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002277 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002278 int ret;
2279
Tejun Heoace2bee2014-02-11 11:52:47 -05002280 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002281
2282 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002283 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002284 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2285 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002286 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2287 continue;
2288 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2289 continue;
2290
Li Zefan2739d3c2013-01-21 18:18:33 +08002291 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002292 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002293 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002294 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002295 cft->name, ret);
2296 return ret;
2297 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002298 } else {
2299 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002300 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002301 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002302 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002303}
2304
Tejun Heo8e3f6542012-04-01 12:09:55 -07002305static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002306 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002307{
2308 /*
2309 * Thanks to the entanglement with vfs inode locking, we can't walk
2310 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002311 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2312 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002313 */
Tejun Heoace2bee2014-02-11 11:52:47 -05002314 mutex_lock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002315 mutex_lock(&cgroup_mutex);
2316}
2317
Tejun Heo2bb566c2013-08-08 20:11:23 -04002318static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002319 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002320{
2321 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002322 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002323 struct cgroup *root = &ss->root->top_cgroup;
Tejun Heo59f52962014-02-11 11:52:49 -05002324 struct cgroup *prev = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002325 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002326 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002327 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002328
Tejun Heo4ac06012014-02-11 11:52:47 -05002329 mutex_unlock(&cgroup_mutex);
2330
Tejun Heo8e3f6542012-04-01 12:09:55 -07002331 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002332 if (!cfts || ss->root == &cgroup_dummy_root) {
Tejun Heoace2bee2014-02-11 11:52:47 -05002333 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002334 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002335 }
2336
Tejun Heo2bd59d42014-02-11 11:52:49 -05002337 cgroup_get_root(ss->root);
2338
Li Zefane8c82d22013-06-18 18:48:37 +08002339 /*
2340 * All cgroups which are created after we drop cgroup_mutex will
2341 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002342 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002343 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002344 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002345
Li Zefane8c82d22013-06-18 18:48:37 +08002346 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002347 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002348 struct cgroup *cgrp = css->cgroup;
2349
Li Zefane8c82d22013-06-18 18:48:37 +08002350 if (cgroup_is_dead(cgrp))
2351 continue;
2352
Tejun Heo59f52962014-02-11 11:52:49 -05002353 cgroup_get(cgrp);
2354 if (prev)
2355 cgroup_put(prev);
2356 prev = cgrp;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002357
Tejun Heo2bd59d42014-02-11 11:52:49 -05002358 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002359 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002360 if (is_add)
2361 kernfs_activate(cgrp->kn);
2362 }
Tejun Heo9ccece82013-06-28 16:24:11 -07002363 if (ret)
2364 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002365 }
Tejun Heoace2bee2014-02-11 11:52:47 -05002366 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo59f52962014-02-11 11:52:49 -05002367 cgroup_put(prev);
2368 cgroup_put_root(ss->root);
Tejun Heo9ccece82013-06-28 16:24:11 -07002369 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002370}
2371
Tejun Heo2da440a2014-02-11 11:52:48 -05002372static void cgroup_exit_cftypes(struct cftype *cfts)
2373{
2374 struct cftype *cft;
2375
Tejun Heo2bd59d42014-02-11 11:52:49 -05002376 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2377 /* free copy for custom atomic_write_len, see init_cftypes() */
2378 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2379 kfree(cft->kf_ops);
2380 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002381 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002382 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002383}
2384
Tejun Heo2bd59d42014-02-11 11:52:49 -05002385static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002386{
2387 struct cftype *cft;
2388
Tejun Heo2bd59d42014-02-11 11:52:49 -05002389 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2390 struct kernfs_ops *kf_ops;
2391
2392 if (cft->seq_start)
2393 kf_ops = &cgroup_kf_ops;
2394 else
2395 kf_ops = &cgroup_kf_single_ops;
2396
2397 /*
2398 * Ugh... if @cft wants a custom max_write_len, we need to
2399 * make a copy of kf_ops to set its atomic_write_len.
2400 */
2401 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2402 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2403 if (!kf_ops) {
2404 cgroup_exit_cftypes(cfts);
2405 return -ENOMEM;
2406 }
2407 kf_ops->atomic_write_len = cft->max_write_len;
2408 }
2409
2410 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002411 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002412 }
2413
2414 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002415}
2416
Tejun Heo8e3f6542012-04-01 12:09:55 -07002417/**
2418 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2419 * @ss: target cgroup subsystem
2420 * @cfts: zero-length name terminated array of cftypes
2421 *
2422 * Register @cfts to @ss. Files described by @cfts are created for all
2423 * existing cgroups to which @ss is attached and all future cgroups will
2424 * have them too. This function can be called anytime whether @ss is
2425 * attached or not.
2426 *
2427 * Returns 0 on successful registration, -errno on failure. Note that this
2428 * function currently returns 0 as long as @cfts registration is successful
2429 * even if some file creation attempts on existing cgroups fail.
2430 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002431int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002432{
2433 struct cftype_set *set;
Tejun Heo9ccece82013-06-28 16:24:11 -07002434 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002435
2436 set = kzalloc(sizeof(*set), GFP_KERNEL);
2437 if (!set)
2438 return -ENOMEM;
2439
Tejun Heo2bd59d42014-02-11 11:52:49 -05002440 ret = cgroup_init_cftypes(ss, cfts);
2441 if (ret)
2442 return ret;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002443
Tejun Heo8e3f6542012-04-01 12:09:55 -07002444 cgroup_cfts_prepare();
2445 set->cfts = cfts;
2446 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002447 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002448 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002449 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002450 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002451}
2452EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2453
Li Zefana043e3b2008-02-23 15:24:09 -08002454/**
Tejun Heo79578622012-04-01 12:09:56 -07002455 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002456 * @cfts: zero-length name terminated array of cftypes
2457 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002458 * Unregister @cfts. Files described by @cfts are removed from all
2459 * existing cgroups and all future cgroups won't have them either. This
2460 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002461 *
2462 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002463 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002464 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002465int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002466{
Tejun Heo2da440a2014-02-11 11:52:48 -05002467 struct cftype *found = NULL;
Tejun Heo79578622012-04-01 12:09:56 -07002468 struct cftype_set *set;
2469
Tejun Heo2bb566c2013-08-08 20:11:23 -04002470 if (!cfts || !cfts[0].ss)
2471 return -ENOENT;
2472
Tejun Heo79578622012-04-01 12:09:56 -07002473 cgroup_cfts_prepare();
2474
Tejun Heo2bb566c2013-08-08 20:11:23 -04002475 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002476 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002477 list_del(&set->node);
2478 kfree(set);
Tejun Heo2da440a2014-02-11 11:52:48 -05002479 found = cfts;
2480 break;
Tejun Heo79578622012-04-01 12:09:56 -07002481 }
2482 }
2483
Tejun Heo2da440a2014-02-11 11:52:48 -05002484 cgroup_cfts_commit(found, false);
2485 cgroup_exit_cftypes(cfts);
2486 return found ? 0 : -ENOENT;
Tejun Heo79578622012-04-01 12:09:56 -07002487}
2488
2489/**
Li Zefana043e3b2008-02-23 15:24:09 -08002490 * cgroup_task_count - count the number of tasks in a cgroup.
2491 * @cgrp: the cgroup in question
2492 *
2493 * Return the number of tasks in the cgroup.
2494 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002495int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002496{
2497 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002498 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002499
Paul Menage817929e2007-10-18 23:39:36 -07002500 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002501 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2502 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002503 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002504 return count;
2505}
2506
2507/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002508 * To reduce the fork() overhead for systems that are not actually using
2509 * their cgroups capability, we don't maintain the lists running through
2510 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002511 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002512 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002513static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002514{
2515 struct task_struct *p, *g;
2516 write_lock(&css_set_lock);
2517 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002518 /*
2519 * We need tasklist_lock because RCU is not safe against
2520 * while_each_thread(). Besides, a forking task that has passed
2521 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2522 * is not guaranteed to have its child immediately visible in the
2523 * tasklist if we walk through it with RCU.
2524 */
2525 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002526 do_each_thread(g, p) {
2527 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002528 /*
2529 * We should check if the process is exiting, otherwise
2530 * it will race with cgroup_exit() in that the list
2531 * entry won't be deleted though the process has exited.
2532 */
2533 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002534 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002535 task_unlock(p);
2536 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002537 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002538 write_unlock(&css_set_lock);
2539}
2540
Tejun Heo574bd9f2012-11-09 09:12:29 -08002541/**
Tejun Heo492eb212013-08-08 20:11:25 -04002542 * css_next_child - find the next child of a given css
2543 * @pos_css: the current position (%NULL to initiate traversal)
2544 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002545 *
Tejun Heo492eb212013-08-08 20:11:25 -04002546 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002547 * under either cgroup_mutex or RCU read lock. The only requirement is
2548 * that @parent_css and @pos_css are accessible. The next sibling is
2549 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002550 */
Tejun Heo492eb212013-08-08 20:11:25 -04002551struct cgroup_subsys_state *
2552css_next_child(struct cgroup_subsys_state *pos_css,
2553 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002554{
Tejun Heo492eb212013-08-08 20:11:25 -04002555 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2556 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002557 struct cgroup *next;
2558
Tejun Heoace2bee2014-02-11 11:52:47 -05002559 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002560
2561 /*
2562 * @pos could already have been removed. Once a cgroup is removed,
2563 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002564 * changes. As CGRP_DEAD assertion is serialized and happens
2565 * before the cgroup is taken off the ->sibling list, if we see it
2566 * unasserted, it's guaranteed that the next sibling hasn't
2567 * finished its grace period even if it's already removed, and thus
2568 * safe to dereference from this RCU critical section. If
2569 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2570 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002571 *
2572 * If @pos is dead, its next pointer can't be dereferenced;
2573 * however, as each cgroup is given a monotonically increasing
2574 * unique serial number and always appended to the sibling list,
2575 * the next one can be found by walking the parent's children until
2576 * we see a cgroup with higher serial number than @pos's. While
2577 * this path can be slower, it's taken only when either the current
2578 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002579 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002580 if (!pos) {
2581 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2582 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002583 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002584 } else {
2585 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2586 if (next->serial_nr > pos->serial_nr)
2587 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002588 }
2589
Tejun Heo492eb212013-08-08 20:11:25 -04002590 if (&next->sibling == &cgrp->children)
2591 return NULL;
2592
Tejun Heoca8bdca2013-08-26 18:40:56 -04002593 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002594}
Tejun Heo492eb212013-08-08 20:11:25 -04002595EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002596
2597/**
Tejun Heo492eb212013-08-08 20:11:25 -04002598 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002599 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002600 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002601 *
Tejun Heo492eb212013-08-08 20:11:25 -04002602 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002603 * to visit for pre-order traversal of @root's descendants. @root is
2604 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002605 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002606 * While this function requires cgroup_mutex or RCU read locking, it
2607 * doesn't require the whole traversal to be contained in a single critical
2608 * section. This function will return the correct next descendant as long
2609 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002610 */
Tejun Heo492eb212013-08-08 20:11:25 -04002611struct cgroup_subsys_state *
2612css_next_descendant_pre(struct cgroup_subsys_state *pos,
2613 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002614{
Tejun Heo492eb212013-08-08 20:11:25 -04002615 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002616
Tejun Heoace2bee2014-02-11 11:52:47 -05002617 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002618
Tejun Heobd8815a2013-08-08 20:11:27 -04002619 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002620 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002621 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002622
2623 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002624 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002625 if (next)
2626 return next;
2627
2628 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002629 while (pos != root) {
2630 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002631 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002632 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002633 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002634 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002635
2636 return NULL;
2637}
Tejun Heo492eb212013-08-08 20:11:25 -04002638EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002639
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002640/**
Tejun Heo492eb212013-08-08 20:11:25 -04002641 * css_rightmost_descendant - return the rightmost descendant of a css
2642 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002643 *
Tejun Heo492eb212013-08-08 20:11:25 -04002644 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2645 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002646 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002647 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002648 * While this function requires cgroup_mutex or RCU read locking, it
2649 * doesn't require the whole traversal to be contained in a single critical
2650 * section. This function will return the correct rightmost descendant as
2651 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002652 */
Tejun Heo492eb212013-08-08 20:11:25 -04002653struct cgroup_subsys_state *
2654css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002655{
Tejun Heo492eb212013-08-08 20:11:25 -04002656 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002657
Tejun Heoace2bee2014-02-11 11:52:47 -05002658 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002659
2660 do {
2661 last = pos;
2662 /* ->prev isn't RCU safe, walk ->next till the end */
2663 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002664 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002665 pos = tmp;
2666 } while (pos);
2667
2668 return last;
2669}
Tejun Heo492eb212013-08-08 20:11:25 -04002670EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002671
Tejun Heo492eb212013-08-08 20:11:25 -04002672static struct cgroup_subsys_state *
2673css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002674{
Tejun Heo492eb212013-08-08 20:11:25 -04002675 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002676
2677 do {
2678 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04002679 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002680 } while (pos);
2681
2682 return last;
2683}
2684
2685/**
Tejun Heo492eb212013-08-08 20:11:25 -04002686 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002687 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002688 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002689 *
Tejun Heo492eb212013-08-08 20:11:25 -04002690 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002691 * to visit for post-order traversal of @root's descendants. @root is
2692 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002693 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002694 * While this function requires cgroup_mutex or RCU read locking, it
2695 * doesn't require the whole traversal to be contained in a single critical
2696 * section. This function will return the correct next descendant as long
2697 * as both @pos and @cgroup are accessible and @pos is a descendant of
2698 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002699 */
Tejun Heo492eb212013-08-08 20:11:25 -04002700struct cgroup_subsys_state *
2701css_next_descendant_post(struct cgroup_subsys_state *pos,
2702 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002703{
Tejun Heo492eb212013-08-08 20:11:25 -04002704 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002705
Tejun Heoace2bee2014-02-11 11:52:47 -05002706 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002707
Tejun Heo58b79a92013-09-06 15:31:08 -04002708 /* if first iteration, visit leftmost descendant which may be @root */
2709 if (!pos)
2710 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002711
Tejun Heobd8815a2013-08-08 20:11:27 -04002712 /* if we visited @root, we're done */
2713 if (pos == root)
2714 return NULL;
2715
Tejun Heo574bd9f2012-11-09 09:12:29 -08002716 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04002717 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002718 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04002719 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002720
2721 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04002722 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002723}
Tejun Heo492eb212013-08-08 20:11:25 -04002724EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002725
Tejun Heo0942eee2013-08-08 20:11:26 -04002726/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002727 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04002728 * @it: the iterator to advance
2729 *
2730 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04002731 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002732static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04002733{
2734 struct list_head *l = it->cset_link;
2735 struct cgrp_cset_link *link;
2736 struct css_set *cset;
2737
2738 /* Advance to the next non-empty css_set */
2739 do {
2740 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04002741 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04002742 it->cset_link = NULL;
2743 return;
2744 }
2745 link = list_entry(l, struct cgrp_cset_link, cset_link);
2746 cset = link->cset;
2747 } while (list_empty(&cset->tasks));
2748 it->cset_link = l;
2749 it->task = cset->tasks.next;
2750}
2751
Tejun Heo0942eee2013-08-08 20:11:26 -04002752/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002753 * css_task_iter_start - initiate task iteration
2754 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04002755 * @it: the task iterator to use
2756 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002757 * Initiate iteration through the tasks of @css. The caller can call
2758 * css_task_iter_next() to walk through the tasks until the function
2759 * returns NULL. On completion of iteration, css_task_iter_end() must be
2760 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04002761 *
2762 * Note that this function acquires a lock which is released when the
2763 * iteration finishes. The caller can't sleep while iteration is in
2764 * progress.
2765 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002766void css_task_iter_start(struct cgroup_subsys_state *css,
2767 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002768 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002769{
2770 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04002771 * The first time anyone tries to iterate across a css, we need to
2772 * enable the list linking each css_set to its tasks, and fix up
2773 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07002774 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002775 if (!use_task_css_set_links)
2776 cgroup_enable_task_cg_lists();
2777
Paul Menage817929e2007-10-18 23:39:36 -07002778 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002779
Tejun Heo72ec7022013-08-08 20:11:26 -04002780 it->origin_css = css;
2781 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002782
Tejun Heo72ec7022013-08-08 20:11:26 -04002783 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002784}
2785
Tejun Heo0942eee2013-08-08 20:11:26 -04002786/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002787 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04002788 * @it: the task iterator being iterated
2789 *
2790 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04002791 * initialized via css_task_iter_start(). Returns NULL when the iteration
2792 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04002793 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002794struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002795{
2796 struct task_struct *res;
2797 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07002798 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002799
2800 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07002801 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07002802 return NULL;
2803 res = list_entry(l, struct task_struct, cg_list);
2804 /* Advance iterator to find next entry */
2805 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002806 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
2807 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04002808 /*
2809 * We reached the end of this task list - move on to the
2810 * next cgrp_cset_link.
2811 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002812 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002813 } else {
2814 it->task = l;
2815 }
2816 return res;
2817}
2818
Tejun Heo0942eee2013-08-08 20:11:26 -04002819/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002820 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04002821 * @it: the task iterator to finish
2822 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002823 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04002824 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002825void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002826 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002827{
2828 read_unlock(&css_set_lock);
2829}
2830
Cliff Wickman31a7df02008-02-07 00:14:42 -08002831static inline int started_after_time(struct task_struct *t1,
2832 struct timespec *time,
2833 struct task_struct *t2)
2834{
2835 int start_diff = timespec_compare(&t1->start_time, time);
2836 if (start_diff > 0) {
2837 return 1;
2838 } else if (start_diff < 0) {
2839 return 0;
2840 } else {
2841 /*
2842 * Arbitrarily, if two processes started at the same
2843 * time, we'll say that the lower pointer value
2844 * started first. Note that t2 may have exited by now
2845 * so this may not be a valid pointer any longer, but
2846 * that's fine - it still serves to distinguish
2847 * between two tasks started (effectively) simultaneously.
2848 */
2849 return t1 > t2;
2850 }
2851}
2852
2853/*
2854 * This function is a callback from heap_insert() and is used to order
2855 * the heap.
2856 * In this case we order the heap in descending task start time.
2857 */
2858static inline int started_after(void *p1, void *p2)
2859{
2860 struct task_struct *t1 = p1;
2861 struct task_struct *t2 = p2;
2862 return started_after_time(t1, &t2->start_time, t2);
2863}
2864
2865/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002866 * css_scan_tasks - iterate though all the tasks in a css
2867 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04002868 * @test: optional test callback
2869 * @process: process callback
2870 * @data: data passed to @test and @process
2871 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08002872 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002873 * Iterate through all the tasks in @css, calling @test for each, and if it
2874 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08002875 *
Tejun Heoe5358372013-08-08 20:11:26 -04002876 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04002877 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04002878 * lock css_set_lock for the call to @process.
2879 *
2880 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04002881 * of @css for the duration of this call. This function may or may not
2882 * call @process for tasks that exit or move to a different css during the
2883 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04002884 *
2885 * Note that @test may be called with locks held, and may in some
2886 * situations be called multiple times for the same task, so it should be
2887 * cheap.
2888 *
2889 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
2890 * heap operations (and its "gt" member will be overwritten), else a
2891 * temporary heap will be used (allocation of which may cause this function
2892 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08002893 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002894int css_scan_tasks(struct cgroup_subsys_state *css,
2895 bool (*test)(struct task_struct *, void *),
2896 void (*process)(struct task_struct *, void *),
2897 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002898{
2899 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04002900 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002901 struct task_struct *p, *dropped;
2902 /* Never dereference latest_task, since it's not refcounted */
2903 struct task_struct *latest_task = NULL;
2904 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002905 struct timespec latest_time = { 0, 0 };
2906
Tejun Heoe5358372013-08-08 20:11:26 -04002907 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08002908 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002909 heap->gt = &started_after;
2910 } else {
2911 /* We need to allocate our own heap memory */
2912 heap = &tmp_heap;
2913 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2914 if (retval)
2915 /* cannot allocate the heap */
2916 return retval;
2917 }
2918
2919 again:
2920 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04002921 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04002922 * which are of interest, and invoking @process callback on the
2923 * ones which need an update. Since we don't want to hold any
2924 * locks during the task updates, gather tasks to be processed in a
2925 * heap structure. The heap is sorted by descending task start
2926 * time. If the statically-sized heap fills up, we overflow tasks
2927 * that started later, and in future iterations only consider tasks
2928 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08002929 * guarantees forward progress and that we don't miss any tasks.
2930 */
2931 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04002932 css_task_iter_start(css, &it);
2933 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08002934 /*
2935 * Only affect tasks that qualify per the caller's callback,
2936 * if he provided one
2937 */
Tejun Heoe5358372013-08-08 20:11:26 -04002938 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002939 continue;
2940 /*
2941 * Only process tasks that started after the last task
2942 * we processed
2943 */
2944 if (!started_after_time(p, &latest_time, latest_task))
2945 continue;
2946 dropped = heap_insert(heap, p);
2947 if (dropped == NULL) {
2948 /*
2949 * The new task was inserted; the heap wasn't
2950 * previously full
2951 */
2952 get_task_struct(p);
2953 } else if (dropped != p) {
2954 /*
2955 * The new task was inserted, and pushed out a
2956 * different task
2957 */
2958 get_task_struct(p);
2959 put_task_struct(dropped);
2960 }
2961 /*
2962 * Else the new task was newer than anything already in
2963 * the heap and wasn't inserted
2964 */
2965 }
Tejun Heo72ec7022013-08-08 20:11:26 -04002966 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002967
2968 if (heap->size) {
2969 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002970 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002971 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002972 latest_time = q->start_time;
2973 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002974 }
2975 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04002976 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07002977 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002978 }
2979 /*
2980 * If we had to process any tasks at all, scan again
2981 * in case some of them were in the middle of forking
2982 * children that didn't get processed.
2983 * Not the most efficient way to do it, but it avoids
2984 * having to take callback_mutex in the fork path
2985 */
2986 goto again;
2987 }
2988 if (heap == &tmp_heap)
2989 heap_free(&tmp_heap);
2990 return 0;
2991}
2992
Tejun Heoe5358372013-08-08 20:11:26 -04002993static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07002994{
Tejun Heoe5358372013-08-08 20:11:26 -04002995 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07002996
Tejun Heo47cfcd02013-04-07 09:29:51 -07002997 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07002998 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002999 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003000}
3001
3002/**
3003 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3004 * @to: cgroup to which the tasks will be moved
3005 * @from: cgroup in which the tasks currently reside
3006 */
3007int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3008{
Tejun Heo72ec7022013-08-08 20:11:26 -04003009 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3010 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003011}
3012
Paul Menage817929e2007-10-18 23:39:36 -07003013/*
Ben Blum102a7752009-09-23 15:56:26 -07003014 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003015 *
3016 * Reading this file can return large amounts of data if a cgroup has
3017 * *lots* of attached tasks. So it may need several calls to read(),
3018 * but we cannot guarantee that the information we produce is correct
3019 * unless we produce it entirely atomically.
3020 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003021 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003022
Li Zefan24528252012-01-20 11:58:43 +08003023/* which pidlist file are we talking about? */
3024enum cgroup_filetype {
3025 CGROUP_FILE_PROCS,
3026 CGROUP_FILE_TASKS,
3027};
3028
3029/*
3030 * A pidlist is a list of pids that virtually represents the contents of one
3031 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3032 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3033 * to the cgroup.
3034 */
3035struct cgroup_pidlist {
3036 /*
3037 * used to find which pidlist is wanted. doesn't change as long as
3038 * this particular list stays in the list.
3039 */
3040 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3041 /* array of xids */
3042 pid_t *list;
3043 /* how many elements the above list has */
3044 int length;
Li Zefan24528252012-01-20 11:58:43 +08003045 /* each of these stored in a list by its cgroup */
3046 struct list_head links;
3047 /* pointer to the cgroup we belong to, for list removal purposes */
3048 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003049 /* for delayed destruction */
3050 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003051};
3052
Paul Menagebbcb81d2007-10-18 23:39:32 -07003053/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003054 * The following two functions "fix" the issue where there are more pids
3055 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3056 * TODO: replace with a kernel-wide solution to this problem
3057 */
3058#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3059static void *pidlist_allocate(int count)
3060{
3061 if (PIDLIST_TOO_LARGE(count))
3062 return vmalloc(count * sizeof(pid_t));
3063 else
3064 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3065}
Tejun Heob1a21362013-11-29 10:42:58 -05003066
Ben Blumd1d9fd32009-09-23 15:56:28 -07003067static void pidlist_free(void *p)
3068{
3069 if (is_vmalloc_addr(p))
3070 vfree(p);
3071 else
3072 kfree(p);
3073}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003074
3075/*
Tejun Heob1a21362013-11-29 10:42:58 -05003076 * Used to destroy all pidlists lingering waiting for destroy timer. None
3077 * should be left afterwards.
3078 */
3079static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3080{
3081 struct cgroup_pidlist *l, *tmp_l;
3082
3083 mutex_lock(&cgrp->pidlist_mutex);
3084 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3085 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3086 mutex_unlock(&cgrp->pidlist_mutex);
3087
3088 flush_workqueue(cgroup_pidlist_destroy_wq);
3089 BUG_ON(!list_empty(&cgrp->pidlists));
3090}
3091
3092static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3093{
3094 struct delayed_work *dwork = to_delayed_work(work);
3095 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3096 destroy_dwork);
3097 struct cgroup_pidlist *tofree = NULL;
3098
3099 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003100
3101 /*
Tejun Heo04502362013-11-29 10:42:59 -05003102 * Destroy iff we didn't get queued again. The state won't change
3103 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003104 */
Tejun Heo04502362013-11-29 10:42:59 -05003105 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003106 list_del(&l->links);
3107 pidlist_free(l->list);
3108 put_pid_ns(l->key.ns);
3109 tofree = l;
3110 }
3111
Tejun Heob1a21362013-11-29 10:42:58 -05003112 mutex_unlock(&l->owner->pidlist_mutex);
3113 kfree(tofree);
3114}
3115
3116/*
Ben Blum102a7752009-09-23 15:56:26 -07003117 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003118 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003119 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003120static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003121{
Ben Blum102a7752009-09-23 15:56:26 -07003122 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003123
3124 /*
3125 * we presume the 0th element is unique, so i starts at 1. trivial
3126 * edge cases first; no work needs to be done for either
3127 */
3128 if (length == 0 || length == 1)
3129 return length;
3130 /* src and dest walk down the list; dest counts unique elements */
3131 for (src = 1; src < length; src++) {
3132 /* find next unique element */
3133 while (list[src] == list[src-1]) {
3134 src++;
3135 if (src == length)
3136 goto after;
3137 }
3138 /* dest always points to where the next unique element goes */
3139 list[dest] = list[src];
3140 dest++;
3141 }
3142after:
Ben Blum102a7752009-09-23 15:56:26 -07003143 return dest;
3144}
3145
Tejun Heoafb2bc12013-11-29 10:42:59 -05003146/*
3147 * The two pid files - task and cgroup.procs - guaranteed that the result
3148 * is sorted, which forced this whole pidlist fiasco. As pid order is
3149 * different per namespace, each namespace needs differently sorted list,
3150 * making it impossible to use, for example, single rbtree of member tasks
3151 * sorted by task pointer. As pidlists can be fairly large, allocating one
3152 * per open file is dangerous, so cgroup had to implement shared pool of
3153 * pidlists keyed by cgroup and namespace.
3154 *
3155 * All this extra complexity was caused by the original implementation
3156 * committing to an entirely unnecessary property. In the long term, we
3157 * want to do away with it. Explicitly scramble sort order if
3158 * sane_behavior so that no such expectation exists in the new interface.
3159 *
3160 * Scrambling is done by swapping every two consecutive bits, which is
3161 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3162 */
3163static pid_t pid_fry(pid_t pid)
3164{
3165 unsigned a = pid & 0x55555555;
3166 unsigned b = pid & 0xAAAAAAAA;
3167
3168 return (a << 1) | (b >> 1);
3169}
3170
3171static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3172{
3173 if (cgroup_sane_behavior(cgrp))
3174 return pid_fry(pid);
3175 else
3176 return pid;
3177}
3178
Ben Blum102a7752009-09-23 15:56:26 -07003179static int cmppid(const void *a, const void *b)
3180{
3181 return *(pid_t *)a - *(pid_t *)b;
3182}
3183
Tejun Heoafb2bc12013-11-29 10:42:59 -05003184static int fried_cmppid(const void *a, const void *b)
3185{
3186 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3187}
3188
Ben Blum72a8cb32009-09-23 15:56:27 -07003189static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3190 enum cgroup_filetype type)
3191{
3192 struct cgroup_pidlist *l;
3193 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003194 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003195
Tejun Heoe6b81712013-11-29 10:42:59 -05003196 lockdep_assert_held(&cgrp->pidlist_mutex);
3197
3198 list_for_each_entry(l, &cgrp->pidlists, links)
3199 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003200 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003201 return NULL;
3202}
3203
Ben Blum72a8cb32009-09-23 15:56:27 -07003204/*
3205 * find the appropriate pidlist for our purpose (given procs vs tasks)
3206 * returns with the lock on that pidlist already held, and takes care
3207 * of the use count, or returns NULL with no locks held if we're out of
3208 * memory.
3209 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003210static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3211 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003212{
3213 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003214
Tejun Heoe6b81712013-11-29 10:42:59 -05003215 lockdep_assert_held(&cgrp->pidlist_mutex);
3216
3217 l = cgroup_pidlist_find(cgrp, type);
3218 if (l)
3219 return l;
3220
Ben Blum72a8cb32009-09-23 15:56:27 -07003221 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003222 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003223 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003224 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003225
Tejun Heob1a21362013-11-29 10:42:58 -05003226 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003227 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003228 /* don't need task_nsproxy() if we're looking at ourself */
3229 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003230 l->owner = cgrp;
3231 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003232 return l;
3233}
3234
3235/*
Ben Blum102a7752009-09-23 15:56:26 -07003236 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3237 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003238static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3239 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003240{
3241 pid_t *array;
3242 int length;
3243 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003244 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003245 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003246 struct cgroup_pidlist *l;
3247
Tejun Heo4bac00d2013-11-29 10:42:59 -05003248 lockdep_assert_held(&cgrp->pidlist_mutex);
3249
Ben Blum102a7752009-09-23 15:56:26 -07003250 /*
3251 * If cgroup gets more users after we read count, we won't have
3252 * enough space - tough. This race is indistinguishable to the
3253 * caller from the case that the additional cgroup users didn't
3254 * show up until sometime later on.
3255 */
3256 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003257 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003258 if (!array)
3259 return -ENOMEM;
3260 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003261 css_task_iter_start(&cgrp->dummy_css, &it);
3262 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003263 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003264 break;
Ben Blum102a7752009-09-23 15:56:26 -07003265 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003266 if (type == CGROUP_FILE_PROCS)
3267 pid = task_tgid_vnr(tsk);
3268 else
3269 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003270 if (pid > 0) /* make sure to only use valid results */
3271 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003272 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003273 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003274 length = n;
3275 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003276 if (cgroup_sane_behavior(cgrp))
3277 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3278 else
3279 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003280 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003281 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003282
Tejun Heoe6b81712013-11-29 10:42:59 -05003283 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003284 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003285 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003286 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003287 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003288 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003289
3290 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003291 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003292 l->list = array;
3293 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003294 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003295 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003296}
3297
Balbir Singh846c7bb2007-10-18 23:39:44 -07003298/**
Li Zefana043e3b2008-02-23 15:24:09 -08003299 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003300 * @stats: cgroupstats to fill information into
3301 * @dentry: A dentry entry belonging to the cgroup for which stats have
3302 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003303 *
3304 * Build and fill cgroupstats so that taskstats can export it to user
3305 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003306 */
3307int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3308{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003309 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003310 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003311 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003312 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003313
Tejun Heo2bd59d42014-02-11 11:52:49 -05003314 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3315 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3316 kernfs_type(kn) != KERNFS_DIR)
3317 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003318
Tejun Heo2bd59d42014-02-11 11:52:49 -05003319 /*
3320 * We aren't being called from kernfs and there's no guarantee on
3321 * @kn->priv's validity. For this and css_tryget_from_dir(),
3322 * @kn->priv is RCU safe. Let's do the RCU dancing.
3323 */
3324 rcu_read_lock();
3325 cgrp = rcu_dereference(kn->priv);
3326 if (!cgrp) {
3327 rcu_read_unlock();
3328 return -ENOENT;
3329 }
Balbir Singh846c7bb2007-10-18 23:39:44 -07003330
Tejun Heo72ec7022013-08-08 20:11:26 -04003331 css_task_iter_start(&cgrp->dummy_css, &it);
3332 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003333 switch (tsk->state) {
3334 case TASK_RUNNING:
3335 stats->nr_running++;
3336 break;
3337 case TASK_INTERRUPTIBLE:
3338 stats->nr_sleeping++;
3339 break;
3340 case TASK_UNINTERRUPTIBLE:
3341 stats->nr_uninterruptible++;
3342 break;
3343 case TASK_STOPPED:
3344 stats->nr_stopped++;
3345 break;
3346 default:
3347 if (delayacct_is_task_waiting_on_io(tsk))
3348 stats->nr_io_wait++;
3349 break;
3350 }
3351 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003352 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003353
Tejun Heo2bd59d42014-02-11 11:52:49 -05003354 rcu_read_unlock();
3355 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003356}
3357
Paul Menage8f3ff202009-09-23 15:56:25 -07003358
Paul Menagecc31edc2008-10-18 20:28:04 -07003359/*
Ben Blum102a7752009-09-23 15:56:26 -07003360 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003361 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003362 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003363 */
3364
Ben Blum102a7752009-09-23 15:56:26 -07003365static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003366{
3367 /*
3368 * Initially we receive a position value that corresponds to
3369 * one more than the last pid shown (or 0 on the first call or
3370 * after a seek to the start). Use a binary-search to find the
3371 * next pid to display, if any
3372 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003373 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003374 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003375 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003376 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003377 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003378 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003379
Tejun Heo4bac00d2013-11-29 10:42:59 -05003380 mutex_lock(&cgrp->pidlist_mutex);
3381
3382 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003383 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003384 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003385 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003386 * could already have been destroyed.
3387 */
Tejun Heo5d224442013-12-05 12:28:04 -05003388 if (of->priv)
3389 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003390
3391 /*
3392 * Either this is the first start() after open or the matching
3393 * pidlist has been destroyed inbetween. Create a new one.
3394 */
Tejun Heo5d224442013-12-05 12:28:04 -05003395 if (!of->priv) {
3396 ret = pidlist_array_load(cgrp, type,
3397 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003398 if (ret)
3399 return ERR_PTR(ret);
3400 }
Tejun Heo5d224442013-12-05 12:28:04 -05003401 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003402
Paul Menagecc31edc2008-10-18 20:28:04 -07003403 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003404 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003405
Paul Menagecc31edc2008-10-18 20:28:04 -07003406 while (index < end) {
3407 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003408 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003409 index = mid;
3410 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003411 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003412 index = mid + 1;
3413 else
3414 end = mid;
3415 }
3416 }
3417 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003418 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003419 return NULL;
3420 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003421 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003422 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003423 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003424}
3425
Ben Blum102a7752009-09-23 15:56:26 -07003426static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003427{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003428 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003429 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003430
Tejun Heo5d224442013-12-05 12:28:04 -05003431 if (l)
3432 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003433 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003434 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003435}
3436
Ben Blum102a7752009-09-23 15:56:26 -07003437static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003438{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003439 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003440 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003441 pid_t *p = v;
3442 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003443 /*
3444 * Advance to the next pid in the array. If this goes off the
3445 * end, we're done
3446 */
3447 p++;
3448 if (p >= end) {
3449 return NULL;
3450 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003451 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003452 return p;
3453 }
3454}
3455
Ben Blum102a7752009-09-23 15:56:26 -07003456static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003457{
3458 return seq_printf(s, "%d\n", *(int *)v);
3459}
3460
Ben Blum102a7752009-09-23 15:56:26 -07003461/*
3462 * seq_operations functions for iterating on pidlists through seq_file -
3463 * independent of whether it's tasks or procs
3464 */
3465static const struct seq_operations cgroup_pidlist_seq_operations = {
3466 .start = cgroup_pidlist_start,
3467 .stop = cgroup_pidlist_stop,
3468 .next = cgroup_pidlist_next,
3469 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003470};
3471
Tejun Heo182446d2013-08-08 20:11:24 -04003472static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3473 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003474{
Tejun Heo182446d2013-08-08 20:11:24 -04003475 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003476}
3477
Tejun Heo182446d2013-08-08 20:11:24 -04003478static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3479 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003480{
Tejun Heo182446d2013-08-08 20:11:24 -04003481 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003482 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003483 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003484 else
Tejun Heo182446d2013-08-08 20:11:24 -04003485 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003486 return 0;
3487}
3488
Tejun Heo182446d2013-08-08 20:11:24 -04003489static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3490 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003491{
Tejun Heo182446d2013-08-08 20:11:24 -04003492 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003493}
3494
Tejun Heo182446d2013-08-08 20:11:24 -04003495static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3496 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003497{
3498 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003499 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003500 else
Tejun Heo182446d2013-08-08 20:11:24 -04003501 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003502 return 0;
3503}
3504
Tejun Heod5c56ce2013-06-03 19:14:34 -07003505static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003506 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003507 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003508 .seq_start = cgroup_pidlist_start,
3509 .seq_next = cgroup_pidlist_next,
3510 .seq_stop = cgroup_pidlist_stop,
3511 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003512 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003513 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003514 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003515 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003516 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003517 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003518 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003519 .read_u64 = cgroup_clone_children_read,
3520 .write_u64 = cgroup_clone_children_write,
3521 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003522 {
Tejun Heo873fe092013-04-14 20:15:26 -07003523 .name = "cgroup.sane_behavior",
3524 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003525 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003526 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003527
3528 /*
3529 * Historical crazy stuff. These don't have "cgroup." prefix and
3530 * don't exist if sane_behavior. If you're depending on these, be
3531 * prepared to be burned.
3532 */
3533 {
3534 .name = "tasks",
3535 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003536 .seq_start = cgroup_pidlist_start,
3537 .seq_next = cgroup_pidlist_next,
3538 .seq_stop = cgroup_pidlist_stop,
3539 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003540 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003541 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003542 .mode = S_IRUGO | S_IWUSR,
3543 },
3544 {
3545 .name = "notify_on_release",
3546 .flags = CFTYPE_INSANE,
3547 .read_u64 = cgroup_read_notify_on_release,
3548 .write_u64 = cgroup_write_notify_on_release,
3549 },
Tejun Heo873fe092013-04-14 20:15:26 -07003550 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003551 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003552 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003553 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003554 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003555 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003556 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003557 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003558};
3559
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003560/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003561 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003562 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003563 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003564 *
3565 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003566 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003567static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003568{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003569 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003570 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003571
Tejun Heo8e3f6542012-04-01 12:09:55 -07003572 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003573 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003574 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07003575
3576 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003577 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003578
Tejun Heobee55092013-06-28 16:24:11 -07003579 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003580 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003581 if (ret < 0)
3582 goto err;
3583 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003584 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003585 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003586err:
3587 cgroup_clear_dir(cgrp, subsys_mask);
3588 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003589}
3590
Tejun Heo0c21ead2013-08-13 20:22:51 -04003591/*
3592 * css destruction is four-stage process.
3593 *
3594 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3595 * Implemented in kill_css().
3596 *
3597 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3598 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3599 * by invoking offline_css(). After offlining, the base ref is put.
3600 * Implemented in css_killed_work_fn().
3601 *
3602 * 3. When the percpu_ref reaches zero, the only possible remaining
3603 * accessors are inside RCU read sections. css_release() schedules the
3604 * RCU callback.
3605 *
3606 * 4. After the grace period, the css can be freed. Implemented in
3607 * css_free_work_fn().
3608 *
3609 * It is actually hairier because both step 2 and 4 require process context
3610 * and thus involve punting to css->destroy_work adding two additional
3611 * steps to the already complex sequence.
3612 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003613static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003614{
3615 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003616 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003617 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003618
Tejun Heo0ae78e02013-08-13 11:01:54 -04003619 if (css->parent)
3620 css_put(css->parent);
3621
Tejun Heo0c21ead2013-08-13 20:22:51 -04003622 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003623 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003624}
3625
3626static void css_free_rcu_fn(struct rcu_head *rcu_head)
3627{
3628 struct cgroup_subsys_state *css =
3629 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3630
Tejun Heo0c21ead2013-08-13 20:22:51 -04003631 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003632 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003633}
3634
Tejun Heod3daf282013-06-13 19:39:16 -07003635static void css_release(struct percpu_ref *ref)
3636{
3637 struct cgroup_subsys_state *css =
3638 container_of(ref, struct cgroup_subsys_state, refcnt);
3639
Tejun Heoaec25022014-02-08 10:36:58 -05003640 rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003641 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003642}
3643
Tejun Heo623f9262013-08-13 11:01:55 -04003644static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3645 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003646{
Paul Menagebd89aab2007-10-18 23:40:44 -07003647 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003648 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003649 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003650
Tejun Heo0ae78e02013-08-13 11:01:54 -04003651 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003652 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003653 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003654 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003655
Tejun Heoca8bdca2013-08-26 18:40:56 -04003656 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003657}
3658
Li Zefan2a4ac632013-07-31 16:16:40 +08003659/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04003660static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003661{
Tejun Heo623f9262013-08-13 11:01:55 -04003662 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08003663 int ret = 0;
3664
Tejun Heoace2bee2014-02-11 11:52:47 -05003665 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003666 lockdep_assert_held(&cgroup_mutex);
3667
Tejun Heo92fb9742012-11-19 08:13:38 -08003668 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04003669 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003670 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04003671 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04003672 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05003673 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003674 }
Tejun Heob1929db2012-11-19 08:13:38 -08003675 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003676}
3677
Li Zefan2a4ac632013-07-31 16:16:40 +08003678/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04003679static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003680{
Tejun Heo623f9262013-08-13 11:01:55 -04003681 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003682
Tejun Heoace2bee2014-02-11 11:52:47 -05003683 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003684 lockdep_assert_held(&cgroup_mutex);
3685
3686 if (!(css->flags & CSS_ONLINE))
3687 return;
3688
Li Zefand7eeac12013-03-12 15:35:59 -07003689 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04003690 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003691
Tejun Heoeb954192013-08-08 20:11:23 -04003692 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04003693 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05003694 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003695}
3696
Tejun Heoc81c925a2013-12-06 15:11:56 -05003697/**
3698 * create_css - create a cgroup_subsys_state
3699 * @cgrp: the cgroup new css will be associated with
3700 * @ss: the subsys of new css
3701 *
3702 * Create a new css associated with @cgrp - @ss pair. On success, the new
3703 * css is online and installed in @cgrp with all interface files created.
3704 * Returns 0 on success, -errno on failure.
3705 */
3706static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
3707{
3708 struct cgroup *parent = cgrp->parent;
3709 struct cgroup_subsys_state *css;
3710 int err;
3711
Tejun Heoc81c925a2013-12-06 15:11:56 -05003712 lockdep_assert_held(&cgroup_mutex);
3713
3714 css = ss->css_alloc(cgroup_css(parent, ss));
3715 if (IS_ERR(css))
3716 return PTR_ERR(css);
3717
3718 err = percpu_ref_init(&css->refcnt, css_release);
3719 if (err)
3720 goto err_free;
3721
3722 init_css(css, ss, cgrp);
3723
Tejun Heoaec25022014-02-08 10:36:58 -05003724 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003725 if (err)
3726 goto err_free;
3727
3728 err = online_css(css);
3729 if (err)
3730 goto err_free;
3731
Tejun Heo59f52962014-02-11 11:52:49 -05003732 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003733 css_get(css->parent);
3734
3735 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
3736 parent->parent) {
3737 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",
3738 current->comm, current->pid, ss->name);
3739 if (!strcmp(ss->name, "memory"))
3740 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
3741 ss->warned_broken_hierarchy = true;
3742 }
3743
3744 return 0;
3745
3746err_free:
3747 percpu_ref_cancel_init(&css->refcnt);
3748 ss->css_free(css);
3749 return err;
3750}
3751
Tejun Heo2bd59d42014-02-11 11:52:49 -05003752/**
Li Zefana043e3b2008-02-23 15:24:09 -08003753 * cgroup_create - create a cgroup
3754 * @parent: cgroup that will be parent of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05003755 * @name_str: name of the new cgroup
3756 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07003757 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003758static long cgroup_create(struct cgroup *parent, const char *name_str,
3759 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003760{
Paul Menagebd89aab2007-10-18 23:40:44 -07003761 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08003762 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003763 struct cgroupfs_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05003764 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003765 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003766 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003767
Tejun Heo0a950f62012-11-19 09:02:12 -08003768 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07003769 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3770 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003771 return -ENOMEM;
3772
Tejun Heo2bd59d42014-02-11 11:52:49 -05003773 name = cgroup_alloc_name(name_str);
Tejun Heob58c8992014-02-08 10:26:33 -05003774 if (!name) {
3775 err = -ENOMEM;
Li Zefan65dff752013-03-01 15:01:56 +08003776 goto err_free_cgrp;
Tejun Heob58c8992014-02-08 10:26:33 -05003777 }
Li Zefan65dff752013-03-01 15:01:56 +08003778 rcu_assign_pointer(cgrp->name, name);
3779
Tejun Heoace2bee2014-02-11 11:52:47 -05003780 mutex_lock(&cgroup_tree_mutex);
3781
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003782 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08003783 * Only live parents can have children. Note that the liveliness
3784 * check isn't strictly necessary because cgroup_mkdir() and
3785 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
3786 * anyway so that locking is contained inside cgroup proper and we
3787 * don't get nasty surprises if we ever grow another caller.
3788 */
3789 if (!cgroup_lock_live_group(parent)) {
3790 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05003791 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08003792 }
3793
3794 /*
3795 * Temporarily set the pointer to NULL, so idr_find() won't return
3796 * a half-baked cgroup.
3797 */
3798 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
3799 if (cgrp->id < 0) {
3800 err = -ENOMEM;
3801 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08003802 }
3803
Paul Menagecc31edc2008-10-18 20:28:04 -07003804 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003805
Paul Menagebd89aab2007-10-18 23:40:44 -07003806 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003807 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07003808 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003809
Li Zefanb6abdb02008-03-04 14:28:19 -08003810 if (notify_on_release(parent))
3811 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3812
Tejun Heo2260e7f2012-11-19 08:13:38 -08003813 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
3814 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003815
Tejun Heo2bd59d42014-02-11 11:52:49 -05003816 /* create the directory */
3817 kn = kernfs_create_dir(parent->kn, name->name, mode, cgrp);
3818 if (IS_ERR(kn)) {
3819 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003820 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003821 }
3822 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003823
Tejun Heo00356bd2013-06-18 11:14:22 -07003824 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09003825
Tejun Heo4e139af2012-11-19 08:13:36 -08003826 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08003827 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
3828 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08003829
Tejun Heo2bd59d42014-02-11 11:52:49 -05003830 /*
3831 * Grab a reference on the root and parent so that they don't get
3832 * deleted while there are child cgroups.
3833 */
3834 cgroup_get_root(root);
Tejun Heo59f52962014-02-11 11:52:49 -05003835 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08003836
Tejun Heo0d802552013-12-06 15:11:56 -05003837 /*
3838 * @cgrp is now fully operational. If something fails after this
3839 * point, it'll be released via the normal destruction path.
3840 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003841 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
3842
Tejun Heo2bb566c2013-08-08 20:11:23 -04003843 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07003844 if (err)
3845 goto err_destroy;
3846
Tejun Heo9d403e92013-12-06 15:11:56 -05003847 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05003848 for_each_subsys(ss, ssid) {
3849 if (root->subsys_mask & (1 << ssid)) {
3850 err = create_css(cgrp, ss);
3851 if (err)
3852 goto err_destroy;
3853 }
Tejun Heoa8638032012-11-09 09:12:29 -08003854 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003855
Tejun Heo2bd59d42014-02-11 11:52:49 -05003856 kernfs_activate(kn);
3857
Paul Menageddbcc7e2007-10-18 23:39:30 -07003858 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003859 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003860
3861 return 0;
3862
Tejun Heo0a950f62012-11-19 09:02:12 -08003863err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003864 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003865err_unlock:
3866 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003867err_unlock_tree:
3868 mutex_unlock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003869 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003870err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07003871 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003872 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003873
3874err_destroy:
3875 cgroup_destroy_locked(cgrp);
3876 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003877 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003878 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003879}
3880
Tejun Heo2bd59d42014-02-11 11:52:49 -05003881static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3882 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003883{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003884 struct cgroup *parent = parent_kn->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003885
Tejun Heo2bd59d42014-02-11 11:52:49 -05003886 return cgroup_create(parent, name, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003887}
3888
Tejun Heo223dbc32013-08-13 20:22:50 -04003889/*
3890 * This is called when the refcnt of a css is confirmed to be killed.
3891 * css_tryget() is now guaranteed to fail.
3892 */
3893static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07003894{
Tejun Heo223dbc32013-08-13 20:22:50 -04003895 struct cgroup_subsys_state *css =
3896 container_of(work, struct cgroup_subsys_state, destroy_work);
3897 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07003898
Tejun Heoace2bee2014-02-11 11:52:47 -05003899 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003900 mutex_lock(&cgroup_mutex);
3901
3902 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04003903 * css_tryget() is guaranteed to fail now. Tell subsystems to
3904 * initate destruction.
3905 */
3906 offline_css(css);
3907
3908 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003909 * If @cgrp is marked dead, it's waiting for refs of all css's to
3910 * be disabled before proceeding to the second phase of cgroup
3911 * destruction. If we are the last one, kick it off.
3912 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04003913 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04003914 cgroup_destroy_css_killed(cgrp);
3915
3916 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003917 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04003918
3919 /*
3920 * Put the css refs from kill_css(). Each css holds an extra
3921 * reference to the cgroup's dentry and cgroup removal proceeds
3922 * regardless of css refs. On the last put of each css, whenever
3923 * that may be, the extra dentry ref is put so that dentry
3924 * destruction happens only after all css's are released.
3925 */
3926 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07003927}
3928
Tejun Heo223dbc32013-08-13 20:22:50 -04003929/* css kill confirmation processing requires process context, bounce */
3930static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07003931{
3932 struct cgroup_subsys_state *css =
3933 container_of(ref, struct cgroup_subsys_state, refcnt);
3934
Tejun Heo223dbc32013-08-13 20:22:50 -04003935 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003936 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07003937}
3938
3939/**
Tejun Heoedae0c32013-08-13 20:22:51 -04003940 * kill_css - destroy a css
3941 * @css: css to destroy
3942 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003943 * This function initiates destruction of @css by removing cgroup interface
3944 * files and putting its base reference. ->css_offline() will be invoked
3945 * asynchronously once css_tryget() is guaranteed to fail and when the
3946 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04003947 */
3948static void kill_css(struct cgroup_subsys_state *css)
3949{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003950 /*
3951 * This must happen before css is disassociated with its cgroup.
3952 * See seq_css() for details.
3953 */
Tejun Heoaec25022014-02-08 10:36:58 -05003954 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003955
Tejun Heoedae0c32013-08-13 20:22:51 -04003956 /*
3957 * Killing would put the base ref, but we need to keep it alive
3958 * until after ->css_offline().
3959 */
3960 css_get(css);
3961
3962 /*
3963 * cgroup core guarantees that, by the time ->css_offline() is
3964 * invoked, no new css reference will be given out via
3965 * css_tryget(). We can't simply call percpu_ref_kill() and
3966 * proceed to offlining css's because percpu_ref_kill() doesn't
3967 * guarantee that the ref is seen as killed on all CPUs on return.
3968 *
3969 * Use percpu_ref_kill_and_confirm() to get notifications as each
3970 * css is confirmed to be seen as killed on all CPUs.
3971 */
3972 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003973}
3974
3975/**
3976 * cgroup_destroy_locked - the first stage of cgroup destruction
3977 * @cgrp: cgroup to be destroyed
3978 *
3979 * css's make use of percpu refcnts whose killing latency shouldn't be
3980 * exposed to userland and are RCU protected. Also, cgroup core needs to
3981 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
3982 * invoked. To satisfy all the requirements, destruction is implemented in
3983 * the following two steps.
3984 *
3985 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
3986 * userland visible parts and start killing the percpu refcnts of
3987 * css's. Set up so that the next stage will be kicked off once all
3988 * the percpu refcnts are confirmed to be killed.
3989 *
3990 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
3991 * rest of destruction. Once all cgroup references are gone, the
3992 * cgroup is RCU-freed.
3993 *
3994 * This function implements s1. After this step, @cgrp is gone as far as
3995 * the userland is concerned and a new cgroup with the same name may be
3996 * created. As cgroup doesn't care about the names internally, this
3997 * doesn't cause any problem.
3998 */
Tejun Heo42809dd2012-11-19 08:13:37 -08003999static int cgroup_destroy_locked(struct cgroup *cgrp)
4000 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004001{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004002 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004003 struct cgroup_subsys_state *css;
4004 struct kernfs_node *kn;
Tejun Heoddd69142013-06-12 21:04:54 -07004005 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004006 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004007
Tejun Heoace2bee2014-02-11 11:52:47 -05004008 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004009 lockdep_assert_held(&cgroup_mutex);
4010
Tejun Heoddd69142013-06-12 21:04:54 -07004011 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004012 * css_set_lock synchronizes access to ->cset_links and prevents
4013 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004014 */
4015 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004016 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004017 read_unlock(&css_set_lock);
4018 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004019 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004020
Tejun Heo1a90dd52012-11-05 09:16:59 -08004021 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004022 * Make sure there's no live children. We can't test ->children
4023 * emptiness as dead children linger on it while being destroyed;
4024 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4025 */
4026 empty = true;
4027 rcu_read_lock();
4028 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4029 empty = cgroup_is_dead(child);
4030 if (!empty)
4031 break;
4032 }
4033 rcu_read_unlock();
4034 if (!empty)
4035 return -EBUSY;
4036
4037 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004038 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4039 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004040 * percpu refs of all css's are confirmed to be killed. This
4041 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004042 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004043 mutex_unlock(&cgroup_mutex);
Tejun Heo1c6727a2013-12-06 15:11:56 -05004044 for_each_css(css, ssid, cgrp)
4045 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004046 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004047
4048 /*
4049 * Mark @cgrp dead. This prevents further task migration and child
4050 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004051 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004052 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004053 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004054 */
Tejun Heo54766d42013-06-12 21:04:53 -07004055 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004056
Tejun Heo455050d2013-06-13 19:27:41 -07004057 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4058 raw_spin_lock(&release_list_lock);
4059 if (!list_empty(&cgrp->release_list))
4060 list_del_init(&cgrp->release_list);
4061 raw_spin_unlock(&release_list_lock);
4062
4063 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004064 * If @cgrp has css's attached, the second stage of cgroup
4065 * destruction is kicked off from css_killed_work_fn() after the
4066 * refs of all attached css's are killed. If @cgrp doesn't have
4067 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004068 */
Tejun Heof20104d2013-08-13 20:22:50 -04004069 if (!cgrp->nr_css)
4070 cgroup_destroy_css_killed(cgrp);
4071
Tejun Heo2bd59d42014-02-11 11:52:49 -05004072 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05004073 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004074
4075 /*
4076 * There are two control paths which try to determine cgroup from
4077 * dentry without going through kernfs - cgroupstats_build() and
4078 * css_tryget_from_dir(). Those are supported by RCU protecting
4079 * clearing of cgrp->kn->priv backpointer, which should happen
4080 * after all files under it have been removed.
4081 */
4082 kn = cgrp->kn;
4083 kernfs_get(kn);
4084
4085 kernfs_remove(cgrp->kn);
4086
4087 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
4088 kernfs_put(kn);
4089
Tejun Heo4ac06012014-02-11 11:52:47 -05004090 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004091
Tejun Heoea15f8c2013-06-13 19:27:42 -07004092 return 0;
4093};
4094
Tejun Heod3daf282013-06-13 19:39:16 -07004095/**
Tejun Heof20104d2013-08-13 20:22:50 -04004096 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004097 * @work: cgroup->destroy_free_work
4098 *
4099 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004100 * destroyed after all css's are offlined and performs the rest of
4101 * destruction. This is the second step of destruction described in the
4102 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004103 */
Tejun Heof20104d2013-08-13 20:22:50 -04004104static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004105{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004106 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004107
Tejun Heoace2bee2014-02-11 11:52:47 -05004108 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004109 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004110
Paul Menage999cd8a2009-01-07 18:08:36 -08004111 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004112 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004113
Tejun Heo59f52962014-02-11 11:52:49 -05004114 cgroup_put(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004115
Paul Menagebd89aab2007-10-18 23:40:44 -07004116 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004117 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004118}
4119
Tejun Heo2bd59d42014-02-11 11:52:49 -05004120static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004121{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004122 struct cgroup *cgrp = kn->priv;
4123 int ret = 0;
4124
4125 /*
4126 * This is self-destruction but @kn can't be removed while this
4127 * callback is in progress. Let's break active protection. Once
4128 * the protection is broken, @cgrp can be destroyed at any point.
4129 * Pin it so that it stays accessible.
4130 */
4131 cgroup_get(cgrp);
4132 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004133
Tejun Heoace2bee2014-02-11 11:52:47 -05004134 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004135 mutex_lock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004136
4137 /*
4138 * @cgrp might already have been destroyed while we're trying to
4139 * grab the mutexes.
4140 */
4141 if (!cgroup_is_dead(cgrp))
4142 ret = cgroup_destroy_locked(cgrp);
4143
Tejun Heo42809dd2012-11-19 08:13:37 -08004144 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004145 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004146
Tejun Heo2bd59d42014-02-11 11:52:49 -05004147 kernfs_unbreak_active_protection(kn);
4148 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004149 return ret;
4150}
4151
Tejun Heo2bd59d42014-02-11 11:52:49 -05004152static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4153 .remount_fs = cgroup_remount,
4154 .show_options = cgroup_show_options,
4155 .mkdir = cgroup_mkdir,
4156 .rmdir = cgroup_rmdir,
4157 .rename = cgroup_rename,
4158};
4159
Li Zefan06a11922008-04-29 01:00:07 -07004160static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004161{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004162 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004163
4164 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004165
Tejun Heoace2bee2014-02-11 11:52:47 -05004166 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004167 mutex_lock(&cgroup_mutex);
4168
Tejun Heode00ffa2014-02-11 11:52:48 -05004169 INIT_LIST_HEAD(&ss->cftsets);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004170
Paul Menageddbcc7e2007-10-18 23:39:30 -07004171 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004172 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004173 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004174 /* We don't handle early failures gracefully */
4175 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004176 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004177
Li Zefane8d55fd2008-04-29 01:00:13 -07004178 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004179 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004180 * newly registered, all tasks and hence the
4181 * init_css_set is in the subsystem's top cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004182 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004183
4184 need_forkexit_callback |= ss->fork || ss->exit;
4185
Li Zefane8d55fd2008-04-29 01:00:13 -07004186 /* At system boot, before all subsystems have been
4187 * registered, no tasks have been forked, so we don't
4188 * need to invoke fork callbacks here. */
4189 BUG_ON(!list_empty(&init_task.tasks));
4190
Tejun Heoae7f1642013-08-13 20:22:50 -04004191 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004192
Tejun Heo648bb562012-11-19 08:13:36 -08004193 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004194 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004195}
4196
4197/**
Li Zefana043e3b2008-02-23 15:24:09 -08004198 * cgroup_init_early - cgroup initialization at system boot
4199 *
4200 * Initialize cgroups at system boot, and initialize any
4201 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004202 */
4203int __init cgroup_init_early(void)
4204{
Tejun Heo30159ec2013-06-25 11:53:37 -07004205 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004206 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004207
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004208 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004209 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004210 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004211 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004212 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004213 init_cgroup_root(&cgroup_dummy_root);
4214 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004215 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004216
Tejun Heo69d02062013-06-12 21:04:50 -07004217 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004218 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4219 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004220 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004221
Tejun Heo3ed80a62014-02-08 10:36:58 -05004222 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004223 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004224 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4225 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004226 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004227 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4228 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4229
Tejun Heoaec25022014-02-08 10:36:58 -05004230 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004231 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004232
4233 if (ss->early_init)
4234 cgroup_init_subsys(ss);
4235 }
4236 return 0;
4237}
4238
4239/**
Li Zefana043e3b2008-02-23 15:24:09 -08004240 * cgroup_init - cgroup initialization
4241 *
4242 * Register cgroup filesystem and /proc file, and initialize
4243 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004244 */
4245int __init cgroup_init(void)
4246{
Tejun Heo30159ec2013-06-25 11:53:37 -07004247 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004248 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004249 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004250
Tejun Heo2bd59d42014-02-11 11:52:49 -05004251 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Tejun Heo2da440a2014-02-11 11:52:48 -05004252
Tejun Heo3ed80a62014-02-08 10:36:58 -05004253 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004254 if (!ss->early_init)
4255 cgroup_init_subsys(ss);
Tejun Heode00ffa2014-02-11 11:52:48 -05004256
4257 /*
4258 * cftype registration needs kmalloc and can't be done
4259 * during early_init. Register base cftypes separately.
4260 */
4261 if (ss->base_cftypes)
4262 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004263 }
4264
Tejun Heofa3ca072013-04-14 11:36:56 -07004265 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004266 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004267
Tejun Heo82fe9b02013-06-25 11:53:37 -07004268 /* Add init_css_set to the hash table */
4269 key = css_set_hash(init_css_set.subsys);
4270 hash_add(css_set_table, &init_css_set.hlist, key);
4271
Tejun Heofc76df72013-06-25 11:53:37 -07004272 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004273
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004274 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4275 0, 1, GFP_KERNEL);
4276 BUG_ON(err < 0);
4277
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004278 mutex_unlock(&cgroup_mutex);
4279
Greg KH676db4a2010-08-05 13:53:35 -07004280 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004281 if (!cgroup_kobj)
4282 return -ENOMEM;
Greg KH676db4a2010-08-05 13:53:35 -07004283
4284 err = register_filesystem(&cgroup_fs_type);
4285 if (err < 0) {
4286 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004287 return err;
Greg KH676db4a2010-08-05 13:53:35 -07004288 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004289
Li Zefan46ae2202008-04-29 01:00:08 -07004290 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004291 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004292}
Paul Menageb4f48b62007-10-18 23:39:33 -07004293
Tejun Heoe5fca242013-11-22 17:14:39 -05004294static int __init cgroup_wq_init(void)
4295{
4296 /*
4297 * There isn't much point in executing destruction path in
4298 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004299 *
4300 * XXX: Must be ordered to make sure parent is offlined after
4301 * children. The ordering requirement is for memcg where a
4302 * parent's offline may wait for a child's leading to deadlock. In
4303 * the long term, this should be fixed from memcg side.
Tejun Heoe5fca242013-11-22 17:14:39 -05004304 *
4305 * We would prefer to do this in cgroup_init() above, but that
4306 * is called before init_workqueues(): so leave this until after.
4307 */
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004308 cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0);
Tejun Heoe5fca242013-11-22 17:14:39 -05004309 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004310
4311 /*
4312 * Used to destroy pidlists and separate to serve as flush domain.
4313 * Cap @max_active to 1 too.
4314 */
4315 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4316 0, 1);
4317 BUG_ON(!cgroup_pidlist_destroy_wq);
4318
Tejun Heoe5fca242013-11-22 17:14:39 -05004319 return 0;
4320}
4321core_initcall(cgroup_wq_init);
4322
Paul Menagea4243162007-10-18 23:39:35 -07004323/*
4324 * proc_cgroup_show()
4325 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4326 * - Used for /proc/<pid>/cgroup.
4327 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4328 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004329 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004330 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4331 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4332 * cgroup to top_cgroup.
4333 */
4334
4335/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004336int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004337{
4338 struct pid *pid;
4339 struct task_struct *tsk;
4340 char *buf;
4341 int retval;
4342 struct cgroupfs_root *root;
4343
4344 retval = -ENOMEM;
4345 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4346 if (!buf)
4347 goto out;
4348
4349 retval = -ESRCH;
4350 pid = m->private;
4351 tsk = get_pid_task(pid, PIDTYPE_PID);
4352 if (!tsk)
4353 goto out_free;
4354
4355 retval = 0;
4356
4357 mutex_lock(&cgroup_mutex);
4358
Li Zefane5f6a862009-01-07 18:07:41 -08004359 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004360 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004361 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004362 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004363
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004364 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004365 for_each_subsys(ss, ssid)
4366 if (root->subsys_mask & (1 << ssid))
4367 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004368 if (strlen(root->name))
4369 seq_printf(m, "%sname=%s", count ? "," : "",
4370 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004371 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004372 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004373 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004374 if (retval < 0)
4375 goto out_unlock;
4376 seq_puts(m, buf);
4377 seq_putc(m, '\n');
4378 }
4379
4380out_unlock:
4381 mutex_unlock(&cgroup_mutex);
4382 put_task_struct(tsk);
4383out_free:
4384 kfree(buf);
4385out:
4386 return retval;
4387}
4388
Paul Menagea4243162007-10-18 23:39:35 -07004389/* Display information about each subsystem and each hierarchy */
4390static int proc_cgroupstats_show(struct seq_file *m, void *v)
4391{
Tejun Heo30159ec2013-06-25 11:53:37 -07004392 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004393 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004394
Paul Menage8bab8dd2008-04-04 14:29:57 -07004395 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004396 /*
4397 * ideally we don't want subsystems moving around while we do this.
4398 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4399 * subsys/hierarchy state.
4400 */
Paul Menagea4243162007-10-18 23:39:35 -07004401 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004402
4403 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004404 seq_printf(m, "%s\t%d\t%d\t%d\n",
4405 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004406 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004407
Paul Menagea4243162007-10-18 23:39:35 -07004408 mutex_unlock(&cgroup_mutex);
4409 return 0;
4410}
4411
4412static int cgroupstats_open(struct inode *inode, struct file *file)
4413{
Al Viro9dce07f2008-03-29 03:07:28 +00004414 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004415}
4416
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004417static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004418 .open = cgroupstats_open,
4419 .read = seq_read,
4420 .llseek = seq_lseek,
4421 .release = single_release,
4422};
4423
Paul Menageb4f48b62007-10-18 23:39:33 -07004424/**
4425 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004426 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004427 *
4428 * Description: A task inherits its parent's cgroup at fork().
4429 *
4430 * A pointer to the shared css_set was automatically copied in
4431 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004432 * it was not made under the protection of RCU or cgroup_mutex, so
4433 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4434 * have already changed current->cgroups, allowing the previously
4435 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004436 *
4437 * At the point that cgroup_fork() is called, 'current' is the parent
4438 * task, and the passed argument 'child' points to the child task.
4439 */
4440void cgroup_fork(struct task_struct *child)
4441{
Tejun Heo9bb71302012-10-18 17:52:07 -07004442 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004443 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004444 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004445 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004446 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004447}
4448
4449/**
Li Zefana043e3b2008-02-23 15:24:09 -08004450 * cgroup_post_fork - called on a new task after adding it to the task list
4451 * @child: the task in question
4452 *
Tejun Heo5edee612012-10-16 15:03:14 -07004453 * Adds the task to the list running through its css_set if necessary and
4454 * call the subsystem fork() callbacks. Has to be after the task is
4455 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004456 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004457 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004458 */
Paul Menage817929e2007-10-18 23:39:36 -07004459void cgroup_post_fork(struct task_struct *child)
4460{
Tejun Heo30159ec2013-06-25 11:53:37 -07004461 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004462 int i;
4463
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004464 /*
4465 * use_task_css_set_links is set to 1 before we walk the tasklist
4466 * under the tasklist_lock and we read it here after we added the child
4467 * to the tasklist under the tasklist_lock as well. If the child wasn't
4468 * yet in the tasklist when we walked through it from
4469 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4470 * should be visible now due to the paired locking and barriers implied
4471 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4472 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4473 * lock on fork.
4474 */
Paul Menage817929e2007-10-18 23:39:36 -07004475 if (use_task_css_set_links) {
4476 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004477 task_lock(child);
4478 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004479 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004480 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004481 write_unlock(&css_set_lock);
4482 }
Tejun Heo5edee612012-10-16 15:03:14 -07004483
4484 /*
4485 * Call ss->fork(). This must happen after @child is linked on
4486 * css_set; otherwise, @child might change state between ->fork()
4487 * and addition to css_set.
4488 */
4489 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004490 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004491 if (ss->fork)
4492 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004493 }
Paul Menage817929e2007-10-18 23:39:36 -07004494}
Tejun Heo5edee612012-10-16 15:03:14 -07004495
Paul Menage817929e2007-10-18 23:39:36 -07004496/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004497 * cgroup_exit - detach cgroup from exiting task
4498 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004499 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004500 *
4501 * Description: Detach cgroup from @tsk and release it.
4502 *
4503 * Note that cgroups marked notify_on_release force every task in
4504 * them to take the global cgroup_mutex mutex when exiting.
4505 * This could impact scaling on very large systems. Be reluctant to
4506 * use notify_on_release cgroups where very high task exit scaling
4507 * is required on large systems.
4508 *
4509 * the_top_cgroup_hack:
4510 *
4511 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4512 *
4513 * We call cgroup_exit() while the task is still competent to
4514 * handle notify_on_release(), then leave the task attached to the
4515 * root cgroup in each hierarchy for the remainder of its exit.
4516 *
4517 * To do this properly, we would increment the reference count on
4518 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4519 * code we would add a second cgroup function call, to drop that
4520 * reference. This would just create an unnecessary hot spot on
4521 * the top_cgroup reference count, to no avail.
4522 *
4523 * Normally, holding a reference to a cgroup without bumping its
4524 * count is unsafe. The cgroup could go away, or someone could
4525 * attach us to a different cgroup, decrementing the count on
4526 * the first cgroup that we never incremented. But in this case,
4527 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004528 * which wards off any cgroup_attach_task() attempts, or task is a failed
4529 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004530 */
4531void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4532{
Tejun Heo30159ec2013-06-25 11:53:37 -07004533 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004534 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004535 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004536
4537 /*
4538 * Unlink from the css_set task list if necessary.
4539 * Optimistically check cg_list before taking
4540 * css_set_lock
4541 */
4542 if (!list_empty(&tsk->cg_list)) {
4543 write_lock(&css_set_lock);
4544 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004545 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004546 write_unlock(&css_set_lock);
4547 }
4548
Paul Menageb4f48b62007-10-18 23:39:33 -07004549 /* Reassign the task to the init_css_set. */
4550 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004551 cset = task_css_set(tsk);
4552 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004553
4554 if (run_callbacks && need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004555 /* see cgroup_post_fork() for details */
4556 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004557 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004558 struct cgroup_subsys_state *old_css = cset->subsys[i];
4559 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004560
Tejun Heoeb954192013-08-08 20:11:23 -04004561 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004562 }
4563 }
4564 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004565 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004566
Tejun Heo5abb8852013-06-12 21:04:49 -07004567 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07004568}
Paul Menage697f4162007-10-18 23:39:34 -07004569
Paul Menagebd89aab2007-10-18 23:40:44 -07004570static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004571{
Li Zefanf50daa72013-03-01 15:06:07 +08004572 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004573 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004574 /*
4575 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004576 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004577 * it now
4578 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004579 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004580
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004581 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004582 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004583 list_empty(&cgrp->release_list)) {
4584 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004585 need_schedule_work = 1;
4586 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004587 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004588 if (need_schedule_work)
4589 schedule_work(&release_agent_work);
4590 }
4591}
4592
Paul Menage81a6a5c2007-10-18 23:39:38 -07004593/*
4594 * Notify userspace when a cgroup is released, by running the
4595 * configured release agent with the name of the cgroup (path
4596 * relative to the root of cgroup file system) as the argument.
4597 *
4598 * Most likely, this user command will try to rmdir this cgroup.
4599 *
4600 * This races with the possibility that some other task will be
4601 * attached to this cgroup before it is removed, or that some other
4602 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4603 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4604 * unused, and this cgroup will be reprieved from its death sentence,
4605 * to continue to serve a useful existence. Next time it's released,
4606 * we will get notified again, if it still has 'notify_on_release' set.
4607 *
4608 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4609 * means only wait until the task is successfully execve()'d. The
4610 * separate release agent task is forked by call_usermodehelper(),
4611 * then control in this thread returns here, without waiting for the
4612 * release agent task. We don't bother to wait because the caller of
4613 * this routine has no use for the exit status of the release agent
4614 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004615 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004616static void cgroup_release_agent(struct work_struct *work)
4617{
4618 BUG_ON(work != &release_agent_work);
4619 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004620 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004621 while (!list_empty(&release_list)) {
4622 char *argv[3], *envp[3];
4623 int i;
Paul Menagee788e062008-07-25 01:46:59 -07004624 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004625 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004626 struct cgroup,
4627 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004628 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004629 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004630 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004631 if (!pathbuf)
4632 goto continue_free;
4633 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
4634 goto continue_free;
4635 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4636 if (!agentbuf)
4637 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004638
4639 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004640 argv[i++] = agentbuf;
4641 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004642 argv[i] = NULL;
4643
4644 i = 0;
4645 /* minimal command environment */
4646 envp[i++] = "HOME=/";
4647 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4648 envp[i] = NULL;
4649
4650 /* Drop the lock while we invoke the usermode helper,
4651 * since the exec could involve hitting disk and hence
4652 * be a slow process */
4653 mutex_unlock(&cgroup_mutex);
4654 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004655 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004656 continue_free:
4657 kfree(pathbuf);
4658 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004659 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004660 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004661 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004662 mutex_unlock(&cgroup_mutex);
4663}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004664
4665static int __init cgroup_disable(char *str)
4666{
Tejun Heo30159ec2013-06-25 11:53:37 -07004667 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004668 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07004669 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004670
4671 while ((token = strsep(&str, ",")) != NULL) {
4672 if (!*token)
4673 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004674
Tejun Heo3ed80a62014-02-08 10:36:58 -05004675 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004676 if (!strcmp(token, ss->name)) {
4677 ss->disabled = 1;
4678 printk(KERN_INFO "Disabling %s control group"
4679 " subsystem\n", ss->name);
4680 break;
4681 }
4682 }
4683 }
4684 return 1;
4685}
4686__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004687
Tejun Heob77d7b62013-08-13 11:01:54 -04004688/**
Tejun Heo5a17f542014-02-11 11:52:47 -05004689 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04004690 * @dentry: directory dentry of interest
4691 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04004692 *
Tejun Heo5a17f542014-02-11 11:52:47 -05004693 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4694 * to get the corresponding css and return it. If such css doesn't exist
4695 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02004696 */
Tejun Heo5a17f542014-02-11 11:52:47 -05004697struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4698 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004699{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004700 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4701 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004702 struct cgroup *cgrp;
Tejun Heob77d7b62013-08-13 11:01:54 -04004703
Tejun Heo35cf0832013-08-26 18:40:56 -04004704 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004705 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4706 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004707 return ERR_PTR(-EBADF);
4708
Tejun Heo5a17f542014-02-11 11:52:47 -05004709 rcu_read_lock();
4710
Tejun Heo2bd59d42014-02-11 11:52:49 -05004711 /*
4712 * This path doesn't originate from kernfs and @kn could already
4713 * have been or be removed at any point. @kn->priv is RCU
4714 * protected for this access. See destroy_locked() for details.
4715 */
4716 cgrp = rcu_dereference(kn->priv);
4717 if (cgrp)
4718 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05004719
4720 if (!css || !css_tryget(css))
4721 css = ERR_PTR(-ENOENT);
4722
4723 rcu_read_unlock();
4724 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004725}
Stephane Eraniane5d13672011-02-14 11:20:01 +02004726
Li Zefan1cb650b2013-08-19 10:05:24 +08004727/**
4728 * css_from_id - lookup css by id
4729 * @id: the cgroup id
4730 * @ss: cgroup subsys to be looked into
4731 *
4732 * Returns the css if there's valid one with @id, otherwise returns NULL.
4733 * Should be called under rcu_read_lock().
4734 */
4735struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4736{
4737 struct cgroup *cgrp;
4738
Tejun Heoace2bee2014-02-11 11:52:47 -05004739 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08004740
4741 cgrp = idr_find(&ss->root->cgroup_idr, id);
4742 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04004743 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08004744 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004745}
4746
Paul Menagefe693432009-09-23 15:56:20 -07004747#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04004748static struct cgroup_subsys_state *
4749debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07004750{
4751 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4752
4753 if (!css)
4754 return ERR_PTR(-ENOMEM);
4755
4756 return css;
4757}
4758
Tejun Heoeb954192013-08-08 20:11:23 -04004759static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07004760{
Tejun Heoeb954192013-08-08 20:11:23 -04004761 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07004762}
4763
Tejun Heo182446d2013-08-08 20:11:24 -04004764static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
4765 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004766{
Tejun Heo182446d2013-08-08 20:11:24 -04004767 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07004768}
4769
Tejun Heo182446d2013-08-08 20:11:24 -04004770static u64 current_css_set_read(struct cgroup_subsys_state *css,
4771 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004772{
4773 return (u64)(unsigned long)current->cgroups;
4774}
4775
Tejun Heo182446d2013-08-08 20:11:24 -04004776static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08004777 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004778{
4779 u64 count;
4780
4781 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07004782 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07004783 rcu_read_unlock();
4784 return count;
4785}
4786
Tejun Heo2da8ca82013-12-05 12:28:04 -05004787static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004788{
Tejun Heo69d02062013-06-12 21:04:50 -07004789 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07004790 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004791
4792 read_lock(&css_set_lock);
4793 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07004794 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07004795 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004796 struct cgroup *c = link->cgrp;
Tejun Heo59f52962014-02-11 11:52:49 -05004797 const char *name = "?";
Paul Menage7717f7b2009-09-23 15:56:22 -07004798
Tejun Heo59f52962014-02-11 11:52:49 -05004799 if (c != cgroup_dummy_top)
4800 name = cgroup_name(c);
4801
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004802 seq_printf(seq, "Root %d group %s\n",
4803 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07004804 }
4805 rcu_read_unlock();
4806 read_unlock(&css_set_lock);
4807 return 0;
4808}
4809
4810#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05004811static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004812{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004813 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07004814 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07004815
4816 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04004817 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004818 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004819 struct task_struct *task;
4820 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07004821 seq_printf(seq, "css_set %p\n", cset);
4822 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004823 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4824 seq_puts(seq, " ...\n");
4825 break;
4826 } else {
4827 seq_printf(seq, " task %d\n",
4828 task_pid_vnr(task));
4829 }
4830 }
4831 }
4832 read_unlock(&css_set_lock);
4833 return 0;
4834}
4835
Tejun Heo182446d2013-08-08 20:11:24 -04004836static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004837{
Tejun Heo182446d2013-08-08 20:11:24 -04004838 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07004839}
4840
4841static struct cftype debug_files[] = {
4842 {
Paul Menagefe693432009-09-23 15:56:20 -07004843 .name = "taskcount",
4844 .read_u64 = debug_taskcount_read,
4845 },
4846
4847 {
4848 .name = "current_css_set",
4849 .read_u64 = current_css_set_read,
4850 },
4851
4852 {
4853 .name = "current_css_set_refcount",
4854 .read_u64 = current_css_set_refcount_read,
4855 },
4856
4857 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004858 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004859 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004860 },
4861
4862 {
4863 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004864 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004865 },
4866
4867 {
Paul Menagefe693432009-09-23 15:56:20 -07004868 .name = "releasable",
4869 .read_u64 = releasable_read,
4870 },
Paul Menagefe693432009-09-23 15:56:20 -07004871
Tejun Heo4baf6e32012-04-01 12:09:55 -07004872 { } /* terminate */
4873};
Paul Menagefe693432009-09-23 15:56:20 -07004874
Tejun Heo073219e2014-02-08 10:36:58 -05004875struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08004876 .css_alloc = debug_css_alloc,
4877 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07004878 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07004879};
4880#endif /* CONFIG_CGROUP_DEBUG */