blob: 89428b9d99334ed9a2ebf8b36430567febbe7459 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070043#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/spinlock.h>
Tejun Heo96d365e2014-02-13 06:58:40 -050045#include <linux/rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070047#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070048#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070049#include <linux/delayacct.h>
50#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080051#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070052#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070053#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070054#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Li Zefan081aa452013-03-13 09:17:09 +080055#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020056#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050057#include <linux/delay.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070058
Arun Sharma600634972011-07-26 16:09:06 -070059#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070060
Tejun Heoe25e2cb2011-12-12 18:12:21 -080061/*
Tejun Heob1a21362013-11-29 10:42:58 -050062 * pidlists linger the following amount before being destroyed. The goal
63 * is avoiding frequent destruction in the middle of consecutive read calls
64 * Expiring in the middle is a performance problem not a correctness one.
65 * 1 sec should be enough.
66 */
67#define CGROUP_PIDLIST_DESTROY_DELAY HZ
68
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050069#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
70 MAX_CFTYPE_NAME + 2)
71
Tejun Heob1a21362013-11-29 10:42:58 -050072/*
Tejun Heoace2bee2014-02-11 11:52:47 -050073 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
74 * creation/removal and hierarchy changing operations including cgroup
75 * creation, removal, css association and controller rebinding. This outer
76 * lock is needed mainly to resolve the circular dependency between kernfs
77 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
78 */
79static DEFINE_MUTEX(cgroup_tree_mutex);
80
81/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080082 * cgroup_mutex is the master lock. Any modification to cgroup or its
83 * hierarchy must be performed while holding it.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080084 */
Tejun Heo22194492013-04-07 09:29:51 -070085#ifdef CONFIG_PROVE_RCU
86DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040087EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070088#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070089static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070090#endif
91
Tejun Heo69e943b2014-02-08 10:36:58 -050092/*
93 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
94 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
95 */
96static DEFINE_SPINLOCK(release_agent_path_lock);
97
Tejun Heoace2bee2014-02-11 11:52:47 -050098#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -050099 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -0500100 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500101 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500102 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo87fb54f2013-12-06 15:11:55 -0500103
Ben Blumaae8aab2010-03-10 15:22:07 -0800104/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500105 * cgroup destruction makes heavy use of work items and there can be a lot
106 * of concurrent destructions. Use a separate workqueue so that cgroup
107 * destruction work items don't end up filling up max_active of system_wq
108 * which may lead to deadlock.
109 */
110static struct workqueue_struct *cgroup_destroy_wq;
111
112/*
Tejun Heob1a21362013-11-29 10:42:58 -0500113 * pidlist destructions need to be flushed on cgroup destruction. Use a
114 * separate workqueue as flush domain.
115 */
116static struct workqueue_struct *cgroup_pidlist_destroy_wq;
117
Tejun Heo3ed80a62014-02-08 10:36:58 -0500118/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500119#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500120static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700121#include <linux/cgroup_subsys.h>
122};
Tejun Heo073219e2014-02-08 10:36:58 -0500123#undef SUBSYS
124
125/* array of cgroup subsystem names */
126#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
127static const char *cgroup_subsys_name[] = {
128#include <linux/cgroup_subsys.h>
129};
130#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700131
Paul Menageddbcc7e2007-10-18 23:39:30 -0700132/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700133 * The dummy hierarchy, reserved for the subsystems that are otherwise
134 * unattached - it never has more than a single cgroup, and all tasks are
135 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700136 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700137static struct cgroupfs_root cgroup_dummy_root;
138
139/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
140static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700141
142/* The list of hierarchy roots */
143
Tejun Heo9871bf92013-06-24 15:21:47 -0700144static LIST_HEAD(cgroup_roots);
145static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700146
Tejun Heo3417ae12014-02-08 10:37:01 -0500147/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700148static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700149
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/*
Tejun Heo96d365e2014-02-13 06:58:40 -0500345 * css_set_rwsem protects the list of css_set objects, and the chain of
346 * tasks off each css_set.
Tejun Heo0942eee2013-08-08 20:11:26 -0400347 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500348static DECLARE_RWSEM(css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700349static int css_set_count;
350
Paul Menage7717f7b2009-09-23 15:56:22 -0700351/*
352 * hash table for cgroup groups. This improves the performance to find
353 * an existing css_set. This hash doesn't (currently) take into
354 * account cgroups in empty hierarchies.
355 */
Li Zefan472b1052008-04-29 01:00:11 -0700356#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800357static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700358
Li Zefan0ac801f2013-01-10 11:49:27 +0800359static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700360{
Li Zefan0ac801f2013-01-10 11:49:27 +0800361 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700362 struct cgroup_subsys *ss;
363 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700364
Tejun Heo30159ec2013-06-25 11:53:37 -0700365 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800366 key += (unsigned long)css[i];
367 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700368
Li Zefan0ac801f2013-01-10 11:49:27 +0800369 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700370}
371
Tejun Heo5abb8852013-06-12 21:04:49 -0700372static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700373{
Tejun Heo69d02062013-06-12 21:04:50 -0700374 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700375
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700376 /*
377 * Ensure that the refcount doesn't hit zero while any readers
378 * can see it. Similar to atomic_dec_and_lock(), but for an
379 * rwlock
380 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700381 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700382 return;
Tejun Heo96d365e2014-02-13 06:58:40 -0500383 down_write(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700384 if (!atomic_dec_and_test(&cset->refcount)) {
Tejun Heo96d365e2014-02-13 06:58:40 -0500385 up_write(&css_set_rwsem);
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700386 return;
387 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700388
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700389 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700390 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700391 css_set_count--;
392
Tejun Heo69d02062013-06-12 21:04:50 -0700393 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700394 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700395
Tejun Heo69d02062013-06-12 21:04:50 -0700396 list_del(&link->cset_link);
397 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800398
Tejun Heo96d365e2014-02-13 06:58:40 -0500399 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700400 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700401 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700402 set_bit(CGRP_RELEASABLE, &cgrp->flags);
403 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700404 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700405
406 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700407 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700408
Tejun Heo96d365e2014-02-13 06:58:40 -0500409 up_write(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700410 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700411}
412
413/*
414 * refcounted get/put for css_set objects
415 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700416static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700417{
Tejun Heo5abb8852013-06-12 21:04:49 -0700418 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700419}
420
Tejun Heo5abb8852013-06-12 21:04:49 -0700421static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700422{
Tejun Heo5abb8852013-06-12 21:04:49 -0700423 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700424}
425
Tejun Heo5abb8852013-06-12 21:04:49 -0700426static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700427{
Tejun Heo5abb8852013-06-12 21:04:49 -0700428 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700429}
430
Tejun Heob326f9d2013-06-24 15:21:48 -0700431/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700432 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700433 * @cset: candidate css_set being tested
434 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700435 * @new_cgrp: cgroup that's being entered by the task
436 * @template: desired set of css pointers in css_set (pre-calculated)
437 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800438 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700439 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
440 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700441static bool compare_css_sets(struct css_set *cset,
442 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700443 struct cgroup *new_cgrp,
444 struct cgroup_subsys_state *template[])
445{
446 struct list_head *l1, *l2;
447
Tejun Heo5abb8852013-06-12 21:04:49 -0700448 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700449 /* Not all subsystems matched */
450 return false;
451 }
452
453 /*
454 * Compare cgroup pointers in order to distinguish between
455 * different cgroups in heirarchies with no subsystems. We
456 * could get by with just this check alone (and skip the
457 * memcmp above) but on most setups the memcmp check will
458 * avoid the need for this more expensive check on almost all
459 * candidates.
460 */
461
Tejun Heo69d02062013-06-12 21:04:50 -0700462 l1 = &cset->cgrp_links;
463 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700464 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700465 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700466 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700467
468 l1 = l1->next;
469 l2 = l2->next;
470 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700471 if (l1 == &cset->cgrp_links) {
472 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700473 break;
474 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700475 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700476 }
477 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700478 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
479 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
480 cgrp1 = link1->cgrp;
481 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700482 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700483 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700484
485 /*
486 * If this hierarchy is the hierarchy of the cgroup
487 * that's changing, then we need to check that this
488 * css_set points to the new cgroup; if it's any other
489 * hierarchy, then this css_set should point to the
490 * same cgroup as the old css_set.
491 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700492 if (cgrp1->root == new_cgrp->root) {
493 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700494 return false;
495 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700496 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700497 return false;
498 }
499 }
500 return true;
501}
502
Tejun Heob326f9d2013-06-24 15:21:48 -0700503/**
504 * find_existing_css_set - init css array and find the matching css_set
505 * @old_cset: the css_set that we're using before the cgroup transition
506 * @cgrp: the cgroup that we're moving into
507 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700508 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700509static struct css_set *find_existing_css_set(struct css_set *old_cset,
510 struct cgroup *cgrp,
511 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700512{
Paul Menagebd89aab2007-10-18 23:40:44 -0700513 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700514 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700515 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800516 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700517 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700518
Ben Blumaae8aab2010-03-10 15:22:07 -0800519 /*
520 * Build the set of subsystem state objects that we want to see in the
521 * new css_set. while subsystems can change globally, the entries here
522 * won't change, so no need for locking.
523 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700524 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400525 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700526 /* Subsystem is in this hierarchy. So we want
527 * the subsystem state from the new
528 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400529 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700530 } else {
531 /* Subsystem is not in this hierarchy, so we
532 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700533 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700534 }
535 }
536
Li Zefan0ac801f2013-01-10 11:49:27 +0800537 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700538 hash_for_each_possible(css_set_table, cset, hlist, key) {
539 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700540 continue;
541
542 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700543 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700544 }
Paul Menage817929e2007-10-18 23:39:36 -0700545
546 /* No existing cgroup group matched */
547 return NULL;
548}
549
Tejun Heo69d02062013-06-12 21:04:50 -0700550static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700551{
Tejun Heo69d02062013-06-12 21:04:50 -0700552 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700553
Tejun Heo69d02062013-06-12 21:04:50 -0700554 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
555 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700556 kfree(link);
557 }
558}
559
Tejun Heo69d02062013-06-12 21:04:50 -0700560/**
561 * allocate_cgrp_cset_links - allocate cgrp_cset_links
562 * @count: the number of links to allocate
563 * @tmp_links: list_head the allocated links are put on
564 *
565 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
566 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700567 */
Tejun Heo69d02062013-06-12 21:04:50 -0700568static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700569{
Tejun Heo69d02062013-06-12 21:04:50 -0700570 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700571 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700572
573 INIT_LIST_HEAD(tmp_links);
574
Li Zefan36553432008-07-29 22:33:19 -0700575 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700576 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700577 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700578 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700579 return -ENOMEM;
580 }
Tejun Heo69d02062013-06-12 21:04:50 -0700581 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700582 }
583 return 0;
584}
585
Li Zefanc12f65d2009-01-07 18:07:42 -0800586/**
587 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700588 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700589 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800590 * @cgrp: the destination cgroup
591 */
Tejun Heo69d02062013-06-12 21:04:50 -0700592static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
593 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800594{
Tejun Heo69d02062013-06-12 21:04:50 -0700595 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800596
Tejun Heo69d02062013-06-12 21:04:50 -0700597 BUG_ON(list_empty(tmp_links));
598 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
599 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700600 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700601 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700602 /*
603 * Always add links to the tail of the list so that the list
604 * is sorted by order of hierarchy creation
605 */
Tejun Heo69d02062013-06-12 21:04:50 -0700606 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800607}
608
Tejun Heob326f9d2013-06-24 15:21:48 -0700609/**
610 * find_css_set - return a new css_set with one cgroup updated
611 * @old_cset: the baseline css_set
612 * @cgrp: the cgroup to be updated
613 *
614 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
615 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700616 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700617static struct css_set *find_css_set(struct css_set *old_cset,
618 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700619{
Tejun Heob326f9d2013-06-24 15:21:48 -0700620 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700621 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700622 struct list_head tmp_links;
623 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800624 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700625
Tejun Heob326f9d2013-06-24 15:21:48 -0700626 lockdep_assert_held(&cgroup_mutex);
627
Paul Menage817929e2007-10-18 23:39:36 -0700628 /* First see if we already have a cgroup group that matches
629 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500630 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700631 cset = find_existing_css_set(old_cset, cgrp, template);
632 if (cset)
633 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500634 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700635
Tejun Heo5abb8852013-06-12 21:04:49 -0700636 if (cset)
637 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700638
Tejun Heof4f4be22013-06-12 21:04:51 -0700639 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700640 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700641 return NULL;
642
Tejun Heo69d02062013-06-12 21:04:50 -0700643 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700644 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700645 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700646 return NULL;
647 }
648
Tejun Heo5abb8852013-06-12 21:04:49 -0700649 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700650 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700651 INIT_LIST_HEAD(&cset->tasks);
652 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700653
654 /* Copy the set of subsystem state objects generated in
655 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700656 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700657
Tejun Heo96d365e2014-02-13 06:58:40 -0500658 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700659 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700660 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700661 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700662
Paul Menage7717f7b2009-09-23 15:56:22 -0700663 if (c->root == cgrp->root)
664 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700665 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700666 }
Paul Menage817929e2007-10-18 23:39:36 -0700667
Tejun Heo69d02062013-06-12 21:04:50 -0700668 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700669
Paul Menage817929e2007-10-18 23:39:36 -0700670 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700671
672 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700673 key = css_set_hash(cset->subsys);
674 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700675
Tejun Heo96d365e2014-02-13 06:58:40 -0500676 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700677
Tejun Heo5abb8852013-06-12 21:04:49 -0700678 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700679}
680
Tejun Heo2bd59d42014-02-11 11:52:49 -0500681static struct cgroupfs_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
682{
683 struct cgroup *top_cgrp = kf_root->kn->priv;
684
685 return top_cgrp->root;
686}
687
Tejun Heof2e85d52014-02-11 11:52:49 -0500688static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
689{
690 int id;
691
692 lockdep_assert_held(&cgroup_mutex);
693
694 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
695 GFP_KERNEL);
696 if (id < 0)
697 return id;
698
699 root->hierarchy_id = id;
700 return 0;
701}
702
703static void cgroup_exit_root_id(struct cgroupfs_root *root)
704{
705 lockdep_assert_held(&cgroup_mutex);
706
707 if (root->hierarchy_id) {
708 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
709 root->hierarchy_id = 0;
710 }
711}
712
713static void cgroup_free_root(struct cgroupfs_root *root)
714{
715 if (root) {
716 /* hierarhcy ID shoulid already have been released */
717 WARN_ON_ONCE(root->hierarchy_id);
718
719 idr_destroy(&root->cgroup_idr);
720 kfree(root);
721 }
722}
723
Tejun Heo776f02f2014-02-12 09:29:50 -0500724static void cgroup_destroy_root(struct cgroupfs_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500725{
Tejun Heof2e85d52014-02-11 11:52:49 -0500726 struct cgroup *cgrp = &root->top_cgroup;
727 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500728
Tejun Heo2bd59d42014-02-11 11:52:49 -0500729 mutex_lock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500730 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500731
Tejun Heo776f02f2014-02-12 09:29:50 -0500732 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500733 BUG_ON(!list_empty(&cgrp->children));
734
Tejun Heof2e85d52014-02-11 11:52:49 -0500735 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo35585572014-02-13 06:58:38 -0500736 WARN_ON(rebind_subsystems(root, 0, root->subsys_mask));
Tejun Heof2e85d52014-02-11 11:52:49 -0500737
738 /*
739 * Release all the links from cset_links to this hierarchy's
740 * root cgroup
741 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500742 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500743
744 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
745 list_del(&link->cset_link);
746 list_del(&link->cgrp_link);
747 kfree(link);
748 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500749 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500750
751 if (!list_empty(&root->root_list)) {
752 list_del(&root->root_list);
753 cgroup_root_count--;
754 }
755
756 cgroup_exit_root_id(root);
757
758 mutex_unlock(&cgroup_mutex);
759 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500760
Tejun Heo2bd59d42014-02-11 11:52:49 -0500761 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500762 cgroup_free_root(root);
763}
764
Paul Menageddbcc7e2007-10-18 23:39:30 -0700765/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700766 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heo96d365e2014-02-13 06:58:40 -0500767 * called with cgroup_mutex and css_set_rwsem held.
Paul Menage7717f7b2009-09-23 15:56:22 -0700768 */
769static struct cgroup *task_cgroup_from_root(struct task_struct *task,
770 struct cgroupfs_root *root)
771{
Tejun Heo5abb8852013-06-12 21:04:49 -0700772 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700773 struct cgroup *res = NULL;
774
Tejun Heo96d365e2014-02-13 06:58:40 -0500775 lockdep_assert_held(&cgroup_mutex);
776 lockdep_assert_held(&css_set_rwsem);
777
Paul Menage7717f7b2009-09-23 15:56:22 -0700778 /*
779 * No need to lock the task - since we hold cgroup_mutex the
780 * task can't change groups, so the only thing that can happen
781 * is that it exits and its css is set back to init_css_set.
782 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700783 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700784 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700785 res = &root->top_cgroup;
786 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700787 struct cgrp_cset_link *link;
788
789 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700790 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700791
Paul Menage7717f7b2009-09-23 15:56:22 -0700792 if (c->root == root) {
793 res = c;
794 break;
795 }
796 }
797 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500798
Paul Menage7717f7b2009-09-23 15:56:22 -0700799 BUG_ON(!res);
800 return res;
801}
802
803/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700804 * There is one global cgroup mutex. We also require taking
805 * task_lock() when dereferencing a task's cgroup subsys pointers.
806 * See "The task_lock() exception", at the end of this comment.
807 *
808 * A task must hold cgroup_mutex to modify cgroups.
809 *
810 * Any task can increment and decrement the count field without lock.
811 * So in general, code holding cgroup_mutex can't rely on the count
812 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800813 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700814 * means that no tasks are currently attached, therefore there is no
815 * way a task attached to that cgroup can fork (the other way to
816 * increment the count). So code holding cgroup_mutex can safely
817 * assume that if the count is zero, it will stay zero. Similarly, if
818 * a task holds cgroup_mutex on a cgroup with zero count, it
819 * knows that the cgroup won't be removed, as cgroup_rmdir()
820 * needs that mutex.
821 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700822 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
823 * (usually) take cgroup_mutex. These are the two most performance
824 * critical pieces of code here. The exception occurs on cgroup_exit(),
825 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
826 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800827 * to the release agent with the name of the cgroup (path relative to
828 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700829 *
830 * A cgroup can only be deleted if both its 'count' of using tasks
831 * is zero, and its list of 'children' cgroups is empty. Since all
832 * tasks in the system use _some_ cgroup, and since there is always at
833 * least one task in the system (init, pid == 1), therefore, top_cgroup
834 * always has either children cgroups and/or using tasks. So we don't
835 * need a special hack to ensure that top_cgroup cannot be deleted.
836 *
837 * The task_lock() exception
838 *
839 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800840 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800841 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700842 * several performance critical places that need to reference
843 * task->cgroup without the expense of grabbing a system global
844 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800845 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700846 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
847 * the task_struct routinely used for such matters.
848 *
849 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800850 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700851 */
852
Tejun Heo628f7cd2013-06-28 16:24:11 -0700853static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500854static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700855static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700856
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500857static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
858 char *buf)
859{
860 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
861 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
862 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
863 cft->ss->name, cft->name);
864 else
865 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
866 return buf;
867}
868
Tejun Heof2e85d52014-02-11 11:52:49 -0500869/**
870 * cgroup_file_mode - deduce file mode of a control file
871 * @cft: the control file in question
872 *
873 * returns cft->mode if ->mode is not 0
874 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
875 * returns S_IRUGO if it has only a read handler
876 * returns S_IWUSR if it has only a write hander
877 */
878static umode_t cgroup_file_mode(const struct cftype *cft)
879{
880 umode_t mode = 0;
881
882 if (cft->mode)
883 return cft->mode;
884
885 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
886 mode |= S_IRUGO;
887
888 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
889 cft->trigger)
890 mode |= S_IWUSR;
891
892 return mode;
893}
894
Li Zefanbe445622013-01-24 14:31:42 +0800895static void cgroup_free_fn(struct work_struct *work)
896{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700897 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800898
Tejun Heo3c9c8252014-02-12 09:29:50 -0500899 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -0500900 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800901
Tejun Heo776f02f2014-02-12 09:29:50 -0500902 if (cgrp->parent) {
903 /*
904 * We get a ref to the parent, and put the ref when this
905 * cgroup is being freed, so it's guaranteed that the
906 * parent won't be destroyed before its children.
907 */
908 cgroup_put(cgrp->parent);
909 kernfs_put(cgrp->kn);
910 kfree(cgrp);
911 } else {
912 /*
913 * This is top cgroup's refcnt reaching zero, which
914 * indicates that the root should be released.
915 */
916 cgroup_destroy_root(cgrp->root);
917 }
Li Zefanbe445622013-01-24 14:31:42 +0800918}
919
920static void cgroup_free_rcu(struct rcu_head *head)
921{
922 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
923
Tejun Heoea15f8c2013-06-13 19:27:42 -0700924 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500925 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800926}
927
Tejun Heo59f52962014-02-11 11:52:49 -0500928static void cgroup_get(struct cgroup *cgrp)
929{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500930 WARN_ON_ONCE(cgroup_is_dead(cgrp));
931 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
932 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700933}
934
Tejun Heo59f52962014-02-11 11:52:49 -0500935static void cgroup_put(struct cgroup *cgrp)
936{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500937 if (!atomic_dec_and_test(&cgrp->refcnt))
938 return;
Tejun Heo776f02f2014-02-12 09:29:50 -0500939 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -0500940 return;
Tejun Heo59f52962014-02-11 11:52:49 -0500941
Tejun Heo2bd59d42014-02-11 11:52:49 -0500942 /*
943 * XXX: cgrp->id is only used to look up css's. As cgroup and
944 * css's lifetimes will be decoupled, it should be made
945 * per-subsystem and moved to css->id so that lookups are
946 * successful until the target css is released.
947 */
948 mutex_lock(&cgroup_mutex);
949 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
950 mutex_unlock(&cgroup_mutex);
951 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700952
Tejun Heo2bd59d42014-02-11 11:52:49 -0500953 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700954}
955
Li Zefan2739d3c2013-01-21 18:18:33 +0800956static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700957{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500958 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700959
Tejun Heoace2bee2014-02-11 11:52:47 -0500960 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500961 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -0700962}
963
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400964/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700965 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700966 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400967 * @subsys_mask: mask of the subsystem ids whose files should be removed
968 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700969static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700970{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400971 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700972 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700973
Tejun Heob420ba72013-07-12 12:34:02 -0700974 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -0500975 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -0700976
977 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400978 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -0500979 list_for_each_entry(cfts, &ss->cfts, node)
980 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400981 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700982}
983
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700985 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986{
Paul Menagebd89aab2007-10-18 23:40:44 -0700987 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700988 struct cgroup_subsys *ss;
Tejun Heo31261212013-06-28 17:07:30 -0700989 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990
Tejun Heoace2bee2014-02-11 11:52:47 -0500991 lockdep_assert_held(&cgroup_tree_mutex);
992 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -0800993
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994 /* Check that any added subsystems are currently free */
Tejun Heo3ed80a62014-02-08 10:36:58 -0500995 for_each_subsys(ss, i)
996 if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
997 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998
Tejun Heo31261212013-06-28 17:07:30 -0700999 ret = cgroup_populate_dir(cgrp, added_mask);
1000 if (ret)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001001 return ret;
Tejun Heo31261212013-06-28 17:07:30 -07001002
1003 /*
1004 * Nothing can fail from this point on. Remove files for the
1005 * removed subsystems and rebind each subsystem.
1006 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001007 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001008 cgroup_clear_dir(cgrp, removed_mask);
Tejun Heo4ac06012014-02-11 11:52:47 -05001009 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001010
Tejun Heo30159ec2013-06-25 11:53:37 -07001011 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001013
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001014 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001016 BUG_ON(cgroup_css(cgrp, ss));
1017 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1018 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001019
Tejun Heo73e80ed2013-08-13 11:01:55 -04001020 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001021 cgroup_css(cgroup_dummy_top, ss));
1022 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001023
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001024 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001026 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001027
Ben Blumcf5d5942010-03-10 15:22:09 -08001028 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001029 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001030 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001032 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1033 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001034
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001036 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001037
Tejun Heoca8bdca2013-08-26 18:40:56 -04001038 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001039 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1040
Tejun Heo9871bf92013-06-24 15:21:47 -07001041 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001042 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 }
1044 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045
Tejun Heo2bd59d42014-02-11 11:52:49 -05001046 kernfs_activate(cgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047 return 0;
1048}
1049
Tejun Heo2bd59d42014-02-11 11:52:49 -05001050static int cgroup_show_options(struct seq_file *seq,
1051 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001053 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001055 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056
Tejun Heob85d2042013-12-06 15:11:57 -05001057 for_each_subsys(ss, ssid)
1058 if (root->subsys_mask & (1 << ssid))
1059 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001060 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1061 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001062 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001064 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001065 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001066
1067 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001068 if (strlen(root->release_agent_path))
1069 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001070 spin_unlock(&release_agent_path_lock);
1071
Tejun Heo2260e7f2012-11-19 08:13:38 -08001072 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001073 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001074 if (strlen(root->name))
1075 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076 return 0;
1077}
1078
1079struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001080 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001082 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001083 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001084 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001085 /* User explicitly requested empty subsystem */
1086 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087};
1088
Ben Blumaae8aab2010-03-10 15:22:07 -08001089/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001090 * Convert a hierarchy specifier into a bitmask of subsystems and
1091 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1092 * array. This function takes refcounts on subsystems to be used, unless it
1093 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001094 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001095static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001097 char *token, *o = data;
1098 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001099 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001100 struct cgroup_subsys *ss;
1101 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001102
Ben Blumaae8aab2010-03-10 15:22:07 -08001103 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1104
Li Zefanf9ab5b52009-06-17 16:26:33 -07001105#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001106 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001107#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108
Paul Menagec6d57f32009-09-23 15:56:19 -07001109 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110
1111 while ((token = strsep(&o, ",")) != NULL) {
1112 if (!*token)
1113 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001114 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001115 /* Explicitly have no subsystems */
1116 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001117 continue;
1118 }
1119 if (!strcmp(token, "all")) {
1120 /* Mutually exclusive option 'all' + subsystem name */
1121 if (one_ss)
1122 return -EINVAL;
1123 all_ss = true;
1124 continue;
1125 }
Tejun Heo873fe092013-04-14 20:15:26 -07001126 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1127 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1128 continue;
1129 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001130 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001131 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001132 continue;
1133 }
1134 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001135 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001136 continue;
1137 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001138 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001139 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001140 continue;
1141 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001142 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001143 /* Specifying two release agents is forbidden */
1144 if (opts->release_agent)
1145 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001146 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001147 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001148 if (!opts->release_agent)
1149 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001150 continue;
1151 }
1152 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001153 const char *name = token + 5;
1154 /* Can't specify an empty name */
1155 if (!strlen(name))
1156 return -EINVAL;
1157 /* Must match [\w.-]+ */
1158 for (i = 0; i < strlen(name); i++) {
1159 char c = name[i];
1160 if (isalnum(c))
1161 continue;
1162 if ((c == '.') || (c == '-') || (c == '_'))
1163 continue;
1164 return -EINVAL;
1165 }
1166 /* Specifying two names is forbidden */
1167 if (opts->name)
1168 return -EINVAL;
1169 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001170 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001171 GFP_KERNEL);
1172 if (!opts->name)
1173 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001174
1175 continue;
1176 }
1177
Tejun Heo30159ec2013-06-25 11:53:37 -07001178 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001179 if (strcmp(token, ss->name))
1180 continue;
1181 if (ss->disabled)
1182 continue;
1183
1184 /* Mutually exclusive option 'all' + subsystem name */
1185 if (all_ss)
1186 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001187 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001188 one_ss = true;
1189
1190 break;
1191 }
1192 if (i == CGROUP_SUBSYS_COUNT)
1193 return -ENOENT;
1194 }
1195
1196 /*
1197 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001198 * otherwise if 'none', 'name=' and a subsystem name options
1199 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001200 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001201 if (all_ss || (!one_ss && !opts->none && !opts->name))
1202 for_each_subsys(ss, i)
1203 if (!ss->disabled)
1204 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001205
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001206 /* Consistency checks */
1207
Tejun Heo873fe092013-04-14 20:15:26 -07001208 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1209 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1210
Tejun Heod3ba07c2014-02-13 06:58:38 -05001211 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1212 opts->cpuset_clone_children || opts->release_agent ||
1213 opts->name) {
1214 pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001215 return -EINVAL;
1216 }
Tejun Heo873fe092013-04-14 20:15:26 -07001217 }
1218
Li Zefanf9ab5b52009-06-17 16:26:33 -07001219 /*
1220 * Option noprefix was introduced just for backward compatibility
1221 * with the old cpuset, so we allow noprefix only if mounting just
1222 * the cpuset subsystem.
1223 */
Tejun Heo93438622013-04-14 20:15:25 -07001224 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001225 return -EINVAL;
1226
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001227
1228 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001229 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001230 return -EINVAL;
1231
1232 /*
1233 * We either have to specify by name or by subsystems. (So all
1234 * empty hierarchies must have a name).
1235 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001236 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001237 return -EINVAL;
1238
1239 return 0;
1240}
1241
Tejun Heo2bd59d42014-02-11 11:52:49 -05001242static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001243{
1244 int ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001245 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001246 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001247 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001248
Tejun Heo873fe092013-04-14 20:15:26 -07001249 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1250 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1251 return -EINVAL;
1252 }
1253
Tejun Heoace2bee2014-02-11 11:52:47 -05001254 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001255 mutex_lock(&cgroup_mutex);
1256
1257 /* See what subsystems are wanted */
1258 ret = parse_cgroupfs_options(data, &opts);
1259 if (ret)
1260 goto out_unlock;
1261
Tejun Heoa8a648c2013-06-24 15:21:47 -07001262 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001263 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1264 task_tgid_nr(current), current->comm);
1265
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001266 added_mask = opts.subsys_mask & ~root->subsys_mask;
1267 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001268
Ben Blumcf5d5942010-03-10 15:22:09 -08001269 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001270 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001271 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001272 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1273 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1274 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001275 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001276 goto out_unlock;
1277 }
1278
Tejun Heof172e672013-06-28 17:07:30 -07001279 /* remounting is not allowed for populated hierarchies */
Tejun Heo3c9c8252014-02-12 09:29:50 -05001280 if (!list_empty(&root->top_cgroup.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001281 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001282 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001283 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001284
Paul Menageddbcc7e2007-10-18 23:39:30 -07001285 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001286 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001287 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001288
Tejun Heo69e943b2014-02-08 10:36:58 -05001289 if (opts.release_agent) {
1290 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001291 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001292 spin_unlock(&release_agent_path_lock);
1293 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001294 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001295 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001296 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001297 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001298 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001299 return ret;
1300}
1301
Tejun Heoafeb0f92014-02-13 06:58:39 -05001302/*
1303 * To reduce the fork() overhead for systems that are not actually using
1304 * their cgroups capability, we don't maintain the lists running through
1305 * each css_set to its tasks until we see the list actually used - in other
1306 * words after the first mount.
1307 */
1308static bool use_task_css_set_links __read_mostly;
1309
1310static void cgroup_enable_task_cg_lists(void)
1311{
1312 struct task_struct *p, *g;
1313
Tejun Heo96d365e2014-02-13 06:58:40 -05001314 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001315
1316 if (use_task_css_set_links)
1317 goto out_unlock;
1318
1319 use_task_css_set_links = true;
1320
1321 /*
1322 * We need tasklist_lock because RCU is not safe against
1323 * while_each_thread(). Besides, a forking task that has passed
1324 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1325 * is not guaranteed to have its child immediately visible in the
1326 * tasklist if we walk through it with RCU.
1327 */
1328 read_lock(&tasklist_lock);
1329 do_each_thread(g, p) {
1330 task_lock(p);
1331
1332 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1333 task_css_set(p) != &init_css_set);
1334
1335 /*
1336 * We should check if the process is exiting, otherwise
1337 * it will race with cgroup_exit() in that the list
1338 * entry won't be deleted though the process has exited.
1339 */
1340 if (!(p->flags & PF_EXITING))
1341 list_add(&p->cg_list, &task_css_set(p)->tasks);
1342
1343 task_unlock(p);
1344 } while_each_thread(g, p);
1345 read_unlock(&tasklist_lock);
1346out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001347 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001348}
1349
Paul Menagecc31edc2008-10-18 20:28:04 -07001350static void init_cgroup_housekeeping(struct cgroup *cgrp)
1351{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001352 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001353 INIT_LIST_HEAD(&cgrp->sibling);
1354 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001355 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001356 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001357 INIT_LIST_HEAD(&cgrp->pidlists);
1358 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001359 cgrp->dummy_css.cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001360}
Paul Menagec6d57f32009-09-23 15:56:19 -07001361
Paul Menageddbcc7e2007-10-18 23:39:30 -07001362static void init_cgroup_root(struct cgroupfs_root *root)
1363{
Paul Menagebd89aab2007-10-18 23:40:44 -07001364 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001365
Paul Menageddbcc7e2007-10-18 23:39:30 -07001366 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001367 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001368 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001369 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee82013-07-31 09:50:50 +08001370 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371}
1372
Paul Menagec6d57f32009-09-23 15:56:19 -07001373static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1374{
1375 struct cgroupfs_root *root;
1376
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001377 if (!opts->subsys_mask && !opts->none)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001378 return ERR_PTR(-EINVAL);
Paul Menagec6d57f32009-09-23 15:56:19 -07001379
1380 root = kzalloc(sizeof(*root), GFP_KERNEL);
1381 if (!root)
1382 return ERR_PTR(-ENOMEM);
1383
1384 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001385
Paul Menagec6d57f32009-09-23 15:56:19 -07001386 root->flags = opts->flags;
1387 if (opts->release_agent)
1388 strcpy(root->release_agent_path, opts->release_agent);
1389 if (opts->name)
1390 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001391 if (opts->cpuset_clone_children)
1392 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001393 return root;
1394}
1395
Tejun Heo35585572014-02-13 06:58:38 -05001396static int cgroup_setup_root(struct cgroupfs_root *root, unsigned long ss_mask)
Tejun Heod427dfe2014-02-11 11:52:48 -05001397{
1398 LIST_HEAD(tmp_links);
Tejun Heod427dfe2014-02-11 11:52:48 -05001399 struct cgroup *root_cgrp = &root->top_cgroup;
Tejun Heod427dfe2014-02-11 11:52:48 -05001400 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001401 int i, ret;
1402
1403 lockdep_assert_held(&cgroup_tree_mutex);
1404 lockdep_assert_held(&cgroup_mutex);
Tejun Heod427dfe2014-02-11 11:52:48 -05001405
1406 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1407 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001408 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001409 root_cgrp->id = ret;
1410
Tejun Heod427dfe2014-02-11 11:52:48 -05001411 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001412 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001413 * but that's OK - it can only be increased by someone holding
1414 * cgroup_lock, and that's us. The worst that can happen is that we
1415 * have some link structures left over
1416 */
1417 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1418 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001419 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001420
1421 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1422 ret = cgroup_init_root_id(root, 2, 0);
1423 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001424 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001425
Tejun Heo2bd59d42014-02-11 11:52:49 -05001426 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1427 KERNFS_ROOT_CREATE_DEACTIVATED,
1428 root_cgrp);
1429 if (IS_ERR(root->kf_root)) {
1430 ret = PTR_ERR(root->kf_root);
1431 goto exit_root_id;
1432 }
1433 root_cgrp->kn = root->kf_root->kn;
Tejun Heod427dfe2014-02-11 11:52:48 -05001434
1435 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1436 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001437 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001438
Tejun Heo35585572014-02-13 06:58:38 -05001439 ret = rebind_subsystems(root, ss_mask, 0);
Tejun Heod427dfe2014-02-11 11:52:48 -05001440 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001441 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001442
1443 /*
1444 * There must be no failure case after here, since rebinding takes
1445 * care of subsystems' refcounts, which are explicitly dropped in
1446 * the failure exit path.
1447 */
1448 list_add(&root->root_list, &cgroup_roots);
1449 cgroup_root_count++;
1450
1451 /*
1452 * Link the top cgroup in this hierarchy into all the css_set
1453 * objects.
1454 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001455 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001456 hash_for_each(css_set_table, i, cset, hlist)
1457 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001458 up_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001459
1460 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001461 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001462
Tejun Heo2bd59d42014-02-11 11:52:49 -05001463 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001464 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001465 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001466
Tejun Heo2bd59d42014-02-11 11:52:49 -05001467destroy_root:
1468 kernfs_destroy_root(root->kf_root);
1469 root->kf_root = NULL;
1470exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001471 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001472out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001473 free_cgrp_cset_links(&tmp_links);
1474 return ret;
1475}
1476
Al Virof7e83572010-07-26 13:23:11 +04001477static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001479 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001480{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001481 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001482 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001483 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001484 int ret;
Tejun Heo56fde9e2014-02-13 06:58:38 -05001485
1486 /*
1487 * The first time anyone tries to mount a cgroup, enable the list
1488 * linking each css_set to its tasks and fix up all existing tasks.
1489 */
1490 if (!use_task_css_set_links)
1491 cgroup_enable_task_cg_lists();
Tejun Heo776f02f2014-02-12 09:29:50 -05001492retry:
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001493 mutex_lock(&cgroup_tree_mutex);
1494 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001495
1496 /* First find the desired set of subsystems */
1497 ret = parse_cgroupfs_options(data, &opts);
Paul Menagec6d57f32009-09-23 15:56:19 -07001498 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001499 goto out_unlock;
Paul Menagec6d57f32009-09-23 15:56:19 -07001500
Tejun Heo2bd59d42014-02-11 11:52:49 -05001501 /* look for a matching existing root */
1502 for_each_active_root(root) {
1503 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001504
Paul Menagec6d57f32009-09-23 15:56:19 -07001505 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001506 * If we asked for a name then it must match. Also, if
1507 * name matches but sybsys_mask doesn't, we should fail.
1508 * Remember whether name matched.
Paul Menagec6d57f32009-09-23 15:56:19 -07001509 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001510 if (opts.name) {
1511 if (strcmp(opts.name, root->name))
1512 continue;
1513 name_match = true;
1514 }
1515
1516 /*
1517 * If we asked for subsystems (or explicitly for no
1518 * subsystems) then they must match.
1519 */
1520 if ((opts.subsys_mask || opts.none) &&
1521 (opts.subsys_mask != root->subsys_mask)) {
1522 if (!name_match)
1523 continue;
1524 ret = -EBUSY;
1525 goto out_unlock;
1526 }
Tejun Heo873fe092013-04-14 20:15:26 -07001527
Tejun Heoc7ba8282013-06-29 14:06:10 -07001528 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001529 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1530 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1531 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001532 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001533 } else {
1534 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1535 }
Tejun Heo873fe092013-04-14 20:15:26 -07001536 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001537
Tejun Heo776f02f2014-02-12 09:29:50 -05001538 /*
1539 * A root's lifetime is governed by its top cgroup. Zero
1540 * ref indicate that the root is being destroyed. Wait for
1541 * destruction to complete so that the subsystems are free.
1542 * We can use wait_queue for the wait but this path is
1543 * super cold. Let's just sleep for a bit and retry.
1544 */
1545 if (!atomic_inc_not_zero(&root->top_cgroup.refcnt)) {
1546 mutex_unlock(&cgroup_mutex);
1547 mutex_unlock(&cgroup_tree_mutex);
1548 msleep(10);
1549 goto retry;
1550 }
1551
1552 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001553 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
Tejun Heo35585572014-02-13 06:58:38 -05001563 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001564 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))
Tejun Heo776f02f2014-02-12 09:29:50 -05001579 cgroup_put(&root->top_cgroup);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001580 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
Tejun Heo776f02f2014-02-12 09:29:50 -05001588 cgroup_put(&root->top_cgroup);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001589 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/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001601 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001602 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001603 * @buf: the buffer to write the path into
1604 * @buflen: the length of the buffer
1605 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001606 * Determine @task's cgroup on the first (the one with the lowest non-zero
1607 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1608 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1609 * cgroup controller callbacks.
1610 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001611 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001612 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001613char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001614{
1615 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001616 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001617 int hierarchy_id = 1;
1618 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001619
1620 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001621 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001622
Tejun Heo913ffdb2013-07-11 16:34:48 -07001623 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1624
Tejun Heo857a2be2013-04-14 20:50:08 -07001625 if (root) {
1626 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001627 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001628 } else {
1629 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001630 if (strlcpy(buf, "/", buflen) < buflen)
1631 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001632 }
1633
Tejun Heo96d365e2014-02-13 06:58:40 -05001634 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001635 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001636 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001637}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001638EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001639
Ben Blum74a11662011-05-26 16:25:20 -07001640/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001641 * Control Group taskset
1642 */
Tejun Heo134d3372011-12-12 18:12:21 -08001643struct task_and_cgroup {
1644 struct task_struct *task;
1645 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001646 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001647};
1648
Tejun Heo2f7ee562011-12-12 18:12:21 -08001649struct cgroup_taskset {
1650 struct task_and_cgroup single;
1651 struct flex_array *tc_array;
1652 int tc_array_len;
1653 int idx;
1654 struct cgroup *cur_cgrp;
1655};
1656
1657/**
1658 * cgroup_taskset_first - reset taskset and return the first task
1659 * @tset: taskset of interest
1660 *
1661 * @tset iteration is initialized and the first task is returned.
1662 */
1663struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1664{
1665 if (tset->tc_array) {
1666 tset->idx = 0;
1667 return cgroup_taskset_next(tset);
1668 } else {
1669 tset->cur_cgrp = tset->single.cgrp;
1670 return tset->single.task;
1671 }
1672}
1673EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1674
1675/**
1676 * cgroup_taskset_next - iterate to the next task in taskset
1677 * @tset: taskset of interest
1678 *
1679 * Return the next task in @tset. Iteration must have been initialized
1680 * with cgroup_taskset_first().
1681 */
1682struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1683{
1684 struct task_and_cgroup *tc;
1685
1686 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1687 return NULL;
1688
1689 tc = flex_array_get(tset->tc_array, tset->idx++);
1690 tset->cur_cgrp = tc->cgrp;
1691 return tc->task;
1692}
1693EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1694
1695/**
Tejun Heod99c8722013-08-08 20:11:27 -04001696 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001697 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001698 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001699 *
Tejun Heod99c8722013-08-08 20:11:27 -04001700 * Return the css for the current (last returned) task of @tset for
1701 * subsystem specified by @subsys_id. This function must be preceded by
1702 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001703 */
Tejun Heod99c8722013-08-08 20:11:27 -04001704struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1705 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001706{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001707 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001708}
Tejun Heod99c8722013-08-08 20:11:27 -04001709EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001710
1711/**
1712 * cgroup_taskset_size - return the number of tasks in taskset
1713 * @tset: taskset of interest
1714 */
1715int cgroup_taskset_size(struct cgroup_taskset *tset)
1716{
1717 return tset->tc_array ? tset->tc_array_len : 1;
1718}
1719EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1720
1721
Ben Blum74a11662011-05-26 16:25:20 -07001722/*
1723 * cgroup_task_migrate - move a task from one cgroup to another.
1724 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001725 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001726 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001727static void cgroup_task_migrate(struct cgroup *old_cgrp,
1728 struct task_struct *tsk,
1729 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001730{
Tejun Heo5abb8852013-06-12 21:04:49 -07001731 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001732
1733 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001734 * We are synchronized through threadgroup_lock() against PF_EXITING
1735 * setting such that we can't race against cgroup_exit() changing the
1736 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001737 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001738 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001739 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001740
Ben Blum74a11662011-05-26 16:25:20 -07001741 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001742 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001743 task_unlock(tsk);
1744
Tejun Heo96d365e2014-02-13 06:58:40 -05001745 down_write(&css_set_rwsem);
Tejun Heo56fde9e2014-02-13 06:58:38 -05001746 list_move(&tsk->cg_list, &new_cset->tasks);
Tejun Heo96d365e2014-02-13 06:58:40 -05001747 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07001748
1749 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001750 * We just gained a reference on old_cset by taking it from the
1751 * task. As trading it for new_cset is protected by cgroup_mutex,
1752 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001753 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001754 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1755 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001756}
1757
Li Zefana043e3b2008-02-23 15:24:09 -08001758/**
Li Zefan081aa452013-03-13 09:17:09 +08001759 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001760 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001761 * @tsk: the task or the leader of the threadgroup to be attached
1762 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001763 *
Tejun Heo257058a2011-12-12 18:12:21 -08001764 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001765 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001766 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001767static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1768 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001769{
1770 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001771 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001772 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001773 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001774 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001775 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001776 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001777 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001778
1779 /*
1780 * step 0: in order to do expensive, possibly blocking operations for
1781 * every thread, we cannot iterate the thread group list, since it needs
1782 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001783 * group - group_rwsem prevents new threads from appearing, and if
1784 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001785 */
Li Zefan081aa452013-03-13 09:17:09 +08001786 if (threadgroup)
1787 group_size = get_nr_threads(tsk);
1788 else
1789 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001790 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001791 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001792 if (!group)
1793 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001794 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001795 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001796 if (retval)
1797 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001798
Ben Blum74a11662011-05-26 16:25:20 -07001799 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001800 /*
1801 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1802 * already PF_EXITING could be freed from underneath us unless we
1803 * take an rcu_read_lock.
1804 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001805 down_read(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001806 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001807 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001808 struct task_and_cgroup ent;
1809
Tejun Heocd3d0952011-12-12 18:12:21 -08001810 /* @tsk either already exited or can't exit until the end */
1811 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001812 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001813
Ben Blum74a11662011-05-26 16:25:20 -07001814 /* as per above, nr_threads may decrease, but not increase. */
1815 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001816 ent.task = tsk;
1817 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001818 /* nothing to do if this task is already in the cgroup */
1819 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001820 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001821 /*
1822 * saying GFP_ATOMIC has no effect here because we did prealloc
1823 * earlier, but it's good form to communicate our expectations.
1824 */
Tejun Heo134d3372011-12-12 18:12:21 -08001825 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07001826 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07001827 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08001828 next:
Li Zefan081aa452013-03-13 09:17:09 +08001829 if (!threadgroup)
1830 break;
Ben Blum74a11662011-05-26 16:25:20 -07001831 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001832 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05001833 up_read(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07001834 /* remember the number of threads in the array for later. */
1835 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001836 tset.tc_array = group;
1837 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001838
Tejun Heo134d3372011-12-12 18:12:21 -08001839 /* methods shouldn't be called if no task is actually migrating */
1840 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001841 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08001842 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08001843
Ben Blum74a11662011-05-26 16:25:20 -07001844 /*
1845 * step 1: check that we can legitimately attach to the cgroup.
1846 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001847 for_each_css(css, i, cgrp) {
1848 if (css->ss->can_attach) {
1849 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001850 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001851 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07001852 goto out_cancel_attach;
1853 }
1854 }
Ben Blum74a11662011-05-26 16:25:20 -07001855 }
1856
1857 /*
1858 * step 2: make sure css_sets exist for all threads to be migrated.
1859 * we use find_css_set, which allocates a new one if necessary.
1860 */
Ben Blum74a11662011-05-26 16:25:20 -07001861 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07001862 struct css_set *old_cset;
1863
Tejun Heo134d3372011-12-12 18:12:21 -08001864 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001865 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001866 tc->cset = find_css_set(old_cset, cgrp);
1867 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001868 retval = -ENOMEM;
1869 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07001870 }
1871 }
1872
1873 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001874 * step 3: now that we're guaranteed success wrt the css_sets,
1875 * proceed to move all tasks to the new cgroup. There are no
1876 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07001877 */
Ben Blum74a11662011-05-26 16:25:20 -07001878 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08001879 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001880 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07001881 }
1882 /* nothing is sensitive to fork() after this point. */
1883
1884 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001885 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07001886 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001887 for_each_css(css, i, cgrp)
1888 if (css->ss->attach)
1889 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001890
1891 /*
1892 * step 5: success! and cleanup
1893 */
Ben Blum74a11662011-05-26 16:25:20 -07001894 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001895out_put_css_set_refs:
1896 if (retval) {
1897 for (i = 0; i < group_size; i++) {
1898 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001899 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001900 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001901 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001902 }
Ben Blum74a11662011-05-26 16:25:20 -07001903 }
1904out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07001905 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001906 for_each_css(css, i, cgrp) {
1907 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07001908 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001909 if (css->ss->cancel_attach)
1910 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001911 }
1912 }
Ben Blum74a11662011-05-26 16:25:20 -07001913out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07001914 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07001915 return retval;
1916}
1917
1918/*
1919 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08001920 * function to attach either it or all tasks in its threadgroup. Will lock
1921 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07001922 */
1923static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001924{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001925 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001926 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001927 int ret;
1928
Ben Blum74a11662011-05-26 16:25:20 -07001929 if (!cgroup_lock_live_group(cgrp))
1930 return -ENODEV;
1931
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001932retry_find_task:
1933 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001934 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001935 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07001936 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07001937 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001938 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001939 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001940 }
Ben Blum74a11662011-05-26 16:25:20 -07001941 /*
1942 * even if we're attaching all tasks in the thread group, we
1943 * only need to check permissions on one of them.
1944 */
David Howellsc69e8d92008-11-14 10:39:19 +11001945 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07001946 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
1947 !uid_eq(cred->euid, tcred->uid) &&
1948 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001949 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001950 ret = -EACCES;
1951 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001952 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001953 } else
1954 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08001955
1956 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001957 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02001958
1959 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07001960 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02001961 * trapped in a cpuset, or RT worker may be born in a cgroup
1962 * with no rt_runtime allocated. Just say no.
1963 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07001964 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02001965 ret = -EINVAL;
1966 rcu_read_unlock();
1967 goto out_unlock_cgroup;
1968 }
1969
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001970 get_task_struct(tsk);
1971 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08001972
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001973 threadgroup_lock(tsk);
1974 if (threadgroup) {
1975 if (!thread_group_leader(tsk)) {
1976 /*
1977 * a race with de_thread from another thread's exec()
1978 * may strip us of our leadership, if this happens,
1979 * there is no choice but to throw this task away and
1980 * try again; this is
1981 * "double-double-toil-and-trouble-check locking".
1982 */
1983 threadgroup_unlock(tsk);
1984 put_task_struct(tsk);
1985 goto retry_find_task;
1986 }
Li Zefan081aa452013-03-13 09:17:09 +08001987 }
1988
1989 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
1990
Tejun Heocd3d0952011-12-12 18:12:21 -08001991 threadgroup_unlock(tsk);
1992
Paul Menagebbcb81d2007-10-18 23:39:32 -07001993 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001994out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07001995 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001996 return ret;
1997}
1998
Tejun Heo7ae1bad2013-04-07 09:29:51 -07001999/**
2000 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2001 * @from: attach to all cgroups of a given task
2002 * @tsk: the task to be attached
2003 */
2004int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2005{
2006 struct cgroupfs_root *root;
2007 int retval = 0;
2008
Tejun Heo47cfcd02013-04-07 09:29:51 -07002009 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002010 for_each_active_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002011 struct cgroup *from_cgrp;
2012
2013 down_read(&css_set_rwsem);
2014 from_cgrp = task_cgroup_from_root(from, root);
2015 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002016
Li Zefan6f4b7e62013-07-31 16:18:36 +08002017 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002018 if (retval)
2019 break;
2020 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002021 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002022
2023 return retval;
2024}
2025EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2026
Tejun Heo182446d2013-08-08 20:11:24 -04002027static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2028 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002029{
Tejun Heo182446d2013-08-08 20:11:24 -04002030 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002031}
2032
Tejun Heo182446d2013-08-08 20:11:24 -04002033static int cgroup_procs_write(struct cgroup_subsys_state *css,
2034 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002035{
Tejun Heo182446d2013-08-08 20:11:24 -04002036 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002037}
2038
Tejun Heo182446d2013-08-08 20:11:24 -04002039static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2040 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002041{
Tejun Heo5f469902014-02-11 11:52:48 -05002042 struct cgroupfs_root *root = css->cgroup->root;
2043
2044 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002045 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002046 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002047 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002048 strlcpy(root->release_agent_path, buffer,
2049 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002050 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002051 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002052 return 0;
2053}
2054
Tejun Heo2da8ca82013-12-05 12:28:04 -05002055static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002056{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002057 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002058
Paul Menagee788e062008-07-25 01:46:59 -07002059 if (!cgroup_lock_live_group(cgrp))
2060 return -ENODEV;
2061 seq_puts(seq, cgrp->root->release_agent_path);
2062 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002063 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002064 return 0;
2065}
2066
Tejun Heo2da8ca82013-12-05 12:28:04 -05002067static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002068{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002069 struct cgroup *cgrp = seq_css(seq)->cgroup;
2070
2071 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002072 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002073}
2074
Tejun Heo2bd59d42014-02-11 11:52:49 -05002075static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2076 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002077{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002078 struct cgroup *cgrp = of->kn->parent->priv;
2079 struct cftype *cft = of->kn->priv;
2080 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002081 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002082
Tejun Heo2bd59d42014-02-11 11:52:49 -05002083 /*
2084 * kernfs guarantees that a file isn't deleted with operations in
2085 * flight, which means that the matching css is and stays alive and
2086 * doesn't need to be pinned. The RCU locking is not necessary
2087 * either. It's just for the convenience of using cgroup_css().
2088 */
2089 rcu_read_lock();
2090 css = cgroup_css(cgrp, cft->ss);
2091 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002092
Tejun Heoa742c592013-12-05 12:28:03 -05002093 if (cft->write_string) {
2094 ret = cft->write_string(css, cft, strstrip(buf));
2095 } else if (cft->write_u64) {
2096 unsigned long long v;
2097 ret = kstrtoull(buf, 0, &v);
2098 if (!ret)
2099 ret = cft->write_u64(css, cft, v);
2100 } else if (cft->write_s64) {
2101 long long v;
2102 ret = kstrtoll(buf, 0, &v);
2103 if (!ret)
2104 ret = cft->write_s64(css, cft, v);
2105 } else if (cft->trigger) {
2106 ret = cft->trigger(css, (unsigned int)cft->private);
2107 } else {
2108 ret = -EINVAL;
2109 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002110
Tejun Heoa742c592013-12-05 12:28:03 -05002111 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002112}
2113
Tejun Heo6612f052013-12-05 12:28:04 -05002114static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002115{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002116 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002117}
2118
2119static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2120{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002121 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002122}
2123
2124static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2125{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002126 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002127}
2128
2129static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2130{
Tejun Heo7da11272013-12-05 12:28:04 -05002131 struct cftype *cft = seq_cft(m);
2132 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002133
Tejun Heo2da8ca82013-12-05 12:28:04 -05002134 if (cft->seq_show)
2135 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002136
Tejun Heo896f5192013-12-05 12:28:04 -05002137 if (cft->read_u64)
2138 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2139 else if (cft->read_s64)
2140 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2141 else
2142 return -EINVAL;
2143 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002144}
2145
Tejun Heo2bd59d42014-02-11 11:52:49 -05002146static struct kernfs_ops cgroup_kf_single_ops = {
2147 .atomic_write_len = PAGE_SIZE,
2148 .write = cgroup_file_write,
2149 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002150};
2151
Tejun Heo2bd59d42014-02-11 11:52:49 -05002152static struct kernfs_ops cgroup_kf_ops = {
2153 .atomic_write_len = PAGE_SIZE,
2154 .write = cgroup_file_write,
2155 .seq_start = cgroup_seqfile_start,
2156 .seq_next = cgroup_seqfile_next,
2157 .seq_stop = cgroup_seqfile_stop,
2158 .seq_show = cgroup_seqfile_show,
2159};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002160
2161/*
2162 * cgroup_rename - Only allow simple rename of directories in place.
2163 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002164static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2165 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002166{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002167 struct cgroup *cgrp = kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002168 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002169
Tejun Heo2bd59d42014-02-11 11:52:49 -05002170 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002171 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002172 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002173 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002174
Tejun Heo6db8e852013-06-14 11:18:22 -07002175 /*
2176 * This isn't a proper migration and its usefulness is very
2177 * limited. Disallow if sane_behavior.
2178 */
2179 if (cgroup_sane_behavior(cgrp))
2180 return -EPERM;
2181
Tejun Heo2bd59d42014-02-11 11:52:49 -05002182 mutex_lock(&cgroup_tree_mutex);
2183 mutex_lock(&cgroup_mutex);
2184
2185 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002186
Tejun Heo2bd59d42014-02-11 11:52:49 -05002187 mutex_unlock(&cgroup_mutex);
2188 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002189 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002190}
2191
Tejun Heo2bb566c2013-08-08 20:11:23 -04002192static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002193{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002194 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002195 struct kernfs_node *kn;
2196 struct lock_class_key *key = NULL;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002197
Tejun Heo2bd59d42014-02-11 11:52:49 -05002198#ifdef CONFIG_DEBUG_LOCK_ALLOC
2199 key = &cft->lockdep_key;
2200#endif
2201 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2202 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2203 NULL, false, key);
2204 if (IS_ERR(kn))
2205 return PTR_ERR(kn);
2206 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002207}
2208
Tejun Heob1f28d32013-06-28 16:24:10 -07002209/**
2210 * cgroup_addrm_files - add or remove files to a cgroup directory
2211 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002212 * @cfts: array of cftypes to be added
2213 * @is_add: whether to add or remove
2214 *
2215 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002216 * For removals, this function never fails. If addition fails, this
2217 * function doesn't remove files already added. The caller is responsible
2218 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002219 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002220static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2221 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002222{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002223 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002224 int ret;
2225
Tejun Heoace2bee2014-02-11 11:52:47 -05002226 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002227
2228 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002229 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002230 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2231 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002232 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2233 continue;
2234 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2235 continue;
2236
Li Zefan2739d3c2013-01-21 18:18:33 +08002237 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002238 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002239 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002240 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002241 cft->name, ret);
2242 return ret;
2243 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002244 } else {
2245 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002246 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002247 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002248 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002249}
2250
Tejun Heo21a2d3432014-02-12 09:29:49 -05002251static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002252{
2253 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002254 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002255 struct cgroup *root = &ss->root->top_cgroup;
Tejun Heo492eb212013-08-08 20:11:25 -04002256 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002257 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002258
Tejun Heo21a2d3432014-02-12 09:29:49 -05002259 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo4ac06012014-02-11 11:52:47 -05002260
Tejun Heo21a2d3432014-02-12 09:29:49 -05002261 /* don't bother if @ss isn't attached */
2262 if (ss->root == &cgroup_dummy_root)
Tejun Heo9ccece82013-06-28 16:24:11 -07002263 return 0;
Li Zefane8c82d22013-06-18 18:48:37 +08002264
Li Zefane8c82d22013-06-18 18:48:37 +08002265 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002266 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002267 struct cgroup *cgrp = css->cgroup;
2268
Li Zefane8c82d22013-06-18 18:48:37 +08002269 if (cgroup_is_dead(cgrp))
2270 continue;
2271
Tejun Heo21a2d3432014-02-12 09:29:49 -05002272 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002273 if (ret)
2274 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002275 }
Tejun Heo21a2d3432014-02-12 09:29:49 -05002276
2277 if (is_add && !ret)
2278 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002279 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002280}
2281
Tejun Heo2da440a2014-02-11 11:52:48 -05002282static void cgroup_exit_cftypes(struct cftype *cfts)
2283{
2284 struct cftype *cft;
2285
Tejun Heo2bd59d42014-02-11 11:52:49 -05002286 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2287 /* free copy for custom atomic_write_len, see init_cftypes() */
2288 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2289 kfree(cft->kf_ops);
2290 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002291 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002292 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002293}
2294
Tejun Heo2bd59d42014-02-11 11:52:49 -05002295static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002296{
2297 struct cftype *cft;
2298
Tejun Heo2bd59d42014-02-11 11:52:49 -05002299 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2300 struct kernfs_ops *kf_ops;
2301
Tejun Heo0adb0702014-02-12 09:29:48 -05002302 WARN_ON(cft->ss || cft->kf_ops);
2303
Tejun Heo2bd59d42014-02-11 11:52:49 -05002304 if (cft->seq_start)
2305 kf_ops = &cgroup_kf_ops;
2306 else
2307 kf_ops = &cgroup_kf_single_ops;
2308
2309 /*
2310 * Ugh... if @cft wants a custom max_write_len, we need to
2311 * make a copy of kf_ops to set its atomic_write_len.
2312 */
2313 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2314 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2315 if (!kf_ops) {
2316 cgroup_exit_cftypes(cfts);
2317 return -ENOMEM;
2318 }
2319 kf_ops->atomic_write_len = cft->max_write_len;
2320 }
2321
2322 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002323 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002324 }
2325
2326 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002327}
2328
Tejun Heo21a2d3432014-02-12 09:29:49 -05002329static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2330{
2331 lockdep_assert_held(&cgroup_tree_mutex);
2332
2333 if (!cfts || !cfts[0].ss)
2334 return -ENOENT;
2335
2336 list_del(&cfts->node);
2337 cgroup_apply_cftypes(cfts, false);
2338 cgroup_exit_cftypes(cfts);
2339 return 0;
2340}
2341
Tejun Heo8e3f6542012-04-01 12:09:55 -07002342/**
Tejun Heo80b13582014-02-12 09:29:48 -05002343 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2344 * @cfts: zero-length name terminated array of cftypes
2345 *
2346 * Unregister @cfts. Files described by @cfts are removed from all
2347 * existing cgroups and all future cgroups won't have them either. This
2348 * function can be called anytime whether @cfts' subsys is attached or not.
2349 *
2350 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2351 * registered.
2352 */
2353int cgroup_rm_cftypes(struct cftype *cfts)
2354{
Tejun Heo21a2d3432014-02-12 09:29:49 -05002355 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002356
Tejun Heo21a2d3432014-02-12 09:29:49 -05002357 mutex_lock(&cgroup_tree_mutex);
2358 ret = cgroup_rm_cftypes_locked(cfts);
2359 mutex_unlock(&cgroup_tree_mutex);
2360 return ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002361}
2362
2363/**
Tejun Heo8e3f6542012-04-01 12:09:55 -07002364 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2365 * @ss: target cgroup subsystem
2366 * @cfts: zero-length name terminated array of cftypes
2367 *
2368 * Register @cfts to @ss. Files described by @cfts are created for all
2369 * existing cgroups to which @ss is attached and all future cgroups will
2370 * have them too. This function can be called anytime whether @ss is
2371 * attached or not.
2372 *
2373 * Returns 0 on successful registration, -errno on failure. Note that this
2374 * function currently returns 0 as long as @cfts registration is successful
2375 * even if some file creation attempts on existing cgroups fail.
2376 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002377int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002378{
Tejun Heo9ccece82013-06-28 16:24:11 -07002379 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002380
Tejun Heo2bd59d42014-02-11 11:52:49 -05002381 ret = cgroup_init_cftypes(ss, cfts);
2382 if (ret)
2383 return ret;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002384
Tejun Heo21a2d3432014-02-12 09:29:49 -05002385 mutex_lock(&cgroup_tree_mutex);
2386
Tejun Heo0adb0702014-02-12 09:29:48 -05002387 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d3432014-02-12 09:29:49 -05002388 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002389 if (ret)
Tejun Heo21a2d3432014-02-12 09:29:49 -05002390 cgroup_rm_cftypes_locked(cfts);
2391
2392 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002393 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002394}
2395EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2396
Li Zefana043e3b2008-02-23 15:24:09 -08002397/**
2398 * cgroup_task_count - count the number of tasks in a cgroup.
2399 * @cgrp: the cgroup in question
2400 *
2401 * Return the number of tasks in the cgroup.
2402 */
Tejun Heo07bc3562014-02-13 06:58:39 -05002403static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002404{
2405 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002406 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002407
Tejun Heo96d365e2014-02-13 06:58:40 -05002408 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07002409 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2410 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05002411 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002412 return count;
2413}
2414
Tejun Heo574bd9f2012-11-09 09:12:29 -08002415/**
Tejun Heo492eb212013-08-08 20:11:25 -04002416 * css_next_child - find the next child of a given css
2417 * @pos_css: the current position (%NULL to initiate traversal)
2418 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002419 *
Tejun Heo492eb212013-08-08 20:11:25 -04002420 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002421 * under either cgroup_mutex or RCU read lock. The only requirement is
2422 * that @parent_css and @pos_css are accessible. The next sibling is
2423 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002424 */
Tejun Heo492eb212013-08-08 20:11:25 -04002425struct cgroup_subsys_state *
2426css_next_child(struct cgroup_subsys_state *pos_css,
2427 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002428{
Tejun Heo492eb212013-08-08 20:11:25 -04002429 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2430 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002431 struct cgroup *next;
2432
Tejun Heoace2bee2014-02-11 11:52:47 -05002433 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002434
2435 /*
2436 * @pos could already have been removed. Once a cgroup is removed,
2437 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002438 * changes. As CGRP_DEAD assertion is serialized and happens
2439 * before the cgroup is taken off the ->sibling list, if we see it
2440 * unasserted, it's guaranteed that the next sibling hasn't
2441 * finished its grace period even if it's already removed, and thus
2442 * safe to dereference from this RCU critical section. If
2443 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2444 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002445 *
2446 * If @pos is dead, its next pointer can't be dereferenced;
2447 * however, as each cgroup is given a monotonically increasing
2448 * unique serial number and always appended to the sibling list,
2449 * the next one can be found by walking the parent's children until
2450 * we see a cgroup with higher serial number than @pos's. While
2451 * this path can be slower, it's taken only when either the current
2452 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002453 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002454 if (!pos) {
2455 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2456 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002457 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002458 } else {
2459 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2460 if (next->serial_nr > pos->serial_nr)
2461 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002462 }
2463
Tejun Heo492eb212013-08-08 20:11:25 -04002464 if (&next->sibling == &cgrp->children)
2465 return NULL;
2466
Tejun Heoca8bdca2013-08-26 18:40:56 -04002467 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002468}
Tejun Heo492eb212013-08-08 20:11:25 -04002469EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002470
2471/**
Tejun Heo492eb212013-08-08 20:11:25 -04002472 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002473 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002474 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002475 *
Tejun Heo492eb212013-08-08 20:11:25 -04002476 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002477 * to visit for pre-order traversal of @root's descendants. @root is
2478 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002479 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002480 * While this function requires cgroup_mutex or RCU read locking, it
2481 * doesn't require the whole traversal to be contained in a single critical
2482 * section. This function will return the correct next descendant as long
2483 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002484 */
Tejun Heo492eb212013-08-08 20:11:25 -04002485struct cgroup_subsys_state *
2486css_next_descendant_pre(struct cgroup_subsys_state *pos,
2487 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002488{
Tejun Heo492eb212013-08-08 20:11:25 -04002489 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002490
Tejun Heoace2bee2014-02-11 11:52:47 -05002491 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002492
Tejun Heobd8815a2013-08-08 20:11:27 -04002493 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002494 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002495 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002496
2497 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002498 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002499 if (next)
2500 return next;
2501
2502 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002503 while (pos != root) {
2504 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002505 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002506 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002507 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002508 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002509
2510 return NULL;
2511}
Tejun Heo492eb212013-08-08 20:11:25 -04002512EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002513
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002514/**
Tejun Heo492eb212013-08-08 20:11:25 -04002515 * css_rightmost_descendant - return the rightmost descendant of a css
2516 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002517 *
Tejun Heo492eb212013-08-08 20:11:25 -04002518 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2519 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002520 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002521 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002522 * While this function requires cgroup_mutex or RCU read locking, it
2523 * doesn't require the whole traversal to be contained in a single critical
2524 * section. This function will return the correct rightmost descendant as
2525 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002526 */
Tejun Heo492eb212013-08-08 20:11:25 -04002527struct cgroup_subsys_state *
2528css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002529{
Tejun Heo492eb212013-08-08 20:11:25 -04002530 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002531
Tejun Heoace2bee2014-02-11 11:52:47 -05002532 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002533
2534 do {
2535 last = pos;
2536 /* ->prev isn't RCU safe, walk ->next till the end */
2537 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002538 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002539 pos = tmp;
2540 } while (pos);
2541
2542 return last;
2543}
Tejun Heo492eb212013-08-08 20:11:25 -04002544EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002545
Tejun Heo492eb212013-08-08 20:11:25 -04002546static struct cgroup_subsys_state *
2547css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002548{
Tejun Heo492eb212013-08-08 20:11:25 -04002549 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002550
2551 do {
2552 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04002553 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002554 } while (pos);
2555
2556 return last;
2557}
2558
2559/**
Tejun Heo492eb212013-08-08 20:11:25 -04002560 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002561 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002562 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002563 *
Tejun Heo492eb212013-08-08 20:11:25 -04002564 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002565 * to visit for post-order traversal of @root's descendants. @root is
2566 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002567 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002568 * While this function requires cgroup_mutex or RCU read locking, it
2569 * doesn't require the whole traversal to be contained in a single critical
2570 * section. This function will return the correct next descendant as long
2571 * as both @pos and @cgroup are accessible and @pos is a descendant of
2572 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002573 */
Tejun Heo492eb212013-08-08 20:11:25 -04002574struct cgroup_subsys_state *
2575css_next_descendant_post(struct cgroup_subsys_state *pos,
2576 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002577{
Tejun Heo492eb212013-08-08 20:11:25 -04002578 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002579
Tejun Heoace2bee2014-02-11 11:52:47 -05002580 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002581
Tejun Heo58b79a92013-09-06 15:31:08 -04002582 /* if first iteration, visit leftmost descendant which may be @root */
2583 if (!pos)
2584 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002585
Tejun Heobd8815a2013-08-08 20:11:27 -04002586 /* if we visited @root, we're done */
2587 if (pos == root)
2588 return NULL;
2589
Tejun Heo574bd9f2012-11-09 09:12:29 -08002590 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04002591 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002592 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04002593 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002594
2595 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04002596 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002597}
Tejun Heo492eb212013-08-08 20:11:25 -04002598EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002599
Tejun Heo0942eee2013-08-08 20:11:26 -04002600/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002601 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04002602 * @it: the iterator to advance
2603 *
2604 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04002605 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002606static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04002607{
2608 struct list_head *l = it->cset_link;
2609 struct cgrp_cset_link *link;
2610 struct css_set *cset;
2611
2612 /* Advance to the next non-empty css_set */
2613 do {
2614 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04002615 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04002616 it->cset_link = NULL;
2617 return;
2618 }
2619 link = list_entry(l, struct cgrp_cset_link, cset_link);
2620 cset = link->cset;
2621 } while (list_empty(&cset->tasks));
2622 it->cset_link = l;
2623 it->task = cset->tasks.next;
2624}
2625
Tejun Heo0942eee2013-08-08 20:11:26 -04002626/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002627 * css_task_iter_start - initiate task iteration
2628 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04002629 * @it: the task iterator to use
2630 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002631 * Initiate iteration through the tasks of @css. The caller can call
2632 * css_task_iter_next() to walk through the tasks until the function
2633 * returns NULL. On completion of iteration, css_task_iter_end() must be
2634 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04002635 *
2636 * Note that this function acquires a lock which is released when the
2637 * iteration finishes. The caller can't sleep while iteration is in
2638 * progress.
2639 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002640void css_task_iter_start(struct cgroup_subsys_state *css,
2641 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05002642 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07002643{
Tejun Heo56fde9e2014-02-13 06:58:38 -05002644 /* no one should try to iterate before mounting cgroups */
2645 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002646
Tejun Heo96d365e2014-02-13 06:58:40 -05002647 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002648
Tejun Heo72ec7022013-08-08 20:11:26 -04002649 it->origin_css = css;
2650 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002651
Tejun Heo72ec7022013-08-08 20:11:26 -04002652 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002653}
2654
Tejun Heo0942eee2013-08-08 20:11:26 -04002655/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002656 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04002657 * @it: the task iterator being iterated
2658 *
2659 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04002660 * initialized via css_task_iter_start(). Returns NULL when the iteration
2661 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04002662 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002663struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002664{
2665 struct task_struct *res;
2666 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07002667 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002668
2669 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07002670 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07002671 return NULL;
2672 res = list_entry(l, struct task_struct, cg_list);
2673 /* Advance iterator to find next entry */
2674 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002675 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
2676 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04002677 /*
2678 * We reached the end of this task list - move on to the
2679 * next cgrp_cset_link.
2680 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002681 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002682 } else {
2683 it->task = l;
2684 }
2685 return res;
2686}
2687
Tejun Heo0942eee2013-08-08 20:11:26 -04002688/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002689 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04002690 * @it: the task iterator to finish
2691 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002692 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04002693 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002694void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05002695 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07002696{
Tejun Heo96d365e2014-02-13 06:58:40 -05002697 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07002698}
2699
Cliff Wickman31a7df02008-02-07 00:14:42 -08002700static inline int started_after_time(struct task_struct *t1,
2701 struct timespec *time,
2702 struct task_struct *t2)
2703{
2704 int start_diff = timespec_compare(&t1->start_time, time);
2705 if (start_diff > 0) {
2706 return 1;
2707 } else if (start_diff < 0) {
2708 return 0;
2709 } else {
2710 /*
2711 * Arbitrarily, if two processes started at the same
2712 * time, we'll say that the lower pointer value
2713 * started first. Note that t2 may have exited by now
2714 * so this may not be a valid pointer any longer, but
2715 * that's fine - it still serves to distinguish
2716 * between two tasks started (effectively) simultaneously.
2717 */
2718 return t1 > t2;
2719 }
2720}
2721
2722/*
2723 * This function is a callback from heap_insert() and is used to order
2724 * the heap.
2725 * In this case we order the heap in descending task start time.
2726 */
2727static inline int started_after(void *p1, void *p2)
2728{
2729 struct task_struct *t1 = p1;
2730 struct task_struct *t2 = p2;
2731 return started_after_time(t1, &t2->start_time, t2);
2732}
2733
2734/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002735 * css_scan_tasks - iterate though all the tasks in a css
2736 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04002737 * @test: optional test callback
2738 * @process: process callback
2739 * @data: data passed to @test and @process
2740 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08002741 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002742 * Iterate through all the tasks in @css, calling @test for each, and if it
2743 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08002744 *
Tejun Heoe5358372013-08-08 20:11:26 -04002745 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04002746 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heo96d365e2014-02-13 06:58:40 -05002747 * lock css_set_rwsem for the call to @process.
Tejun Heoe5358372013-08-08 20:11:26 -04002748 *
2749 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04002750 * of @css for the duration of this call. This function may or may not
2751 * call @process for tasks that exit or move to a different css during the
2752 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04002753 *
2754 * Note that @test may be called with locks held, and may in some
2755 * situations be called multiple times for the same task, so it should be
2756 * cheap.
2757 *
2758 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
2759 * heap operations (and its "gt" member will be overwritten), else a
2760 * temporary heap will be used (allocation of which may cause this function
2761 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08002762 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002763int css_scan_tasks(struct cgroup_subsys_state *css,
2764 bool (*test)(struct task_struct *, void *),
2765 void (*process)(struct task_struct *, void *),
2766 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002767{
2768 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04002769 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002770 struct task_struct *p, *dropped;
2771 /* Never dereference latest_task, since it's not refcounted */
2772 struct task_struct *latest_task = NULL;
2773 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002774 struct timespec latest_time = { 0, 0 };
2775
Tejun Heoe5358372013-08-08 20:11:26 -04002776 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08002777 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002778 heap->gt = &started_after;
2779 } else {
2780 /* We need to allocate our own heap memory */
2781 heap = &tmp_heap;
2782 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2783 if (retval)
2784 /* cannot allocate the heap */
2785 return retval;
2786 }
2787
2788 again:
2789 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04002790 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04002791 * which are of interest, and invoking @process callback on the
2792 * ones which need an update. Since we don't want to hold any
2793 * locks during the task updates, gather tasks to be processed in a
2794 * heap structure. The heap is sorted by descending task start
2795 * time. If the statically-sized heap fills up, we overflow tasks
2796 * that started later, and in future iterations only consider tasks
2797 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08002798 * guarantees forward progress and that we don't miss any tasks.
2799 */
2800 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04002801 css_task_iter_start(css, &it);
2802 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08002803 /*
2804 * Only affect tasks that qualify per the caller's callback,
2805 * if he provided one
2806 */
Tejun Heoe5358372013-08-08 20:11:26 -04002807 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002808 continue;
2809 /*
2810 * Only process tasks that started after the last task
2811 * we processed
2812 */
2813 if (!started_after_time(p, &latest_time, latest_task))
2814 continue;
2815 dropped = heap_insert(heap, p);
2816 if (dropped == NULL) {
2817 /*
2818 * The new task was inserted; the heap wasn't
2819 * previously full
2820 */
2821 get_task_struct(p);
2822 } else if (dropped != p) {
2823 /*
2824 * The new task was inserted, and pushed out a
2825 * different task
2826 */
2827 get_task_struct(p);
2828 put_task_struct(dropped);
2829 }
2830 /*
2831 * Else the new task was newer than anything already in
2832 * the heap and wasn't inserted
2833 */
2834 }
Tejun Heo72ec7022013-08-08 20:11:26 -04002835 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002836
2837 if (heap->size) {
2838 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002839 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002840 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002841 latest_time = q->start_time;
2842 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002843 }
2844 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04002845 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07002846 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002847 }
2848 /*
2849 * If we had to process any tasks at all, scan again
2850 * in case some of them were in the middle of forking
2851 * children that didn't get processed.
2852 * Not the most efficient way to do it, but it avoids
2853 * having to take callback_mutex in the fork path
2854 */
2855 goto again;
2856 }
2857 if (heap == &tmp_heap)
2858 heap_free(&tmp_heap);
2859 return 0;
2860}
2861
Tejun Heo8cc99342013-04-07 09:29:50 -07002862/**
2863 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
2864 * @to: cgroup to which the tasks will be moved
2865 * @from: cgroup in which the tasks currently reside
2866 */
2867int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
2868{
Tejun Heoe406d1c2014-02-13 06:58:39 -05002869 struct css_task_iter it;
2870 struct task_struct *task;
2871 int ret = 0;
2872
2873 do {
2874 css_task_iter_start(&from->dummy_css, &it);
2875 task = css_task_iter_next(&it);
2876 if (task)
2877 get_task_struct(task);
2878 css_task_iter_end(&it);
2879
2880 if (task) {
2881 mutex_lock(&cgroup_mutex);
2882 ret = cgroup_attach_task(to, task, false);
2883 mutex_unlock(&cgroup_mutex);
2884 put_task_struct(task);
2885 }
2886 } while (task && !ret);
2887
2888 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07002889}
2890
Paul Menage817929e2007-10-18 23:39:36 -07002891/*
Ben Blum102a7752009-09-23 15:56:26 -07002892 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002893 *
2894 * Reading this file can return large amounts of data if a cgroup has
2895 * *lots* of attached tasks. So it may need several calls to read(),
2896 * but we cannot guarantee that the information we produce is correct
2897 * unless we produce it entirely atomically.
2898 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002899 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002900
Li Zefan24528252012-01-20 11:58:43 +08002901/* which pidlist file are we talking about? */
2902enum cgroup_filetype {
2903 CGROUP_FILE_PROCS,
2904 CGROUP_FILE_TASKS,
2905};
2906
2907/*
2908 * A pidlist is a list of pids that virtually represents the contents of one
2909 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
2910 * a pair (one each for procs, tasks) for each pid namespace that's relevant
2911 * to the cgroup.
2912 */
2913struct cgroup_pidlist {
2914 /*
2915 * used to find which pidlist is wanted. doesn't change as long as
2916 * this particular list stays in the list.
2917 */
2918 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
2919 /* array of xids */
2920 pid_t *list;
2921 /* how many elements the above list has */
2922 int length;
Li Zefan24528252012-01-20 11:58:43 +08002923 /* each of these stored in a list by its cgroup */
2924 struct list_head links;
2925 /* pointer to the cgroup we belong to, for list removal purposes */
2926 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05002927 /* for delayed destruction */
2928 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08002929};
2930
Paul Menagebbcb81d2007-10-18 23:39:32 -07002931/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002932 * The following two functions "fix" the issue where there are more pids
2933 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2934 * TODO: replace with a kernel-wide solution to this problem
2935 */
2936#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2937static void *pidlist_allocate(int count)
2938{
2939 if (PIDLIST_TOO_LARGE(count))
2940 return vmalloc(count * sizeof(pid_t));
2941 else
2942 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2943}
Tejun Heob1a21362013-11-29 10:42:58 -05002944
Ben Blumd1d9fd32009-09-23 15:56:28 -07002945static void pidlist_free(void *p)
2946{
2947 if (is_vmalloc_addr(p))
2948 vfree(p);
2949 else
2950 kfree(p);
2951}
Ben Blumd1d9fd32009-09-23 15:56:28 -07002952
2953/*
Tejun Heob1a21362013-11-29 10:42:58 -05002954 * Used to destroy all pidlists lingering waiting for destroy timer. None
2955 * should be left afterwards.
2956 */
2957static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
2958{
2959 struct cgroup_pidlist *l, *tmp_l;
2960
2961 mutex_lock(&cgrp->pidlist_mutex);
2962 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
2963 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
2964 mutex_unlock(&cgrp->pidlist_mutex);
2965
2966 flush_workqueue(cgroup_pidlist_destroy_wq);
2967 BUG_ON(!list_empty(&cgrp->pidlists));
2968}
2969
2970static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
2971{
2972 struct delayed_work *dwork = to_delayed_work(work);
2973 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
2974 destroy_dwork);
2975 struct cgroup_pidlist *tofree = NULL;
2976
2977 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05002978
2979 /*
Tejun Heo04502362013-11-29 10:42:59 -05002980 * Destroy iff we didn't get queued again. The state won't change
2981 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05002982 */
Tejun Heo04502362013-11-29 10:42:59 -05002983 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05002984 list_del(&l->links);
2985 pidlist_free(l->list);
2986 put_pid_ns(l->key.ns);
2987 tofree = l;
2988 }
2989
Tejun Heob1a21362013-11-29 10:42:58 -05002990 mutex_unlock(&l->owner->pidlist_mutex);
2991 kfree(tofree);
2992}
2993
2994/*
Ben Blum102a7752009-09-23 15:56:26 -07002995 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07002996 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002997 */
Li Zefan6ee211a2013-03-12 15:36:00 -07002998static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002999{
Ben Blum102a7752009-09-23 15:56:26 -07003000 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003001
3002 /*
3003 * we presume the 0th element is unique, so i starts at 1. trivial
3004 * edge cases first; no work needs to be done for either
3005 */
3006 if (length == 0 || length == 1)
3007 return length;
3008 /* src and dest walk down the list; dest counts unique elements */
3009 for (src = 1; src < length; src++) {
3010 /* find next unique element */
3011 while (list[src] == list[src-1]) {
3012 src++;
3013 if (src == length)
3014 goto after;
3015 }
3016 /* dest always points to where the next unique element goes */
3017 list[dest] = list[src];
3018 dest++;
3019 }
3020after:
Ben Blum102a7752009-09-23 15:56:26 -07003021 return dest;
3022}
3023
Tejun Heoafb2bc12013-11-29 10:42:59 -05003024/*
3025 * The two pid files - task and cgroup.procs - guaranteed that the result
3026 * is sorted, which forced this whole pidlist fiasco. As pid order is
3027 * different per namespace, each namespace needs differently sorted list,
3028 * making it impossible to use, for example, single rbtree of member tasks
3029 * sorted by task pointer. As pidlists can be fairly large, allocating one
3030 * per open file is dangerous, so cgroup had to implement shared pool of
3031 * pidlists keyed by cgroup and namespace.
3032 *
3033 * All this extra complexity was caused by the original implementation
3034 * committing to an entirely unnecessary property. In the long term, we
3035 * want to do away with it. Explicitly scramble sort order if
3036 * sane_behavior so that no such expectation exists in the new interface.
3037 *
3038 * Scrambling is done by swapping every two consecutive bits, which is
3039 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3040 */
3041static pid_t pid_fry(pid_t pid)
3042{
3043 unsigned a = pid & 0x55555555;
3044 unsigned b = pid & 0xAAAAAAAA;
3045
3046 return (a << 1) | (b >> 1);
3047}
3048
3049static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3050{
3051 if (cgroup_sane_behavior(cgrp))
3052 return pid_fry(pid);
3053 else
3054 return pid;
3055}
3056
Ben Blum102a7752009-09-23 15:56:26 -07003057static int cmppid(const void *a, const void *b)
3058{
3059 return *(pid_t *)a - *(pid_t *)b;
3060}
3061
Tejun Heoafb2bc12013-11-29 10:42:59 -05003062static int fried_cmppid(const void *a, const void *b)
3063{
3064 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3065}
3066
Ben Blum72a8cb32009-09-23 15:56:27 -07003067static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3068 enum cgroup_filetype type)
3069{
3070 struct cgroup_pidlist *l;
3071 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003072 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003073
Tejun Heoe6b81712013-11-29 10:42:59 -05003074 lockdep_assert_held(&cgrp->pidlist_mutex);
3075
3076 list_for_each_entry(l, &cgrp->pidlists, links)
3077 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003078 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003079 return NULL;
3080}
3081
Ben Blum72a8cb32009-09-23 15:56:27 -07003082/*
3083 * find the appropriate pidlist for our purpose (given procs vs tasks)
3084 * returns with the lock on that pidlist already held, and takes care
3085 * of the use count, or returns NULL with no locks held if we're out of
3086 * memory.
3087 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003088static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3089 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003090{
3091 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003092
Tejun Heoe6b81712013-11-29 10:42:59 -05003093 lockdep_assert_held(&cgrp->pidlist_mutex);
3094
3095 l = cgroup_pidlist_find(cgrp, type);
3096 if (l)
3097 return l;
3098
Ben Blum72a8cb32009-09-23 15:56:27 -07003099 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003100 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003101 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003102 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003103
Tejun Heob1a21362013-11-29 10:42:58 -05003104 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003105 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003106 /* don't need task_nsproxy() if we're looking at ourself */
3107 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003108 l->owner = cgrp;
3109 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003110 return l;
3111}
3112
3113/*
Ben Blum102a7752009-09-23 15:56:26 -07003114 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3115 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003116static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3117 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003118{
3119 pid_t *array;
3120 int length;
3121 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003122 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003123 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003124 struct cgroup_pidlist *l;
3125
Tejun Heo4bac00d2013-11-29 10:42:59 -05003126 lockdep_assert_held(&cgrp->pidlist_mutex);
3127
Ben Blum102a7752009-09-23 15:56:26 -07003128 /*
3129 * If cgroup gets more users after we read count, we won't have
3130 * enough space - tough. This race is indistinguishable to the
3131 * caller from the case that the additional cgroup users didn't
3132 * show up until sometime later on.
3133 */
3134 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003135 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003136 if (!array)
3137 return -ENOMEM;
3138 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003139 css_task_iter_start(&cgrp->dummy_css, &it);
3140 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003141 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003142 break;
Ben Blum102a7752009-09-23 15:56:26 -07003143 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003144 if (type == CGROUP_FILE_PROCS)
3145 pid = task_tgid_vnr(tsk);
3146 else
3147 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003148 if (pid > 0) /* make sure to only use valid results */
3149 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003150 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003151 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003152 length = n;
3153 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003154 if (cgroup_sane_behavior(cgrp))
3155 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3156 else
3157 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003158 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003159 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003160
Tejun Heoe6b81712013-11-29 10:42:59 -05003161 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003162 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003163 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003164 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003165 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003166 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003167
3168 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003169 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003170 l->list = array;
3171 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003172 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003173 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003174}
3175
Balbir Singh846c7bb2007-10-18 23:39:44 -07003176/**
Li Zefana043e3b2008-02-23 15:24:09 -08003177 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003178 * @stats: cgroupstats to fill information into
3179 * @dentry: A dentry entry belonging to the cgroup for which stats have
3180 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003181 *
3182 * Build and fill cgroupstats so that taskstats can export it to user
3183 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003184 */
3185int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3186{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003187 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003188 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003189 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003190 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003191
Tejun Heo2bd59d42014-02-11 11:52:49 -05003192 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3193 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3194 kernfs_type(kn) != KERNFS_DIR)
3195 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003196
Tejun Heo2bd59d42014-02-11 11:52:49 -05003197 /*
3198 * We aren't being called from kernfs and there's no guarantee on
3199 * @kn->priv's validity. For this and css_tryget_from_dir(),
3200 * @kn->priv is RCU safe. Let's do the RCU dancing.
3201 */
3202 rcu_read_lock();
3203 cgrp = rcu_dereference(kn->priv);
3204 if (!cgrp) {
3205 rcu_read_unlock();
3206 return -ENOENT;
3207 }
Balbir Singh846c7bb2007-10-18 23:39:44 -07003208
Tejun Heo72ec7022013-08-08 20:11:26 -04003209 css_task_iter_start(&cgrp->dummy_css, &it);
3210 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003211 switch (tsk->state) {
3212 case TASK_RUNNING:
3213 stats->nr_running++;
3214 break;
3215 case TASK_INTERRUPTIBLE:
3216 stats->nr_sleeping++;
3217 break;
3218 case TASK_UNINTERRUPTIBLE:
3219 stats->nr_uninterruptible++;
3220 break;
3221 case TASK_STOPPED:
3222 stats->nr_stopped++;
3223 break;
3224 default:
3225 if (delayacct_is_task_waiting_on_io(tsk))
3226 stats->nr_io_wait++;
3227 break;
3228 }
3229 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003230 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003231
Tejun Heo2bd59d42014-02-11 11:52:49 -05003232 rcu_read_unlock();
3233 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003234}
3235
Paul Menage8f3ff202009-09-23 15:56:25 -07003236
Paul Menagecc31edc2008-10-18 20:28:04 -07003237/*
Ben Blum102a7752009-09-23 15:56:26 -07003238 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003239 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003240 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003241 */
3242
Ben Blum102a7752009-09-23 15:56:26 -07003243static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003244{
3245 /*
3246 * Initially we receive a position value that corresponds to
3247 * one more than the last pid shown (or 0 on the first call or
3248 * after a seek to the start). Use a binary-search to find the
3249 * next pid to display, if any
3250 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003251 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003252 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003253 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003254 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003255 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003256 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003257
Tejun Heo4bac00d2013-11-29 10:42:59 -05003258 mutex_lock(&cgrp->pidlist_mutex);
3259
3260 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003261 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003262 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003263 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003264 * could already have been destroyed.
3265 */
Tejun Heo5d224442013-12-05 12:28:04 -05003266 if (of->priv)
3267 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003268
3269 /*
3270 * Either this is the first start() after open or the matching
3271 * pidlist has been destroyed inbetween. Create a new one.
3272 */
Tejun Heo5d224442013-12-05 12:28:04 -05003273 if (!of->priv) {
3274 ret = pidlist_array_load(cgrp, type,
3275 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003276 if (ret)
3277 return ERR_PTR(ret);
3278 }
Tejun Heo5d224442013-12-05 12:28:04 -05003279 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003280
Paul Menagecc31edc2008-10-18 20:28:04 -07003281 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003282 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003283
Paul Menagecc31edc2008-10-18 20:28:04 -07003284 while (index < end) {
3285 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003286 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003287 index = mid;
3288 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003289 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003290 index = mid + 1;
3291 else
3292 end = mid;
3293 }
3294 }
3295 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003296 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003297 return NULL;
3298 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003299 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003300 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003301 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003302}
3303
Ben Blum102a7752009-09-23 15:56:26 -07003304static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003305{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003306 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003307 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003308
Tejun Heo5d224442013-12-05 12:28:04 -05003309 if (l)
3310 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003311 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003312 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003313}
3314
Ben Blum102a7752009-09-23 15:56:26 -07003315static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003316{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003317 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003318 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003319 pid_t *p = v;
3320 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003321 /*
3322 * Advance to the next pid in the array. If this goes off the
3323 * end, we're done
3324 */
3325 p++;
3326 if (p >= end) {
3327 return NULL;
3328 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003329 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003330 return p;
3331 }
3332}
3333
Ben Blum102a7752009-09-23 15:56:26 -07003334static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003335{
3336 return seq_printf(s, "%d\n", *(int *)v);
3337}
3338
Ben Blum102a7752009-09-23 15:56:26 -07003339/*
3340 * seq_operations functions for iterating on pidlists through seq_file -
3341 * independent of whether it's tasks or procs
3342 */
3343static const struct seq_operations cgroup_pidlist_seq_operations = {
3344 .start = cgroup_pidlist_start,
3345 .stop = cgroup_pidlist_stop,
3346 .next = cgroup_pidlist_next,
3347 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003348};
3349
Tejun Heo182446d2013-08-08 20:11:24 -04003350static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3351 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003352{
Tejun Heo182446d2013-08-08 20:11:24 -04003353 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003354}
3355
Tejun Heo182446d2013-08-08 20:11:24 -04003356static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3357 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003358{
Tejun Heo182446d2013-08-08 20:11:24 -04003359 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003360 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003361 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003362 else
Tejun Heo182446d2013-08-08 20:11:24 -04003363 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003364 return 0;
3365}
3366
Tejun Heo182446d2013-08-08 20:11:24 -04003367static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3368 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003369{
Tejun Heo182446d2013-08-08 20:11:24 -04003370 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003371}
3372
Tejun Heo182446d2013-08-08 20:11:24 -04003373static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3374 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003375{
3376 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003377 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003378 else
Tejun Heo182446d2013-08-08 20:11:24 -04003379 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003380 return 0;
3381}
3382
Tejun Heod5c56ce2013-06-03 19:14:34 -07003383static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003384 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003385 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003386 .seq_start = cgroup_pidlist_start,
3387 .seq_next = cgroup_pidlist_next,
3388 .seq_stop = cgroup_pidlist_stop,
3389 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003390 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003391 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003392 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003393 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003394 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003395 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003396 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003397 .read_u64 = cgroup_clone_children_read,
3398 .write_u64 = cgroup_clone_children_write,
3399 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003400 {
Tejun Heo873fe092013-04-14 20:15:26 -07003401 .name = "cgroup.sane_behavior",
3402 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003403 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003404 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003405
3406 /*
3407 * Historical crazy stuff. These don't have "cgroup." prefix and
3408 * don't exist if sane_behavior. If you're depending on these, be
3409 * prepared to be burned.
3410 */
3411 {
3412 .name = "tasks",
3413 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003414 .seq_start = cgroup_pidlist_start,
3415 .seq_next = cgroup_pidlist_next,
3416 .seq_stop = cgroup_pidlist_stop,
3417 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003418 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003419 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003420 .mode = S_IRUGO | S_IWUSR,
3421 },
3422 {
3423 .name = "notify_on_release",
3424 .flags = CFTYPE_INSANE,
3425 .read_u64 = cgroup_read_notify_on_release,
3426 .write_u64 = cgroup_write_notify_on_release,
3427 },
Tejun Heo873fe092013-04-14 20:15:26 -07003428 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003429 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003430 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003431 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003432 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003433 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003434 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003435 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003436};
3437
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003438/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003439 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003440 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003441 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003442 *
3443 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003444 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003445static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003446{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003447 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003448 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003449
Tejun Heo8e3f6542012-04-01 12:09:55 -07003450 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003451 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05003452 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07003453
3454 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003455 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003456
Tejun Heo0adb0702014-02-12 09:29:48 -05003457 list_for_each_entry(cfts, &ss->cfts, node) {
3458 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003459 if (ret < 0)
3460 goto err;
3461 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003462 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003463 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003464err:
3465 cgroup_clear_dir(cgrp, subsys_mask);
3466 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003467}
3468
Tejun Heo0c21ead2013-08-13 20:22:51 -04003469/*
3470 * css destruction is four-stage process.
3471 *
3472 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3473 * Implemented in kill_css().
3474 *
3475 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3476 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3477 * by invoking offline_css(). After offlining, the base ref is put.
3478 * Implemented in css_killed_work_fn().
3479 *
3480 * 3. When the percpu_ref reaches zero, the only possible remaining
3481 * accessors are inside RCU read sections. css_release() schedules the
3482 * RCU callback.
3483 *
3484 * 4. After the grace period, the css can be freed. Implemented in
3485 * css_free_work_fn().
3486 *
3487 * It is actually hairier because both step 2 and 4 require process context
3488 * and thus involve punting to css->destroy_work adding two additional
3489 * steps to the already complex sequence.
3490 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003491static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003492{
3493 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003494 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003495 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003496
Tejun Heo0ae78e02013-08-13 11:01:54 -04003497 if (css->parent)
3498 css_put(css->parent);
3499
Tejun Heo0c21ead2013-08-13 20:22:51 -04003500 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003501 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003502}
3503
3504static void css_free_rcu_fn(struct rcu_head *rcu_head)
3505{
3506 struct cgroup_subsys_state *css =
3507 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3508
Tejun Heo0c21ead2013-08-13 20:22:51 -04003509 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003510 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003511}
3512
Tejun Heod3daf282013-06-13 19:39:16 -07003513static void css_release(struct percpu_ref *ref)
3514{
3515 struct cgroup_subsys_state *css =
3516 container_of(ref, struct cgroup_subsys_state, refcnt);
3517
Tejun Heoaec25022014-02-08 10:36:58 -05003518 rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003519 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003520}
3521
Tejun Heo623f9262013-08-13 11:01:55 -04003522static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3523 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003524{
Paul Menagebd89aab2007-10-18 23:40:44 -07003525 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003526 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003527 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003528
Tejun Heo0ae78e02013-08-13 11:01:54 -04003529 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003530 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003531 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003532 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003533
Tejun Heoca8bdca2013-08-26 18:40:56 -04003534 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003535}
3536
Li Zefan2a4ac632013-07-31 16:16:40 +08003537/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04003538static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003539{
Tejun Heo623f9262013-08-13 11:01:55 -04003540 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08003541 int ret = 0;
3542
Tejun Heoace2bee2014-02-11 11:52:47 -05003543 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003544 lockdep_assert_held(&cgroup_mutex);
3545
Tejun Heo92fb9742012-11-19 08:13:38 -08003546 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04003547 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003548 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04003549 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04003550 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05003551 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003552 }
Tejun Heob1929db2012-11-19 08:13:38 -08003553 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003554}
3555
Li Zefan2a4ac632013-07-31 16:16:40 +08003556/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04003557static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003558{
Tejun Heo623f9262013-08-13 11:01:55 -04003559 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003560
Tejun Heoace2bee2014-02-11 11:52:47 -05003561 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003562 lockdep_assert_held(&cgroup_mutex);
3563
3564 if (!(css->flags & CSS_ONLINE))
3565 return;
3566
Li Zefand7eeac12013-03-12 15:35:59 -07003567 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04003568 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003569
Tejun Heoeb954192013-08-08 20:11:23 -04003570 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04003571 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05003572 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003573}
3574
Tejun Heoc81c925a2013-12-06 15:11:56 -05003575/**
3576 * create_css - create a cgroup_subsys_state
3577 * @cgrp: the cgroup new css will be associated with
3578 * @ss: the subsys of new css
3579 *
3580 * Create a new css associated with @cgrp - @ss pair. On success, the new
3581 * css is online and installed in @cgrp with all interface files created.
3582 * Returns 0 on success, -errno on failure.
3583 */
3584static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
3585{
3586 struct cgroup *parent = cgrp->parent;
3587 struct cgroup_subsys_state *css;
3588 int err;
3589
Tejun Heoc81c925a2013-12-06 15:11:56 -05003590 lockdep_assert_held(&cgroup_mutex);
3591
3592 css = ss->css_alloc(cgroup_css(parent, ss));
3593 if (IS_ERR(css))
3594 return PTR_ERR(css);
3595
3596 err = percpu_ref_init(&css->refcnt, css_release);
3597 if (err)
3598 goto err_free;
3599
3600 init_css(css, ss, cgrp);
3601
Tejun Heoaec25022014-02-08 10:36:58 -05003602 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003603 if (err)
3604 goto err_free;
3605
3606 err = online_css(css);
3607 if (err)
3608 goto err_free;
3609
Tejun Heo59f52962014-02-11 11:52:49 -05003610 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003611 css_get(css->parent);
3612
3613 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
3614 parent->parent) {
3615 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",
3616 current->comm, current->pid, ss->name);
3617 if (!strcmp(ss->name, "memory"))
3618 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
3619 ss->warned_broken_hierarchy = true;
3620 }
3621
3622 return 0;
3623
3624err_free:
3625 percpu_ref_cancel_init(&css->refcnt);
3626 ss->css_free(css);
3627 return err;
3628}
3629
Tejun Heo2bd59d42014-02-11 11:52:49 -05003630/**
Li Zefana043e3b2008-02-23 15:24:09 -08003631 * cgroup_create - create a cgroup
3632 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05003633 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05003634 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07003635 */
Tejun Heoe61734c2014-02-12 09:29:50 -05003636static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003637 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003638{
Paul Menagebd89aab2007-10-18 23:40:44 -07003639 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003640 struct cgroupfs_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05003641 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003642 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003643 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003644
Tejun Heo0a950f62012-11-19 09:02:12 -08003645 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07003646 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3647 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003648 return -ENOMEM;
3649
Tejun Heoace2bee2014-02-11 11:52:47 -05003650 mutex_lock(&cgroup_tree_mutex);
3651
Li Zefan4e96ee82013-07-31 09:50:50 +08003652 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08003653 * Only live parents can have children. Note that the liveliness
3654 * check isn't strictly necessary because cgroup_mkdir() and
3655 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
3656 * anyway so that locking is contained inside cgroup proper and we
3657 * don't get nasty surprises if we ever grow another caller.
3658 */
3659 if (!cgroup_lock_live_group(parent)) {
3660 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05003661 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08003662 }
3663
3664 /*
3665 * Temporarily set the pointer to NULL, so idr_find() won't return
3666 * a half-baked cgroup.
3667 */
3668 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
3669 if (cgrp->id < 0) {
3670 err = -ENOMEM;
3671 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08003672 }
3673
Paul Menagecc31edc2008-10-18 20:28:04 -07003674 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003675
Paul Menagebd89aab2007-10-18 23:40:44 -07003676 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003677 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07003678 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003679
Li Zefanb6abdb02008-03-04 14:28:19 -08003680 if (notify_on_release(parent))
3681 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3682
Tejun Heo2260e7f2012-11-19 08:13:38 -08003683 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
3684 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003685
Tejun Heo2bd59d42014-02-11 11:52:49 -05003686 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05003687 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003688 if (IS_ERR(kn)) {
3689 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003690 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003691 }
3692 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003693
Tejun Heo6f305582014-02-12 09:29:50 -05003694 /*
3695 * This extra ref will be put in cgroup_free_fn() and guarantees
3696 * that @cgrp->kn is always accessible.
3697 */
3698 kernfs_get(kn);
3699
Tejun Heo00356bd2013-06-18 11:14:22 -07003700 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09003701
Tejun Heo4e139af2012-11-19 08:13:36 -08003702 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08003703 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05003704 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05003705 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08003706
Tejun Heo0d802552013-12-06 15:11:56 -05003707 /*
3708 * @cgrp is now fully operational. If something fails after this
3709 * point, it'll be released via the normal destruction path.
3710 */
Li Zefan4e96ee82013-07-31 09:50:50 +08003711 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
3712
Tejun Heo2bb566c2013-08-08 20:11:23 -04003713 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07003714 if (err)
3715 goto err_destroy;
3716
Tejun Heo9d403e92013-12-06 15:11:56 -05003717 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05003718 for_each_subsys(ss, ssid) {
3719 if (root->subsys_mask & (1 << ssid)) {
3720 err = create_css(cgrp, ss);
3721 if (err)
3722 goto err_destroy;
3723 }
Tejun Heoa8638032012-11-09 09:12:29 -08003724 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003725
Tejun Heo2bd59d42014-02-11 11:52:49 -05003726 kernfs_activate(kn);
3727
Paul Menageddbcc7e2007-10-18 23:39:30 -07003728 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003729 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003730
3731 return 0;
3732
Tejun Heo0a950f62012-11-19 09:02:12 -08003733err_free_id:
Li Zefan4e96ee82013-07-31 09:50:50 +08003734 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003735err_unlock:
3736 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003737err_unlock_tree:
3738 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003739 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003740 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08003741
3742err_destroy:
3743 cgroup_destroy_locked(cgrp);
3744 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003745 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08003746 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003747}
3748
Tejun Heo2bd59d42014-02-11 11:52:49 -05003749static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3750 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003751{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003752 struct cgroup *parent = parent_kn->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003753
Tejun Heo2bd59d42014-02-11 11:52:49 -05003754 return cgroup_create(parent, name, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003755}
3756
Tejun Heo223dbc32013-08-13 20:22:50 -04003757/*
3758 * This is called when the refcnt of a css is confirmed to be killed.
3759 * css_tryget() is now guaranteed to fail.
3760 */
3761static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07003762{
Tejun Heo223dbc32013-08-13 20:22:50 -04003763 struct cgroup_subsys_state *css =
3764 container_of(work, struct cgroup_subsys_state, destroy_work);
3765 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07003766
Tejun Heoace2bee2014-02-11 11:52:47 -05003767 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003768 mutex_lock(&cgroup_mutex);
3769
3770 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04003771 * css_tryget() is guaranteed to fail now. Tell subsystems to
3772 * initate destruction.
3773 */
3774 offline_css(css);
3775
3776 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003777 * If @cgrp is marked dead, it's waiting for refs of all css's to
3778 * be disabled before proceeding to the second phase of cgroup
3779 * destruction. If we are the last one, kick it off.
3780 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04003781 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04003782 cgroup_destroy_css_killed(cgrp);
3783
3784 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003785 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04003786
3787 /*
3788 * Put the css refs from kill_css(). Each css holds an extra
3789 * reference to the cgroup's dentry and cgroup removal proceeds
3790 * regardless of css refs. On the last put of each css, whenever
3791 * that may be, the extra dentry ref is put so that dentry
3792 * destruction happens only after all css's are released.
3793 */
3794 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07003795}
3796
Tejun Heo223dbc32013-08-13 20:22:50 -04003797/* css kill confirmation processing requires process context, bounce */
3798static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07003799{
3800 struct cgroup_subsys_state *css =
3801 container_of(ref, struct cgroup_subsys_state, refcnt);
3802
Tejun Heo223dbc32013-08-13 20:22:50 -04003803 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003804 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07003805}
3806
3807/**
Tejun Heoedae0c32013-08-13 20:22:51 -04003808 * kill_css - destroy a css
3809 * @css: css to destroy
3810 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003811 * This function initiates destruction of @css by removing cgroup interface
3812 * files and putting its base reference. ->css_offline() will be invoked
3813 * asynchronously once css_tryget() is guaranteed to fail and when the
3814 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04003815 */
3816static void kill_css(struct cgroup_subsys_state *css)
3817{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003818 /*
3819 * This must happen before css is disassociated with its cgroup.
3820 * See seq_css() for details.
3821 */
Tejun Heoaec25022014-02-08 10:36:58 -05003822 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003823
Tejun Heoedae0c32013-08-13 20:22:51 -04003824 /*
3825 * Killing would put the base ref, but we need to keep it alive
3826 * until after ->css_offline().
3827 */
3828 css_get(css);
3829
3830 /*
3831 * cgroup core guarantees that, by the time ->css_offline() is
3832 * invoked, no new css reference will be given out via
3833 * css_tryget(). We can't simply call percpu_ref_kill() and
3834 * proceed to offlining css's because percpu_ref_kill() doesn't
3835 * guarantee that the ref is seen as killed on all CPUs on return.
3836 *
3837 * Use percpu_ref_kill_and_confirm() to get notifications as each
3838 * css is confirmed to be seen as killed on all CPUs.
3839 */
3840 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003841}
3842
3843/**
3844 * cgroup_destroy_locked - the first stage of cgroup destruction
3845 * @cgrp: cgroup to be destroyed
3846 *
3847 * css's make use of percpu refcnts whose killing latency shouldn't be
3848 * exposed to userland and are RCU protected. Also, cgroup core needs to
3849 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
3850 * invoked. To satisfy all the requirements, destruction is implemented in
3851 * the following two steps.
3852 *
3853 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
3854 * userland visible parts and start killing the percpu refcnts of
3855 * css's. Set up so that the next stage will be kicked off once all
3856 * the percpu refcnts are confirmed to be killed.
3857 *
3858 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
3859 * rest of destruction. Once all cgroup references are gone, the
3860 * cgroup is RCU-freed.
3861 *
3862 * This function implements s1. After this step, @cgrp is gone as far as
3863 * the userland is concerned and a new cgroup with the same name may be
3864 * created. As cgroup doesn't care about the names internally, this
3865 * doesn't cause any problem.
3866 */
Tejun Heo42809dd2012-11-19 08:13:37 -08003867static int cgroup_destroy_locked(struct cgroup *cgrp)
3868 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003869{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003870 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003871 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07003872 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05003873 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003874
Tejun Heoace2bee2014-02-11 11:52:47 -05003875 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003876 lockdep_assert_held(&cgroup_mutex);
3877
Tejun Heoddd69142013-06-12 21:04:54 -07003878 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05003879 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo6f3d828f02013-06-12 21:04:55 -07003880 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07003881 */
Tejun Heo96d365e2014-02-13 06:58:40 -05003882 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003883 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05003884 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07003885 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003886 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08003887
Tejun Heo1a90dd52012-11-05 09:16:59 -08003888 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003889 * Make sure there's no live children. We can't test ->children
3890 * emptiness as dead children linger on it while being destroyed;
3891 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
3892 */
3893 empty = true;
3894 rcu_read_lock();
3895 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
3896 empty = cgroup_is_dead(child);
3897 if (!empty)
3898 break;
3899 }
3900 rcu_read_unlock();
3901 if (!empty)
3902 return -EBUSY;
3903
3904 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04003905 * Initiate massacre of all css's. cgroup_destroy_css_killed()
3906 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05003907 * percpu refs of all css's are confirmed to be killed. This
3908 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08003909 */
Tejun Heo4ac06012014-02-11 11:52:47 -05003910 mutex_unlock(&cgroup_mutex);
Tejun Heo1c6727a2013-12-06 15:11:56 -05003911 for_each_css(css, ssid, cgrp)
3912 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05003913 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07003914
3915 /*
3916 * Mark @cgrp dead. This prevents further task migration and child
3917 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04003918 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07003919 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04003920 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07003921 */
Tejun Heo54766d42013-06-12 21:04:53 -07003922 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08003923
Tejun Heo455050d2013-06-13 19:27:41 -07003924 /* CGRP_DEAD is set, remove from ->release_list for the last time */
3925 raw_spin_lock(&release_list_lock);
3926 if (!list_empty(&cgrp->release_list))
3927 list_del_init(&cgrp->release_list);
3928 raw_spin_unlock(&release_list_lock);
3929
3930 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003931 * If @cgrp has css's attached, the second stage of cgroup
3932 * destruction is kicked off from css_killed_work_fn() after the
3933 * refs of all attached css's are killed. If @cgrp doesn't have
3934 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07003935 */
Tejun Heof20104d2013-08-13 20:22:50 -04003936 if (!cgrp->nr_css)
3937 cgroup_destroy_css_killed(cgrp);
3938
Tejun Heo2bd59d42014-02-11 11:52:49 -05003939 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05003940 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003941
3942 /*
3943 * There are two control paths which try to determine cgroup from
3944 * dentry without going through kernfs - cgroupstats_build() and
3945 * css_tryget_from_dir(). Those are supported by RCU protecting
3946 * clearing of cgrp->kn->priv backpointer, which should happen
3947 * after all files under it have been removed.
3948 */
Tejun Heo6f305582014-02-12 09:29:50 -05003949 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003950 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003951
Tejun Heo4ac06012014-02-11 11:52:47 -05003952 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07003953
Tejun Heoea15f8c2013-06-13 19:27:42 -07003954 return 0;
3955};
3956
Tejun Heod3daf282013-06-13 19:39:16 -07003957/**
Tejun Heof20104d2013-08-13 20:22:50 -04003958 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07003959 * @work: cgroup->destroy_free_work
3960 *
3961 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04003962 * destroyed after all css's are offlined and performs the rest of
3963 * destruction. This is the second step of destruction described in the
3964 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07003965 */
Tejun Heof20104d2013-08-13 20:22:50 -04003966static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07003967{
Tejun Heoea15f8c2013-06-13 19:27:42 -07003968 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07003969
Tejun Heoace2bee2014-02-11 11:52:47 -05003970 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003971 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003972
Paul Menage999cd8a2009-01-07 18:08:36 -08003973 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08003974 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07003975
Tejun Heo59f52962014-02-11 11:52:49 -05003976 cgroup_put(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003977
Paul Menagebd89aab2007-10-18 23:40:44 -07003978 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003979 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003980}
3981
Tejun Heo2bd59d42014-02-11 11:52:49 -05003982static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08003983{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003984 struct cgroup *cgrp = kn->priv;
3985 int ret = 0;
3986
3987 /*
3988 * This is self-destruction but @kn can't be removed while this
3989 * callback is in progress. Let's break active protection. Once
3990 * the protection is broken, @cgrp can be destroyed at any point.
3991 * Pin it so that it stays accessible.
3992 */
3993 cgroup_get(cgrp);
3994 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08003995
Tejun Heoace2bee2014-02-11 11:52:47 -05003996 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003997 mutex_lock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003998
3999 /*
4000 * @cgrp might already have been destroyed while we're trying to
4001 * grab the mutexes.
4002 */
4003 if (!cgroup_is_dead(cgrp))
4004 ret = cgroup_destroy_locked(cgrp);
4005
Tejun Heo42809dd2012-11-19 08:13:37 -08004006 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004007 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004008
Tejun Heo2bd59d42014-02-11 11:52:49 -05004009 kernfs_unbreak_active_protection(kn);
4010 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004011 return ret;
4012}
4013
Tejun Heo2bd59d42014-02-11 11:52:49 -05004014static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4015 .remount_fs = cgroup_remount,
4016 .show_options = cgroup_show_options,
4017 .mkdir = cgroup_mkdir,
4018 .rmdir = cgroup_rmdir,
4019 .rename = cgroup_rename,
4020};
4021
Li Zefan06a11922008-04-29 01:00:07 -07004022static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004023{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004024 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004025
4026 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004027
Tejun Heoace2bee2014-02-11 11:52:47 -05004028 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004029 mutex_lock(&cgroup_mutex);
4030
Tejun Heo0adb0702014-02-12 09:29:48 -05004031 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004032
Paul Menageddbcc7e2007-10-18 23:39:30 -07004033 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004034 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004035 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004036 /* We don't handle early failures gracefully */
4037 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004038 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004039
Li Zefane8d55fd2008-04-29 01:00:13 -07004040 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004041 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004042 * newly registered, all tasks and hence the
4043 * init_css_set is in the subsystem's top cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004044 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004045
4046 need_forkexit_callback |= ss->fork || ss->exit;
4047
Li Zefane8d55fd2008-04-29 01:00:13 -07004048 /* At system boot, before all subsystems have been
4049 * registered, no tasks have been forked, so we don't
4050 * need to invoke fork callbacks here. */
4051 BUG_ON(!list_empty(&init_task.tasks));
4052
Tejun Heoae7f1642013-08-13 20:22:50 -04004053 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004054
Tejun Heo648bb562012-11-19 08:13:36 -08004055 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004056 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004057}
4058
4059/**
Li Zefana043e3b2008-02-23 15:24:09 -08004060 * cgroup_init_early - cgroup initialization at system boot
4061 *
4062 * Initialize cgroups at system boot, and initialize any
4063 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004064 */
4065int __init cgroup_init_early(void)
4066{
Tejun Heo30159ec2013-06-25 11:53:37 -07004067 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004068 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004069
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004070 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004071 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004072 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004073 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004074 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004075 init_cgroup_root(&cgroup_dummy_root);
4076 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004077 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004078
Tejun Heo69d02062013-06-12 21:04:50 -07004079 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004080 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4081 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004082 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004083
Tejun Heo3ed80a62014-02-08 10:36:58 -05004084 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004085 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004086 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4087 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004088 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004089 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4090 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4091
Tejun Heoaec25022014-02-08 10:36:58 -05004092 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004093 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004094
4095 if (ss->early_init)
4096 cgroup_init_subsys(ss);
4097 }
4098 return 0;
4099}
4100
4101/**
Li Zefana043e3b2008-02-23 15:24:09 -08004102 * cgroup_init - cgroup initialization
4103 *
4104 * Register cgroup filesystem and /proc file, and initialize
4105 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004106 */
4107int __init cgroup_init(void)
4108{
Tejun Heo30159ec2013-06-25 11:53:37 -07004109 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004110 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004111 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004112
Tejun Heo2bd59d42014-02-11 11:52:49 -05004113 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Tejun Heo2da440a2014-02-11 11:52:48 -05004114
Tejun Heo3ed80a62014-02-08 10:36:58 -05004115 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004116 if (!ss->early_init)
4117 cgroup_init_subsys(ss);
Tejun Heode00ffa2014-02-11 11:52:48 -05004118
4119 /*
4120 * cftype registration needs kmalloc and can't be done
4121 * during early_init. Register base cftypes separately.
4122 */
4123 if (ss->base_cftypes)
4124 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004125 }
4126
Tejun Heofa3ca072013-04-14 11:36:56 -07004127 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004128 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004129
Tejun Heo82fe9b02013-06-25 11:53:37 -07004130 /* Add init_css_set to the hash table */
4131 key = css_set_hash(init_css_set.subsys);
4132 hash_add(css_set_table, &init_css_set.hlist, key);
4133
Tejun Heofc76df72013-06-25 11:53:37 -07004134 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004135
Li Zefan4e96ee82013-07-31 09:50:50 +08004136 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4137 0, 1, GFP_KERNEL);
4138 BUG_ON(err < 0);
4139
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004140 mutex_unlock(&cgroup_mutex);
4141
Greg KH676db4a2010-08-05 13:53:35 -07004142 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004143 if (!cgroup_kobj)
4144 return -ENOMEM;
Greg KH676db4a2010-08-05 13:53:35 -07004145
4146 err = register_filesystem(&cgroup_fs_type);
4147 if (err < 0) {
4148 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004149 return err;
Greg KH676db4a2010-08-05 13:53:35 -07004150 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004151
Li Zefan46ae2202008-04-29 01:00:08 -07004152 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004153 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004154}
Paul Menageb4f48b62007-10-18 23:39:33 -07004155
Tejun Heoe5fca242013-11-22 17:14:39 -05004156static int __init cgroup_wq_init(void)
4157{
4158 /*
4159 * There isn't much point in executing destruction path in
4160 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004161 *
4162 * XXX: Must be ordered to make sure parent is offlined after
4163 * children. The ordering requirement is for memcg where a
4164 * parent's offline may wait for a child's leading to deadlock. In
4165 * the long term, this should be fixed from memcg side.
Tejun Heoe5fca242013-11-22 17:14:39 -05004166 *
4167 * We would prefer to do this in cgroup_init() above, but that
4168 * is called before init_workqueues(): so leave this until after.
4169 */
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004170 cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0);
Tejun Heoe5fca242013-11-22 17:14:39 -05004171 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004172
4173 /*
4174 * Used to destroy pidlists and separate to serve as flush domain.
4175 * Cap @max_active to 1 too.
4176 */
4177 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4178 0, 1);
4179 BUG_ON(!cgroup_pidlist_destroy_wq);
4180
Tejun Heoe5fca242013-11-22 17:14:39 -05004181 return 0;
4182}
4183core_initcall(cgroup_wq_init);
4184
Paul Menagea4243162007-10-18 23:39:35 -07004185/*
4186 * proc_cgroup_show()
4187 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4188 * - Used for /proc/<pid>/cgroup.
4189 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4190 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004191 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004192 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4193 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4194 * cgroup to top_cgroup.
4195 */
4196
4197/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004198int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004199{
4200 struct pid *pid;
4201 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004202 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004203 int retval;
4204 struct cgroupfs_root *root;
4205
4206 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004207 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004208 if (!buf)
4209 goto out;
4210
4211 retval = -ESRCH;
4212 pid = m->private;
4213 tsk = get_pid_task(pid, PIDTYPE_PID);
4214 if (!tsk)
4215 goto out_free;
4216
4217 retval = 0;
4218
4219 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004220 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004221
Li Zefane5f6a862009-01-07 18:07:41 -08004222 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004223 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004224 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004225 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004226
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004227 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004228 for_each_subsys(ss, ssid)
4229 if (root->subsys_mask & (1 << ssid))
4230 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004231 if (strlen(root->name))
4232 seq_printf(m, "%sname=%s", count ? "," : "",
4233 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004234 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004235 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004236 path = cgroup_path(cgrp, buf, PATH_MAX);
4237 if (!path) {
4238 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004239 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004240 }
4241 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004242 seq_putc(m, '\n');
4243 }
4244
4245out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004246 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004247 mutex_unlock(&cgroup_mutex);
4248 put_task_struct(tsk);
4249out_free:
4250 kfree(buf);
4251out:
4252 return retval;
4253}
4254
Paul Menagea4243162007-10-18 23:39:35 -07004255/* Display information about each subsystem and each hierarchy */
4256static int proc_cgroupstats_show(struct seq_file *m, void *v)
4257{
Tejun Heo30159ec2013-06-25 11:53:37 -07004258 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004259 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004260
Paul Menage8bab8dd2008-04-04 14:29:57 -07004261 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004262 /*
4263 * ideally we don't want subsystems moving around while we do this.
4264 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4265 * subsys/hierarchy state.
4266 */
Paul Menagea4243162007-10-18 23:39:35 -07004267 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004268
4269 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004270 seq_printf(m, "%s\t%d\t%d\t%d\n",
4271 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004272 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004273
Paul Menagea4243162007-10-18 23:39:35 -07004274 mutex_unlock(&cgroup_mutex);
4275 return 0;
4276}
4277
4278static int cgroupstats_open(struct inode *inode, struct file *file)
4279{
Al Viro9dce07f2008-03-29 03:07:28 +00004280 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004281}
4282
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004283static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004284 .open = cgroupstats_open,
4285 .read = seq_read,
4286 .llseek = seq_lseek,
4287 .release = single_release,
4288};
4289
Paul Menageb4f48b62007-10-18 23:39:33 -07004290/**
4291 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004292 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004293 *
4294 * Description: A task inherits its parent's cgroup at fork().
4295 *
4296 * A pointer to the shared css_set was automatically copied in
4297 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004298 * it was not made under the protection of RCU or cgroup_mutex, so
4299 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4300 * have already changed current->cgroups, allowing the previously
4301 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004302 *
4303 * At the point that cgroup_fork() is called, 'current' is the parent
4304 * task, and the passed argument 'child' points to the child task.
4305 */
4306void cgroup_fork(struct task_struct *child)
4307{
Tejun Heo9bb71302012-10-18 17:52:07 -07004308 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004309 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004310 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004311 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004312 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004313}
4314
4315/**
Li Zefana043e3b2008-02-23 15:24:09 -08004316 * cgroup_post_fork - called on a new task after adding it to the task list
4317 * @child: the task in question
4318 *
Tejun Heo5edee612012-10-16 15:03:14 -07004319 * Adds the task to the list running through its css_set if necessary and
4320 * call the subsystem fork() callbacks. Has to be after the task is
4321 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004322 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004323 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004324 */
Paul Menage817929e2007-10-18 23:39:36 -07004325void cgroup_post_fork(struct task_struct *child)
4326{
Tejun Heo30159ec2013-06-25 11:53:37 -07004327 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004328 int i;
4329
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004330 /*
4331 * use_task_css_set_links is set to 1 before we walk the tasklist
4332 * under the tasklist_lock and we read it here after we added the child
4333 * to the tasklist under the tasklist_lock as well. If the child wasn't
4334 * yet in the tasklist when we walked through it from
4335 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4336 * should be visible now due to the paired locking and barriers implied
4337 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4338 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4339 * lock on fork.
4340 */
Paul Menage817929e2007-10-18 23:39:36 -07004341 if (use_task_css_set_links) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004342 down_write(&css_set_rwsem);
Tejun Heod8783832012-10-18 17:40:30 -07004343 task_lock(child);
4344 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004345 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004346 task_unlock(child);
Tejun Heo96d365e2014-02-13 06:58:40 -05004347 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004348 }
Tejun Heo5edee612012-10-16 15:03:14 -07004349
4350 /*
4351 * Call ss->fork(). This must happen after @child is linked on
4352 * css_set; otherwise, @child might change state between ->fork()
4353 * and addition to css_set.
4354 */
4355 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004356 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004357 if (ss->fork)
4358 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004359 }
Paul Menage817929e2007-10-18 23:39:36 -07004360}
Tejun Heo5edee612012-10-16 15:03:14 -07004361
Paul Menage817929e2007-10-18 23:39:36 -07004362/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004363 * cgroup_exit - detach cgroup from exiting task
4364 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004365 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004366 *
4367 * Description: Detach cgroup from @tsk and release it.
4368 *
4369 * Note that cgroups marked notify_on_release force every task in
4370 * them to take the global cgroup_mutex mutex when exiting.
4371 * This could impact scaling on very large systems. Be reluctant to
4372 * use notify_on_release cgroups where very high task exit scaling
4373 * is required on large systems.
4374 *
4375 * the_top_cgroup_hack:
4376 *
4377 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4378 *
4379 * We call cgroup_exit() while the task is still competent to
4380 * handle notify_on_release(), then leave the task attached to the
4381 * root cgroup in each hierarchy for the remainder of its exit.
4382 *
4383 * To do this properly, we would increment the reference count on
4384 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4385 * code we would add a second cgroup function call, to drop that
4386 * reference. This would just create an unnecessary hot spot on
4387 * the top_cgroup reference count, to no avail.
4388 *
4389 * Normally, holding a reference to a cgroup without bumping its
4390 * count is unsafe. The cgroup could go away, or someone could
4391 * attach us to a different cgroup, decrementing the count on
4392 * the first cgroup that we never incremented. But in this case,
4393 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004394 * which wards off any cgroup_attach_task() attempts, or task is a failed
4395 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004396 */
4397void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4398{
Tejun Heo30159ec2013-06-25 11:53:37 -07004399 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004400 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004401 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004402
4403 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004404 * Unlink from the css_set task list if necessary. Optimistically
4405 * check cg_list before taking css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004406 */
4407 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004408 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004409 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004410 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004411 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004412 }
4413
Paul Menageb4f48b62007-10-18 23:39:33 -07004414 /* Reassign the task to the init_css_set. */
4415 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004416 cset = task_css_set(tsk);
4417 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004418
4419 if (run_callbacks && need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004420 /* see cgroup_post_fork() for details */
4421 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004422 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004423 struct cgroup_subsys_state *old_css = cset->subsys[i];
4424 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004425
Tejun Heoeb954192013-08-08 20:11:23 -04004426 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004427 }
4428 }
4429 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004430 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004431
Tejun Heo5abb8852013-06-12 21:04:49 -07004432 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07004433}
Paul Menage697f4162007-10-18 23:39:34 -07004434
Paul Menagebd89aab2007-10-18 23:40:44 -07004435static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004436{
Li Zefanf50daa72013-03-01 15:06:07 +08004437 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004438 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004439 /*
4440 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004441 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004442 * it now
4443 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004444 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004445
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004446 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004447 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004448 list_empty(&cgrp->release_list)) {
4449 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004450 need_schedule_work = 1;
4451 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004452 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004453 if (need_schedule_work)
4454 schedule_work(&release_agent_work);
4455 }
4456}
4457
Paul Menage81a6a5c2007-10-18 23:39:38 -07004458/*
4459 * Notify userspace when a cgroup is released, by running the
4460 * configured release agent with the name of the cgroup (path
4461 * relative to the root of cgroup file system) as the argument.
4462 *
4463 * Most likely, this user command will try to rmdir this cgroup.
4464 *
4465 * This races with the possibility that some other task will be
4466 * attached to this cgroup before it is removed, or that some other
4467 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4468 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4469 * unused, and this cgroup will be reprieved from its death sentence,
4470 * to continue to serve a useful existence. Next time it's released,
4471 * we will get notified again, if it still has 'notify_on_release' set.
4472 *
4473 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4474 * means only wait until the task is successfully execve()'d. The
4475 * separate release agent task is forked by call_usermodehelper(),
4476 * then control in this thread returns here, without waiting for the
4477 * release agent task. We don't bother to wait because the caller of
4478 * this routine has no use for the exit status of the release agent
4479 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004480 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004481static void cgroup_release_agent(struct work_struct *work)
4482{
4483 BUG_ON(work != &release_agent_work);
4484 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004485 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004486 while (!list_empty(&release_list)) {
4487 char *argv[3], *envp[3];
4488 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05004489 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07004490 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004491 struct cgroup,
4492 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004493 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004494 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05004495 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004496 if (!pathbuf)
4497 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05004498 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
4499 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07004500 goto continue_free;
4501 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4502 if (!agentbuf)
4503 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004504
4505 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004506 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05004507 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004508 argv[i] = NULL;
4509
4510 i = 0;
4511 /* minimal command environment */
4512 envp[i++] = "HOME=/";
4513 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4514 envp[i] = NULL;
4515
4516 /* Drop the lock while we invoke the usermode helper,
4517 * since the exec could involve hitting disk and hence
4518 * be a slow process */
4519 mutex_unlock(&cgroup_mutex);
4520 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004521 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004522 continue_free:
4523 kfree(pathbuf);
4524 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004525 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004526 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004527 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004528 mutex_unlock(&cgroup_mutex);
4529}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004530
4531static int __init cgroup_disable(char *str)
4532{
Tejun Heo30159ec2013-06-25 11:53:37 -07004533 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004534 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07004535 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004536
4537 while ((token = strsep(&str, ",")) != NULL) {
4538 if (!*token)
4539 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004540
Tejun Heo3ed80a62014-02-08 10:36:58 -05004541 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004542 if (!strcmp(token, ss->name)) {
4543 ss->disabled = 1;
4544 printk(KERN_INFO "Disabling %s control group"
4545 " subsystem\n", ss->name);
4546 break;
4547 }
4548 }
4549 }
4550 return 1;
4551}
4552__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004553
Tejun Heob77d7b62013-08-13 11:01:54 -04004554/**
Tejun Heo5a17f542014-02-11 11:52:47 -05004555 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04004556 * @dentry: directory dentry of interest
4557 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04004558 *
Tejun Heo5a17f542014-02-11 11:52:47 -05004559 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4560 * to get the corresponding css and return it. If such css doesn't exist
4561 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02004562 */
Tejun Heo5a17f542014-02-11 11:52:47 -05004563struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4564 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004565{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004566 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4567 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004568 struct cgroup *cgrp;
Tejun Heob77d7b62013-08-13 11:01:54 -04004569
Tejun Heo35cf0832013-08-26 18:40:56 -04004570 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004571 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4572 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004573 return ERR_PTR(-EBADF);
4574
Tejun Heo5a17f542014-02-11 11:52:47 -05004575 rcu_read_lock();
4576
Tejun Heo2bd59d42014-02-11 11:52:49 -05004577 /*
4578 * This path doesn't originate from kernfs and @kn could already
4579 * have been or be removed at any point. @kn->priv is RCU
4580 * protected for this access. See destroy_locked() for details.
4581 */
4582 cgrp = rcu_dereference(kn->priv);
4583 if (cgrp)
4584 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05004585
4586 if (!css || !css_tryget(css))
4587 css = ERR_PTR(-ENOENT);
4588
4589 rcu_read_unlock();
4590 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004591}
Stephane Eraniane5d13672011-02-14 11:20:01 +02004592
Li Zefan1cb650b2013-08-19 10:05:24 +08004593/**
4594 * css_from_id - lookup css by id
4595 * @id: the cgroup id
4596 * @ss: cgroup subsys to be looked into
4597 *
4598 * Returns the css if there's valid one with @id, otherwise returns NULL.
4599 * Should be called under rcu_read_lock().
4600 */
4601struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4602{
4603 struct cgroup *cgrp;
4604
Tejun Heoace2bee2014-02-11 11:52:47 -05004605 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08004606
4607 cgrp = idr_find(&ss->root->cgroup_idr, id);
4608 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04004609 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08004610 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004611}
4612
Paul Menagefe693432009-09-23 15:56:20 -07004613#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04004614static struct cgroup_subsys_state *
4615debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07004616{
4617 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4618
4619 if (!css)
4620 return ERR_PTR(-ENOMEM);
4621
4622 return css;
4623}
4624
Tejun Heoeb954192013-08-08 20:11:23 -04004625static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07004626{
Tejun Heoeb954192013-08-08 20:11:23 -04004627 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07004628}
4629
Tejun Heo182446d2013-08-08 20:11:24 -04004630static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
4631 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004632{
Tejun Heo182446d2013-08-08 20:11:24 -04004633 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07004634}
4635
Tejun Heo182446d2013-08-08 20:11:24 -04004636static u64 current_css_set_read(struct cgroup_subsys_state *css,
4637 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004638{
4639 return (u64)(unsigned long)current->cgroups;
4640}
4641
Tejun Heo182446d2013-08-08 20:11:24 -04004642static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08004643 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004644{
4645 u64 count;
4646
4647 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07004648 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07004649 rcu_read_unlock();
4650 return count;
4651}
4652
Tejun Heo2da8ca82013-12-05 12:28:04 -05004653static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004654{
Tejun Heo69d02062013-06-12 21:04:50 -07004655 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07004656 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05004657 char *name_buf;
4658
4659 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
4660 if (!name_buf)
4661 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07004662
Tejun Heo96d365e2014-02-13 06:58:40 -05004663 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07004664 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07004665 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07004666 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004667 struct cgroup *c = link->cgrp;
Tejun Heo59f52962014-02-11 11:52:49 -05004668 const char *name = "?";
Paul Menage7717f7b2009-09-23 15:56:22 -07004669
Tejun Heoe61734c2014-02-12 09:29:50 -05004670 if (c != cgroup_dummy_top) {
4671 cgroup_name(c, name_buf, NAME_MAX + 1);
4672 name = name_buf;
4673 }
Tejun Heo59f52962014-02-11 11:52:49 -05004674
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004675 seq_printf(seq, "Root %d group %s\n",
4676 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07004677 }
4678 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05004679 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05004680 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004681 return 0;
4682}
4683
4684#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05004685static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004686{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004687 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07004688 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07004689
Tejun Heo96d365e2014-02-13 06:58:40 -05004690 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04004691 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004692 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004693 struct task_struct *task;
4694 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07004695 seq_printf(seq, "css_set %p\n", cset);
4696 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004697 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4698 seq_puts(seq, " ...\n");
4699 break;
4700 } else {
4701 seq_printf(seq, " task %d\n",
4702 task_pid_vnr(task));
4703 }
4704 }
4705 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004706 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07004707 return 0;
4708}
4709
Tejun Heo182446d2013-08-08 20:11:24 -04004710static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004711{
Tejun Heo182446d2013-08-08 20:11:24 -04004712 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07004713}
4714
4715static struct cftype debug_files[] = {
4716 {
Paul Menagefe693432009-09-23 15:56:20 -07004717 .name = "taskcount",
4718 .read_u64 = debug_taskcount_read,
4719 },
4720
4721 {
4722 .name = "current_css_set",
4723 .read_u64 = current_css_set_read,
4724 },
4725
4726 {
4727 .name = "current_css_set_refcount",
4728 .read_u64 = current_css_set_refcount_read,
4729 },
4730
4731 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004732 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004733 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004734 },
4735
4736 {
4737 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004738 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004739 },
4740
4741 {
Paul Menagefe693432009-09-23 15:56:20 -07004742 .name = "releasable",
4743 .read_u64 = releasable_read,
4744 },
Paul Menagefe693432009-09-23 15:56:20 -07004745
Tejun Heo4baf6e32012-04-01 12:09:55 -07004746 { } /* terminate */
4747};
Paul Menagefe693432009-09-23 15:56:20 -07004748
Tejun Heo073219e2014-02-08 10:36:58 -05004749struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08004750 .css_alloc = debug_css_alloc,
4751 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07004752 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07004753};
4754#endif /* CONFIG_CGROUP_DEBUG */