blob: 59dfb025f1ac66c8391838243d7fbbf7207c6aa4 [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
Tejun Heo6f305582014-02-12 09:29:50 -0500960 kernfs_put(cgrp->kn);
961
Li Zefan65dff752013-03-01 15:01:56 +0800962 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800963 kfree(cgrp);
964}
965
966static void cgroup_free_rcu(struct rcu_head *head)
967{
968 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
969
Tejun Heoea15f8c2013-06-13 19:27:42 -0700970 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500971 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800972}
973
Tejun Heo59f52962014-02-11 11:52:49 -0500974static void cgroup_get(struct cgroup *cgrp)
975{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500976 WARN_ON_ONCE(cgroup_is_dead(cgrp));
977 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
978 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700979}
980
Tejun Heo59f52962014-02-11 11:52:49 -0500981static void cgroup_put(struct cgroup *cgrp)
982{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500983 if (!atomic_dec_and_test(&cgrp->refcnt))
984 return;
985 if (WARN_ON_ONCE(!cgroup_is_dead(cgrp)))
986 return;
Tejun Heo59f52962014-02-11 11:52:49 -0500987
Tejun Heo2bd59d42014-02-11 11:52:49 -0500988 /*
989 * XXX: cgrp->id is only used to look up css's. As cgroup and
990 * css's lifetimes will be decoupled, it should be made
991 * per-subsystem and moved to css->id so that lookups are
992 * successful until the target css is released.
993 */
994 mutex_lock(&cgroup_mutex);
995 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
996 mutex_unlock(&cgroup_mutex);
997 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998
Tejun Heo2bd59d42014-02-11 11:52:49 -0500999 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000}
1001
Li Zefan2739d3c2013-01-21 18:18:33 +08001002static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001004 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001005
Tejun Heoace2bee2014-02-11 11:52:47 -05001006 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001007 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001008}
1009
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001010/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001011 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001012 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001013 * @subsys_mask: mask of the subsystem ids whose files should be removed
1014 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07001015static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001016{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001017 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001018 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001019
Tejun Heob420ba72013-07-12 12:34:02 -07001020 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001021 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001022
1023 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001024 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001025 list_for_each_entry(cfts, &ss->cfts, node)
1026 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001027 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028}
1029
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001031 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032{
Paul Menagebd89aab2007-10-18 23:40:44 -07001033 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001034 struct cgroup_subsys *ss;
Tejun Heo31261212013-06-28 17:07:30 -07001035 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036
Tejun Heoace2bee2014-02-11 11:52:47 -05001037 lockdep_assert_held(&cgroup_tree_mutex);
1038 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001039
Paul Menageddbcc7e2007-10-18 23:39:30 -07001040 /* Check that any added subsystems are currently free */
Tejun Heo3ed80a62014-02-08 10:36:58 -05001041 for_each_subsys(ss, i)
1042 if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
1043 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001044
Tejun Heo31261212013-06-28 17:07:30 -07001045 ret = cgroup_populate_dir(cgrp, added_mask);
1046 if (ret)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001047 return ret;
Tejun Heo31261212013-06-28 17:07:30 -07001048
1049 /*
1050 * Nothing can fail from this point on. Remove files for the
1051 * removed subsystems and rebind each subsystem.
1052 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001053 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001054 cgroup_clear_dir(cgrp, removed_mask);
Tejun Heo4ac06012014-02-11 11:52:47 -05001055 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001056
Tejun Heo30159ec2013-06-25 11:53:37 -07001057 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001059
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001060 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001062 BUG_ON(cgroup_css(cgrp, ss));
1063 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1064 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001065
Tejun Heo73e80ed2013-08-13 11:01:55 -04001066 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001067 cgroup_css(cgroup_dummy_top, ss));
1068 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001069
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001070 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001072 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001073
Ben Blumcf5d5942010-03-10 15:22:09 -08001074 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001075 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001076 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001078 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1079 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001080
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001082 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001083
Tejun Heoca8bdca2013-08-26 18:40:56 -04001084 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001085 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1086
Tejun Heo9871bf92013-06-24 15:21:47 -07001087 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001088 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089 }
1090 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091
Tejun Heo1672d042013-06-25 18:04:54 -07001092 /*
1093 * Mark @root has finished binding subsystems. @root->subsys_mask
1094 * now matches the bound subsystems.
1095 */
1096 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001097 kernfs_activate(cgrp->kn);
Tejun Heo1672d042013-06-25 18:04:54 -07001098
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099 return 0;
1100}
1101
Tejun Heo2bd59d42014-02-11 11:52:49 -05001102static int cgroup_show_options(struct seq_file *seq,
1103 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001105 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001107 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108
Tejun Heob85d2042013-12-06 15:11:57 -05001109 for_each_subsys(ss, ssid)
1110 if (root->subsys_mask & (1 << ssid))
1111 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001112 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1113 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001114 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001115 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001116 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001117 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001118
1119 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001120 if (strlen(root->release_agent_path))
1121 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001122 spin_unlock(&release_agent_path_lock);
1123
Tejun Heo2260e7f2012-11-19 08:13:38 -08001124 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001125 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001126 if (strlen(root->name))
1127 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128 return 0;
1129}
1130
1131struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001132 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001134 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001135 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001136 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001137 /* User explicitly requested empty subsystem */
1138 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001139};
1140
Ben Blumaae8aab2010-03-10 15:22:07 -08001141/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001142 * Convert a hierarchy specifier into a bitmask of subsystems and
1143 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1144 * array. This function takes refcounts on subsystems to be used, unless it
1145 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001146 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001147static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001148{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001149 char *token, *o = data;
1150 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001151 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001152 struct cgroup_subsys *ss;
1153 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001154
Ben Blumaae8aab2010-03-10 15:22:07 -08001155 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1156
Li Zefanf9ab5b52009-06-17 16:26:33 -07001157#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001158 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001159#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001160
Paul Menagec6d57f32009-09-23 15:56:19 -07001161 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001162
1163 while ((token = strsep(&o, ",")) != NULL) {
1164 if (!*token)
1165 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001166 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001167 /* Explicitly have no subsystems */
1168 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001169 continue;
1170 }
1171 if (!strcmp(token, "all")) {
1172 /* Mutually exclusive option 'all' + subsystem name */
1173 if (one_ss)
1174 return -EINVAL;
1175 all_ss = true;
1176 continue;
1177 }
Tejun Heo873fe092013-04-14 20:15:26 -07001178 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1179 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1180 continue;
1181 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001182 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001183 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001184 continue;
1185 }
1186 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001187 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001188 continue;
1189 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001190 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001191 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001192 continue;
1193 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001194 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001195 /* Specifying two release agents is forbidden */
1196 if (opts->release_agent)
1197 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001198 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001199 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001200 if (!opts->release_agent)
1201 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001202 continue;
1203 }
1204 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001205 const char *name = token + 5;
1206 /* Can't specify an empty name */
1207 if (!strlen(name))
1208 return -EINVAL;
1209 /* Must match [\w.-]+ */
1210 for (i = 0; i < strlen(name); i++) {
1211 char c = name[i];
1212 if (isalnum(c))
1213 continue;
1214 if ((c == '.') || (c == '-') || (c == '_'))
1215 continue;
1216 return -EINVAL;
1217 }
1218 /* Specifying two names is forbidden */
1219 if (opts->name)
1220 return -EINVAL;
1221 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001222 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001223 GFP_KERNEL);
1224 if (!opts->name)
1225 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001226
1227 continue;
1228 }
1229
Tejun Heo30159ec2013-06-25 11:53:37 -07001230 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001231 if (strcmp(token, ss->name))
1232 continue;
1233 if (ss->disabled)
1234 continue;
1235
1236 /* Mutually exclusive option 'all' + subsystem name */
1237 if (all_ss)
1238 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001239 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001240 one_ss = true;
1241
1242 break;
1243 }
1244 if (i == CGROUP_SUBSYS_COUNT)
1245 return -ENOENT;
1246 }
1247
1248 /*
1249 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001250 * otherwise if 'none', 'name=' and a subsystem name options
1251 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001252 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001253 if (all_ss || (!one_ss && !opts->none && !opts->name))
1254 for_each_subsys(ss, i)
1255 if (!ss->disabled)
1256 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001257
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001258 /* Consistency checks */
1259
Tejun Heo873fe092013-04-14 20:15:26 -07001260 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1261 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1262
1263 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1264 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1265 return -EINVAL;
1266 }
1267
1268 if (opts->cpuset_clone_children) {
1269 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1270 return -EINVAL;
1271 }
Tejun Heo86bf4b62014-02-12 09:29:48 -05001272
1273 if (opts->flags & CGRP_ROOT_XATTR)
1274 pr_warning("cgroup: sane_behavior: xattr is always available, flag unnecessary\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001275 }
1276
Li Zefanf9ab5b52009-06-17 16:26:33 -07001277 /*
1278 * Option noprefix was introduced just for backward compatibility
1279 * with the old cpuset, so we allow noprefix only if mounting just
1280 * the cpuset subsystem.
1281 */
Tejun Heo93438622013-04-14 20:15:25 -07001282 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001283 return -EINVAL;
1284
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001285
1286 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001287 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001288 return -EINVAL;
1289
1290 /*
1291 * We either have to specify by name or by subsystems. (So all
1292 * empty hierarchies must have a name).
1293 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001294 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001295 return -EINVAL;
1296
1297 return 0;
1298}
1299
Tejun Heo2bd59d42014-02-11 11:52:49 -05001300static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001301{
1302 int ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001303 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001305 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001306
Tejun Heo873fe092013-04-14 20:15:26 -07001307 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1308 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1309 return -EINVAL;
1310 }
1311
Tejun Heoace2bee2014-02-11 11:52:47 -05001312 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313 mutex_lock(&cgroup_mutex);
1314
1315 /* See what subsystems are wanted */
1316 ret = parse_cgroupfs_options(data, &opts);
1317 if (ret)
1318 goto out_unlock;
1319
Tejun Heoa8a648c2013-06-24 15:21:47 -07001320 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001321 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1322 task_tgid_nr(current), current->comm);
1323
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001324 added_mask = opts.subsys_mask & ~root->subsys_mask;
1325 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001326
Ben Blumcf5d5942010-03-10 15:22:09 -08001327 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001328 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001329 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001330 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1331 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1332 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001334 goto out_unlock;
1335 }
1336
Tejun Heof172e672013-06-28 17:07:30 -07001337 /* remounting is not allowed for populated hierarchies */
1338 if (root->number_of_cgroups > 1) {
1339 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001340 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001341 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342
Paul Menageddbcc7e2007-10-18 23:39:30 -07001343 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001344 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001345 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346
Tejun Heo69e943b2014-02-08 10:36:58 -05001347 if (opts.release_agent) {
1348 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001349 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001350 spin_unlock(&release_agent_path_lock);
1351 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001352 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001353 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001354 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001355 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001356 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001357 return ret;
1358}
1359
Paul Menagecc31edc2008-10-18 20:28:04 -07001360static void init_cgroup_housekeeping(struct cgroup *cgrp)
1361{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001362 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001363 INIT_LIST_HEAD(&cgrp->sibling);
1364 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001365 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001366 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001367 INIT_LIST_HEAD(&cgrp->pidlists);
1368 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001369 cgrp->dummy_css.cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001370}
Paul Menagec6d57f32009-09-23 15:56:19 -07001371
Paul Menageddbcc7e2007-10-18 23:39:30 -07001372static void init_cgroup_root(struct cgroupfs_root *root)
1373{
Paul Menagebd89aab2007-10-18 23:40:44 -07001374 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001375
Tejun Heo2bd59d42014-02-11 11:52:49 -05001376 atomic_set(&root->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001377 INIT_LIST_HEAD(&root->root_list);
1378 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001379 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001380 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001381 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001382 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383}
1384
Paul Menagec6d57f32009-09-23 15:56:19 -07001385static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1386{
1387 struct cgroupfs_root *root;
1388
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001389 if (!opts->subsys_mask && !opts->none)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001390 return ERR_PTR(-EINVAL);
Paul Menagec6d57f32009-09-23 15:56:19 -07001391
1392 root = kzalloc(sizeof(*root), GFP_KERNEL);
1393 if (!root)
1394 return ERR_PTR(-ENOMEM);
1395
1396 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001397
Tejun Heo1672d042013-06-25 18:04:54 -07001398 /*
1399 * We need to set @root->subsys_mask now so that @root can be
1400 * matched by cgroup_test_super() before it finishes
1401 * initialization; otherwise, competing mounts with the same
1402 * options may try to bind the same subsystems instead of waiting
1403 * for the first one leading to unexpected mount errors.
1404 * SUBSYS_BOUND will be set once actual binding is complete.
1405 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001406 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001407 root->flags = opts->flags;
1408 if (opts->release_agent)
1409 strcpy(root->release_agent_path, opts->release_agent);
1410 if (opts->name)
1411 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001412 if (opts->cpuset_clone_children)
1413 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001414 return root;
1415}
1416
Tejun Heod427dfe2014-02-11 11:52:48 -05001417static int cgroup_setup_root(struct cgroupfs_root *root)
1418{
1419 LIST_HEAD(tmp_links);
Tejun Heod427dfe2014-02-11 11:52:48 -05001420 struct cgroup *root_cgrp = &root->top_cgroup;
Tejun Heod427dfe2014-02-11 11:52:48 -05001421 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001422 int i, ret;
1423
1424 lockdep_assert_held(&cgroup_tree_mutex);
1425 lockdep_assert_held(&cgroup_mutex);
Tejun Heod427dfe2014-02-11 11:52:48 -05001426
1427 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1428 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001429 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001430 root_cgrp->id = ret;
1431
Tejun Heod427dfe2014-02-11 11:52:48 -05001432 /*
1433 * We're accessing css_set_count without locking css_set_lock here,
1434 * but that's OK - it can only be increased by someone holding
1435 * cgroup_lock, and that's us. The worst that can happen is that we
1436 * have some link structures left over
1437 */
1438 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1439 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001440 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001441
1442 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1443 ret = cgroup_init_root_id(root, 2, 0);
1444 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001445 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001446
Tejun Heo2bd59d42014-02-11 11:52:49 -05001447 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1448 KERNFS_ROOT_CREATE_DEACTIVATED,
1449 root_cgrp);
1450 if (IS_ERR(root->kf_root)) {
1451 ret = PTR_ERR(root->kf_root);
1452 goto exit_root_id;
1453 }
1454 root_cgrp->kn = root->kf_root->kn;
Tejun Heod427dfe2014-02-11 11:52:48 -05001455
1456 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1457 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001458 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001459
1460 ret = rebind_subsystems(root, root->subsys_mask, 0);
1461 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001462 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001463
1464 /*
1465 * There must be no failure case after here, since rebinding takes
1466 * care of subsystems' refcounts, which are explicitly dropped in
1467 * the failure exit path.
1468 */
1469 list_add(&root->root_list, &cgroup_roots);
1470 cgroup_root_count++;
1471
1472 /*
1473 * Link the top cgroup in this hierarchy into all the css_set
1474 * objects.
1475 */
1476 write_lock(&css_set_lock);
1477 hash_for_each(css_set_table, i, cset, hlist)
1478 link_css_set(&tmp_links, cset, root_cgrp);
1479 write_unlock(&css_set_lock);
1480
1481 BUG_ON(!list_empty(&root_cgrp->children));
1482 BUG_ON(root->number_of_cgroups != 1);
1483
Tejun Heo2bd59d42014-02-11 11:52:49 -05001484 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001485 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001486 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001487
Tejun Heo2bd59d42014-02-11 11:52:49 -05001488destroy_root:
1489 kernfs_destroy_root(root->kf_root);
1490 root->kf_root = NULL;
1491exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001492 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001493out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001494 free_cgrp_cset_links(&tmp_links);
1495 return ret;
1496}
1497
Al Virof7e83572010-07-26 13:23:11 +04001498static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001499 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001500 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001501{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001502 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001503 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001504 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001505 int ret;
1506
1507 mutex_lock(&cgroup_tree_mutex);
1508 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001509
1510 /* First find the desired set of subsystems */
1511 ret = parse_cgroupfs_options(data, &opts);
Paul Menagec6d57f32009-09-23 15:56:19 -07001512 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001513 goto out_unlock;
Paul Menagec6d57f32009-09-23 15:56:19 -07001514
Tejun Heo2bd59d42014-02-11 11:52:49 -05001515 /* look for a matching existing root */
1516 for_each_active_root(root) {
1517 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001518
Paul Menagec6d57f32009-09-23 15:56:19 -07001519 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001520 * If we asked for a name then it must match. Also, if
1521 * name matches but sybsys_mask doesn't, we should fail.
1522 * Remember whether name matched.
Paul Menagec6d57f32009-09-23 15:56:19 -07001523 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001524 if (opts.name) {
1525 if (strcmp(opts.name, root->name))
1526 continue;
1527 name_match = true;
1528 }
1529
1530 /*
1531 * If we asked for subsystems (or explicitly for no
1532 * subsystems) then they must match.
1533 */
1534 if ((opts.subsys_mask || opts.none) &&
1535 (opts.subsys_mask != root->subsys_mask)) {
1536 if (!name_match)
1537 continue;
1538 ret = -EBUSY;
1539 goto out_unlock;
1540 }
Tejun Heo873fe092013-04-14 20:15:26 -07001541
Tejun Heoc7ba8282013-06-29 14:06:10 -07001542 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001543 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1544 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1545 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001546 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001547 } else {
1548 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1549 }
Tejun Heo873fe092013-04-14 20:15:26 -07001550 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001551
1552 cgroup_get_root(root);
1553 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554 }
1555
Tejun Heo2bd59d42014-02-11 11:52:49 -05001556 /* no such thing, create a new one */
1557 root = cgroup_root_from_opts(&opts);
1558 if (IS_ERR(root)) {
1559 ret = PTR_ERR(root);
1560 goto out_unlock;
1561 }
1562
1563 ret = cgroup_setup_root(root);
1564 if (ret)
1565 cgroup_free_root(root);
1566
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001567out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001568 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001569 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001570
Paul Menagec6d57f32009-09-23 15:56:19 -07001571 kfree(opts.release_agent);
1572 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001573
Tejun Heo2bd59d42014-02-11 11:52:49 -05001574 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001575 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001576
1577 dentry = kernfs_mount(fs_type, flags, root->kf_root);
1578 if (IS_ERR(dentry))
1579 cgroup_put_root(root);
1580 return dentry;
1581}
1582
1583static void cgroup_kill_sb(struct super_block *sb)
1584{
1585 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
1586 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
1587
1588 cgroup_put_root(root);
1589 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001590}
1591
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592static struct file_system_type cgroup_fs_type = {
1593 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001594 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595 .kill_sb = cgroup_kill_sb,
1596};
1597
Greg KH676db4a2010-08-05 13:53:35 -07001598static struct kobject *cgroup_kobj;
1599
Li Zefana043e3b2008-02-23 15:24:09 -08001600/**
1601 * cgroup_path - generate the path of a cgroup
1602 * @cgrp: the cgroup in question
1603 * @buf: the buffer to write the path into
1604 * @buflen: the length of the buffer
1605 *
Li Zefan65dff752013-03-01 15:01:56 +08001606 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1607 *
1608 * We can't generate cgroup path using dentry->d_name, as accessing
1609 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1610 * inode's i_mutex, while on the other hand cgroup_path() can be called
1611 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001613int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614{
Li Zefan65dff752013-03-01 15:01:56 +08001615 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001616 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001617
Tejun Heoda1f2962013-04-14 10:32:19 -07001618 if (!cgrp->parent) {
1619 if (strlcpy(buf, "/", buflen) >= buflen)
1620 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621 return 0;
1622 }
1623
Tao Ma316eb662012-11-08 21:36:38 +08001624 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001625 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001626
Li Zefan65dff752013-03-01 15:01:56 +08001627 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001628 do {
Li Zefan65dff752013-03-01 15:01:56 +08001629 const char *name = cgroup_name(cgrp);
1630 int len;
1631
1632 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001633 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001634 goto out;
1635 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001636
Paul Menageddbcc7e2007-10-18 23:39:30 -07001637 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001638 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001639 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001640
1641 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001642 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001643 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001645out:
1646 rcu_read_unlock();
1647 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001648}
Ben Blum67523c42010-03-10 15:22:11 -08001649EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001650
Tejun Heo857a2be2013-04-14 20:50:08 -07001651/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001652 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001653 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001654 * @buf: the buffer to write the path into
1655 * @buflen: the length of the buffer
1656 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001657 * Determine @task's cgroup on the first (the one with the lowest non-zero
1658 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1659 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1660 * cgroup controller callbacks.
1661 *
1662 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001663 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001664int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001665{
1666 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001667 struct cgroup *cgrp;
1668 int hierarchy_id = 1, ret = 0;
1669
1670 if (buflen < 2)
1671 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001672
1673 mutex_lock(&cgroup_mutex);
1674
Tejun Heo913ffdb2013-07-11 16:34:48 -07001675 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1676
Tejun Heo857a2be2013-04-14 20:50:08 -07001677 if (root) {
1678 cgrp = task_cgroup_from_root(task, root);
1679 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001680 } else {
1681 /* if no hierarchy exists, everyone is in "/" */
1682 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001683 }
1684
1685 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001686 return ret;
1687}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001688EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001689
Ben Blum74a11662011-05-26 16:25:20 -07001690/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001691 * Control Group taskset
1692 */
Tejun Heo134d3372011-12-12 18:12:21 -08001693struct task_and_cgroup {
1694 struct task_struct *task;
1695 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001696 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001697};
1698
Tejun Heo2f7ee562011-12-12 18:12:21 -08001699struct cgroup_taskset {
1700 struct task_and_cgroup single;
1701 struct flex_array *tc_array;
1702 int tc_array_len;
1703 int idx;
1704 struct cgroup *cur_cgrp;
1705};
1706
1707/**
1708 * cgroup_taskset_first - reset taskset and return the first task
1709 * @tset: taskset of interest
1710 *
1711 * @tset iteration is initialized and the first task is returned.
1712 */
1713struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1714{
1715 if (tset->tc_array) {
1716 tset->idx = 0;
1717 return cgroup_taskset_next(tset);
1718 } else {
1719 tset->cur_cgrp = tset->single.cgrp;
1720 return tset->single.task;
1721 }
1722}
1723EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1724
1725/**
1726 * cgroup_taskset_next - iterate to the next task in taskset
1727 * @tset: taskset of interest
1728 *
1729 * Return the next task in @tset. Iteration must have been initialized
1730 * with cgroup_taskset_first().
1731 */
1732struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1733{
1734 struct task_and_cgroup *tc;
1735
1736 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1737 return NULL;
1738
1739 tc = flex_array_get(tset->tc_array, tset->idx++);
1740 tset->cur_cgrp = tc->cgrp;
1741 return tc->task;
1742}
1743EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1744
1745/**
Tejun Heod99c8722013-08-08 20:11:27 -04001746 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001747 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001748 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001749 *
Tejun Heod99c8722013-08-08 20:11:27 -04001750 * Return the css for the current (last returned) task of @tset for
1751 * subsystem specified by @subsys_id. This function must be preceded by
1752 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001753 */
Tejun Heod99c8722013-08-08 20:11:27 -04001754struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1755 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001756{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001757 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001758}
Tejun Heod99c8722013-08-08 20:11:27 -04001759EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001760
1761/**
1762 * cgroup_taskset_size - return the number of tasks in taskset
1763 * @tset: taskset of interest
1764 */
1765int cgroup_taskset_size(struct cgroup_taskset *tset)
1766{
1767 return tset->tc_array ? tset->tc_array_len : 1;
1768}
1769EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1770
1771
Ben Blum74a11662011-05-26 16:25:20 -07001772/*
1773 * cgroup_task_migrate - move a task from one cgroup to another.
1774 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001775 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001776 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001777static void cgroup_task_migrate(struct cgroup *old_cgrp,
1778 struct task_struct *tsk,
1779 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001780{
Tejun Heo5abb8852013-06-12 21:04:49 -07001781 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001782
1783 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001784 * We are synchronized through threadgroup_lock() against PF_EXITING
1785 * setting such that we can't race against cgroup_exit() changing the
1786 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001787 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001788 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001789 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001790
Ben Blum74a11662011-05-26 16:25:20 -07001791 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001792 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001793 task_unlock(tsk);
1794
1795 /* Update the css_set linked lists if we're using them */
1796 write_lock(&css_set_lock);
1797 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001798 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001799 write_unlock(&css_set_lock);
1800
1801 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001802 * We just gained a reference on old_cset by taking it from the
1803 * task. As trading it for new_cset is protected by cgroup_mutex,
1804 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001805 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001806 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1807 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001808}
1809
Li Zefana043e3b2008-02-23 15:24:09 -08001810/**
Li Zefan081aa452013-03-13 09:17:09 +08001811 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001812 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001813 * @tsk: the task or the leader of the threadgroup to be attached
1814 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001815 *
Tejun Heo257058a2011-12-12 18:12:21 -08001816 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001817 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001818 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001819static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1820 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001821{
1822 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001823 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001824 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001825 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001826 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001827 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001828 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001829 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001830
1831 /*
1832 * step 0: in order to do expensive, possibly blocking operations for
1833 * every thread, we cannot iterate the thread group list, since it needs
1834 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001835 * group - group_rwsem prevents new threads from appearing, and if
1836 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001837 */
Li Zefan081aa452013-03-13 09:17:09 +08001838 if (threadgroup)
1839 group_size = get_nr_threads(tsk);
1840 else
1841 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001842 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001843 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001844 if (!group)
1845 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001846 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001847 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001848 if (retval)
1849 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001850
Ben Blum74a11662011-05-26 16:25:20 -07001851 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001852 /*
1853 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1854 * already PF_EXITING could be freed from underneath us unless we
1855 * take an rcu_read_lock.
1856 */
1857 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001858 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001859 struct task_and_cgroup ent;
1860
Tejun Heocd3d0952011-12-12 18:12:21 -08001861 /* @tsk either already exited or can't exit until the end */
1862 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001863 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001864
Ben Blum74a11662011-05-26 16:25:20 -07001865 /* as per above, nr_threads may decrease, but not increase. */
1866 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001867 ent.task = tsk;
1868 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001869 /* nothing to do if this task is already in the cgroup */
1870 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001871 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001872 /*
1873 * saying GFP_ATOMIC has no effect here because we did prealloc
1874 * earlier, but it's good form to communicate our expectations.
1875 */
Tejun Heo134d3372011-12-12 18:12:21 -08001876 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07001877 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07001878 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08001879 next:
Li Zefan081aa452013-03-13 09:17:09 +08001880 if (!threadgroup)
1881 break;
Ben Blum74a11662011-05-26 16:25:20 -07001882 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001883 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07001884 /* remember the number of threads in the array for later. */
1885 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001886 tset.tc_array = group;
1887 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001888
Tejun Heo134d3372011-12-12 18:12:21 -08001889 /* methods shouldn't be called if no task is actually migrating */
1890 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001891 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08001892 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08001893
Ben Blum74a11662011-05-26 16:25:20 -07001894 /*
1895 * step 1: check that we can legitimately attach to the cgroup.
1896 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001897 for_each_css(css, i, cgrp) {
1898 if (css->ss->can_attach) {
1899 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001900 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001901 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07001902 goto out_cancel_attach;
1903 }
1904 }
Ben Blum74a11662011-05-26 16:25:20 -07001905 }
1906
1907 /*
1908 * step 2: make sure css_sets exist for all threads to be migrated.
1909 * we use find_css_set, which allocates a new one if necessary.
1910 */
Ben Blum74a11662011-05-26 16:25:20 -07001911 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07001912 struct css_set *old_cset;
1913
Tejun Heo134d3372011-12-12 18:12:21 -08001914 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001915 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001916 tc->cset = find_css_set(old_cset, cgrp);
1917 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001918 retval = -ENOMEM;
1919 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07001920 }
1921 }
1922
1923 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001924 * step 3: now that we're guaranteed success wrt the css_sets,
1925 * proceed to move all tasks to the new cgroup. There are no
1926 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07001927 */
Ben Blum74a11662011-05-26 16:25:20 -07001928 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08001929 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001930 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07001931 }
1932 /* nothing is sensitive to fork() after this point. */
1933
1934 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001935 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07001936 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001937 for_each_css(css, i, cgrp)
1938 if (css->ss->attach)
1939 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001940
1941 /*
1942 * step 5: success! and cleanup
1943 */
Ben Blum74a11662011-05-26 16:25:20 -07001944 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001945out_put_css_set_refs:
1946 if (retval) {
1947 for (i = 0; i < group_size; i++) {
1948 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001949 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001950 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001951 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001952 }
Ben Blum74a11662011-05-26 16:25:20 -07001953 }
1954out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07001955 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001956 for_each_css(css, i, cgrp) {
1957 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07001958 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001959 if (css->ss->cancel_attach)
1960 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001961 }
1962 }
Ben Blum74a11662011-05-26 16:25:20 -07001963out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07001964 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07001965 return retval;
1966}
1967
1968/*
1969 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08001970 * function to attach either it or all tasks in its threadgroup. Will lock
1971 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07001972 */
1973static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001974{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001975 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001976 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001977 int ret;
1978
Ben Blum74a11662011-05-26 16:25:20 -07001979 if (!cgroup_lock_live_group(cgrp))
1980 return -ENODEV;
1981
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001982retry_find_task:
1983 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001984 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001985 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07001986 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07001987 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001988 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001989 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001990 }
Ben Blum74a11662011-05-26 16:25:20 -07001991 /*
1992 * even if we're attaching all tasks in the thread group, we
1993 * only need to check permissions on one of them.
1994 */
David Howellsc69e8d92008-11-14 10:39:19 +11001995 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07001996 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
1997 !uid_eq(cred->euid, tcred->uid) &&
1998 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001999 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002000 ret = -EACCES;
2001 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002002 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002003 } else
2004 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002005
2006 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002007 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002008
2009 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002010 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002011 * trapped in a cpuset, or RT worker may be born in a cgroup
2012 * with no rt_runtime allocated. Just say no.
2013 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002014 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002015 ret = -EINVAL;
2016 rcu_read_unlock();
2017 goto out_unlock_cgroup;
2018 }
2019
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002020 get_task_struct(tsk);
2021 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002022
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002023 threadgroup_lock(tsk);
2024 if (threadgroup) {
2025 if (!thread_group_leader(tsk)) {
2026 /*
2027 * a race with de_thread from another thread's exec()
2028 * may strip us of our leadership, if this happens,
2029 * there is no choice but to throw this task away and
2030 * try again; this is
2031 * "double-double-toil-and-trouble-check locking".
2032 */
2033 threadgroup_unlock(tsk);
2034 put_task_struct(tsk);
2035 goto retry_find_task;
2036 }
Li Zefan081aa452013-03-13 09:17:09 +08002037 }
2038
2039 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2040
Tejun Heocd3d0952011-12-12 18:12:21 -08002041 threadgroup_unlock(tsk);
2042
Paul Menagebbcb81d2007-10-18 23:39:32 -07002043 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002044out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002045 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002046 return ret;
2047}
2048
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002049/**
2050 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2051 * @from: attach to all cgroups of a given task
2052 * @tsk: the task to be attached
2053 */
2054int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2055{
2056 struct cgroupfs_root *root;
2057 int retval = 0;
2058
Tejun Heo47cfcd02013-04-07 09:29:51 -07002059 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002060 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002061 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002062
Li Zefan6f4b7e62013-07-31 16:18:36 +08002063 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002064 if (retval)
2065 break;
2066 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002067 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002068
2069 return retval;
2070}
2071EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2072
Tejun Heo182446d2013-08-08 20:11:24 -04002073static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2074 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002075{
Tejun Heo182446d2013-08-08 20:11:24 -04002076 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002077}
2078
Tejun Heo182446d2013-08-08 20:11:24 -04002079static int cgroup_procs_write(struct cgroup_subsys_state *css,
2080 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002081{
Tejun Heo182446d2013-08-08 20:11:24 -04002082 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002083}
2084
Tejun Heo182446d2013-08-08 20:11:24 -04002085static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2086 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002087{
Tejun Heo5f469902014-02-11 11:52:48 -05002088 struct cgroupfs_root *root = css->cgroup->root;
2089
2090 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002091 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002092 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002093 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002094 strlcpy(root->release_agent_path, buffer,
2095 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002096 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002097 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002098 return 0;
2099}
2100
Tejun Heo2da8ca82013-12-05 12:28:04 -05002101static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002102{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002103 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002104
Paul Menagee788e062008-07-25 01:46:59 -07002105 if (!cgroup_lock_live_group(cgrp))
2106 return -ENODEV;
2107 seq_puts(seq, cgrp->root->release_agent_path);
2108 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002109 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002110 return 0;
2111}
2112
Tejun Heo2da8ca82013-12-05 12:28:04 -05002113static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002114{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002115 struct cgroup *cgrp = seq_css(seq)->cgroup;
2116
2117 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002118 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002119}
2120
Tejun Heo2bd59d42014-02-11 11:52:49 -05002121static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2122 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002123{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002124 struct cgroup *cgrp = of->kn->parent->priv;
2125 struct cftype *cft = of->kn->priv;
2126 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002127 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002128
Tejun Heo2bd59d42014-02-11 11:52:49 -05002129 /*
2130 * kernfs guarantees that a file isn't deleted with operations in
2131 * flight, which means that the matching css is and stays alive and
2132 * doesn't need to be pinned. The RCU locking is not necessary
2133 * either. It's just for the convenience of using cgroup_css().
2134 */
2135 rcu_read_lock();
2136 css = cgroup_css(cgrp, cft->ss);
2137 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002138
Tejun Heoa742c592013-12-05 12:28:03 -05002139 if (cft->write_string) {
2140 ret = cft->write_string(css, cft, strstrip(buf));
2141 } else if (cft->write_u64) {
2142 unsigned long long v;
2143 ret = kstrtoull(buf, 0, &v);
2144 if (!ret)
2145 ret = cft->write_u64(css, cft, v);
2146 } else if (cft->write_s64) {
2147 long long v;
2148 ret = kstrtoll(buf, 0, &v);
2149 if (!ret)
2150 ret = cft->write_s64(css, cft, v);
2151 } else if (cft->trigger) {
2152 ret = cft->trigger(css, (unsigned int)cft->private);
2153 } else {
2154 ret = -EINVAL;
2155 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002156
Tejun Heoa742c592013-12-05 12:28:03 -05002157 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002158}
2159
Tejun Heo6612f052013-12-05 12:28:04 -05002160static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002161{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002162 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002163}
2164
2165static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2166{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002167 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002168}
2169
2170static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2171{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002172 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002173}
2174
2175static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2176{
Tejun Heo7da11272013-12-05 12:28:04 -05002177 struct cftype *cft = seq_cft(m);
2178 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002179
Tejun Heo2da8ca82013-12-05 12:28:04 -05002180 if (cft->seq_show)
2181 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002182
Tejun Heo896f5192013-12-05 12:28:04 -05002183 if (cft->read_u64)
2184 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2185 else if (cft->read_s64)
2186 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2187 else
2188 return -EINVAL;
2189 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002190}
2191
Tejun Heo2bd59d42014-02-11 11:52:49 -05002192static struct kernfs_ops cgroup_kf_single_ops = {
2193 .atomic_write_len = PAGE_SIZE,
2194 .write = cgroup_file_write,
2195 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002196};
2197
Tejun Heo2bd59d42014-02-11 11:52:49 -05002198static struct kernfs_ops cgroup_kf_ops = {
2199 .atomic_write_len = PAGE_SIZE,
2200 .write = cgroup_file_write,
2201 .seq_start = cgroup_seqfile_start,
2202 .seq_next = cgroup_seqfile_next,
2203 .seq_stop = cgroup_seqfile_stop,
2204 .seq_show = cgroup_seqfile_show,
2205};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002206
2207/*
2208 * cgroup_rename - Only allow simple rename of directories in place.
2209 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002210static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2211 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002212{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002213 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002214 struct cgroup_name *name, *old_name;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002215 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002216
Tejun Heo2bd59d42014-02-11 11:52:49 -05002217 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002218 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002219 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002220 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002221
Tejun Heo6db8e852013-06-14 11:18:22 -07002222 /*
2223 * This isn't a proper migration and its usefulness is very
2224 * limited. Disallow if sane_behavior.
2225 */
2226 if (cgroup_sane_behavior(cgrp))
2227 return -EPERM;
2228
Tejun Heo2bd59d42014-02-11 11:52:49 -05002229 name = cgroup_alloc_name(new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002230 if (!name)
2231 return -ENOMEM;
2232
Tejun Heo2bd59d42014-02-11 11:52:49 -05002233 mutex_lock(&cgroup_tree_mutex);
2234 mutex_lock(&cgroup_mutex);
2235
2236 ret = kernfs_rename(kn, new_parent, new_name_str);
2237 if (!ret) {
2238 old_name = rcu_dereference_protected(cgrp->name, true);
2239 rcu_assign_pointer(cgrp->name, name);
2240 } else {
2241 old_name = name;
Li Zefan65dff752013-03-01 15:01:56 +08002242 }
2243
Tejun Heo2bd59d42014-02-11 11:52:49 -05002244 mutex_unlock(&cgroup_mutex);
2245 mutex_unlock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002246
2247 kfree_rcu(old_name, rcu_head);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002248 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002249}
2250
Tejun Heo2bb566c2013-08-08 20:11:23 -04002251static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002252{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002253 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002254 struct kernfs_node *kn;
2255 struct lock_class_key *key = NULL;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002256
Tejun Heo2bd59d42014-02-11 11:52:49 -05002257#ifdef CONFIG_DEBUG_LOCK_ALLOC
2258 key = &cft->lockdep_key;
2259#endif
2260 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2261 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2262 NULL, false, key);
2263 if (IS_ERR(kn))
2264 return PTR_ERR(kn);
2265 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002266}
2267
Tejun Heob1f28d32013-06-28 16:24:10 -07002268/**
2269 * cgroup_addrm_files - add or remove files to a cgroup directory
2270 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002271 * @cfts: array of cftypes to be added
2272 * @is_add: whether to add or remove
2273 *
2274 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002275 * For removals, this function never fails. If addition fails, this
2276 * function doesn't remove files already added. The caller is responsible
2277 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002278 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002279static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2280 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002281{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002282 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002283 int ret;
2284
Tejun Heoace2bee2014-02-11 11:52:47 -05002285 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002286
2287 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002288 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002289 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2290 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002291 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2292 continue;
2293 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2294 continue;
2295
Li Zefan2739d3c2013-01-21 18:18:33 +08002296 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002297 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002298 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002299 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002300 cft->name, ret);
2301 return ret;
2302 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002303 } else {
2304 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002305 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002306 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002307 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002308}
2309
Tejun Heo21a2d342014-02-12 09:29:49 -05002310static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002311{
2312 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002313 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002314 struct cgroup *root = &ss->root->top_cgroup;
Tejun Heo492eb212013-08-08 20:11:25 -04002315 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002316 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002317
Tejun Heo21a2d342014-02-12 09:29:49 -05002318 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo4ac06012014-02-11 11:52:47 -05002319
Tejun Heo21a2d342014-02-12 09:29:49 -05002320 /* don't bother if @ss isn't attached */
2321 if (ss->root == &cgroup_dummy_root)
Tejun Heo9ccece82013-06-28 16:24:11 -07002322 return 0;
Li Zefane8c82d22013-06-18 18:48:37 +08002323
Li Zefane8c82d22013-06-18 18:48:37 +08002324 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002325 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002326 struct cgroup *cgrp = css->cgroup;
2327
Li Zefane8c82d22013-06-18 18:48:37 +08002328 if (cgroup_is_dead(cgrp))
2329 continue;
2330
Tejun Heo21a2d342014-02-12 09:29:49 -05002331 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002332 if (ret)
2333 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002334 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002335
2336 if (is_add && !ret)
2337 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002338 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002339}
2340
Tejun Heo2da440a2014-02-11 11:52:48 -05002341static void cgroup_exit_cftypes(struct cftype *cfts)
2342{
2343 struct cftype *cft;
2344
Tejun Heo2bd59d42014-02-11 11:52:49 -05002345 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2346 /* free copy for custom atomic_write_len, see init_cftypes() */
2347 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2348 kfree(cft->kf_ops);
2349 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002350 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002351 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002352}
2353
Tejun Heo2bd59d42014-02-11 11:52:49 -05002354static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002355{
2356 struct cftype *cft;
2357
Tejun Heo2bd59d42014-02-11 11:52:49 -05002358 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2359 struct kernfs_ops *kf_ops;
2360
Tejun Heo0adb0702014-02-12 09:29:48 -05002361 WARN_ON(cft->ss || cft->kf_ops);
2362
Tejun Heo2bd59d42014-02-11 11:52:49 -05002363 if (cft->seq_start)
2364 kf_ops = &cgroup_kf_ops;
2365 else
2366 kf_ops = &cgroup_kf_single_ops;
2367
2368 /*
2369 * Ugh... if @cft wants a custom max_write_len, we need to
2370 * make a copy of kf_ops to set its atomic_write_len.
2371 */
2372 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2373 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2374 if (!kf_ops) {
2375 cgroup_exit_cftypes(cfts);
2376 return -ENOMEM;
2377 }
2378 kf_ops->atomic_write_len = cft->max_write_len;
2379 }
2380
2381 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002382 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002383 }
2384
2385 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002386}
2387
Tejun Heo21a2d342014-02-12 09:29:49 -05002388static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2389{
2390 lockdep_assert_held(&cgroup_tree_mutex);
2391
2392 if (!cfts || !cfts[0].ss)
2393 return -ENOENT;
2394
2395 list_del(&cfts->node);
2396 cgroup_apply_cftypes(cfts, false);
2397 cgroup_exit_cftypes(cfts);
2398 return 0;
2399}
2400
Tejun Heo8e3f6542012-04-01 12:09:55 -07002401/**
Tejun Heo80b13582014-02-12 09:29:48 -05002402 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2403 * @cfts: zero-length name terminated array of cftypes
2404 *
2405 * Unregister @cfts. Files described by @cfts are removed from all
2406 * existing cgroups and all future cgroups won't have them either. This
2407 * function can be called anytime whether @cfts' subsys is attached or not.
2408 *
2409 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2410 * registered.
2411 */
2412int cgroup_rm_cftypes(struct cftype *cfts)
2413{
Tejun Heo21a2d342014-02-12 09:29:49 -05002414 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002415
Tejun Heo21a2d342014-02-12 09:29:49 -05002416 mutex_lock(&cgroup_tree_mutex);
2417 ret = cgroup_rm_cftypes_locked(cfts);
2418 mutex_unlock(&cgroup_tree_mutex);
2419 return ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002420}
2421
2422/**
Tejun Heo8e3f6542012-04-01 12:09:55 -07002423 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2424 * @ss: target cgroup subsystem
2425 * @cfts: zero-length name terminated array of cftypes
2426 *
2427 * Register @cfts to @ss. Files described by @cfts are created for all
2428 * existing cgroups to which @ss is attached and all future cgroups will
2429 * have them too. This function can be called anytime whether @ss is
2430 * attached or not.
2431 *
2432 * Returns 0 on successful registration, -errno on failure. Note that this
2433 * function currently returns 0 as long as @cfts registration is successful
2434 * even if some file creation attempts on existing cgroups fail.
2435 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002436int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002437{
Tejun Heo9ccece82013-06-28 16:24:11 -07002438 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002439
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 Heo21a2d342014-02-12 09:29:49 -05002444 mutex_lock(&cgroup_tree_mutex);
2445
Tejun Heo0adb0702014-02-12 09:29:48 -05002446 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05002447 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002448 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05002449 cgroup_rm_cftypes_locked(cfts);
2450
2451 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002452 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002453}
2454EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2455
Li Zefana043e3b2008-02-23 15:24:09 -08002456/**
2457 * cgroup_task_count - count the number of tasks in a cgroup.
2458 * @cgrp: the cgroup in question
2459 *
2460 * Return the number of tasks in the cgroup.
2461 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002462int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002463{
2464 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002465 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002466
Paul Menage817929e2007-10-18 23:39:36 -07002467 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002468 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2469 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002470 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002471 return count;
2472}
2473
2474/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002475 * To reduce the fork() overhead for systems that are not actually using
2476 * their cgroups capability, we don't maintain the lists running through
2477 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002478 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002479 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002480static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002481{
2482 struct task_struct *p, *g;
2483 write_lock(&css_set_lock);
2484 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002485 /*
2486 * We need tasklist_lock because RCU is not safe against
2487 * while_each_thread(). Besides, a forking task that has passed
2488 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2489 * is not guaranteed to have its child immediately visible in the
2490 * tasklist if we walk through it with RCU.
2491 */
2492 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002493 do_each_thread(g, p) {
2494 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002495 /*
2496 * We should check if the process is exiting, otherwise
2497 * it will race with cgroup_exit() in that the list
2498 * entry won't be deleted though the process has exited.
2499 */
2500 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002501 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002502 task_unlock(p);
2503 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002504 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002505 write_unlock(&css_set_lock);
2506}
2507
Tejun Heo574bd9f2012-11-09 09:12:29 -08002508/**
Tejun Heo492eb212013-08-08 20:11:25 -04002509 * css_next_child - find the next child of a given css
2510 * @pos_css: the current position (%NULL to initiate traversal)
2511 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002512 *
Tejun Heo492eb212013-08-08 20:11:25 -04002513 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002514 * under either cgroup_mutex or RCU read lock. The only requirement is
2515 * that @parent_css and @pos_css are accessible. The next sibling is
2516 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002517 */
Tejun Heo492eb212013-08-08 20:11:25 -04002518struct cgroup_subsys_state *
2519css_next_child(struct cgroup_subsys_state *pos_css,
2520 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002521{
Tejun Heo492eb212013-08-08 20:11:25 -04002522 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2523 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002524 struct cgroup *next;
2525
Tejun Heoace2bee2014-02-11 11:52:47 -05002526 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002527
2528 /*
2529 * @pos could already have been removed. Once a cgroup is removed,
2530 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002531 * changes. As CGRP_DEAD assertion is serialized and happens
2532 * before the cgroup is taken off the ->sibling list, if we see it
2533 * unasserted, it's guaranteed that the next sibling hasn't
2534 * finished its grace period even if it's already removed, and thus
2535 * safe to dereference from this RCU critical section. If
2536 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2537 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002538 *
2539 * If @pos is dead, its next pointer can't be dereferenced;
2540 * however, as each cgroup is given a monotonically increasing
2541 * unique serial number and always appended to the sibling list,
2542 * the next one can be found by walking the parent's children until
2543 * we see a cgroup with higher serial number than @pos's. While
2544 * this path can be slower, it's taken only when either the current
2545 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002546 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002547 if (!pos) {
2548 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2549 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002550 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002551 } else {
2552 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2553 if (next->serial_nr > pos->serial_nr)
2554 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002555 }
2556
Tejun Heo492eb212013-08-08 20:11:25 -04002557 if (&next->sibling == &cgrp->children)
2558 return NULL;
2559
Tejun Heoca8bdca2013-08-26 18:40:56 -04002560 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002561}
Tejun Heo492eb212013-08-08 20:11:25 -04002562EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002563
2564/**
Tejun Heo492eb212013-08-08 20:11:25 -04002565 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002566 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002567 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002568 *
Tejun Heo492eb212013-08-08 20:11:25 -04002569 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002570 * to visit for pre-order traversal of @root's descendants. @root is
2571 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002572 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002573 * While this function requires cgroup_mutex or RCU read locking, it
2574 * doesn't require the whole traversal to be contained in a single critical
2575 * section. This function will return the correct next descendant as long
2576 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002577 */
Tejun Heo492eb212013-08-08 20:11:25 -04002578struct cgroup_subsys_state *
2579css_next_descendant_pre(struct cgroup_subsys_state *pos,
2580 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002581{
Tejun Heo492eb212013-08-08 20:11:25 -04002582 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002583
Tejun Heoace2bee2014-02-11 11:52:47 -05002584 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002585
Tejun Heobd8815a2013-08-08 20:11:27 -04002586 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002587 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002588 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002589
2590 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002591 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002592 if (next)
2593 return next;
2594
2595 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002596 while (pos != root) {
2597 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002598 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002599 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002600 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002601 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002602
2603 return NULL;
2604}
Tejun Heo492eb212013-08-08 20:11:25 -04002605EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002606
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002607/**
Tejun Heo492eb212013-08-08 20:11:25 -04002608 * css_rightmost_descendant - return the rightmost descendant of a css
2609 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002610 *
Tejun Heo492eb212013-08-08 20:11:25 -04002611 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2612 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002613 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002614 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002615 * While this function requires cgroup_mutex or RCU read locking, it
2616 * doesn't require the whole traversal to be contained in a single critical
2617 * section. This function will return the correct rightmost descendant as
2618 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002619 */
Tejun Heo492eb212013-08-08 20:11:25 -04002620struct cgroup_subsys_state *
2621css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002622{
Tejun Heo492eb212013-08-08 20:11:25 -04002623 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002624
Tejun Heoace2bee2014-02-11 11:52:47 -05002625 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002626
2627 do {
2628 last = pos;
2629 /* ->prev isn't RCU safe, walk ->next till the end */
2630 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002631 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002632 pos = tmp;
2633 } while (pos);
2634
2635 return last;
2636}
Tejun Heo492eb212013-08-08 20:11:25 -04002637EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002638
Tejun Heo492eb212013-08-08 20:11:25 -04002639static struct cgroup_subsys_state *
2640css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002641{
Tejun Heo492eb212013-08-08 20:11:25 -04002642 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002643
2644 do {
2645 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04002646 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002647 } while (pos);
2648
2649 return last;
2650}
2651
2652/**
Tejun Heo492eb212013-08-08 20:11:25 -04002653 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002654 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002655 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002656 *
Tejun Heo492eb212013-08-08 20:11:25 -04002657 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002658 * to visit for post-order traversal of @root's descendants. @root is
2659 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002660 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002661 * While this function requires cgroup_mutex or RCU read locking, it
2662 * doesn't require the whole traversal to be contained in a single critical
2663 * section. This function will return the correct next descendant as long
2664 * as both @pos and @cgroup are accessible and @pos is a descendant of
2665 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002666 */
Tejun Heo492eb212013-08-08 20:11:25 -04002667struct cgroup_subsys_state *
2668css_next_descendant_post(struct cgroup_subsys_state *pos,
2669 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002670{
Tejun Heo492eb212013-08-08 20:11:25 -04002671 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002672
Tejun Heoace2bee2014-02-11 11:52:47 -05002673 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002674
Tejun Heo58b79a92013-09-06 15:31:08 -04002675 /* if first iteration, visit leftmost descendant which may be @root */
2676 if (!pos)
2677 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002678
Tejun Heobd8815a2013-08-08 20:11:27 -04002679 /* if we visited @root, we're done */
2680 if (pos == root)
2681 return NULL;
2682
Tejun Heo574bd9f2012-11-09 09:12:29 -08002683 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04002684 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002685 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04002686 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002687
2688 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04002689 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002690}
Tejun Heo492eb212013-08-08 20:11:25 -04002691EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002692
Tejun Heo0942eee2013-08-08 20:11:26 -04002693/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002694 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04002695 * @it: the iterator to advance
2696 *
2697 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04002698 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002699static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04002700{
2701 struct list_head *l = it->cset_link;
2702 struct cgrp_cset_link *link;
2703 struct css_set *cset;
2704
2705 /* Advance to the next non-empty css_set */
2706 do {
2707 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04002708 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04002709 it->cset_link = NULL;
2710 return;
2711 }
2712 link = list_entry(l, struct cgrp_cset_link, cset_link);
2713 cset = link->cset;
2714 } while (list_empty(&cset->tasks));
2715 it->cset_link = l;
2716 it->task = cset->tasks.next;
2717}
2718
Tejun Heo0942eee2013-08-08 20:11:26 -04002719/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002720 * css_task_iter_start - initiate task iteration
2721 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04002722 * @it: the task iterator to use
2723 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002724 * Initiate iteration through the tasks of @css. The caller can call
2725 * css_task_iter_next() to walk through the tasks until the function
2726 * returns NULL. On completion of iteration, css_task_iter_end() must be
2727 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04002728 *
2729 * Note that this function acquires a lock which is released when the
2730 * iteration finishes. The caller can't sleep while iteration is in
2731 * progress.
2732 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002733void css_task_iter_start(struct cgroup_subsys_state *css,
2734 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002735 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002736{
2737 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04002738 * The first time anyone tries to iterate across a css, we need to
2739 * enable the list linking each css_set to its tasks, and fix up
2740 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07002741 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002742 if (!use_task_css_set_links)
2743 cgroup_enable_task_cg_lists();
2744
Paul Menage817929e2007-10-18 23:39:36 -07002745 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002746
Tejun Heo72ec7022013-08-08 20:11:26 -04002747 it->origin_css = css;
2748 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002749
Tejun Heo72ec7022013-08-08 20:11:26 -04002750 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002751}
2752
Tejun Heo0942eee2013-08-08 20:11:26 -04002753/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002754 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04002755 * @it: the task iterator being iterated
2756 *
2757 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04002758 * initialized via css_task_iter_start(). Returns NULL when the iteration
2759 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04002760 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002761struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002762{
2763 struct task_struct *res;
2764 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07002765 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002766
2767 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07002768 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07002769 return NULL;
2770 res = list_entry(l, struct task_struct, cg_list);
2771 /* Advance iterator to find next entry */
2772 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002773 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
2774 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04002775 /*
2776 * We reached the end of this task list - move on to the
2777 * next cgrp_cset_link.
2778 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002779 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002780 } else {
2781 it->task = l;
2782 }
2783 return res;
2784}
2785
Tejun Heo0942eee2013-08-08 20:11:26 -04002786/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002787 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04002788 * @it: the task iterator to finish
2789 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002790 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04002791 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002792void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002793 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002794{
2795 read_unlock(&css_set_lock);
2796}
2797
Cliff Wickman31a7df02008-02-07 00:14:42 -08002798static inline int started_after_time(struct task_struct *t1,
2799 struct timespec *time,
2800 struct task_struct *t2)
2801{
2802 int start_diff = timespec_compare(&t1->start_time, time);
2803 if (start_diff > 0) {
2804 return 1;
2805 } else if (start_diff < 0) {
2806 return 0;
2807 } else {
2808 /*
2809 * Arbitrarily, if two processes started at the same
2810 * time, we'll say that the lower pointer value
2811 * started first. Note that t2 may have exited by now
2812 * so this may not be a valid pointer any longer, but
2813 * that's fine - it still serves to distinguish
2814 * between two tasks started (effectively) simultaneously.
2815 */
2816 return t1 > t2;
2817 }
2818}
2819
2820/*
2821 * This function is a callback from heap_insert() and is used to order
2822 * the heap.
2823 * In this case we order the heap in descending task start time.
2824 */
2825static inline int started_after(void *p1, void *p2)
2826{
2827 struct task_struct *t1 = p1;
2828 struct task_struct *t2 = p2;
2829 return started_after_time(t1, &t2->start_time, t2);
2830}
2831
2832/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002833 * css_scan_tasks - iterate though all the tasks in a css
2834 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04002835 * @test: optional test callback
2836 * @process: process callback
2837 * @data: data passed to @test and @process
2838 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08002839 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002840 * Iterate through all the tasks in @css, calling @test for each, and if it
2841 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08002842 *
Tejun Heoe5358372013-08-08 20:11:26 -04002843 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04002844 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04002845 * lock css_set_lock for the call to @process.
2846 *
2847 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04002848 * of @css for the duration of this call. This function may or may not
2849 * call @process for tasks that exit or move to a different css during the
2850 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04002851 *
2852 * Note that @test may be called with locks held, and may in some
2853 * situations be called multiple times for the same task, so it should be
2854 * cheap.
2855 *
2856 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
2857 * heap operations (and its "gt" member will be overwritten), else a
2858 * temporary heap will be used (allocation of which may cause this function
2859 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08002860 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002861int css_scan_tasks(struct cgroup_subsys_state *css,
2862 bool (*test)(struct task_struct *, void *),
2863 void (*process)(struct task_struct *, void *),
2864 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002865{
2866 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04002867 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002868 struct task_struct *p, *dropped;
2869 /* Never dereference latest_task, since it's not refcounted */
2870 struct task_struct *latest_task = NULL;
2871 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002872 struct timespec latest_time = { 0, 0 };
2873
Tejun Heoe5358372013-08-08 20:11:26 -04002874 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08002875 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002876 heap->gt = &started_after;
2877 } else {
2878 /* We need to allocate our own heap memory */
2879 heap = &tmp_heap;
2880 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2881 if (retval)
2882 /* cannot allocate the heap */
2883 return retval;
2884 }
2885
2886 again:
2887 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04002888 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04002889 * which are of interest, and invoking @process callback on the
2890 * ones which need an update. Since we don't want to hold any
2891 * locks during the task updates, gather tasks to be processed in a
2892 * heap structure. The heap is sorted by descending task start
2893 * time. If the statically-sized heap fills up, we overflow tasks
2894 * that started later, and in future iterations only consider tasks
2895 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08002896 * guarantees forward progress and that we don't miss any tasks.
2897 */
2898 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04002899 css_task_iter_start(css, &it);
2900 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08002901 /*
2902 * Only affect tasks that qualify per the caller's callback,
2903 * if he provided one
2904 */
Tejun Heoe5358372013-08-08 20:11:26 -04002905 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002906 continue;
2907 /*
2908 * Only process tasks that started after the last task
2909 * we processed
2910 */
2911 if (!started_after_time(p, &latest_time, latest_task))
2912 continue;
2913 dropped = heap_insert(heap, p);
2914 if (dropped == NULL) {
2915 /*
2916 * The new task was inserted; the heap wasn't
2917 * previously full
2918 */
2919 get_task_struct(p);
2920 } else if (dropped != p) {
2921 /*
2922 * The new task was inserted, and pushed out a
2923 * different task
2924 */
2925 get_task_struct(p);
2926 put_task_struct(dropped);
2927 }
2928 /*
2929 * Else the new task was newer than anything already in
2930 * the heap and wasn't inserted
2931 */
2932 }
Tejun Heo72ec7022013-08-08 20:11:26 -04002933 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002934
2935 if (heap->size) {
2936 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002937 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002938 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002939 latest_time = q->start_time;
2940 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002941 }
2942 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04002943 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07002944 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002945 }
2946 /*
2947 * If we had to process any tasks at all, scan again
2948 * in case some of them were in the middle of forking
2949 * children that didn't get processed.
2950 * Not the most efficient way to do it, but it avoids
2951 * having to take callback_mutex in the fork path
2952 */
2953 goto again;
2954 }
2955 if (heap == &tmp_heap)
2956 heap_free(&tmp_heap);
2957 return 0;
2958}
2959
Tejun Heoe5358372013-08-08 20:11:26 -04002960static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07002961{
Tejun Heoe5358372013-08-08 20:11:26 -04002962 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07002963
Tejun Heo47cfcd02013-04-07 09:29:51 -07002964 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07002965 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002966 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07002967}
2968
2969/**
2970 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
2971 * @to: cgroup to which the tasks will be moved
2972 * @from: cgroup in which the tasks currently reside
2973 */
2974int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
2975{
Tejun Heo72ec7022013-08-08 20:11:26 -04002976 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
2977 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07002978}
2979
Paul Menage817929e2007-10-18 23:39:36 -07002980/*
Ben Blum102a7752009-09-23 15:56:26 -07002981 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002982 *
2983 * Reading this file can return large amounts of data if a cgroup has
2984 * *lots* of attached tasks. So it may need several calls to read(),
2985 * but we cannot guarantee that the information we produce is correct
2986 * unless we produce it entirely atomically.
2987 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002988 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002989
Li Zefan24528252012-01-20 11:58:43 +08002990/* which pidlist file are we talking about? */
2991enum cgroup_filetype {
2992 CGROUP_FILE_PROCS,
2993 CGROUP_FILE_TASKS,
2994};
2995
2996/*
2997 * A pidlist is a list of pids that virtually represents the contents of one
2998 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
2999 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3000 * to the cgroup.
3001 */
3002struct cgroup_pidlist {
3003 /*
3004 * used to find which pidlist is wanted. doesn't change as long as
3005 * this particular list stays in the list.
3006 */
3007 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3008 /* array of xids */
3009 pid_t *list;
3010 /* how many elements the above list has */
3011 int length;
Li Zefan24528252012-01-20 11:58:43 +08003012 /* each of these stored in a list by its cgroup */
3013 struct list_head links;
3014 /* pointer to the cgroup we belong to, for list removal purposes */
3015 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003016 /* for delayed destruction */
3017 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003018};
3019
Paul Menagebbcb81d2007-10-18 23:39:32 -07003020/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003021 * The following two functions "fix" the issue where there are more pids
3022 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3023 * TODO: replace with a kernel-wide solution to this problem
3024 */
3025#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3026static void *pidlist_allocate(int count)
3027{
3028 if (PIDLIST_TOO_LARGE(count))
3029 return vmalloc(count * sizeof(pid_t));
3030 else
3031 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3032}
Tejun Heob1a21362013-11-29 10:42:58 -05003033
Ben Blumd1d9fd32009-09-23 15:56:28 -07003034static void pidlist_free(void *p)
3035{
3036 if (is_vmalloc_addr(p))
3037 vfree(p);
3038 else
3039 kfree(p);
3040}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003041
3042/*
Tejun Heob1a21362013-11-29 10:42:58 -05003043 * Used to destroy all pidlists lingering waiting for destroy timer. None
3044 * should be left afterwards.
3045 */
3046static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3047{
3048 struct cgroup_pidlist *l, *tmp_l;
3049
3050 mutex_lock(&cgrp->pidlist_mutex);
3051 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3052 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3053 mutex_unlock(&cgrp->pidlist_mutex);
3054
3055 flush_workqueue(cgroup_pidlist_destroy_wq);
3056 BUG_ON(!list_empty(&cgrp->pidlists));
3057}
3058
3059static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3060{
3061 struct delayed_work *dwork = to_delayed_work(work);
3062 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3063 destroy_dwork);
3064 struct cgroup_pidlist *tofree = NULL;
3065
3066 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003067
3068 /*
Tejun Heo04502362013-11-29 10:42:59 -05003069 * Destroy iff we didn't get queued again. The state won't change
3070 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003071 */
Tejun Heo04502362013-11-29 10:42:59 -05003072 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003073 list_del(&l->links);
3074 pidlist_free(l->list);
3075 put_pid_ns(l->key.ns);
3076 tofree = l;
3077 }
3078
Tejun Heob1a21362013-11-29 10:42:58 -05003079 mutex_unlock(&l->owner->pidlist_mutex);
3080 kfree(tofree);
3081}
3082
3083/*
Ben Blum102a7752009-09-23 15:56:26 -07003084 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003085 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003086 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003087static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003088{
Ben Blum102a7752009-09-23 15:56:26 -07003089 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003090
3091 /*
3092 * we presume the 0th element is unique, so i starts at 1. trivial
3093 * edge cases first; no work needs to be done for either
3094 */
3095 if (length == 0 || length == 1)
3096 return length;
3097 /* src and dest walk down the list; dest counts unique elements */
3098 for (src = 1; src < length; src++) {
3099 /* find next unique element */
3100 while (list[src] == list[src-1]) {
3101 src++;
3102 if (src == length)
3103 goto after;
3104 }
3105 /* dest always points to where the next unique element goes */
3106 list[dest] = list[src];
3107 dest++;
3108 }
3109after:
Ben Blum102a7752009-09-23 15:56:26 -07003110 return dest;
3111}
3112
Tejun Heoafb2bc12013-11-29 10:42:59 -05003113/*
3114 * The two pid files - task and cgroup.procs - guaranteed that the result
3115 * is sorted, which forced this whole pidlist fiasco. As pid order is
3116 * different per namespace, each namespace needs differently sorted list,
3117 * making it impossible to use, for example, single rbtree of member tasks
3118 * sorted by task pointer. As pidlists can be fairly large, allocating one
3119 * per open file is dangerous, so cgroup had to implement shared pool of
3120 * pidlists keyed by cgroup and namespace.
3121 *
3122 * All this extra complexity was caused by the original implementation
3123 * committing to an entirely unnecessary property. In the long term, we
3124 * want to do away with it. Explicitly scramble sort order if
3125 * sane_behavior so that no such expectation exists in the new interface.
3126 *
3127 * Scrambling is done by swapping every two consecutive bits, which is
3128 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3129 */
3130static pid_t pid_fry(pid_t pid)
3131{
3132 unsigned a = pid & 0x55555555;
3133 unsigned b = pid & 0xAAAAAAAA;
3134
3135 return (a << 1) | (b >> 1);
3136}
3137
3138static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3139{
3140 if (cgroup_sane_behavior(cgrp))
3141 return pid_fry(pid);
3142 else
3143 return pid;
3144}
3145
Ben Blum102a7752009-09-23 15:56:26 -07003146static int cmppid(const void *a, const void *b)
3147{
3148 return *(pid_t *)a - *(pid_t *)b;
3149}
3150
Tejun Heoafb2bc12013-11-29 10:42:59 -05003151static int fried_cmppid(const void *a, const void *b)
3152{
3153 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3154}
3155
Ben Blum72a8cb32009-09-23 15:56:27 -07003156static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3157 enum cgroup_filetype type)
3158{
3159 struct cgroup_pidlist *l;
3160 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003161 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003162
Tejun Heoe6b81712013-11-29 10:42:59 -05003163 lockdep_assert_held(&cgrp->pidlist_mutex);
3164
3165 list_for_each_entry(l, &cgrp->pidlists, links)
3166 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003167 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003168 return NULL;
3169}
3170
Ben Blum72a8cb32009-09-23 15:56:27 -07003171/*
3172 * find the appropriate pidlist for our purpose (given procs vs tasks)
3173 * returns with the lock on that pidlist already held, and takes care
3174 * of the use count, or returns NULL with no locks held if we're out of
3175 * memory.
3176 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003177static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3178 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003179{
3180 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003181
Tejun Heoe6b81712013-11-29 10:42:59 -05003182 lockdep_assert_held(&cgrp->pidlist_mutex);
3183
3184 l = cgroup_pidlist_find(cgrp, type);
3185 if (l)
3186 return l;
3187
Ben Blum72a8cb32009-09-23 15:56:27 -07003188 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003189 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003190 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003191 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003192
Tejun Heob1a21362013-11-29 10:42:58 -05003193 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003194 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003195 /* don't need task_nsproxy() if we're looking at ourself */
3196 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003197 l->owner = cgrp;
3198 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003199 return l;
3200}
3201
3202/*
Ben Blum102a7752009-09-23 15:56:26 -07003203 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3204 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003205static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3206 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003207{
3208 pid_t *array;
3209 int length;
3210 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003211 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003212 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003213 struct cgroup_pidlist *l;
3214
Tejun Heo4bac00d2013-11-29 10:42:59 -05003215 lockdep_assert_held(&cgrp->pidlist_mutex);
3216
Ben Blum102a7752009-09-23 15:56:26 -07003217 /*
3218 * If cgroup gets more users after we read count, we won't have
3219 * enough space - tough. This race is indistinguishable to the
3220 * caller from the case that the additional cgroup users didn't
3221 * show up until sometime later on.
3222 */
3223 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003224 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003225 if (!array)
3226 return -ENOMEM;
3227 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003228 css_task_iter_start(&cgrp->dummy_css, &it);
3229 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003230 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003231 break;
Ben Blum102a7752009-09-23 15:56:26 -07003232 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003233 if (type == CGROUP_FILE_PROCS)
3234 pid = task_tgid_vnr(tsk);
3235 else
3236 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003237 if (pid > 0) /* make sure to only use valid results */
3238 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003239 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003240 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003241 length = n;
3242 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003243 if (cgroup_sane_behavior(cgrp))
3244 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3245 else
3246 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003247 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003248 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003249
Tejun Heoe6b81712013-11-29 10:42:59 -05003250 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003251 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003252 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003253 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003254 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003255 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003256
3257 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003258 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003259 l->list = array;
3260 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003261 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003262 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003263}
3264
Balbir Singh846c7bb2007-10-18 23:39:44 -07003265/**
Li Zefana043e3b2008-02-23 15:24:09 -08003266 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003267 * @stats: cgroupstats to fill information into
3268 * @dentry: A dentry entry belonging to the cgroup for which stats have
3269 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003270 *
3271 * Build and fill cgroupstats so that taskstats can export it to user
3272 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003273 */
3274int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3275{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003276 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003277 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003278 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003279 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003280
Tejun Heo2bd59d42014-02-11 11:52:49 -05003281 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3282 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3283 kernfs_type(kn) != KERNFS_DIR)
3284 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003285
Tejun Heo2bd59d42014-02-11 11:52:49 -05003286 /*
3287 * We aren't being called from kernfs and there's no guarantee on
3288 * @kn->priv's validity. For this and css_tryget_from_dir(),
3289 * @kn->priv is RCU safe. Let's do the RCU dancing.
3290 */
3291 rcu_read_lock();
3292 cgrp = rcu_dereference(kn->priv);
3293 if (!cgrp) {
3294 rcu_read_unlock();
3295 return -ENOENT;
3296 }
Balbir Singh846c7bb2007-10-18 23:39:44 -07003297
Tejun Heo72ec7022013-08-08 20:11:26 -04003298 css_task_iter_start(&cgrp->dummy_css, &it);
3299 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003300 switch (tsk->state) {
3301 case TASK_RUNNING:
3302 stats->nr_running++;
3303 break;
3304 case TASK_INTERRUPTIBLE:
3305 stats->nr_sleeping++;
3306 break;
3307 case TASK_UNINTERRUPTIBLE:
3308 stats->nr_uninterruptible++;
3309 break;
3310 case TASK_STOPPED:
3311 stats->nr_stopped++;
3312 break;
3313 default:
3314 if (delayacct_is_task_waiting_on_io(tsk))
3315 stats->nr_io_wait++;
3316 break;
3317 }
3318 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003319 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003320
Tejun Heo2bd59d42014-02-11 11:52:49 -05003321 rcu_read_unlock();
3322 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003323}
3324
Paul Menage8f3ff202009-09-23 15:56:25 -07003325
Paul Menagecc31edc2008-10-18 20:28:04 -07003326/*
Ben Blum102a7752009-09-23 15:56:26 -07003327 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003328 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003329 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003330 */
3331
Ben Blum102a7752009-09-23 15:56:26 -07003332static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003333{
3334 /*
3335 * Initially we receive a position value that corresponds to
3336 * one more than the last pid shown (or 0 on the first call or
3337 * after a seek to the start). Use a binary-search to find the
3338 * next pid to display, if any
3339 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003340 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003341 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003342 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003343 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003344 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003345 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003346
Tejun Heo4bac00d2013-11-29 10:42:59 -05003347 mutex_lock(&cgrp->pidlist_mutex);
3348
3349 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003350 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003351 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003352 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003353 * could already have been destroyed.
3354 */
Tejun Heo5d224442013-12-05 12:28:04 -05003355 if (of->priv)
3356 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003357
3358 /*
3359 * Either this is the first start() after open or the matching
3360 * pidlist has been destroyed inbetween. Create a new one.
3361 */
Tejun Heo5d224442013-12-05 12:28:04 -05003362 if (!of->priv) {
3363 ret = pidlist_array_load(cgrp, type,
3364 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003365 if (ret)
3366 return ERR_PTR(ret);
3367 }
Tejun Heo5d224442013-12-05 12:28:04 -05003368 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003369
Paul Menagecc31edc2008-10-18 20:28:04 -07003370 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003371 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003372
Paul Menagecc31edc2008-10-18 20:28:04 -07003373 while (index < end) {
3374 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003375 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003376 index = mid;
3377 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003378 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003379 index = mid + 1;
3380 else
3381 end = mid;
3382 }
3383 }
3384 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003385 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003386 return NULL;
3387 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003388 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003389 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003390 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003391}
3392
Ben Blum102a7752009-09-23 15:56:26 -07003393static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003394{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003395 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003396 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003397
Tejun Heo5d224442013-12-05 12:28:04 -05003398 if (l)
3399 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003400 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003401 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003402}
3403
Ben Blum102a7752009-09-23 15:56:26 -07003404static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003405{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003406 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003407 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003408 pid_t *p = v;
3409 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003410 /*
3411 * Advance to the next pid in the array. If this goes off the
3412 * end, we're done
3413 */
3414 p++;
3415 if (p >= end) {
3416 return NULL;
3417 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003418 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003419 return p;
3420 }
3421}
3422
Ben Blum102a7752009-09-23 15:56:26 -07003423static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003424{
3425 return seq_printf(s, "%d\n", *(int *)v);
3426}
3427
Ben Blum102a7752009-09-23 15:56:26 -07003428/*
3429 * seq_operations functions for iterating on pidlists through seq_file -
3430 * independent of whether it's tasks or procs
3431 */
3432static const struct seq_operations cgroup_pidlist_seq_operations = {
3433 .start = cgroup_pidlist_start,
3434 .stop = cgroup_pidlist_stop,
3435 .next = cgroup_pidlist_next,
3436 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003437};
3438
Tejun Heo182446d2013-08-08 20:11:24 -04003439static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3440 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003441{
Tejun Heo182446d2013-08-08 20:11:24 -04003442 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003443}
3444
Tejun Heo182446d2013-08-08 20:11:24 -04003445static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3446 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003447{
Tejun Heo182446d2013-08-08 20:11:24 -04003448 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003449 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003450 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003451 else
Tejun Heo182446d2013-08-08 20:11:24 -04003452 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003453 return 0;
3454}
3455
Tejun Heo182446d2013-08-08 20:11:24 -04003456static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3457 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003458{
Tejun Heo182446d2013-08-08 20:11:24 -04003459 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003460}
3461
Tejun Heo182446d2013-08-08 20:11:24 -04003462static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3463 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003464{
3465 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003466 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003467 else
Tejun Heo182446d2013-08-08 20:11:24 -04003468 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003469 return 0;
3470}
3471
Tejun Heod5c56ce2013-06-03 19:14:34 -07003472static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003473 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003474 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003475 .seq_start = cgroup_pidlist_start,
3476 .seq_next = cgroup_pidlist_next,
3477 .seq_stop = cgroup_pidlist_stop,
3478 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003479 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003480 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003481 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003482 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003483 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003484 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003485 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003486 .read_u64 = cgroup_clone_children_read,
3487 .write_u64 = cgroup_clone_children_write,
3488 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003489 {
Tejun Heo873fe092013-04-14 20:15:26 -07003490 .name = "cgroup.sane_behavior",
3491 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003492 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003493 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003494
3495 /*
3496 * Historical crazy stuff. These don't have "cgroup." prefix and
3497 * don't exist if sane_behavior. If you're depending on these, be
3498 * prepared to be burned.
3499 */
3500 {
3501 .name = "tasks",
3502 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003503 .seq_start = cgroup_pidlist_start,
3504 .seq_next = cgroup_pidlist_next,
3505 .seq_stop = cgroup_pidlist_stop,
3506 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003507 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003508 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003509 .mode = S_IRUGO | S_IWUSR,
3510 },
3511 {
3512 .name = "notify_on_release",
3513 .flags = CFTYPE_INSANE,
3514 .read_u64 = cgroup_read_notify_on_release,
3515 .write_u64 = cgroup_write_notify_on_release,
3516 },
Tejun Heo873fe092013-04-14 20:15:26 -07003517 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003518 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003519 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003520 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003521 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003522 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003523 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003524 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003525};
3526
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003527/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003528 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003529 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003530 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003531 *
3532 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003533 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003534static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003535{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003536 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003537 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003538
Tejun Heo8e3f6542012-04-01 12:09:55 -07003539 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003540 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05003541 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07003542
3543 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003544 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003545
Tejun Heo0adb0702014-02-12 09:29:48 -05003546 list_for_each_entry(cfts, &ss->cfts, node) {
3547 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003548 if (ret < 0)
3549 goto err;
3550 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003551 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003552 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003553err:
3554 cgroup_clear_dir(cgrp, subsys_mask);
3555 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003556}
3557
Tejun Heo0c21ead2013-08-13 20:22:51 -04003558/*
3559 * css destruction is four-stage process.
3560 *
3561 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3562 * Implemented in kill_css().
3563 *
3564 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3565 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3566 * by invoking offline_css(). After offlining, the base ref is put.
3567 * Implemented in css_killed_work_fn().
3568 *
3569 * 3. When the percpu_ref reaches zero, the only possible remaining
3570 * accessors are inside RCU read sections. css_release() schedules the
3571 * RCU callback.
3572 *
3573 * 4. After the grace period, the css can be freed. Implemented in
3574 * css_free_work_fn().
3575 *
3576 * It is actually hairier because both step 2 and 4 require process context
3577 * and thus involve punting to css->destroy_work adding two additional
3578 * steps to the already complex sequence.
3579 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003580static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003581{
3582 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003583 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003584 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003585
Tejun Heo0ae78e02013-08-13 11:01:54 -04003586 if (css->parent)
3587 css_put(css->parent);
3588
Tejun Heo0c21ead2013-08-13 20:22:51 -04003589 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003590 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003591}
3592
3593static void css_free_rcu_fn(struct rcu_head *rcu_head)
3594{
3595 struct cgroup_subsys_state *css =
3596 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3597
Tejun Heo0c21ead2013-08-13 20:22:51 -04003598 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003599 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003600}
3601
Tejun Heod3daf282013-06-13 19:39:16 -07003602static void css_release(struct percpu_ref *ref)
3603{
3604 struct cgroup_subsys_state *css =
3605 container_of(ref, struct cgroup_subsys_state, refcnt);
3606
Tejun Heoaec25022014-02-08 10:36:58 -05003607 rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003608 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003609}
3610
Tejun Heo623f9262013-08-13 11:01:55 -04003611static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3612 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003613{
Paul Menagebd89aab2007-10-18 23:40:44 -07003614 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003615 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003616 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003617
Tejun Heo0ae78e02013-08-13 11:01:54 -04003618 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003619 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003620 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003621 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003622
Tejun Heoca8bdca2013-08-26 18:40:56 -04003623 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003624}
3625
Li Zefan2a4ac632013-07-31 16:16:40 +08003626/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04003627static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003628{
Tejun Heo623f9262013-08-13 11:01:55 -04003629 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08003630 int ret = 0;
3631
Tejun Heoace2bee2014-02-11 11:52:47 -05003632 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003633 lockdep_assert_held(&cgroup_mutex);
3634
Tejun Heo92fb9742012-11-19 08:13:38 -08003635 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04003636 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003637 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04003638 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04003639 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05003640 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003641 }
Tejun Heob1929db2012-11-19 08:13:38 -08003642 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003643}
3644
Li Zefan2a4ac632013-07-31 16:16:40 +08003645/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04003646static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003647{
Tejun Heo623f9262013-08-13 11:01:55 -04003648 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003649
Tejun Heoace2bee2014-02-11 11:52:47 -05003650 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003651 lockdep_assert_held(&cgroup_mutex);
3652
3653 if (!(css->flags & CSS_ONLINE))
3654 return;
3655
Li Zefand7eeac12013-03-12 15:35:59 -07003656 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04003657 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003658
Tejun Heoeb954192013-08-08 20:11:23 -04003659 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04003660 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05003661 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003662}
3663
Tejun Heoc81c925a2013-12-06 15:11:56 -05003664/**
3665 * create_css - create a cgroup_subsys_state
3666 * @cgrp: the cgroup new css will be associated with
3667 * @ss: the subsys of new css
3668 *
3669 * Create a new css associated with @cgrp - @ss pair. On success, the new
3670 * css is online and installed in @cgrp with all interface files created.
3671 * Returns 0 on success, -errno on failure.
3672 */
3673static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
3674{
3675 struct cgroup *parent = cgrp->parent;
3676 struct cgroup_subsys_state *css;
3677 int err;
3678
Tejun Heoc81c925a2013-12-06 15:11:56 -05003679 lockdep_assert_held(&cgroup_mutex);
3680
3681 css = ss->css_alloc(cgroup_css(parent, ss));
3682 if (IS_ERR(css))
3683 return PTR_ERR(css);
3684
3685 err = percpu_ref_init(&css->refcnt, css_release);
3686 if (err)
3687 goto err_free;
3688
3689 init_css(css, ss, cgrp);
3690
Tejun Heoaec25022014-02-08 10:36:58 -05003691 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003692 if (err)
3693 goto err_free;
3694
3695 err = online_css(css);
3696 if (err)
3697 goto err_free;
3698
Tejun Heo59f52962014-02-11 11:52:49 -05003699 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003700 css_get(css->parent);
3701
3702 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
3703 parent->parent) {
3704 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",
3705 current->comm, current->pid, ss->name);
3706 if (!strcmp(ss->name, "memory"))
3707 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
3708 ss->warned_broken_hierarchy = true;
3709 }
3710
3711 return 0;
3712
3713err_free:
3714 percpu_ref_cancel_init(&css->refcnt);
3715 ss->css_free(css);
3716 return err;
3717}
3718
Tejun Heo2bd59d42014-02-11 11:52:49 -05003719/**
Li Zefana043e3b2008-02-23 15:24:09 -08003720 * cgroup_create - create a cgroup
3721 * @parent: cgroup that will be parent of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05003722 * @name_str: name of the new cgroup
3723 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07003724 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003725static long cgroup_create(struct cgroup *parent, const char *name_str,
3726 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003727{
Paul Menagebd89aab2007-10-18 23:40:44 -07003728 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08003729 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003730 struct cgroupfs_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05003731 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003732 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003733 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003734
Tejun Heo0a950f62012-11-19 09:02:12 -08003735 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07003736 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3737 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003738 return -ENOMEM;
3739
Tejun Heo2bd59d42014-02-11 11:52:49 -05003740 name = cgroup_alloc_name(name_str);
Tejun Heob58c8992014-02-08 10:26:33 -05003741 if (!name) {
3742 err = -ENOMEM;
Li Zefan65dff752013-03-01 15:01:56 +08003743 goto err_free_cgrp;
Tejun Heob58c8992014-02-08 10:26:33 -05003744 }
Li Zefan65dff752013-03-01 15:01:56 +08003745 rcu_assign_pointer(cgrp->name, name);
3746
Tejun Heoace2bee2014-02-11 11:52:47 -05003747 mutex_lock(&cgroup_tree_mutex);
3748
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003749 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08003750 * Only live parents can have children. Note that the liveliness
3751 * check isn't strictly necessary because cgroup_mkdir() and
3752 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
3753 * anyway so that locking is contained inside cgroup proper and we
3754 * don't get nasty surprises if we ever grow another caller.
3755 */
3756 if (!cgroup_lock_live_group(parent)) {
3757 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05003758 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08003759 }
3760
3761 /*
3762 * Temporarily set the pointer to NULL, so idr_find() won't return
3763 * a half-baked cgroup.
3764 */
3765 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
3766 if (cgrp->id < 0) {
3767 err = -ENOMEM;
3768 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08003769 }
3770
Paul Menagecc31edc2008-10-18 20:28:04 -07003771 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003772
Paul Menagebd89aab2007-10-18 23:40:44 -07003773 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003774 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07003775 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003776
Li Zefanb6abdb02008-03-04 14:28:19 -08003777 if (notify_on_release(parent))
3778 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3779
Tejun Heo2260e7f2012-11-19 08:13:38 -08003780 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
3781 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003782
Tejun Heo2bd59d42014-02-11 11:52:49 -05003783 /* create the directory */
3784 kn = kernfs_create_dir(parent->kn, name->name, mode, cgrp);
3785 if (IS_ERR(kn)) {
3786 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003787 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003788 }
3789 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003790
Tejun Heo6f305582014-02-12 09:29:50 -05003791 /*
3792 * This extra ref will be put in cgroup_free_fn() and guarantees
3793 * that @cgrp->kn is always accessible.
3794 */
3795 kernfs_get(kn);
3796
Tejun Heo00356bd2013-06-18 11:14:22 -07003797 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09003798
Tejun Heo4e139af2012-11-19 08:13:36 -08003799 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08003800 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
3801 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08003802
Tejun Heo2bd59d42014-02-11 11:52:49 -05003803 /*
3804 * Grab a reference on the root and parent so that they don't get
3805 * deleted while there are child cgroups.
3806 */
3807 cgroup_get_root(root);
Tejun Heo59f52962014-02-11 11:52:49 -05003808 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08003809
Tejun Heo0d802552013-12-06 15:11:56 -05003810 /*
3811 * @cgrp is now fully operational. If something fails after this
3812 * point, it'll be released via the normal destruction path.
3813 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003814 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
3815
Tejun Heo2bb566c2013-08-08 20:11:23 -04003816 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07003817 if (err)
3818 goto err_destroy;
3819
Tejun Heo9d403e92013-12-06 15:11:56 -05003820 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05003821 for_each_subsys(ss, ssid) {
3822 if (root->subsys_mask & (1 << ssid)) {
3823 err = create_css(cgrp, ss);
3824 if (err)
3825 goto err_destroy;
3826 }
Tejun Heoa8638032012-11-09 09:12:29 -08003827 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003828
Tejun Heo2bd59d42014-02-11 11:52:49 -05003829 kernfs_activate(kn);
3830
Paul Menageddbcc7e2007-10-18 23:39:30 -07003831 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003832 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003833
3834 return 0;
3835
Tejun Heo0a950f62012-11-19 09:02:12 -08003836err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003837 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003838err_unlock:
3839 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003840err_unlock_tree:
3841 mutex_unlock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08003842 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003843err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07003844 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003845 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003846
3847err_destroy:
3848 cgroup_destroy_locked(cgrp);
3849 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003850 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003851 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003852}
3853
Tejun Heo2bd59d42014-02-11 11:52:49 -05003854static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3855 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003856{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003857 struct cgroup *parent = parent_kn->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003858
Tejun Heo2bd59d42014-02-11 11:52:49 -05003859 return cgroup_create(parent, name, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003860}
3861
Tejun Heo223dbc32013-08-13 20:22:50 -04003862/*
3863 * This is called when the refcnt of a css is confirmed to be killed.
3864 * css_tryget() is now guaranteed to fail.
3865 */
3866static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07003867{
Tejun Heo223dbc32013-08-13 20:22:50 -04003868 struct cgroup_subsys_state *css =
3869 container_of(work, struct cgroup_subsys_state, destroy_work);
3870 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07003871
Tejun Heoace2bee2014-02-11 11:52:47 -05003872 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003873 mutex_lock(&cgroup_mutex);
3874
3875 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04003876 * css_tryget() is guaranteed to fail now. Tell subsystems to
3877 * initate destruction.
3878 */
3879 offline_css(css);
3880
3881 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003882 * If @cgrp is marked dead, it's waiting for refs of all css's to
3883 * be disabled before proceeding to the second phase of cgroup
3884 * destruction. If we are the last one, kick it off.
3885 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04003886 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04003887 cgroup_destroy_css_killed(cgrp);
3888
3889 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003890 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04003891
3892 /*
3893 * Put the css refs from kill_css(). Each css holds an extra
3894 * reference to the cgroup's dentry and cgroup removal proceeds
3895 * regardless of css refs. On the last put of each css, whenever
3896 * that may be, the extra dentry ref is put so that dentry
3897 * destruction happens only after all css's are released.
3898 */
3899 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07003900}
3901
Tejun Heo223dbc32013-08-13 20:22:50 -04003902/* css kill confirmation processing requires process context, bounce */
3903static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07003904{
3905 struct cgroup_subsys_state *css =
3906 container_of(ref, struct cgroup_subsys_state, refcnt);
3907
Tejun Heo223dbc32013-08-13 20:22:50 -04003908 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003909 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07003910}
3911
3912/**
Tejun Heoedae0c32013-08-13 20:22:51 -04003913 * kill_css - destroy a css
3914 * @css: css to destroy
3915 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003916 * This function initiates destruction of @css by removing cgroup interface
3917 * files and putting its base reference. ->css_offline() will be invoked
3918 * asynchronously once css_tryget() is guaranteed to fail and when the
3919 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04003920 */
3921static void kill_css(struct cgroup_subsys_state *css)
3922{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003923 /*
3924 * This must happen before css is disassociated with its cgroup.
3925 * See seq_css() for details.
3926 */
Tejun Heoaec25022014-02-08 10:36:58 -05003927 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003928
Tejun Heoedae0c32013-08-13 20:22:51 -04003929 /*
3930 * Killing would put the base ref, but we need to keep it alive
3931 * until after ->css_offline().
3932 */
3933 css_get(css);
3934
3935 /*
3936 * cgroup core guarantees that, by the time ->css_offline() is
3937 * invoked, no new css reference will be given out via
3938 * css_tryget(). We can't simply call percpu_ref_kill() and
3939 * proceed to offlining css's because percpu_ref_kill() doesn't
3940 * guarantee that the ref is seen as killed on all CPUs on return.
3941 *
3942 * Use percpu_ref_kill_and_confirm() to get notifications as each
3943 * css is confirmed to be seen as killed on all CPUs.
3944 */
3945 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003946}
3947
3948/**
3949 * cgroup_destroy_locked - the first stage of cgroup destruction
3950 * @cgrp: cgroup to be destroyed
3951 *
3952 * css's make use of percpu refcnts whose killing latency shouldn't be
3953 * exposed to userland and are RCU protected. Also, cgroup core needs to
3954 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
3955 * invoked. To satisfy all the requirements, destruction is implemented in
3956 * the following two steps.
3957 *
3958 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
3959 * userland visible parts and start killing the percpu refcnts of
3960 * css's. Set up so that the next stage will be kicked off once all
3961 * the percpu refcnts are confirmed to be killed.
3962 *
3963 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
3964 * rest of destruction. Once all cgroup references are gone, the
3965 * cgroup is RCU-freed.
3966 *
3967 * This function implements s1. After this step, @cgrp is gone as far as
3968 * the userland is concerned and a new cgroup with the same name may be
3969 * created. As cgroup doesn't care about the names internally, this
3970 * doesn't cause any problem.
3971 */
Tejun Heo42809dd2012-11-19 08:13:37 -08003972static int cgroup_destroy_locked(struct cgroup *cgrp)
3973 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003974{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003975 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003976 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07003977 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05003978 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003979
Tejun Heoace2bee2014-02-11 11:52:47 -05003980 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003981 lockdep_assert_held(&cgroup_mutex);
3982
Tejun Heoddd69142013-06-12 21:04:54 -07003983 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07003984 * css_set_lock synchronizes access to ->cset_links and prevents
3985 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07003986 */
3987 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003988 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07003989 read_unlock(&css_set_lock);
3990 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003991 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08003992
Tejun Heo1a90dd52012-11-05 09:16:59 -08003993 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003994 * Make sure there's no live children. We can't test ->children
3995 * emptiness as dead children linger on it while being destroyed;
3996 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
3997 */
3998 empty = true;
3999 rcu_read_lock();
4000 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4001 empty = cgroup_is_dead(child);
4002 if (!empty)
4003 break;
4004 }
4005 rcu_read_unlock();
4006 if (!empty)
4007 return -EBUSY;
4008
4009 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004010 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4011 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004012 * percpu refs of all css's are confirmed to be killed. This
4013 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004014 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004015 mutex_unlock(&cgroup_mutex);
Tejun Heo1c6727a2013-12-06 15:11:56 -05004016 for_each_css(css, ssid, cgrp)
4017 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004018 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004019
4020 /*
4021 * Mark @cgrp dead. This prevents further task migration and child
4022 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004023 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004024 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004025 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004026 */
Tejun Heo54766d42013-06-12 21:04:53 -07004027 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004028
Tejun Heo455050d2013-06-13 19:27:41 -07004029 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4030 raw_spin_lock(&release_list_lock);
4031 if (!list_empty(&cgrp->release_list))
4032 list_del_init(&cgrp->release_list);
4033 raw_spin_unlock(&release_list_lock);
4034
4035 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004036 * If @cgrp has css's attached, the second stage of cgroup
4037 * destruction is kicked off from css_killed_work_fn() after the
4038 * refs of all attached css's are killed. If @cgrp doesn't have
4039 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004040 */
Tejun Heof20104d2013-08-13 20:22:50 -04004041 if (!cgrp->nr_css)
4042 cgroup_destroy_css_killed(cgrp);
4043
Tejun Heo2bd59d42014-02-11 11:52:49 -05004044 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05004045 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004046
4047 /*
4048 * There are two control paths which try to determine cgroup from
4049 * dentry without going through kernfs - cgroupstats_build() and
4050 * css_tryget_from_dir(). Those are supported by RCU protecting
4051 * clearing of cgrp->kn->priv backpointer, which should happen
4052 * after all files under it have been removed.
4053 */
Tejun Heo6f305582014-02-12 09:29:50 -05004054 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004055 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004056
Tejun Heo4ac06012014-02-11 11:52:47 -05004057 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004058
Tejun Heoea15f8c2013-06-13 19:27:42 -07004059 return 0;
4060};
4061
Tejun Heod3daf282013-06-13 19:39:16 -07004062/**
Tejun Heof20104d2013-08-13 20:22:50 -04004063 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004064 * @work: cgroup->destroy_free_work
4065 *
4066 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004067 * destroyed after all css's are offlined and performs the rest of
4068 * destruction. This is the second step of destruction described in the
4069 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004070 */
Tejun Heof20104d2013-08-13 20:22:50 -04004071static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004072{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004073 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004074
Tejun Heoace2bee2014-02-11 11:52:47 -05004075 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004076 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004077
Paul Menage999cd8a2009-01-07 18:08:36 -08004078 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004079 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004080
Tejun Heo59f52962014-02-11 11:52:49 -05004081 cgroup_put(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004082
Paul Menagebd89aab2007-10-18 23:40:44 -07004083 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004084 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004085}
4086
Tejun Heo2bd59d42014-02-11 11:52:49 -05004087static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004088{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004089 struct cgroup *cgrp = kn->priv;
4090 int ret = 0;
4091
4092 /*
4093 * This is self-destruction but @kn can't be removed while this
4094 * callback is in progress. Let's break active protection. Once
4095 * the protection is broken, @cgrp can be destroyed at any point.
4096 * Pin it so that it stays accessible.
4097 */
4098 cgroup_get(cgrp);
4099 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004100
Tejun Heoace2bee2014-02-11 11:52:47 -05004101 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004102 mutex_lock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004103
4104 /*
4105 * @cgrp might already have been destroyed while we're trying to
4106 * grab the mutexes.
4107 */
4108 if (!cgroup_is_dead(cgrp))
4109 ret = cgroup_destroy_locked(cgrp);
4110
Tejun Heo42809dd2012-11-19 08:13:37 -08004111 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004112 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004113
Tejun Heo2bd59d42014-02-11 11:52:49 -05004114 kernfs_unbreak_active_protection(kn);
4115 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004116 return ret;
4117}
4118
Tejun Heo2bd59d42014-02-11 11:52:49 -05004119static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4120 .remount_fs = cgroup_remount,
4121 .show_options = cgroup_show_options,
4122 .mkdir = cgroup_mkdir,
4123 .rmdir = cgroup_rmdir,
4124 .rename = cgroup_rename,
4125};
4126
Li Zefan06a11922008-04-29 01:00:07 -07004127static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004128{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004129 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004130
4131 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004132
Tejun Heoace2bee2014-02-11 11:52:47 -05004133 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004134 mutex_lock(&cgroup_mutex);
4135
Tejun Heo0adb0702014-02-12 09:29:48 -05004136 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004137
Paul Menageddbcc7e2007-10-18 23:39:30 -07004138 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004139 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004140 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004141 /* We don't handle early failures gracefully */
4142 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004143 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004144
Li Zefane8d55fd2008-04-29 01:00:13 -07004145 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004146 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004147 * newly registered, all tasks and hence the
4148 * init_css_set is in the subsystem's top cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004149 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004150
4151 need_forkexit_callback |= ss->fork || ss->exit;
4152
Li Zefane8d55fd2008-04-29 01:00:13 -07004153 /* At system boot, before all subsystems have been
4154 * registered, no tasks have been forked, so we don't
4155 * need to invoke fork callbacks here. */
4156 BUG_ON(!list_empty(&init_task.tasks));
4157
Tejun Heoae7f1642013-08-13 20:22:50 -04004158 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004159
Tejun Heo648bb562012-11-19 08:13:36 -08004160 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004161 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004162}
4163
4164/**
Li Zefana043e3b2008-02-23 15:24:09 -08004165 * cgroup_init_early - cgroup initialization at system boot
4166 *
4167 * Initialize cgroups at system boot, and initialize any
4168 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004169 */
4170int __init cgroup_init_early(void)
4171{
Tejun Heo30159ec2013-06-25 11:53:37 -07004172 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004173 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004174
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004175 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004176 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004177 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004178 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004179 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004180 init_cgroup_root(&cgroup_dummy_root);
4181 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004182 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004183
Tejun Heo69d02062013-06-12 21:04:50 -07004184 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004185 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4186 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004187 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004188
Tejun Heo3ed80a62014-02-08 10:36:58 -05004189 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004190 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004191 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4192 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004193 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004194 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4195 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4196
Tejun Heoaec25022014-02-08 10:36:58 -05004197 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004198 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004199
4200 if (ss->early_init)
4201 cgroup_init_subsys(ss);
4202 }
4203 return 0;
4204}
4205
4206/**
Li Zefana043e3b2008-02-23 15:24:09 -08004207 * cgroup_init - cgroup initialization
4208 *
4209 * Register cgroup filesystem and /proc file, and initialize
4210 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004211 */
4212int __init cgroup_init(void)
4213{
Tejun Heo30159ec2013-06-25 11:53:37 -07004214 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004215 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004216 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004217
Tejun Heo2bd59d42014-02-11 11:52:49 -05004218 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Tejun Heo2da440a2014-02-11 11:52:48 -05004219
Tejun Heo3ed80a62014-02-08 10:36:58 -05004220 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004221 if (!ss->early_init)
4222 cgroup_init_subsys(ss);
Tejun Heode00ffa2014-02-11 11:52:48 -05004223
4224 /*
4225 * cftype registration needs kmalloc and can't be done
4226 * during early_init. Register base cftypes separately.
4227 */
4228 if (ss->base_cftypes)
4229 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004230 }
4231
Tejun Heofa3ca072013-04-14 11:36:56 -07004232 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004233 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004234
Tejun Heo82fe9b02013-06-25 11:53:37 -07004235 /* Add init_css_set to the hash table */
4236 key = css_set_hash(init_css_set.subsys);
4237 hash_add(css_set_table, &init_css_set.hlist, key);
4238
Tejun Heofc76df72013-06-25 11:53:37 -07004239 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004240
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004241 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4242 0, 1, GFP_KERNEL);
4243 BUG_ON(err < 0);
4244
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004245 mutex_unlock(&cgroup_mutex);
4246
Greg KH676db4a2010-08-05 13:53:35 -07004247 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004248 if (!cgroup_kobj)
4249 return -ENOMEM;
Greg KH676db4a2010-08-05 13:53:35 -07004250
4251 err = register_filesystem(&cgroup_fs_type);
4252 if (err < 0) {
4253 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004254 return err;
Greg KH676db4a2010-08-05 13:53:35 -07004255 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004256
Li Zefan46ae2202008-04-29 01:00:08 -07004257 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004258 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004259}
Paul Menageb4f48b62007-10-18 23:39:33 -07004260
Tejun Heoe5fca242013-11-22 17:14:39 -05004261static int __init cgroup_wq_init(void)
4262{
4263 /*
4264 * There isn't much point in executing destruction path in
4265 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004266 *
4267 * XXX: Must be ordered to make sure parent is offlined after
4268 * children. The ordering requirement is for memcg where a
4269 * parent's offline may wait for a child's leading to deadlock. In
4270 * the long term, this should be fixed from memcg side.
Tejun Heoe5fca242013-11-22 17:14:39 -05004271 *
4272 * We would prefer to do this in cgroup_init() above, but that
4273 * is called before init_workqueues(): so leave this until after.
4274 */
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004275 cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0);
Tejun Heoe5fca242013-11-22 17:14:39 -05004276 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004277
4278 /*
4279 * Used to destroy pidlists and separate to serve as flush domain.
4280 * Cap @max_active to 1 too.
4281 */
4282 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4283 0, 1);
4284 BUG_ON(!cgroup_pidlist_destroy_wq);
4285
Tejun Heoe5fca242013-11-22 17:14:39 -05004286 return 0;
4287}
4288core_initcall(cgroup_wq_init);
4289
Paul Menagea4243162007-10-18 23:39:35 -07004290/*
4291 * proc_cgroup_show()
4292 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4293 * - Used for /proc/<pid>/cgroup.
4294 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4295 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004296 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004297 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4298 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4299 * cgroup to top_cgroup.
4300 */
4301
4302/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004303int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004304{
4305 struct pid *pid;
4306 struct task_struct *tsk;
4307 char *buf;
4308 int retval;
4309 struct cgroupfs_root *root;
4310
4311 retval = -ENOMEM;
4312 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4313 if (!buf)
4314 goto out;
4315
4316 retval = -ESRCH;
4317 pid = m->private;
4318 tsk = get_pid_task(pid, PIDTYPE_PID);
4319 if (!tsk)
4320 goto out_free;
4321
4322 retval = 0;
4323
4324 mutex_lock(&cgroup_mutex);
4325
Li Zefane5f6a862009-01-07 18:07:41 -08004326 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004327 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004328 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004329 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004330
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004331 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004332 for_each_subsys(ss, ssid)
4333 if (root->subsys_mask & (1 << ssid))
4334 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004335 if (strlen(root->name))
4336 seq_printf(m, "%sname=%s", count ? "," : "",
4337 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004338 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004339 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004340 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004341 if (retval < 0)
4342 goto out_unlock;
4343 seq_puts(m, buf);
4344 seq_putc(m, '\n');
4345 }
4346
4347out_unlock:
4348 mutex_unlock(&cgroup_mutex);
4349 put_task_struct(tsk);
4350out_free:
4351 kfree(buf);
4352out:
4353 return retval;
4354}
4355
Paul Menagea4243162007-10-18 23:39:35 -07004356/* Display information about each subsystem and each hierarchy */
4357static int proc_cgroupstats_show(struct seq_file *m, void *v)
4358{
Tejun Heo30159ec2013-06-25 11:53:37 -07004359 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004360 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004361
Paul Menage8bab8dd2008-04-04 14:29:57 -07004362 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004363 /*
4364 * ideally we don't want subsystems moving around while we do this.
4365 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4366 * subsys/hierarchy state.
4367 */
Paul Menagea4243162007-10-18 23:39:35 -07004368 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004369
4370 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004371 seq_printf(m, "%s\t%d\t%d\t%d\n",
4372 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004373 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004374
Paul Menagea4243162007-10-18 23:39:35 -07004375 mutex_unlock(&cgroup_mutex);
4376 return 0;
4377}
4378
4379static int cgroupstats_open(struct inode *inode, struct file *file)
4380{
Al Viro9dce07f2008-03-29 03:07:28 +00004381 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004382}
4383
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004384static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004385 .open = cgroupstats_open,
4386 .read = seq_read,
4387 .llseek = seq_lseek,
4388 .release = single_release,
4389};
4390
Paul Menageb4f48b62007-10-18 23:39:33 -07004391/**
4392 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004393 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004394 *
4395 * Description: A task inherits its parent's cgroup at fork().
4396 *
4397 * A pointer to the shared css_set was automatically copied in
4398 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004399 * it was not made under the protection of RCU or cgroup_mutex, so
4400 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4401 * have already changed current->cgroups, allowing the previously
4402 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004403 *
4404 * At the point that cgroup_fork() is called, 'current' is the parent
4405 * task, and the passed argument 'child' points to the child task.
4406 */
4407void cgroup_fork(struct task_struct *child)
4408{
Tejun Heo9bb71302012-10-18 17:52:07 -07004409 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004410 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004411 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004412 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004413 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004414}
4415
4416/**
Li Zefana043e3b2008-02-23 15:24:09 -08004417 * cgroup_post_fork - called on a new task after adding it to the task list
4418 * @child: the task in question
4419 *
Tejun Heo5edee612012-10-16 15:03:14 -07004420 * Adds the task to the list running through its css_set if necessary and
4421 * call the subsystem fork() callbacks. Has to be after the task is
4422 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004423 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004424 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004425 */
Paul Menage817929e2007-10-18 23:39:36 -07004426void cgroup_post_fork(struct task_struct *child)
4427{
Tejun Heo30159ec2013-06-25 11:53:37 -07004428 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004429 int i;
4430
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004431 /*
4432 * use_task_css_set_links is set to 1 before we walk the tasklist
4433 * under the tasklist_lock and we read it here after we added the child
4434 * to the tasklist under the tasklist_lock as well. If the child wasn't
4435 * yet in the tasklist when we walked through it from
4436 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4437 * should be visible now due to the paired locking and barriers implied
4438 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4439 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4440 * lock on fork.
4441 */
Paul Menage817929e2007-10-18 23:39:36 -07004442 if (use_task_css_set_links) {
4443 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004444 task_lock(child);
4445 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004446 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004447 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004448 write_unlock(&css_set_lock);
4449 }
Tejun Heo5edee612012-10-16 15:03:14 -07004450
4451 /*
4452 * Call ss->fork(). This must happen after @child is linked on
4453 * css_set; otherwise, @child might change state between ->fork()
4454 * and addition to css_set.
4455 */
4456 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004457 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004458 if (ss->fork)
4459 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004460 }
Paul Menage817929e2007-10-18 23:39:36 -07004461}
Tejun Heo5edee612012-10-16 15:03:14 -07004462
Paul Menage817929e2007-10-18 23:39:36 -07004463/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004464 * cgroup_exit - detach cgroup from exiting task
4465 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004466 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004467 *
4468 * Description: Detach cgroup from @tsk and release it.
4469 *
4470 * Note that cgroups marked notify_on_release force every task in
4471 * them to take the global cgroup_mutex mutex when exiting.
4472 * This could impact scaling on very large systems. Be reluctant to
4473 * use notify_on_release cgroups where very high task exit scaling
4474 * is required on large systems.
4475 *
4476 * the_top_cgroup_hack:
4477 *
4478 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4479 *
4480 * We call cgroup_exit() while the task is still competent to
4481 * handle notify_on_release(), then leave the task attached to the
4482 * root cgroup in each hierarchy for the remainder of its exit.
4483 *
4484 * To do this properly, we would increment the reference count on
4485 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4486 * code we would add a second cgroup function call, to drop that
4487 * reference. This would just create an unnecessary hot spot on
4488 * the top_cgroup reference count, to no avail.
4489 *
4490 * Normally, holding a reference to a cgroup without bumping its
4491 * count is unsafe. The cgroup could go away, or someone could
4492 * attach us to a different cgroup, decrementing the count on
4493 * the first cgroup that we never incremented. But in this case,
4494 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004495 * which wards off any cgroup_attach_task() attempts, or task is a failed
4496 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004497 */
4498void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4499{
Tejun Heo30159ec2013-06-25 11:53:37 -07004500 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004501 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004502 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004503
4504 /*
4505 * Unlink from the css_set task list if necessary.
4506 * Optimistically check cg_list before taking
4507 * css_set_lock
4508 */
4509 if (!list_empty(&tsk->cg_list)) {
4510 write_lock(&css_set_lock);
4511 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004512 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004513 write_unlock(&css_set_lock);
4514 }
4515
Paul Menageb4f48b62007-10-18 23:39:33 -07004516 /* Reassign the task to the init_css_set. */
4517 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004518 cset = task_css_set(tsk);
4519 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004520
4521 if (run_callbacks && need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004522 /* see cgroup_post_fork() for details */
4523 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004524 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004525 struct cgroup_subsys_state *old_css = cset->subsys[i];
4526 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004527
Tejun Heoeb954192013-08-08 20:11:23 -04004528 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004529 }
4530 }
4531 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004532 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004533
Tejun Heo5abb8852013-06-12 21:04:49 -07004534 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07004535}
Paul Menage697f4162007-10-18 23:39:34 -07004536
Paul Menagebd89aab2007-10-18 23:40:44 -07004537static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004538{
Li Zefanf50daa72013-03-01 15:06:07 +08004539 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004540 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004541 /*
4542 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004543 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004544 * it now
4545 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004546 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004547
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004548 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004549 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004550 list_empty(&cgrp->release_list)) {
4551 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004552 need_schedule_work = 1;
4553 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004554 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004555 if (need_schedule_work)
4556 schedule_work(&release_agent_work);
4557 }
4558}
4559
Paul Menage81a6a5c2007-10-18 23:39:38 -07004560/*
4561 * Notify userspace when a cgroup is released, by running the
4562 * configured release agent with the name of the cgroup (path
4563 * relative to the root of cgroup file system) as the argument.
4564 *
4565 * Most likely, this user command will try to rmdir this cgroup.
4566 *
4567 * This races with the possibility that some other task will be
4568 * attached to this cgroup before it is removed, or that some other
4569 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4570 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4571 * unused, and this cgroup will be reprieved from its death sentence,
4572 * to continue to serve a useful existence. Next time it's released,
4573 * we will get notified again, if it still has 'notify_on_release' set.
4574 *
4575 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4576 * means only wait until the task is successfully execve()'d. The
4577 * separate release agent task is forked by call_usermodehelper(),
4578 * then control in this thread returns here, without waiting for the
4579 * release agent task. We don't bother to wait because the caller of
4580 * this routine has no use for the exit status of the release agent
4581 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004582 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004583static void cgroup_release_agent(struct work_struct *work)
4584{
4585 BUG_ON(work != &release_agent_work);
4586 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004587 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004588 while (!list_empty(&release_list)) {
4589 char *argv[3], *envp[3];
4590 int i;
Paul Menagee788e062008-07-25 01:46:59 -07004591 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004592 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004593 struct cgroup,
4594 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004595 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004596 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004597 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004598 if (!pathbuf)
4599 goto continue_free;
4600 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
4601 goto continue_free;
4602 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4603 if (!agentbuf)
4604 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004605
4606 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004607 argv[i++] = agentbuf;
4608 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004609 argv[i] = NULL;
4610
4611 i = 0;
4612 /* minimal command environment */
4613 envp[i++] = "HOME=/";
4614 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4615 envp[i] = NULL;
4616
4617 /* Drop the lock while we invoke the usermode helper,
4618 * since the exec could involve hitting disk and hence
4619 * be a slow process */
4620 mutex_unlock(&cgroup_mutex);
4621 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004622 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004623 continue_free:
4624 kfree(pathbuf);
4625 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004626 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004627 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004628 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004629 mutex_unlock(&cgroup_mutex);
4630}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004631
4632static int __init cgroup_disable(char *str)
4633{
Tejun Heo30159ec2013-06-25 11:53:37 -07004634 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004635 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07004636 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004637
4638 while ((token = strsep(&str, ",")) != NULL) {
4639 if (!*token)
4640 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004641
Tejun Heo3ed80a62014-02-08 10:36:58 -05004642 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004643 if (!strcmp(token, ss->name)) {
4644 ss->disabled = 1;
4645 printk(KERN_INFO "Disabling %s control group"
4646 " subsystem\n", ss->name);
4647 break;
4648 }
4649 }
4650 }
4651 return 1;
4652}
4653__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004654
Tejun Heob77d7b62013-08-13 11:01:54 -04004655/**
Tejun Heo5a17f542014-02-11 11:52:47 -05004656 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04004657 * @dentry: directory dentry of interest
4658 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04004659 *
Tejun Heo5a17f542014-02-11 11:52:47 -05004660 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4661 * to get the corresponding css and return it. If such css doesn't exist
4662 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02004663 */
Tejun Heo5a17f542014-02-11 11:52:47 -05004664struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4665 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004666{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004667 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4668 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004669 struct cgroup *cgrp;
Tejun Heob77d7b62013-08-13 11:01:54 -04004670
Tejun Heo35cf0832013-08-26 18:40:56 -04004671 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004672 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4673 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004674 return ERR_PTR(-EBADF);
4675
Tejun Heo5a17f542014-02-11 11:52:47 -05004676 rcu_read_lock();
4677
Tejun Heo2bd59d42014-02-11 11:52:49 -05004678 /*
4679 * This path doesn't originate from kernfs and @kn could already
4680 * have been or be removed at any point. @kn->priv is RCU
4681 * protected for this access. See destroy_locked() for details.
4682 */
4683 cgrp = rcu_dereference(kn->priv);
4684 if (cgrp)
4685 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05004686
4687 if (!css || !css_tryget(css))
4688 css = ERR_PTR(-ENOENT);
4689
4690 rcu_read_unlock();
4691 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004692}
Stephane Eraniane5d13672011-02-14 11:20:01 +02004693
Li Zefan1cb650b2013-08-19 10:05:24 +08004694/**
4695 * css_from_id - lookup css by id
4696 * @id: the cgroup id
4697 * @ss: cgroup subsys to be looked into
4698 *
4699 * Returns the css if there's valid one with @id, otherwise returns NULL.
4700 * Should be called under rcu_read_lock().
4701 */
4702struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4703{
4704 struct cgroup *cgrp;
4705
Tejun Heoace2bee2014-02-11 11:52:47 -05004706 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08004707
4708 cgrp = idr_find(&ss->root->cgroup_idr, id);
4709 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04004710 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08004711 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004712}
4713
Paul Menagefe693432009-09-23 15:56:20 -07004714#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04004715static struct cgroup_subsys_state *
4716debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07004717{
4718 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4719
4720 if (!css)
4721 return ERR_PTR(-ENOMEM);
4722
4723 return css;
4724}
4725
Tejun Heoeb954192013-08-08 20:11:23 -04004726static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07004727{
Tejun Heoeb954192013-08-08 20:11:23 -04004728 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07004729}
4730
Tejun Heo182446d2013-08-08 20:11:24 -04004731static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
4732 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004733{
Tejun Heo182446d2013-08-08 20:11:24 -04004734 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07004735}
4736
Tejun Heo182446d2013-08-08 20:11:24 -04004737static u64 current_css_set_read(struct cgroup_subsys_state *css,
4738 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004739{
4740 return (u64)(unsigned long)current->cgroups;
4741}
4742
Tejun Heo182446d2013-08-08 20:11:24 -04004743static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08004744 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004745{
4746 u64 count;
4747
4748 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07004749 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07004750 rcu_read_unlock();
4751 return count;
4752}
4753
Tejun Heo2da8ca82013-12-05 12:28:04 -05004754static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004755{
Tejun Heo69d02062013-06-12 21:04:50 -07004756 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07004757 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004758
4759 read_lock(&css_set_lock);
4760 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07004761 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07004762 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004763 struct cgroup *c = link->cgrp;
Tejun Heo59f52962014-02-11 11:52:49 -05004764 const char *name = "?";
Paul Menage7717f7b2009-09-23 15:56:22 -07004765
Tejun Heo59f52962014-02-11 11:52:49 -05004766 if (c != cgroup_dummy_top)
4767 name = cgroup_name(c);
4768
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004769 seq_printf(seq, "Root %d group %s\n",
4770 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07004771 }
4772 rcu_read_unlock();
4773 read_unlock(&css_set_lock);
4774 return 0;
4775}
4776
4777#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05004778static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004779{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004780 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07004781 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07004782
4783 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04004784 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004785 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004786 struct task_struct *task;
4787 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07004788 seq_printf(seq, "css_set %p\n", cset);
4789 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004790 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4791 seq_puts(seq, " ...\n");
4792 break;
4793 } else {
4794 seq_printf(seq, " task %d\n",
4795 task_pid_vnr(task));
4796 }
4797 }
4798 }
4799 read_unlock(&css_set_lock);
4800 return 0;
4801}
4802
Tejun Heo182446d2013-08-08 20:11:24 -04004803static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004804{
Tejun Heo182446d2013-08-08 20:11:24 -04004805 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07004806}
4807
4808static struct cftype debug_files[] = {
4809 {
Paul Menagefe693432009-09-23 15:56:20 -07004810 .name = "taskcount",
4811 .read_u64 = debug_taskcount_read,
4812 },
4813
4814 {
4815 .name = "current_css_set",
4816 .read_u64 = current_css_set_read,
4817 },
4818
4819 {
4820 .name = "current_css_set_refcount",
4821 .read_u64 = current_css_set_refcount_read,
4822 },
4823
4824 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004825 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004826 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004827 },
4828
4829 {
4830 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004831 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004832 },
4833
4834 {
Paul Menagefe693432009-09-23 15:56:20 -07004835 .name = "releasable",
4836 .read_u64 = releasable_read,
4837 },
Paul Menagefe693432009-09-23 15:56:20 -07004838
Tejun Heo4baf6e32012-04-01 12:09:55 -07004839 { } /* terminate */
4840};
Paul Menagefe693432009-09-23 15:56:20 -07004841
Tejun Heo073219e2014-02-08 10:36:58 -05004842struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08004843 .css_alloc = debug_css_alloc,
4844 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07004845 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07004846};
4847#endif /* CONFIG_CGROUP_DEBUG */