blob: ec7746e5ded1cdf4458dc9c053968405cfe6a862 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070043#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/spinlock.h>
45#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070046#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070047#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070048#include <linux/delayacct.h>
49#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080050#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070051#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070052#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070053#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Li Zefan081aa452013-03-13 09:17:09 +080054#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020055#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050056#include <linux/delay.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070057
Arun Sharma600634972011-07-26 16:09:06 -070058#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070059
Tejun Heoe25e2cb2011-12-12 18:12:21 -080060/*
Tejun Heob1a21362013-11-29 10:42:58 -050061 * pidlists linger the following amount before being destroyed. The goal
62 * is avoiding frequent destruction in the middle of consecutive read calls
63 * Expiring in the middle is a performance problem not a correctness one.
64 * 1 sec should be enough.
65 */
66#define CGROUP_PIDLIST_DESTROY_DELAY HZ
67
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050068#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
69 MAX_CFTYPE_NAME + 2)
70
Tejun Heob1a21362013-11-29 10:42:58 -050071/*
Tejun Heoace2bee2014-02-11 11:52:47 -050072 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
73 * creation/removal and hierarchy changing operations including cgroup
74 * creation, removal, css association and controller rebinding. This outer
75 * lock is needed mainly to resolve the circular dependency between kernfs
76 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
77 */
78static DEFINE_MUTEX(cgroup_tree_mutex);
79
80/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080081 * cgroup_mutex is the master lock. Any modification to cgroup or its
82 * hierarchy must be performed while holding it.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080083 */
Tejun Heo22194492013-04-07 09:29:51 -070084#ifdef CONFIG_PROVE_RCU
85DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040086EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070087#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070088static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070089#endif
90
Tejun Heo69e943b2014-02-08 10:36:58 -050091/*
92 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
93 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
94 */
95static DEFINE_SPINLOCK(release_agent_path_lock);
96
Tejun Heoace2bee2014-02-11 11:52:47 -050097#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -050098 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -050099 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500100 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500101 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo87fb54f2013-12-06 15:11:55 -0500102
Ben Blumaae8aab2010-03-10 15:22:07 -0800103/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500104 * cgroup destruction makes heavy use of work items and there can be a lot
105 * of concurrent destructions. Use a separate workqueue so that cgroup
106 * destruction work items don't end up filling up max_active of system_wq
107 * which may lead to deadlock.
108 */
109static struct workqueue_struct *cgroup_destroy_wq;
110
111/*
Tejun Heob1a21362013-11-29 10:42:58 -0500112 * pidlist destructions need to be flushed on cgroup destruction. Use a
113 * separate workqueue as flush domain.
114 */
115static struct workqueue_struct *cgroup_pidlist_destroy_wq;
116
Tejun Heo3ed80a62014-02-08 10:36:58 -0500117/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500118#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500119static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120#include <linux/cgroup_subsys.h>
121};
Tejun Heo073219e2014-02-08 10:36:58 -0500122#undef SUBSYS
123
124/* array of cgroup subsystem names */
125#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
126static const char *cgroup_subsys_name[] = {
127#include <linux/cgroup_subsys.h>
128};
129#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700130
Paul Menageddbcc7e2007-10-18 23:39:30 -0700131/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700132 * The dummy hierarchy, reserved for the subsystems that are otherwise
133 * unattached - it never has more than a single cgroup, and all tasks are
134 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700135 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700136static struct cgroupfs_root cgroup_dummy_root;
137
138/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
139static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700140
141/* The list of hierarchy roots */
142
Tejun Heo9871bf92013-06-24 15:21:47 -0700143static LIST_HEAD(cgroup_roots);
144static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700145
Tejun Heo3417ae12014-02-08 10:37:01 -0500146/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700147static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700148
Li Zefan794611a2013-06-18 18:53:53 +0800149/*
150 * Assign a monotonically increasing serial number to cgroups. It
151 * guarantees cgroups with bigger numbers are newer than those with smaller
152 * numbers. Also, as cgroups are always appended to the parent's
153 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700154 * the ascending serial number order on the list. Protected by
155 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800156 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700157static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800158
Paul Menageddbcc7e2007-10-18 23:39:30 -0700159/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800160 * check for fork/exit handlers to call. This avoids us having to do
161 * extra work in the fork/exit path if none of the subsystems need to
162 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700163 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700164static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700165
Tejun Heo628f7cd2013-06-28 16:24:11 -0700166static struct cftype cgroup_base_files[];
167
Tejun Heo59f52962014-02-11 11:52:49 -0500168static void cgroup_put(struct cgroup *cgrp);
Tejun Heof2e85d52014-02-11 11:52:49 -0500169static int rebind_subsystems(struct cgroupfs_root *root,
170 unsigned long added_mask, unsigned removed_mask);
Tejun Heof20104d2013-08-13 20:22:50 -0400171static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800172static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400173static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
174 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500175static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800176
Tejun Heo95109b62013-08-08 20:11:27 -0400177/**
178 * cgroup_css - obtain a cgroup's css for the specified subsystem
179 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400180 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400181 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400182 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
183 * function must be called either under cgroup_mutex or rcu_read_lock() and
184 * the caller is responsible for pinning the returned css if it wants to
185 * keep accessing it outside the said locks. This function may return
186 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400187 */
188static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400189 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400190{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400191 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500192 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500193 lockdep_is_held(&cgroup_tree_mutex) ||
194 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400195 else
196 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400197}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700198
Paul Menageddbcc7e2007-10-18 23:39:30 -0700199/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700200static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700201{
Tejun Heo54766d42013-06-12 21:04:53 -0700202 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700203}
204
Tejun Heo59f52962014-02-11 11:52:49 -0500205struct cgroup_subsys_state *seq_css(struct seq_file *seq)
206{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500207 struct kernfs_open_file *of = seq->private;
208 struct cgroup *cgrp = of->kn->parent->priv;
209 struct cftype *cft = seq_cft(seq);
210
211 /*
212 * This is open and unprotected implementation of cgroup_css().
213 * seq_css() is only called from a kernfs file operation which has
214 * an active reference on the file. Because all the subsystem
215 * files are drained before a css is disassociated with a cgroup,
216 * the matching css from the cgroup's subsys table is guaranteed to
217 * be and stay valid until the enclosing operation is complete.
218 */
219 if (cft->ss)
220 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
221 else
222 return &cgrp->dummy_css;
Tejun Heo59f52962014-02-11 11:52:49 -0500223}
224EXPORT_SYMBOL_GPL(seq_css);
225
Li Zefan78574cf2013-04-08 19:00:38 -0700226/**
227 * cgroup_is_descendant - test ancestry
228 * @cgrp: the cgroup to be tested
229 * @ancestor: possible ancestor of @cgrp
230 *
231 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
232 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
233 * and @ancestor are accessible.
234 */
235bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
236{
237 while (cgrp) {
238 if (cgrp == ancestor)
239 return true;
240 cgrp = cgrp->parent;
241 }
242 return false;
243}
244EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700245
Adrian Bunke9685a02008-02-07 00:13:46 -0800246static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700247{
248 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700249 (1 << CGRP_RELEASABLE) |
250 (1 << CGRP_NOTIFY_ON_RELEASE);
251 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700252}
253
Adrian Bunke9685a02008-02-07 00:13:46 -0800254static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700255{
Paul Menagebd89aab2007-10-18 23:40:44 -0700256 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700257}
258
Tejun Heo30159ec2013-06-25 11:53:37 -0700259/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500260 * for_each_css - iterate all css's of a cgroup
261 * @css: the iteration cursor
262 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
263 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700264 *
265 * Should be called under cgroup_mutex.
266 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500267#define for_each_css(css, ssid, cgrp) \
268 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
269 if (!((css) = rcu_dereference_check( \
270 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500271 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500272 lockdep_is_held(&cgroup_mutex)))) { } \
273 else
274
275/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500276 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700277 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500278 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700279 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500280#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500281 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
282 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700283
Tejun Heo5549c492013-06-24 15:21:48 -0700284/* iterate across the active hierarchies */
285#define for_each_active_root(root) \
286 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700287
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700288/**
289 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
290 * @cgrp: the cgroup to be checked for liveness
291 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700292 * On success, returns true; the mutex should be later unlocked. On
293 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700294 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700295static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700296{
297 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700298 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700299 mutex_unlock(&cgroup_mutex);
300 return false;
301 }
302 return true;
303}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700304
Paul Menage81a6a5c2007-10-18 23:39:38 -0700305/* the list of cgroups eligible for automatic release. Protected by
306 * release_list_lock */
307static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200308static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700309static void cgroup_release_agent(struct work_struct *work);
310static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700311static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700312
Tejun Heo69d02062013-06-12 21:04:50 -0700313/*
314 * A cgroup can be associated with multiple css_sets as different tasks may
315 * belong to different cgroups on different hierarchies. In the other
316 * direction, a css_set is naturally associated with multiple cgroups.
317 * This M:N relationship is represented by the following link structure
318 * which exists for each association and allows traversing the associations
319 * from both sides.
320 */
321struct cgrp_cset_link {
322 /* the cgroup and css_set this link associates */
323 struct cgroup *cgrp;
324 struct css_set *cset;
325
326 /* list of cgrp_cset_links anchored at cgrp->cset_links */
327 struct list_head cset_link;
328
329 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
330 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700331};
332
333/* The default css_set - used by init and its children prior to any
334 * hierarchies being mounted. It contains a pointer to the root state
335 * for each subsystem. Also used to anchor the list of css_sets. Not
336 * reference-counted, to improve performance when child cgroups
337 * haven't been created.
338 */
339
340static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700341static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700342
Tejun Heo0942eee2013-08-08 20:11:26 -0400343/*
344 * css_set_lock protects the list of css_set objects, and the chain of
345 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400346 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400347 */
Paul Menage817929e2007-10-18 23:39:36 -0700348static DEFINE_RWLOCK(css_set_lock);
349static 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;
383 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700384 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700385 write_unlock(&css_set_lock);
386 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 Heoddd69142013-06-12 21:04:54 -0700399 /* @cgrp can't go away while we're holding css_set_lock */
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
409 write_unlock(&css_set_lock);
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 */
Li Zefan7e9abd82008-07-25 01:46:54 -0700630 read_lock(&css_set_lock);
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);
Li Zefan7e9abd82008-07-25 01:46:54 -0700634 read_unlock(&css_set_lock);
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
658 write_lock(&css_set_lock);
659 /* 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
Paul Menage817929e2007-10-18 23:39:36 -0700676 write_unlock(&css_set_lock);
677
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 */
742 write_lock(&css_set_lock);
743
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 }
749 write_unlock(&css_set_lock);
750
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
767 * called with cgroup_mutex held.
768 */
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
775 BUG_ON(!mutex_is_locked(&cgroup_mutex));
776 read_lock(&css_set_lock);
777 /*
778 * No need to lock the task - since we hold cgroup_mutex the
779 * task can't change groups, so the only thing that can happen
780 * is that it exits and its css is set back to init_css_set.
781 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700782 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700783 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700784 res = &root->top_cgroup;
785 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700786 struct cgrp_cset_link *link;
787
788 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700789 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700790
Paul Menage7717f7b2009-09-23 15:56:22 -0700791 if (c->root == root) {
792 res = c;
793 break;
794 }
795 }
796 }
797 read_unlock(&css_set_lock);
798 BUG_ON(!res);
799 return res;
800}
801
802/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803 * There is one global cgroup mutex. We also require taking
804 * task_lock() when dereferencing a task's cgroup subsys pointers.
805 * See "The task_lock() exception", at the end of this comment.
806 *
807 * A task must hold cgroup_mutex to modify cgroups.
808 *
809 * Any task can increment and decrement the count field without lock.
810 * So in general, code holding cgroup_mutex can't rely on the count
811 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800812 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700813 * means that no tasks are currently attached, therefore there is no
814 * way a task attached to that cgroup can fork (the other way to
815 * increment the count). So code holding cgroup_mutex can safely
816 * assume that if the count is zero, it will stay zero. Similarly, if
817 * a task holds cgroup_mutex on a cgroup with zero count, it
818 * knows that the cgroup won't be removed, as cgroup_rmdir()
819 * needs that mutex.
820 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700821 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
822 * (usually) take cgroup_mutex. These are the two most performance
823 * critical pieces of code here. The exception occurs on cgroup_exit(),
824 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
825 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800826 * to the release agent with the name of the cgroup (path relative to
827 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700828 *
829 * A cgroup can only be deleted if both its 'count' of using tasks
830 * is zero, and its list of 'children' cgroups is empty. Since all
831 * tasks in the system use _some_ cgroup, and since there is always at
832 * least one task in the system (init, pid == 1), therefore, top_cgroup
833 * always has either children cgroups and/or using tasks. So we don't
834 * need a special hack to ensure that top_cgroup cannot be deleted.
835 *
836 * The task_lock() exception
837 *
838 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800839 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800840 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700841 * several performance critical places that need to reference
842 * task->cgroup without the expense of grabbing a system global
843 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800844 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700845 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
846 * the task_struct routinely used for such matters.
847 *
848 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800849 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700850 */
851
Tejun Heo628f7cd2013-06-28 16:24:11 -0700852static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500853static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700854static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700855
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500856static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
857 char *buf)
858{
859 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
860 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
861 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
862 cft->ss->name, cft->name);
863 else
864 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
865 return buf;
866}
867
Tejun Heof2e85d52014-02-11 11:52:49 -0500868/**
869 * cgroup_file_mode - deduce file mode of a control file
870 * @cft: the control file in question
871 *
872 * returns cft->mode if ->mode is not 0
873 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
874 * returns S_IRUGO if it has only a read handler
875 * returns S_IWUSR if it has only a write hander
876 */
877static umode_t cgroup_file_mode(const struct cftype *cft)
878{
879 umode_t mode = 0;
880
881 if (cft->mode)
882 return cft->mode;
883
884 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
885 mode |= S_IRUGO;
886
887 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
888 cft->trigger)
889 mode |= S_IWUSR;
890
891 return mode;
892}
893
Li Zefanbe445622013-01-24 14:31:42 +0800894static void cgroup_free_fn(struct work_struct *work)
895{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700896 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800897
Tejun Heo3c9c8252014-02-12 09:29:50 -0500898 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -0500899 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800900
Tejun Heo776f02f2014-02-12 09:29:50 -0500901 if (cgrp->parent) {
902 /*
903 * We get a ref to the parent, and put the ref when this
904 * cgroup is being freed, so it's guaranteed that the
905 * parent won't be destroyed before its children.
906 */
907 cgroup_put(cgrp->parent);
908 kernfs_put(cgrp->kn);
909 kfree(cgrp);
910 } else {
911 /*
912 * This is top cgroup's refcnt reaching zero, which
913 * indicates that the root should be released.
914 */
915 cgroup_destroy_root(cgrp->root);
916 }
Li Zefanbe445622013-01-24 14:31:42 +0800917}
918
919static void cgroup_free_rcu(struct rcu_head *head)
920{
921 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
922
Tejun Heoea15f8c2013-06-13 19:27:42 -0700923 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500924 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800925}
926
Tejun Heo59f52962014-02-11 11:52:49 -0500927static void cgroup_get(struct cgroup *cgrp)
928{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500929 WARN_ON_ONCE(cgroup_is_dead(cgrp));
930 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
931 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700932}
933
Tejun Heo59f52962014-02-11 11:52:49 -0500934static void cgroup_put(struct cgroup *cgrp)
935{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500936 if (!atomic_dec_and_test(&cgrp->refcnt))
937 return;
Tejun Heo776f02f2014-02-12 09:29:50 -0500938 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -0500939 return;
Tejun Heo59f52962014-02-11 11:52:49 -0500940
Tejun Heo2bd59d42014-02-11 11:52:49 -0500941 /*
942 * XXX: cgrp->id is only used to look up css's. As cgroup and
943 * css's lifetimes will be decoupled, it should be made
944 * per-subsystem and moved to css->id so that lookups are
945 * successful until the target css is released.
946 */
947 mutex_lock(&cgroup_mutex);
948 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
949 mutex_unlock(&cgroup_mutex);
950 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700951
Tejun Heo2bd59d42014-02-11 11:52:49 -0500952 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700953}
954
Li Zefan2739d3c2013-01-21 18:18:33 +0800955static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700956{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500957 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700958
Tejun Heoace2bee2014-02-11 11:52:47 -0500959 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500960 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -0700961}
962
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400963/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700964 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700965 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400966 * @subsys_mask: mask of the subsystem ids whose files should be removed
967 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700968static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700969{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400970 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700971 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700972
Tejun Heob420ba72013-07-12 12:34:02 -0700973 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -0500974 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -0700975
976 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400977 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -0500978 list_for_each_entry(cfts, &ss->cfts, node)
979 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400980 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700981}
982
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700984 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700985{
Paul Menagebd89aab2007-10-18 23:40:44 -0700986 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700987 struct cgroup_subsys *ss;
Tejun Heo31261212013-06-28 17:07:30 -0700988 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700989
Tejun Heoace2bee2014-02-11 11:52:47 -0500990 lockdep_assert_held(&cgroup_tree_mutex);
991 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -0800992
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993 /* Check that any added subsystems are currently free */
Tejun Heo3ed80a62014-02-08 10:36:58 -0500994 for_each_subsys(ss, i)
995 if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
996 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997
Tejun Heo31261212013-06-28 17:07:30 -0700998 ret = cgroup_populate_dir(cgrp, added_mask);
999 if (ret)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001000 return ret;
Tejun Heo31261212013-06-28 17:07:30 -07001001
1002 /*
1003 * Nothing can fail from this point on. Remove files for the
1004 * removed subsystems and rebind each subsystem.
1005 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001006 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001007 cgroup_clear_dir(cgrp, removed_mask);
Tejun Heo4ac06012014-02-11 11:52:47 -05001008 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001009
Tejun Heo30159ec2013-06-25 11:53:37 -07001010 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001011 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001012
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001013 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001014 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001015 BUG_ON(cgroup_css(cgrp, ss));
1016 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1017 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001018
Tejun Heo73e80ed2013-08-13 11:01:55 -04001019 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001020 cgroup_css(cgroup_dummy_top, ss));
1021 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001022
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001023 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001025 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001026
Ben Blumcf5d5942010-03-10 15:22:09 -08001027 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001028 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001029 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001031 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1032 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001033
Paul Menageddbcc7e2007-10-18 23:39:30 -07001034 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001035 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001036
Tejun Heoca8bdca2013-08-26 18:40:56 -04001037 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001038 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1039
Tejun Heo9871bf92013-06-24 15:21:47 -07001040 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001041 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001042 }
1043 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001044
Tejun Heo2bd59d42014-02-11 11:52:49 -05001045 kernfs_activate(cgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046 return 0;
1047}
1048
Tejun Heo2bd59d42014-02-11 11:52:49 -05001049static int cgroup_show_options(struct seq_file *seq,
1050 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001052 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001054 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055
Tejun Heob85d2042013-12-06 15:11:57 -05001056 for_each_subsys(ss, ssid)
1057 if (root->subsys_mask & (1 << ssid))
1058 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001059 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1060 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001061 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001062 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001063 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001064 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001065
1066 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001067 if (strlen(root->release_agent_path))
1068 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001069 spin_unlock(&release_agent_path_lock);
1070
Tejun Heo2260e7f2012-11-19 08:13:38 -08001071 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001072 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001073 if (strlen(root->name))
1074 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 return 0;
1076}
1077
1078struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001079 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001081 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001082 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001083 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001084 /* User explicitly requested empty subsystem */
1085 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086};
1087
Ben Blumaae8aab2010-03-10 15:22:07 -08001088/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001089 * Convert a hierarchy specifier into a bitmask of subsystems and
1090 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1091 * array. This function takes refcounts on subsystems to be used, unless it
1092 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001093 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001094static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001096 char *token, *o = data;
1097 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001098 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001099 struct cgroup_subsys *ss;
1100 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001101
Ben Blumaae8aab2010-03-10 15:22:07 -08001102 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1103
Li Zefanf9ab5b52009-06-17 16:26:33 -07001104#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001105 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001106#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107
Paul Menagec6d57f32009-09-23 15:56:19 -07001108 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109
1110 while ((token = strsep(&o, ",")) != NULL) {
1111 if (!*token)
1112 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001113 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001114 /* Explicitly have no subsystems */
1115 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001116 continue;
1117 }
1118 if (!strcmp(token, "all")) {
1119 /* Mutually exclusive option 'all' + subsystem name */
1120 if (one_ss)
1121 return -EINVAL;
1122 all_ss = true;
1123 continue;
1124 }
Tejun Heo873fe092013-04-14 20:15:26 -07001125 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1126 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1127 continue;
1128 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001129 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001130 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001131 continue;
1132 }
1133 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001134 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001135 continue;
1136 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001137 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001138 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001139 continue;
1140 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001141 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001142 /* Specifying two release agents is forbidden */
1143 if (opts->release_agent)
1144 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001145 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001146 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001147 if (!opts->release_agent)
1148 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001149 continue;
1150 }
1151 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001152 const char *name = token + 5;
1153 /* Can't specify an empty name */
1154 if (!strlen(name))
1155 return -EINVAL;
1156 /* Must match [\w.-]+ */
1157 for (i = 0; i < strlen(name); i++) {
1158 char c = name[i];
1159 if (isalnum(c))
1160 continue;
1161 if ((c == '.') || (c == '-') || (c == '_'))
1162 continue;
1163 return -EINVAL;
1164 }
1165 /* Specifying two names is forbidden */
1166 if (opts->name)
1167 return -EINVAL;
1168 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001169 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001170 GFP_KERNEL);
1171 if (!opts->name)
1172 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001173
1174 continue;
1175 }
1176
Tejun Heo30159ec2013-06-25 11:53:37 -07001177 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001178 if (strcmp(token, ss->name))
1179 continue;
1180 if (ss->disabled)
1181 continue;
1182
1183 /* Mutually exclusive option 'all' + subsystem name */
1184 if (all_ss)
1185 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001186 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001187 one_ss = true;
1188
1189 break;
1190 }
1191 if (i == CGROUP_SUBSYS_COUNT)
1192 return -ENOENT;
1193 }
1194
1195 /*
1196 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001197 * otherwise if 'none', 'name=' and a subsystem name options
1198 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001199 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001200 if (all_ss || (!one_ss && !opts->none && !opts->name))
1201 for_each_subsys(ss, i)
1202 if (!ss->disabled)
1203 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001204
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001205 /* Consistency checks */
1206
Tejun Heo873fe092013-04-14 20:15:26 -07001207 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1208 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1209
Tejun Heod3ba07c2014-02-13 06:58:38 -05001210 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1211 opts->cpuset_clone_children || opts->release_agent ||
1212 opts->name) {
1213 pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001214 return -EINVAL;
1215 }
Tejun Heo873fe092013-04-14 20:15:26 -07001216 }
1217
Li Zefanf9ab5b52009-06-17 16:26:33 -07001218 /*
1219 * Option noprefix was introduced just for backward compatibility
1220 * with the old cpuset, so we allow noprefix only if mounting just
1221 * the cpuset subsystem.
1222 */
Tejun Heo93438622013-04-14 20:15:25 -07001223 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001224 return -EINVAL;
1225
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001226
1227 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001228 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001229 return -EINVAL;
1230
1231 /*
1232 * We either have to specify by name or by subsystems. (So all
1233 * empty hierarchies must have a name).
1234 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001235 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001236 return -EINVAL;
1237
1238 return 0;
1239}
1240
Tejun Heo2bd59d42014-02-11 11:52:49 -05001241static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001242{
1243 int ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001244 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001245 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001246 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001247
Tejun Heo873fe092013-04-14 20:15:26 -07001248 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1249 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1250 return -EINVAL;
1251 }
1252
Tejun Heoace2bee2014-02-11 11:52:47 -05001253 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001254 mutex_lock(&cgroup_mutex);
1255
1256 /* See what subsystems are wanted */
1257 ret = parse_cgroupfs_options(data, &opts);
1258 if (ret)
1259 goto out_unlock;
1260
Tejun Heoa8a648c2013-06-24 15:21:47 -07001261 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001262 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1263 task_tgid_nr(current), current->comm);
1264
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001265 added_mask = opts.subsys_mask & ~root->subsys_mask;
1266 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001267
Ben Blumcf5d5942010-03-10 15:22:09 -08001268 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001269 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001270 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001271 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1272 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1273 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001274 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001275 goto out_unlock;
1276 }
1277
Tejun Heof172e672013-06-28 17:07:30 -07001278 /* remounting is not allowed for populated hierarchies */
Tejun Heo3c9c8252014-02-12 09:29:50 -05001279 if (!list_empty(&root->top_cgroup.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001280 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001281 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001282 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001283
Paul Menageddbcc7e2007-10-18 23:39:30 -07001284 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001285 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001286 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001287
Tejun Heo69e943b2014-02-08 10:36:58 -05001288 if (opts.release_agent) {
1289 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001290 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001291 spin_unlock(&release_agent_path_lock);
1292 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001293 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001294 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001295 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001296 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001297 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001298 return ret;
1299}
1300
Tejun Heoafeb0f92014-02-13 06:58:39 -05001301/*
1302 * To reduce the fork() overhead for systems that are not actually using
1303 * their cgroups capability, we don't maintain the lists running through
1304 * each css_set to its tasks until we see the list actually used - in other
1305 * words after the first mount.
1306 */
1307static bool use_task_css_set_links __read_mostly;
1308
1309static void cgroup_enable_task_cg_lists(void)
1310{
1311 struct task_struct *p, *g;
1312
1313 write_lock(&css_set_lock);
1314
1315 if (use_task_css_set_links)
1316 goto out_unlock;
1317
1318 use_task_css_set_links = true;
1319
1320 /*
1321 * We need tasklist_lock because RCU is not safe against
1322 * while_each_thread(). Besides, a forking task that has passed
1323 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1324 * is not guaranteed to have its child immediately visible in the
1325 * tasklist if we walk through it with RCU.
1326 */
1327 read_lock(&tasklist_lock);
1328 do_each_thread(g, p) {
1329 task_lock(p);
1330
1331 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1332 task_css_set(p) != &init_css_set);
1333
1334 /*
1335 * We should check if the process is exiting, otherwise
1336 * it will race with cgroup_exit() in that the list
1337 * entry won't be deleted though the process has exited.
1338 */
1339 if (!(p->flags & PF_EXITING))
1340 list_add(&p->cg_list, &task_css_set(p)->tasks);
1341
1342 task_unlock(p);
1343 } while_each_thread(g, p);
1344 read_unlock(&tasklist_lock);
1345out_unlock:
1346 write_unlock(&css_set_lock);
1347}
1348
Paul Menagecc31edc2008-10-18 20:28:04 -07001349static void init_cgroup_housekeeping(struct cgroup *cgrp)
1350{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001351 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001352 INIT_LIST_HEAD(&cgrp->sibling);
1353 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001354 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001355 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001356 INIT_LIST_HEAD(&cgrp->pidlists);
1357 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001358 cgrp->dummy_css.cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001359}
Paul Menagec6d57f32009-09-23 15:56:19 -07001360
Paul Menageddbcc7e2007-10-18 23:39:30 -07001361static void init_cgroup_root(struct cgroupfs_root *root)
1362{
Paul Menagebd89aab2007-10-18 23:40:44 -07001363 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001364
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001366 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001367 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001368 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee82013-07-31 09:50:50 +08001369 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001370}
1371
Paul Menagec6d57f32009-09-23 15:56:19 -07001372static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1373{
1374 struct cgroupfs_root *root;
1375
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001376 if (!opts->subsys_mask && !opts->none)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001377 return ERR_PTR(-EINVAL);
Paul Menagec6d57f32009-09-23 15:56:19 -07001378
1379 root = kzalloc(sizeof(*root), GFP_KERNEL);
1380 if (!root)
1381 return ERR_PTR(-ENOMEM);
1382
1383 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001384
Paul Menagec6d57f32009-09-23 15:56:19 -07001385 root->flags = opts->flags;
1386 if (opts->release_agent)
1387 strcpy(root->release_agent_path, opts->release_agent);
1388 if (opts->name)
1389 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001390 if (opts->cpuset_clone_children)
1391 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001392 return root;
1393}
1394
Tejun Heo35585572014-02-13 06:58:38 -05001395static int cgroup_setup_root(struct cgroupfs_root *root, unsigned long ss_mask)
Tejun Heod427dfe2014-02-11 11:52:48 -05001396{
1397 LIST_HEAD(tmp_links);
Tejun Heod427dfe2014-02-11 11:52:48 -05001398 struct cgroup *root_cgrp = &root->top_cgroup;
Tejun Heod427dfe2014-02-11 11:52:48 -05001399 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001400 int i, ret;
1401
1402 lockdep_assert_held(&cgroup_tree_mutex);
1403 lockdep_assert_held(&cgroup_mutex);
Tejun Heod427dfe2014-02-11 11:52:48 -05001404
1405 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1406 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001407 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001408 root_cgrp->id = ret;
1409
Tejun Heod427dfe2014-02-11 11:52:48 -05001410 /*
1411 * We're accessing css_set_count without locking css_set_lock here,
1412 * but that's OK - it can only be increased by someone holding
1413 * cgroup_lock, and that's us. The worst that can happen is that we
1414 * have some link structures left over
1415 */
1416 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1417 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001418 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001419
1420 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1421 ret = cgroup_init_root_id(root, 2, 0);
1422 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001423 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001424
Tejun Heo2bd59d42014-02-11 11:52:49 -05001425 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1426 KERNFS_ROOT_CREATE_DEACTIVATED,
1427 root_cgrp);
1428 if (IS_ERR(root->kf_root)) {
1429 ret = PTR_ERR(root->kf_root);
1430 goto exit_root_id;
1431 }
1432 root_cgrp->kn = root->kf_root->kn;
Tejun Heod427dfe2014-02-11 11:52:48 -05001433
1434 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1435 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001436 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001437
Tejun Heo35585572014-02-13 06:58:38 -05001438 ret = rebind_subsystems(root, ss_mask, 0);
Tejun Heod427dfe2014-02-11 11:52:48 -05001439 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001440 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001441
1442 /*
1443 * There must be no failure case after here, since rebinding takes
1444 * care of subsystems' refcounts, which are explicitly dropped in
1445 * the failure exit path.
1446 */
1447 list_add(&root->root_list, &cgroup_roots);
1448 cgroup_root_count++;
1449
1450 /*
1451 * Link the top cgroup in this hierarchy into all the css_set
1452 * objects.
1453 */
1454 write_lock(&css_set_lock);
1455 hash_for_each(css_set_table, i, cset, hlist)
1456 link_css_set(&tmp_links, cset, root_cgrp);
1457 write_unlock(&css_set_lock);
1458
1459 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001460 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001461
Tejun Heo2bd59d42014-02-11 11:52:49 -05001462 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001463 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001464 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001465
Tejun Heo2bd59d42014-02-11 11:52:49 -05001466destroy_root:
1467 kernfs_destroy_root(root->kf_root);
1468 root->kf_root = NULL;
1469exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001470 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001471out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001472 free_cgrp_cset_links(&tmp_links);
1473 return ret;
1474}
1475
Al Virof7e83572010-07-26 13:23:11 +04001476static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001477 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001478 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001479{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001480 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001481 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001482 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001483 int ret;
Tejun Heo56fde9e2014-02-13 06:58:38 -05001484
1485 /*
1486 * The first time anyone tries to mount a cgroup, enable the list
1487 * linking each css_set to its tasks and fix up all existing tasks.
1488 */
1489 if (!use_task_css_set_links)
1490 cgroup_enable_task_cg_lists();
Tejun Heo776f02f2014-02-12 09:29:50 -05001491retry:
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001492 mutex_lock(&cgroup_tree_mutex);
1493 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001494
1495 /* First find the desired set of subsystems */
1496 ret = parse_cgroupfs_options(data, &opts);
Paul Menagec6d57f32009-09-23 15:56:19 -07001497 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001498 goto out_unlock;
Paul Menagec6d57f32009-09-23 15:56:19 -07001499
Tejun Heo2bd59d42014-02-11 11:52:49 -05001500 /* look for a matching existing root */
1501 for_each_active_root(root) {
1502 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001503
Paul Menagec6d57f32009-09-23 15:56:19 -07001504 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001505 * If we asked for a name then it must match. Also, if
1506 * name matches but sybsys_mask doesn't, we should fail.
1507 * Remember whether name matched.
Paul Menagec6d57f32009-09-23 15:56:19 -07001508 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001509 if (opts.name) {
1510 if (strcmp(opts.name, root->name))
1511 continue;
1512 name_match = true;
1513 }
1514
1515 /*
1516 * If we asked for subsystems (or explicitly for no
1517 * subsystems) then they must match.
1518 */
1519 if ((opts.subsys_mask || opts.none) &&
1520 (opts.subsys_mask != root->subsys_mask)) {
1521 if (!name_match)
1522 continue;
1523 ret = -EBUSY;
1524 goto out_unlock;
1525 }
Tejun Heo873fe092013-04-14 20:15:26 -07001526
Tejun Heoc7ba8282013-06-29 14:06:10 -07001527 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001528 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1529 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1530 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001531 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001532 } else {
1533 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1534 }
Tejun Heo873fe092013-04-14 20:15:26 -07001535 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001536
Tejun Heo776f02f2014-02-12 09:29:50 -05001537 /*
1538 * A root's lifetime is governed by its top cgroup. Zero
1539 * ref indicate that the root is being destroyed. Wait for
1540 * destruction to complete so that the subsystems are free.
1541 * We can use wait_queue for the wait but this path is
1542 * super cold. Let's just sleep for a bit and retry.
1543 */
1544 if (!atomic_inc_not_zero(&root->top_cgroup.refcnt)) {
1545 mutex_unlock(&cgroup_mutex);
1546 mutex_unlock(&cgroup_tree_mutex);
1547 msleep(10);
1548 goto retry;
1549 }
1550
1551 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001552 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553 }
1554
Tejun Heo2bd59d42014-02-11 11:52:49 -05001555 /* no such thing, create a new one */
1556 root = cgroup_root_from_opts(&opts);
1557 if (IS_ERR(root)) {
1558 ret = PTR_ERR(root);
1559 goto out_unlock;
1560 }
1561
Tejun Heo35585572014-02-13 06:58:38 -05001562 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001563 if (ret)
1564 cgroup_free_root(root);
1565
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001566out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001567 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001568 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001569
Paul Menagec6d57f32009-09-23 15:56:19 -07001570 kfree(opts.release_agent);
1571 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001572
Tejun Heo2bd59d42014-02-11 11:52:49 -05001573 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001574 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001575
1576 dentry = kernfs_mount(fs_type, flags, root->kf_root);
1577 if (IS_ERR(dentry))
Tejun Heo776f02f2014-02-12 09:29:50 -05001578 cgroup_put(&root->top_cgroup);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001579 return dentry;
1580}
1581
1582static void cgroup_kill_sb(struct super_block *sb)
1583{
1584 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
1585 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
1586
Tejun Heo776f02f2014-02-12 09:29:50 -05001587 cgroup_put(&root->top_cgroup);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001588 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001589}
1590
Paul Menageddbcc7e2007-10-18 23:39:30 -07001591static struct file_system_type cgroup_fs_type = {
1592 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001593 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001594 .kill_sb = cgroup_kill_sb,
1595};
1596
Greg KH676db4a2010-08-05 13:53:35 -07001597static struct kobject *cgroup_kobj;
1598
Li Zefana043e3b2008-02-23 15:24:09 -08001599/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001600 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001601 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001602 * @buf: the buffer to write the path into
1603 * @buflen: the length of the buffer
1604 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001605 * Determine @task's cgroup on the first (the one with the lowest non-zero
1606 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1607 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1608 * cgroup controller callbacks.
1609 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001610 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001611 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001612char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001613{
1614 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001615 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001616 int hierarchy_id = 1;
1617 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001618
1619 mutex_lock(&cgroup_mutex);
1620
Tejun Heo913ffdb2013-07-11 16:34:48 -07001621 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1622
Tejun Heo857a2be2013-04-14 20:50:08 -07001623 if (root) {
1624 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001625 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001626 } else {
1627 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001628 if (strlcpy(buf, "/", buflen) < buflen)
1629 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001630 }
1631
1632 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001633 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001634}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001635EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001636
Ben Blum74a11662011-05-26 16:25:20 -07001637/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001638 * Control Group taskset
1639 */
Tejun Heo134d3372011-12-12 18:12:21 -08001640struct task_and_cgroup {
1641 struct task_struct *task;
1642 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001643 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001644};
1645
Tejun Heo2f7ee562011-12-12 18:12:21 -08001646struct cgroup_taskset {
1647 struct task_and_cgroup single;
1648 struct flex_array *tc_array;
1649 int tc_array_len;
1650 int idx;
1651 struct cgroup *cur_cgrp;
1652};
1653
1654/**
1655 * cgroup_taskset_first - reset taskset and return the first task
1656 * @tset: taskset of interest
1657 *
1658 * @tset iteration is initialized and the first task is returned.
1659 */
1660struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1661{
1662 if (tset->tc_array) {
1663 tset->idx = 0;
1664 return cgroup_taskset_next(tset);
1665 } else {
1666 tset->cur_cgrp = tset->single.cgrp;
1667 return tset->single.task;
1668 }
1669}
1670EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1671
1672/**
1673 * cgroup_taskset_next - iterate to the next task in taskset
1674 * @tset: taskset of interest
1675 *
1676 * Return the next task in @tset. Iteration must have been initialized
1677 * with cgroup_taskset_first().
1678 */
1679struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1680{
1681 struct task_and_cgroup *tc;
1682
1683 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1684 return NULL;
1685
1686 tc = flex_array_get(tset->tc_array, tset->idx++);
1687 tset->cur_cgrp = tc->cgrp;
1688 return tc->task;
1689}
1690EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1691
1692/**
Tejun Heod99c8722013-08-08 20:11:27 -04001693 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001694 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001695 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001696 *
Tejun Heod99c8722013-08-08 20:11:27 -04001697 * Return the css for the current (last returned) task of @tset for
1698 * subsystem specified by @subsys_id. This function must be preceded by
1699 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001700 */
Tejun Heod99c8722013-08-08 20:11:27 -04001701struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1702 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001703{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001704 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001705}
Tejun Heod99c8722013-08-08 20:11:27 -04001706EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001707
1708/**
1709 * cgroup_taskset_size - return the number of tasks in taskset
1710 * @tset: taskset of interest
1711 */
1712int cgroup_taskset_size(struct cgroup_taskset *tset)
1713{
1714 return tset->tc_array ? tset->tc_array_len : 1;
1715}
1716EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1717
1718
Ben Blum74a11662011-05-26 16:25:20 -07001719/*
1720 * cgroup_task_migrate - move a task from one cgroup to another.
1721 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001722 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001723 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001724static void cgroup_task_migrate(struct cgroup *old_cgrp,
1725 struct task_struct *tsk,
1726 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001727{
Tejun Heo5abb8852013-06-12 21:04:49 -07001728 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001729
1730 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001731 * We are synchronized through threadgroup_lock() against PF_EXITING
1732 * setting such that we can't race against cgroup_exit() changing the
1733 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001734 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001735 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001736 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001737
Ben Blum74a11662011-05-26 16:25:20 -07001738 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001739 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001740 task_unlock(tsk);
1741
Ben Blum74a11662011-05-26 16:25:20 -07001742 write_lock(&css_set_lock);
Tejun Heo56fde9e2014-02-13 06:58:38 -05001743 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001744 write_unlock(&css_set_lock);
1745
1746 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001747 * We just gained a reference on old_cset by taking it from the
1748 * task. As trading it for new_cset is protected by cgroup_mutex,
1749 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001750 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001751 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1752 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001753}
1754
Li Zefana043e3b2008-02-23 15:24:09 -08001755/**
Li Zefan081aa452013-03-13 09:17:09 +08001756 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001757 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001758 * @tsk: the task or the leader of the threadgroup to be attached
1759 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001760 *
Tejun Heo257058a2011-12-12 18:12:21 -08001761 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001762 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001763 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001764static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1765 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001766{
1767 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001768 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001769 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001770 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001771 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001772 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001773 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001774 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001775
1776 /*
1777 * step 0: in order to do expensive, possibly blocking operations for
1778 * every thread, we cannot iterate the thread group list, since it needs
1779 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001780 * group - group_rwsem prevents new threads from appearing, and if
1781 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001782 */
Li Zefan081aa452013-03-13 09:17:09 +08001783 if (threadgroup)
1784 group_size = get_nr_threads(tsk);
1785 else
1786 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001787 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001788 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001789 if (!group)
1790 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001791 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001792 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001793 if (retval)
1794 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001795
Ben Blum74a11662011-05-26 16:25:20 -07001796 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001797 /*
1798 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1799 * already PF_EXITING could be freed from underneath us unless we
1800 * take an rcu_read_lock.
1801 */
1802 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001803 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001804 struct task_and_cgroup ent;
1805
Tejun Heocd3d0952011-12-12 18:12:21 -08001806 /* @tsk either already exited or can't exit until the end */
1807 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001808 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001809
Ben Blum74a11662011-05-26 16:25:20 -07001810 /* as per above, nr_threads may decrease, but not increase. */
1811 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001812 ent.task = tsk;
1813 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001814 /* nothing to do if this task is already in the cgroup */
1815 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001816 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001817 /*
1818 * saying GFP_ATOMIC has no effect here because we did prealloc
1819 * earlier, but it's good form to communicate our expectations.
1820 */
Tejun Heo134d3372011-12-12 18:12:21 -08001821 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07001822 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07001823 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08001824 next:
Li Zefan081aa452013-03-13 09:17:09 +08001825 if (!threadgroup)
1826 break;
Ben Blum74a11662011-05-26 16:25:20 -07001827 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001828 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07001829 /* remember the number of threads in the array for later. */
1830 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001831 tset.tc_array = group;
1832 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001833
Tejun Heo134d3372011-12-12 18:12:21 -08001834 /* methods shouldn't be called if no task is actually migrating */
1835 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001836 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08001837 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08001838
Ben Blum74a11662011-05-26 16:25:20 -07001839 /*
1840 * step 1: check that we can legitimately attach to the cgroup.
1841 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001842 for_each_css(css, i, cgrp) {
1843 if (css->ss->can_attach) {
1844 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001845 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001846 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07001847 goto out_cancel_attach;
1848 }
1849 }
Ben Blum74a11662011-05-26 16:25:20 -07001850 }
1851
1852 /*
1853 * step 2: make sure css_sets exist for all threads to be migrated.
1854 * we use find_css_set, which allocates a new one if necessary.
1855 */
Ben Blum74a11662011-05-26 16:25:20 -07001856 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07001857 struct css_set *old_cset;
1858
Tejun Heo134d3372011-12-12 18:12:21 -08001859 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001860 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001861 tc->cset = find_css_set(old_cset, cgrp);
1862 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001863 retval = -ENOMEM;
1864 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07001865 }
1866 }
1867
1868 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001869 * step 3: now that we're guaranteed success wrt the css_sets,
1870 * proceed to move all tasks to the new cgroup. There are no
1871 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07001872 */
Ben Blum74a11662011-05-26 16:25:20 -07001873 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08001874 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001875 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07001876 }
1877 /* nothing is sensitive to fork() after this point. */
1878
1879 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001880 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07001881 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001882 for_each_css(css, i, cgrp)
1883 if (css->ss->attach)
1884 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001885
1886 /*
1887 * step 5: success! and cleanup
1888 */
Ben Blum74a11662011-05-26 16:25:20 -07001889 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001890out_put_css_set_refs:
1891 if (retval) {
1892 for (i = 0; i < group_size; i++) {
1893 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001894 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001895 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001896 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001897 }
Ben Blum74a11662011-05-26 16:25:20 -07001898 }
1899out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07001900 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001901 for_each_css(css, i, cgrp) {
1902 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07001903 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001904 if (css->ss->cancel_attach)
1905 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001906 }
1907 }
Ben Blum74a11662011-05-26 16:25:20 -07001908out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07001909 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07001910 return retval;
1911}
1912
1913/*
1914 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08001915 * function to attach either it or all tasks in its threadgroup. Will lock
1916 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07001917 */
1918static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001919{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001920 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001921 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001922 int ret;
1923
Ben Blum74a11662011-05-26 16:25:20 -07001924 if (!cgroup_lock_live_group(cgrp))
1925 return -ENODEV;
1926
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001927retry_find_task:
1928 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001929 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001930 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07001931 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07001932 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001933 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001934 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001935 }
Ben Blum74a11662011-05-26 16:25:20 -07001936 /*
1937 * even if we're attaching all tasks in the thread group, we
1938 * only need to check permissions on one of them.
1939 */
David Howellsc69e8d92008-11-14 10:39:19 +11001940 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07001941 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
1942 !uid_eq(cred->euid, tcred->uid) &&
1943 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001944 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001945 ret = -EACCES;
1946 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001947 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001948 } else
1949 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08001950
1951 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001952 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02001953
1954 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07001955 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02001956 * trapped in a cpuset, or RT worker may be born in a cgroup
1957 * with no rt_runtime allocated. Just say no.
1958 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07001959 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02001960 ret = -EINVAL;
1961 rcu_read_unlock();
1962 goto out_unlock_cgroup;
1963 }
1964
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001965 get_task_struct(tsk);
1966 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08001967
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001968 threadgroup_lock(tsk);
1969 if (threadgroup) {
1970 if (!thread_group_leader(tsk)) {
1971 /*
1972 * a race with de_thread from another thread's exec()
1973 * may strip us of our leadership, if this happens,
1974 * there is no choice but to throw this task away and
1975 * try again; this is
1976 * "double-double-toil-and-trouble-check locking".
1977 */
1978 threadgroup_unlock(tsk);
1979 put_task_struct(tsk);
1980 goto retry_find_task;
1981 }
Li Zefan081aa452013-03-13 09:17:09 +08001982 }
1983
1984 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
1985
Tejun Heocd3d0952011-12-12 18:12:21 -08001986 threadgroup_unlock(tsk);
1987
Paul Menagebbcb81d2007-10-18 23:39:32 -07001988 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001989out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07001990 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001991 return ret;
1992}
1993
Tejun Heo7ae1bad2013-04-07 09:29:51 -07001994/**
1995 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1996 * @from: attach to all cgroups of a given task
1997 * @tsk: the task to be attached
1998 */
1999int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2000{
2001 struct cgroupfs_root *root;
2002 int retval = 0;
2003
Tejun Heo47cfcd02013-04-07 09:29:51 -07002004 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002005 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002006 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002007
Li Zefan6f4b7e62013-07-31 16:18:36 +08002008 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002009 if (retval)
2010 break;
2011 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002012 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002013
2014 return retval;
2015}
2016EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2017
Tejun Heo182446d2013-08-08 20:11:24 -04002018static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2019 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002020{
Tejun Heo182446d2013-08-08 20:11:24 -04002021 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002022}
2023
Tejun Heo182446d2013-08-08 20:11:24 -04002024static int cgroup_procs_write(struct cgroup_subsys_state *css,
2025 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002026{
Tejun Heo182446d2013-08-08 20:11:24 -04002027 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002028}
2029
Tejun Heo182446d2013-08-08 20:11:24 -04002030static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2031 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002032{
Tejun Heo5f469902014-02-11 11:52:48 -05002033 struct cgroupfs_root *root = css->cgroup->root;
2034
2035 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002036 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002037 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002038 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002039 strlcpy(root->release_agent_path, buffer,
2040 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002041 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002042 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002043 return 0;
2044}
2045
Tejun Heo2da8ca82013-12-05 12:28:04 -05002046static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002047{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002048 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002049
Paul Menagee788e062008-07-25 01:46:59 -07002050 if (!cgroup_lock_live_group(cgrp))
2051 return -ENODEV;
2052 seq_puts(seq, cgrp->root->release_agent_path);
2053 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002054 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002055 return 0;
2056}
2057
Tejun Heo2da8ca82013-12-05 12:28:04 -05002058static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002059{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002060 struct cgroup *cgrp = seq_css(seq)->cgroup;
2061
2062 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002063 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002064}
2065
Tejun Heo2bd59d42014-02-11 11:52:49 -05002066static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2067 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002068{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002069 struct cgroup *cgrp = of->kn->parent->priv;
2070 struct cftype *cft = of->kn->priv;
2071 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002072 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002073
Tejun Heo2bd59d42014-02-11 11:52:49 -05002074 /*
2075 * kernfs guarantees that a file isn't deleted with operations in
2076 * flight, which means that the matching css is and stays alive and
2077 * doesn't need to be pinned. The RCU locking is not necessary
2078 * either. It's just for the convenience of using cgroup_css().
2079 */
2080 rcu_read_lock();
2081 css = cgroup_css(cgrp, cft->ss);
2082 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002083
Tejun Heoa742c592013-12-05 12:28:03 -05002084 if (cft->write_string) {
2085 ret = cft->write_string(css, cft, strstrip(buf));
2086 } else if (cft->write_u64) {
2087 unsigned long long v;
2088 ret = kstrtoull(buf, 0, &v);
2089 if (!ret)
2090 ret = cft->write_u64(css, cft, v);
2091 } else if (cft->write_s64) {
2092 long long v;
2093 ret = kstrtoll(buf, 0, &v);
2094 if (!ret)
2095 ret = cft->write_s64(css, cft, v);
2096 } else if (cft->trigger) {
2097 ret = cft->trigger(css, (unsigned int)cft->private);
2098 } else {
2099 ret = -EINVAL;
2100 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002101
Tejun Heoa742c592013-12-05 12:28:03 -05002102 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002103}
2104
Tejun Heo6612f052013-12-05 12:28:04 -05002105static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002106{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002107 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002108}
2109
2110static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2111{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002112 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002113}
2114
2115static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2116{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002117 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002118}
2119
2120static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2121{
Tejun Heo7da11272013-12-05 12:28:04 -05002122 struct cftype *cft = seq_cft(m);
2123 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002124
Tejun Heo2da8ca82013-12-05 12:28:04 -05002125 if (cft->seq_show)
2126 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002127
Tejun Heo896f5192013-12-05 12:28:04 -05002128 if (cft->read_u64)
2129 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2130 else if (cft->read_s64)
2131 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2132 else
2133 return -EINVAL;
2134 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002135}
2136
Tejun Heo2bd59d42014-02-11 11:52:49 -05002137static struct kernfs_ops cgroup_kf_single_ops = {
2138 .atomic_write_len = PAGE_SIZE,
2139 .write = cgroup_file_write,
2140 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002141};
2142
Tejun Heo2bd59d42014-02-11 11:52:49 -05002143static struct kernfs_ops cgroup_kf_ops = {
2144 .atomic_write_len = PAGE_SIZE,
2145 .write = cgroup_file_write,
2146 .seq_start = cgroup_seqfile_start,
2147 .seq_next = cgroup_seqfile_next,
2148 .seq_stop = cgroup_seqfile_stop,
2149 .seq_show = cgroup_seqfile_show,
2150};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002151
2152/*
2153 * cgroup_rename - Only allow simple rename of directories in place.
2154 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002155static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2156 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002157{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002158 struct cgroup *cgrp = kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002159 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002160
Tejun Heo2bd59d42014-02-11 11:52:49 -05002161 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002162 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002163 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002164 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002165
Tejun Heo6db8e852013-06-14 11:18:22 -07002166 /*
2167 * This isn't a proper migration and its usefulness is very
2168 * limited. Disallow if sane_behavior.
2169 */
2170 if (cgroup_sane_behavior(cgrp))
2171 return -EPERM;
2172
Tejun Heo2bd59d42014-02-11 11:52:49 -05002173 mutex_lock(&cgroup_tree_mutex);
2174 mutex_lock(&cgroup_mutex);
2175
2176 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002177
Tejun Heo2bd59d42014-02-11 11:52:49 -05002178 mutex_unlock(&cgroup_mutex);
2179 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002180 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002181}
2182
Tejun Heo2bb566c2013-08-08 20:11:23 -04002183static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002184{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002185 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002186 struct kernfs_node *kn;
2187 struct lock_class_key *key = NULL;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002188
Tejun Heo2bd59d42014-02-11 11:52:49 -05002189#ifdef CONFIG_DEBUG_LOCK_ALLOC
2190 key = &cft->lockdep_key;
2191#endif
2192 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2193 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2194 NULL, false, key);
2195 if (IS_ERR(kn))
2196 return PTR_ERR(kn);
2197 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002198}
2199
Tejun Heob1f28d32013-06-28 16:24:10 -07002200/**
2201 * cgroup_addrm_files - add or remove files to a cgroup directory
2202 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002203 * @cfts: array of cftypes to be added
2204 * @is_add: whether to add or remove
2205 *
2206 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002207 * For removals, this function never fails. If addition fails, this
2208 * function doesn't remove files already added. The caller is responsible
2209 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002210 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002211static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2212 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002213{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002214 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002215 int ret;
2216
Tejun Heoace2bee2014-02-11 11:52:47 -05002217 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002218
2219 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002220 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002221 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2222 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002223 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2224 continue;
2225 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2226 continue;
2227
Li Zefan2739d3c2013-01-21 18:18:33 +08002228 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002229 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002230 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002231 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002232 cft->name, ret);
2233 return ret;
2234 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002235 } else {
2236 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002237 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002238 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002239 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002240}
2241
Tejun Heo21a2d3432014-02-12 09:29:49 -05002242static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002243{
2244 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002245 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002246 struct cgroup *root = &ss->root->top_cgroup;
Tejun Heo492eb212013-08-08 20:11:25 -04002247 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002248 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002249
Tejun Heo21a2d3432014-02-12 09:29:49 -05002250 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo4ac06012014-02-11 11:52:47 -05002251
Tejun Heo21a2d3432014-02-12 09:29:49 -05002252 /* don't bother if @ss isn't attached */
2253 if (ss->root == &cgroup_dummy_root)
Tejun Heo9ccece82013-06-28 16:24:11 -07002254 return 0;
Li Zefane8c82d22013-06-18 18:48:37 +08002255
Li Zefane8c82d22013-06-18 18:48:37 +08002256 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002257 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002258 struct cgroup *cgrp = css->cgroup;
2259
Li Zefane8c82d22013-06-18 18:48:37 +08002260 if (cgroup_is_dead(cgrp))
2261 continue;
2262
Tejun Heo21a2d3432014-02-12 09:29:49 -05002263 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002264 if (ret)
2265 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002266 }
Tejun Heo21a2d3432014-02-12 09:29:49 -05002267
2268 if (is_add && !ret)
2269 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002270 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002271}
2272
Tejun Heo2da440a2014-02-11 11:52:48 -05002273static void cgroup_exit_cftypes(struct cftype *cfts)
2274{
2275 struct cftype *cft;
2276
Tejun Heo2bd59d42014-02-11 11:52:49 -05002277 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2278 /* free copy for custom atomic_write_len, see init_cftypes() */
2279 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2280 kfree(cft->kf_ops);
2281 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002282 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002283 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002284}
2285
Tejun Heo2bd59d42014-02-11 11:52:49 -05002286static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002287{
2288 struct cftype *cft;
2289
Tejun Heo2bd59d42014-02-11 11:52:49 -05002290 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2291 struct kernfs_ops *kf_ops;
2292
Tejun Heo0adb0702014-02-12 09:29:48 -05002293 WARN_ON(cft->ss || cft->kf_ops);
2294
Tejun Heo2bd59d42014-02-11 11:52:49 -05002295 if (cft->seq_start)
2296 kf_ops = &cgroup_kf_ops;
2297 else
2298 kf_ops = &cgroup_kf_single_ops;
2299
2300 /*
2301 * Ugh... if @cft wants a custom max_write_len, we need to
2302 * make a copy of kf_ops to set its atomic_write_len.
2303 */
2304 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2305 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2306 if (!kf_ops) {
2307 cgroup_exit_cftypes(cfts);
2308 return -ENOMEM;
2309 }
2310 kf_ops->atomic_write_len = cft->max_write_len;
2311 }
2312
2313 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002314 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002315 }
2316
2317 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002318}
2319
Tejun Heo21a2d3432014-02-12 09:29:49 -05002320static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2321{
2322 lockdep_assert_held(&cgroup_tree_mutex);
2323
2324 if (!cfts || !cfts[0].ss)
2325 return -ENOENT;
2326
2327 list_del(&cfts->node);
2328 cgroup_apply_cftypes(cfts, false);
2329 cgroup_exit_cftypes(cfts);
2330 return 0;
2331}
2332
Tejun Heo8e3f6542012-04-01 12:09:55 -07002333/**
Tejun Heo80b13582014-02-12 09:29:48 -05002334 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2335 * @cfts: zero-length name terminated array of cftypes
2336 *
2337 * Unregister @cfts. Files described by @cfts are removed from all
2338 * existing cgroups and all future cgroups won't have them either. This
2339 * function can be called anytime whether @cfts' subsys is attached or not.
2340 *
2341 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2342 * registered.
2343 */
2344int cgroup_rm_cftypes(struct cftype *cfts)
2345{
Tejun Heo21a2d3432014-02-12 09:29:49 -05002346 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002347
Tejun Heo21a2d3432014-02-12 09:29:49 -05002348 mutex_lock(&cgroup_tree_mutex);
2349 ret = cgroup_rm_cftypes_locked(cfts);
2350 mutex_unlock(&cgroup_tree_mutex);
2351 return ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002352}
2353
2354/**
Tejun Heo8e3f6542012-04-01 12:09:55 -07002355 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2356 * @ss: target cgroup subsystem
2357 * @cfts: zero-length name terminated array of cftypes
2358 *
2359 * Register @cfts to @ss. Files described by @cfts are created for all
2360 * existing cgroups to which @ss is attached and all future cgroups will
2361 * have them too. This function can be called anytime whether @ss is
2362 * attached or not.
2363 *
2364 * Returns 0 on successful registration, -errno on failure. Note that this
2365 * function currently returns 0 as long as @cfts registration is successful
2366 * even if some file creation attempts on existing cgroups fail.
2367 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002368int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002369{
Tejun Heo9ccece82013-06-28 16:24:11 -07002370 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002371
Tejun Heo2bd59d42014-02-11 11:52:49 -05002372 ret = cgroup_init_cftypes(ss, cfts);
2373 if (ret)
2374 return ret;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002375
Tejun Heo21a2d3432014-02-12 09:29:49 -05002376 mutex_lock(&cgroup_tree_mutex);
2377
Tejun Heo0adb0702014-02-12 09:29:48 -05002378 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d3432014-02-12 09:29:49 -05002379 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002380 if (ret)
Tejun Heo21a2d3432014-02-12 09:29:49 -05002381 cgroup_rm_cftypes_locked(cfts);
2382
2383 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002384 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002385}
2386EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2387
Li Zefana043e3b2008-02-23 15:24:09 -08002388/**
2389 * cgroup_task_count - count the number of tasks in a cgroup.
2390 * @cgrp: the cgroup in question
2391 *
2392 * Return the number of tasks in the cgroup.
2393 */
Tejun Heo07bc3562014-02-13 06:58:39 -05002394static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002395{
2396 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002397 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002398
Paul Menage817929e2007-10-18 23:39:36 -07002399 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002400 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2401 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002402 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002403 return count;
2404}
2405
Tejun Heo574bd9f2012-11-09 09:12:29 -08002406/**
Tejun Heo492eb212013-08-08 20:11:25 -04002407 * css_next_child - find the next child of a given css
2408 * @pos_css: the current position (%NULL to initiate traversal)
2409 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002410 *
Tejun Heo492eb212013-08-08 20:11:25 -04002411 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002412 * under either cgroup_mutex or RCU read lock. The only requirement is
2413 * that @parent_css and @pos_css are accessible. The next sibling is
2414 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002415 */
Tejun Heo492eb212013-08-08 20:11:25 -04002416struct cgroup_subsys_state *
2417css_next_child(struct cgroup_subsys_state *pos_css,
2418 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002419{
Tejun Heo492eb212013-08-08 20:11:25 -04002420 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2421 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002422 struct cgroup *next;
2423
Tejun Heoace2bee2014-02-11 11:52:47 -05002424 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002425
2426 /*
2427 * @pos could already have been removed. Once a cgroup is removed,
2428 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002429 * changes. As CGRP_DEAD assertion is serialized and happens
2430 * before the cgroup is taken off the ->sibling list, if we see it
2431 * unasserted, it's guaranteed that the next sibling hasn't
2432 * finished its grace period even if it's already removed, and thus
2433 * safe to dereference from this RCU critical section. If
2434 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2435 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002436 *
2437 * If @pos is dead, its next pointer can't be dereferenced;
2438 * however, as each cgroup is given a monotonically increasing
2439 * unique serial number and always appended to the sibling list,
2440 * the next one can be found by walking the parent's children until
2441 * we see a cgroup with higher serial number than @pos's. While
2442 * this path can be slower, it's taken only when either the current
2443 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002444 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002445 if (!pos) {
2446 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2447 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002448 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002449 } else {
2450 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2451 if (next->serial_nr > pos->serial_nr)
2452 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002453 }
2454
Tejun Heo492eb212013-08-08 20:11:25 -04002455 if (&next->sibling == &cgrp->children)
2456 return NULL;
2457
Tejun Heoca8bdca2013-08-26 18:40:56 -04002458 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002459}
Tejun Heo492eb212013-08-08 20:11:25 -04002460EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002461
2462/**
Tejun Heo492eb212013-08-08 20:11:25 -04002463 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002464 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002465 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002466 *
Tejun Heo492eb212013-08-08 20:11:25 -04002467 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002468 * to visit for pre-order traversal of @root's descendants. @root is
2469 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002470 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002471 * While this function requires cgroup_mutex or RCU read locking, it
2472 * doesn't require the whole traversal to be contained in a single critical
2473 * section. This function will return the correct next descendant as long
2474 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002475 */
Tejun Heo492eb212013-08-08 20:11:25 -04002476struct cgroup_subsys_state *
2477css_next_descendant_pre(struct cgroup_subsys_state *pos,
2478 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002479{
Tejun Heo492eb212013-08-08 20:11:25 -04002480 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002481
Tejun Heoace2bee2014-02-11 11:52:47 -05002482 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002483
Tejun Heobd8815a2013-08-08 20:11:27 -04002484 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002485 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002486 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002487
2488 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002489 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002490 if (next)
2491 return next;
2492
2493 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002494 while (pos != root) {
2495 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002496 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002497 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002498 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002499 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002500
2501 return NULL;
2502}
Tejun Heo492eb212013-08-08 20:11:25 -04002503EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002504
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002505/**
Tejun Heo492eb212013-08-08 20:11:25 -04002506 * css_rightmost_descendant - return the rightmost descendant of a css
2507 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002508 *
Tejun Heo492eb212013-08-08 20:11:25 -04002509 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2510 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002511 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002512 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002513 * While this function requires cgroup_mutex or RCU read locking, it
2514 * doesn't require the whole traversal to be contained in a single critical
2515 * section. This function will return the correct rightmost descendant as
2516 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002517 */
Tejun Heo492eb212013-08-08 20:11:25 -04002518struct cgroup_subsys_state *
2519css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002520{
Tejun Heo492eb212013-08-08 20:11:25 -04002521 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002522
Tejun Heoace2bee2014-02-11 11:52:47 -05002523 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002524
2525 do {
2526 last = pos;
2527 /* ->prev isn't RCU safe, walk ->next till the end */
2528 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002529 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002530 pos = tmp;
2531 } while (pos);
2532
2533 return last;
2534}
Tejun Heo492eb212013-08-08 20:11:25 -04002535EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002536
Tejun Heo492eb212013-08-08 20:11:25 -04002537static struct cgroup_subsys_state *
2538css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002539{
Tejun Heo492eb212013-08-08 20:11:25 -04002540 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002541
2542 do {
2543 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04002544 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002545 } while (pos);
2546
2547 return last;
2548}
2549
2550/**
Tejun Heo492eb212013-08-08 20:11:25 -04002551 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002552 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002553 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002554 *
Tejun Heo492eb212013-08-08 20:11:25 -04002555 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002556 * to visit for post-order traversal of @root's descendants. @root is
2557 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002558 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002559 * While this function requires cgroup_mutex or RCU read locking, it
2560 * doesn't require the whole traversal to be contained in a single critical
2561 * section. This function will return the correct next descendant as long
2562 * as both @pos and @cgroup are accessible and @pos is a descendant of
2563 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002564 */
Tejun Heo492eb212013-08-08 20:11:25 -04002565struct cgroup_subsys_state *
2566css_next_descendant_post(struct cgroup_subsys_state *pos,
2567 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002568{
Tejun Heo492eb212013-08-08 20:11:25 -04002569 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002570
Tejun Heoace2bee2014-02-11 11:52:47 -05002571 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002572
Tejun Heo58b79a92013-09-06 15:31:08 -04002573 /* if first iteration, visit leftmost descendant which may be @root */
2574 if (!pos)
2575 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002576
Tejun Heobd8815a2013-08-08 20:11:27 -04002577 /* if we visited @root, we're done */
2578 if (pos == root)
2579 return NULL;
2580
Tejun Heo574bd9f2012-11-09 09:12:29 -08002581 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04002582 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002583 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04002584 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002585
2586 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04002587 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002588}
Tejun Heo492eb212013-08-08 20:11:25 -04002589EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002590
Tejun Heo0942eee2013-08-08 20:11:26 -04002591/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002592 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04002593 * @it: the iterator to advance
2594 *
2595 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04002596 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002597static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04002598{
2599 struct list_head *l = it->cset_link;
2600 struct cgrp_cset_link *link;
2601 struct css_set *cset;
2602
2603 /* Advance to the next non-empty css_set */
2604 do {
2605 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04002606 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04002607 it->cset_link = NULL;
2608 return;
2609 }
2610 link = list_entry(l, struct cgrp_cset_link, cset_link);
2611 cset = link->cset;
2612 } while (list_empty(&cset->tasks));
2613 it->cset_link = l;
2614 it->task = cset->tasks.next;
2615}
2616
Tejun Heo0942eee2013-08-08 20:11:26 -04002617/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002618 * css_task_iter_start - initiate task iteration
2619 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04002620 * @it: the task iterator to use
2621 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002622 * Initiate iteration through the tasks of @css. The caller can call
2623 * css_task_iter_next() to walk through the tasks until the function
2624 * returns NULL. On completion of iteration, css_task_iter_end() must be
2625 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04002626 *
2627 * Note that this function acquires a lock which is released when the
2628 * iteration finishes. The caller can't sleep while iteration is in
2629 * progress.
2630 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002631void css_task_iter_start(struct cgroup_subsys_state *css,
2632 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002633 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002634{
Tejun Heo56fde9e2014-02-13 06:58:38 -05002635 /* no one should try to iterate before mounting cgroups */
2636 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002637
Paul Menage817929e2007-10-18 23:39:36 -07002638 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002639
Tejun Heo72ec7022013-08-08 20:11:26 -04002640 it->origin_css = css;
2641 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002642
Tejun Heo72ec7022013-08-08 20:11:26 -04002643 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002644}
2645
Tejun Heo0942eee2013-08-08 20:11:26 -04002646/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002647 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04002648 * @it: the task iterator being iterated
2649 *
2650 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04002651 * initialized via css_task_iter_start(). Returns NULL when the iteration
2652 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04002653 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002654struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002655{
2656 struct task_struct *res;
2657 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07002658 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002659
2660 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07002661 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07002662 return NULL;
2663 res = list_entry(l, struct task_struct, cg_list);
2664 /* Advance iterator to find next entry */
2665 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002666 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
2667 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04002668 /*
2669 * We reached the end of this task list - move on to the
2670 * next cgrp_cset_link.
2671 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002672 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002673 } else {
2674 it->task = l;
2675 }
2676 return res;
2677}
2678
Tejun Heo0942eee2013-08-08 20:11:26 -04002679/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002680 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04002681 * @it: the task iterator to finish
2682 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002683 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04002684 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002685void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002686 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002687{
2688 read_unlock(&css_set_lock);
2689}
2690
Cliff Wickman31a7df02008-02-07 00:14:42 -08002691static inline int started_after_time(struct task_struct *t1,
2692 struct timespec *time,
2693 struct task_struct *t2)
2694{
2695 int start_diff = timespec_compare(&t1->start_time, time);
2696 if (start_diff > 0) {
2697 return 1;
2698 } else if (start_diff < 0) {
2699 return 0;
2700 } else {
2701 /*
2702 * Arbitrarily, if two processes started at the same
2703 * time, we'll say that the lower pointer value
2704 * started first. Note that t2 may have exited by now
2705 * so this may not be a valid pointer any longer, but
2706 * that's fine - it still serves to distinguish
2707 * between two tasks started (effectively) simultaneously.
2708 */
2709 return t1 > t2;
2710 }
2711}
2712
2713/*
2714 * This function is a callback from heap_insert() and is used to order
2715 * the heap.
2716 * In this case we order the heap in descending task start time.
2717 */
2718static inline int started_after(void *p1, void *p2)
2719{
2720 struct task_struct *t1 = p1;
2721 struct task_struct *t2 = p2;
2722 return started_after_time(t1, &t2->start_time, t2);
2723}
2724
2725/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002726 * css_scan_tasks - iterate though all the tasks in a css
2727 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04002728 * @test: optional test callback
2729 * @process: process callback
2730 * @data: data passed to @test and @process
2731 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08002732 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002733 * Iterate through all the tasks in @css, calling @test for each, and if it
2734 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08002735 *
Tejun Heoe5358372013-08-08 20:11:26 -04002736 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04002737 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04002738 * lock css_set_lock for the call to @process.
2739 *
2740 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04002741 * of @css for the duration of this call. This function may or may not
2742 * call @process for tasks that exit or move to a different css during the
2743 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04002744 *
2745 * Note that @test may be called with locks held, and may in some
2746 * situations be called multiple times for the same task, so it should be
2747 * cheap.
2748 *
2749 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
2750 * heap operations (and its "gt" member will be overwritten), else a
2751 * temporary heap will be used (allocation of which may cause this function
2752 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08002753 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002754int css_scan_tasks(struct cgroup_subsys_state *css,
2755 bool (*test)(struct task_struct *, void *),
2756 void (*process)(struct task_struct *, void *),
2757 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002758{
2759 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04002760 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002761 struct task_struct *p, *dropped;
2762 /* Never dereference latest_task, since it's not refcounted */
2763 struct task_struct *latest_task = NULL;
2764 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002765 struct timespec latest_time = { 0, 0 };
2766
Tejun Heoe5358372013-08-08 20:11:26 -04002767 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08002768 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002769 heap->gt = &started_after;
2770 } else {
2771 /* We need to allocate our own heap memory */
2772 heap = &tmp_heap;
2773 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2774 if (retval)
2775 /* cannot allocate the heap */
2776 return retval;
2777 }
2778
2779 again:
2780 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04002781 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04002782 * which are of interest, and invoking @process callback on the
2783 * ones which need an update. Since we don't want to hold any
2784 * locks during the task updates, gather tasks to be processed in a
2785 * heap structure. The heap is sorted by descending task start
2786 * time. If the statically-sized heap fills up, we overflow tasks
2787 * that started later, and in future iterations only consider tasks
2788 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08002789 * guarantees forward progress and that we don't miss any tasks.
2790 */
2791 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04002792 css_task_iter_start(css, &it);
2793 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08002794 /*
2795 * Only affect tasks that qualify per the caller's callback,
2796 * if he provided one
2797 */
Tejun Heoe5358372013-08-08 20:11:26 -04002798 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002799 continue;
2800 /*
2801 * Only process tasks that started after the last task
2802 * we processed
2803 */
2804 if (!started_after_time(p, &latest_time, latest_task))
2805 continue;
2806 dropped = heap_insert(heap, p);
2807 if (dropped == NULL) {
2808 /*
2809 * The new task was inserted; the heap wasn't
2810 * previously full
2811 */
2812 get_task_struct(p);
2813 } else if (dropped != p) {
2814 /*
2815 * The new task was inserted, and pushed out a
2816 * different task
2817 */
2818 get_task_struct(p);
2819 put_task_struct(dropped);
2820 }
2821 /*
2822 * Else the new task was newer than anything already in
2823 * the heap and wasn't inserted
2824 */
2825 }
Tejun Heo72ec7022013-08-08 20:11:26 -04002826 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002827
2828 if (heap->size) {
2829 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002830 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002831 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002832 latest_time = q->start_time;
2833 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002834 }
2835 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04002836 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07002837 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002838 }
2839 /*
2840 * If we had to process any tasks at all, scan again
2841 * in case some of them were in the middle of forking
2842 * children that didn't get processed.
2843 * Not the most efficient way to do it, but it avoids
2844 * having to take callback_mutex in the fork path
2845 */
2846 goto again;
2847 }
2848 if (heap == &tmp_heap)
2849 heap_free(&tmp_heap);
2850 return 0;
2851}
2852
Tejun Heoe5358372013-08-08 20:11:26 -04002853static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07002854{
Tejun Heoe5358372013-08-08 20:11:26 -04002855 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07002856
Tejun Heo47cfcd02013-04-07 09:29:51 -07002857 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07002858 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002859 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07002860}
2861
2862/**
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 Heo72ec7022013-08-08 20:11:26 -04002869 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
2870 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07002871}
2872
Paul Menage817929e2007-10-18 23:39:36 -07002873/*
Ben Blum102a7752009-09-23 15:56:26 -07002874 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002875 *
2876 * Reading this file can return large amounts of data if a cgroup has
2877 * *lots* of attached tasks. So it may need several calls to read(),
2878 * but we cannot guarantee that the information we produce is correct
2879 * unless we produce it entirely atomically.
2880 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002881 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002882
Li Zefan24528252012-01-20 11:58:43 +08002883/* which pidlist file are we talking about? */
2884enum cgroup_filetype {
2885 CGROUP_FILE_PROCS,
2886 CGROUP_FILE_TASKS,
2887};
2888
2889/*
2890 * A pidlist is a list of pids that virtually represents the contents of one
2891 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
2892 * a pair (one each for procs, tasks) for each pid namespace that's relevant
2893 * to the cgroup.
2894 */
2895struct cgroup_pidlist {
2896 /*
2897 * used to find which pidlist is wanted. doesn't change as long as
2898 * this particular list stays in the list.
2899 */
2900 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
2901 /* array of xids */
2902 pid_t *list;
2903 /* how many elements the above list has */
2904 int length;
Li Zefan24528252012-01-20 11:58:43 +08002905 /* each of these stored in a list by its cgroup */
2906 struct list_head links;
2907 /* pointer to the cgroup we belong to, for list removal purposes */
2908 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05002909 /* for delayed destruction */
2910 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08002911};
2912
Paul Menagebbcb81d2007-10-18 23:39:32 -07002913/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002914 * The following two functions "fix" the issue where there are more pids
2915 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2916 * TODO: replace with a kernel-wide solution to this problem
2917 */
2918#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2919static void *pidlist_allocate(int count)
2920{
2921 if (PIDLIST_TOO_LARGE(count))
2922 return vmalloc(count * sizeof(pid_t));
2923 else
2924 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2925}
Tejun Heob1a21362013-11-29 10:42:58 -05002926
Ben Blumd1d9fd32009-09-23 15:56:28 -07002927static void pidlist_free(void *p)
2928{
2929 if (is_vmalloc_addr(p))
2930 vfree(p);
2931 else
2932 kfree(p);
2933}
Ben Blumd1d9fd32009-09-23 15:56:28 -07002934
2935/*
Tejun Heob1a21362013-11-29 10:42:58 -05002936 * Used to destroy all pidlists lingering waiting for destroy timer. None
2937 * should be left afterwards.
2938 */
2939static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
2940{
2941 struct cgroup_pidlist *l, *tmp_l;
2942
2943 mutex_lock(&cgrp->pidlist_mutex);
2944 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
2945 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
2946 mutex_unlock(&cgrp->pidlist_mutex);
2947
2948 flush_workqueue(cgroup_pidlist_destroy_wq);
2949 BUG_ON(!list_empty(&cgrp->pidlists));
2950}
2951
2952static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
2953{
2954 struct delayed_work *dwork = to_delayed_work(work);
2955 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
2956 destroy_dwork);
2957 struct cgroup_pidlist *tofree = NULL;
2958
2959 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05002960
2961 /*
Tejun Heo04502362013-11-29 10:42:59 -05002962 * Destroy iff we didn't get queued again. The state won't change
2963 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05002964 */
Tejun Heo04502362013-11-29 10:42:59 -05002965 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05002966 list_del(&l->links);
2967 pidlist_free(l->list);
2968 put_pid_ns(l->key.ns);
2969 tofree = l;
2970 }
2971
Tejun Heob1a21362013-11-29 10:42:58 -05002972 mutex_unlock(&l->owner->pidlist_mutex);
2973 kfree(tofree);
2974}
2975
2976/*
Ben Blum102a7752009-09-23 15:56:26 -07002977 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07002978 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002979 */
Li Zefan6ee211a2013-03-12 15:36:00 -07002980static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002981{
Ben Blum102a7752009-09-23 15:56:26 -07002982 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07002983
2984 /*
2985 * we presume the 0th element is unique, so i starts at 1. trivial
2986 * edge cases first; no work needs to be done for either
2987 */
2988 if (length == 0 || length == 1)
2989 return length;
2990 /* src and dest walk down the list; dest counts unique elements */
2991 for (src = 1; src < length; src++) {
2992 /* find next unique element */
2993 while (list[src] == list[src-1]) {
2994 src++;
2995 if (src == length)
2996 goto after;
2997 }
2998 /* dest always points to where the next unique element goes */
2999 list[dest] = list[src];
3000 dest++;
3001 }
3002after:
Ben Blum102a7752009-09-23 15:56:26 -07003003 return dest;
3004}
3005
Tejun Heoafb2bc12013-11-29 10:42:59 -05003006/*
3007 * The two pid files - task and cgroup.procs - guaranteed that the result
3008 * is sorted, which forced this whole pidlist fiasco. As pid order is
3009 * different per namespace, each namespace needs differently sorted list,
3010 * making it impossible to use, for example, single rbtree of member tasks
3011 * sorted by task pointer. As pidlists can be fairly large, allocating one
3012 * per open file is dangerous, so cgroup had to implement shared pool of
3013 * pidlists keyed by cgroup and namespace.
3014 *
3015 * All this extra complexity was caused by the original implementation
3016 * committing to an entirely unnecessary property. In the long term, we
3017 * want to do away with it. Explicitly scramble sort order if
3018 * sane_behavior so that no such expectation exists in the new interface.
3019 *
3020 * Scrambling is done by swapping every two consecutive bits, which is
3021 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3022 */
3023static pid_t pid_fry(pid_t pid)
3024{
3025 unsigned a = pid & 0x55555555;
3026 unsigned b = pid & 0xAAAAAAAA;
3027
3028 return (a << 1) | (b >> 1);
3029}
3030
3031static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3032{
3033 if (cgroup_sane_behavior(cgrp))
3034 return pid_fry(pid);
3035 else
3036 return pid;
3037}
3038
Ben Blum102a7752009-09-23 15:56:26 -07003039static int cmppid(const void *a, const void *b)
3040{
3041 return *(pid_t *)a - *(pid_t *)b;
3042}
3043
Tejun Heoafb2bc12013-11-29 10:42:59 -05003044static int fried_cmppid(const void *a, const void *b)
3045{
3046 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3047}
3048
Ben Blum72a8cb32009-09-23 15:56:27 -07003049static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3050 enum cgroup_filetype type)
3051{
3052 struct cgroup_pidlist *l;
3053 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003054 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003055
Tejun Heoe6b81712013-11-29 10:42:59 -05003056 lockdep_assert_held(&cgrp->pidlist_mutex);
3057
3058 list_for_each_entry(l, &cgrp->pidlists, links)
3059 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003060 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003061 return NULL;
3062}
3063
Ben Blum72a8cb32009-09-23 15:56:27 -07003064/*
3065 * find the appropriate pidlist for our purpose (given procs vs tasks)
3066 * returns with the lock on that pidlist already held, and takes care
3067 * of the use count, or returns NULL with no locks held if we're out of
3068 * memory.
3069 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003070static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3071 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003072{
3073 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003074
Tejun Heoe6b81712013-11-29 10:42:59 -05003075 lockdep_assert_held(&cgrp->pidlist_mutex);
3076
3077 l = cgroup_pidlist_find(cgrp, type);
3078 if (l)
3079 return l;
3080
Ben Blum72a8cb32009-09-23 15:56:27 -07003081 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003082 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003083 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003084 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003085
Tejun Heob1a21362013-11-29 10:42:58 -05003086 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003087 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003088 /* don't need task_nsproxy() if we're looking at ourself */
3089 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003090 l->owner = cgrp;
3091 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003092 return l;
3093}
3094
3095/*
Ben Blum102a7752009-09-23 15:56:26 -07003096 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3097 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003098static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3099 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003100{
3101 pid_t *array;
3102 int length;
3103 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003104 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003105 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003106 struct cgroup_pidlist *l;
3107
Tejun Heo4bac00d2013-11-29 10:42:59 -05003108 lockdep_assert_held(&cgrp->pidlist_mutex);
3109
Ben Blum102a7752009-09-23 15:56:26 -07003110 /*
3111 * If cgroup gets more users after we read count, we won't have
3112 * enough space - tough. This race is indistinguishable to the
3113 * caller from the case that the additional cgroup users didn't
3114 * show up until sometime later on.
3115 */
3116 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003117 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003118 if (!array)
3119 return -ENOMEM;
3120 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003121 css_task_iter_start(&cgrp->dummy_css, &it);
3122 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003123 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003124 break;
Ben Blum102a7752009-09-23 15:56:26 -07003125 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003126 if (type == CGROUP_FILE_PROCS)
3127 pid = task_tgid_vnr(tsk);
3128 else
3129 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003130 if (pid > 0) /* make sure to only use valid results */
3131 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003132 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003133 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003134 length = n;
3135 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003136 if (cgroup_sane_behavior(cgrp))
3137 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3138 else
3139 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003140 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003141 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003142
Tejun Heoe6b81712013-11-29 10:42:59 -05003143 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003144 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003145 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003146 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003147 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003148 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003149
3150 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003151 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003152 l->list = array;
3153 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003154 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003155 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003156}
3157
Balbir Singh846c7bb2007-10-18 23:39:44 -07003158/**
Li Zefana043e3b2008-02-23 15:24:09 -08003159 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003160 * @stats: cgroupstats to fill information into
3161 * @dentry: A dentry entry belonging to the cgroup for which stats have
3162 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003163 *
3164 * Build and fill cgroupstats so that taskstats can export it to user
3165 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003166 */
3167int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3168{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003169 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003170 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003171 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003172 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003173
Tejun Heo2bd59d42014-02-11 11:52:49 -05003174 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3175 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3176 kernfs_type(kn) != KERNFS_DIR)
3177 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003178
Tejun Heo2bd59d42014-02-11 11:52:49 -05003179 /*
3180 * We aren't being called from kernfs and there's no guarantee on
3181 * @kn->priv's validity. For this and css_tryget_from_dir(),
3182 * @kn->priv is RCU safe. Let's do the RCU dancing.
3183 */
3184 rcu_read_lock();
3185 cgrp = rcu_dereference(kn->priv);
3186 if (!cgrp) {
3187 rcu_read_unlock();
3188 return -ENOENT;
3189 }
Balbir Singh846c7bb2007-10-18 23:39:44 -07003190
Tejun Heo72ec7022013-08-08 20:11:26 -04003191 css_task_iter_start(&cgrp->dummy_css, &it);
3192 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003193 switch (tsk->state) {
3194 case TASK_RUNNING:
3195 stats->nr_running++;
3196 break;
3197 case TASK_INTERRUPTIBLE:
3198 stats->nr_sleeping++;
3199 break;
3200 case TASK_UNINTERRUPTIBLE:
3201 stats->nr_uninterruptible++;
3202 break;
3203 case TASK_STOPPED:
3204 stats->nr_stopped++;
3205 break;
3206 default:
3207 if (delayacct_is_task_waiting_on_io(tsk))
3208 stats->nr_io_wait++;
3209 break;
3210 }
3211 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003212 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003213
Tejun Heo2bd59d42014-02-11 11:52:49 -05003214 rcu_read_unlock();
3215 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003216}
3217
Paul Menage8f3ff202009-09-23 15:56:25 -07003218
Paul Menagecc31edc2008-10-18 20:28:04 -07003219/*
Ben Blum102a7752009-09-23 15:56:26 -07003220 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003221 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003222 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003223 */
3224
Ben Blum102a7752009-09-23 15:56:26 -07003225static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003226{
3227 /*
3228 * Initially we receive a position value that corresponds to
3229 * one more than the last pid shown (or 0 on the first call or
3230 * after a seek to the start). Use a binary-search to find the
3231 * next pid to display, if any
3232 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003233 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003234 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003235 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003236 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003237 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003238 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003239
Tejun Heo4bac00d2013-11-29 10:42:59 -05003240 mutex_lock(&cgrp->pidlist_mutex);
3241
3242 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003243 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003244 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003245 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003246 * could already have been destroyed.
3247 */
Tejun Heo5d224442013-12-05 12:28:04 -05003248 if (of->priv)
3249 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003250
3251 /*
3252 * Either this is the first start() after open or the matching
3253 * pidlist has been destroyed inbetween. Create a new one.
3254 */
Tejun Heo5d224442013-12-05 12:28:04 -05003255 if (!of->priv) {
3256 ret = pidlist_array_load(cgrp, type,
3257 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003258 if (ret)
3259 return ERR_PTR(ret);
3260 }
Tejun Heo5d224442013-12-05 12:28:04 -05003261 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003262
Paul Menagecc31edc2008-10-18 20:28:04 -07003263 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003264 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003265
Paul Menagecc31edc2008-10-18 20:28:04 -07003266 while (index < end) {
3267 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003268 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003269 index = mid;
3270 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003271 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003272 index = mid + 1;
3273 else
3274 end = mid;
3275 }
3276 }
3277 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003278 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003279 return NULL;
3280 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003281 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003282 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003283 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003284}
3285
Ben Blum102a7752009-09-23 15:56:26 -07003286static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003287{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003288 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003289 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003290
Tejun Heo5d224442013-12-05 12:28:04 -05003291 if (l)
3292 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003293 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003294 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003295}
3296
Ben Blum102a7752009-09-23 15:56:26 -07003297static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003298{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003299 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003300 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003301 pid_t *p = v;
3302 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003303 /*
3304 * Advance to the next pid in the array. If this goes off the
3305 * end, we're done
3306 */
3307 p++;
3308 if (p >= end) {
3309 return NULL;
3310 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003311 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003312 return p;
3313 }
3314}
3315
Ben Blum102a7752009-09-23 15:56:26 -07003316static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003317{
3318 return seq_printf(s, "%d\n", *(int *)v);
3319}
3320
Ben Blum102a7752009-09-23 15:56:26 -07003321/*
3322 * seq_operations functions for iterating on pidlists through seq_file -
3323 * independent of whether it's tasks or procs
3324 */
3325static const struct seq_operations cgroup_pidlist_seq_operations = {
3326 .start = cgroup_pidlist_start,
3327 .stop = cgroup_pidlist_stop,
3328 .next = cgroup_pidlist_next,
3329 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003330};
3331
Tejun Heo182446d2013-08-08 20:11:24 -04003332static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3333 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003334{
Tejun Heo182446d2013-08-08 20:11:24 -04003335 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003336}
3337
Tejun Heo182446d2013-08-08 20:11:24 -04003338static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3339 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003340{
Tejun Heo182446d2013-08-08 20:11:24 -04003341 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003342 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003343 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003344 else
Tejun Heo182446d2013-08-08 20:11:24 -04003345 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003346 return 0;
3347}
3348
Tejun Heo182446d2013-08-08 20:11:24 -04003349static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3350 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003351{
Tejun Heo182446d2013-08-08 20:11:24 -04003352 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003353}
3354
Tejun Heo182446d2013-08-08 20:11:24 -04003355static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3356 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003357{
3358 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003359 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003360 else
Tejun Heo182446d2013-08-08 20:11:24 -04003361 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003362 return 0;
3363}
3364
Tejun Heod5c56ce2013-06-03 19:14:34 -07003365static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003366 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003367 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003368 .seq_start = cgroup_pidlist_start,
3369 .seq_next = cgroup_pidlist_next,
3370 .seq_stop = cgroup_pidlist_stop,
3371 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003372 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003373 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003374 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003375 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003376 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003377 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003378 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003379 .read_u64 = cgroup_clone_children_read,
3380 .write_u64 = cgroup_clone_children_write,
3381 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003382 {
Tejun Heo873fe092013-04-14 20:15:26 -07003383 .name = "cgroup.sane_behavior",
3384 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003385 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003386 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003387
3388 /*
3389 * Historical crazy stuff. These don't have "cgroup." prefix and
3390 * don't exist if sane_behavior. If you're depending on these, be
3391 * prepared to be burned.
3392 */
3393 {
3394 .name = "tasks",
3395 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003396 .seq_start = cgroup_pidlist_start,
3397 .seq_next = cgroup_pidlist_next,
3398 .seq_stop = cgroup_pidlist_stop,
3399 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003400 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003401 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003402 .mode = S_IRUGO | S_IWUSR,
3403 },
3404 {
3405 .name = "notify_on_release",
3406 .flags = CFTYPE_INSANE,
3407 .read_u64 = cgroup_read_notify_on_release,
3408 .write_u64 = cgroup_write_notify_on_release,
3409 },
Tejun Heo873fe092013-04-14 20:15:26 -07003410 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003411 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003412 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003413 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003414 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003415 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003416 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003417 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003418};
3419
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003420/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003421 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003422 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003423 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003424 *
3425 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003426 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003427static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003428{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003429 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003430 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003431
Tejun Heo8e3f6542012-04-01 12:09:55 -07003432 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003433 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05003434 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07003435
3436 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003437 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003438
Tejun Heo0adb0702014-02-12 09:29:48 -05003439 list_for_each_entry(cfts, &ss->cfts, node) {
3440 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003441 if (ret < 0)
3442 goto err;
3443 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003444 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003445 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003446err:
3447 cgroup_clear_dir(cgrp, subsys_mask);
3448 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003449}
3450
Tejun Heo0c21ead2013-08-13 20:22:51 -04003451/*
3452 * css destruction is four-stage process.
3453 *
3454 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3455 * Implemented in kill_css().
3456 *
3457 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3458 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3459 * by invoking offline_css(). After offlining, the base ref is put.
3460 * Implemented in css_killed_work_fn().
3461 *
3462 * 3. When the percpu_ref reaches zero, the only possible remaining
3463 * accessors are inside RCU read sections. css_release() schedules the
3464 * RCU callback.
3465 *
3466 * 4. After the grace period, the css can be freed. Implemented in
3467 * css_free_work_fn().
3468 *
3469 * It is actually hairier because both step 2 and 4 require process context
3470 * and thus involve punting to css->destroy_work adding two additional
3471 * steps to the already complex sequence.
3472 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003473static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003474{
3475 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003476 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003477 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003478
Tejun Heo0ae78e02013-08-13 11:01:54 -04003479 if (css->parent)
3480 css_put(css->parent);
3481
Tejun Heo0c21ead2013-08-13 20:22:51 -04003482 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003483 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003484}
3485
3486static void css_free_rcu_fn(struct rcu_head *rcu_head)
3487{
3488 struct cgroup_subsys_state *css =
3489 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3490
Tejun Heo0c21ead2013-08-13 20:22:51 -04003491 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003492 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003493}
3494
Tejun Heod3daf282013-06-13 19:39:16 -07003495static void css_release(struct percpu_ref *ref)
3496{
3497 struct cgroup_subsys_state *css =
3498 container_of(ref, struct cgroup_subsys_state, refcnt);
3499
Tejun Heoaec25022014-02-08 10:36:58 -05003500 rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003501 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003502}
3503
Tejun Heo623f9262013-08-13 11:01:55 -04003504static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3505 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003506{
Paul Menagebd89aab2007-10-18 23:40:44 -07003507 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003508 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003509 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003510
Tejun Heo0ae78e02013-08-13 11:01:54 -04003511 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003512 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003513 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003514 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003515
Tejun Heoca8bdca2013-08-26 18:40:56 -04003516 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003517}
3518
Li Zefan2a4ac632013-07-31 16:16:40 +08003519/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04003520static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003521{
Tejun Heo623f9262013-08-13 11:01:55 -04003522 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08003523 int ret = 0;
3524
Tejun Heoace2bee2014-02-11 11:52:47 -05003525 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003526 lockdep_assert_held(&cgroup_mutex);
3527
Tejun Heo92fb9742012-11-19 08:13:38 -08003528 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04003529 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003530 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04003531 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04003532 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05003533 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003534 }
Tejun Heob1929db2012-11-19 08:13:38 -08003535 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003536}
3537
Li Zefan2a4ac632013-07-31 16:16:40 +08003538/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04003539static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003540{
Tejun Heo623f9262013-08-13 11:01:55 -04003541 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003542
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
3546 if (!(css->flags & CSS_ONLINE))
3547 return;
3548
Li Zefand7eeac12013-03-12 15:35:59 -07003549 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04003550 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003551
Tejun Heoeb954192013-08-08 20:11:23 -04003552 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04003553 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05003554 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003555}
3556
Tejun Heoc81c925a2013-12-06 15:11:56 -05003557/**
3558 * create_css - create a cgroup_subsys_state
3559 * @cgrp: the cgroup new css will be associated with
3560 * @ss: the subsys of new css
3561 *
3562 * Create a new css associated with @cgrp - @ss pair. On success, the new
3563 * css is online and installed in @cgrp with all interface files created.
3564 * Returns 0 on success, -errno on failure.
3565 */
3566static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
3567{
3568 struct cgroup *parent = cgrp->parent;
3569 struct cgroup_subsys_state *css;
3570 int err;
3571
Tejun Heoc81c925a2013-12-06 15:11:56 -05003572 lockdep_assert_held(&cgroup_mutex);
3573
3574 css = ss->css_alloc(cgroup_css(parent, ss));
3575 if (IS_ERR(css))
3576 return PTR_ERR(css);
3577
3578 err = percpu_ref_init(&css->refcnt, css_release);
3579 if (err)
3580 goto err_free;
3581
3582 init_css(css, ss, cgrp);
3583
Tejun Heoaec25022014-02-08 10:36:58 -05003584 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003585 if (err)
3586 goto err_free;
3587
3588 err = online_css(css);
3589 if (err)
3590 goto err_free;
3591
Tejun Heo59f52962014-02-11 11:52:49 -05003592 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003593 css_get(css->parent);
3594
3595 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
3596 parent->parent) {
3597 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",
3598 current->comm, current->pid, ss->name);
3599 if (!strcmp(ss->name, "memory"))
3600 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
3601 ss->warned_broken_hierarchy = true;
3602 }
3603
3604 return 0;
3605
3606err_free:
3607 percpu_ref_cancel_init(&css->refcnt);
3608 ss->css_free(css);
3609 return err;
3610}
3611
Tejun Heo2bd59d42014-02-11 11:52:49 -05003612/**
Li Zefana043e3b2008-02-23 15:24:09 -08003613 * cgroup_create - create a cgroup
3614 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05003615 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05003616 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07003617 */
Tejun Heoe61734c2014-02-12 09:29:50 -05003618static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003619 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003620{
Paul Menagebd89aab2007-10-18 23:40:44 -07003621 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003622 struct cgroupfs_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05003623 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003624 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003625 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003626
Tejun Heo0a950f62012-11-19 09:02:12 -08003627 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07003628 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3629 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003630 return -ENOMEM;
3631
Tejun Heoace2bee2014-02-11 11:52:47 -05003632 mutex_lock(&cgroup_tree_mutex);
3633
Li Zefan4e96ee82013-07-31 09:50:50 +08003634 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08003635 * Only live parents can have children. Note that the liveliness
3636 * check isn't strictly necessary because cgroup_mkdir() and
3637 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
3638 * anyway so that locking is contained inside cgroup proper and we
3639 * don't get nasty surprises if we ever grow another caller.
3640 */
3641 if (!cgroup_lock_live_group(parent)) {
3642 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05003643 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08003644 }
3645
3646 /*
3647 * Temporarily set the pointer to NULL, so idr_find() won't return
3648 * a half-baked cgroup.
3649 */
3650 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
3651 if (cgrp->id < 0) {
3652 err = -ENOMEM;
3653 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08003654 }
3655
Paul Menagecc31edc2008-10-18 20:28:04 -07003656 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003657
Paul Menagebd89aab2007-10-18 23:40:44 -07003658 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003659 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07003660 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003661
Li Zefanb6abdb02008-03-04 14:28:19 -08003662 if (notify_on_release(parent))
3663 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3664
Tejun Heo2260e7f2012-11-19 08:13:38 -08003665 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
3666 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003667
Tejun Heo2bd59d42014-02-11 11:52:49 -05003668 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05003669 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003670 if (IS_ERR(kn)) {
3671 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003672 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003673 }
3674 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003675
Tejun Heo6f305582014-02-12 09:29:50 -05003676 /*
3677 * This extra ref will be put in cgroup_free_fn() and guarantees
3678 * that @cgrp->kn is always accessible.
3679 */
3680 kernfs_get(kn);
3681
Tejun Heo00356bd2013-06-18 11:14:22 -07003682 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09003683
Tejun Heo4e139af2012-11-19 08:13:36 -08003684 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08003685 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05003686 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05003687 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08003688
Tejun Heo0d802552013-12-06 15:11:56 -05003689 /*
3690 * @cgrp is now fully operational. If something fails after this
3691 * point, it'll be released via the normal destruction path.
3692 */
Li Zefan4e96ee82013-07-31 09:50:50 +08003693 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
3694
Tejun Heo2bb566c2013-08-08 20:11:23 -04003695 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07003696 if (err)
3697 goto err_destroy;
3698
Tejun Heo9d403e92013-12-06 15:11:56 -05003699 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05003700 for_each_subsys(ss, ssid) {
3701 if (root->subsys_mask & (1 << ssid)) {
3702 err = create_css(cgrp, ss);
3703 if (err)
3704 goto err_destroy;
3705 }
Tejun Heoa8638032012-11-09 09:12:29 -08003706 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003707
Tejun Heo2bd59d42014-02-11 11:52:49 -05003708 kernfs_activate(kn);
3709
Paul Menageddbcc7e2007-10-18 23:39:30 -07003710 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003711 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003712
3713 return 0;
3714
Tejun Heo0a950f62012-11-19 09:02:12 -08003715err_free_id:
Li Zefan4e96ee82013-07-31 09:50:50 +08003716 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003717err_unlock:
3718 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003719err_unlock_tree:
3720 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003721 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003722 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08003723
3724err_destroy:
3725 cgroup_destroy_locked(cgrp);
3726 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003727 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08003728 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003729}
3730
Tejun Heo2bd59d42014-02-11 11:52:49 -05003731static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3732 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003733{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003734 struct cgroup *parent = parent_kn->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003735
Tejun Heo2bd59d42014-02-11 11:52:49 -05003736 return cgroup_create(parent, name, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003737}
3738
Tejun Heo223dbc32013-08-13 20:22:50 -04003739/*
3740 * This is called when the refcnt of a css is confirmed to be killed.
3741 * css_tryget() is now guaranteed to fail.
3742 */
3743static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07003744{
Tejun Heo223dbc32013-08-13 20:22:50 -04003745 struct cgroup_subsys_state *css =
3746 container_of(work, struct cgroup_subsys_state, destroy_work);
3747 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07003748
Tejun Heoace2bee2014-02-11 11:52:47 -05003749 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003750 mutex_lock(&cgroup_mutex);
3751
3752 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04003753 * css_tryget() is guaranteed to fail now. Tell subsystems to
3754 * initate destruction.
3755 */
3756 offline_css(css);
3757
3758 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003759 * If @cgrp is marked dead, it's waiting for refs of all css's to
3760 * be disabled before proceeding to the second phase of cgroup
3761 * destruction. If we are the last one, kick it off.
3762 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04003763 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04003764 cgroup_destroy_css_killed(cgrp);
3765
3766 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003767 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04003768
3769 /*
3770 * Put the css refs from kill_css(). Each css holds an extra
3771 * reference to the cgroup's dentry and cgroup removal proceeds
3772 * regardless of css refs. On the last put of each css, whenever
3773 * that may be, the extra dentry ref is put so that dentry
3774 * destruction happens only after all css's are released.
3775 */
3776 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07003777}
3778
Tejun Heo223dbc32013-08-13 20:22:50 -04003779/* css kill confirmation processing requires process context, bounce */
3780static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07003781{
3782 struct cgroup_subsys_state *css =
3783 container_of(ref, struct cgroup_subsys_state, refcnt);
3784
Tejun Heo223dbc32013-08-13 20:22:50 -04003785 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003786 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07003787}
3788
3789/**
Tejun Heoedae0c32013-08-13 20:22:51 -04003790 * kill_css - destroy a css
3791 * @css: css to destroy
3792 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003793 * This function initiates destruction of @css by removing cgroup interface
3794 * files and putting its base reference. ->css_offline() will be invoked
3795 * asynchronously once css_tryget() is guaranteed to fail and when the
3796 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04003797 */
3798static void kill_css(struct cgroup_subsys_state *css)
3799{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003800 /*
3801 * This must happen before css is disassociated with its cgroup.
3802 * See seq_css() for details.
3803 */
Tejun Heoaec25022014-02-08 10:36:58 -05003804 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003805
Tejun Heoedae0c32013-08-13 20:22:51 -04003806 /*
3807 * Killing would put the base ref, but we need to keep it alive
3808 * until after ->css_offline().
3809 */
3810 css_get(css);
3811
3812 /*
3813 * cgroup core guarantees that, by the time ->css_offline() is
3814 * invoked, no new css reference will be given out via
3815 * css_tryget(). We can't simply call percpu_ref_kill() and
3816 * proceed to offlining css's because percpu_ref_kill() doesn't
3817 * guarantee that the ref is seen as killed on all CPUs on return.
3818 *
3819 * Use percpu_ref_kill_and_confirm() to get notifications as each
3820 * css is confirmed to be seen as killed on all CPUs.
3821 */
3822 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003823}
3824
3825/**
3826 * cgroup_destroy_locked - the first stage of cgroup destruction
3827 * @cgrp: cgroup to be destroyed
3828 *
3829 * css's make use of percpu refcnts whose killing latency shouldn't be
3830 * exposed to userland and are RCU protected. Also, cgroup core needs to
3831 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
3832 * invoked. To satisfy all the requirements, destruction is implemented in
3833 * the following two steps.
3834 *
3835 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
3836 * userland visible parts and start killing the percpu refcnts of
3837 * css's. Set up so that the next stage will be kicked off once all
3838 * the percpu refcnts are confirmed to be killed.
3839 *
3840 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
3841 * rest of destruction. Once all cgroup references are gone, the
3842 * cgroup is RCU-freed.
3843 *
3844 * This function implements s1. After this step, @cgrp is gone as far as
3845 * the userland is concerned and a new cgroup with the same name may be
3846 * created. As cgroup doesn't care about the names internally, this
3847 * doesn't cause any problem.
3848 */
Tejun Heo42809dd2012-11-19 08:13:37 -08003849static int cgroup_destroy_locked(struct cgroup *cgrp)
3850 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003851{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003852 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003853 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07003854 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05003855 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003856
Tejun Heoace2bee2014-02-11 11:52:47 -05003857 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003858 lockdep_assert_held(&cgroup_mutex);
3859
Tejun Heoddd69142013-06-12 21:04:54 -07003860 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07003861 * css_set_lock synchronizes access to ->cset_links and prevents
3862 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07003863 */
3864 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003865 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07003866 read_unlock(&css_set_lock);
3867 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003868 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08003869
Tejun Heo1a90dd52012-11-05 09:16:59 -08003870 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003871 * Make sure there's no live children. We can't test ->children
3872 * emptiness as dead children linger on it while being destroyed;
3873 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
3874 */
3875 empty = true;
3876 rcu_read_lock();
3877 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
3878 empty = cgroup_is_dead(child);
3879 if (!empty)
3880 break;
3881 }
3882 rcu_read_unlock();
3883 if (!empty)
3884 return -EBUSY;
3885
3886 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04003887 * Initiate massacre of all css's. cgroup_destroy_css_killed()
3888 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05003889 * percpu refs of all css's are confirmed to be killed. This
3890 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08003891 */
Tejun Heo4ac06012014-02-11 11:52:47 -05003892 mutex_unlock(&cgroup_mutex);
Tejun Heo1c6727a2013-12-06 15:11:56 -05003893 for_each_css(css, ssid, cgrp)
3894 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05003895 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07003896
3897 /*
3898 * Mark @cgrp dead. This prevents further task migration and child
3899 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04003900 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07003901 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04003902 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07003903 */
Tejun Heo54766d42013-06-12 21:04:53 -07003904 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08003905
Tejun Heo455050d2013-06-13 19:27:41 -07003906 /* CGRP_DEAD is set, remove from ->release_list for the last time */
3907 raw_spin_lock(&release_list_lock);
3908 if (!list_empty(&cgrp->release_list))
3909 list_del_init(&cgrp->release_list);
3910 raw_spin_unlock(&release_list_lock);
3911
3912 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003913 * If @cgrp has css's attached, the second stage of cgroup
3914 * destruction is kicked off from css_killed_work_fn() after the
3915 * refs of all attached css's are killed. If @cgrp doesn't have
3916 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07003917 */
Tejun Heof20104d2013-08-13 20:22:50 -04003918 if (!cgrp->nr_css)
3919 cgroup_destroy_css_killed(cgrp);
3920
Tejun Heo2bd59d42014-02-11 11:52:49 -05003921 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05003922 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003923
3924 /*
3925 * There are two control paths which try to determine cgroup from
3926 * dentry without going through kernfs - cgroupstats_build() and
3927 * css_tryget_from_dir(). Those are supported by RCU protecting
3928 * clearing of cgrp->kn->priv backpointer, which should happen
3929 * after all files under it have been removed.
3930 */
Tejun Heo6f305582014-02-12 09:29:50 -05003931 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003932 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003933
Tejun Heo4ac06012014-02-11 11:52:47 -05003934 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07003935
Tejun Heoea15f8c2013-06-13 19:27:42 -07003936 return 0;
3937};
3938
Tejun Heod3daf282013-06-13 19:39:16 -07003939/**
Tejun Heof20104d2013-08-13 20:22:50 -04003940 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07003941 * @work: cgroup->destroy_free_work
3942 *
3943 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04003944 * destroyed after all css's are offlined and performs the rest of
3945 * destruction. This is the second step of destruction described in the
3946 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07003947 */
Tejun Heof20104d2013-08-13 20:22:50 -04003948static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07003949{
Tejun Heoea15f8c2013-06-13 19:27:42 -07003950 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07003951
Tejun Heoace2bee2014-02-11 11:52:47 -05003952 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003953 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003954
Paul Menage999cd8a2009-01-07 18:08:36 -08003955 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08003956 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07003957
Tejun Heo59f52962014-02-11 11:52:49 -05003958 cgroup_put(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003959
Paul Menagebd89aab2007-10-18 23:40:44 -07003960 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003961 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003962}
3963
Tejun Heo2bd59d42014-02-11 11:52:49 -05003964static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08003965{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003966 struct cgroup *cgrp = kn->priv;
3967 int ret = 0;
3968
3969 /*
3970 * This is self-destruction but @kn can't be removed while this
3971 * callback is in progress. Let's break active protection. Once
3972 * the protection is broken, @cgrp can be destroyed at any point.
3973 * Pin it so that it stays accessible.
3974 */
3975 cgroup_get(cgrp);
3976 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08003977
Tejun Heoace2bee2014-02-11 11:52:47 -05003978 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003979 mutex_lock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003980
3981 /*
3982 * @cgrp might already have been destroyed while we're trying to
3983 * grab the mutexes.
3984 */
3985 if (!cgroup_is_dead(cgrp))
3986 ret = cgroup_destroy_locked(cgrp);
3987
Tejun Heo42809dd2012-11-19 08:13:37 -08003988 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003989 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003990
Tejun Heo2bd59d42014-02-11 11:52:49 -05003991 kernfs_unbreak_active_protection(kn);
3992 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08003993 return ret;
3994}
3995
Tejun Heo2bd59d42014-02-11 11:52:49 -05003996static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
3997 .remount_fs = cgroup_remount,
3998 .show_options = cgroup_show_options,
3999 .mkdir = cgroup_mkdir,
4000 .rmdir = cgroup_rmdir,
4001 .rename = cgroup_rename,
4002};
4003
Li Zefan06a11922008-04-29 01:00:07 -07004004static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004005{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004006 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004007
4008 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004009
Tejun Heoace2bee2014-02-11 11:52:47 -05004010 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004011 mutex_lock(&cgroup_mutex);
4012
Tejun Heo0adb0702014-02-12 09:29:48 -05004013 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004014
Paul Menageddbcc7e2007-10-18 23:39:30 -07004015 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004016 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004017 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004018 /* We don't handle early failures gracefully */
4019 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004020 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004021
Li Zefane8d55fd2008-04-29 01:00:13 -07004022 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004023 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004024 * newly registered, all tasks and hence the
4025 * init_css_set is in the subsystem's top cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004026 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004027
4028 need_forkexit_callback |= ss->fork || ss->exit;
4029
Li Zefane8d55fd2008-04-29 01:00:13 -07004030 /* At system boot, before all subsystems have been
4031 * registered, no tasks have been forked, so we don't
4032 * need to invoke fork callbacks here. */
4033 BUG_ON(!list_empty(&init_task.tasks));
4034
Tejun Heoae7f1642013-08-13 20:22:50 -04004035 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004036
Tejun Heo648bb562012-11-19 08:13:36 -08004037 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004038 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004039}
4040
4041/**
Li Zefana043e3b2008-02-23 15:24:09 -08004042 * cgroup_init_early - cgroup initialization at system boot
4043 *
4044 * Initialize cgroups at system boot, and initialize any
4045 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004046 */
4047int __init cgroup_init_early(void)
4048{
Tejun Heo30159ec2013-06-25 11:53:37 -07004049 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004050 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004051
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004052 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004053 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004054 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004055 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004056 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004057 init_cgroup_root(&cgroup_dummy_root);
4058 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004059 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004060
Tejun Heo69d02062013-06-12 21:04:50 -07004061 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004062 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4063 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004064 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004065
Tejun Heo3ed80a62014-02-08 10:36:58 -05004066 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004067 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004068 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4069 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004070 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004071 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4072 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4073
Tejun Heoaec25022014-02-08 10:36:58 -05004074 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004075 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004076
4077 if (ss->early_init)
4078 cgroup_init_subsys(ss);
4079 }
4080 return 0;
4081}
4082
4083/**
Li Zefana043e3b2008-02-23 15:24:09 -08004084 * cgroup_init - cgroup initialization
4085 *
4086 * Register cgroup filesystem and /proc file, and initialize
4087 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004088 */
4089int __init cgroup_init(void)
4090{
Tejun Heo30159ec2013-06-25 11:53:37 -07004091 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004092 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004093 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004094
Tejun Heo2bd59d42014-02-11 11:52:49 -05004095 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Tejun Heo2da440a2014-02-11 11:52:48 -05004096
Tejun Heo3ed80a62014-02-08 10:36:58 -05004097 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004098 if (!ss->early_init)
4099 cgroup_init_subsys(ss);
Tejun Heode00ffa2014-02-11 11:52:48 -05004100
4101 /*
4102 * cftype registration needs kmalloc and can't be done
4103 * during early_init. Register base cftypes separately.
4104 */
4105 if (ss->base_cftypes)
4106 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004107 }
4108
Tejun Heofa3ca072013-04-14 11:36:56 -07004109 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004110 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004111
Tejun Heo82fe9b02013-06-25 11:53:37 -07004112 /* Add init_css_set to the hash table */
4113 key = css_set_hash(init_css_set.subsys);
4114 hash_add(css_set_table, &init_css_set.hlist, key);
4115
Tejun Heofc76df72013-06-25 11:53:37 -07004116 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004117
Li Zefan4e96ee82013-07-31 09:50:50 +08004118 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4119 0, 1, GFP_KERNEL);
4120 BUG_ON(err < 0);
4121
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004122 mutex_unlock(&cgroup_mutex);
4123
Greg KH676db4a2010-08-05 13:53:35 -07004124 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004125 if (!cgroup_kobj)
4126 return -ENOMEM;
Greg KH676db4a2010-08-05 13:53:35 -07004127
4128 err = register_filesystem(&cgroup_fs_type);
4129 if (err < 0) {
4130 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004131 return err;
Greg KH676db4a2010-08-05 13:53:35 -07004132 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004133
Li Zefan46ae2202008-04-29 01:00:08 -07004134 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004135 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004136}
Paul Menageb4f48b62007-10-18 23:39:33 -07004137
Tejun Heoe5fca242013-11-22 17:14:39 -05004138static int __init cgroup_wq_init(void)
4139{
4140 /*
4141 * There isn't much point in executing destruction path in
4142 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004143 *
4144 * XXX: Must be ordered to make sure parent is offlined after
4145 * children. The ordering requirement is for memcg where a
4146 * parent's offline may wait for a child's leading to deadlock. In
4147 * the long term, this should be fixed from memcg side.
Tejun Heoe5fca242013-11-22 17:14:39 -05004148 *
4149 * We would prefer to do this in cgroup_init() above, but that
4150 * is called before init_workqueues(): so leave this until after.
4151 */
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004152 cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0);
Tejun Heoe5fca242013-11-22 17:14:39 -05004153 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004154
4155 /*
4156 * Used to destroy pidlists and separate to serve as flush domain.
4157 * Cap @max_active to 1 too.
4158 */
4159 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4160 0, 1);
4161 BUG_ON(!cgroup_pidlist_destroy_wq);
4162
Tejun Heoe5fca242013-11-22 17:14:39 -05004163 return 0;
4164}
4165core_initcall(cgroup_wq_init);
4166
Paul Menagea4243162007-10-18 23:39:35 -07004167/*
4168 * proc_cgroup_show()
4169 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4170 * - Used for /proc/<pid>/cgroup.
4171 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4172 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004173 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004174 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4175 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4176 * cgroup to top_cgroup.
4177 */
4178
4179/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004180int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004181{
4182 struct pid *pid;
4183 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004184 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004185 int retval;
4186 struct cgroupfs_root *root;
4187
4188 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004189 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004190 if (!buf)
4191 goto out;
4192
4193 retval = -ESRCH;
4194 pid = m->private;
4195 tsk = get_pid_task(pid, PIDTYPE_PID);
4196 if (!tsk)
4197 goto out_free;
4198
4199 retval = 0;
4200
4201 mutex_lock(&cgroup_mutex);
4202
Li Zefane5f6a862009-01-07 18:07:41 -08004203 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004204 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004205 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004206 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004207
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004208 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004209 for_each_subsys(ss, ssid)
4210 if (root->subsys_mask & (1 << ssid))
4211 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004212 if (strlen(root->name))
4213 seq_printf(m, "%sname=%s", count ? "," : "",
4214 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004215 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004216 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004217 path = cgroup_path(cgrp, buf, PATH_MAX);
4218 if (!path) {
4219 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004220 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004221 }
4222 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004223 seq_putc(m, '\n');
4224 }
4225
4226out_unlock:
4227 mutex_unlock(&cgroup_mutex);
4228 put_task_struct(tsk);
4229out_free:
4230 kfree(buf);
4231out:
4232 return retval;
4233}
4234
Paul Menagea4243162007-10-18 23:39:35 -07004235/* Display information about each subsystem and each hierarchy */
4236static int proc_cgroupstats_show(struct seq_file *m, void *v)
4237{
Tejun Heo30159ec2013-06-25 11:53:37 -07004238 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004239 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004240
Paul Menage8bab8dd2008-04-04 14:29:57 -07004241 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004242 /*
4243 * ideally we don't want subsystems moving around while we do this.
4244 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4245 * subsys/hierarchy state.
4246 */
Paul Menagea4243162007-10-18 23:39:35 -07004247 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004248
4249 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004250 seq_printf(m, "%s\t%d\t%d\t%d\n",
4251 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004252 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004253
Paul Menagea4243162007-10-18 23:39:35 -07004254 mutex_unlock(&cgroup_mutex);
4255 return 0;
4256}
4257
4258static int cgroupstats_open(struct inode *inode, struct file *file)
4259{
Al Viro9dce07f2008-03-29 03:07:28 +00004260 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004261}
4262
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004263static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004264 .open = cgroupstats_open,
4265 .read = seq_read,
4266 .llseek = seq_lseek,
4267 .release = single_release,
4268};
4269
Paul Menageb4f48b62007-10-18 23:39:33 -07004270/**
4271 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004272 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004273 *
4274 * Description: A task inherits its parent's cgroup at fork().
4275 *
4276 * A pointer to the shared css_set was automatically copied in
4277 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004278 * it was not made under the protection of RCU or cgroup_mutex, so
4279 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4280 * have already changed current->cgroups, allowing the previously
4281 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004282 *
4283 * At the point that cgroup_fork() is called, 'current' is the parent
4284 * task, and the passed argument 'child' points to the child task.
4285 */
4286void cgroup_fork(struct task_struct *child)
4287{
Tejun Heo9bb71302012-10-18 17:52:07 -07004288 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004289 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004290 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004291 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004292 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004293}
4294
4295/**
Li Zefana043e3b2008-02-23 15:24:09 -08004296 * cgroup_post_fork - called on a new task after adding it to the task list
4297 * @child: the task in question
4298 *
Tejun Heo5edee612012-10-16 15:03:14 -07004299 * Adds the task to the list running through its css_set if necessary and
4300 * call the subsystem fork() callbacks. Has to be after the task is
4301 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004302 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004303 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004304 */
Paul Menage817929e2007-10-18 23:39:36 -07004305void cgroup_post_fork(struct task_struct *child)
4306{
Tejun Heo30159ec2013-06-25 11:53:37 -07004307 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004308 int i;
4309
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004310 /*
4311 * use_task_css_set_links is set to 1 before we walk the tasklist
4312 * under the tasklist_lock and we read it here after we added the child
4313 * to the tasklist under the tasklist_lock as well. If the child wasn't
4314 * yet in the tasklist when we walked through it from
4315 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4316 * should be visible now due to the paired locking and barriers implied
4317 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4318 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4319 * lock on fork.
4320 */
Paul Menage817929e2007-10-18 23:39:36 -07004321 if (use_task_css_set_links) {
4322 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004323 task_lock(child);
4324 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004325 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004326 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004327 write_unlock(&css_set_lock);
4328 }
Tejun Heo5edee612012-10-16 15:03:14 -07004329
4330 /*
4331 * Call ss->fork(). This must happen after @child is linked on
4332 * css_set; otherwise, @child might change state between ->fork()
4333 * and addition to css_set.
4334 */
4335 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004336 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004337 if (ss->fork)
4338 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004339 }
Paul Menage817929e2007-10-18 23:39:36 -07004340}
Tejun Heo5edee612012-10-16 15:03:14 -07004341
Paul Menage817929e2007-10-18 23:39:36 -07004342/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004343 * cgroup_exit - detach cgroup from exiting task
4344 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004345 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004346 *
4347 * Description: Detach cgroup from @tsk and release it.
4348 *
4349 * Note that cgroups marked notify_on_release force every task in
4350 * them to take the global cgroup_mutex mutex when exiting.
4351 * This could impact scaling on very large systems. Be reluctant to
4352 * use notify_on_release cgroups where very high task exit scaling
4353 * is required on large systems.
4354 *
4355 * the_top_cgroup_hack:
4356 *
4357 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4358 *
4359 * We call cgroup_exit() while the task is still competent to
4360 * handle notify_on_release(), then leave the task attached to the
4361 * root cgroup in each hierarchy for the remainder of its exit.
4362 *
4363 * To do this properly, we would increment the reference count on
4364 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4365 * code we would add a second cgroup function call, to drop that
4366 * reference. This would just create an unnecessary hot spot on
4367 * the top_cgroup reference count, to no avail.
4368 *
4369 * Normally, holding a reference to a cgroup without bumping its
4370 * count is unsafe. The cgroup could go away, or someone could
4371 * attach us to a different cgroup, decrementing the count on
4372 * the first cgroup that we never incremented. But in this case,
4373 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004374 * which wards off any cgroup_attach_task() attempts, or task is a failed
4375 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004376 */
4377void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4378{
Tejun Heo30159ec2013-06-25 11:53:37 -07004379 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004380 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004381 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004382
4383 /*
4384 * Unlink from the css_set task list if necessary.
4385 * Optimistically check cg_list before taking
4386 * css_set_lock
4387 */
4388 if (!list_empty(&tsk->cg_list)) {
4389 write_lock(&css_set_lock);
4390 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004391 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004392 write_unlock(&css_set_lock);
4393 }
4394
Paul Menageb4f48b62007-10-18 23:39:33 -07004395 /* Reassign the task to the init_css_set. */
4396 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004397 cset = task_css_set(tsk);
4398 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004399
4400 if (run_callbacks && need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004401 /* see cgroup_post_fork() for details */
4402 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004403 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004404 struct cgroup_subsys_state *old_css = cset->subsys[i];
4405 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004406
Tejun Heoeb954192013-08-08 20:11:23 -04004407 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004408 }
4409 }
4410 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004411 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004412
Tejun Heo5abb8852013-06-12 21:04:49 -07004413 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07004414}
Paul Menage697f4162007-10-18 23:39:34 -07004415
Paul Menagebd89aab2007-10-18 23:40:44 -07004416static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004417{
Li Zefanf50daa72013-03-01 15:06:07 +08004418 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004419 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004420 /*
4421 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004422 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004423 * it now
4424 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004425 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004426
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004427 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004428 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004429 list_empty(&cgrp->release_list)) {
4430 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004431 need_schedule_work = 1;
4432 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004433 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004434 if (need_schedule_work)
4435 schedule_work(&release_agent_work);
4436 }
4437}
4438
Paul Menage81a6a5c2007-10-18 23:39:38 -07004439/*
4440 * Notify userspace when a cgroup is released, by running the
4441 * configured release agent with the name of the cgroup (path
4442 * relative to the root of cgroup file system) as the argument.
4443 *
4444 * Most likely, this user command will try to rmdir this cgroup.
4445 *
4446 * This races with the possibility that some other task will be
4447 * attached to this cgroup before it is removed, or that some other
4448 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4449 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4450 * unused, and this cgroup will be reprieved from its death sentence,
4451 * to continue to serve a useful existence. Next time it's released,
4452 * we will get notified again, if it still has 'notify_on_release' set.
4453 *
4454 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4455 * means only wait until the task is successfully execve()'d. The
4456 * separate release agent task is forked by call_usermodehelper(),
4457 * then control in this thread returns here, without waiting for the
4458 * release agent task. We don't bother to wait because the caller of
4459 * this routine has no use for the exit status of the release agent
4460 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004461 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004462static void cgroup_release_agent(struct work_struct *work)
4463{
4464 BUG_ON(work != &release_agent_work);
4465 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004466 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004467 while (!list_empty(&release_list)) {
4468 char *argv[3], *envp[3];
4469 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05004470 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07004471 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004472 struct cgroup,
4473 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004474 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004475 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05004476 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004477 if (!pathbuf)
4478 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05004479 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
4480 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07004481 goto continue_free;
4482 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4483 if (!agentbuf)
4484 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004485
4486 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004487 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05004488 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004489 argv[i] = NULL;
4490
4491 i = 0;
4492 /* minimal command environment */
4493 envp[i++] = "HOME=/";
4494 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4495 envp[i] = NULL;
4496
4497 /* Drop the lock while we invoke the usermode helper,
4498 * since the exec could involve hitting disk and hence
4499 * be a slow process */
4500 mutex_unlock(&cgroup_mutex);
4501 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004502 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004503 continue_free:
4504 kfree(pathbuf);
4505 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004506 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004507 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004508 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004509 mutex_unlock(&cgroup_mutex);
4510}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004511
4512static int __init cgroup_disable(char *str)
4513{
Tejun Heo30159ec2013-06-25 11:53:37 -07004514 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004515 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07004516 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004517
4518 while ((token = strsep(&str, ",")) != NULL) {
4519 if (!*token)
4520 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004521
Tejun Heo3ed80a62014-02-08 10:36:58 -05004522 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004523 if (!strcmp(token, ss->name)) {
4524 ss->disabled = 1;
4525 printk(KERN_INFO "Disabling %s control group"
4526 " subsystem\n", ss->name);
4527 break;
4528 }
4529 }
4530 }
4531 return 1;
4532}
4533__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004534
Tejun Heob77d7b62013-08-13 11:01:54 -04004535/**
Tejun Heo5a17f542014-02-11 11:52:47 -05004536 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04004537 * @dentry: directory dentry of interest
4538 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04004539 *
Tejun Heo5a17f542014-02-11 11:52:47 -05004540 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4541 * to get the corresponding css and return it. If such css doesn't exist
4542 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02004543 */
Tejun Heo5a17f542014-02-11 11:52:47 -05004544struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4545 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004546{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004547 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4548 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004549 struct cgroup *cgrp;
Tejun Heob77d7b62013-08-13 11:01:54 -04004550
Tejun Heo35cf0832013-08-26 18:40:56 -04004551 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004552 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4553 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004554 return ERR_PTR(-EBADF);
4555
Tejun Heo5a17f542014-02-11 11:52:47 -05004556 rcu_read_lock();
4557
Tejun Heo2bd59d42014-02-11 11:52:49 -05004558 /*
4559 * This path doesn't originate from kernfs and @kn could already
4560 * have been or be removed at any point. @kn->priv is RCU
4561 * protected for this access. See destroy_locked() for details.
4562 */
4563 cgrp = rcu_dereference(kn->priv);
4564 if (cgrp)
4565 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05004566
4567 if (!css || !css_tryget(css))
4568 css = ERR_PTR(-ENOENT);
4569
4570 rcu_read_unlock();
4571 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004572}
Stephane Eraniane5d13672011-02-14 11:20:01 +02004573
Li Zefan1cb650b2013-08-19 10:05:24 +08004574/**
4575 * css_from_id - lookup css by id
4576 * @id: the cgroup id
4577 * @ss: cgroup subsys to be looked into
4578 *
4579 * Returns the css if there's valid one with @id, otherwise returns NULL.
4580 * Should be called under rcu_read_lock().
4581 */
4582struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4583{
4584 struct cgroup *cgrp;
4585
Tejun Heoace2bee2014-02-11 11:52:47 -05004586 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08004587
4588 cgrp = idr_find(&ss->root->cgroup_idr, id);
4589 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04004590 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08004591 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004592}
4593
Paul Menagefe693432009-09-23 15:56:20 -07004594#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04004595static struct cgroup_subsys_state *
4596debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07004597{
4598 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4599
4600 if (!css)
4601 return ERR_PTR(-ENOMEM);
4602
4603 return css;
4604}
4605
Tejun Heoeb954192013-08-08 20:11:23 -04004606static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07004607{
Tejun Heoeb954192013-08-08 20:11:23 -04004608 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07004609}
4610
Tejun Heo182446d2013-08-08 20:11:24 -04004611static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
4612 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004613{
Tejun Heo182446d2013-08-08 20:11:24 -04004614 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07004615}
4616
Tejun Heo182446d2013-08-08 20:11:24 -04004617static u64 current_css_set_read(struct cgroup_subsys_state *css,
4618 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004619{
4620 return (u64)(unsigned long)current->cgroups;
4621}
4622
Tejun Heo182446d2013-08-08 20:11:24 -04004623static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08004624 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004625{
4626 u64 count;
4627
4628 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07004629 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07004630 rcu_read_unlock();
4631 return count;
4632}
4633
Tejun Heo2da8ca82013-12-05 12:28:04 -05004634static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004635{
Tejun Heo69d02062013-06-12 21:04:50 -07004636 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07004637 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05004638 char *name_buf;
4639
4640 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
4641 if (!name_buf)
4642 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07004643
4644 read_lock(&css_set_lock);
4645 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07004646 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07004647 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004648 struct cgroup *c = link->cgrp;
Tejun Heo59f52962014-02-11 11:52:49 -05004649 const char *name = "?";
Paul Menage7717f7b2009-09-23 15:56:22 -07004650
Tejun Heoe61734c2014-02-12 09:29:50 -05004651 if (c != cgroup_dummy_top) {
4652 cgroup_name(c, name_buf, NAME_MAX + 1);
4653 name = name_buf;
4654 }
Tejun Heo59f52962014-02-11 11:52:49 -05004655
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004656 seq_printf(seq, "Root %d group %s\n",
4657 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07004658 }
4659 rcu_read_unlock();
4660 read_unlock(&css_set_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05004661 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004662 return 0;
4663}
4664
4665#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05004666static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004667{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004668 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07004669 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07004670
4671 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04004672 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004673 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004674 struct task_struct *task;
4675 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07004676 seq_printf(seq, "css_set %p\n", cset);
4677 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004678 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4679 seq_puts(seq, " ...\n");
4680 break;
4681 } else {
4682 seq_printf(seq, " task %d\n",
4683 task_pid_vnr(task));
4684 }
4685 }
4686 }
4687 read_unlock(&css_set_lock);
4688 return 0;
4689}
4690
Tejun Heo182446d2013-08-08 20:11:24 -04004691static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004692{
Tejun Heo182446d2013-08-08 20:11:24 -04004693 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07004694}
4695
4696static struct cftype debug_files[] = {
4697 {
Paul Menagefe693432009-09-23 15:56:20 -07004698 .name = "taskcount",
4699 .read_u64 = debug_taskcount_read,
4700 },
4701
4702 {
4703 .name = "current_css_set",
4704 .read_u64 = current_css_set_read,
4705 },
4706
4707 {
4708 .name = "current_css_set_refcount",
4709 .read_u64 = current_css_set_refcount_read,
4710 },
4711
4712 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004713 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004714 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004715 },
4716
4717 {
4718 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004719 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004720 },
4721
4722 {
Paul Menagefe693432009-09-23 15:56:20 -07004723 .name = "releasable",
4724 .read_u64 = releasable_read,
4725 },
Paul Menagefe693432009-09-23 15:56:20 -07004726
Tejun Heo4baf6e32012-04-01 12:09:55 -07004727 { } /* terminate */
4728};
Paul Menagefe693432009-09-23 15:56:20 -07004729
Tejun Heo073219e2014-02-08 10:36:58 -05004730struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08004731 .css_alloc = debug_css_alloc,
4732 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07004733 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07004734};
4735#endif /* CONFIG_CGROUP_DEBUG */