blob: 079c478a4735cf92fadb92ba8641d3ecbc0890fc [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 Heo0942eee2013-08-08 20:11:26 -0400372/*
373 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400374 * until after the first call to css_task_iter_start(). This reduces the
375 * fork()/exit() overhead for people who have cgroups compiled into their
376 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400377 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700378static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700379
Tejun Heo5abb8852013-06-12 21:04:49 -0700380static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700381{
Tejun Heo69d02062013-06-12 21:04:50 -0700382 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700383
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700384 /*
385 * Ensure that the refcount doesn't hit zero while any readers
386 * can see it. Similar to atomic_dec_and_lock(), but for an
387 * rwlock
388 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700389 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700390 return;
391 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700392 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700393 write_unlock(&css_set_lock);
394 return;
395 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700396
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700397 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700398 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700399 css_set_count--;
400
Tejun Heo69d02062013-06-12 21:04:50 -0700401 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700402 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700403
Tejun Heo69d02062013-06-12 21:04:50 -0700404 list_del(&link->cset_link);
405 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800406
Tejun Heoddd69142013-06-12 21:04:54 -0700407 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700408 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700409 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700410 set_bit(CGRP_RELEASABLE, &cgrp->flags);
411 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700412 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700413
414 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700415 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700416
417 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700418 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700419}
420
421/*
422 * refcounted get/put for css_set objects
423 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700424static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700425{
Tejun Heo5abb8852013-06-12 21:04:49 -0700426 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700427}
428
Tejun Heo5abb8852013-06-12 21:04:49 -0700429static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700430{
Tejun Heo5abb8852013-06-12 21:04:49 -0700431 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700432}
433
Tejun Heo5abb8852013-06-12 21:04:49 -0700434static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700435{
Tejun Heo5abb8852013-06-12 21:04:49 -0700436 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700437}
438
Tejun Heob326f9d2013-06-24 15:21:48 -0700439/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700440 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700441 * @cset: candidate css_set being tested
442 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700443 * @new_cgrp: cgroup that's being entered by the task
444 * @template: desired set of css pointers in css_set (pre-calculated)
445 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800446 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700447 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
448 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700449static bool compare_css_sets(struct css_set *cset,
450 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700451 struct cgroup *new_cgrp,
452 struct cgroup_subsys_state *template[])
453{
454 struct list_head *l1, *l2;
455
Tejun Heo5abb8852013-06-12 21:04:49 -0700456 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700457 /* Not all subsystems matched */
458 return false;
459 }
460
461 /*
462 * Compare cgroup pointers in order to distinguish between
463 * different cgroups in heirarchies with no subsystems. We
464 * could get by with just this check alone (and skip the
465 * memcmp above) but on most setups the memcmp check will
466 * avoid the need for this more expensive check on almost all
467 * candidates.
468 */
469
Tejun Heo69d02062013-06-12 21:04:50 -0700470 l1 = &cset->cgrp_links;
471 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700472 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700473 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700474 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700475
476 l1 = l1->next;
477 l2 = l2->next;
478 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700479 if (l1 == &cset->cgrp_links) {
480 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700481 break;
482 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700483 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700484 }
485 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700486 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
487 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
488 cgrp1 = link1->cgrp;
489 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700490 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700491 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700492
493 /*
494 * If this hierarchy is the hierarchy of the cgroup
495 * that's changing, then we need to check that this
496 * css_set points to the new cgroup; if it's any other
497 * hierarchy, then this css_set should point to the
498 * same cgroup as the old css_set.
499 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700500 if (cgrp1->root == new_cgrp->root) {
501 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700502 return false;
503 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700504 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700505 return false;
506 }
507 }
508 return true;
509}
510
Tejun Heob326f9d2013-06-24 15:21:48 -0700511/**
512 * find_existing_css_set - init css array and find the matching css_set
513 * @old_cset: the css_set that we're using before the cgroup transition
514 * @cgrp: the cgroup that we're moving into
515 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700516 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700517static struct css_set *find_existing_css_set(struct css_set *old_cset,
518 struct cgroup *cgrp,
519 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700520{
Paul Menagebd89aab2007-10-18 23:40:44 -0700521 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700522 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700523 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800524 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700525 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700526
Ben Blumaae8aab2010-03-10 15:22:07 -0800527 /*
528 * Build the set of subsystem state objects that we want to see in the
529 * new css_set. while subsystems can change globally, the entries here
530 * won't change, so no need for locking.
531 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700532 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400533 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700534 /* Subsystem is in this hierarchy. So we want
535 * the subsystem state from the new
536 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400537 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700538 } else {
539 /* Subsystem is not in this hierarchy, so we
540 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700541 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700542 }
543 }
544
Li Zefan0ac801f2013-01-10 11:49:27 +0800545 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700546 hash_for_each_possible(css_set_table, cset, hlist, key) {
547 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700548 continue;
549
550 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700551 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700552 }
Paul Menage817929e2007-10-18 23:39:36 -0700553
554 /* No existing cgroup group matched */
555 return NULL;
556}
557
Tejun Heo69d02062013-06-12 21:04:50 -0700558static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700559{
Tejun Heo69d02062013-06-12 21:04:50 -0700560 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700561
Tejun Heo69d02062013-06-12 21:04:50 -0700562 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
563 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700564 kfree(link);
565 }
566}
567
Tejun Heo69d02062013-06-12 21:04:50 -0700568/**
569 * allocate_cgrp_cset_links - allocate cgrp_cset_links
570 * @count: the number of links to allocate
571 * @tmp_links: list_head the allocated links are put on
572 *
573 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
574 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700575 */
Tejun Heo69d02062013-06-12 21:04:50 -0700576static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700577{
Tejun Heo69d02062013-06-12 21:04:50 -0700578 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700579 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700580
581 INIT_LIST_HEAD(tmp_links);
582
Li Zefan36553432008-07-29 22:33:19 -0700583 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700584 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700585 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700586 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700587 return -ENOMEM;
588 }
Tejun Heo69d02062013-06-12 21:04:50 -0700589 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700590 }
591 return 0;
592}
593
Li Zefanc12f65d2009-01-07 18:07:42 -0800594/**
595 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700596 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700597 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800598 * @cgrp: the destination cgroup
599 */
Tejun Heo69d02062013-06-12 21:04:50 -0700600static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
601 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800602{
Tejun Heo69d02062013-06-12 21:04:50 -0700603 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800604
Tejun Heo69d02062013-06-12 21:04:50 -0700605 BUG_ON(list_empty(tmp_links));
606 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
607 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700608 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700609 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700610 /*
611 * Always add links to the tail of the list so that the list
612 * is sorted by order of hierarchy creation
613 */
Tejun Heo69d02062013-06-12 21:04:50 -0700614 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800615}
616
Tejun Heob326f9d2013-06-24 15:21:48 -0700617/**
618 * find_css_set - return a new css_set with one cgroup updated
619 * @old_cset: the baseline css_set
620 * @cgrp: the cgroup to be updated
621 *
622 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
623 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700624 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700625static struct css_set *find_css_set(struct css_set *old_cset,
626 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700627{
Tejun Heob326f9d2013-06-24 15:21:48 -0700628 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700629 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700630 struct list_head tmp_links;
631 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800632 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700633
Tejun Heob326f9d2013-06-24 15:21:48 -0700634 lockdep_assert_held(&cgroup_mutex);
635
Paul Menage817929e2007-10-18 23:39:36 -0700636 /* First see if we already have a cgroup group that matches
637 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700638 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700639 cset = find_existing_css_set(old_cset, cgrp, template);
640 if (cset)
641 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700642 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700643
Tejun Heo5abb8852013-06-12 21:04:49 -0700644 if (cset)
645 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700646
Tejun Heof4f4be22013-06-12 21:04:51 -0700647 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700648 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700649 return NULL;
650
Tejun Heo69d02062013-06-12 21:04:50 -0700651 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700652 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700653 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700654 return NULL;
655 }
656
Tejun Heo5abb8852013-06-12 21:04:49 -0700657 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700658 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700659 INIT_LIST_HEAD(&cset->tasks);
660 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700661
662 /* Copy the set of subsystem state objects generated in
663 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700664 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700665
666 write_lock(&css_set_lock);
667 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700668 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700669 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700670
Paul Menage7717f7b2009-09-23 15:56:22 -0700671 if (c->root == cgrp->root)
672 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700673 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700674 }
Paul Menage817929e2007-10-18 23:39:36 -0700675
Tejun Heo69d02062013-06-12 21:04:50 -0700676 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700677
Paul Menage817929e2007-10-18 23:39:36 -0700678 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700679
680 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700681 key = css_set_hash(cset->subsys);
682 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700683
Paul Menage817929e2007-10-18 23:39:36 -0700684 write_unlock(&css_set_lock);
685
Tejun Heo5abb8852013-06-12 21:04:49 -0700686 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700687}
688
Tejun Heo2bd59d42014-02-11 11:52:49 -0500689static struct cgroupfs_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
690{
691 struct cgroup *top_cgrp = kf_root->kn->priv;
692
693 return top_cgrp->root;
694}
695
Tejun Heof2e85d52014-02-11 11:52:49 -0500696static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
697{
698 int id;
699
700 lockdep_assert_held(&cgroup_mutex);
701
702 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
703 GFP_KERNEL);
704 if (id < 0)
705 return id;
706
707 root->hierarchy_id = id;
708 return 0;
709}
710
711static void cgroup_exit_root_id(struct cgroupfs_root *root)
712{
713 lockdep_assert_held(&cgroup_mutex);
714
715 if (root->hierarchy_id) {
716 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
717 root->hierarchy_id = 0;
718 }
719}
720
721static void cgroup_free_root(struct cgroupfs_root *root)
722{
723 if (root) {
724 /* hierarhcy ID shoulid already have been released */
725 WARN_ON_ONCE(root->hierarchy_id);
726
727 idr_destroy(&root->cgroup_idr);
728 kfree(root);
729 }
730}
731
Tejun Heo776f02f2014-02-12 09:29:50 -0500732static void cgroup_destroy_root(struct cgroupfs_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500733{
Tejun Heof2e85d52014-02-11 11:52:49 -0500734 struct cgroup *cgrp = &root->top_cgroup;
735 struct cgrp_cset_link *link, *tmp_link;
736 int ret;
737
Tejun Heo2bd59d42014-02-11 11:52:49 -0500738 mutex_lock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500739 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500740
Tejun Heo776f02f2014-02-12 09:29:50 -0500741 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500742 BUG_ON(!list_empty(&cgrp->children));
743
Tejun Heof2e85d52014-02-11 11:52:49 -0500744 /* Rebind all subsystems back to the default hierarchy */
745 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
746 ret = rebind_subsystems(root, 0, root->subsys_mask);
747 /* Shouldn't be able to fail ... */
748 BUG_ON(ret);
749 }
750
751 /*
752 * Release all the links from cset_links to this hierarchy's
753 * root cgroup
754 */
755 write_lock(&css_set_lock);
756
757 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
758 list_del(&link->cset_link);
759 list_del(&link->cgrp_link);
760 kfree(link);
761 }
762 write_unlock(&css_set_lock);
763
764 if (!list_empty(&root->root_list)) {
765 list_del(&root->root_list);
766 cgroup_root_count--;
767 }
768
769 cgroup_exit_root_id(root);
770
771 mutex_unlock(&cgroup_mutex);
772 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500773
Tejun Heo2bd59d42014-02-11 11:52:49 -0500774 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500775 cgroup_free_root(root);
776}
777
Paul Menageddbcc7e2007-10-18 23:39:30 -0700778/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700779 * Return the cgroup for "task" from the given hierarchy. Must be
780 * called with cgroup_mutex held.
781 */
782static struct cgroup *task_cgroup_from_root(struct task_struct *task,
783 struct cgroupfs_root *root)
784{
Tejun Heo5abb8852013-06-12 21:04:49 -0700785 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700786 struct cgroup *res = NULL;
787
788 BUG_ON(!mutex_is_locked(&cgroup_mutex));
789 read_lock(&css_set_lock);
790 /*
791 * No need to lock the task - since we hold cgroup_mutex the
792 * task can't change groups, so the only thing that can happen
793 * is that it exits and its css is set back to init_css_set.
794 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700795 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700796 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700797 res = &root->top_cgroup;
798 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700799 struct cgrp_cset_link *link;
800
801 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700802 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700803
Paul Menage7717f7b2009-09-23 15:56:22 -0700804 if (c->root == root) {
805 res = c;
806 break;
807 }
808 }
809 }
810 read_unlock(&css_set_lock);
811 BUG_ON(!res);
812 return res;
813}
814
815/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700816 * There is one global cgroup mutex. We also require taking
817 * task_lock() when dereferencing a task's cgroup subsys pointers.
818 * See "The task_lock() exception", at the end of this comment.
819 *
820 * A task must hold cgroup_mutex to modify cgroups.
821 *
822 * Any task can increment and decrement the count field without lock.
823 * So in general, code holding cgroup_mutex can't rely on the count
824 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800825 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700826 * means that no tasks are currently attached, therefore there is no
827 * way a task attached to that cgroup can fork (the other way to
828 * increment the count). So code holding cgroup_mutex can safely
829 * assume that if the count is zero, it will stay zero. Similarly, if
830 * a task holds cgroup_mutex on a cgroup with zero count, it
831 * knows that the cgroup won't be removed, as cgroup_rmdir()
832 * needs that mutex.
833 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700834 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
835 * (usually) take cgroup_mutex. These are the two most performance
836 * critical pieces of code here. The exception occurs on cgroup_exit(),
837 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
838 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800839 * to the release agent with the name of the cgroup (path relative to
840 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700841 *
842 * A cgroup can only be deleted if both its 'count' of using tasks
843 * is zero, and its list of 'children' cgroups is empty. Since all
844 * tasks in the system use _some_ cgroup, and since there is always at
845 * least one task in the system (init, pid == 1), therefore, top_cgroup
846 * always has either children cgroups and/or using tasks. So we don't
847 * need a special hack to ensure that top_cgroup cannot be deleted.
848 *
849 * The task_lock() exception
850 *
851 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800852 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800853 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700854 * several performance critical places that need to reference
855 * task->cgroup without the expense of grabbing a system global
856 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800857 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700858 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
859 * the task_struct routinely used for such matters.
860 *
861 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800862 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700863 */
864
Tejun Heo628f7cd2013-06-28 16:24:11 -0700865static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500866static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700867static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700868
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500869static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
870 char *buf)
871{
872 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
873 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
874 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
875 cft->ss->name, cft->name);
876 else
877 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
878 return buf;
879}
880
Tejun Heof2e85d52014-02-11 11:52:49 -0500881/**
882 * cgroup_file_mode - deduce file mode of a control file
883 * @cft: the control file in question
884 *
885 * returns cft->mode if ->mode is not 0
886 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
887 * returns S_IRUGO if it has only a read handler
888 * returns S_IWUSR if it has only a write hander
889 */
890static umode_t cgroup_file_mode(const struct cftype *cft)
891{
892 umode_t mode = 0;
893
894 if (cft->mode)
895 return cft->mode;
896
897 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
898 mode |= S_IRUGO;
899
900 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
901 cft->trigger)
902 mode |= S_IWUSR;
903
904 return mode;
905}
906
Li Zefanbe445622013-01-24 14:31:42 +0800907static void cgroup_free_fn(struct work_struct *work)
908{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700909 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800910
Tejun Heo3c9c8252014-02-12 09:29:50 -0500911 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -0500912 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800913
Tejun Heo776f02f2014-02-12 09:29:50 -0500914 if (cgrp->parent) {
915 /*
916 * We get a ref to the parent, and put the ref when this
917 * cgroup is being freed, so it's guaranteed that the
918 * parent won't be destroyed before its children.
919 */
920 cgroup_put(cgrp->parent);
921 kernfs_put(cgrp->kn);
922 kfree(cgrp);
923 } else {
924 /*
925 * This is top cgroup's refcnt reaching zero, which
926 * indicates that the root should be released.
927 */
928 cgroup_destroy_root(cgrp->root);
929 }
Li Zefanbe445622013-01-24 14:31:42 +0800930}
931
932static void cgroup_free_rcu(struct rcu_head *head)
933{
934 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
935
Tejun Heoea15f8c2013-06-13 19:27:42 -0700936 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500937 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800938}
939
Tejun Heo59f52962014-02-11 11:52:49 -0500940static void cgroup_get(struct cgroup *cgrp)
941{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500942 WARN_ON_ONCE(cgroup_is_dead(cgrp));
943 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
944 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700945}
946
Tejun Heo59f52962014-02-11 11:52:49 -0500947static void cgroup_put(struct cgroup *cgrp)
948{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500949 if (!atomic_dec_and_test(&cgrp->refcnt))
950 return;
Tejun Heo776f02f2014-02-12 09:29:50 -0500951 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -0500952 return;
Tejun Heo59f52962014-02-11 11:52:49 -0500953
Tejun Heo2bd59d42014-02-11 11:52:49 -0500954 /*
955 * XXX: cgrp->id is only used to look up css's. As cgroup and
956 * css's lifetimes will be decoupled, it should be made
957 * per-subsystem and moved to css->id so that lookups are
958 * successful until the target css is released.
959 */
960 mutex_lock(&cgroup_mutex);
961 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
962 mutex_unlock(&cgroup_mutex);
963 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700964
Tejun Heo2bd59d42014-02-11 11:52:49 -0500965 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700966}
967
Li Zefan2739d3c2013-01-21 18:18:33 +0800968static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700969{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500970 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700971
Tejun Heoace2bee2014-02-11 11:52:47 -0500972 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500973 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -0700974}
975
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400976/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700977 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700978 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400979 * @subsys_mask: mask of the subsystem ids whose files should be removed
980 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700981static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700982{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400983 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700984 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700985
Tejun Heob420ba72013-07-12 12:34:02 -0700986 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -0500987 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -0700988
989 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400990 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -0500991 list_for_each_entry(cfts, &ss->cfts, node)
992 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400993 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994}
995
Paul Menageddbcc7e2007-10-18 23:39:30 -0700996static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700997 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998{
Paul Menagebd89aab2007-10-18 23:40:44 -0700999 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001000 struct cgroup_subsys *ss;
Tejun Heo31261212013-06-28 17:07:30 -07001001 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002
Tejun Heoace2bee2014-02-11 11:52:47 -05001003 lockdep_assert_held(&cgroup_tree_mutex);
1004 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001005
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006 /* Check that any added subsystems are currently free */
Tejun Heo3ed80a62014-02-08 10:36:58 -05001007 for_each_subsys(ss, i)
1008 if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
1009 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010
Tejun Heo31261212013-06-28 17:07:30 -07001011 ret = cgroup_populate_dir(cgrp, added_mask);
1012 if (ret)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001013 return ret;
Tejun Heo31261212013-06-28 17:07:30 -07001014
1015 /*
1016 * Nothing can fail from this point on. Remove files for the
1017 * removed subsystems and rebind each subsystem.
1018 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001019 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001020 cgroup_clear_dir(cgrp, removed_mask);
Tejun Heo4ac06012014-02-11 11:52:47 -05001021 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001022
Tejun Heo30159ec2013-06-25 11:53:37 -07001023 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001025
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001026 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001027 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001028 BUG_ON(cgroup_css(cgrp, ss));
1029 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1030 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001031
Tejun Heo73e80ed2013-08-13 11:01:55 -04001032 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001033 cgroup_css(cgroup_dummy_top, ss));
1034 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001035
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001036 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001037 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001038 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001039
Ben Blumcf5d5942010-03-10 15:22:09 -08001040 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001041 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001042 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001044 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1045 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001046
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001048 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001049
Tejun Heoca8bdca2013-08-26 18:40:56 -04001050 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001051 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1052
Tejun Heo9871bf92013-06-24 15:21:47 -07001053 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001054 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 }
1056 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057
Tejun Heo1672d042013-06-25 18:04:54 -07001058 /*
1059 * Mark @root has finished binding subsystems. @root->subsys_mask
1060 * now matches the bound subsystems.
1061 */
1062 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001063 kernfs_activate(cgrp->kn);
Tejun Heo1672d042013-06-25 18:04:54 -07001064
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065 return 0;
1066}
1067
Tejun Heo2bd59d42014-02-11 11:52:49 -05001068static int cgroup_show_options(struct seq_file *seq,
1069 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001071 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001073 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074
Tejun Heob85d2042013-12-06 15:11:57 -05001075 for_each_subsys(ss, ssid)
1076 if (root->subsys_mask & (1 << ssid))
1077 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001078 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1079 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001080 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001082 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001083 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001084
1085 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001086 if (strlen(root->release_agent_path))
1087 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001088 spin_unlock(&release_agent_path_lock);
1089
Tejun Heo2260e7f2012-11-19 08:13:38 -08001090 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001091 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001092 if (strlen(root->name))
1093 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 return 0;
1095}
1096
1097struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001098 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001100 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001101 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001102 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001103 /* User explicitly requested empty subsystem */
1104 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001105};
1106
Ben Blumaae8aab2010-03-10 15:22:07 -08001107/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001108 * Convert a hierarchy specifier into a bitmask of subsystems and
1109 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1110 * array. This function takes refcounts on subsystems to be used, unless it
1111 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001112 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001113static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001115 char *token, *o = data;
1116 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001117 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001118 struct cgroup_subsys *ss;
1119 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001120
Ben Blumaae8aab2010-03-10 15:22:07 -08001121 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1122
Li Zefanf9ab5b52009-06-17 16:26:33 -07001123#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001124 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001125#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001126
Paul Menagec6d57f32009-09-23 15:56:19 -07001127 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128
1129 while ((token = strsep(&o, ",")) != NULL) {
1130 if (!*token)
1131 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001132 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001133 /* Explicitly have no subsystems */
1134 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001135 continue;
1136 }
1137 if (!strcmp(token, "all")) {
1138 /* Mutually exclusive option 'all' + subsystem name */
1139 if (one_ss)
1140 return -EINVAL;
1141 all_ss = true;
1142 continue;
1143 }
Tejun Heo873fe092013-04-14 20:15:26 -07001144 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1145 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1146 continue;
1147 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001148 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001149 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001150 continue;
1151 }
1152 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001153 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001154 continue;
1155 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001156 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001157 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001158 continue;
1159 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001160 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001161 /* Specifying two release agents is forbidden */
1162 if (opts->release_agent)
1163 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001164 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001165 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001166 if (!opts->release_agent)
1167 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001168 continue;
1169 }
1170 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001171 const char *name = token + 5;
1172 /* Can't specify an empty name */
1173 if (!strlen(name))
1174 return -EINVAL;
1175 /* Must match [\w.-]+ */
1176 for (i = 0; i < strlen(name); i++) {
1177 char c = name[i];
1178 if (isalnum(c))
1179 continue;
1180 if ((c == '.') || (c == '-') || (c == '_'))
1181 continue;
1182 return -EINVAL;
1183 }
1184 /* Specifying two names is forbidden */
1185 if (opts->name)
1186 return -EINVAL;
1187 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001188 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001189 GFP_KERNEL);
1190 if (!opts->name)
1191 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001192
1193 continue;
1194 }
1195
Tejun Heo30159ec2013-06-25 11:53:37 -07001196 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001197 if (strcmp(token, ss->name))
1198 continue;
1199 if (ss->disabled)
1200 continue;
1201
1202 /* Mutually exclusive option 'all' + subsystem name */
1203 if (all_ss)
1204 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001205 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001206 one_ss = true;
1207
1208 break;
1209 }
1210 if (i == CGROUP_SUBSYS_COUNT)
1211 return -ENOENT;
1212 }
1213
1214 /*
1215 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001216 * otherwise if 'none', 'name=' and a subsystem name options
1217 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001218 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001219 if (all_ss || (!one_ss && !opts->none && !opts->name))
1220 for_each_subsys(ss, i)
1221 if (!ss->disabled)
1222 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001223
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001224 /* Consistency checks */
1225
Tejun Heo873fe092013-04-14 20:15:26 -07001226 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1227 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1228
Tejun Heod3ba07c2014-02-13 06:58:38 -05001229 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1230 opts->cpuset_clone_children || opts->release_agent ||
1231 opts->name) {
1232 pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001233 return -EINVAL;
1234 }
Tejun Heo873fe092013-04-14 20:15:26 -07001235 }
1236
Li Zefanf9ab5b52009-06-17 16:26:33 -07001237 /*
1238 * Option noprefix was introduced just for backward compatibility
1239 * with the old cpuset, so we allow noprefix only if mounting just
1240 * the cpuset subsystem.
1241 */
Tejun Heo93438622013-04-14 20:15:25 -07001242 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001243 return -EINVAL;
1244
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001245
1246 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001247 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001248 return -EINVAL;
1249
1250 /*
1251 * We either have to specify by name or by subsystems. (So all
1252 * empty hierarchies must have a name).
1253 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001254 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001255 return -EINVAL;
1256
1257 return 0;
1258}
1259
Tejun Heo2bd59d42014-02-11 11:52:49 -05001260static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001261{
1262 int ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001263 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001264 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001265 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001266
Tejun Heo873fe092013-04-14 20:15:26 -07001267 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1268 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1269 return -EINVAL;
1270 }
1271
Tejun Heoace2bee2014-02-11 11:52:47 -05001272 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001273 mutex_lock(&cgroup_mutex);
1274
1275 /* See what subsystems are wanted */
1276 ret = parse_cgroupfs_options(data, &opts);
1277 if (ret)
1278 goto out_unlock;
1279
Tejun Heoa8a648c2013-06-24 15:21:47 -07001280 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001281 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1282 task_tgid_nr(current), current->comm);
1283
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001284 added_mask = opts.subsys_mask & ~root->subsys_mask;
1285 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001286
Ben Blumcf5d5942010-03-10 15:22:09 -08001287 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001288 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001289 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001290 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1291 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1292 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001293 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001294 goto out_unlock;
1295 }
1296
Tejun Heof172e672013-06-28 17:07:30 -07001297 /* remounting is not allowed for populated hierarchies */
Tejun Heo3c9c8252014-02-12 09:29:50 -05001298 if (!list_empty(&root->top_cgroup.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001299 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001300 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001301 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001302
Paul Menageddbcc7e2007-10-18 23:39:30 -07001303 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001304 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001305 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001306
Tejun Heo69e943b2014-02-08 10:36:58 -05001307 if (opts.release_agent) {
1308 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001309 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001310 spin_unlock(&release_agent_path_lock);
1311 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001312 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001313 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001314 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001315 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001316 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001317 return ret;
1318}
1319
Paul Menagecc31edc2008-10-18 20:28:04 -07001320static void init_cgroup_housekeeping(struct cgroup *cgrp)
1321{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001322 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001323 INIT_LIST_HEAD(&cgrp->sibling);
1324 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001325 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001326 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001327 INIT_LIST_HEAD(&cgrp->pidlists);
1328 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001329 cgrp->dummy_css.cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001330}
Paul Menagec6d57f32009-09-23 15:56:19 -07001331
Paul Menageddbcc7e2007-10-18 23:39:30 -07001332static void init_cgroup_root(struct cgroupfs_root *root)
1333{
Paul Menagebd89aab2007-10-18 23:40:44 -07001334 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001335
Paul Menageddbcc7e2007-10-18 23:39:30 -07001336 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001337 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001338 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001339 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001340 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001341}
1342
Paul Menagec6d57f32009-09-23 15:56:19 -07001343static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1344{
1345 struct cgroupfs_root *root;
1346
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001347 if (!opts->subsys_mask && !opts->none)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001348 return ERR_PTR(-EINVAL);
Paul Menagec6d57f32009-09-23 15:56:19 -07001349
1350 root = kzalloc(sizeof(*root), GFP_KERNEL);
1351 if (!root)
1352 return ERR_PTR(-ENOMEM);
1353
1354 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001355
Tejun Heo1672d042013-06-25 18:04:54 -07001356 /*
1357 * We need to set @root->subsys_mask now so that @root can be
1358 * matched by cgroup_test_super() before it finishes
1359 * initialization; otherwise, competing mounts with the same
1360 * options may try to bind the same subsystems instead of waiting
1361 * for the first one leading to unexpected mount errors.
1362 * SUBSYS_BOUND will be set once actual binding is complete.
1363 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001364 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001365 root->flags = opts->flags;
1366 if (opts->release_agent)
1367 strcpy(root->release_agent_path, opts->release_agent);
1368 if (opts->name)
1369 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001370 if (opts->cpuset_clone_children)
1371 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001372 return root;
1373}
1374
Tejun Heod427dfe2014-02-11 11:52:48 -05001375static int cgroup_setup_root(struct cgroupfs_root *root)
1376{
1377 LIST_HEAD(tmp_links);
Tejun Heod427dfe2014-02-11 11:52:48 -05001378 struct cgroup *root_cgrp = &root->top_cgroup;
Tejun Heod427dfe2014-02-11 11:52:48 -05001379 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001380 int i, ret;
1381
1382 lockdep_assert_held(&cgroup_tree_mutex);
1383 lockdep_assert_held(&cgroup_mutex);
Tejun Heod427dfe2014-02-11 11:52:48 -05001384
1385 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1386 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001387 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001388 root_cgrp->id = ret;
1389
Tejun Heod427dfe2014-02-11 11:52:48 -05001390 /*
1391 * We're accessing css_set_count without locking css_set_lock here,
1392 * but that's OK - it can only be increased by someone holding
1393 * cgroup_lock, and that's us. The worst that can happen is that we
1394 * have some link structures left over
1395 */
1396 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1397 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001398 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001399
1400 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1401 ret = cgroup_init_root_id(root, 2, 0);
1402 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001403 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001404
Tejun Heo2bd59d42014-02-11 11:52:49 -05001405 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1406 KERNFS_ROOT_CREATE_DEACTIVATED,
1407 root_cgrp);
1408 if (IS_ERR(root->kf_root)) {
1409 ret = PTR_ERR(root->kf_root);
1410 goto exit_root_id;
1411 }
1412 root_cgrp->kn = root->kf_root->kn;
Tejun Heod427dfe2014-02-11 11:52:48 -05001413
1414 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1415 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001416 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001417
1418 ret = rebind_subsystems(root, root->subsys_mask, 0);
1419 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001420 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001421
1422 /*
1423 * There must be no failure case after here, since rebinding takes
1424 * care of subsystems' refcounts, which are explicitly dropped in
1425 * the failure exit path.
1426 */
1427 list_add(&root->root_list, &cgroup_roots);
1428 cgroup_root_count++;
1429
1430 /*
1431 * Link the top cgroup in this hierarchy into all the css_set
1432 * objects.
1433 */
1434 write_lock(&css_set_lock);
1435 hash_for_each(css_set_table, i, cset, hlist)
1436 link_css_set(&tmp_links, cset, root_cgrp);
1437 write_unlock(&css_set_lock);
1438
1439 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001440 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001441
Tejun Heo2bd59d42014-02-11 11:52:49 -05001442 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001443 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001444 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001445
Tejun Heo2bd59d42014-02-11 11:52:49 -05001446destroy_root:
1447 kernfs_destroy_root(root->kf_root);
1448 root->kf_root = NULL;
1449exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001450 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001451out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001452 free_cgrp_cset_links(&tmp_links);
1453 return ret;
1454}
1455
Al Virof7e83572010-07-26 13:23:11 +04001456static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001457 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001458 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001459{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001460 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001462 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001463 int ret;
Tejun Heo776f02f2014-02-12 09:29:50 -05001464retry:
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001465 mutex_lock(&cgroup_tree_mutex);
1466 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001467
1468 /* First find the desired set of subsystems */
1469 ret = parse_cgroupfs_options(data, &opts);
Paul Menagec6d57f32009-09-23 15:56:19 -07001470 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001471 goto out_unlock;
Paul Menagec6d57f32009-09-23 15:56:19 -07001472
Tejun Heo2bd59d42014-02-11 11:52:49 -05001473 /* look for a matching existing root */
1474 for_each_active_root(root) {
1475 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001476
Paul Menagec6d57f32009-09-23 15:56:19 -07001477 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001478 * If we asked for a name then it must match. Also, if
1479 * name matches but sybsys_mask doesn't, we should fail.
1480 * Remember whether name matched.
Paul Menagec6d57f32009-09-23 15:56:19 -07001481 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001482 if (opts.name) {
1483 if (strcmp(opts.name, root->name))
1484 continue;
1485 name_match = true;
1486 }
1487
1488 /*
1489 * If we asked for subsystems (or explicitly for no
1490 * subsystems) then they must match.
1491 */
1492 if ((opts.subsys_mask || opts.none) &&
1493 (opts.subsys_mask != root->subsys_mask)) {
1494 if (!name_match)
1495 continue;
1496 ret = -EBUSY;
1497 goto out_unlock;
1498 }
Tejun Heo873fe092013-04-14 20:15:26 -07001499
Tejun Heoc7ba8282013-06-29 14:06:10 -07001500 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001501 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1502 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1503 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001504 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001505 } else {
1506 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1507 }
Tejun Heo873fe092013-04-14 20:15:26 -07001508 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001509
Tejun Heo776f02f2014-02-12 09:29:50 -05001510 /*
1511 * A root's lifetime is governed by its top cgroup. Zero
1512 * ref indicate that the root is being destroyed. Wait for
1513 * destruction to complete so that the subsystems are free.
1514 * We can use wait_queue for the wait but this path is
1515 * super cold. Let's just sleep for a bit and retry.
1516 */
1517 if (!atomic_inc_not_zero(&root->top_cgroup.refcnt)) {
1518 mutex_unlock(&cgroup_mutex);
1519 mutex_unlock(&cgroup_tree_mutex);
1520 msleep(10);
1521 goto retry;
1522 }
1523
1524 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001525 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526 }
1527
Tejun Heo2bd59d42014-02-11 11:52:49 -05001528 /* no such thing, create a new one */
1529 root = cgroup_root_from_opts(&opts);
1530 if (IS_ERR(root)) {
1531 ret = PTR_ERR(root);
1532 goto out_unlock;
1533 }
1534
1535 ret = cgroup_setup_root(root);
1536 if (ret)
1537 cgroup_free_root(root);
1538
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001539out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001540 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001541 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001542
Paul Menagec6d57f32009-09-23 15:56:19 -07001543 kfree(opts.release_agent);
1544 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001545
Tejun Heo2bd59d42014-02-11 11:52:49 -05001546 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001547 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001548
1549 dentry = kernfs_mount(fs_type, flags, root->kf_root);
1550 if (IS_ERR(dentry))
Tejun Heo776f02f2014-02-12 09:29:50 -05001551 cgroup_put(&root->top_cgroup);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001552 return dentry;
1553}
1554
1555static void cgroup_kill_sb(struct super_block *sb)
1556{
1557 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
1558 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
1559
Tejun Heo776f02f2014-02-12 09:29:50 -05001560 cgroup_put(&root->top_cgroup);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001561 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562}
1563
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564static struct file_system_type cgroup_fs_type = {
1565 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001566 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567 .kill_sb = cgroup_kill_sb,
1568};
1569
Greg KH676db4a2010-08-05 13:53:35 -07001570static struct kobject *cgroup_kobj;
1571
Li Zefana043e3b2008-02-23 15:24:09 -08001572/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001573 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001574 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001575 * @buf: the buffer to write the path into
1576 * @buflen: the length of the buffer
1577 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001578 * Determine @task's cgroup on the first (the one with the lowest non-zero
1579 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1580 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1581 * cgroup controller callbacks.
1582 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001583 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001584 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001585char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001586{
1587 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001588 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001589 int hierarchy_id = 1;
1590 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001591
1592 mutex_lock(&cgroup_mutex);
1593
Tejun Heo913ffdb2013-07-11 16:34:48 -07001594 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1595
Tejun Heo857a2be2013-04-14 20:50:08 -07001596 if (root) {
1597 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001598 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001599 } else {
1600 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001601 if (strlcpy(buf, "/", buflen) < buflen)
1602 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001603 }
1604
1605 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001606 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001607}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001608EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001609
Ben Blum74a11662011-05-26 16:25:20 -07001610/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001611 * Control Group taskset
1612 */
Tejun Heo134d3372011-12-12 18:12:21 -08001613struct task_and_cgroup {
1614 struct task_struct *task;
1615 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001616 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001617};
1618
Tejun Heo2f7ee562011-12-12 18:12:21 -08001619struct cgroup_taskset {
1620 struct task_and_cgroup single;
1621 struct flex_array *tc_array;
1622 int tc_array_len;
1623 int idx;
1624 struct cgroup *cur_cgrp;
1625};
1626
1627/**
1628 * cgroup_taskset_first - reset taskset and return the first task
1629 * @tset: taskset of interest
1630 *
1631 * @tset iteration is initialized and the first task is returned.
1632 */
1633struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1634{
1635 if (tset->tc_array) {
1636 tset->idx = 0;
1637 return cgroup_taskset_next(tset);
1638 } else {
1639 tset->cur_cgrp = tset->single.cgrp;
1640 return tset->single.task;
1641 }
1642}
1643EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1644
1645/**
1646 * cgroup_taskset_next - iterate to the next task in taskset
1647 * @tset: taskset of interest
1648 *
1649 * Return the next task in @tset. Iteration must have been initialized
1650 * with cgroup_taskset_first().
1651 */
1652struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1653{
1654 struct task_and_cgroup *tc;
1655
1656 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1657 return NULL;
1658
1659 tc = flex_array_get(tset->tc_array, tset->idx++);
1660 tset->cur_cgrp = tc->cgrp;
1661 return tc->task;
1662}
1663EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1664
1665/**
Tejun Heod99c8722013-08-08 20:11:27 -04001666 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001667 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001668 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001669 *
Tejun Heod99c8722013-08-08 20:11:27 -04001670 * Return the css for the current (last returned) task of @tset for
1671 * subsystem specified by @subsys_id. This function must be preceded by
1672 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001673 */
Tejun Heod99c8722013-08-08 20:11:27 -04001674struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1675 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001676{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001677 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001678}
Tejun Heod99c8722013-08-08 20:11:27 -04001679EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001680
1681/**
1682 * cgroup_taskset_size - return the number of tasks in taskset
1683 * @tset: taskset of interest
1684 */
1685int cgroup_taskset_size(struct cgroup_taskset *tset)
1686{
1687 return tset->tc_array ? tset->tc_array_len : 1;
1688}
1689EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1690
1691
Ben Blum74a11662011-05-26 16:25:20 -07001692/*
1693 * cgroup_task_migrate - move a task from one cgroup to another.
1694 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001695 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001696 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001697static void cgroup_task_migrate(struct cgroup *old_cgrp,
1698 struct task_struct *tsk,
1699 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001700{
Tejun Heo5abb8852013-06-12 21:04:49 -07001701 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001702
1703 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001704 * We are synchronized through threadgroup_lock() against PF_EXITING
1705 * setting such that we can't race against cgroup_exit() changing the
1706 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001707 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001708 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001709 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001710
Ben Blum74a11662011-05-26 16:25:20 -07001711 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001712 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001713 task_unlock(tsk);
1714
1715 /* Update the css_set linked lists if we're using them */
1716 write_lock(&css_set_lock);
1717 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001718 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001719 write_unlock(&css_set_lock);
1720
1721 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001722 * We just gained a reference on old_cset by taking it from the
1723 * task. As trading it for new_cset is protected by cgroup_mutex,
1724 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001725 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001726 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1727 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001728}
1729
Li Zefana043e3b2008-02-23 15:24:09 -08001730/**
Li Zefan081aa452013-03-13 09:17:09 +08001731 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001732 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001733 * @tsk: the task or the leader of the threadgroup to be attached
1734 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001735 *
Tejun Heo257058a2011-12-12 18:12:21 -08001736 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001737 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001738 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001739static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1740 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001741{
1742 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001743 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001744 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001745 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001746 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001747 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001748 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001749 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001750
1751 /*
1752 * step 0: in order to do expensive, possibly blocking operations for
1753 * every thread, we cannot iterate the thread group list, since it needs
1754 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001755 * group - group_rwsem prevents new threads from appearing, and if
1756 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001757 */
Li Zefan081aa452013-03-13 09:17:09 +08001758 if (threadgroup)
1759 group_size = get_nr_threads(tsk);
1760 else
1761 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001762 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001763 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001764 if (!group)
1765 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001766 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001767 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001768 if (retval)
1769 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001770
Ben Blum74a11662011-05-26 16:25:20 -07001771 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001772 /*
1773 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1774 * already PF_EXITING could be freed from underneath us unless we
1775 * take an rcu_read_lock.
1776 */
1777 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001778 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001779 struct task_and_cgroup ent;
1780
Tejun Heocd3d0952011-12-12 18:12:21 -08001781 /* @tsk either already exited or can't exit until the end */
1782 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001783 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001784
Ben Blum74a11662011-05-26 16:25:20 -07001785 /* as per above, nr_threads may decrease, but not increase. */
1786 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001787 ent.task = tsk;
1788 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001789 /* nothing to do if this task is already in the cgroup */
1790 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001791 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001792 /*
1793 * saying GFP_ATOMIC has no effect here because we did prealloc
1794 * earlier, but it's good form to communicate our expectations.
1795 */
Tejun Heo134d3372011-12-12 18:12:21 -08001796 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07001797 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07001798 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08001799 next:
Li Zefan081aa452013-03-13 09:17:09 +08001800 if (!threadgroup)
1801 break;
Ben Blum74a11662011-05-26 16:25:20 -07001802 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001803 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07001804 /* remember the number of threads in the array for later. */
1805 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001806 tset.tc_array = group;
1807 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001808
Tejun Heo134d3372011-12-12 18:12:21 -08001809 /* methods shouldn't be called if no task is actually migrating */
1810 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001811 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08001812 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08001813
Ben Blum74a11662011-05-26 16:25:20 -07001814 /*
1815 * step 1: check that we can legitimately attach to the cgroup.
1816 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001817 for_each_css(css, i, cgrp) {
1818 if (css->ss->can_attach) {
1819 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001820 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001821 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07001822 goto out_cancel_attach;
1823 }
1824 }
Ben Blum74a11662011-05-26 16:25:20 -07001825 }
1826
1827 /*
1828 * step 2: make sure css_sets exist for all threads to be migrated.
1829 * we use find_css_set, which allocates a new one if necessary.
1830 */
Ben Blum74a11662011-05-26 16:25:20 -07001831 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07001832 struct css_set *old_cset;
1833
Tejun Heo134d3372011-12-12 18:12:21 -08001834 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001835 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001836 tc->cset = find_css_set(old_cset, cgrp);
1837 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001838 retval = -ENOMEM;
1839 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07001840 }
1841 }
1842
1843 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001844 * step 3: now that we're guaranteed success wrt the css_sets,
1845 * proceed to move all tasks to the new cgroup. There are no
1846 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07001847 */
Ben Blum74a11662011-05-26 16:25:20 -07001848 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08001849 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001850 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07001851 }
1852 /* nothing is sensitive to fork() after this point. */
1853
1854 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001855 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07001856 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001857 for_each_css(css, i, cgrp)
1858 if (css->ss->attach)
1859 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001860
1861 /*
1862 * step 5: success! and cleanup
1863 */
Ben Blum74a11662011-05-26 16:25:20 -07001864 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001865out_put_css_set_refs:
1866 if (retval) {
1867 for (i = 0; i < group_size; i++) {
1868 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001869 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001870 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001871 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001872 }
Ben Blum74a11662011-05-26 16:25:20 -07001873 }
1874out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07001875 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001876 for_each_css(css, i, cgrp) {
1877 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07001878 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001879 if (css->ss->cancel_attach)
1880 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001881 }
1882 }
Ben Blum74a11662011-05-26 16:25:20 -07001883out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07001884 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07001885 return retval;
1886}
1887
1888/*
1889 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08001890 * function to attach either it or all tasks in its threadgroup. Will lock
1891 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07001892 */
1893static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001894{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001895 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001896 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001897 int ret;
1898
Ben Blum74a11662011-05-26 16:25:20 -07001899 if (!cgroup_lock_live_group(cgrp))
1900 return -ENODEV;
1901
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001902retry_find_task:
1903 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001904 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001905 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07001906 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07001907 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001908 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001909 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001910 }
Ben Blum74a11662011-05-26 16:25:20 -07001911 /*
1912 * even if we're attaching all tasks in the thread group, we
1913 * only need to check permissions on one of them.
1914 */
David Howellsc69e8d92008-11-14 10:39:19 +11001915 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07001916 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
1917 !uid_eq(cred->euid, tcred->uid) &&
1918 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001919 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001920 ret = -EACCES;
1921 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001922 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001923 } else
1924 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08001925
1926 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001927 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02001928
1929 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07001930 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02001931 * trapped in a cpuset, or RT worker may be born in a cgroup
1932 * with no rt_runtime allocated. Just say no.
1933 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07001934 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02001935 ret = -EINVAL;
1936 rcu_read_unlock();
1937 goto out_unlock_cgroup;
1938 }
1939
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001940 get_task_struct(tsk);
1941 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08001942
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001943 threadgroup_lock(tsk);
1944 if (threadgroup) {
1945 if (!thread_group_leader(tsk)) {
1946 /*
1947 * a race with de_thread from another thread's exec()
1948 * may strip us of our leadership, if this happens,
1949 * there is no choice but to throw this task away and
1950 * try again; this is
1951 * "double-double-toil-and-trouble-check locking".
1952 */
1953 threadgroup_unlock(tsk);
1954 put_task_struct(tsk);
1955 goto retry_find_task;
1956 }
Li Zefan081aa452013-03-13 09:17:09 +08001957 }
1958
1959 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
1960
Tejun Heocd3d0952011-12-12 18:12:21 -08001961 threadgroup_unlock(tsk);
1962
Paul Menagebbcb81d2007-10-18 23:39:32 -07001963 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001964out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07001965 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001966 return ret;
1967}
1968
Tejun Heo7ae1bad2013-04-07 09:29:51 -07001969/**
1970 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1971 * @from: attach to all cgroups of a given task
1972 * @tsk: the task to be attached
1973 */
1974int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
1975{
1976 struct cgroupfs_root *root;
1977 int retval = 0;
1978
Tejun Heo47cfcd02013-04-07 09:29:51 -07001979 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07001980 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08001981 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07001982
Li Zefan6f4b7e62013-07-31 16:18:36 +08001983 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07001984 if (retval)
1985 break;
1986 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07001987 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07001988
1989 return retval;
1990}
1991EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
1992
Tejun Heo182446d2013-08-08 20:11:24 -04001993static int cgroup_tasks_write(struct cgroup_subsys_state *css,
1994 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07001995{
Tejun Heo182446d2013-08-08 20:11:24 -04001996 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07001997}
1998
Tejun Heo182446d2013-08-08 20:11:24 -04001999static int cgroup_procs_write(struct cgroup_subsys_state *css,
2000 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002001{
Tejun Heo182446d2013-08-08 20:11:24 -04002002 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002003}
2004
Tejun Heo182446d2013-08-08 20:11:24 -04002005static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2006 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002007{
Tejun Heo5f469902014-02-11 11:52:48 -05002008 struct cgroupfs_root *root = css->cgroup->root;
2009
2010 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002011 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002012 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002013 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002014 strlcpy(root->release_agent_path, buffer,
2015 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002016 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002017 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002018 return 0;
2019}
2020
Tejun Heo2da8ca82013-12-05 12:28:04 -05002021static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002022{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002023 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002024
Paul Menagee788e062008-07-25 01:46:59 -07002025 if (!cgroup_lock_live_group(cgrp))
2026 return -ENODEV;
2027 seq_puts(seq, cgrp->root->release_agent_path);
2028 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002029 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002030 return 0;
2031}
2032
Tejun Heo2da8ca82013-12-05 12:28:04 -05002033static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002034{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002035 struct cgroup *cgrp = seq_css(seq)->cgroup;
2036
2037 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002038 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002039}
2040
Tejun Heo2bd59d42014-02-11 11:52:49 -05002041static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2042 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002043{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002044 struct cgroup *cgrp = of->kn->parent->priv;
2045 struct cftype *cft = of->kn->priv;
2046 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002047 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002048
Tejun Heo2bd59d42014-02-11 11:52:49 -05002049 /*
2050 * kernfs guarantees that a file isn't deleted with operations in
2051 * flight, which means that the matching css is and stays alive and
2052 * doesn't need to be pinned. The RCU locking is not necessary
2053 * either. It's just for the convenience of using cgroup_css().
2054 */
2055 rcu_read_lock();
2056 css = cgroup_css(cgrp, cft->ss);
2057 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002058
Tejun Heoa742c592013-12-05 12:28:03 -05002059 if (cft->write_string) {
2060 ret = cft->write_string(css, cft, strstrip(buf));
2061 } else if (cft->write_u64) {
2062 unsigned long long v;
2063 ret = kstrtoull(buf, 0, &v);
2064 if (!ret)
2065 ret = cft->write_u64(css, cft, v);
2066 } else if (cft->write_s64) {
2067 long long v;
2068 ret = kstrtoll(buf, 0, &v);
2069 if (!ret)
2070 ret = cft->write_s64(css, cft, v);
2071 } else if (cft->trigger) {
2072 ret = cft->trigger(css, (unsigned int)cft->private);
2073 } else {
2074 ret = -EINVAL;
2075 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002076
Tejun Heoa742c592013-12-05 12:28:03 -05002077 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002078}
2079
Tejun Heo6612f052013-12-05 12:28:04 -05002080static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002081{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002082 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002083}
2084
2085static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2086{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002087 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002088}
2089
2090static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2091{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002092 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002093}
2094
2095static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2096{
Tejun Heo7da11272013-12-05 12:28:04 -05002097 struct cftype *cft = seq_cft(m);
2098 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002099
Tejun Heo2da8ca82013-12-05 12:28:04 -05002100 if (cft->seq_show)
2101 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002102
Tejun Heo896f5192013-12-05 12:28:04 -05002103 if (cft->read_u64)
2104 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2105 else if (cft->read_s64)
2106 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2107 else
2108 return -EINVAL;
2109 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002110}
2111
Tejun Heo2bd59d42014-02-11 11:52:49 -05002112static struct kernfs_ops cgroup_kf_single_ops = {
2113 .atomic_write_len = PAGE_SIZE,
2114 .write = cgroup_file_write,
2115 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002116};
2117
Tejun Heo2bd59d42014-02-11 11:52:49 -05002118static struct kernfs_ops cgroup_kf_ops = {
2119 .atomic_write_len = PAGE_SIZE,
2120 .write = cgroup_file_write,
2121 .seq_start = cgroup_seqfile_start,
2122 .seq_next = cgroup_seqfile_next,
2123 .seq_stop = cgroup_seqfile_stop,
2124 .seq_show = cgroup_seqfile_show,
2125};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002126
2127/*
2128 * cgroup_rename - Only allow simple rename of directories in place.
2129 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002130static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2131 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002132{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002133 struct cgroup *cgrp = kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002134 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002135
Tejun Heo2bd59d42014-02-11 11:52:49 -05002136 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002137 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002138 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002139 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002140
Tejun Heo6db8e852013-06-14 11:18:22 -07002141 /*
2142 * This isn't a proper migration and its usefulness is very
2143 * limited. Disallow if sane_behavior.
2144 */
2145 if (cgroup_sane_behavior(cgrp))
2146 return -EPERM;
2147
Tejun Heo2bd59d42014-02-11 11:52:49 -05002148 mutex_lock(&cgroup_tree_mutex);
2149 mutex_lock(&cgroup_mutex);
2150
2151 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002152
Tejun Heo2bd59d42014-02-11 11:52:49 -05002153 mutex_unlock(&cgroup_mutex);
2154 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002155 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002156}
2157
Tejun Heo2bb566c2013-08-08 20:11:23 -04002158static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002159{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002160 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002161 struct kernfs_node *kn;
2162 struct lock_class_key *key = NULL;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002163
Tejun Heo2bd59d42014-02-11 11:52:49 -05002164#ifdef CONFIG_DEBUG_LOCK_ALLOC
2165 key = &cft->lockdep_key;
2166#endif
2167 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2168 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2169 NULL, false, key);
2170 if (IS_ERR(kn))
2171 return PTR_ERR(kn);
2172 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002173}
2174
Tejun Heob1f28d32013-06-28 16:24:10 -07002175/**
2176 * cgroup_addrm_files - add or remove files to a cgroup directory
2177 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002178 * @cfts: array of cftypes to be added
2179 * @is_add: whether to add or remove
2180 *
2181 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002182 * For removals, this function never fails. If addition fails, this
2183 * function doesn't remove files already added. The caller is responsible
2184 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002185 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002186static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2187 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002188{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002189 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002190 int ret;
2191
Tejun Heoace2bee2014-02-11 11:52:47 -05002192 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002193
2194 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002195 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002196 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2197 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002198 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2199 continue;
2200 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2201 continue;
2202
Li Zefan2739d3c2013-01-21 18:18:33 +08002203 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002204 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002205 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002206 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002207 cft->name, ret);
2208 return ret;
2209 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002210 } else {
2211 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002212 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002213 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002214 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002215}
2216
Tejun Heo21a2d342014-02-12 09:29:49 -05002217static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002218{
2219 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002220 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002221 struct cgroup *root = &ss->root->top_cgroup;
Tejun Heo492eb212013-08-08 20:11:25 -04002222 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002223 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002224
Tejun Heo21a2d342014-02-12 09:29:49 -05002225 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo4ac06012014-02-11 11:52:47 -05002226
Tejun Heo21a2d342014-02-12 09:29:49 -05002227 /* don't bother if @ss isn't attached */
2228 if (ss->root == &cgroup_dummy_root)
Tejun Heo9ccece82013-06-28 16:24:11 -07002229 return 0;
Li Zefane8c82d22013-06-18 18:48:37 +08002230
Li Zefane8c82d22013-06-18 18:48:37 +08002231 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002232 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002233 struct cgroup *cgrp = css->cgroup;
2234
Li Zefane8c82d22013-06-18 18:48:37 +08002235 if (cgroup_is_dead(cgrp))
2236 continue;
2237
Tejun Heo21a2d342014-02-12 09:29:49 -05002238 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002239 if (ret)
2240 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002241 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002242
2243 if (is_add && !ret)
2244 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002245 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002246}
2247
Tejun Heo2da440a2014-02-11 11:52:48 -05002248static void cgroup_exit_cftypes(struct cftype *cfts)
2249{
2250 struct cftype *cft;
2251
Tejun Heo2bd59d42014-02-11 11:52:49 -05002252 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2253 /* free copy for custom atomic_write_len, see init_cftypes() */
2254 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2255 kfree(cft->kf_ops);
2256 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002257 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002258 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002259}
2260
Tejun Heo2bd59d42014-02-11 11:52:49 -05002261static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002262{
2263 struct cftype *cft;
2264
Tejun Heo2bd59d42014-02-11 11:52:49 -05002265 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2266 struct kernfs_ops *kf_ops;
2267
Tejun Heo0adb0702014-02-12 09:29:48 -05002268 WARN_ON(cft->ss || cft->kf_ops);
2269
Tejun Heo2bd59d42014-02-11 11:52:49 -05002270 if (cft->seq_start)
2271 kf_ops = &cgroup_kf_ops;
2272 else
2273 kf_ops = &cgroup_kf_single_ops;
2274
2275 /*
2276 * Ugh... if @cft wants a custom max_write_len, we need to
2277 * make a copy of kf_ops to set its atomic_write_len.
2278 */
2279 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2280 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2281 if (!kf_ops) {
2282 cgroup_exit_cftypes(cfts);
2283 return -ENOMEM;
2284 }
2285 kf_ops->atomic_write_len = cft->max_write_len;
2286 }
2287
2288 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002289 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002290 }
2291
2292 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002293}
2294
Tejun Heo21a2d342014-02-12 09:29:49 -05002295static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2296{
2297 lockdep_assert_held(&cgroup_tree_mutex);
2298
2299 if (!cfts || !cfts[0].ss)
2300 return -ENOENT;
2301
2302 list_del(&cfts->node);
2303 cgroup_apply_cftypes(cfts, false);
2304 cgroup_exit_cftypes(cfts);
2305 return 0;
2306}
2307
Tejun Heo8e3f6542012-04-01 12:09:55 -07002308/**
Tejun Heo80b13582014-02-12 09:29:48 -05002309 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2310 * @cfts: zero-length name terminated array of cftypes
2311 *
2312 * Unregister @cfts. Files described by @cfts are removed from all
2313 * existing cgroups and all future cgroups won't have them either. This
2314 * function can be called anytime whether @cfts' subsys is attached or not.
2315 *
2316 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2317 * registered.
2318 */
2319int cgroup_rm_cftypes(struct cftype *cfts)
2320{
Tejun Heo21a2d342014-02-12 09:29:49 -05002321 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002322
Tejun Heo21a2d342014-02-12 09:29:49 -05002323 mutex_lock(&cgroup_tree_mutex);
2324 ret = cgroup_rm_cftypes_locked(cfts);
2325 mutex_unlock(&cgroup_tree_mutex);
2326 return ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002327}
2328
2329/**
Tejun Heo8e3f6542012-04-01 12:09:55 -07002330 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2331 * @ss: target cgroup subsystem
2332 * @cfts: zero-length name terminated array of cftypes
2333 *
2334 * Register @cfts to @ss. Files described by @cfts are created for all
2335 * existing cgroups to which @ss is attached and all future cgroups will
2336 * have them too. This function can be called anytime whether @ss is
2337 * attached or not.
2338 *
2339 * Returns 0 on successful registration, -errno on failure. Note that this
2340 * function currently returns 0 as long as @cfts registration is successful
2341 * even if some file creation attempts on existing cgroups fail.
2342 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002343int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002344{
Tejun Heo9ccece82013-06-28 16:24:11 -07002345 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002346
Tejun Heo2bd59d42014-02-11 11:52:49 -05002347 ret = cgroup_init_cftypes(ss, cfts);
2348 if (ret)
2349 return ret;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002350
Tejun Heo21a2d342014-02-12 09:29:49 -05002351 mutex_lock(&cgroup_tree_mutex);
2352
Tejun Heo0adb0702014-02-12 09:29:48 -05002353 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05002354 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002355 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05002356 cgroup_rm_cftypes_locked(cfts);
2357
2358 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002359 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002360}
2361EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2362
Li Zefana043e3b2008-02-23 15:24:09 -08002363/**
2364 * cgroup_task_count - count the number of tasks in a cgroup.
2365 * @cgrp: the cgroup in question
2366 *
2367 * Return the number of tasks in the cgroup.
2368 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002369int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002370{
2371 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002372 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002373
Paul Menage817929e2007-10-18 23:39:36 -07002374 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002375 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2376 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002377 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002378 return count;
2379}
2380
2381/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002382 * To reduce the fork() overhead for systems that are not actually using
2383 * their cgroups capability, we don't maintain the lists running through
2384 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002385 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002386 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002387static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002388{
2389 struct task_struct *p, *g;
2390 write_lock(&css_set_lock);
2391 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002392 /*
2393 * We need tasklist_lock because RCU is not safe against
2394 * while_each_thread(). Besides, a forking task that has passed
2395 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2396 * is not guaranteed to have its child immediately visible in the
2397 * tasklist if we walk through it with RCU.
2398 */
2399 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002400 do_each_thread(g, p) {
2401 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002402 /*
2403 * We should check if the process is exiting, otherwise
2404 * it will race with cgroup_exit() in that the list
2405 * entry won't be deleted though the process has exited.
2406 */
2407 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002408 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002409 task_unlock(p);
2410 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002411 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002412 write_unlock(&css_set_lock);
2413}
2414
Tejun Heo574bd9f2012-11-09 09:12:29 -08002415/**
Tejun Heo492eb212013-08-08 20:11:25 -04002416 * css_next_child - find the next child of a given css
2417 * @pos_css: the current position (%NULL to initiate traversal)
2418 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002419 *
Tejun Heo492eb212013-08-08 20:11:25 -04002420 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002421 * under either cgroup_mutex or RCU read lock. The only requirement is
2422 * that @parent_css and @pos_css are accessible. The next sibling is
2423 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002424 */
Tejun Heo492eb212013-08-08 20:11:25 -04002425struct cgroup_subsys_state *
2426css_next_child(struct cgroup_subsys_state *pos_css,
2427 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002428{
Tejun Heo492eb212013-08-08 20:11:25 -04002429 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2430 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002431 struct cgroup *next;
2432
Tejun Heoace2bee2014-02-11 11:52:47 -05002433 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002434
2435 /*
2436 * @pos could already have been removed. Once a cgroup is removed,
2437 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002438 * changes. As CGRP_DEAD assertion is serialized and happens
2439 * before the cgroup is taken off the ->sibling list, if we see it
2440 * unasserted, it's guaranteed that the next sibling hasn't
2441 * finished its grace period even if it's already removed, and thus
2442 * safe to dereference from this RCU critical section. If
2443 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2444 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002445 *
2446 * If @pos is dead, its next pointer can't be dereferenced;
2447 * however, as each cgroup is given a monotonically increasing
2448 * unique serial number and always appended to the sibling list,
2449 * the next one can be found by walking the parent's children until
2450 * we see a cgroup with higher serial number than @pos's. While
2451 * this path can be slower, it's taken only when either the current
2452 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002453 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002454 if (!pos) {
2455 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2456 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002457 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002458 } else {
2459 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2460 if (next->serial_nr > pos->serial_nr)
2461 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002462 }
2463
Tejun Heo492eb212013-08-08 20:11:25 -04002464 if (&next->sibling == &cgrp->children)
2465 return NULL;
2466
Tejun Heoca8bdca2013-08-26 18:40:56 -04002467 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002468}
Tejun Heo492eb212013-08-08 20:11:25 -04002469EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002470
2471/**
Tejun Heo492eb212013-08-08 20:11:25 -04002472 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002473 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002474 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002475 *
Tejun Heo492eb212013-08-08 20:11:25 -04002476 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002477 * to visit for pre-order traversal of @root's descendants. @root is
2478 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002479 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002480 * While this function requires cgroup_mutex or RCU read locking, it
2481 * doesn't require the whole traversal to be contained in a single critical
2482 * section. This function will return the correct next descendant as long
2483 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002484 */
Tejun Heo492eb212013-08-08 20:11:25 -04002485struct cgroup_subsys_state *
2486css_next_descendant_pre(struct cgroup_subsys_state *pos,
2487 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002488{
Tejun Heo492eb212013-08-08 20:11:25 -04002489 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002490
Tejun Heoace2bee2014-02-11 11:52:47 -05002491 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002492
Tejun Heobd8815a2013-08-08 20:11:27 -04002493 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002494 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002495 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002496
2497 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002498 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002499 if (next)
2500 return next;
2501
2502 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002503 while (pos != root) {
2504 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002505 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002506 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002507 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002508 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002509
2510 return NULL;
2511}
Tejun Heo492eb212013-08-08 20:11:25 -04002512EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002513
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002514/**
Tejun Heo492eb212013-08-08 20:11:25 -04002515 * css_rightmost_descendant - return the rightmost descendant of a css
2516 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002517 *
Tejun Heo492eb212013-08-08 20:11:25 -04002518 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2519 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002520 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002521 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002522 * While this function requires cgroup_mutex or RCU read locking, it
2523 * doesn't require the whole traversal to be contained in a single critical
2524 * section. This function will return the correct rightmost descendant as
2525 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002526 */
Tejun Heo492eb212013-08-08 20:11:25 -04002527struct cgroup_subsys_state *
2528css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002529{
Tejun Heo492eb212013-08-08 20:11:25 -04002530 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002531
Tejun Heoace2bee2014-02-11 11:52:47 -05002532 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002533
2534 do {
2535 last = pos;
2536 /* ->prev isn't RCU safe, walk ->next till the end */
2537 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002538 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002539 pos = tmp;
2540 } while (pos);
2541
2542 return last;
2543}
Tejun Heo492eb212013-08-08 20:11:25 -04002544EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002545
Tejun Heo492eb212013-08-08 20:11:25 -04002546static struct cgroup_subsys_state *
2547css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002548{
Tejun Heo492eb212013-08-08 20:11:25 -04002549 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002550
2551 do {
2552 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04002553 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002554 } while (pos);
2555
2556 return last;
2557}
2558
2559/**
Tejun Heo492eb212013-08-08 20:11:25 -04002560 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002561 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002562 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002563 *
Tejun Heo492eb212013-08-08 20:11:25 -04002564 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002565 * to visit for post-order traversal of @root's descendants. @root is
2566 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002567 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002568 * While this function requires cgroup_mutex or RCU read locking, it
2569 * doesn't require the whole traversal to be contained in a single critical
2570 * section. This function will return the correct next descendant as long
2571 * as both @pos and @cgroup are accessible and @pos is a descendant of
2572 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002573 */
Tejun Heo492eb212013-08-08 20:11:25 -04002574struct cgroup_subsys_state *
2575css_next_descendant_post(struct cgroup_subsys_state *pos,
2576 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002577{
Tejun Heo492eb212013-08-08 20:11:25 -04002578 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002579
Tejun Heoace2bee2014-02-11 11:52:47 -05002580 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002581
Tejun Heo58b79a92013-09-06 15:31:08 -04002582 /* if first iteration, visit leftmost descendant which may be @root */
2583 if (!pos)
2584 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002585
Tejun Heobd8815a2013-08-08 20:11:27 -04002586 /* if we visited @root, we're done */
2587 if (pos == root)
2588 return NULL;
2589
Tejun Heo574bd9f2012-11-09 09:12:29 -08002590 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04002591 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002592 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04002593 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002594
2595 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04002596 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002597}
Tejun Heo492eb212013-08-08 20:11:25 -04002598EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002599
Tejun Heo0942eee2013-08-08 20:11:26 -04002600/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002601 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04002602 * @it: the iterator to advance
2603 *
2604 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04002605 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002606static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04002607{
2608 struct list_head *l = it->cset_link;
2609 struct cgrp_cset_link *link;
2610 struct css_set *cset;
2611
2612 /* Advance to the next non-empty css_set */
2613 do {
2614 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04002615 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04002616 it->cset_link = NULL;
2617 return;
2618 }
2619 link = list_entry(l, struct cgrp_cset_link, cset_link);
2620 cset = link->cset;
2621 } while (list_empty(&cset->tasks));
2622 it->cset_link = l;
2623 it->task = cset->tasks.next;
2624}
2625
Tejun Heo0942eee2013-08-08 20:11:26 -04002626/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002627 * css_task_iter_start - initiate task iteration
2628 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04002629 * @it: the task iterator to use
2630 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002631 * Initiate iteration through the tasks of @css. The caller can call
2632 * css_task_iter_next() to walk through the tasks until the function
2633 * returns NULL. On completion of iteration, css_task_iter_end() must be
2634 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04002635 *
2636 * Note that this function acquires a lock which is released when the
2637 * iteration finishes. The caller can't sleep while iteration is in
2638 * progress.
2639 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002640void css_task_iter_start(struct cgroup_subsys_state *css,
2641 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002642 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002643{
2644 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04002645 * The first time anyone tries to iterate across a css, we need to
2646 * enable the list linking each css_set to its tasks, and fix up
2647 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07002648 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002649 if (!use_task_css_set_links)
2650 cgroup_enable_task_cg_lists();
2651
Paul Menage817929e2007-10-18 23:39:36 -07002652 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002653
Tejun Heo72ec7022013-08-08 20:11:26 -04002654 it->origin_css = css;
2655 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002656
Tejun Heo72ec7022013-08-08 20:11:26 -04002657 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002658}
2659
Tejun Heo0942eee2013-08-08 20:11:26 -04002660/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002661 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04002662 * @it: the task iterator being iterated
2663 *
2664 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04002665 * initialized via css_task_iter_start(). Returns NULL when the iteration
2666 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04002667 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002668struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002669{
2670 struct task_struct *res;
2671 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07002672 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002673
2674 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07002675 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07002676 return NULL;
2677 res = list_entry(l, struct task_struct, cg_list);
2678 /* Advance iterator to find next entry */
2679 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002680 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
2681 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04002682 /*
2683 * We reached the end of this task list - move on to the
2684 * next cgrp_cset_link.
2685 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002686 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002687 } else {
2688 it->task = l;
2689 }
2690 return res;
2691}
2692
Tejun Heo0942eee2013-08-08 20:11:26 -04002693/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002694 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04002695 * @it: the task iterator to finish
2696 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002697 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04002698 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002699void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002700 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002701{
2702 read_unlock(&css_set_lock);
2703}
2704
Cliff Wickman31a7df02008-02-07 00:14:42 -08002705static inline int started_after_time(struct task_struct *t1,
2706 struct timespec *time,
2707 struct task_struct *t2)
2708{
2709 int start_diff = timespec_compare(&t1->start_time, time);
2710 if (start_diff > 0) {
2711 return 1;
2712 } else if (start_diff < 0) {
2713 return 0;
2714 } else {
2715 /*
2716 * Arbitrarily, if two processes started at the same
2717 * time, we'll say that the lower pointer value
2718 * started first. Note that t2 may have exited by now
2719 * so this may not be a valid pointer any longer, but
2720 * that's fine - it still serves to distinguish
2721 * between two tasks started (effectively) simultaneously.
2722 */
2723 return t1 > t2;
2724 }
2725}
2726
2727/*
2728 * This function is a callback from heap_insert() and is used to order
2729 * the heap.
2730 * In this case we order the heap in descending task start time.
2731 */
2732static inline int started_after(void *p1, void *p2)
2733{
2734 struct task_struct *t1 = p1;
2735 struct task_struct *t2 = p2;
2736 return started_after_time(t1, &t2->start_time, t2);
2737}
2738
2739/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002740 * css_scan_tasks - iterate though all the tasks in a css
2741 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04002742 * @test: optional test callback
2743 * @process: process callback
2744 * @data: data passed to @test and @process
2745 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08002746 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002747 * Iterate through all the tasks in @css, calling @test for each, and if it
2748 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08002749 *
Tejun Heoe5358372013-08-08 20:11:26 -04002750 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04002751 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04002752 * lock css_set_lock for the call to @process.
2753 *
2754 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04002755 * of @css for the duration of this call. This function may or may not
2756 * call @process for tasks that exit or move to a different css during the
2757 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04002758 *
2759 * Note that @test may be called with locks held, and may in some
2760 * situations be called multiple times for the same task, so it should be
2761 * cheap.
2762 *
2763 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
2764 * heap operations (and its "gt" member will be overwritten), else a
2765 * temporary heap will be used (allocation of which may cause this function
2766 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08002767 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002768int css_scan_tasks(struct cgroup_subsys_state *css,
2769 bool (*test)(struct task_struct *, void *),
2770 void (*process)(struct task_struct *, void *),
2771 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002772{
2773 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04002774 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002775 struct task_struct *p, *dropped;
2776 /* Never dereference latest_task, since it's not refcounted */
2777 struct task_struct *latest_task = NULL;
2778 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002779 struct timespec latest_time = { 0, 0 };
2780
Tejun Heoe5358372013-08-08 20:11:26 -04002781 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08002782 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002783 heap->gt = &started_after;
2784 } else {
2785 /* We need to allocate our own heap memory */
2786 heap = &tmp_heap;
2787 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2788 if (retval)
2789 /* cannot allocate the heap */
2790 return retval;
2791 }
2792
2793 again:
2794 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04002795 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04002796 * which are of interest, and invoking @process callback on the
2797 * ones which need an update. Since we don't want to hold any
2798 * locks during the task updates, gather tasks to be processed in a
2799 * heap structure. The heap is sorted by descending task start
2800 * time. If the statically-sized heap fills up, we overflow tasks
2801 * that started later, and in future iterations only consider tasks
2802 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08002803 * guarantees forward progress and that we don't miss any tasks.
2804 */
2805 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04002806 css_task_iter_start(css, &it);
2807 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08002808 /*
2809 * Only affect tasks that qualify per the caller's callback,
2810 * if he provided one
2811 */
Tejun Heoe5358372013-08-08 20:11:26 -04002812 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002813 continue;
2814 /*
2815 * Only process tasks that started after the last task
2816 * we processed
2817 */
2818 if (!started_after_time(p, &latest_time, latest_task))
2819 continue;
2820 dropped = heap_insert(heap, p);
2821 if (dropped == NULL) {
2822 /*
2823 * The new task was inserted; the heap wasn't
2824 * previously full
2825 */
2826 get_task_struct(p);
2827 } else if (dropped != p) {
2828 /*
2829 * The new task was inserted, and pushed out a
2830 * different task
2831 */
2832 get_task_struct(p);
2833 put_task_struct(dropped);
2834 }
2835 /*
2836 * Else the new task was newer than anything already in
2837 * the heap and wasn't inserted
2838 */
2839 }
Tejun Heo72ec7022013-08-08 20:11:26 -04002840 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002841
2842 if (heap->size) {
2843 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002844 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002845 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002846 latest_time = q->start_time;
2847 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002848 }
2849 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04002850 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07002851 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002852 }
2853 /*
2854 * If we had to process any tasks at all, scan again
2855 * in case some of them were in the middle of forking
2856 * children that didn't get processed.
2857 * Not the most efficient way to do it, but it avoids
2858 * having to take callback_mutex in the fork path
2859 */
2860 goto again;
2861 }
2862 if (heap == &tmp_heap)
2863 heap_free(&tmp_heap);
2864 return 0;
2865}
2866
Tejun Heoe5358372013-08-08 20:11:26 -04002867static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07002868{
Tejun Heoe5358372013-08-08 20:11:26 -04002869 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07002870
Tejun Heo47cfcd02013-04-07 09:29:51 -07002871 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07002872 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002873 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07002874}
2875
2876/**
2877 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
2878 * @to: cgroup to which the tasks will be moved
2879 * @from: cgroup in which the tasks currently reside
2880 */
2881int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
2882{
Tejun Heo72ec7022013-08-08 20:11:26 -04002883 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
2884 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07002885}
2886
Paul Menage817929e2007-10-18 23:39:36 -07002887/*
Ben Blum102a7752009-09-23 15:56:26 -07002888 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002889 *
2890 * Reading this file can return large amounts of data if a cgroup has
2891 * *lots* of attached tasks. So it may need several calls to read(),
2892 * but we cannot guarantee that the information we produce is correct
2893 * unless we produce it entirely atomically.
2894 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002895 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002896
Li Zefan24528252012-01-20 11:58:43 +08002897/* which pidlist file are we talking about? */
2898enum cgroup_filetype {
2899 CGROUP_FILE_PROCS,
2900 CGROUP_FILE_TASKS,
2901};
2902
2903/*
2904 * A pidlist is a list of pids that virtually represents the contents of one
2905 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
2906 * a pair (one each for procs, tasks) for each pid namespace that's relevant
2907 * to the cgroup.
2908 */
2909struct cgroup_pidlist {
2910 /*
2911 * used to find which pidlist is wanted. doesn't change as long as
2912 * this particular list stays in the list.
2913 */
2914 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
2915 /* array of xids */
2916 pid_t *list;
2917 /* how many elements the above list has */
2918 int length;
Li Zefan24528252012-01-20 11:58:43 +08002919 /* each of these stored in a list by its cgroup */
2920 struct list_head links;
2921 /* pointer to the cgroup we belong to, for list removal purposes */
2922 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05002923 /* for delayed destruction */
2924 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08002925};
2926
Paul Menagebbcb81d2007-10-18 23:39:32 -07002927/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002928 * The following two functions "fix" the issue where there are more pids
2929 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2930 * TODO: replace with a kernel-wide solution to this problem
2931 */
2932#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2933static void *pidlist_allocate(int count)
2934{
2935 if (PIDLIST_TOO_LARGE(count))
2936 return vmalloc(count * sizeof(pid_t));
2937 else
2938 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2939}
Tejun Heob1a21362013-11-29 10:42:58 -05002940
Ben Blumd1d9fd32009-09-23 15:56:28 -07002941static void pidlist_free(void *p)
2942{
2943 if (is_vmalloc_addr(p))
2944 vfree(p);
2945 else
2946 kfree(p);
2947}
Ben Blumd1d9fd32009-09-23 15:56:28 -07002948
2949/*
Tejun Heob1a21362013-11-29 10:42:58 -05002950 * Used to destroy all pidlists lingering waiting for destroy timer. None
2951 * should be left afterwards.
2952 */
2953static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
2954{
2955 struct cgroup_pidlist *l, *tmp_l;
2956
2957 mutex_lock(&cgrp->pidlist_mutex);
2958 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
2959 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
2960 mutex_unlock(&cgrp->pidlist_mutex);
2961
2962 flush_workqueue(cgroup_pidlist_destroy_wq);
2963 BUG_ON(!list_empty(&cgrp->pidlists));
2964}
2965
2966static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
2967{
2968 struct delayed_work *dwork = to_delayed_work(work);
2969 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
2970 destroy_dwork);
2971 struct cgroup_pidlist *tofree = NULL;
2972
2973 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05002974
2975 /*
Tejun Heo04502362013-11-29 10:42:59 -05002976 * Destroy iff we didn't get queued again. The state won't change
2977 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05002978 */
Tejun Heo04502362013-11-29 10:42:59 -05002979 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05002980 list_del(&l->links);
2981 pidlist_free(l->list);
2982 put_pid_ns(l->key.ns);
2983 tofree = l;
2984 }
2985
Tejun Heob1a21362013-11-29 10:42:58 -05002986 mutex_unlock(&l->owner->pidlist_mutex);
2987 kfree(tofree);
2988}
2989
2990/*
Ben Blum102a7752009-09-23 15:56:26 -07002991 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07002992 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002993 */
Li Zefan6ee211a2013-03-12 15:36:00 -07002994static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002995{
Ben Blum102a7752009-09-23 15:56:26 -07002996 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07002997
2998 /*
2999 * we presume the 0th element is unique, so i starts at 1. trivial
3000 * edge cases first; no work needs to be done for either
3001 */
3002 if (length == 0 || length == 1)
3003 return length;
3004 /* src and dest walk down the list; dest counts unique elements */
3005 for (src = 1; src < length; src++) {
3006 /* find next unique element */
3007 while (list[src] == list[src-1]) {
3008 src++;
3009 if (src == length)
3010 goto after;
3011 }
3012 /* dest always points to where the next unique element goes */
3013 list[dest] = list[src];
3014 dest++;
3015 }
3016after:
Ben Blum102a7752009-09-23 15:56:26 -07003017 return dest;
3018}
3019
Tejun Heoafb2bc12013-11-29 10:42:59 -05003020/*
3021 * The two pid files - task and cgroup.procs - guaranteed that the result
3022 * is sorted, which forced this whole pidlist fiasco. As pid order is
3023 * different per namespace, each namespace needs differently sorted list,
3024 * making it impossible to use, for example, single rbtree of member tasks
3025 * sorted by task pointer. As pidlists can be fairly large, allocating one
3026 * per open file is dangerous, so cgroup had to implement shared pool of
3027 * pidlists keyed by cgroup and namespace.
3028 *
3029 * All this extra complexity was caused by the original implementation
3030 * committing to an entirely unnecessary property. In the long term, we
3031 * want to do away with it. Explicitly scramble sort order if
3032 * sane_behavior so that no such expectation exists in the new interface.
3033 *
3034 * Scrambling is done by swapping every two consecutive bits, which is
3035 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3036 */
3037static pid_t pid_fry(pid_t pid)
3038{
3039 unsigned a = pid & 0x55555555;
3040 unsigned b = pid & 0xAAAAAAAA;
3041
3042 return (a << 1) | (b >> 1);
3043}
3044
3045static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3046{
3047 if (cgroup_sane_behavior(cgrp))
3048 return pid_fry(pid);
3049 else
3050 return pid;
3051}
3052
Ben Blum102a7752009-09-23 15:56:26 -07003053static int cmppid(const void *a, const void *b)
3054{
3055 return *(pid_t *)a - *(pid_t *)b;
3056}
3057
Tejun Heoafb2bc12013-11-29 10:42:59 -05003058static int fried_cmppid(const void *a, const void *b)
3059{
3060 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3061}
3062
Ben Blum72a8cb32009-09-23 15:56:27 -07003063static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3064 enum cgroup_filetype type)
3065{
3066 struct cgroup_pidlist *l;
3067 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003068 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003069
Tejun Heoe6b81712013-11-29 10:42:59 -05003070 lockdep_assert_held(&cgrp->pidlist_mutex);
3071
3072 list_for_each_entry(l, &cgrp->pidlists, links)
3073 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003074 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003075 return NULL;
3076}
3077
Ben Blum72a8cb32009-09-23 15:56:27 -07003078/*
3079 * find the appropriate pidlist for our purpose (given procs vs tasks)
3080 * returns with the lock on that pidlist already held, and takes care
3081 * of the use count, or returns NULL with no locks held if we're out of
3082 * memory.
3083 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003084static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3085 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003086{
3087 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003088
Tejun Heoe6b81712013-11-29 10:42:59 -05003089 lockdep_assert_held(&cgrp->pidlist_mutex);
3090
3091 l = cgroup_pidlist_find(cgrp, type);
3092 if (l)
3093 return l;
3094
Ben Blum72a8cb32009-09-23 15:56:27 -07003095 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003096 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003097 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003098 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003099
Tejun Heob1a21362013-11-29 10:42:58 -05003100 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003101 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003102 /* don't need task_nsproxy() if we're looking at ourself */
3103 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003104 l->owner = cgrp;
3105 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003106 return l;
3107}
3108
3109/*
Ben Blum102a7752009-09-23 15:56:26 -07003110 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3111 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003112static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3113 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003114{
3115 pid_t *array;
3116 int length;
3117 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003118 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003119 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003120 struct cgroup_pidlist *l;
3121
Tejun Heo4bac00d2013-11-29 10:42:59 -05003122 lockdep_assert_held(&cgrp->pidlist_mutex);
3123
Ben Blum102a7752009-09-23 15:56:26 -07003124 /*
3125 * If cgroup gets more users after we read count, we won't have
3126 * enough space - tough. This race is indistinguishable to the
3127 * caller from the case that the additional cgroup users didn't
3128 * show up until sometime later on.
3129 */
3130 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003131 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003132 if (!array)
3133 return -ENOMEM;
3134 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003135 css_task_iter_start(&cgrp->dummy_css, &it);
3136 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003137 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003138 break;
Ben Blum102a7752009-09-23 15:56:26 -07003139 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003140 if (type == CGROUP_FILE_PROCS)
3141 pid = task_tgid_vnr(tsk);
3142 else
3143 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003144 if (pid > 0) /* make sure to only use valid results */
3145 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003146 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003147 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003148 length = n;
3149 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003150 if (cgroup_sane_behavior(cgrp))
3151 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3152 else
3153 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003154 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003155 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003156
Tejun Heoe6b81712013-11-29 10:42:59 -05003157 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003158 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003159 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003160 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003161 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003162 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003163
3164 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003165 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003166 l->list = array;
3167 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003168 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003169 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003170}
3171
Balbir Singh846c7bb2007-10-18 23:39:44 -07003172/**
Li Zefana043e3b2008-02-23 15:24:09 -08003173 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003174 * @stats: cgroupstats to fill information into
3175 * @dentry: A dentry entry belonging to the cgroup for which stats have
3176 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003177 *
3178 * Build and fill cgroupstats so that taskstats can export it to user
3179 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003180 */
3181int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3182{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003183 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003184 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003185 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003186 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003187
Tejun Heo2bd59d42014-02-11 11:52:49 -05003188 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3189 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3190 kernfs_type(kn) != KERNFS_DIR)
3191 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003192
Tejun Heo2bd59d42014-02-11 11:52:49 -05003193 /*
3194 * We aren't being called from kernfs and there's no guarantee on
3195 * @kn->priv's validity. For this and css_tryget_from_dir(),
3196 * @kn->priv is RCU safe. Let's do the RCU dancing.
3197 */
3198 rcu_read_lock();
3199 cgrp = rcu_dereference(kn->priv);
3200 if (!cgrp) {
3201 rcu_read_unlock();
3202 return -ENOENT;
3203 }
Balbir Singh846c7bb2007-10-18 23:39:44 -07003204
Tejun Heo72ec7022013-08-08 20:11:26 -04003205 css_task_iter_start(&cgrp->dummy_css, &it);
3206 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003207 switch (tsk->state) {
3208 case TASK_RUNNING:
3209 stats->nr_running++;
3210 break;
3211 case TASK_INTERRUPTIBLE:
3212 stats->nr_sleeping++;
3213 break;
3214 case TASK_UNINTERRUPTIBLE:
3215 stats->nr_uninterruptible++;
3216 break;
3217 case TASK_STOPPED:
3218 stats->nr_stopped++;
3219 break;
3220 default:
3221 if (delayacct_is_task_waiting_on_io(tsk))
3222 stats->nr_io_wait++;
3223 break;
3224 }
3225 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003226 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003227
Tejun Heo2bd59d42014-02-11 11:52:49 -05003228 rcu_read_unlock();
3229 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003230}
3231
Paul Menage8f3ff202009-09-23 15:56:25 -07003232
Paul Menagecc31edc2008-10-18 20:28:04 -07003233/*
Ben Blum102a7752009-09-23 15:56:26 -07003234 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003235 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003236 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003237 */
3238
Ben Blum102a7752009-09-23 15:56:26 -07003239static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003240{
3241 /*
3242 * Initially we receive a position value that corresponds to
3243 * one more than the last pid shown (or 0 on the first call or
3244 * after a seek to the start). Use a binary-search to find the
3245 * next pid to display, if any
3246 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003247 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003248 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003249 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003250 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003251 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003252 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003253
Tejun Heo4bac00d2013-11-29 10:42:59 -05003254 mutex_lock(&cgrp->pidlist_mutex);
3255
3256 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003257 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003258 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003259 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003260 * could already have been destroyed.
3261 */
Tejun Heo5d224442013-12-05 12:28:04 -05003262 if (of->priv)
3263 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003264
3265 /*
3266 * Either this is the first start() after open or the matching
3267 * pidlist has been destroyed inbetween. Create a new one.
3268 */
Tejun Heo5d224442013-12-05 12:28:04 -05003269 if (!of->priv) {
3270 ret = pidlist_array_load(cgrp, type,
3271 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003272 if (ret)
3273 return ERR_PTR(ret);
3274 }
Tejun Heo5d224442013-12-05 12:28:04 -05003275 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003276
Paul Menagecc31edc2008-10-18 20:28:04 -07003277 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003278 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003279
Paul Menagecc31edc2008-10-18 20:28:04 -07003280 while (index < end) {
3281 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003282 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003283 index = mid;
3284 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003285 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003286 index = mid + 1;
3287 else
3288 end = mid;
3289 }
3290 }
3291 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003292 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003293 return NULL;
3294 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003295 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003296 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003297 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003298}
3299
Ben Blum102a7752009-09-23 15:56:26 -07003300static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003301{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003302 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003303 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003304
Tejun Heo5d224442013-12-05 12:28:04 -05003305 if (l)
3306 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003307 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003308 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003309}
3310
Ben Blum102a7752009-09-23 15:56:26 -07003311static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003312{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003313 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003314 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003315 pid_t *p = v;
3316 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003317 /*
3318 * Advance to the next pid in the array. If this goes off the
3319 * end, we're done
3320 */
3321 p++;
3322 if (p >= end) {
3323 return NULL;
3324 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003325 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003326 return p;
3327 }
3328}
3329
Ben Blum102a7752009-09-23 15:56:26 -07003330static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003331{
3332 return seq_printf(s, "%d\n", *(int *)v);
3333}
3334
Ben Blum102a7752009-09-23 15:56:26 -07003335/*
3336 * seq_operations functions for iterating on pidlists through seq_file -
3337 * independent of whether it's tasks or procs
3338 */
3339static const struct seq_operations cgroup_pidlist_seq_operations = {
3340 .start = cgroup_pidlist_start,
3341 .stop = cgroup_pidlist_stop,
3342 .next = cgroup_pidlist_next,
3343 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003344};
3345
Tejun Heo182446d2013-08-08 20:11:24 -04003346static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3347 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003348{
Tejun Heo182446d2013-08-08 20:11:24 -04003349 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003350}
3351
Tejun Heo182446d2013-08-08 20:11:24 -04003352static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3353 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003354{
Tejun Heo182446d2013-08-08 20:11:24 -04003355 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003356 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003357 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003358 else
Tejun Heo182446d2013-08-08 20:11:24 -04003359 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003360 return 0;
3361}
3362
Tejun Heo182446d2013-08-08 20:11:24 -04003363static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3364 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003365{
Tejun Heo182446d2013-08-08 20:11:24 -04003366 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003367}
3368
Tejun Heo182446d2013-08-08 20:11:24 -04003369static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3370 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003371{
3372 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003373 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003374 else
Tejun Heo182446d2013-08-08 20:11:24 -04003375 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003376 return 0;
3377}
3378
Tejun Heod5c56ce2013-06-03 19:14:34 -07003379static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003380 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003381 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003382 .seq_start = cgroup_pidlist_start,
3383 .seq_next = cgroup_pidlist_next,
3384 .seq_stop = cgroup_pidlist_stop,
3385 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003386 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003387 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003388 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003389 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003390 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003391 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003392 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003393 .read_u64 = cgroup_clone_children_read,
3394 .write_u64 = cgroup_clone_children_write,
3395 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003396 {
Tejun Heo873fe092013-04-14 20:15:26 -07003397 .name = "cgroup.sane_behavior",
3398 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003399 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003400 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003401
3402 /*
3403 * Historical crazy stuff. These don't have "cgroup." prefix and
3404 * don't exist if sane_behavior. If you're depending on these, be
3405 * prepared to be burned.
3406 */
3407 {
3408 .name = "tasks",
3409 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003410 .seq_start = cgroup_pidlist_start,
3411 .seq_next = cgroup_pidlist_next,
3412 .seq_stop = cgroup_pidlist_stop,
3413 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003414 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003415 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003416 .mode = S_IRUGO | S_IWUSR,
3417 },
3418 {
3419 .name = "notify_on_release",
3420 .flags = CFTYPE_INSANE,
3421 .read_u64 = cgroup_read_notify_on_release,
3422 .write_u64 = cgroup_write_notify_on_release,
3423 },
Tejun Heo873fe092013-04-14 20:15:26 -07003424 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003425 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003426 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003427 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003428 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003429 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003430 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003431 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003432};
3433
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003434/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003435 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003436 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003437 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003438 *
3439 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003440 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003441static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003442{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003443 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003444 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003445
Tejun Heo8e3f6542012-04-01 12:09:55 -07003446 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003447 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05003448 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07003449
3450 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003451 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003452
Tejun Heo0adb0702014-02-12 09:29:48 -05003453 list_for_each_entry(cfts, &ss->cfts, node) {
3454 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003455 if (ret < 0)
3456 goto err;
3457 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003458 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003459 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003460err:
3461 cgroup_clear_dir(cgrp, subsys_mask);
3462 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003463}
3464
Tejun Heo0c21ead2013-08-13 20:22:51 -04003465/*
3466 * css destruction is four-stage process.
3467 *
3468 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3469 * Implemented in kill_css().
3470 *
3471 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3472 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3473 * by invoking offline_css(). After offlining, the base ref is put.
3474 * Implemented in css_killed_work_fn().
3475 *
3476 * 3. When the percpu_ref reaches zero, the only possible remaining
3477 * accessors are inside RCU read sections. css_release() schedules the
3478 * RCU callback.
3479 *
3480 * 4. After the grace period, the css can be freed. Implemented in
3481 * css_free_work_fn().
3482 *
3483 * It is actually hairier because both step 2 and 4 require process context
3484 * and thus involve punting to css->destroy_work adding two additional
3485 * steps to the already complex sequence.
3486 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003487static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003488{
3489 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003490 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003491 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003492
Tejun Heo0ae78e02013-08-13 11:01:54 -04003493 if (css->parent)
3494 css_put(css->parent);
3495
Tejun Heo0c21ead2013-08-13 20:22:51 -04003496 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003497 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003498}
3499
3500static void css_free_rcu_fn(struct rcu_head *rcu_head)
3501{
3502 struct cgroup_subsys_state *css =
3503 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3504
Tejun Heo0c21ead2013-08-13 20:22:51 -04003505 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003506 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003507}
3508
Tejun Heod3daf282013-06-13 19:39:16 -07003509static void css_release(struct percpu_ref *ref)
3510{
3511 struct cgroup_subsys_state *css =
3512 container_of(ref, struct cgroup_subsys_state, refcnt);
3513
Tejun Heoaec25022014-02-08 10:36:58 -05003514 rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003515 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003516}
3517
Tejun Heo623f9262013-08-13 11:01:55 -04003518static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3519 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003520{
Paul Menagebd89aab2007-10-18 23:40:44 -07003521 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003522 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003523 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003524
Tejun Heo0ae78e02013-08-13 11:01:54 -04003525 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003526 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003527 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003528 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003529
Tejun Heoca8bdca2013-08-26 18:40:56 -04003530 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003531}
3532
Li Zefan2a4ac632013-07-31 16:16:40 +08003533/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04003534static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003535{
Tejun Heo623f9262013-08-13 11:01:55 -04003536 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08003537 int ret = 0;
3538
Tejun Heoace2bee2014-02-11 11:52:47 -05003539 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003540 lockdep_assert_held(&cgroup_mutex);
3541
Tejun Heo92fb9742012-11-19 08:13:38 -08003542 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04003543 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003544 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04003545 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04003546 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05003547 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003548 }
Tejun Heob1929db2012-11-19 08:13:38 -08003549 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003550}
3551
Li Zefan2a4ac632013-07-31 16:16:40 +08003552/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04003553static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003554{
Tejun Heo623f9262013-08-13 11:01:55 -04003555 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003556
Tejun Heoace2bee2014-02-11 11:52:47 -05003557 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003558 lockdep_assert_held(&cgroup_mutex);
3559
3560 if (!(css->flags & CSS_ONLINE))
3561 return;
3562
Li Zefand7eeac12013-03-12 15:35:59 -07003563 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04003564 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003565
Tejun Heoeb954192013-08-08 20:11:23 -04003566 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04003567 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05003568 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003569}
3570
Tejun Heoc81c925a2013-12-06 15:11:56 -05003571/**
3572 * create_css - create a cgroup_subsys_state
3573 * @cgrp: the cgroup new css will be associated with
3574 * @ss: the subsys of new css
3575 *
3576 * Create a new css associated with @cgrp - @ss pair. On success, the new
3577 * css is online and installed in @cgrp with all interface files created.
3578 * Returns 0 on success, -errno on failure.
3579 */
3580static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
3581{
3582 struct cgroup *parent = cgrp->parent;
3583 struct cgroup_subsys_state *css;
3584 int err;
3585
Tejun Heoc81c925a2013-12-06 15:11:56 -05003586 lockdep_assert_held(&cgroup_mutex);
3587
3588 css = ss->css_alloc(cgroup_css(parent, ss));
3589 if (IS_ERR(css))
3590 return PTR_ERR(css);
3591
3592 err = percpu_ref_init(&css->refcnt, css_release);
3593 if (err)
3594 goto err_free;
3595
3596 init_css(css, ss, cgrp);
3597
Tejun Heoaec25022014-02-08 10:36:58 -05003598 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003599 if (err)
3600 goto err_free;
3601
3602 err = online_css(css);
3603 if (err)
3604 goto err_free;
3605
Tejun Heo59f52962014-02-11 11:52:49 -05003606 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003607 css_get(css->parent);
3608
3609 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
3610 parent->parent) {
3611 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",
3612 current->comm, current->pid, ss->name);
3613 if (!strcmp(ss->name, "memory"))
3614 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
3615 ss->warned_broken_hierarchy = true;
3616 }
3617
3618 return 0;
3619
3620err_free:
3621 percpu_ref_cancel_init(&css->refcnt);
3622 ss->css_free(css);
3623 return err;
3624}
3625
Tejun Heo2bd59d42014-02-11 11:52:49 -05003626/**
Li Zefana043e3b2008-02-23 15:24:09 -08003627 * cgroup_create - create a cgroup
3628 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05003629 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05003630 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07003631 */
Tejun Heoe61734c2014-02-12 09:29:50 -05003632static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003633 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003634{
Paul Menagebd89aab2007-10-18 23:40:44 -07003635 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003636 struct cgroupfs_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05003637 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003638 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003639 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003640
Tejun Heo0a950f62012-11-19 09:02:12 -08003641 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07003642 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3643 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003644 return -ENOMEM;
3645
Tejun Heoace2bee2014-02-11 11:52:47 -05003646 mutex_lock(&cgroup_tree_mutex);
3647
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003648 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08003649 * Only live parents can have children. Note that the liveliness
3650 * check isn't strictly necessary because cgroup_mkdir() and
3651 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
3652 * anyway so that locking is contained inside cgroup proper and we
3653 * don't get nasty surprises if we ever grow another caller.
3654 */
3655 if (!cgroup_lock_live_group(parent)) {
3656 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05003657 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08003658 }
3659
3660 /*
3661 * Temporarily set the pointer to NULL, so idr_find() won't return
3662 * a half-baked cgroup.
3663 */
3664 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
3665 if (cgrp->id < 0) {
3666 err = -ENOMEM;
3667 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08003668 }
3669
Paul Menagecc31edc2008-10-18 20:28:04 -07003670 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003671
Paul Menagebd89aab2007-10-18 23:40:44 -07003672 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003673 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07003674 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003675
Li Zefanb6abdb02008-03-04 14:28:19 -08003676 if (notify_on_release(parent))
3677 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3678
Tejun Heo2260e7f2012-11-19 08:13:38 -08003679 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
3680 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003681
Tejun Heo2bd59d42014-02-11 11:52:49 -05003682 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05003683 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003684 if (IS_ERR(kn)) {
3685 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003686 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003687 }
3688 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003689
Tejun Heo6f305582014-02-12 09:29:50 -05003690 /*
3691 * This extra ref will be put in cgroup_free_fn() and guarantees
3692 * that @cgrp->kn is always accessible.
3693 */
3694 kernfs_get(kn);
3695
Tejun Heo00356bd2013-06-18 11:14:22 -07003696 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09003697
Tejun Heo4e139af2012-11-19 08:13:36 -08003698 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08003699 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05003700 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05003701 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08003702
Tejun Heo0d802552013-12-06 15:11:56 -05003703 /*
3704 * @cgrp is now fully operational. If something fails after this
3705 * point, it'll be released via the normal destruction path.
3706 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003707 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
3708
Tejun Heo2bb566c2013-08-08 20:11:23 -04003709 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07003710 if (err)
3711 goto err_destroy;
3712
Tejun Heo9d403e92013-12-06 15:11:56 -05003713 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05003714 for_each_subsys(ss, ssid) {
3715 if (root->subsys_mask & (1 << ssid)) {
3716 err = create_css(cgrp, ss);
3717 if (err)
3718 goto err_destroy;
3719 }
Tejun Heoa8638032012-11-09 09:12:29 -08003720 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003721
Tejun Heo2bd59d42014-02-11 11:52:49 -05003722 kernfs_activate(kn);
3723
Paul Menageddbcc7e2007-10-18 23:39:30 -07003724 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003725 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003726
3727 return 0;
3728
Tejun Heo0a950f62012-11-19 09:02:12 -08003729err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003730 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003731err_unlock:
3732 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003733err_unlock_tree:
3734 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003735 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003736 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003737
3738err_destroy:
3739 cgroup_destroy_locked(cgrp);
3740 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003741 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003742 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003743}
3744
Tejun Heo2bd59d42014-02-11 11:52:49 -05003745static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3746 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003747{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003748 struct cgroup *parent = parent_kn->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003749
Tejun Heo2bd59d42014-02-11 11:52:49 -05003750 return cgroup_create(parent, name, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003751}
3752
Tejun Heo223dbc32013-08-13 20:22:50 -04003753/*
3754 * This is called when the refcnt of a css is confirmed to be killed.
3755 * css_tryget() is now guaranteed to fail.
3756 */
3757static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07003758{
Tejun Heo223dbc32013-08-13 20:22:50 -04003759 struct cgroup_subsys_state *css =
3760 container_of(work, struct cgroup_subsys_state, destroy_work);
3761 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07003762
Tejun Heoace2bee2014-02-11 11:52:47 -05003763 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003764 mutex_lock(&cgroup_mutex);
3765
3766 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04003767 * css_tryget() is guaranteed to fail now. Tell subsystems to
3768 * initate destruction.
3769 */
3770 offline_css(css);
3771
3772 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003773 * If @cgrp is marked dead, it's waiting for refs of all css's to
3774 * be disabled before proceeding to the second phase of cgroup
3775 * destruction. If we are the last one, kick it off.
3776 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04003777 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04003778 cgroup_destroy_css_killed(cgrp);
3779
3780 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003781 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04003782
3783 /*
3784 * Put the css refs from kill_css(). Each css holds an extra
3785 * reference to the cgroup's dentry and cgroup removal proceeds
3786 * regardless of css refs. On the last put of each css, whenever
3787 * that may be, the extra dentry ref is put so that dentry
3788 * destruction happens only after all css's are released.
3789 */
3790 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07003791}
3792
Tejun Heo223dbc32013-08-13 20:22:50 -04003793/* css kill confirmation processing requires process context, bounce */
3794static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07003795{
3796 struct cgroup_subsys_state *css =
3797 container_of(ref, struct cgroup_subsys_state, refcnt);
3798
Tejun Heo223dbc32013-08-13 20:22:50 -04003799 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003800 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07003801}
3802
3803/**
Tejun Heoedae0c32013-08-13 20:22:51 -04003804 * kill_css - destroy a css
3805 * @css: css to destroy
3806 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003807 * This function initiates destruction of @css by removing cgroup interface
3808 * files and putting its base reference. ->css_offline() will be invoked
3809 * asynchronously once css_tryget() is guaranteed to fail and when the
3810 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04003811 */
3812static void kill_css(struct cgroup_subsys_state *css)
3813{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003814 /*
3815 * This must happen before css is disassociated with its cgroup.
3816 * See seq_css() for details.
3817 */
Tejun Heoaec25022014-02-08 10:36:58 -05003818 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003819
Tejun Heoedae0c32013-08-13 20:22:51 -04003820 /*
3821 * Killing would put the base ref, but we need to keep it alive
3822 * until after ->css_offline().
3823 */
3824 css_get(css);
3825
3826 /*
3827 * cgroup core guarantees that, by the time ->css_offline() is
3828 * invoked, no new css reference will be given out via
3829 * css_tryget(). We can't simply call percpu_ref_kill() and
3830 * proceed to offlining css's because percpu_ref_kill() doesn't
3831 * guarantee that the ref is seen as killed on all CPUs on return.
3832 *
3833 * Use percpu_ref_kill_and_confirm() to get notifications as each
3834 * css is confirmed to be seen as killed on all CPUs.
3835 */
3836 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003837}
3838
3839/**
3840 * cgroup_destroy_locked - the first stage of cgroup destruction
3841 * @cgrp: cgroup to be destroyed
3842 *
3843 * css's make use of percpu refcnts whose killing latency shouldn't be
3844 * exposed to userland and are RCU protected. Also, cgroup core needs to
3845 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
3846 * invoked. To satisfy all the requirements, destruction is implemented in
3847 * the following two steps.
3848 *
3849 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
3850 * userland visible parts and start killing the percpu refcnts of
3851 * css's. Set up so that the next stage will be kicked off once all
3852 * the percpu refcnts are confirmed to be killed.
3853 *
3854 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
3855 * rest of destruction. Once all cgroup references are gone, the
3856 * cgroup is RCU-freed.
3857 *
3858 * This function implements s1. After this step, @cgrp is gone as far as
3859 * the userland is concerned and a new cgroup with the same name may be
3860 * created. As cgroup doesn't care about the names internally, this
3861 * doesn't cause any problem.
3862 */
Tejun Heo42809dd2012-11-19 08:13:37 -08003863static int cgroup_destroy_locked(struct cgroup *cgrp)
3864 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003865{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003866 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003867 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07003868 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05003869 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003870
Tejun Heoace2bee2014-02-11 11:52:47 -05003871 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003872 lockdep_assert_held(&cgroup_mutex);
3873
Tejun Heoddd69142013-06-12 21:04:54 -07003874 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07003875 * css_set_lock synchronizes access to ->cset_links and prevents
3876 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07003877 */
3878 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003879 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07003880 read_unlock(&css_set_lock);
3881 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003882 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08003883
Tejun Heo1a90dd52012-11-05 09:16:59 -08003884 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003885 * Make sure there's no live children. We can't test ->children
3886 * emptiness as dead children linger on it while being destroyed;
3887 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
3888 */
3889 empty = true;
3890 rcu_read_lock();
3891 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
3892 empty = cgroup_is_dead(child);
3893 if (!empty)
3894 break;
3895 }
3896 rcu_read_unlock();
3897 if (!empty)
3898 return -EBUSY;
3899
3900 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04003901 * Initiate massacre of all css's. cgroup_destroy_css_killed()
3902 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05003903 * percpu refs of all css's are confirmed to be killed. This
3904 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08003905 */
Tejun Heo4ac06012014-02-11 11:52:47 -05003906 mutex_unlock(&cgroup_mutex);
Tejun Heo1c6727a2013-12-06 15:11:56 -05003907 for_each_css(css, ssid, cgrp)
3908 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05003909 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07003910
3911 /*
3912 * Mark @cgrp dead. This prevents further task migration and child
3913 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04003914 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07003915 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04003916 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07003917 */
Tejun Heo54766d42013-06-12 21:04:53 -07003918 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08003919
Tejun Heo455050d2013-06-13 19:27:41 -07003920 /* CGRP_DEAD is set, remove from ->release_list for the last time */
3921 raw_spin_lock(&release_list_lock);
3922 if (!list_empty(&cgrp->release_list))
3923 list_del_init(&cgrp->release_list);
3924 raw_spin_unlock(&release_list_lock);
3925
3926 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003927 * If @cgrp has css's attached, the second stage of cgroup
3928 * destruction is kicked off from css_killed_work_fn() after the
3929 * refs of all attached css's are killed. If @cgrp doesn't have
3930 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07003931 */
Tejun Heof20104d2013-08-13 20:22:50 -04003932 if (!cgrp->nr_css)
3933 cgroup_destroy_css_killed(cgrp);
3934
Tejun Heo2bd59d42014-02-11 11:52:49 -05003935 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05003936 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003937
3938 /*
3939 * There are two control paths which try to determine cgroup from
3940 * dentry without going through kernfs - cgroupstats_build() and
3941 * css_tryget_from_dir(). Those are supported by RCU protecting
3942 * clearing of cgrp->kn->priv backpointer, which should happen
3943 * after all files under it have been removed.
3944 */
Tejun Heo6f305582014-02-12 09:29:50 -05003945 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003946 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003947
Tejun Heo4ac06012014-02-11 11:52:47 -05003948 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07003949
Tejun Heoea15f8c2013-06-13 19:27:42 -07003950 return 0;
3951};
3952
Tejun Heod3daf282013-06-13 19:39:16 -07003953/**
Tejun Heof20104d2013-08-13 20:22:50 -04003954 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07003955 * @work: cgroup->destroy_free_work
3956 *
3957 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04003958 * destroyed after all css's are offlined and performs the rest of
3959 * destruction. This is the second step of destruction described in the
3960 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07003961 */
Tejun Heof20104d2013-08-13 20:22:50 -04003962static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07003963{
Tejun Heoea15f8c2013-06-13 19:27:42 -07003964 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07003965
Tejun Heoace2bee2014-02-11 11:52:47 -05003966 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003967 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003968
Paul Menage999cd8a2009-01-07 18:08:36 -08003969 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08003970 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07003971
Tejun Heo59f52962014-02-11 11:52:49 -05003972 cgroup_put(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003973
Paul Menagebd89aab2007-10-18 23:40:44 -07003974 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003975 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003976}
3977
Tejun Heo2bd59d42014-02-11 11:52:49 -05003978static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08003979{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003980 struct cgroup *cgrp = kn->priv;
3981 int ret = 0;
3982
3983 /*
3984 * This is self-destruction but @kn can't be removed while this
3985 * callback is in progress. Let's break active protection. Once
3986 * the protection is broken, @cgrp can be destroyed at any point.
3987 * Pin it so that it stays accessible.
3988 */
3989 cgroup_get(cgrp);
3990 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08003991
Tejun Heoace2bee2014-02-11 11:52:47 -05003992 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003993 mutex_lock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003994
3995 /*
3996 * @cgrp might already have been destroyed while we're trying to
3997 * grab the mutexes.
3998 */
3999 if (!cgroup_is_dead(cgrp))
4000 ret = cgroup_destroy_locked(cgrp);
4001
Tejun Heo42809dd2012-11-19 08:13:37 -08004002 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004003 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004004
Tejun Heo2bd59d42014-02-11 11:52:49 -05004005 kernfs_unbreak_active_protection(kn);
4006 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004007 return ret;
4008}
4009
Tejun Heo2bd59d42014-02-11 11:52:49 -05004010static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4011 .remount_fs = cgroup_remount,
4012 .show_options = cgroup_show_options,
4013 .mkdir = cgroup_mkdir,
4014 .rmdir = cgroup_rmdir,
4015 .rename = cgroup_rename,
4016};
4017
Li Zefan06a11922008-04-29 01:00:07 -07004018static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004019{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004020 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004021
4022 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004023
Tejun Heoace2bee2014-02-11 11:52:47 -05004024 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004025 mutex_lock(&cgroup_mutex);
4026
Tejun Heo0adb0702014-02-12 09:29:48 -05004027 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004028
Paul Menageddbcc7e2007-10-18 23:39:30 -07004029 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004030 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004031 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004032 /* We don't handle early failures gracefully */
4033 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004034 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004035
Li Zefane8d55fd2008-04-29 01:00:13 -07004036 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004037 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004038 * newly registered, all tasks and hence the
4039 * init_css_set is in the subsystem's top cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004040 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004041
4042 need_forkexit_callback |= ss->fork || ss->exit;
4043
Li Zefane8d55fd2008-04-29 01:00:13 -07004044 /* At system boot, before all subsystems have been
4045 * registered, no tasks have been forked, so we don't
4046 * need to invoke fork callbacks here. */
4047 BUG_ON(!list_empty(&init_task.tasks));
4048
Tejun Heoae7f1642013-08-13 20:22:50 -04004049 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004050
Tejun Heo648bb562012-11-19 08:13:36 -08004051 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004052 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004053}
4054
4055/**
Li Zefana043e3b2008-02-23 15:24:09 -08004056 * cgroup_init_early - cgroup initialization at system boot
4057 *
4058 * Initialize cgroups at system boot, and initialize any
4059 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004060 */
4061int __init cgroup_init_early(void)
4062{
Tejun Heo30159ec2013-06-25 11:53:37 -07004063 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004064 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004065
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004066 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004067 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004068 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004069 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004070 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004071 init_cgroup_root(&cgroup_dummy_root);
4072 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004073 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004074
Tejun Heo69d02062013-06-12 21:04:50 -07004075 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004076 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4077 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004078 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004079
Tejun Heo3ed80a62014-02-08 10:36:58 -05004080 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004081 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004082 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4083 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004084 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004085 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4086 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4087
Tejun Heoaec25022014-02-08 10:36:58 -05004088 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004089 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004090
4091 if (ss->early_init)
4092 cgroup_init_subsys(ss);
4093 }
4094 return 0;
4095}
4096
4097/**
Li Zefana043e3b2008-02-23 15:24:09 -08004098 * cgroup_init - cgroup initialization
4099 *
4100 * Register cgroup filesystem and /proc file, and initialize
4101 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004102 */
4103int __init cgroup_init(void)
4104{
Tejun Heo30159ec2013-06-25 11:53:37 -07004105 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004106 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004107 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004108
Tejun Heo2bd59d42014-02-11 11:52:49 -05004109 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Tejun Heo2da440a2014-02-11 11:52:48 -05004110
Tejun Heo3ed80a62014-02-08 10:36:58 -05004111 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004112 if (!ss->early_init)
4113 cgroup_init_subsys(ss);
Tejun Heode00ffa2014-02-11 11:52:48 -05004114
4115 /*
4116 * cftype registration needs kmalloc and can't be done
4117 * during early_init. Register base cftypes separately.
4118 */
4119 if (ss->base_cftypes)
4120 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004121 }
4122
Tejun Heofa3ca072013-04-14 11:36:56 -07004123 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004124 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004125
Tejun Heo82fe9b02013-06-25 11:53:37 -07004126 /* Add init_css_set to the hash table */
4127 key = css_set_hash(init_css_set.subsys);
4128 hash_add(css_set_table, &init_css_set.hlist, key);
4129
Tejun Heofc76df72013-06-25 11:53:37 -07004130 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004131
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004132 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4133 0, 1, GFP_KERNEL);
4134 BUG_ON(err < 0);
4135
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004136 mutex_unlock(&cgroup_mutex);
4137
Greg KH676db4a2010-08-05 13:53:35 -07004138 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004139 if (!cgroup_kobj)
4140 return -ENOMEM;
Greg KH676db4a2010-08-05 13:53:35 -07004141
4142 err = register_filesystem(&cgroup_fs_type);
4143 if (err < 0) {
4144 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004145 return err;
Greg KH676db4a2010-08-05 13:53:35 -07004146 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004147
Li Zefan46ae2202008-04-29 01:00:08 -07004148 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004149 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004150}
Paul Menageb4f48b62007-10-18 23:39:33 -07004151
Tejun Heoe5fca242013-11-22 17:14:39 -05004152static int __init cgroup_wq_init(void)
4153{
4154 /*
4155 * There isn't much point in executing destruction path in
4156 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004157 *
4158 * XXX: Must be ordered to make sure parent is offlined after
4159 * children. The ordering requirement is for memcg where a
4160 * parent's offline may wait for a child's leading to deadlock. In
4161 * the long term, this should be fixed from memcg side.
Tejun Heoe5fca242013-11-22 17:14:39 -05004162 *
4163 * We would prefer to do this in cgroup_init() above, but that
4164 * is called before init_workqueues(): so leave this until after.
4165 */
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004166 cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0);
Tejun Heoe5fca242013-11-22 17:14:39 -05004167 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004168
4169 /*
4170 * Used to destroy pidlists and separate to serve as flush domain.
4171 * Cap @max_active to 1 too.
4172 */
4173 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4174 0, 1);
4175 BUG_ON(!cgroup_pidlist_destroy_wq);
4176
Tejun Heoe5fca242013-11-22 17:14:39 -05004177 return 0;
4178}
4179core_initcall(cgroup_wq_init);
4180
Paul Menagea4243162007-10-18 23:39:35 -07004181/*
4182 * proc_cgroup_show()
4183 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4184 * - Used for /proc/<pid>/cgroup.
4185 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4186 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004187 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004188 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4189 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4190 * cgroup to top_cgroup.
4191 */
4192
4193/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004194int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004195{
4196 struct pid *pid;
4197 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004198 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004199 int retval;
4200 struct cgroupfs_root *root;
4201
4202 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004203 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004204 if (!buf)
4205 goto out;
4206
4207 retval = -ESRCH;
4208 pid = m->private;
4209 tsk = get_pid_task(pid, PIDTYPE_PID);
4210 if (!tsk)
4211 goto out_free;
4212
4213 retval = 0;
4214
4215 mutex_lock(&cgroup_mutex);
4216
Li Zefane5f6a862009-01-07 18:07:41 -08004217 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004218 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004219 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004220 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004221
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004222 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004223 for_each_subsys(ss, ssid)
4224 if (root->subsys_mask & (1 << ssid))
4225 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004226 if (strlen(root->name))
4227 seq_printf(m, "%sname=%s", count ? "," : "",
4228 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004229 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004230 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004231 path = cgroup_path(cgrp, buf, PATH_MAX);
4232 if (!path) {
4233 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004234 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004235 }
4236 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004237 seq_putc(m, '\n');
4238 }
4239
4240out_unlock:
4241 mutex_unlock(&cgroup_mutex);
4242 put_task_struct(tsk);
4243out_free:
4244 kfree(buf);
4245out:
4246 return retval;
4247}
4248
Paul Menagea4243162007-10-18 23:39:35 -07004249/* Display information about each subsystem and each hierarchy */
4250static int proc_cgroupstats_show(struct seq_file *m, void *v)
4251{
Tejun Heo30159ec2013-06-25 11:53:37 -07004252 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004253 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004254
Paul Menage8bab8dd2008-04-04 14:29:57 -07004255 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004256 /*
4257 * ideally we don't want subsystems moving around while we do this.
4258 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4259 * subsys/hierarchy state.
4260 */
Paul Menagea4243162007-10-18 23:39:35 -07004261 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004262
4263 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004264 seq_printf(m, "%s\t%d\t%d\t%d\n",
4265 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004266 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004267
Paul Menagea4243162007-10-18 23:39:35 -07004268 mutex_unlock(&cgroup_mutex);
4269 return 0;
4270}
4271
4272static int cgroupstats_open(struct inode *inode, struct file *file)
4273{
Al Viro9dce07f2008-03-29 03:07:28 +00004274 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004275}
4276
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004277static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004278 .open = cgroupstats_open,
4279 .read = seq_read,
4280 .llseek = seq_lseek,
4281 .release = single_release,
4282};
4283
Paul Menageb4f48b62007-10-18 23:39:33 -07004284/**
4285 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004286 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004287 *
4288 * Description: A task inherits its parent's cgroup at fork().
4289 *
4290 * A pointer to the shared css_set was automatically copied in
4291 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004292 * it was not made under the protection of RCU or cgroup_mutex, so
4293 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4294 * have already changed current->cgroups, allowing the previously
4295 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004296 *
4297 * At the point that cgroup_fork() is called, 'current' is the parent
4298 * task, and the passed argument 'child' points to the child task.
4299 */
4300void cgroup_fork(struct task_struct *child)
4301{
Tejun Heo9bb71302012-10-18 17:52:07 -07004302 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004303 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004304 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004305 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004306 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004307}
4308
4309/**
Li Zefana043e3b2008-02-23 15:24:09 -08004310 * cgroup_post_fork - called on a new task after adding it to the task list
4311 * @child: the task in question
4312 *
Tejun Heo5edee612012-10-16 15:03:14 -07004313 * Adds the task to the list running through its css_set if necessary and
4314 * call the subsystem fork() callbacks. Has to be after the task is
4315 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004316 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004317 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004318 */
Paul Menage817929e2007-10-18 23:39:36 -07004319void cgroup_post_fork(struct task_struct *child)
4320{
Tejun Heo30159ec2013-06-25 11:53:37 -07004321 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004322 int i;
4323
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004324 /*
4325 * use_task_css_set_links is set to 1 before we walk the tasklist
4326 * under the tasklist_lock and we read it here after we added the child
4327 * to the tasklist under the tasklist_lock as well. If the child wasn't
4328 * yet in the tasklist when we walked through it from
4329 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4330 * should be visible now due to the paired locking and barriers implied
4331 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4332 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4333 * lock on fork.
4334 */
Paul Menage817929e2007-10-18 23:39:36 -07004335 if (use_task_css_set_links) {
4336 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004337 task_lock(child);
4338 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004339 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004340 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004341 write_unlock(&css_set_lock);
4342 }
Tejun Heo5edee612012-10-16 15:03:14 -07004343
4344 /*
4345 * Call ss->fork(). This must happen after @child is linked on
4346 * css_set; otherwise, @child might change state between ->fork()
4347 * and addition to css_set.
4348 */
4349 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004350 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004351 if (ss->fork)
4352 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004353 }
Paul Menage817929e2007-10-18 23:39:36 -07004354}
Tejun Heo5edee612012-10-16 15:03:14 -07004355
Paul Menage817929e2007-10-18 23:39:36 -07004356/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004357 * cgroup_exit - detach cgroup from exiting task
4358 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004359 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004360 *
4361 * Description: Detach cgroup from @tsk and release it.
4362 *
4363 * Note that cgroups marked notify_on_release force every task in
4364 * them to take the global cgroup_mutex mutex when exiting.
4365 * This could impact scaling on very large systems. Be reluctant to
4366 * use notify_on_release cgroups where very high task exit scaling
4367 * is required on large systems.
4368 *
4369 * the_top_cgroup_hack:
4370 *
4371 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4372 *
4373 * We call cgroup_exit() while the task is still competent to
4374 * handle notify_on_release(), then leave the task attached to the
4375 * root cgroup in each hierarchy for the remainder of its exit.
4376 *
4377 * To do this properly, we would increment the reference count on
4378 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4379 * code we would add a second cgroup function call, to drop that
4380 * reference. This would just create an unnecessary hot spot on
4381 * the top_cgroup reference count, to no avail.
4382 *
4383 * Normally, holding a reference to a cgroup without bumping its
4384 * count is unsafe. The cgroup could go away, or someone could
4385 * attach us to a different cgroup, decrementing the count on
4386 * the first cgroup that we never incremented. But in this case,
4387 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004388 * which wards off any cgroup_attach_task() attempts, or task is a failed
4389 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004390 */
4391void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4392{
Tejun Heo30159ec2013-06-25 11:53:37 -07004393 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004394 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004395 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004396
4397 /*
4398 * Unlink from the css_set task list if necessary.
4399 * Optimistically check cg_list before taking
4400 * css_set_lock
4401 */
4402 if (!list_empty(&tsk->cg_list)) {
4403 write_lock(&css_set_lock);
4404 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004405 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004406 write_unlock(&css_set_lock);
4407 }
4408
Paul Menageb4f48b62007-10-18 23:39:33 -07004409 /* Reassign the task to the init_css_set. */
4410 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004411 cset = task_css_set(tsk);
4412 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004413
4414 if (run_callbacks && need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004415 /* see cgroup_post_fork() for details */
4416 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004417 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004418 struct cgroup_subsys_state *old_css = cset->subsys[i];
4419 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004420
Tejun Heoeb954192013-08-08 20:11:23 -04004421 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004422 }
4423 }
4424 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004425 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004426
Tejun Heo5abb8852013-06-12 21:04:49 -07004427 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07004428}
Paul Menage697f4162007-10-18 23:39:34 -07004429
Paul Menagebd89aab2007-10-18 23:40:44 -07004430static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004431{
Li Zefanf50daa72013-03-01 15:06:07 +08004432 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004433 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004434 /*
4435 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004436 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004437 * it now
4438 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004439 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004440
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004441 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004442 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004443 list_empty(&cgrp->release_list)) {
4444 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004445 need_schedule_work = 1;
4446 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004447 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004448 if (need_schedule_work)
4449 schedule_work(&release_agent_work);
4450 }
4451}
4452
Paul Menage81a6a5c2007-10-18 23:39:38 -07004453/*
4454 * Notify userspace when a cgroup is released, by running the
4455 * configured release agent with the name of the cgroup (path
4456 * relative to the root of cgroup file system) as the argument.
4457 *
4458 * Most likely, this user command will try to rmdir this cgroup.
4459 *
4460 * This races with the possibility that some other task will be
4461 * attached to this cgroup before it is removed, or that some other
4462 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4463 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4464 * unused, and this cgroup will be reprieved from its death sentence,
4465 * to continue to serve a useful existence. Next time it's released,
4466 * we will get notified again, if it still has 'notify_on_release' set.
4467 *
4468 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4469 * means only wait until the task is successfully execve()'d. The
4470 * separate release agent task is forked by call_usermodehelper(),
4471 * then control in this thread returns here, without waiting for the
4472 * release agent task. We don't bother to wait because the caller of
4473 * this routine has no use for the exit status of the release agent
4474 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004475 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004476static void cgroup_release_agent(struct work_struct *work)
4477{
4478 BUG_ON(work != &release_agent_work);
4479 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004480 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004481 while (!list_empty(&release_list)) {
4482 char *argv[3], *envp[3];
4483 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05004484 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07004485 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004486 struct cgroup,
4487 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004488 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004489 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05004490 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004491 if (!pathbuf)
4492 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05004493 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
4494 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07004495 goto continue_free;
4496 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4497 if (!agentbuf)
4498 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004499
4500 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004501 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05004502 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004503 argv[i] = NULL;
4504
4505 i = 0;
4506 /* minimal command environment */
4507 envp[i++] = "HOME=/";
4508 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4509 envp[i] = NULL;
4510
4511 /* Drop the lock while we invoke the usermode helper,
4512 * since the exec could involve hitting disk and hence
4513 * be a slow process */
4514 mutex_unlock(&cgroup_mutex);
4515 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004516 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004517 continue_free:
4518 kfree(pathbuf);
4519 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004520 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004521 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004522 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004523 mutex_unlock(&cgroup_mutex);
4524}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004525
4526static int __init cgroup_disable(char *str)
4527{
Tejun Heo30159ec2013-06-25 11:53:37 -07004528 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004529 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07004530 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004531
4532 while ((token = strsep(&str, ",")) != NULL) {
4533 if (!*token)
4534 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004535
Tejun Heo3ed80a62014-02-08 10:36:58 -05004536 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004537 if (!strcmp(token, ss->name)) {
4538 ss->disabled = 1;
4539 printk(KERN_INFO "Disabling %s control group"
4540 " subsystem\n", ss->name);
4541 break;
4542 }
4543 }
4544 }
4545 return 1;
4546}
4547__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004548
Tejun Heob77d7b62013-08-13 11:01:54 -04004549/**
Tejun Heo5a17f542014-02-11 11:52:47 -05004550 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04004551 * @dentry: directory dentry of interest
4552 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04004553 *
Tejun Heo5a17f542014-02-11 11:52:47 -05004554 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4555 * to get the corresponding css and return it. If such css doesn't exist
4556 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02004557 */
Tejun Heo5a17f542014-02-11 11:52:47 -05004558struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4559 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004560{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004561 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4562 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004563 struct cgroup *cgrp;
Tejun Heob77d7b62013-08-13 11:01:54 -04004564
Tejun Heo35cf0832013-08-26 18:40:56 -04004565 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004566 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4567 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004568 return ERR_PTR(-EBADF);
4569
Tejun Heo5a17f542014-02-11 11:52:47 -05004570 rcu_read_lock();
4571
Tejun Heo2bd59d42014-02-11 11:52:49 -05004572 /*
4573 * This path doesn't originate from kernfs and @kn could already
4574 * have been or be removed at any point. @kn->priv is RCU
4575 * protected for this access. See destroy_locked() for details.
4576 */
4577 cgrp = rcu_dereference(kn->priv);
4578 if (cgrp)
4579 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05004580
4581 if (!css || !css_tryget(css))
4582 css = ERR_PTR(-ENOENT);
4583
4584 rcu_read_unlock();
4585 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004586}
Stephane Eraniane5d13672011-02-14 11:20:01 +02004587
Li Zefan1cb650b2013-08-19 10:05:24 +08004588/**
4589 * css_from_id - lookup css by id
4590 * @id: the cgroup id
4591 * @ss: cgroup subsys to be looked into
4592 *
4593 * Returns the css if there's valid one with @id, otherwise returns NULL.
4594 * Should be called under rcu_read_lock().
4595 */
4596struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4597{
4598 struct cgroup *cgrp;
4599
Tejun Heoace2bee2014-02-11 11:52:47 -05004600 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08004601
4602 cgrp = idr_find(&ss->root->cgroup_idr, id);
4603 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04004604 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08004605 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004606}
4607
Paul Menagefe693432009-09-23 15:56:20 -07004608#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04004609static struct cgroup_subsys_state *
4610debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07004611{
4612 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4613
4614 if (!css)
4615 return ERR_PTR(-ENOMEM);
4616
4617 return css;
4618}
4619
Tejun Heoeb954192013-08-08 20:11:23 -04004620static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07004621{
Tejun Heoeb954192013-08-08 20:11:23 -04004622 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07004623}
4624
Tejun Heo182446d2013-08-08 20:11:24 -04004625static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
4626 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004627{
Tejun Heo182446d2013-08-08 20:11:24 -04004628 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07004629}
4630
Tejun Heo182446d2013-08-08 20:11:24 -04004631static u64 current_css_set_read(struct cgroup_subsys_state *css,
4632 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004633{
4634 return (u64)(unsigned long)current->cgroups;
4635}
4636
Tejun Heo182446d2013-08-08 20:11:24 -04004637static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08004638 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004639{
4640 u64 count;
4641
4642 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07004643 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07004644 rcu_read_unlock();
4645 return count;
4646}
4647
Tejun Heo2da8ca82013-12-05 12:28:04 -05004648static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004649{
Tejun Heo69d02062013-06-12 21:04:50 -07004650 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07004651 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05004652 char *name_buf;
4653
4654 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
4655 if (!name_buf)
4656 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07004657
4658 read_lock(&css_set_lock);
4659 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07004660 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07004661 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004662 struct cgroup *c = link->cgrp;
Tejun Heo59f52962014-02-11 11:52:49 -05004663 const char *name = "?";
Paul Menage7717f7b2009-09-23 15:56:22 -07004664
Tejun Heoe61734c2014-02-12 09:29:50 -05004665 if (c != cgroup_dummy_top) {
4666 cgroup_name(c, name_buf, NAME_MAX + 1);
4667 name = name_buf;
4668 }
Tejun Heo59f52962014-02-11 11:52:49 -05004669
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004670 seq_printf(seq, "Root %d group %s\n",
4671 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07004672 }
4673 rcu_read_unlock();
4674 read_unlock(&css_set_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05004675 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004676 return 0;
4677}
4678
4679#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05004680static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004681{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004682 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07004683 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07004684
4685 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04004686 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004687 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004688 struct task_struct *task;
4689 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07004690 seq_printf(seq, "css_set %p\n", cset);
4691 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004692 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4693 seq_puts(seq, " ...\n");
4694 break;
4695 } else {
4696 seq_printf(seq, " task %d\n",
4697 task_pid_vnr(task));
4698 }
4699 }
4700 }
4701 read_unlock(&css_set_lock);
4702 return 0;
4703}
4704
Tejun Heo182446d2013-08-08 20:11:24 -04004705static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004706{
Tejun Heo182446d2013-08-08 20:11:24 -04004707 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07004708}
4709
4710static struct cftype debug_files[] = {
4711 {
Paul Menagefe693432009-09-23 15:56:20 -07004712 .name = "taskcount",
4713 .read_u64 = debug_taskcount_read,
4714 },
4715
4716 {
4717 .name = "current_css_set",
4718 .read_u64 = current_css_set_read,
4719 },
4720
4721 {
4722 .name = "current_css_set_refcount",
4723 .read_u64 = current_css_set_refcount_read,
4724 },
4725
4726 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004727 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004728 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004729 },
4730
4731 {
4732 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004733 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004734 },
4735
4736 {
Paul Menagefe693432009-09-23 15:56:20 -07004737 .name = "releasable",
4738 .read_u64 = releasable_read,
4739 },
Paul Menagefe693432009-09-23 15:56:20 -07004740
Tejun Heo4baf6e32012-04-01 12:09:55 -07004741 { } /* terminate */
4742};
Paul Menagefe693432009-09-23 15:56:20 -07004743
Tejun Heo073219e2014-02-08 10:36:58 -05004744struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08004745 .css_alloc = debug_css_alloc,
4746 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07004747 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07004748};
4749#endif /* CONFIG_CGROUP_DEBUG */