blob: fc2db071d95e423a3aa8f9f010c1713a8d01c25a [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 Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/slab.h>
45#include <linux/magic.h>
46#include <linux/spinlock.h>
47#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070048#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070049#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070050#include <linux/delayacct.h>
51#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080052#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040053#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070054#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070055#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070056#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Li Zefan081aa452013-03-13 09:17:09 +080057#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020058#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070059
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070061
Tejun Heoe25e2cb2011-12-12 18:12:21 -080062/*
Tejun Heob1a21362013-11-29 10:42:58 -050063 * pidlists linger the following amount before being destroyed. The goal
64 * is avoiding frequent destruction in the middle of consecutive read calls
65 * Expiring in the middle is a performance problem not a correctness one.
66 * 1 sec should be enough.
67 */
68#define CGROUP_PIDLIST_DESTROY_DELAY HZ
69
70/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080071 * cgroup_mutex is the master lock. Any modification to cgroup or its
72 * hierarchy must be performed while holding it.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080073 */
Tejun Heo22194492013-04-07 09:29:51 -070074#ifdef CONFIG_PROVE_RCU
75DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040076EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070077#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070078static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070079#endif
80
Tejun Heo69e943b2014-02-08 10:36:58 -050081/*
82 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
83 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
84 */
85static DEFINE_SPINLOCK(release_agent_path_lock);
86
Tejun Heo87fb54f2013-12-06 15:11:55 -050087#define cgroup_assert_mutex_or_rcu_locked() \
88 rcu_lockdep_assert(rcu_read_lock_held() || \
89 lockdep_is_held(&cgroup_mutex), \
90 "cgroup_mutex or RCU read lock required");
91
Ben Blumaae8aab2010-03-10 15:22:07 -080092/*
Tejun Heoe5fca242013-11-22 17:14:39 -050093 * cgroup destruction makes heavy use of work items and there can be a lot
94 * of concurrent destructions. Use a separate workqueue so that cgroup
95 * destruction work items don't end up filling up max_active of system_wq
96 * which may lead to deadlock.
97 */
98static struct workqueue_struct *cgroup_destroy_wq;
99
100/*
Tejun Heob1a21362013-11-29 10:42:58 -0500101 * pidlist destructions need to be flushed on cgroup destruction. Use a
102 * separate workqueue as flush domain.
103 */
104static struct workqueue_struct *cgroup_pidlist_destroy_wq;
105
Tejun Heo3ed80a62014-02-08 10:36:58 -0500106/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500107#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500108static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700109#include <linux/cgroup_subsys.h>
110};
Tejun Heo073219e2014-02-08 10:36:58 -0500111#undef SUBSYS
112
113/* array of cgroup subsystem names */
114#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
115static const char *cgroup_subsys_name[] = {
116#include <linux/cgroup_subsys.h>
117};
118#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700119
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700121 * The dummy hierarchy, reserved for the subsystems that are otherwise
122 * unattached - it never has more than a single cgroup, and all tasks are
123 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700124 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700125static struct cgroupfs_root cgroup_dummy_root;
126
127/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
128static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700129
130/* The list of hierarchy roots */
131
Tejun Heo9871bf92013-06-24 15:21:47 -0700132static LIST_HEAD(cgroup_roots);
133static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700134
Tejun Heo3417ae12014-02-08 10:37:01 -0500135/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700136static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700137
Li Zefan65dff752013-03-01 15:01:56 +0800138static struct cgroup_name root_cgroup_name = { .name = "/" };
139
Li Zefan794611a2013-06-18 18:53:53 +0800140/*
141 * Assign a monotonically increasing serial number to cgroups. It
142 * guarantees cgroups with bigger numbers are newer than those with smaller
143 * numbers. Also, as cgroups are always appended to the parent's
144 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700145 * the ascending serial number order on the list. Protected by
146 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800147 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700148static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800149
Paul Menageddbcc7e2007-10-18 23:39:30 -0700150/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800151 * check for fork/exit handlers to call. This avoids us having to do
152 * extra work in the fork/exit path if none of the subsystems need to
153 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700154 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700155static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700156
Tejun Heo628f7cd2013-06-28 16:24:11 -0700157static struct cftype cgroup_base_files[];
158
Tejun Heof20104d2013-08-13 20:22:50 -0400159static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800160static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400161static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
162 bool is_add);
Tejun Heoe605b362013-11-27 18:16:21 -0500163static int cgroup_file_release(struct inode *inode, struct file *file);
Tejun Heob1a21362013-11-29 10:42:58 -0500164static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800165
Tejun Heo95109b62013-08-08 20:11:27 -0400166/**
167 * cgroup_css - obtain a cgroup's css for the specified subsystem
168 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400169 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400170 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400171 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
172 * function must be called either under cgroup_mutex or rcu_read_lock() and
173 * the caller is responsible for pinning the returned css if it wants to
174 * keep accessing it outside the said locks. This function may return
175 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400176 */
177static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400178 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400179{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400180 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500181 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoca8bdca2013-08-26 18:40:56 -0400182 lockdep_is_held(&cgroup_mutex));
183 else
184 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400185}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700188static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700189{
Tejun Heo54766d42013-06-12 21:04:53 -0700190 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191}
192
Li Zefan78574cf2013-04-08 19:00:38 -0700193/**
194 * cgroup_is_descendant - test ancestry
195 * @cgrp: the cgroup to be tested
196 * @ancestor: possible ancestor of @cgrp
197 *
198 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
199 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
200 * and @ancestor are accessible.
201 */
202bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
203{
204 while (cgrp) {
205 if (cgrp == ancestor)
206 return true;
207 cgrp = cgrp->parent;
208 }
209 return false;
210}
211EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700212
Adrian Bunke9685a02008-02-07 00:13:46 -0800213static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700214{
215 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700216 (1 << CGRP_RELEASABLE) |
217 (1 << CGRP_NOTIFY_ON_RELEASE);
218 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700219}
220
Adrian Bunke9685a02008-02-07 00:13:46 -0800221static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700222{
Paul Menagebd89aab2007-10-18 23:40:44 -0700223 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700224}
225
Tejun Heo30159ec2013-06-25 11:53:37 -0700226/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500227 * for_each_css - iterate all css's of a cgroup
228 * @css: the iteration cursor
229 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
230 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700231 *
232 * Should be called under cgroup_mutex.
233 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500234#define for_each_css(css, ssid, cgrp) \
235 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
236 if (!((css) = rcu_dereference_check( \
237 (cgrp)->subsys[(ssid)], \
238 lockdep_is_held(&cgroup_mutex)))) { } \
239 else
240
241/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500242 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700243 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500244 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700245 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500246#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500247 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
248 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700249
Tejun Heo5549c492013-06-24 15:21:48 -0700250/* iterate across the active hierarchies */
251#define for_each_active_root(root) \
252 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700253
Tejun Heof6ea9372012-04-01 12:09:55 -0700254static inline struct cgroup *__d_cgrp(struct dentry *dentry)
255{
256 return dentry->d_fsdata;
257}
258
Tejun Heo05ef1d72012-04-01 12:09:56 -0700259static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700260{
261 return dentry->d_fsdata;
262}
263
Tejun Heo05ef1d72012-04-01 12:09:56 -0700264static inline struct cftype *__d_cft(struct dentry *dentry)
265{
266 return __d_cfe(dentry)->type;
267}
268
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700269/**
270 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
271 * @cgrp: the cgroup to be checked for liveness
272 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700273 * On success, returns true; the mutex should be later unlocked. On
274 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700275 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700276static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700277{
278 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700279 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700280 mutex_unlock(&cgroup_mutex);
281 return false;
282 }
283 return true;
284}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700285
Paul Menage81a6a5c2007-10-18 23:39:38 -0700286/* the list of cgroups eligible for automatic release. Protected by
287 * release_list_lock */
288static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200289static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700290static void cgroup_release_agent(struct work_struct *work);
291static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700292static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700293
Tejun Heo69d02062013-06-12 21:04:50 -0700294/*
295 * A cgroup can be associated with multiple css_sets as different tasks may
296 * belong to different cgroups on different hierarchies. In the other
297 * direction, a css_set is naturally associated with multiple cgroups.
298 * This M:N relationship is represented by the following link structure
299 * which exists for each association and allows traversing the associations
300 * from both sides.
301 */
302struct cgrp_cset_link {
303 /* the cgroup and css_set this link associates */
304 struct cgroup *cgrp;
305 struct css_set *cset;
306
307 /* list of cgrp_cset_links anchored at cgrp->cset_links */
308 struct list_head cset_link;
309
310 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
311 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700312};
313
314/* The default css_set - used by init and its children prior to any
315 * hierarchies being mounted. It contains a pointer to the root state
316 * for each subsystem. Also used to anchor the list of css_sets. Not
317 * reference-counted, to improve performance when child cgroups
318 * haven't been created.
319 */
320
321static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700322static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700323
Tejun Heo0942eee2013-08-08 20:11:26 -0400324/*
325 * css_set_lock protects the list of css_set objects, and the chain of
326 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400327 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400328 */
Paul Menage817929e2007-10-18 23:39:36 -0700329static DEFINE_RWLOCK(css_set_lock);
330static int css_set_count;
331
Paul Menage7717f7b2009-09-23 15:56:22 -0700332/*
333 * hash table for cgroup groups. This improves the performance to find
334 * an existing css_set. This hash doesn't (currently) take into
335 * account cgroups in empty hierarchies.
336 */
Li Zefan472b1052008-04-29 01:00:11 -0700337#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800338static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700339
Li Zefan0ac801f2013-01-10 11:49:27 +0800340static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700341{
Li Zefan0ac801f2013-01-10 11:49:27 +0800342 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700343 struct cgroup_subsys *ss;
344 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700345
Tejun Heo30159ec2013-06-25 11:53:37 -0700346 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800347 key += (unsigned long)css[i];
348 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700349
Li Zefan0ac801f2013-01-10 11:49:27 +0800350 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700351}
352
Tejun Heo0942eee2013-08-08 20:11:26 -0400353/*
354 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400355 * until after the first call to css_task_iter_start(). This reduces the
356 * fork()/exit() overhead for people who have cgroups compiled into their
357 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400358 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700359static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700360
Tejun Heo5abb8852013-06-12 21:04:49 -0700361static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700362{
Tejun Heo69d02062013-06-12 21:04:50 -0700363 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700364
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700365 /*
366 * Ensure that the refcount doesn't hit zero while any readers
367 * can see it. Similar to atomic_dec_and_lock(), but for an
368 * rwlock
369 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700370 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700371 return;
372 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700373 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700374 write_unlock(&css_set_lock);
375 return;
376 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700377
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700378 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700379 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700380 css_set_count--;
381
Tejun Heo69d02062013-06-12 21:04:50 -0700382 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700383 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700384
Tejun Heo69d02062013-06-12 21:04:50 -0700385 list_del(&link->cset_link);
386 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800387
Tejun Heoddd69142013-06-12 21:04:54 -0700388 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700389 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700390 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700391 set_bit(CGRP_RELEASABLE, &cgrp->flags);
392 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700393 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700394
395 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700396 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700397
398 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700399 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700400}
401
402/*
403 * refcounted get/put for css_set objects
404 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700405static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700406{
Tejun Heo5abb8852013-06-12 21:04:49 -0700407 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700408}
409
Tejun Heo5abb8852013-06-12 21:04:49 -0700410static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700411{
Tejun Heo5abb8852013-06-12 21:04:49 -0700412 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700413}
414
Tejun Heo5abb8852013-06-12 21:04:49 -0700415static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700416{
Tejun Heo5abb8852013-06-12 21:04:49 -0700417 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700418}
419
Tejun Heob326f9d2013-06-24 15:21:48 -0700420/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700421 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700422 * @cset: candidate css_set being tested
423 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700424 * @new_cgrp: cgroup that's being entered by the task
425 * @template: desired set of css pointers in css_set (pre-calculated)
426 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800427 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700428 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
429 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700430static bool compare_css_sets(struct css_set *cset,
431 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700432 struct cgroup *new_cgrp,
433 struct cgroup_subsys_state *template[])
434{
435 struct list_head *l1, *l2;
436
Tejun Heo5abb8852013-06-12 21:04:49 -0700437 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700438 /* Not all subsystems matched */
439 return false;
440 }
441
442 /*
443 * Compare cgroup pointers in order to distinguish between
444 * different cgroups in heirarchies with no subsystems. We
445 * could get by with just this check alone (and skip the
446 * memcmp above) but on most setups the memcmp check will
447 * avoid the need for this more expensive check on almost all
448 * candidates.
449 */
450
Tejun Heo69d02062013-06-12 21:04:50 -0700451 l1 = &cset->cgrp_links;
452 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700453 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700454 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700455 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700456
457 l1 = l1->next;
458 l2 = l2->next;
459 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700460 if (l1 == &cset->cgrp_links) {
461 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700462 break;
463 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700464 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700465 }
466 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700467 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
468 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
469 cgrp1 = link1->cgrp;
470 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700471 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700472 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700473
474 /*
475 * If this hierarchy is the hierarchy of the cgroup
476 * that's changing, then we need to check that this
477 * css_set points to the new cgroup; if it's any other
478 * hierarchy, then this css_set should point to the
479 * same cgroup as the old css_set.
480 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700481 if (cgrp1->root == new_cgrp->root) {
482 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700483 return false;
484 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700485 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700486 return false;
487 }
488 }
489 return true;
490}
491
Tejun Heob326f9d2013-06-24 15:21:48 -0700492/**
493 * find_existing_css_set - init css array and find the matching css_set
494 * @old_cset: the css_set that we're using before the cgroup transition
495 * @cgrp: the cgroup that we're moving into
496 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700497 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700498static struct css_set *find_existing_css_set(struct css_set *old_cset,
499 struct cgroup *cgrp,
500 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700501{
Paul Menagebd89aab2007-10-18 23:40:44 -0700502 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700503 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700504 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800505 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700506 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700507
Ben Blumaae8aab2010-03-10 15:22:07 -0800508 /*
509 * Build the set of subsystem state objects that we want to see in the
510 * new css_set. while subsystems can change globally, the entries here
511 * won't change, so no need for locking.
512 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700513 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400514 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700515 /* Subsystem is in this hierarchy. So we want
516 * the subsystem state from the new
517 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400518 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700519 } else {
520 /* Subsystem is not in this hierarchy, so we
521 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700522 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700523 }
524 }
525
Li Zefan0ac801f2013-01-10 11:49:27 +0800526 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700527 hash_for_each_possible(css_set_table, cset, hlist, key) {
528 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700529 continue;
530
531 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700532 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700533 }
Paul Menage817929e2007-10-18 23:39:36 -0700534
535 /* No existing cgroup group matched */
536 return NULL;
537}
538
Tejun Heo69d02062013-06-12 21:04:50 -0700539static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700540{
Tejun Heo69d02062013-06-12 21:04:50 -0700541 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700542
Tejun Heo69d02062013-06-12 21:04:50 -0700543 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
544 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700545 kfree(link);
546 }
547}
548
Tejun Heo69d02062013-06-12 21:04:50 -0700549/**
550 * allocate_cgrp_cset_links - allocate cgrp_cset_links
551 * @count: the number of links to allocate
552 * @tmp_links: list_head the allocated links are put on
553 *
554 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
555 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700556 */
Tejun Heo69d02062013-06-12 21:04:50 -0700557static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700558{
Tejun Heo69d02062013-06-12 21:04:50 -0700559 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700560 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700561
562 INIT_LIST_HEAD(tmp_links);
563
Li Zefan36553432008-07-29 22:33:19 -0700564 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700565 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700566 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700567 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700568 return -ENOMEM;
569 }
Tejun Heo69d02062013-06-12 21:04:50 -0700570 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700571 }
572 return 0;
573}
574
Li Zefanc12f65d2009-01-07 18:07:42 -0800575/**
576 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700577 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700578 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800579 * @cgrp: the destination cgroup
580 */
Tejun Heo69d02062013-06-12 21:04:50 -0700581static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
582 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800583{
Tejun Heo69d02062013-06-12 21:04:50 -0700584 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800585
Tejun Heo69d02062013-06-12 21:04:50 -0700586 BUG_ON(list_empty(tmp_links));
587 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
588 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700589 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700590 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700591 /*
592 * Always add links to the tail of the list so that the list
593 * is sorted by order of hierarchy creation
594 */
Tejun Heo69d02062013-06-12 21:04:50 -0700595 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800596}
597
Tejun Heob326f9d2013-06-24 15:21:48 -0700598/**
599 * find_css_set - return a new css_set with one cgroup updated
600 * @old_cset: the baseline css_set
601 * @cgrp: the cgroup to be updated
602 *
603 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
604 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700605 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700606static struct css_set *find_css_set(struct css_set *old_cset,
607 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700608{
Tejun Heob326f9d2013-06-24 15:21:48 -0700609 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700610 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700611 struct list_head tmp_links;
612 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800613 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700614
Tejun Heob326f9d2013-06-24 15:21:48 -0700615 lockdep_assert_held(&cgroup_mutex);
616
Paul Menage817929e2007-10-18 23:39:36 -0700617 /* First see if we already have a cgroup group that matches
618 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700619 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700620 cset = find_existing_css_set(old_cset, cgrp, template);
621 if (cset)
622 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700623 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700624
Tejun Heo5abb8852013-06-12 21:04:49 -0700625 if (cset)
626 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700627
Tejun Heof4f4be22013-06-12 21:04:51 -0700628 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700629 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700630 return NULL;
631
Tejun Heo69d02062013-06-12 21:04:50 -0700632 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700633 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700634 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700635 return NULL;
636 }
637
Tejun Heo5abb8852013-06-12 21:04:49 -0700638 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700639 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700640 INIT_LIST_HEAD(&cset->tasks);
641 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700642
643 /* Copy the set of subsystem state objects generated in
644 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700645 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700646
647 write_lock(&css_set_lock);
648 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700649 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700650 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700651
Paul Menage7717f7b2009-09-23 15:56:22 -0700652 if (c->root == cgrp->root)
653 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700654 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700655 }
Paul Menage817929e2007-10-18 23:39:36 -0700656
Tejun Heo69d02062013-06-12 21:04:50 -0700657 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700658
Paul Menage817929e2007-10-18 23:39:36 -0700659 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700660
661 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700662 key = css_set_hash(cset->subsys);
663 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700664
Paul Menage817929e2007-10-18 23:39:36 -0700665 write_unlock(&css_set_lock);
666
Tejun Heo5abb8852013-06-12 21:04:49 -0700667 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700668}
669
Paul Menageddbcc7e2007-10-18 23:39:30 -0700670/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700671 * Return the cgroup for "task" from the given hierarchy. Must be
672 * called with cgroup_mutex held.
673 */
674static struct cgroup *task_cgroup_from_root(struct task_struct *task,
675 struct cgroupfs_root *root)
676{
Tejun Heo5abb8852013-06-12 21:04:49 -0700677 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700678 struct cgroup *res = NULL;
679
680 BUG_ON(!mutex_is_locked(&cgroup_mutex));
681 read_lock(&css_set_lock);
682 /*
683 * No need to lock the task - since we hold cgroup_mutex the
684 * task can't change groups, so the only thing that can happen
685 * is that it exits and its css is set back to init_css_set.
686 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700687 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700688 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700689 res = &root->top_cgroup;
690 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700691 struct cgrp_cset_link *link;
692
693 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700694 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700695
Paul Menage7717f7b2009-09-23 15:56:22 -0700696 if (c->root == root) {
697 res = c;
698 break;
699 }
700 }
701 }
702 read_unlock(&css_set_lock);
703 BUG_ON(!res);
704 return res;
705}
706
707/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700708 * There is one global cgroup mutex. We also require taking
709 * task_lock() when dereferencing a task's cgroup subsys pointers.
710 * See "The task_lock() exception", at the end of this comment.
711 *
712 * A task must hold cgroup_mutex to modify cgroups.
713 *
714 * Any task can increment and decrement the count field without lock.
715 * So in general, code holding cgroup_mutex can't rely on the count
716 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800717 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700718 * means that no tasks are currently attached, therefore there is no
719 * way a task attached to that cgroup can fork (the other way to
720 * increment the count). So code holding cgroup_mutex can safely
721 * assume that if the count is zero, it will stay zero. Similarly, if
722 * a task holds cgroup_mutex on a cgroup with zero count, it
723 * knows that the cgroup won't be removed, as cgroup_rmdir()
724 * needs that mutex.
725 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700726 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
727 * (usually) take cgroup_mutex. These are the two most performance
728 * critical pieces of code here. The exception occurs on cgroup_exit(),
729 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
730 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800731 * to the release agent with the name of the cgroup (path relative to
732 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700733 *
734 * A cgroup can only be deleted if both its 'count' of using tasks
735 * is zero, and its list of 'children' cgroups is empty. Since all
736 * tasks in the system use _some_ cgroup, and since there is always at
737 * least one task in the system (init, pid == 1), therefore, top_cgroup
738 * always has either children cgroups and/or using tasks. So we don't
739 * need a special hack to ensure that top_cgroup cannot be deleted.
740 *
741 * The task_lock() exception
742 *
743 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800744 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800745 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700746 * several performance critical places that need to reference
747 * task->cgroup without the expense of grabbing a system global
748 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800749 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700750 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
751 * the task_struct routinely used for such matters.
752 *
753 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800754 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700755 */
756
Paul Menageddbcc7e2007-10-18 23:39:30 -0700757/*
758 * A couple of forward declarations required, due to cyclic reference loop:
759 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
760 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
761 * -> cgroup_mkdir.
762 */
763
Al Viro18bb1db2011-07-26 01:41:39 -0400764static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700765static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700766static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700767static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700768static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700769
770static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200771 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700772 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700773};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700774
Al Viroa5e7ed32011-07-26 01:55:55 -0400775static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700776{
777 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700778
779 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400780 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700781 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100782 inode->i_uid = current_fsuid();
783 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700784 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
785 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
786 }
787 return inode;
788}
789
Li Zefan65dff752013-03-01 15:01:56 +0800790static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
791{
792 struct cgroup_name *name;
793
794 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
795 if (!name)
796 return NULL;
797 strcpy(name->name, dentry->d_name.name);
798 return name;
799}
800
Li Zefanbe445622013-01-24 14:31:42 +0800801static void cgroup_free_fn(struct work_struct *work)
802{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700803 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800804
805 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800806 cgrp->root->number_of_cgroups--;
807 mutex_unlock(&cgroup_mutex);
808
809 /*
Li Zefan415cf072013-04-08 14:35:02 +0800810 * We get a ref to the parent's dentry, and put the ref when
811 * this cgroup is being freed, so it's guaranteed that the
812 * parent won't be destroyed before its children.
813 */
814 dput(cgrp->parent->dentry);
815
816 /*
Li Zefanbe445622013-01-24 14:31:42 +0800817 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700818 * created the cgroup. This will free cgrp->root, if we are
819 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800820 */
821 deactivate_super(cgrp->root->sb);
822
Tejun Heob1a21362013-11-29 10:42:58 -0500823 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800824
825 simple_xattrs_free(&cgrp->xattrs);
826
Li Zefan65dff752013-03-01 15:01:56 +0800827 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800828 kfree(cgrp);
829}
830
831static void cgroup_free_rcu(struct rcu_head *head)
832{
833 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
834
Tejun Heoea15f8c2013-06-13 19:27:42 -0700835 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500836 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800837}
838
Paul Menageddbcc7e2007-10-18 23:39:30 -0700839static void cgroup_diput(struct dentry *dentry, struct inode *inode)
840{
841 /* is dentry a directory ? if so, kfree() associated cgroup */
842 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700843 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800844
Tejun Heo54766d42013-06-12 21:04:53 -0700845 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanc1a71502013-12-17 11:13:39 +0800846
847 /*
848 * XXX: cgrp->id is only used to look up css's. As cgroup
849 * and css's lifetimes will be decoupled, it should be made
850 * per-subsystem and moved to css->id so that lookups are
851 * successful until the target css is released.
852 */
Li Zefan0ab02ca2014-02-11 16:05:46 +0800853 mutex_lock(&cgroup_mutex);
Li Zefanc1a71502013-12-17 11:13:39 +0800854 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +0800855 mutex_unlock(&cgroup_mutex);
Li Zefanc1a71502013-12-17 11:13:39 +0800856 cgrp->id = -1;
857
Li Zefanbe445622013-01-24 14:31:42 +0800858 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700859 } else {
860 struct cfent *cfe = __d_cfe(dentry);
861 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
862
863 WARN_ONCE(!list_empty(&cfe->node) &&
864 cgrp != &cgrp->root->top_cgroup,
865 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700866 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700867 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700868 }
869 iput(inode);
870}
871
872static void remove_dir(struct dentry *d)
873{
874 struct dentry *parent = dget(d->d_parent);
875
876 d_delete(d);
877 simple_rmdir(parent->d_inode, d);
878 dput(parent);
879}
880
Li Zefan2739d3c2013-01-21 18:18:33 +0800881static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700882{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700883 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700884
Tejun Heo05ef1d72012-04-01 12:09:56 -0700885 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
886 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100887
Li Zefan2739d3c2013-01-21 18:18:33 +0800888 /*
889 * If we're doing cleanup due to failure of cgroup_create(),
890 * the corresponding @cfe may not exist.
891 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700892 list_for_each_entry(cfe, &cgrp->files, node) {
893 struct dentry *d = cfe->dentry;
894
895 if (cft && cfe->type != cft)
896 continue;
897
898 dget(d);
899 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700900 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700901 list_del_init(&cfe->node);
902 dput(d);
903
Li Zefan2739d3c2013-01-21 18:18:33 +0800904 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700905 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700906}
907
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400908/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700909 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700910 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400911 * @subsys_mask: mask of the subsystem ids whose files should be removed
912 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700913static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700914{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400915 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700916 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700917
Tejun Heob420ba72013-07-12 12:34:02 -0700918 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400919 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700920
921 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400922 continue;
923 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400924 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400925 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700926}
927
928/*
929 * NOTE : the dentry must have been dget()'ed
930 */
931static void cgroup_d_remove_dir(struct dentry *dentry)
932{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100933 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700934
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100935 parent = dentry->d_parent;
936 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800937 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700938 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100939 spin_unlock(&dentry->d_lock);
940 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700941 remove_dir(dentry);
942}
943
944static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700945 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700946{
Paul Menagebd89aab2007-10-18 23:40:44 -0700947 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700948 struct cgroup_subsys *ss;
Tejun Heo31261212013-06-28 17:07:30 -0700949 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700950
Ben Blumaae8aab2010-03-10 15:22:07 -0800951 BUG_ON(!mutex_is_locked(&cgroup_mutex));
952
Paul Menageddbcc7e2007-10-18 23:39:30 -0700953 /* Check that any added subsystems are currently free */
Tejun Heo3ed80a62014-02-08 10:36:58 -0500954 for_each_subsys(ss, i)
955 if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
956 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700957
Tejun Heo31261212013-06-28 17:07:30 -0700958 ret = cgroup_populate_dir(cgrp, added_mask);
959 if (ret)
Tejun Heo3ed80a62014-02-08 10:36:58 -0500960 return ret;
Tejun Heo31261212013-06-28 17:07:30 -0700961
962 /*
963 * Nothing can fail from this point on. Remove files for the
964 * removed subsystems and rebind each subsystem.
965 */
966 cgroup_clear_dir(cgrp, removed_mask);
967
Tejun Heo30159ec2013-06-25 11:53:37 -0700968 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700969 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -0700970
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400971 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400973 BUG_ON(cgroup_css(cgrp, ss));
974 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
975 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -0700976
Tejun Heo73e80ed2013-08-13 11:01:55 -0400977 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -0400978 cgroup_css(cgroup_dummy_top, ss));
979 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -0400980
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800981 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700982 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -0400983 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -0700984
Ben Blumcf5d5942010-03-10 15:22:09 -0800985 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -0700986 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400987 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700988 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400989 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
990 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -0700991
Paul Menageddbcc7e2007-10-18 23:39:30 -0700992 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -0400993 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -0400994
Tejun Heoca8bdca2013-08-26 18:40:56 -0400995 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -0400996 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
997
Tejun Heo9871bf92013-06-24 15:21:47 -0700998 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -0700999 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000 }
1001 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002
Tejun Heo1672d042013-06-25 18:04:54 -07001003 /*
1004 * Mark @root has finished binding subsystems. @root->subsys_mask
1005 * now matches the bound subsystems.
1006 */
1007 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1008
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009 return 0;
1010}
1011
Al Viro34c80b12011-12-08 21:32:45 -05001012static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013{
Al Viro34c80b12011-12-08 21:32:45 -05001014 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001016 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017
Tejun Heob85d2042013-12-06 15:11:57 -05001018 for_each_subsys(ss, ssid)
1019 if (root->subsys_mask & (1 << ssid))
1020 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001021 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1022 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001023 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001025 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001026 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001027
1028 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001029 if (strlen(root->release_agent_path))
1030 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001031 spin_unlock(&release_agent_path_lock);
1032
Tejun Heo2260e7f2012-11-19 08:13:38 -08001033 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001034 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001035 if (strlen(root->name))
1036 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001037 return 0;
1038}
1039
1040struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001041 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001042 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001043 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001044 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001045 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001046 /* User explicitly requested empty subsystem */
1047 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001048
1049 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001050
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051};
1052
Ben Blumaae8aab2010-03-10 15:22:07 -08001053/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001054 * Convert a hierarchy specifier into a bitmask of subsystems and
1055 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1056 * array. This function takes refcounts on subsystems to be used, unless it
1057 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001058 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001059static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001061 char *token, *o = data;
1062 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001063 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001064 struct cgroup_subsys *ss;
1065 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001066
Ben Blumaae8aab2010-03-10 15:22:07 -08001067 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1068
Li Zefanf9ab5b52009-06-17 16:26:33 -07001069#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001070 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001071#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072
Paul Menagec6d57f32009-09-23 15:56:19 -07001073 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074
1075 while ((token = strsep(&o, ",")) != NULL) {
1076 if (!*token)
1077 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001078 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001079 /* Explicitly have no subsystems */
1080 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001081 continue;
1082 }
1083 if (!strcmp(token, "all")) {
1084 /* Mutually exclusive option 'all' + subsystem name */
1085 if (one_ss)
1086 return -EINVAL;
1087 all_ss = true;
1088 continue;
1089 }
Tejun Heo873fe092013-04-14 20:15:26 -07001090 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1091 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1092 continue;
1093 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001094 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001095 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001096 continue;
1097 }
1098 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001099 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001100 continue;
1101 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001102 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001103 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001104 continue;
1105 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001106 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001107 /* Specifying two release agents is forbidden */
1108 if (opts->release_agent)
1109 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001110 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001111 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001112 if (!opts->release_agent)
1113 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001114 continue;
1115 }
1116 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001117 const char *name = token + 5;
1118 /* Can't specify an empty name */
1119 if (!strlen(name))
1120 return -EINVAL;
1121 /* Must match [\w.-]+ */
1122 for (i = 0; i < strlen(name); i++) {
1123 char c = name[i];
1124 if (isalnum(c))
1125 continue;
1126 if ((c == '.') || (c == '-') || (c == '_'))
1127 continue;
1128 return -EINVAL;
1129 }
1130 /* Specifying two names is forbidden */
1131 if (opts->name)
1132 return -EINVAL;
1133 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001134 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001135 GFP_KERNEL);
1136 if (!opts->name)
1137 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001138
1139 continue;
1140 }
1141
Tejun Heo30159ec2013-06-25 11:53:37 -07001142 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001143 if (strcmp(token, ss->name))
1144 continue;
1145 if (ss->disabled)
1146 continue;
1147
1148 /* Mutually exclusive option 'all' + subsystem name */
1149 if (all_ss)
1150 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001151 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001152 one_ss = true;
1153
1154 break;
1155 }
1156 if (i == CGROUP_SUBSYS_COUNT)
1157 return -ENOENT;
1158 }
1159
1160 /*
1161 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001162 * otherwise if 'none', 'name=' and a subsystem name options
1163 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001164 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001165 if (all_ss || (!one_ss && !opts->none && !opts->name))
1166 for_each_subsys(ss, i)
1167 if (!ss->disabled)
1168 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001169
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001170 /* Consistency checks */
1171
Tejun Heo873fe092013-04-14 20:15:26 -07001172 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1173 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1174
1175 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1176 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1177 return -EINVAL;
1178 }
1179
1180 if (opts->cpuset_clone_children) {
1181 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1182 return -EINVAL;
1183 }
1184 }
1185
Li Zefanf9ab5b52009-06-17 16:26:33 -07001186 /*
1187 * Option noprefix was introduced just for backward compatibility
1188 * with the old cpuset, so we allow noprefix only if mounting just
1189 * the cpuset subsystem.
1190 */
Tejun Heo93438622013-04-14 20:15:25 -07001191 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001192 return -EINVAL;
1193
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001194
1195 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001196 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001197 return -EINVAL;
1198
1199 /*
1200 * We either have to specify by name or by subsystems. (So all
1201 * empty hierarchies must have a name).
1202 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001203 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001204 return -EINVAL;
1205
1206 return 0;
1207}
1208
1209static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1210{
1211 int ret = 0;
1212 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001213 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001214 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001215 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001216
Tejun Heo873fe092013-04-14 20:15:26 -07001217 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1218 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1219 return -EINVAL;
1220 }
1221
Paul Menagebd89aab2007-10-18 23:40:44 -07001222 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001223 mutex_lock(&cgroup_mutex);
1224
1225 /* See what subsystems are wanted */
1226 ret = parse_cgroupfs_options(data, &opts);
1227 if (ret)
1228 goto out_unlock;
1229
Tejun Heoa8a648c2013-06-24 15:21:47 -07001230 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001231 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1232 task_tgid_nr(current), current->comm);
1233
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001234 added_mask = opts.subsys_mask & ~root->subsys_mask;
1235 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001236
Ben Blumcf5d5942010-03-10 15:22:09 -08001237 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001238 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001239 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001240 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1241 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1242 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001243 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001244 goto out_unlock;
1245 }
1246
Tejun Heof172e672013-06-28 17:07:30 -07001247 /* remounting is not allowed for populated hierarchies */
1248 if (root->number_of_cgroups > 1) {
1249 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001250 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001251 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001252
Paul Menageddbcc7e2007-10-18 23:39:30 -07001253 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001254 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001255 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001256
Tejun Heo69e943b2014-02-08 10:36:58 -05001257 if (opts.release_agent) {
1258 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001259 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001260 spin_unlock(&release_agent_path_lock);
1261 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001262 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001263 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001264 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001265 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001266 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001267 return ret;
1268}
1269
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001270static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001271 .statfs = simple_statfs,
1272 .drop_inode = generic_delete_inode,
1273 .show_options = cgroup_show_options,
1274 .remount_fs = cgroup_remount,
1275};
1276
Paul Menagecc31edc2008-10-18 20:28:04 -07001277static void init_cgroup_housekeeping(struct cgroup *cgrp)
1278{
1279 INIT_LIST_HEAD(&cgrp->sibling);
1280 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001281 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001282 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001283 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001284 INIT_LIST_HEAD(&cgrp->pidlists);
1285 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001286 cgrp->dummy_css.cgroup = cgrp;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001287 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001288}
Paul Menagec6d57f32009-09-23 15:56:19 -07001289
Paul Menageddbcc7e2007-10-18 23:39:30 -07001290static void init_cgroup_root(struct cgroupfs_root *root)
1291{
Paul Menagebd89aab2007-10-18 23:40:44 -07001292 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001293
Paul Menageddbcc7e2007-10-18 23:39:30 -07001294 INIT_LIST_HEAD(&root->root_list);
1295 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001296 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001297 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001298 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001299 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001300}
1301
Tejun Heofc76df72013-06-25 11:53:37 -07001302static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001303{
Tejun Heo1a574232013-04-14 11:36:58 -07001304 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001305
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001306 lockdep_assert_held(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001307
Tejun Heofc76df72013-06-25 11:53:37 -07001308 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1309 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001310 if (id < 0)
1311 return id;
1312
1313 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001314 return 0;
1315}
1316
1317static void cgroup_exit_root_id(struct cgroupfs_root *root)
1318{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001319 lockdep_assert_held(&cgroup_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001320
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001321 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001322 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001323 root->hierarchy_id = 0;
1324 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001325}
1326
Paul Menageddbcc7e2007-10-18 23:39:30 -07001327static int cgroup_test_super(struct super_block *sb, void *data)
1328{
Paul Menagec6d57f32009-09-23 15:56:19 -07001329 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330 struct cgroupfs_root *root = sb->s_fs_info;
1331
Paul Menagec6d57f32009-09-23 15:56:19 -07001332 /* If we asked for a name then it must match */
1333 if (opts->name && strcmp(opts->name, root->name))
1334 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001336 /*
1337 * If we asked for subsystems (or explicitly for no
1338 * subsystems) then they must match
1339 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001340 if ((opts->subsys_mask || opts->none)
1341 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342 return 0;
1343
1344 return 1;
1345}
1346
Paul Menagec6d57f32009-09-23 15:56:19 -07001347static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1348{
1349 struct cgroupfs_root *root;
1350
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001351 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001352 return NULL;
1353
1354 root = kzalloc(sizeof(*root), GFP_KERNEL);
1355 if (!root)
1356 return ERR_PTR(-ENOMEM);
1357
1358 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001359
Tejun Heo1672d042013-06-25 18:04:54 -07001360 /*
1361 * We need to set @root->subsys_mask now so that @root can be
1362 * matched by cgroup_test_super() before it finishes
1363 * initialization; otherwise, competing mounts with the same
1364 * options may try to bind the same subsystems instead of waiting
1365 * for the first one leading to unexpected mount errors.
1366 * SUBSYS_BOUND will be set once actual binding is complete.
1367 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001368 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001369 root->flags = opts->flags;
1370 if (opts->release_agent)
1371 strcpy(root->release_agent_path, opts->release_agent);
1372 if (opts->name)
1373 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001374 if (opts->cpuset_clone_children)
1375 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001376 return root;
1377}
1378
Tejun Heofa3ca072013-04-14 11:36:56 -07001379static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001380{
Tejun Heofa3ca072013-04-14 11:36:56 -07001381 if (root) {
1382 /* hierarhcy ID shoulid already have been released */
1383 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001384
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001385 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001386 kfree(root);
1387 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001388}
1389
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390static int cgroup_set_super(struct super_block *sb, void *data)
1391{
1392 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001393 struct cgroup_sb_opts *opts = data;
1394
1395 /* If we don't have a new root, we can't set up a new sb */
1396 if (!opts->new_root)
1397 return -EINVAL;
1398
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001399 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001400
1401 ret = set_anon_super(sb, NULL);
1402 if (ret)
1403 return ret;
1404
Paul Menagec6d57f32009-09-23 15:56:19 -07001405 sb->s_fs_info = opts->new_root;
1406 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001407
1408 sb->s_blocksize = PAGE_CACHE_SIZE;
1409 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1410 sb->s_magic = CGROUP_SUPER_MAGIC;
1411 sb->s_op = &cgroup_ops;
1412
1413 return 0;
1414}
1415
1416static int cgroup_get_rootdir(struct super_block *sb)
1417{
Al Viro0df6a632010-12-21 13:29:29 -05001418 static const struct dentry_operations cgroup_dops = {
1419 .d_iput = cgroup_diput,
Al Virob26d4cd2013-10-25 18:47:37 -04001420 .d_delete = always_delete_dentry,
Al Viro0df6a632010-12-21 13:29:29 -05001421 };
1422
Paul Menageddbcc7e2007-10-18 23:39:30 -07001423 struct inode *inode =
1424 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001425
1426 if (!inode)
1427 return -ENOMEM;
1428
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429 inode->i_fop = &simple_dir_operations;
1430 inode->i_op = &cgroup_dir_inode_operations;
1431 /* directories start off with i_nlink == 2 (for "." entry) */
1432 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001433 sb->s_root = d_make_root(inode);
1434 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001435 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001436 /* for everything else we want ->d_op set */
1437 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001438 return 0;
1439}
1440
Al Virof7e83572010-07-26 13:23:11 +04001441static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001443 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001444{
1445 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001446 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001447 int ret = 0;
1448 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001449 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001450 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001451 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001452 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001453
1454 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001455 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001456 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001457 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001458 if (ret)
1459 goto out_err;
1460
1461 /*
1462 * Allocate a new cgroup root. We may not need it if we're
1463 * reusing an existing hierarchy.
1464 */
1465 new_root = cgroup_root_from_opts(&opts);
1466 if (IS_ERR(new_root)) {
1467 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001468 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001469 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001470 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001471
Paul Menagec6d57f32009-09-23 15:56:19 -07001472 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001473 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001474 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001475 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001476 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001477 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478 }
1479
Paul Menagec6d57f32009-09-23 15:56:19 -07001480 root = sb->s_fs_info;
1481 BUG_ON(!root);
1482 if (root == opts.new_root) {
1483 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001484 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001485 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001486 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001487 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001488
1489 BUG_ON(sb->s_root != NULL);
1490
1491 ret = cgroup_get_rootdir(sb);
1492 if (ret)
1493 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001494 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001495
Paul Menage817929e2007-10-18 23:39:36 -07001496 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001497 mutex_lock(&cgroup_mutex);
1498
Tejun Heoeb46bf82014-02-08 10:26:33 -05001499 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1500 if (ret < 0)
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001501 goto unlock_drop;
Tejun Heoeb46bf82014-02-08 10:26:33 -05001502 root_cgrp->id = ret;
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001503
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001504 /* Check for name clashes with existing mounts */
1505 ret = -EBUSY;
1506 if (strlen(root->name))
1507 for_each_active_root(existing_root)
1508 if (!strcmp(existing_root->name, root->name))
1509 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001510
Paul Menage817929e2007-10-18 23:39:36 -07001511 /*
1512 * We're accessing css_set_count without locking
1513 * css_set_lock here, but that's OK - it can only be
1514 * increased by someone holding cgroup_lock, and
1515 * that's us. The worst that can happen is that we
1516 * have some link structures left over
1517 */
Tejun Heo69d02062013-06-12 21:04:50 -07001518 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001519 if (ret)
1520 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001521
Tejun Heofc76df72013-06-25 11:53:37 -07001522 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1523 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001524 if (ret)
1525 goto unlock_drop;
1526
Tejun Heo31261212013-06-28 17:07:30 -07001527 sb->s_root->d_fsdata = root_cgrp;
1528 root_cgrp->dentry = sb->s_root;
1529
1530 /*
1531 * We're inside get_sb() and will call lookup_one_len() to
1532 * create the root files, which doesn't work if SELinux is
1533 * in use. The following cred dancing somehow works around
1534 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1535 * populating new cgroupfs mount") for more details.
1536 */
1537 cred = override_creds(&init_cred);
1538
Tejun Heo2bb566c2013-08-08 20:11:23 -04001539 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001540 if (ret)
1541 goto rm_base_files;
1542
Tejun Heoa8a648c2013-06-24 15:21:47 -07001543 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001544 if (ret)
1545 goto rm_base_files;
1546
1547 revert_creds(cred);
1548
Ben Blumcf5d5942010-03-10 15:22:09 -08001549 /*
1550 * There must be no failure case after here, since rebinding
1551 * takes care of subsystems' refcounts, which are explicitly
1552 * dropped in the failure exit path.
1553 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554
Tejun Heo9871bf92013-06-24 15:21:47 -07001555 list_add(&root->root_list, &cgroup_roots);
1556 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557
Paul Menage817929e2007-10-18 23:39:36 -07001558 /* Link the top cgroup in this hierarchy into all
1559 * the css_set objects */
1560 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001561 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001562 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001563 write_unlock(&css_set_lock);
1564
Tejun Heo69d02062013-06-12 21:04:50 -07001565 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001566
Li Zefanc12f65d2009-01-07 18:07:42 -08001567 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568 BUG_ON(root->number_of_cgroups != 1);
1569
Paul Menageddbcc7e2007-10-18 23:39:30 -07001570 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001571 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001572 } else {
1573 /*
1574 * We re-used an existing hierarchy - the new root (if
1575 * any) is not needed
1576 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001577 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001578
Tejun Heoc7ba8282013-06-29 14:06:10 -07001579 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001580 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1581 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1582 ret = -EINVAL;
1583 goto drop_new_super;
1584 } else {
1585 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1586 }
Tejun Heo873fe092013-04-14 20:15:26 -07001587 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588 }
1589
Paul Menagec6d57f32009-09-23 15:56:19 -07001590 kfree(opts.release_agent);
1591 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001592 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001593
Tejun Heo31261212013-06-28 17:07:30 -07001594 rm_base_files:
1595 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001596 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001597 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001598 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001599 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001600 mutex_unlock(&cgroup_mutex);
1601 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001602 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001603 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001604 out_err:
1605 kfree(opts.release_agent);
1606 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001607 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001608}
1609
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001610static void cgroup_kill_sb(struct super_block *sb)
1611{
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001613 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001614 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001615 int ret;
1616
1617 BUG_ON(!root);
1618
1619 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001620 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621
Tejun Heo31261212013-06-28 17:07:30 -07001622 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623 mutex_lock(&cgroup_mutex);
1624
1625 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001626 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1627 ret = rebind_subsystems(root, 0, root->subsys_mask);
1628 /* Shouldn't be able to fail ... */
1629 BUG_ON(ret);
1630 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631
Paul Menage817929e2007-10-18 23:39:36 -07001632 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001633 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001634 * root cgroup
1635 */
1636 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001637
Tejun Heo69d02062013-06-12 21:04:50 -07001638 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1639 list_del(&link->cset_link);
1640 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001641 kfree(link);
1642 }
1643 write_unlock(&css_set_lock);
1644
Paul Menage839ec542009-01-29 14:25:22 -08001645 if (!list_empty(&root->root_list)) {
1646 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001647 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001648 }
Li Zefane5f6a862009-01-07 18:07:41 -08001649
Tejun Heofa3ca072013-04-14 11:36:56 -07001650 cgroup_exit_root_id(root);
1651
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001653 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001655 simple_xattrs_free(&cgrp->xattrs);
1656
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001658 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659}
1660
1661static struct file_system_type cgroup_fs_type = {
1662 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001663 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664 .kill_sb = cgroup_kill_sb,
1665};
1666
Greg KH676db4a2010-08-05 13:53:35 -07001667static struct kobject *cgroup_kobj;
1668
Li Zefana043e3b2008-02-23 15:24:09 -08001669/**
1670 * cgroup_path - generate the path of a cgroup
1671 * @cgrp: the cgroup in question
1672 * @buf: the buffer to write the path into
1673 * @buflen: the length of the buffer
1674 *
Li Zefan65dff752013-03-01 15:01:56 +08001675 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1676 *
1677 * We can't generate cgroup path using dentry->d_name, as accessing
1678 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1679 * inode's i_mutex, while on the other hand cgroup_path() can be called
1680 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001681 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001682int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683{
Li Zefan65dff752013-03-01 15:01:56 +08001684 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001685 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001686
Tejun Heoda1f2962013-04-14 10:32:19 -07001687 if (!cgrp->parent) {
1688 if (strlcpy(buf, "/", buflen) >= buflen)
1689 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001690 return 0;
1691 }
1692
Tao Ma316eb662012-11-08 21:36:38 +08001693 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001694 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001695
Li Zefan65dff752013-03-01 15:01:56 +08001696 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001697 do {
Li Zefan65dff752013-03-01 15:01:56 +08001698 const char *name = cgroup_name(cgrp);
1699 int len;
1700
1701 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001702 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001703 goto out;
1704 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001705
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001707 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001709
1710 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001711 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001712 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001713 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001714out:
1715 rcu_read_unlock();
1716 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001717}
Ben Blum67523c42010-03-10 15:22:11 -08001718EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719
Tejun Heo857a2be2013-04-14 20:50:08 -07001720/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001721 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001722 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001723 * @buf: the buffer to write the path into
1724 * @buflen: the length of the buffer
1725 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001726 * Determine @task's cgroup on the first (the one with the lowest non-zero
1727 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1728 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1729 * cgroup controller callbacks.
1730 *
1731 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001732 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001733int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001734{
1735 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001736 struct cgroup *cgrp;
1737 int hierarchy_id = 1, ret = 0;
1738
1739 if (buflen < 2)
1740 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001741
1742 mutex_lock(&cgroup_mutex);
1743
Tejun Heo913ffdb2013-07-11 16:34:48 -07001744 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1745
Tejun Heo857a2be2013-04-14 20:50:08 -07001746 if (root) {
1747 cgrp = task_cgroup_from_root(task, root);
1748 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001749 } else {
1750 /* if no hierarchy exists, everyone is in "/" */
1751 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001752 }
1753
1754 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001755 return ret;
1756}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001757EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001758
Ben Blum74a11662011-05-26 16:25:20 -07001759/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001760 * Control Group taskset
1761 */
Tejun Heo134d3372011-12-12 18:12:21 -08001762struct task_and_cgroup {
1763 struct task_struct *task;
1764 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001765 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001766};
1767
Tejun Heo2f7ee562011-12-12 18:12:21 -08001768struct cgroup_taskset {
1769 struct task_and_cgroup single;
1770 struct flex_array *tc_array;
1771 int tc_array_len;
1772 int idx;
1773 struct cgroup *cur_cgrp;
1774};
1775
1776/**
1777 * cgroup_taskset_first - reset taskset and return the first task
1778 * @tset: taskset of interest
1779 *
1780 * @tset iteration is initialized and the first task is returned.
1781 */
1782struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1783{
1784 if (tset->tc_array) {
1785 tset->idx = 0;
1786 return cgroup_taskset_next(tset);
1787 } else {
1788 tset->cur_cgrp = tset->single.cgrp;
1789 return tset->single.task;
1790 }
1791}
1792EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1793
1794/**
1795 * cgroup_taskset_next - iterate to the next task in taskset
1796 * @tset: taskset of interest
1797 *
1798 * Return the next task in @tset. Iteration must have been initialized
1799 * with cgroup_taskset_first().
1800 */
1801struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1802{
1803 struct task_and_cgroup *tc;
1804
1805 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1806 return NULL;
1807
1808 tc = flex_array_get(tset->tc_array, tset->idx++);
1809 tset->cur_cgrp = tc->cgrp;
1810 return tc->task;
1811}
1812EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1813
1814/**
Tejun Heod99c8722013-08-08 20:11:27 -04001815 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001816 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001817 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001818 *
Tejun Heod99c8722013-08-08 20:11:27 -04001819 * Return the css for the current (last returned) task of @tset for
1820 * subsystem specified by @subsys_id. This function must be preceded by
1821 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001822 */
Tejun Heod99c8722013-08-08 20:11:27 -04001823struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1824 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001825{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001826 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001827}
Tejun Heod99c8722013-08-08 20:11:27 -04001828EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001829
1830/**
1831 * cgroup_taskset_size - return the number of tasks in taskset
1832 * @tset: taskset of interest
1833 */
1834int cgroup_taskset_size(struct cgroup_taskset *tset)
1835{
1836 return tset->tc_array ? tset->tc_array_len : 1;
1837}
1838EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1839
1840
Ben Blum74a11662011-05-26 16:25:20 -07001841/*
1842 * cgroup_task_migrate - move a task from one cgroup to another.
1843 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001844 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001845 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001846static void cgroup_task_migrate(struct cgroup *old_cgrp,
1847 struct task_struct *tsk,
1848 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001849{
Tejun Heo5abb8852013-06-12 21:04:49 -07001850 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001851
1852 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001853 * We are synchronized through threadgroup_lock() against PF_EXITING
1854 * setting such that we can't race against cgroup_exit() changing the
1855 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001856 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001857 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001858 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001859
Ben Blum74a11662011-05-26 16:25:20 -07001860 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001861 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001862 task_unlock(tsk);
1863
1864 /* Update the css_set linked lists if we're using them */
1865 write_lock(&css_set_lock);
1866 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001867 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001868 write_unlock(&css_set_lock);
1869
1870 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001871 * We just gained a reference on old_cset by taking it from the
1872 * task. As trading it for new_cset is protected by cgroup_mutex,
1873 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001874 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001875 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1876 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001877}
1878
Li Zefana043e3b2008-02-23 15:24:09 -08001879/**
Li Zefan081aa452013-03-13 09:17:09 +08001880 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001881 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001882 * @tsk: the task or the leader of the threadgroup to be attached
1883 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001884 *
Tejun Heo257058a2011-12-12 18:12:21 -08001885 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001886 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001887 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001888static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1889 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001890{
1891 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001892 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001893 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001894 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001895 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001896 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001897 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001898 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001899
1900 /*
1901 * step 0: in order to do expensive, possibly blocking operations for
1902 * every thread, we cannot iterate the thread group list, since it needs
1903 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001904 * group - group_rwsem prevents new threads from appearing, and if
1905 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001906 */
Li Zefan081aa452013-03-13 09:17:09 +08001907 if (threadgroup)
1908 group_size = get_nr_threads(tsk);
1909 else
1910 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001911 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001912 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001913 if (!group)
1914 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001915 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001916 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001917 if (retval)
1918 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001919
Ben Blum74a11662011-05-26 16:25:20 -07001920 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001921 /*
1922 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1923 * already PF_EXITING could be freed from underneath us unless we
1924 * take an rcu_read_lock.
1925 */
1926 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001927 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001928 struct task_and_cgroup ent;
1929
Tejun Heocd3d0952011-12-12 18:12:21 -08001930 /* @tsk either already exited or can't exit until the end */
1931 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001932 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001933
Ben Blum74a11662011-05-26 16:25:20 -07001934 /* as per above, nr_threads may decrease, but not increase. */
1935 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001936 ent.task = tsk;
1937 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001938 /* nothing to do if this task is already in the cgroup */
1939 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001940 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001941 /*
1942 * saying GFP_ATOMIC has no effect here because we did prealloc
1943 * earlier, but it's good form to communicate our expectations.
1944 */
Tejun Heo134d3372011-12-12 18:12:21 -08001945 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07001946 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07001947 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08001948 next:
Li Zefan081aa452013-03-13 09:17:09 +08001949 if (!threadgroup)
1950 break;
Ben Blum74a11662011-05-26 16:25:20 -07001951 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001952 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07001953 /* remember the number of threads in the array for later. */
1954 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001955 tset.tc_array = group;
1956 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001957
Tejun Heo134d3372011-12-12 18:12:21 -08001958 /* methods shouldn't be called if no task is actually migrating */
1959 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001960 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08001961 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08001962
Ben Blum74a11662011-05-26 16:25:20 -07001963 /*
1964 * step 1: check that we can legitimately attach to the cgroup.
1965 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001966 for_each_css(css, i, cgrp) {
1967 if (css->ss->can_attach) {
1968 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001969 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001970 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07001971 goto out_cancel_attach;
1972 }
1973 }
Ben Blum74a11662011-05-26 16:25:20 -07001974 }
1975
1976 /*
1977 * step 2: make sure css_sets exist for all threads to be migrated.
1978 * we use find_css_set, which allocates a new one if necessary.
1979 */
Ben Blum74a11662011-05-26 16:25:20 -07001980 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07001981 struct css_set *old_cset;
1982
Tejun Heo134d3372011-12-12 18:12:21 -08001983 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001984 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001985 tc->cset = find_css_set(old_cset, cgrp);
1986 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001987 retval = -ENOMEM;
1988 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07001989 }
1990 }
1991
1992 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001993 * step 3: now that we're guaranteed success wrt the css_sets,
1994 * proceed to move all tasks to the new cgroup. There are no
1995 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07001996 */
Ben Blum74a11662011-05-26 16:25:20 -07001997 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08001998 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001999 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002000 }
2001 /* nothing is sensitive to fork() after this point. */
2002
2003 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002004 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002005 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05002006 for_each_css(css, i, cgrp)
2007 if (css->ss->attach)
2008 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002009
2010 /*
2011 * step 5: success! and cleanup
2012 */
Ben Blum74a11662011-05-26 16:25:20 -07002013 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002014out_put_css_set_refs:
2015 if (retval) {
2016 for (i = 0; i < group_size; i++) {
2017 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002018 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002019 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002020 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002021 }
Ben Blum74a11662011-05-26 16:25:20 -07002022 }
2023out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002024 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002025 for_each_css(css, i, cgrp) {
2026 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07002027 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05002028 if (css->ss->cancel_attach)
2029 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002030 }
2031 }
Ben Blum74a11662011-05-26 16:25:20 -07002032out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002033 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002034 return retval;
2035}
2036
2037/*
2038 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002039 * function to attach either it or all tasks in its threadgroup. Will lock
2040 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002041 */
2042static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002043{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002044 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002045 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002046 int ret;
2047
Ben Blum74a11662011-05-26 16:25:20 -07002048 if (!cgroup_lock_live_group(cgrp))
2049 return -ENODEV;
2050
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002051retry_find_task:
2052 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002053 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002054 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002055 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002056 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002057 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002058 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002059 }
Ben Blum74a11662011-05-26 16:25:20 -07002060 /*
2061 * even if we're attaching all tasks in the thread group, we
2062 * only need to check permissions on one of them.
2063 */
David Howellsc69e8d92008-11-14 10:39:19 +11002064 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002065 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2066 !uid_eq(cred->euid, tcred->uid) &&
2067 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002068 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002069 ret = -EACCES;
2070 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002071 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002072 } else
2073 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002074
2075 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002076 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002077
2078 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002079 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002080 * trapped in a cpuset, or RT worker may be born in a cgroup
2081 * with no rt_runtime allocated. Just say no.
2082 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002083 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002084 ret = -EINVAL;
2085 rcu_read_unlock();
2086 goto out_unlock_cgroup;
2087 }
2088
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002089 get_task_struct(tsk);
2090 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002091
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002092 threadgroup_lock(tsk);
2093 if (threadgroup) {
2094 if (!thread_group_leader(tsk)) {
2095 /*
2096 * a race with de_thread from another thread's exec()
2097 * may strip us of our leadership, if this happens,
2098 * there is no choice but to throw this task away and
2099 * try again; this is
2100 * "double-double-toil-and-trouble-check locking".
2101 */
2102 threadgroup_unlock(tsk);
2103 put_task_struct(tsk);
2104 goto retry_find_task;
2105 }
Li Zefan081aa452013-03-13 09:17:09 +08002106 }
2107
2108 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2109
Tejun Heocd3d0952011-12-12 18:12:21 -08002110 threadgroup_unlock(tsk);
2111
Paul Menagebbcb81d2007-10-18 23:39:32 -07002112 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002113out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002114 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002115 return ret;
2116}
2117
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002118/**
2119 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2120 * @from: attach to all cgroups of a given task
2121 * @tsk: the task to be attached
2122 */
2123int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2124{
2125 struct cgroupfs_root *root;
2126 int retval = 0;
2127
Tejun Heo47cfcd02013-04-07 09:29:51 -07002128 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002129 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002130 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002131
Li Zefan6f4b7e62013-07-31 16:18:36 +08002132 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002133 if (retval)
2134 break;
2135 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002136 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002137
2138 return retval;
2139}
2140EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2141
Tejun Heo182446d2013-08-08 20:11:24 -04002142static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2143 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002144{
Tejun Heo182446d2013-08-08 20:11:24 -04002145 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002146}
2147
Tejun Heo182446d2013-08-08 20:11:24 -04002148static int cgroup_procs_write(struct cgroup_subsys_state *css,
2149 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002150{
Tejun Heo182446d2013-08-08 20:11:24 -04002151 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002152}
2153
Tejun Heo182446d2013-08-08 20:11:24 -04002154static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2155 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002156{
Tejun Heo182446d2013-08-08 20:11:24 -04002157 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002158 if (strlen(buffer) >= PATH_MAX)
2159 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002160 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002161 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002162 spin_lock(&release_agent_path_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04002163 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heo69e943b2014-02-08 10:36:58 -05002164 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002165 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002166 return 0;
2167}
2168
Tejun Heo2da8ca82013-12-05 12:28:04 -05002169static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002170{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002171 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002172
Paul Menagee788e062008-07-25 01:46:59 -07002173 if (!cgroup_lock_live_group(cgrp))
2174 return -ENODEV;
2175 seq_puts(seq, cgrp->root->release_agent_path);
2176 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002177 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002178 return 0;
2179}
2180
Tejun Heo2da8ca82013-12-05 12:28:04 -05002181static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002182{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002183 struct cgroup *cgrp = seq_css(seq)->cgroup;
2184
2185 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002186 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002187}
2188
Paul Menage84eea842008-07-25 01:47:00 -07002189/* A buffer size big enough for numbers or short strings */
2190#define CGROUP_LOCAL_BUFFER_SIZE 64
2191
Tejun Heoa742c592013-12-05 12:28:03 -05002192static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
Tejun Heo182446d2013-08-08 20:11:24 -04002193 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002194{
Tejun Heo182446d2013-08-08 20:11:24 -04002195 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002196 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002197 struct cgroup_subsys_state *css = cfe->css;
Tejun Heoa742c592013-12-05 12:28:03 -05002198 size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1;
2199 char *buf;
2200 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002201
Tejun Heoa742c592013-12-05 12:28:03 -05002202 if (nbytes >= max_bytes)
2203 return -E2BIG;
2204
2205 buf = kmalloc(nbytes + 1, GFP_KERNEL);
2206 if (!buf)
2207 return -ENOMEM;
2208
2209 if (copy_from_user(buf, userbuf, nbytes)) {
2210 ret = -EFAULT;
2211 goto out_free;
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002212 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002213
Tejun Heoa742c592013-12-05 12:28:03 -05002214 buf[nbytes] = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07002215
Tejun Heoa742c592013-12-05 12:28:03 -05002216 if (cft->write_string) {
2217 ret = cft->write_string(css, cft, strstrip(buf));
2218 } else if (cft->write_u64) {
2219 unsigned long long v;
2220 ret = kstrtoull(buf, 0, &v);
2221 if (!ret)
2222 ret = cft->write_u64(css, cft, v);
2223 } else if (cft->write_s64) {
2224 long long v;
2225 ret = kstrtoll(buf, 0, &v);
2226 if (!ret)
2227 ret = cft->write_s64(css, cft, v);
2228 } else if (cft->trigger) {
2229 ret = cft->trigger(css, (unsigned int)cft->private);
2230 } else {
2231 ret = -EINVAL;
2232 }
2233out_free:
2234 kfree(buf);
2235 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002236}
2237
Paul Menage91796562008-04-29 01:00:01 -07002238/*
2239 * seqfile ops/methods for returning structured data. Currently just
2240 * supports string->u64 maps, but can be extended in future.
2241 */
2242
Tejun Heo6612f052013-12-05 12:28:04 -05002243static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002244{
Tejun Heo6612f052013-12-05 12:28:04 -05002245 struct cftype *cft = seq_cft(seq);
2246
2247 if (cft->seq_start) {
2248 return cft->seq_start(seq, ppos);
2249 } else {
2250 /*
2251 * The same behavior and code as single_open(). Returns
2252 * !NULL if pos is at the beginning; otherwise, NULL.
2253 */
2254 return NULL + !*ppos;
2255 }
2256}
2257
2258static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2259{
2260 struct cftype *cft = seq_cft(seq);
2261
2262 if (cft->seq_next) {
2263 return cft->seq_next(seq, v, ppos);
2264 } else {
2265 /*
2266 * The same behavior and code as single_open(), always
2267 * terminate after the initial read.
2268 */
2269 ++*ppos;
2270 return NULL;
2271 }
2272}
2273
2274static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2275{
2276 struct cftype *cft = seq_cft(seq);
2277
2278 if (cft->seq_stop)
2279 cft->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002280}
2281
2282static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2283{
Tejun Heo7da11272013-12-05 12:28:04 -05002284 struct cftype *cft = seq_cft(m);
2285 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002286
Tejun Heo2da8ca82013-12-05 12:28:04 -05002287 if (cft->seq_show)
2288 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002289
Tejun Heo896f5192013-12-05 12:28:04 -05002290 if (cft->read_u64)
2291 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2292 else if (cft->read_s64)
2293 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2294 else
2295 return -EINVAL;
2296 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002297}
2298
Tejun Heo6612f052013-12-05 12:28:04 -05002299static struct seq_operations cgroup_seq_operations = {
2300 .start = cgroup_seqfile_start,
2301 .next = cgroup_seqfile_next,
2302 .stop = cgroup_seqfile_stop,
2303 .show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002304};
2305
Paul Menageddbcc7e2007-10-18 23:39:30 -07002306static int cgroup_file_open(struct inode *inode, struct file *file)
2307{
Tejun Heof7d58812013-08-08 20:11:23 -04002308 struct cfent *cfe = __d_cfe(file->f_dentry);
2309 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002310 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2311 struct cgroup_subsys_state *css;
Tejun Heo6612f052013-12-05 12:28:04 -05002312 struct cgroup_open_file *of;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002313 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002314
2315 err = generic_file_open(inode, file);
2316 if (err)
2317 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002318
2319 /*
2320 * If the file belongs to a subsystem, pin the css. Will be
2321 * unpinned either on open failure or release. This ensures that
2322 * @css stays alive for all file operations.
2323 */
Tejun Heo105347b2013-08-13 11:01:55 -04002324 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002325 css = cgroup_css(cgrp, cft->ss);
2326 if (cft->ss && !css_tryget(css))
2327 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002328 rcu_read_unlock();
2329
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002330 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002331 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002332
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002333 /*
2334 * @cfe->css is used by read/write/close to determine the
2335 * associated css. @file->private_data would be a better place but
2336 * that's already used by seqfile. Multiple accessors may use it
2337 * simultaneously which is okay as the association never changes.
2338 */
2339 WARN_ON_ONCE(cfe->css && cfe->css != css);
2340 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002341
Tejun Heo6612f052013-12-05 12:28:04 -05002342 of = __seq_open_private(file, &cgroup_seq_operations,
2343 sizeof(struct cgroup_open_file));
2344 if (of) {
2345 of->cfe = cfe;
2346 return 0;
Li Zefane0798ce2013-07-31 17:36:25 +08002347 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002348
Tejun Heo6612f052013-12-05 12:28:04 -05002349 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002350 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002351 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002352}
2353
2354static int cgroup_file_release(struct inode *inode, struct file *file)
2355{
Tejun Heof7d58812013-08-08 20:11:23 -04002356 struct cfent *cfe = __d_cfe(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002357 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002358
Tejun Heo67f4c362013-08-08 20:11:24 -04002359 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002360 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002361 return seq_release_private(inode, file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002362}
2363
2364/*
2365 * cgroup_rename - Only allow simple rename of directories in place.
2366 */
2367static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2368 struct inode *new_dir, struct dentry *new_dentry)
2369{
Li Zefan65dff752013-03-01 15:01:56 +08002370 int ret;
2371 struct cgroup_name *name, *old_name;
2372 struct cgroup *cgrp;
2373
2374 /*
2375 * It's convinient to use parent dir's i_mutex to protected
2376 * cgrp->name.
2377 */
2378 lockdep_assert_held(&old_dir->i_mutex);
2379
Paul Menageddbcc7e2007-10-18 23:39:30 -07002380 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2381 return -ENOTDIR;
2382 if (new_dentry->d_inode)
2383 return -EEXIST;
2384 if (old_dir != new_dir)
2385 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002386
2387 cgrp = __d_cgrp(old_dentry);
2388
Tejun Heo6db8e852013-06-14 11:18:22 -07002389 /*
2390 * This isn't a proper migration and its usefulness is very
2391 * limited. Disallow if sane_behavior.
2392 */
2393 if (cgroup_sane_behavior(cgrp))
2394 return -EPERM;
2395
Li Zefan65dff752013-03-01 15:01:56 +08002396 name = cgroup_alloc_name(new_dentry);
2397 if (!name)
2398 return -ENOMEM;
2399
2400 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2401 if (ret) {
2402 kfree(name);
2403 return ret;
2404 }
2405
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002406 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002407 rcu_assign_pointer(cgrp->name, name);
2408
2409 kfree_rcu(old_name, rcu_head);
2410 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002411}
2412
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002413static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2414{
2415 if (S_ISDIR(dentry->d_inode->i_mode))
2416 return &__d_cgrp(dentry)->xattrs;
2417 else
Li Zefan712317a2013-04-18 23:09:52 -07002418 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002419}
2420
2421static inline int xattr_enabled(struct dentry *dentry)
2422{
2423 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002424 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002425}
2426
2427static bool is_valid_xattr(const char *name)
2428{
2429 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2430 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2431 return true;
2432 return false;
2433}
2434
2435static int cgroup_setxattr(struct dentry *dentry, const char *name,
2436 const void *val, size_t size, int flags)
2437{
2438 if (!xattr_enabled(dentry))
2439 return -EOPNOTSUPP;
2440 if (!is_valid_xattr(name))
2441 return -EINVAL;
2442 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2443}
2444
2445static int cgroup_removexattr(struct dentry *dentry, const char *name)
2446{
2447 if (!xattr_enabled(dentry))
2448 return -EOPNOTSUPP;
2449 if (!is_valid_xattr(name))
2450 return -EINVAL;
2451 return simple_xattr_remove(__d_xattrs(dentry), name);
2452}
2453
2454static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2455 void *buf, size_t size)
2456{
2457 if (!xattr_enabled(dentry))
2458 return -EOPNOTSUPP;
2459 if (!is_valid_xattr(name))
2460 return -EINVAL;
2461 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2462}
2463
2464static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2465{
2466 if (!xattr_enabled(dentry))
2467 return -EOPNOTSUPP;
2468 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2469}
2470
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002471static const struct file_operations cgroup_file_operations = {
Tejun Heo896f5192013-12-05 12:28:04 -05002472 .read = seq_read,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002473 .write = cgroup_file_write,
2474 .llseek = generic_file_llseek,
2475 .open = cgroup_file_open,
2476 .release = cgroup_file_release,
2477};
2478
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002479static const struct inode_operations cgroup_file_inode_operations = {
2480 .setxattr = cgroup_setxattr,
2481 .getxattr = cgroup_getxattr,
2482 .listxattr = cgroup_listxattr,
2483 .removexattr = cgroup_removexattr,
2484};
2485
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002486static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002487 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002488 .mkdir = cgroup_mkdir,
2489 .rmdir = cgroup_rmdir,
2490 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002491 .setxattr = cgroup_setxattr,
2492 .getxattr = cgroup_getxattr,
2493 .listxattr = cgroup_listxattr,
2494 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002495};
2496
Al Viroa5e7ed32011-07-26 01:55:55 -04002497static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002498 struct super_block *sb)
2499{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002500 struct inode *inode;
2501
2502 if (!dentry)
2503 return -ENOENT;
2504 if (dentry->d_inode)
2505 return -EEXIST;
2506
2507 inode = cgroup_new_inode(mode, sb);
2508 if (!inode)
2509 return -ENOMEM;
2510
2511 if (S_ISDIR(mode)) {
2512 inode->i_op = &cgroup_dir_inode_operations;
2513 inode->i_fop = &simple_dir_operations;
2514
2515 /* start off with i_nlink == 2 (for "." entry) */
2516 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002517 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002518
Tejun Heob8a2df62012-11-19 08:13:37 -08002519 /*
2520 * Control reaches here with cgroup_mutex held.
2521 * @inode->i_mutex should nest outside cgroup_mutex but we
2522 * want to populate it immediately without releasing
2523 * cgroup_mutex. As @inode isn't visible to anyone else
2524 * yet, trylock will always succeed without affecting
2525 * lockdep checks.
2526 */
2527 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002528 } else if (S_ISREG(mode)) {
2529 inode->i_size = 0;
2530 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002531 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002532 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002533 d_instantiate(dentry, inode);
2534 dget(dentry); /* Extra count - pin the dentry in core */
2535 return 0;
2536}
2537
Li Zefan099fca32009-04-02 16:57:29 -07002538/**
2539 * cgroup_file_mode - deduce file mode of a control file
2540 * @cft: the control file in question
2541 *
2542 * returns cft->mode if ->mode is not 0
2543 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2544 * returns S_IRUGO if it has only a read handler
2545 * returns S_IWUSR if it has only a write hander
2546 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002547static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002548{
Al Viroa5e7ed32011-07-26 01:55:55 -04002549 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002550
2551 if (cft->mode)
2552 return cft->mode;
2553
Tejun Heo2da8ca82013-12-05 12:28:04 -05002554 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
Li Zefan099fca32009-04-02 16:57:29 -07002555 mode |= S_IRUGO;
2556
Tejun Heo6e0755b2013-12-05 12:28:03 -05002557 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
2558 cft->trigger)
Li Zefan099fca32009-04-02 16:57:29 -07002559 mode |= S_IWUSR;
2560
2561 return mode;
2562}
2563
Tejun Heo2bb566c2013-08-08 20:11:23 -04002564static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002565{
Paul Menagebd89aab2007-10-18 23:40:44 -07002566 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002567 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002568 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002569 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002570 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002571 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002572 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002573
Tejun Heo9fa4db32013-08-26 18:40:56 -04002574 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2575 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002576 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002577 strcat(name, ".");
2578 }
2579 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002580
Paul Menageddbcc7e2007-10-18 23:39:30 -07002581 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002582
2583 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2584 if (!cfe)
2585 return -ENOMEM;
2586
Paul Menageddbcc7e2007-10-18 23:39:30 -07002587 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002588 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002589 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002590 goto out;
2591 }
2592
Li Zefand6cbf352013-05-14 19:44:20 +08002593 cfe->type = (void *)cft;
2594 cfe->dentry = dentry;
2595 dentry->d_fsdata = cfe;
2596 simple_xattrs_init(&cfe->xattrs);
2597
Tejun Heo05ef1d72012-04-01 12:09:56 -07002598 mode = cgroup_file_mode(cft);
2599 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2600 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002601 list_add_tail(&cfe->node, &parent->files);
2602 cfe = NULL;
2603 }
2604 dput(dentry);
2605out:
2606 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002607 return error;
2608}
2609
Tejun Heob1f28d32013-06-28 16:24:10 -07002610/**
2611 * cgroup_addrm_files - add or remove files to a cgroup directory
2612 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002613 * @cfts: array of cftypes to be added
2614 * @is_add: whether to add or remove
2615 *
2616 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002617 * For removals, this function never fails. If addition fails, this
2618 * function doesn't remove files already added. The caller is responsible
2619 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002620 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002621static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2622 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002623{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002624 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002625 int ret;
2626
2627 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2628 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002629
2630 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002631 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002632 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2633 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002634 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2635 continue;
2636 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2637 continue;
2638
Li Zefan2739d3c2013-01-21 18:18:33 +08002639 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002640 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002641 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002642 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002643 cft->name, ret);
2644 return ret;
2645 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002646 } else {
2647 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002648 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002649 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002650 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002651}
2652
Tejun Heo8e3f6542012-04-01 12:09:55 -07002653static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002654 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002655{
2656 /*
2657 * Thanks to the entanglement with vfs inode locking, we can't walk
2658 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002659 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2660 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002661 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002662 mutex_lock(&cgroup_mutex);
2663}
2664
Tejun Heo2bb566c2013-08-08 20:11:23 -04002665static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002666 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002667{
2668 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002669 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002670 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002671 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002672 struct dentry *prev = NULL;
2673 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002674 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002675 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002676 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002677
2678 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002679 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002680 !atomic_inc_not_zero(&sb->s_active)) {
2681 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002682 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002683 }
2684
Li Zefane8c82d22013-06-18 18:48:37 +08002685 /*
2686 * All cgroups which are created after we drop cgroup_mutex will
2687 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002688 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002689 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002690 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002691
Li Zefane8c82d22013-06-18 18:48:37 +08002692 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002693 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002694 struct cgroup *cgrp = css->cgroup;
2695
Li Zefane8c82d22013-06-18 18:48:37 +08002696 if (cgroup_is_dead(cgrp))
2697 continue;
2698
2699 inode = cgrp->dentry->d_inode;
2700 dget(cgrp->dentry);
Li Zefane8c82d22013-06-18 18:48:37 +08002701 dput(prev);
2702 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002703
Tejun Heo48573a82014-02-08 10:26:34 -05002704 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002705 mutex_lock(&inode->i_mutex);
2706 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002707 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002708 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002709 mutex_unlock(&inode->i_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002710 if (ret)
2711 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002712 }
Tejun Heo48573a82014-02-08 10:26:34 -05002713 mutex_unlock(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08002714 dput(prev);
2715 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002716 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002717}
2718
2719/**
2720 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2721 * @ss: target cgroup subsystem
2722 * @cfts: zero-length name terminated array of cftypes
2723 *
2724 * Register @cfts to @ss. Files described by @cfts are created for all
2725 * existing cgroups to which @ss is attached and all future cgroups will
2726 * have them too. This function can be called anytime whether @ss is
2727 * attached or not.
2728 *
2729 * Returns 0 on successful registration, -errno on failure. Note that this
2730 * function currently returns 0 as long as @cfts registration is successful
2731 * even if some file creation attempts on existing cgroups fail.
2732 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002733int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002734{
2735 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002736 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002737 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002738
2739 set = kzalloc(sizeof(*set), GFP_KERNEL);
2740 if (!set)
2741 return -ENOMEM;
2742
Tejun Heo2bb566c2013-08-08 20:11:23 -04002743 for (cft = cfts; cft->name[0] != '\0'; cft++)
2744 cft->ss = ss;
2745
Tejun Heo8e3f6542012-04-01 12:09:55 -07002746 cgroup_cfts_prepare();
2747 set->cfts = cfts;
2748 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002749 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002750 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002751 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002752 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002753}
2754EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2755
Li Zefana043e3b2008-02-23 15:24:09 -08002756/**
Tejun Heo79578622012-04-01 12:09:56 -07002757 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002758 * @cfts: zero-length name terminated array of cftypes
2759 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002760 * Unregister @cfts. Files described by @cfts are removed from all
2761 * existing cgroups and all future cgroups won't have them either. This
2762 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002763 *
2764 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002765 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002766 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002767int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002768{
2769 struct cftype_set *set;
2770
Tejun Heo2bb566c2013-08-08 20:11:23 -04002771 if (!cfts || !cfts[0].ss)
2772 return -ENOENT;
2773
Tejun Heo79578622012-04-01 12:09:56 -07002774 cgroup_cfts_prepare();
2775
Tejun Heo2bb566c2013-08-08 20:11:23 -04002776 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002777 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002778 list_del(&set->node);
2779 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002780 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002781 return 0;
2782 }
2783 }
2784
Tejun Heo2bb566c2013-08-08 20:11:23 -04002785 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002786 return -ENOENT;
2787}
2788
2789/**
Li Zefana043e3b2008-02-23 15:24:09 -08002790 * cgroup_task_count - count the number of tasks in a cgroup.
2791 * @cgrp: the cgroup in question
2792 *
2793 * Return the number of tasks in the cgroup.
2794 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002795int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002796{
2797 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002798 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002799
Paul Menage817929e2007-10-18 23:39:36 -07002800 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002801 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2802 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002803 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002804 return count;
2805}
2806
2807/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002808 * To reduce the fork() overhead for systems that are not actually using
2809 * their cgroups capability, we don't maintain the lists running through
2810 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002811 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002812 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002813static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002814{
2815 struct task_struct *p, *g;
2816 write_lock(&css_set_lock);
2817 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002818 /*
2819 * We need tasklist_lock because RCU is not safe against
2820 * while_each_thread(). Besides, a forking task that has passed
2821 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2822 * is not guaranteed to have its child immediately visible in the
2823 * tasklist if we walk through it with RCU.
2824 */
2825 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002826 do_each_thread(g, p) {
2827 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002828 /*
2829 * We should check if the process is exiting, otherwise
2830 * it will race with cgroup_exit() in that the list
2831 * entry won't be deleted though the process has exited.
2832 */
2833 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002834 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002835 task_unlock(p);
2836 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002837 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002838 write_unlock(&css_set_lock);
2839}
2840
Tejun Heo574bd9f2012-11-09 09:12:29 -08002841/**
Tejun Heo492eb212013-08-08 20:11:25 -04002842 * css_next_child - find the next child of a given css
2843 * @pos_css: the current position (%NULL to initiate traversal)
2844 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002845 *
Tejun Heo492eb212013-08-08 20:11:25 -04002846 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002847 * under either cgroup_mutex or RCU read lock. The only requirement is
2848 * that @parent_css and @pos_css are accessible. The next sibling is
2849 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002850 */
Tejun Heo492eb212013-08-08 20:11:25 -04002851struct cgroup_subsys_state *
2852css_next_child(struct cgroup_subsys_state *pos_css,
2853 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002854{
Tejun Heo492eb212013-08-08 20:11:25 -04002855 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2856 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002857 struct cgroup *next;
2858
Tejun Heo87fb54f2013-12-06 15:11:55 -05002859 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002860
2861 /*
2862 * @pos could already have been removed. Once a cgroup is removed,
2863 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002864 * changes. As CGRP_DEAD assertion is serialized and happens
2865 * before the cgroup is taken off the ->sibling list, if we see it
2866 * unasserted, it's guaranteed that the next sibling hasn't
2867 * finished its grace period even if it's already removed, and thus
2868 * safe to dereference from this RCU critical section. If
2869 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2870 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002871 *
2872 * If @pos is dead, its next pointer can't be dereferenced;
2873 * however, as each cgroup is given a monotonically increasing
2874 * unique serial number and always appended to the sibling list,
2875 * the next one can be found by walking the parent's children until
2876 * we see a cgroup with higher serial number than @pos's. While
2877 * this path can be slower, it's taken only when either the current
2878 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002879 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002880 if (!pos) {
2881 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2882 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002883 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002884 } else {
2885 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2886 if (next->serial_nr > pos->serial_nr)
2887 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002888 }
2889
Tejun Heo492eb212013-08-08 20:11:25 -04002890 if (&next->sibling == &cgrp->children)
2891 return NULL;
2892
Tejun Heoca8bdca2013-08-26 18:40:56 -04002893 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002894}
Tejun Heo492eb212013-08-08 20:11:25 -04002895EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002896
2897/**
Tejun Heo492eb212013-08-08 20:11:25 -04002898 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002899 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002900 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002901 *
Tejun Heo492eb212013-08-08 20:11:25 -04002902 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002903 * to visit for pre-order traversal of @root's descendants. @root is
2904 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002905 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002906 * While this function requires cgroup_mutex or RCU read locking, it
2907 * doesn't require the whole traversal to be contained in a single critical
2908 * section. This function will return the correct next descendant as long
2909 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002910 */
Tejun Heo492eb212013-08-08 20:11:25 -04002911struct cgroup_subsys_state *
2912css_next_descendant_pre(struct cgroup_subsys_state *pos,
2913 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002914{
Tejun Heo492eb212013-08-08 20:11:25 -04002915 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002916
Tejun Heo87fb54f2013-12-06 15:11:55 -05002917 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002918
Tejun Heobd8815a2013-08-08 20:11:27 -04002919 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002920 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002921 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002922
2923 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002924 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002925 if (next)
2926 return next;
2927
2928 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002929 while (pos != root) {
2930 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002931 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002932 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002933 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002934 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002935
2936 return NULL;
2937}
Tejun Heo492eb212013-08-08 20:11:25 -04002938EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002939
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002940/**
Tejun Heo492eb212013-08-08 20:11:25 -04002941 * css_rightmost_descendant - return the rightmost descendant of a css
2942 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002943 *
Tejun Heo492eb212013-08-08 20:11:25 -04002944 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2945 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002946 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002947 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002948 * While this function requires cgroup_mutex or RCU read locking, it
2949 * doesn't require the whole traversal to be contained in a single critical
2950 * section. This function will return the correct rightmost descendant as
2951 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002952 */
Tejun Heo492eb212013-08-08 20:11:25 -04002953struct cgroup_subsys_state *
2954css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002955{
Tejun Heo492eb212013-08-08 20:11:25 -04002956 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002957
Tejun Heo87fb54f2013-12-06 15:11:55 -05002958 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002959
2960 do {
2961 last = pos;
2962 /* ->prev isn't RCU safe, walk ->next till the end */
2963 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002964 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002965 pos = tmp;
2966 } while (pos);
2967
2968 return last;
2969}
Tejun Heo492eb212013-08-08 20:11:25 -04002970EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002971
Tejun Heo492eb212013-08-08 20:11:25 -04002972static struct cgroup_subsys_state *
2973css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002974{
Tejun Heo492eb212013-08-08 20:11:25 -04002975 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002976
2977 do {
2978 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04002979 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002980 } while (pos);
2981
2982 return last;
2983}
2984
2985/**
Tejun Heo492eb212013-08-08 20:11:25 -04002986 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002987 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002988 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002989 *
Tejun Heo492eb212013-08-08 20:11:25 -04002990 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002991 * to visit for post-order traversal of @root's descendants. @root is
2992 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002993 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002994 * While this function requires cgroup_mutex or RCU read locking, it
2995 * doesn't require the whole traversal to be contained in a single critical
2996 * section. This function will return the correct next descendant as long
2997 * as both @pos and @cgroup are accessible and @pos is a descendant of
2998 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002999 */
Tejun Heo492eb212013-08-08 20:11:25 -04003000struct cgroup_subsys_state *
3001css_next_descendant_post(struct cgroup_subsys_state *pos,
3002 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003003{
Tejun Heo492eb212013-08-08 20:11:25 -04003004 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003005
Tejun Heo87fb54f2013-12-06 15:11:55 -05003006 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003007
Tejun Heo58b79a92013-09-06 15:31:08 -04003008 /* if first iteration, visit leftmost descendant which may be @root */
3009 if (!pos)
3010 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003011
Tejun Heobd8815a2013-08-08 20:11:27 -04003012 /* if we visited @root, we're done */
3013 if (pos == root)
3014 return NULL;
3015
Tejun Heo574bd9f2012-11-09 09:12:29 -08003016 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003017 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003018 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003019 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003020
3021 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003022 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003023}
Tejun Heo492eb212013-08-08 20:11:25 -04003024EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003025
Tejun Heo0942eee2013-08-08 20:11:26 -04003026/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003027 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003028 * @it: the iterator to advance
3029 *
3030 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003031 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003032static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003033{
3034 struct list_head *l = it->cset_link;
3035 struct cgrp_cset_link *link;
3036 struct css_set *cset;
3037
3038 /* Advance to the next non-empty css_set */
3039 do {
3040 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003041 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003042 it->cset_link = NULL;
3043 return;
3044 }
3045 link = list_entry(l, struct cgrp_cset_link, cset_link);
3046 cset = link->cset;
3047 } while (list_empty(&cset->tasks));
3048 it->cset_link = l;
3049 it->task = cset->tasks.next;
3050}
3051
Tejun Heo0942eee2013-08-08 20:11:26 -04003052/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003053 * css_task_iter_start - initiate task iteration
3054 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003055 * @it: the task iterator to use
3056 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003057 * Initiate iteration through the tasks of @css. The caller can call
3058 * css_task_iter_next() to walk through the tasks until the function
3059 * returns NULL. On completion of iteration, css_task_iter_end() must be
3060 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003061 *
3062 * Note that this function acquires a lock which is released when the
3063 * iteration finishes. The caller can't sleep while iteration is in
3064 * progress.
3065 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003066void css_task_iter_start(struct cgroup_subsys_state *css,
3067 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003068 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003069{
3070 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003071 * The first time anyone tries to iterate across a css, we need to
3072 * enable the list linking each css_set to its tasks, and fix up
3073 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003074 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003075 if (!use_task_css_set_links)
3076 cgroup_enable_task_cg_lists();
3077
Paul Menage817929e2007-10-18 23:39:36 -07003078 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003079
Tejun Heo72ec7022013-08-08 20:11:26 -04003080 it->origin_css = css;
3081 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003082
Tejun Heo72ec7022013-08-08 20:11:26 -04003083 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003084}
3085
Tejun Heo0942eee2013-08-08 20:11:26 -04003086/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003087 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003088 * @it: the task iterator being iterated
3089 *
3090 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003091 * initialized via css_task_iter_start(). Returns NULL when the iteration
3092 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003093 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003094struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003095{
3096 struct task_struct *res;
3097 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003098 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003099
3100 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003101 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003102 return NULL;
3103 res = list_entry(l, struct task_struct, cg_list);
3104 /* Advance iterator to find next entry */
3105 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003106 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3107 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003108 /*
3109 * We reached the end of this task list - move on to the
3110 * next cgrp_cset_link.
3111 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003112 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003113 } else {
3114 it->task = l;
3115 }
3116 return res;
3117}
3118
Tejun Heo0942eee2013-08-08 20:11:26 -04003119/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003120 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003121 * @it: the task iterator to finish
3122 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003123 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003124 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003125void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003126 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003127{
3128 read_unlock(&css_set_lock);
3129}
3130
Cliff Wickman31a7df02008-02-07 00:14:42 -08003131static inline int started_after_time(struct task_struct *t1,
3132 struct timespec *time,
3133 struct task_struct *t2)
3134{
3135 int start_diff = timespec_compare(&t1->start_time, time);
3136 if (start_diff > 0) {
3137 return 1;
3138 } else if (start_diff < 0) {
3139 return 0;
3140 } else {
3141 /*
3142 * Arbitrarily, if two processes started at the same
3143 * time, we'll say that the lower pointer value
3144 * started first. Note that t2 may have exited by now
3145 * so this may not be a valid pointer any longer, but
3146 * that's fine - it still serves to distinguish
3147 * between two tasks started (effectively) simultaneously.
3148 */
3149 return t1 > t2;
3150 }
3151}
3152
3153/*
3154 * This function is a callback from heap_insert() and is used to order
3155 * the heap.
3156 * In this case we order the heap in descending task start time.
3157 */
3158static inline int started_after(void *p1, void *p2)
3159{
3160 struct task_struct *t1 = p1;
3161 struct task_struct *t2 = p2;
3162 return started_after_time(t1, &t2->start_time, t2);
3163}
3164
3165/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003166 * css_scan_tasks - iterate though all the tasks in a css
3167 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003168 * @test: optional test callback
3169 * @process: process callback
3170 * @data: data passed to @test and @process
3171 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003172 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003173 * Iterate through all the tasks in @css, calling @test for each, and if it
3174 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003175 *
Tejun Heoe5358372013-08-08 20:11:26 -04003176 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003177 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003178 * lock css_set_lock for the call to @process.
3179 *
3180 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003181 * of @css for the duration of this call. This function may or may not
3182 * call @process for tasks that exit or move to a different css during the
3183 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003184 *
3185 * Note that @test may be called with locks held, and may in some
3186 * situations be called multiple times for the same task, so it should be
3187 * cheap.
3188 *
3189 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3190 * heap operations (and its "gt" member will be overwritten), else a
3191 * temporary heap will be used (allocation of which may cause this function
3192 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003193 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003194int css_scan_tasks(struct cgroup_subsys_state *css,
3195 bool (*test)(struct task_struct *, void *),
3196 void (*process)(struct task_struct *, void *),
3197 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003198{
3199 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003200 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003201 struct task_struct *p, *dropped;
3202 /* Never dereference latest_task, since it's not refcounted */
3203 struct task_struct *latest_task = NULL;
3204 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003205 struct timespec latest_time = { 0, 0 };
3206
Tejun Heoe5358372013-08-08 20:11:26 -04003207 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003208 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003209 heap->gt = &started_after;
3210 } else {
3211 /* We need to allocate our own heap memory */
3212 heap = &tmp_heap;
3213 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3214 if (retval)
3215 /* cannot allocate the heap */
3216 return retval;
3217 }
3218
3219 again:
3220 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003221 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003222 * which are of interest, and invoking @process callback on the
3223 * ones which need an update. Since we don't want to hold any
3224 * locks during the task updates, gather tasks to be processed in a
3225 * heap structure. The heap is sorted by descending task start
3226 * time. If the statically-sized heap fills up, we overflow tasks
3227 * that started later, and in future iterations only consider tasks
3228 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003229 * guarantees forward progress and that we don't miss any tasks.
3230 */
3231 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003232 css_task_iter_start(css, &it);
3233 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003234 /*
3235 * Only affect tasks that qualify per the caller's callback,
3236 * if he provided one
3237 */
Tejun Heoe5358372013-08-08 20:11:26 -04003238 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003239 continue;
3240 /*
3241 * Only process tasks that started after the last task
3242 * we processed
3243 */
3244 if (!started_after_time(p, &latest_time, latest_task))
3245 continue;
3246 dropped = heap_insert(heap, p);
3247 if (dropped == NULL) {
3248 /*
3249 * The new task was inserted; the heap wasn't
3250 * previously full
3251 */
3252 get_task_struct(p);
3253 } else if (dropped != p) {
3254 /*
3255 * The new task was inserted, and pushed out a
3256 * different task
3257 */
3258 get_task_struct(p);
3259 put_task_struct(dropped);
3260 }
3261 /*
3262 * Else the new task was newer than anything already in
3263 * the heap and wasn't inserted
3264 */
3265 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003266 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003267
3268 if (heap->size) {
3269 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003270 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003271 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003272 latest_time = q->start_time;
3273 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003274 }
3275 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003276 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003277 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003278 }
3279 /*
3280 * If we had to process any tasks at all, scan again
3281 * in case some of them were in the middle of forking
3282 * children that didn't get processed.
3283 * Not the most efficient way to do it, but it avoids
3284 * having to take callback_mutex in the fork path
3285 */
3286 goto again;
3287 }
3288 if (heap == &tmp_heap)
3289 heap_free(&tmp_heap);
3290 return 0;
3291}
3292
Tejun Heoe5358372013-08-08 20:11:26 -04003293static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003294{
Tejun Heoe5358372013-08-08 20:11:26 -04003295 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003296
Tejun Heo47cfcd02013-04-07 09:29:51 -07003297 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003298 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003299 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003300}
3301
3302/**
3303 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3304 * @to: cgroup to which the tasks will be moved
3305 * @from: cgroup in which the tasks currently reside
3306 */
3307int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3308{
Tejun Heo72ec7022013-08-08 20:11:26 -04003309 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3310 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003311}
3312
Paul Menage817929e2007-10-18 23:39:36 -07003313/*
Ben Blum102a7752009-09-23 15:56:26 -07003314 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003315 *
3316 * Reading this file can return large amounts of data if a cgroup has
3317 * *lots* of attached tasks. So it may need several calls to read(),
3318 * but we cannot guarantee that the information we produce is correct
3319 * unless we produce it entirely atomically.
3320 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003321 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003322
Li Zefan24528252012-01-20 11:58:43 +08003323/* which pidlist file are we talking about? */
3324enum cgroup_filetype {
3325 CGROUP_FILE_PROCS,
3326 CGROUP_FILE_TASKS,
3327};
3328
3329/*
3330 * A pidlist is a list of pids that virtually represents the contents of one
3331 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3332 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3333 * to the cgroup.
3334 */
3335struct cgroup_pidlist {
3336 /*
3337 * used to find which pidlist is wanted. doesn't change as long as
3338 * this particular list stays in the list.
3339 */
3340 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3341 /* array of xids */
3342 pid_t *list;
3343 /* how many elements the above list has */
3344 int length;
Li Zefan24528252012-01-20 11:58:43 +08003345 /* each of these stored in a list by its cgroup */
3346 struct list_head links;
3347 /* pointer to the cgroup we belong to, for list removal purposes */
3348 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003349 /* for delayed destruction */
3350 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003351};
3352
Paul Menagebbcb81d2007-10-18 23:39:32 -07003353/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003354 * The following two functions "fix" the issue where there are more pids
3355 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3356 * TODO: replace with a kernel-wide solution to this problem
3357 */
3358#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3359static void *pidlist_allocate(int count)
3360{
3361 if (PIDLIST_TOO_LARGE(count))
3362 return vmalloc(count * sizeof(pid_t));
3363 else
3364 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3365}
Tejun Heob1a21362013-11-29 10:42:58 -05003366
Ben Blumd1d9fd32009-09-23 15:56:28 -07003367static void pidlist_free(void *p)
3368{
3369 if (is_vmalloc_addr(p))
3370 vfree(p);
3371 else
3372 kfree(p);
3373}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003374
3375/*
Tejun Heob1a21362013-11-29 10:42:58 -05003376 * Used to destroy all pidlists lingering waiting for destroy timer. None
3377 * should be left afterwards.
3378 */
3379static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3380{
3381 struct cgroup_pidlist *l, *tmp_l;
3382
3383 mutex_lock(&cgrp->pidlist_mutex);
3384 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3385 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3386 mutex_unlock(&cgrp->pidlist_mutex);
3387
3388 flush_workqueue(cgroup_pidlist_destroy_wq);
3389 BUG_ON(!list_empty(&cgrp->pidlists));
3390}
3391
3392static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3393{
3394 struct delayed_work *dwork = to_delayed_work(work);
3395 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3396 destroy_dwork);
3397 struct cgroup_pidlist *tofree = NULL;
3398
3399 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003400
3401 /*
Tejun Heo04502362013-11-29 10:42:59 -05003402 * Destroy iff we didn't get queued again. The state won't change
3403 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003404 */
Tejun Heo04502362013-11-29 10:42:59 -05003405 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003406 list_del(&l->links);
3407 pidlist_free(l->list);
3408 put_pid_ns(l->key.ns);
3409 tofree = l;
3410 }
3411
Tejun Heob1a21362013-11-29 10:42:58 -05003412 mutex_unlock(&l->owner->pidlist_mutex);
3413 kfree(tofree);
3414}
3415
3416/*
Ben Blum102a7752009-09-23 15:56:26 -07003417 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003418 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003419 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003420static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003421{
Ben Blum102a7752009-09-23 15:56:26 -07003422 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003423
3424 /*
3425 * we presume the 0th element is unique, so i starts at 1. trivial
3426 * edge cases first; no work needs to be done for either
3427 */
3428 if (length == 0 || length == 1)
3429 return length;
3430 /* src and dest walk down the list; dest counts unique elements */
3431 for (src = 1; src < length; src++) {
3432 /* find next unique element */
3433 while (list[src] == list[src-1]) {
3434 src++;
3435 if (src == length)
3436 goto after;
3437 }
3438 /* dest always points to where the next unique element goes */
3439 list[dest] = list[src];
3440 dest++;
3441 }
3442after:
Ben Blum102a7752009-09-23 15:56:26 -07003443 return dest;
3444}
3445
Tejun Heoafb2bc12013-11-29 10:42:59 -05003446/*
3447 * The two pid files - task and cgroup.procs - guaranteed that the result
3448 * is sorted, which forced this whole pidlist fiasco. As pid order is
3449 * different per namespace, each namespace needs differently sorted list,
3450 * making it impossible to use, for example, single rbtree of member tasks
3451 * sorted by task pointer. As pidlists can be fairly large, allocating one
3452 * per open file is dangerous, so cgroup had to implement shared pool of
3453 * pidlists keyed by cgroup and namespace.
3454 *
3455 * All this extra complexity was caused by the original implementation
3456 * committing to an entirely unnecessary property. In the long term, we
3457 * want to do away with it. Explicitly scramble sort order if
3458 * sane_behavior so that no such expectation exists in the new interface.
3459 *
3460 * Scrambling is done by swapping every two consecutive bits, which is
3461 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3462 */
3463static pid_t pid_fry(pid_t pid)
3464{
3465 unsigned a = pid & 0x55555555;
3466 unsigned b = pid & 0xAAAAAAAA;
3467
3468 return (a << 1) | (b >> 1);
3469}
3470
3471static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3472{
3473 if (cgroup_sane_behavior(cgrp))
3474 return pid_fry(pid);
3475 else
3476 return pid;
3477}
3478
Ben Blum102a7752009-09-23 15:56:26 -07003479static int cmppid(const void *a, const void *b)
3480{
3481 return *(pid_t *)a - *(pid_t *)b;
3482}
3483
Tejun Heoafb2bc12013-11-29 10:42:59 -05003484static int fried_cmppid(const void *a, const void *b)
3485{
3486 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3487}
3488
Ben Blum72a8cb32009-09-23 15:56:27 -07003489static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3490 enum cgroup_filetype type)
3491{
3492 struct cgroup_pidlist *l;
3493 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003494 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003495
Tejun Heoe6b81712013-11-29 10:42:59 -05003496 lockdep_assert_held(&cgrp->pidlist_mutex);
3497
3498 list_for_each_entry(l, &cgrp->pidlists, links)
3499 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003500 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003501 return NULL;
3502}
3503
Ben Blum72a8cb32009-09-23 15:56:27 -07003504/*
3505 * find the appropriate pidlist for our purpose (given procs vs tasks)
3506 * returns with the lock on that pidlist already held, and takes care
3507 * of the use count, or returns NULL with no locks held if we're out of
3508 * memory.
3509 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003510static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3511 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003512{
3513 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003514
Tejun Heoe6b81712013-11-29 10:42:59 -05003515 lockdep_assert_held(&cgrp->pidlist_mutex);
3516
3517 l = cgroup_pidlist_find(cgrp, type);
3518 if (l)
3519 return l;
3520
Ben Blum72a8cb32009-09-23 15:56:27 -07003521 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003522 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003523 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003524 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003525
Tejun Heob1a21362013-11-29 10:42:58 -05003526 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003527 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003528 /* don't need task_nsproxy() if we're looking at ourself */
3529 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003530 l->owner = cgrp;
3531 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003532 return l;
3533}
3534
3535/*
Ben Blum102a7752009-09-23 15:56:26 -07003536 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3537 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003538static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3539 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003540{
3541 pid_t *array;
3542 int length;
3543 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003544 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003545 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003546 struct cgroup_pidlist *l;
3547
Tejun Heo4bac00d2013-11-29 10:42:59 -05003548 lockdep_assert_held(&cgrp->pidlist_mutex);
3549
Ben Blum102a7752009-09-23 15:56:26 -07003550 /*
3551 * If cgroup gets more users after we read count, we won't have
3552 * enough space - tough. This race is indistinguishable to the
3553 * caller from the case that the additional cgroup users didn't
3554 * show up until sometime later on.
3555 */
3556 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003557 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003558 if (!array)
3559 return -ENOMEM;
3560 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003561 css_task_iter_start(&cgrp->dummy_css, &it);
3562 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003563 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003564 break;
Ben Blum102a7752009-09-23 15:56:26 -07003565 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003566 if (type == CGROUP_FILE_PROCS)
3567 pid = task_tgid_vnr(tsk);
3568 else
3569 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003570 if (pid > 0) /* make sure to only use valid results */
3571 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003572 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003573 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003574 length = n;
3575 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003576 if (cgroup_sane_behavior(cgrp))
3577 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3578 else
3579 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003580 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003581 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003582
Tejun Heoe6b81712013-11-29 10:42:59 -05003583 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003584 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003585 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003586 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003587 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003588 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003589
3590 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003591 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003592 l->list = array;
3593 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003594 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003595 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003596}
3597
Balbir Singh846c7bb2007-10-18 23:39:44 -07003598/**
Li Zefana043e3b2008-02-23 15:24:09 -08003599 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003600 * @stats: cgroupstats to fill information into
3601 * @dentry: A dentry entry belonging to the cgroup for which stats have
3602 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003603 *
3604 * Build and fill cgroupstats so that taskstats can export it to user
3605 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003606 */
3607int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3608{
3609 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003610 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003611 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003612 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003613
Balbir Singh846c7bb2007-10-18 23:39:44 -07003614 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003615 * Validate dentry by checking the superblock operations,
3616 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003617 */
Li Zefan33d283b2008-11-19 15:36:48 -08003618 if (dentry->d_sb->s_op != &cgroup_ops ||
3619 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003620 goto err;
3621
3622 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003623 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003624
Tejun Heo72ec7022013-08-08 20:11:26 -04003625 css_task_iter_start(&cgrp->dummy_css, &it);
3626 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003627 switch (tsk->state) {
3628 case TASK_RUNNING:
3629 stats->nr_running++;
3630 break;
3631 case TASK_INTERRUPTIBLE:
3632 stats->nr_sleeping++;
3633 break;
3634 case TASK_UNINTERRUPTIBLE:
3635 stats->nr_uninterruptible++;
3636 break;
3637 case TASK_STOPPED:
3638 stats->nr_stopped++;
3639 break;
3640 default:
3641 if (delayacct_is_task_waiting_on_io(tsk))
3642 stats->nr_io_wait++;
3643 break;
3644 }
3645 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003646 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003647
Balbir Singh846c7bb2007-10-18 23:39:44 -07003648err:
3649 return ret;
3650}
3651
Paul Menage8f3ff202009-09-23 15:56:25 -07003652
Paul Menagecc31edc2008-10-18 20:28:04 -07003653/*
Ben Blum102a7752009-09-23 15:56:26 -07003654 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003655 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003656 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003657 */
3658
Ben Blum102a7752009-09-23 15:56:26 -07003659static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003660{
3661 /*
3662 * Initially we receive a position value that corresponds to
3663 * one more than the last pid shown (or 0 on the first call or
3664 * after a seek to the start). Use a binary-search to find the
3665 * next pid to display, if any
3666 */
Tejun Heo5d224442013-12-05 12:28:04 -05003667 struct cgroup_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003668 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003669 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003670 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003671 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003672 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003673
Tejun Heo4bac00d2013-11-29 10:42:59 -05003674 mutex_lock(&cgrp->pidlist_mutex);
3675
3676 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003677 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003678 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003679 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003680 * could already have been destroyed.
3681 */
Tejun Heo5d224442013-12-05 12:28:04 -05003682 if (of->priv)
3683 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003684
3685 /*
3686 * Either this is the first start() after open or the matching
3687 * pidlist has been destroyed inbetween. Create a new one.
3688 */
Tejun Heo5d224442013-12-05 12:28:04 -05003689 if (!of->priv) {
3690 ret = pidlist_array_load(cgrp, type,
3691 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003692 if (ret)
3693 return ERR_PTR(ret);
3694 }
Tejun Heo5d224442013-12-05 12:28:04 -05003695 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003696
Paul Menagecc31edc2008-10-18 20:28:04 -07003697 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003698 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003699
Paul Menagecc31edc2008-10-18 20:28:04 -07003700 while (index < end) {
3701 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003702 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003703 index = mid;
3704 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003705 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003706 index = mid + 1;
3707 else
3708 end = mid;
3709 }
3710 }
3711 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003712 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003713 return NULL;
3714 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003715 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003716 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003717 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003718}
3719
Ben Blum102a7752009-09-23 15:56:26 -07003720static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003721{
Tejun Heo5d224442013-12-05 12:28:04 -05003722 struct cgroup_open_file *of = s->private;
3723 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003724
Tejun Heo5d224442013-12-05 12:28:04 -05003725 if (l)
3726 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003727 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003728 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003729}
3730
Ben Blum102a7752009-09-23 15:56:26 -07003731static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003732{
Tejun Heo5d224442013-12-05 12:28:04 -05003733 struct cgroup_open_file *of = s->private;
3734 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003735 pid_t *p = v;
3736 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003737 /*
3738 * Advance to the next pid in the array. If this goes off the
3739 * end, we're done
3740 */
3741 p++;
3742 if (p >= end) {
3743 return NULL;
3744 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003745 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003746 return p;
3747 }
3748}
3749
Ben Blum102a7752009-09-23 15:56:26 -07003750static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003751{
3752 return seq_printf(s, "%d\n", *(int *)v);
3753}
3754
Ben Blum102a7752009-09-23 15:56:26 -07003755/*
3756 * seq_operations functions for iterating on pidlists through seq_file -
3757 * independent of whether it's tasks or procs
3758 */
3759static const struct seq_operations cgroup_pidlist_seq_operations = {
3760 .start = cgroup_pidlist_start,
3761 .stop = cgroup_pidlist_stop,
3762 .next = cgroup_pidlist_next,
3763 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003764};
3765
Tejun Heo182446d2013-08-08 20:11:24 -04003766static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3767 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003768{
Tejun Heo182446d2013-08-08 20:11:24 -04003769 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003770}
3771
Tejun Heo182446d2013-08-08 20:11:24 -04003772static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3773 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003774{
Tejun Heo182446d2013-08-08 20:11:24 -04003775 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003776 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003777 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003778 else
Tejun Heo182446d2013-08-08 20:11:24 -04003779 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003780 return 0;
3781}
3782
Paul Menagebbcb81d2007-10-18 23:39:32 -07003783/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003784 * When dput() is called asynchronously, if umount has been done and
3785 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3786 * there's a small window that vfs will see the root dentry with non-zero
3787 * refcnt and trigger BUG().
3788 *
3789 * That's why we hold a reference before dput() and drop it right after.
3790 */
3791static void cgroup_dput(struct cgroup *cgrp)
3792{
3793 struct super_block *sb = cgrp->root->sb;
3794
3795 atomic_inc(&sb->s_active);
3796 dput(cgrp->dentry);
3797 deactivate_super(sb);
3798}
3799
Tejun Heo182446d2013-08-08 20:11:24 -04003800static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3801 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003802{
Tejun Heo182446d2013-08-08 20:11:24 -04003803 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003804}
3805
Tejun Heo182446d2013-08-08 20:11:24 -04003806static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3807 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003808{
3809 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003810 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003811 else
Tejun Heo182446d2013-08-08 20:11:24 -04003812 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003813 return 0;
3814}
3815
Tejun Heod5c56ce2013-06-03 19:14:34 -07003816static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003817 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003818 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003819 .seq_start = cgroup_pidlist_start,
3820 .seq_next = cgroup_pidlist_next,
3821 .seq_stop = cgroup_pidlist_stop,
3822 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003823 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003824 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003825 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003826 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003827 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003828 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003829 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003830 .read_u64 = cgroup_clone_children_read,
3831 .write_u64 = cgroup_clone_children_write,
3832 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003833 {
Tejun Heo873fe092013-04-14 20:15:26 -07003834 .name = "cgroup.sane_behavior",
3835 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003836 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003837 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003838
3839 /*
3840 * Historical crazy stuff. These don't have "cgroup." prefix and
3841 * don't exist if sane_behavior. If you're depending on these, be
3842 * prepared to be burned.
3843 */
3844 {
3845 .name = "tasks",
3846 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003847 .seq_start = cgroup_pidlist_start,
3848 .seq_next = cgroup_pidlist_next,
3849 .seq_stop = cgroup_pidlist_stop,
3850 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003851 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003852 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003853 .mode = S_IRUGO | S_IWUSR,
3854 },
3855 {
3856 .name = "notify_on_release",
3857 .flags = CFTYPE_INSANE,
3858 .read_u64 = cgroup_read_notify_on_release,
3859 .write_u64 = cgroup_write_notify_on_release,
3860 },
Tejun Heo873fe092013-04-14 20:15:26 -07003861 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003862 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003863 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003864 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003865 .write_string = cgroup_release_agent_write,
3866 .max_write_len = PATH_MAX,
3867 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003868 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003869};
3870
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003871/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003872 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003873 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003874 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003875 *
3876 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003877 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003878static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003879{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003880 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003881 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003882
Tejun Heo8e3f6542012-04-01 12:09:55 -07003883 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003884 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003885 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07003886
3887 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003888 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003889
Tejun Heobee55092013-06-28 16:24:11 -07003890 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003891 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003892 if (ret < 0)
3893 goto err;
3894 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003895 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003896 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003897err:
3898 cgroup_clear_dir(cgrp, subsys_mask);
3899 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003900}
3901
Tejun Heo0c21ead2013-08-13 20:22:51 -04003902/*
3903 * css destruction is four-stage process.
3904 *
3905 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3906 * Implemented in kill_css().
3907 *
3908 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3909 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3910 * by invoking offline_css(). After offlining, the base ref is put.
3911 * Implemented in css_killed_work_fn().
3912 *
3913 * 3. When the percpu_ref reaches zero, the only possible remaining
3914 * accessors are inside RCU read sections. css_release() schedules the
3915 * RCU callback.
3916 *
3917 * 4. After the grace period, the css can be freed. Implemented in
3918 * css_free_work_fn().
3919 *
3920 * It is actually hairier because both step 2 and 4 require process context
3921 * and thus involve punting to css->destroy_work adding two additional
3922 * steps to the already complex sequence.
3923 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003924static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003925{
3926 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003927 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003928 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003929
Tejun Heo0ae78e02013-08-13 11:01:54 -04003930 if (css->parent)
3931 css_put(css->parent);
3932
Tejun Heo0c21ead2013-08-13 20:22:51 -04003933 css->ss->css_free(css);
3934 cgroup_dput(cgrp);
3935}
3936
3937static void css_free_rcu_fn(struct rcu_head *rcu_head)
3938{
3939 struct cgroup_subsys_state *css =
3940 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3941
3942 /*
3943 * css holds an extra ref to @cgrp->dentry which is put on the last
3944 * css_put(). dput() requires process context which we don't have.
3945 */
3946 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003947 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003948}
3949
Tejun Heod3daf282013-06-13 19:39:16 -07003950static void css_release(struct percpu_ref *ref)
3951{
3952 struct cgroup_subsys_state *css =
3953 container_of(ref, struct cgroup_subsys_state, refcnt);
3954
Tejun Heoaec25022014-02-08 10:36:58 -05003955 rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003956 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003957}
3958
Tejun Heo623f9262013-08-13 11:01:55 -04003959static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3960 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003961{
Paul Menagebd89aab2007-10-18 23:40:44 -07003962 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003963 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003964 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003965
Tejun Heo0ae78e02013-08-13 11:01:54 -04003966 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003967 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003968 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003969 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003970
Tejun Heoca8bdca2013-08-26 18:40:56 -04003971 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003972}
3973
Li Zefan2a4ac632013-07-31 16:16:40 +08003974/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04003975static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003976{
Tejun Heo623f9262013-08-13 11:01:55 -04003977 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08003978 int ret = 0;
3979
Tejun Heoa31f2d32012-11-19 08:13:37 -08003980 lockdep_assert_held(&cgroup_mutex);
3981
Tejun Heo92fb9742012-11-19 08:13:38 -08003982 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04003983 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003984 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04003985 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04003986 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05003987 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003988 }
Tejun Heob1929db2012-11-19 08:13:38 -08003989 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003990}
3991
Li Zefan2a4ac632013-07-31 16:16:40 +08003992/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04003993static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003994{
Tejun Heo623f9262013-08-13 11:01:55 -04003995 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003996
3997 lockdep_assert_held(&cgroup_mutex);
3998
3999 if (!(css->flags & CSS_ONLINE))
4000 return;
4001
Li Zefand7eeac12013-03-12 15:35:59 -07004002 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004003 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004004
Tejun Heoeb954192013-08-08 20:11:23 -04004005 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004006 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05004007 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004008}
4009
Tejun Heoc81c925a2013-12-06 15:11:56 -05004010/**
4011 * create_css - create a cgroup_subsys_state
4012 * @cgrp: the cgroup new css will be associated with
4013 * @ss: the subsys of new css
4014 *
4015 * Create a new css associated with @cgrp - @ss pair. On success, the new
4016 * css is online and installed in @cgrp with all interface files created.
4017 * Returns 0 on success, -errno on failure.
4018 */
4019static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4020{
4021 struct cgroup *parent = cgrp->parent;
4022 struct cgroup_subsys_state *css;
4023 int err;
4024
4025 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
4026 lockdep_assert_held(&cgroup_mutex);
4027
4028 css = ss->css_alloc(cgroup_css(parent, ss));
4029 if (IS_ERR(css))
4030 return PTR_ERR(css);
4031
4032 err = percpu_ref_init(&css->refcnt, css_release);
4033 if (err)
4034 goto err_free;
4035
4036 init_css(css, ss, cgrp);
4037
Tejun Heoaec25022014-02-08 10:36:58 -05004038 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004039 if (err)
4040 goto err_free;
4041
4042 err = online_css(css);
4043 if (err)
4044 goto err_free;
4045
4046 dget(cgrp->dentry);
4047 css_get(css->parent);
4048
4049 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4050 parent->parent) {
4051 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",
4052 current->comm, current->pid, ss->name);
4053 if (!strcmp(ss->name, "memory"))
4054 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4055 ss->warned_broken_hierarchy = true;
4056 }
4057
4058 return 0;
4059
4060err_free:
4061 percpu_ref_cancel_init(&css->refcnt);
4062 ss->css_free(css);
4063 return err;
4064}
4065
Paul Menageddbcc7e2007-10-18 23:39:30 -07004066/*
Li Zefana043e3b2008-02-23 15:24:09 -08004067 * cgroup_create - create a cgroup
4068 * @parent: cgroup that will be parent of the new cgroup
4069 * @dentry: dentry of the new cgroup
4070 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004071 *
Li Zefana043e3b2008-02-23 15:24:09 -08004072 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004073 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004074static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004075 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004076{
Paul Menagebd89aab2007-10-18 23:40:44 -07004077 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004078 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004079 struct cgroupfs_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05004080 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004081 struct cgroup_subsys *ss;
4082 struct super_block *sb = root->sb;
4083
Tejun Heo0a950f62012-11-19 09:02:12 -08004084 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004085 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4086 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004087 return -ENOMEM;
4088
Li Zefan65dff752013-03-01 15:01:56 +08004089 name = cgroup_alloc_name(dentry);
Tejun Heob58c8992014-02-08 10:26:33 -05004090 if (!name) {
4091 err = -ENOMEM;
Li Zefan65dff752013-03-01 15:01:56 +08004092 goto err_free_cgrp;
Tejun Heob58c8992014-02-08 10:26:33 -05004093 }
Li Zefan65dff752013-03-01 15:01:56 +08004094 rcu_assign_pointer(cgrp->name, name);
4095
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004096 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08004097 * Only live parents can have children. Note that the liveliness
4098 * check isn't strictly necessary because cgroup_mkdir() and
4099 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4100 * anyway so that locking is contained inside cgroup proper and we
4101 * don't get nasty surprises if we ever grow another caller.
4102 */
4103 if (!cgroup_lock_live_group(parent)) {
4104 err = -ENODEV;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004105 goto err_free_name;
4106 }
4107
4108 /*
4109 * Temporarily set the pointer to NULL, so idr_find() won't return
4110 * a half-baked cgroup.
4111 */
4112 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
4113 if (cgrp->id < 0) {
4114 err = -ENOMEM;
4115 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08004116 }
4117
Paul Menageddbcc7e2007-10-18 23:39:30 -07004118 /* Grab a reference on the superblock so the hierarchy doesn't
4119 * get deleted on unmount if there are child cgroups. This
4120 * can be done outside cgroup_mutex, since the sb can't
4121 * disappear while someone has an open control file on the
4122 * fs */
4123 atomic_inc(&sb->s_active);
4124
Paul Menagecc31edc2008-10-18 20:28:04 -07004125 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004126
Li Zefanfe1c06c2013-01-24 14:30:22 +08004127 dentry->d_fsdata = cgrp;
4128 cgrp->dentry = dentry;
4129
Paul Menagebd89aab2007-10-18 23:40:44 -07004130 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004131 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004132 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004133
Li Zefanb6abdb02008-03-04 14:28:19 -08004134 if (notify_on_release(parent))
4135 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4136
Tejun Heo2260e7f2012-11-19 08:13:38 -08004137 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4138 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004139
Tejun Heo4e139af2012-11-19 08:13:36 -08004140 /*
4141 * Create directory. cgroup_create_file() returns with the new
4142 * directory locked on success so that it can be populated without
4143 * dropping cgroup_mutex.
4144 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004145 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004146 if (err < 0)
Li Zefan0ab02ca2014-02-11 16:05:46 +08004147 goto err_free_id;
Tejun Heo4e139af2012-11-19 08:13:36 -08004148 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004149
Tejun Heo00356bd2013-06-18 11:14:22 -07004150 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004151
Tejun Heo4e139af2012-11-19 08:13:36 -08004152 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004153 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4154 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004155
Li Zefan415cf072013-04-08 14:35:02 +08004156 /* hold a ref to the parent's dentry */
4157 dget(parent->dentry);
4158
Tejun Heo0d802552013-12-06 15:11:56 -05004159 /*
4160 * @cgrp is now fully operational. If something fails after this
4161 * point, it'll be released via the normal destruction path.
4162 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004163 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4164
Tejun Heo2bb566c2013-08-08 20:11:23 -04004165 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004166 if (err)
4167 goto err_destroy;
4168
Tejun Heo9d403e92013-12-06 15:11:56 -05004169 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004170 for_each_subsys(ss, ssid) {
4171 if (root->subsys_mask & (1 << ssid)) {
4172 err = create_css(cgrp, ss);
4173 if (err)
4174 goto err_destroy;
4175 }
Tejun Heoa8638032012-11-09 09:12:29 -08004176 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004177
4178 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004179 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004180
4181 return 0;
4182
Tejun Heo0a950f62012-11-19 09:02:12 -08004183err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004184 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004185 /* Release the reference count that we took on the superblock */
4186 deactivate_super(sb);
4187err_unlock:
4188 mutex_unlock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08004189err_free_name:
4190 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004191err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004192 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004193 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004194
4195err_destroy:
4196 cgroup_destroy_locked(cgrp);
4197 mutex_unlock(&cgroup_mutex);
4198 mutex_unlock(&dentry->d_inode->i_mutex);
4199 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004200}
4201
Al Viro18bb1db2011-07-26 01:41:39 -04004202static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004203{
4204 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4205
4206 /* the vfs holds inode->i_mutex already */
4207 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4208}
4209
Tejun Heo223dbc32013-08-13 20:22:50 -04004210/*
4211 * This is called when the refcnt of a css is confirmed to be killed.
4212 * css_tryget() is now guaranteed to fail.
4213 */
4214static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004215{
Tejun Heo223dbc32013-08-13 20:22:50 -04004216 struct cgroup_subsys_state *css =
4217 container_of(work, struct cgroup_subsys_state, destroy_work);
4218 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004219
Tejun Heof20104d2013-08-13 20:22:50 -04004220 mutex_lock(&cgroup_mutex);
4221
4222 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004223 * css_tryget() is guaranteed to fail now. Tell subsystems to
4224 * initate destruction.
4225 */
4226 offline_css(css);
4227
4228 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004229 * If @cgrp is marked dead, it's waiting for refs of all css's to
4230 * be disabled before proceeding to the second phase of cgroup
4231 * destruction. If we are the last one, kick it off.
4232 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004233 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004234 cgroup_destroy_css_killed(cgrp);
4235
4236 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004237
4238 /*
4239 * Put the css refs from kill_css(). Each css holds an extra
4240 * reference to the cgroup's dentry and cgroup removal proceeds
4241 * regardless of css refs. On the last put of each css, whenever
4242 * that may be, the extra dentry ref is put so that dentry
4243 * destruction happens only after all css's are released.
4244 */
4245 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004246}
4247
Tejun Heo223dbc32013-08-13 20:22:50 -04004248/* css kill confirmation processing requires process context, bounce */
4249static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004250{
4251 struct cgroup_subsys_state *css =
4252 container_of(ref, struct cgroup_subsys_state, refcnt);
4253
Tejun Heo223dbc32013-08-13 20:22:50 -04004254 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004255 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004256}
4257
4258/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004259 * kill_css - destroy a css
4260 * @css: css to destroy
4261 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004262 * This function initiates destruction of @css by removing cgroup interface
4263 * files and putting its base reference. ->css_offline() will be invoked
4264 * asynchronously once css_tryget() is guaranteed to fail and when the
4265 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004266 */
4267static void kill_css(struct cgroup_subsys_state *css)
4268{
Tejun Heoaec25022014-02-08 10:36:58 -05004269 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004270
Tejun Heoedae0c32013-08-13 20:22:51 -04004271 /*
4272 * Killing would put the base ref, but we need to keep it alive
4273 * until after ->css_offline().
4274 */
4275 css_get(css);
4276
4277 /*
4278 * cgroup core guarantees that, by the time ->css_offline() is
4279 * invoked, no new css reference will be given out via
4280 * css_tryget(). We can't simply call percpu_ref_kill() and
4281 * proceed to offlining css's because percpu_ref_kill() doesn't
4282 * guarantee that the ref is seen as killed on all CPUs on return.
4283 *
4284 * Use percpu_ref_kill_and_confirm() to get notifications as each
4285 * css is confirmed to be seen as killed on all CPUs.
4286 */
4287 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004288}
4289
4290/**
4291 * cgroup_destroy_locked - the first stage of cgroup destruction
4292 * @cgrp: cgroup to be destroyed
4293 *
4294 * css's make use of percpu refcnts whose killing latency shouldn't be
4295 * exposed to userland and are RCU protected. Also, cgroup core needs to
4296 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4297 * invoked. To satisfy all the requirements, destruction is implemented in
4298 * the following two steps.
4299 *
4300 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4301 * userland visible parts and start killing the percpu refcnts of
4302 * css's. Set up so that the next stage will be kicked off once all
4303 * the percpu refcnts are confirmed to be killed.
4304 *
4305 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4306 * rest of destruction. Once all cgroup references are gone, the
4307 * cgroup is RCU-freed.
4308 *
4309 * This function implements s1. After this step, @cgrp is gone as far as
4310 * the userland is concerned and a new cgroup with the same name may be
4311 * created. As cgroup doesn't care about the names internally, this
4312 * doesn't cause any problem.
4313 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004314static int cgroup_destroy_locked(struct cgroup *cgrp)
4315 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004316{
Tejun Heo42809dd2012-11-19 08:13:37 -08004317 struct dentry *d = cgrp->dentry;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004318 struct cgroup_subsys_state *css;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004319 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004320 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004321 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004322
Tejun Heo42809dd2012-11-19 08:13:37 -08004323 lockdep_assert_held(&d->d_inode->i_mutex);
4324 lockdep_assert_held(&cgroup_mutex);
4325
Tejun Heoddd69142013-06-12 21:04:54 -07004326 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004327 * css_set_lock synchronizes access to ->cset_links and prevents
4328 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004329 */
4330 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004331 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004332 read_unlock(&css_set_lock);
4333 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004334 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004335
Tejun Heo1a90dd52012-11-05 09:16:59 -08004336 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004337 * Make sure there's no live children. We can't test ->children
4338 * emptiness as dead children linger on it while being destroyed;
4339 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4340 */
4341 empty = true;
4342 rcu_read_lock();
4343 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4344 empty = cgroup_is_dead(child);
4345 if (!empty)
4346 break;
4347 }
4348 rcu_read_unlock();
4349 if (!empty)
4350 return -EBUSY;
4351
4352 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004353 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4354 * will be invoked to perform the rest of destruction once the
4355 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004356 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05004357 for_each_css(css, ssid, cgrp)
4358 kill_css(css);
Tejun Heo455050d2013-06-13 19:27:41 -07004359
4360 /*
4361 * Mark @cgrp dead. This prevents further task migration and child
4362 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004363 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004364 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004365 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004366 */
Tejun Heo54766d42013-06-12 21:04:53 -07004367 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004368
Tejun Heo455050d2013-06-13 19:27:41 -07004369 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4370 raw_spin_lock(&release_list_lock);
4371 if (!list_empty(&cgrp->release_list))
4372 list_del_init(&cgrp->release_list);
4373 raw_spin_unlock(&release_list_lock);
4374
4375 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004376 * If @cgrp has css's attached, the second stage of cgroup
4377 * destruction is kicked off from css_killed_work_fn() after the
4378 * refs of all attached css's are killed. If @cgrp doesn't have
4379 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004380 */
Tejun Heof20104d2013-08-13 20:22:50 -04004381 if (!cgrp->nr_css)
4382 cgroup_destroy_css_killed(cgrp);
4383
4384 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004385 * Clear the base files and remove @cgrp directory. The removal
4386 * puts the base ref but we aren't quite done with @cgrp yet, so
4387 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004388 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004389 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004390 dget(d);
4391 cgroup_d_remove_dir(d);
4392
Tejun Heoea15f8c2013-06-13 19:27:42 -07004393 return 0;
4394};
4395
Tejun Heod3daf282013-06-13 19:39:16 -07004396/**
Tejun Heof20104d2013-08-13 20:22:50 -04004397 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004398 * @work: cgroup->destroy_free_work
4399 *
4400 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004401 * destroyed after all css's are offlined and performs the rest of
4402 * destruction. This is the second step of destruction described in the
4403 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004404 */
Tejun Heof20104d2013-08-13 20:22:50 -04004405static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004406{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004407 struct cgroup *parent = cgrp->parent;
4408 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004409
Tejun Heof20104d2013-08-13 20:22:50 -04004410 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004411
Paul Menage999cd8a2009-01-07 18:08:36 -08004412 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004413 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004414
Paul Menageddbcc7e2007-10-18 23:39:30 -07004415 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004416
Paul Menagebd89aab2007-10-18 23:40:44 -07004417 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004418 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004419}
4420
Tejun Heo42809dd2012-11-19 08:13:37 -08004421static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4422{
4423 int ret;
4424
4425 mutex_lock(&cgroup_mutex);
4426 ret = cgroup_destroy_locked(dentry->d_fsdata);
4427 mutex_unlock(&cgroup_mutex);
4428
4429 return ret;
4430}
4431
Tejun Heo3ed80a62014-02-08 10:36:58 -05004432static void __init cgroup_init_cftsets(struct cgroup_subsys *ss)
Tejun Heo8e3f6542012-04-01 12:09:55 -07004433{
4434 INIT_LIST_HEAD(&ss->cftsets);
4435
4436 /*
4437 * base_cftset is embedded in subsys itself, no need to worry about
4438 * deregistration.
4439 */
4440 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004441 struct cftype *cft;
4442
4443 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4444 cft->ss = ss;
4445
Tejun Heo8e3f6542012-04-01 12:09:55 -07004446 ss->base_cftset.cfts = ss->base_cftypes;
4447 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4448 }
4449}
4450
Li Zefan06a11922008-04-29 01:00:07 -07004451static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004452{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004453 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004454
4455 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004456
Tejun Heo648bb562012-11-19 08:13:36 -08004457 mutex_lock(&cgroup_mutex);
4458
Tejun Heo8e3f6542012-04-01 12:09:55 -07004459 /* init base cftset */
4460 cgroup_init_cftsets(ss);
4461
Paul Menageddbcc7e2007-10-18 23:39:30 -07004462 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004463 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004464 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004465 /* We don't handle early failures gracefully */
4466 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004467 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004468
Li Zefane8d55fd2008-04-29 01:00:13 -07004469 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004470 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004471 * newly registered, all tasks and hence the
4472 * init_css_set is in the subsystem's top cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004473 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004474
4475 need_forkexit_callback |= ss->fork || ss->exit;
4476
Li Zefane8d55fd2008-04-29 01:00:13 -07004477 /* At system boot, before all subsystems have been
4478 * registered, no tasks have been forked, so we don't
4479 * need to invoke fork callbacks here. */
4480 BUG_ON(!list_empty(&init_task.tasks));
4481
Tejun Heoae7f1642013-08-13 20:22:50 -04004482 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004483
Tejun Heo648bb562012-11-19 08:13:36 -08004484 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004485}
4486
4487/**
Li Zefana043e3b2008-02-23 15:24:09 -08004488 * cgroup_init_early - cgroup initialization at system boot
4489 *
4490 * Initialize cgroups at system boot, and initialize any
4491 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004492 */
4493int __init cgroup_init_early(void)
4494{
Tejun Heo30159ec2013-06-25 11:53:37 -07004495 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004496 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004497
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004498 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004499 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004500 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004501 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004502 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004503 init_cgroup_root(&cgroup_dummy_root);
4504 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004505 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004506
Tejun Heo69d02062013-06-12 21:04:50 -07004507 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004508 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4509 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004510 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004511
Tejun Heo3ed80a62014-02-08 10:36:58 -05004512 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004513 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004514 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4515 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004516 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004517 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4518 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4519
Tejun Heoaec25022014-02-08 10:36:58 -05004520 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004521 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004522
4523 if (ss->early_init)
4524 cgroup_init_subsys(ss);
4525 }
4526 return 0;
4527}
4528
4529/**
Li Zefana043e3b2008-02-23 15:24:09 -08004530 * cgroup_init - cgroup initialization
4531 *
4532 * Register cgroup filesystem and /proc file, and initialize
4533 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004534 */
4535int __init cgroup_init(void)
4536{
Tejun Heo30159ec2013-06-25 11:53:37 -07004537 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004538 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004539 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004540
4541 err = bdi_init(&cgroup_backing_dev_info);
4542 if (err)
4543 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004544
Tejun Heo3ed80a62014-02-08 10:36:58 -05004545 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004546 if (!ss->early_init)
4547 cgroup_init_subsys(ss);
4548 }
4549
Tejun Heofa3ca072013-04-14 11:36:56 -07004550 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004551 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004552
Tejun Heo82fe9b02013-06-25 11:53:37 -07004553 /* Add init_css_set to the hash table */
4554 key = css_set_hash(init_css_set.subsys);
4555 hash_add(css_set_table, &init_css_set.hlist, key);
4556
Tejun Heofc76df72013-06-25 11:53:37 -07004557 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004558
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004559 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4560 0, 1, GFP_KERNEL);
4561 BUG_ON(err < 0);
4562
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004563 mutex_unlock(&cgroup_mutex);
4564
Greg KH676db4a2010-08-05 13:53:35 -07004565 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4566 if (!cgroup_kobj) {
4567 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004568 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004569 }
4570
4571 err = register_filesystem(&cgroup_fs_type);
4572 if (err < 0) {
4573 kobject_put(cgroup_kobj);
4574 goto out;
4575 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004576
Li Zefan46ae2202008-04-29 01:00:08 -07004577 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004578
Paul Menageddbcc7e2007-10-18 23:39:30 -07004579out:
Paul Menagea4243162007-10-18 23:39:35 -07004580 if (err)
4581 bdi_destroy(&cgroup_backing_dev_info);
4582
Paul Menageddbcc7e2007-10-18 23:39:30 -07004583 return err;
4584}
Paul Menageb4f48b62007-10-18 23:39:33 -07004585
Tejun Heoe5fca242013-11-22 17:14:39 -05004586static int __init cgroup_wq_init(void)
4587{
4588 /*
4589 * There isn't much point in executing destruction path in
4590 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004591 *
4592 * XXX: Must be ordered to make sure parent is offlined after
4593 * children. The ordering requirement is for memcg where a
4594 * parent's offline may wait for a child's leading to deadlock. In
4595 * the long term, this should be fixed from memcg side.
Tejun Heoe5fca242013-11-22 17:14:39 -05004596 *
4597 * We would prefer to do this in cgroup_init() above, but that
4598 * is called before init_workqueues(): so leave this until after.
4599 */
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004600 cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0);
Tejun Heoe5fca242013-11-22 17:14:39 -05004601 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004602
4603 /*
4604 * Used to destroy pidlists and separate to serve as flush domain.
4605 * Cap @max_active to 1 too.
4606 */
4607 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4608 0, 1);
4609 BUG_ON(!cgroup_pidlist_destroy_wq);
4610
Tejun Heoe5fca242013-11-22 17:14:39 -05004611 return 0;
4612}
4613core_initcall(cgroup_wq_init);
4614
Paul Menagea4243162007-10-18 23:39:35 -07004615/*
4616 * proc_cgroup_show()
4617 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4618 * - Used for /proc/<pid>/cgroup.
4619 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4620 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004621 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004622 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4623 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4624 * cgroup to top_cgroup.
4625 */
4626
4627/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004628int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004629{
4630 struct pid *pid;
4631 struct task_struct *tsk;
4632 char *buf;
4633 int retval;
4634 struct cgroupfs_root *root;
4635
4636 retval = -ENOMEM;
4637 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4638 if (!buf)
4639 goto out;
4640
4641 retval = -ESRCH;
4642 pid = m->private;
4643 tsk = get_pid_task(pid, PIDTYPE_PID);
4644 if (!tsk)
4645 goto out_free;
4646
4647 retval = 0;
4648
4649 mutex_lock(&cgroup_mutex);
4650
Li Zefane5f6a862009-01-07 18:07:41 -08004651 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004652 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004653 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004654 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004655
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004656 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004657 for_each_subsys(ss, ssid)
4658 if (root->subsys_mask & (1 << ssid))
4659 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004660 if (strlen(root->name))
4661 seq_printf(m, "%sname=%s", count ? "," : "",
4662 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004663 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004664 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004665 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004666 if (retval < 0)
4667 goto out_unlock;
4668 seq_puts(m, buf);
4669 seq_putc(m, '\n');
4670 }
4671
4672out_unlock:
4673 mutex_unlock(&cgroup_mutex);
4674 put_task_struct(tsk);
4675out_free:
4676 kfree(buf);
4677out:
4678 return retval;
4679}
4680
Paul Menagea4243162007-10-18 23:39:35 -07004681/* Display information about each subsystem and each hierarchy */
4682static int proc_cgroupstats_show(struct seq_file *m, void *v)
4683{
Tejun Heo30159ec2013-06-25 11:53:37 -07004684 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004685 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004686
Paul Menage8bab8dd2008-04-04 14:29:57 -07004687 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004688 /*
4689 * ideally we don't want subsystems moving around while we do this.
4690 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4691 * subsys/hierarchy state.
4692 */
Paul Menagea4243162007-10-18 23:39:35 -07004693 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004694
4695 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004696 seq_printf(m, "%s\t%d\t%d\t%d\n",
4697 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004698 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004699
Paul Menagea4243162007-10-18 23:39:35 -07004700 mutex_unlock(&cgroup_mutex);
4701 return 0;
4702}
4703
4704static int cgroupstats_open(struct inode *inode, struct file *file)
4705{
Al Viro9dce07f2008-03-29 03:07:28 +00004706 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004707}
4708
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004709static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004710 .open = cgroupstats_open,
4711 .read = seq_read,
4712 .llseek = seq_lseek,
4713 .release = single_release,
4714};
4715
Paul Menageb4f48b62007-10-18 23:39:33 -07004716/**
4717 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004718 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004719 *
4720 * Description: A task inherits its parent's cgroup at fork().
4721 *
4722 * A pointer to the shared css_set was automatically copied in
4723 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004724 * it was not made under the protection of RCU or cgroup_mutex, so
4725 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4726 * have already changed current->cgroups, allowing the previously
4727 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004728 *
4729 * At the point that cgroup_fork() is called, 'current' is the parent
4730 * task, and the passed argument 'child' points to the child task.
4731 */
4732void cgroup_fork(struct task_struct *child)
4733{
Tejun Heo9bb71302012-10-18 17:52:07 -07004734 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004735 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004736 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004737 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004738 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004739}
4740
4741/**
Li Zefana043e3b2008-02-23 15:24:09 -08004742 * cgroup_post_fork - called on a new task after adding it to the task list
4743 * @child: the task in question
4744 *
Tejun Heo5edee612012-10-16 15:03:14 -07004745 * Adds the task to the list running through its css_set if necessary and
4746 * call the subsystem fork() callbacks. Has to be after the task is
4747 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004748 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004749 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004750 */
Paul Menage817929e2007-10-18 23:39:36 -07004751void cgroup_post_fork(struct task_struct *child)
4752{
Tejun Heo30159ec2013-06-25 11:53:37 -07004753 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004754 int i;
4755
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004756 /*
4757 * use_task_css_set_links is set to 1 before we walk the tasklist
4758 * under the tasklist_lock and we read it here after we added the child
4759 * to the tasklist under the tasklist_lock as well. If the child wasn't
4760 * yet in the tasklist when we walked through it from
4761 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4762 * should be visible now due to the paired locking and barriers implied
4763 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4764 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4765 * lock on fork.
4766 */
Paul Menage817929e2007-10-18 23:39:36 -07004767 if (use_task_css_set_links) {
4768 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004769 task_lock(child);
4770 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004771 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004772 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004773 write_unlock(&css_set_lock);
4774 }
Tejun Heo5edee612012-10-16 15:03:14 -07004775
4776 /*
4777 * Call ss->fork(). This must happen after @child is linked on
4778 * css_set; otherwise, @child might change state between ->fork()
4779 * and addition to css_set.
4780 */
4781 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004782 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004783 if (ss->fork)
4784 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004785 }
Paul Menage817929e2007-10-18 23:39:36 -07004786}
Tejun Heo5edee612012-10-16 15:03:14 -07004787
Paul Menage817929e2007-10-18 23:39:36 -07004788/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004789 * cgroup_exit - detach cgroup from exiting task
4790 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004791 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004792 *
4793 * Description: Detach cgroup from @tsk and release it.
4794 *
4795 * Note that cgroups marked notify_on_release force every task in
4796 * them to take the global cgroup_mutex mutex when exiting.
4797 * This could impact scaling on very large systems. Be reluctant to
4798 * use notify_on_release cgroups where very high task exit scaling
4799 * is required on large systems.
4800 *
4801 * the_top_cgroup_hack:
4802 *
4803 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4804 *
4805 * We call cgroup_exit() while the task is still competent to
4806 * handle notify_on_release(), then leave the task attached to the
4807 * root cgroup in each hierarchy for the remainder of its exit.
4808 *
4809 * To do this properly, we would increment the reference count on
4810 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4811 * code we would add a second cgroup function call, to drop that
4812 * reference. This would just create an unnecessary hot spot on
4813 * the top_cgroup reference count, to no avail.
4814 *
4815 * Normally, holding a reference to a cgroup without bumping its
4816 * count is unsafe. The cgroup could go away, or someone could
4817 * attach us to a different cgroup, decrementing the count on
4818 * the first cgroup that we never incremented. But in this case,
4819 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004820 * which wards off any cgroup_attach_task() attempts, or task is a failed
4821 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004822 */
4823void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4824{
Tejun Heo30159ec2013-06-25 11:53:37 -07004825 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004826 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004827 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004828
4829 /*
4830 * Unlink from the css_set task list if necessary.
4831 * Optimistically check cg_list before taking
4832 * css_set_lock
4833 */
4834 if (!list_empty(&tsk->cg_list)) {
4835 write_lock(&css_set_lock);
4836 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004837 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004838 write_unlock(&css_set_lock);
4839 }
4840
Paul Menageb4f48b62007-10-18 23:39:33 -07004841 /* Reassign the task to the init_css_set. */
4842 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004843 cset = task_css_set(tsk);
4844 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004845
4846 if (run_callbacks && need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004847 /* see cgroup_post_fork() for details */
4848 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004849 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004850 struct cgroup_subsys_state *old_css = cset->subsys[i];
4851 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004852
Tejun Heoeb954192013-08-08 20:11:23 -04004853 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004854 }
4855 }
4856 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004857 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004858
Tejun Heo5abb8852013-06-12 21:04:49 -07004859 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07004860}
Paul Menage697f4162007-10-18 23:39:34 -07004861
Paul Menagebd89aab2007-10-18 23:40:44 -07004862static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004863{
Li Zefanf50daa72013-03-01 15:06:07 +08004864 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004865 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004866 /*
4867 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004868 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004869 * it now
4870 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004871 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004872
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004873 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004874 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004875 list_empty(&cgrp->release_list)) {
4876 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004877 need_schedule_work = 1;
4878 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004879 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004880 if (need_schedule_work)
4881 schedule_work(&release_agent_work);
4882 }
4883}
4884
Paul Menage81a6a5c2007-10-18 23:39:38 -07004885/*
4886 * Notify userspace when a cgroup is released, by running the
4887 * configured release agent with the name of the cgroup (path
4888 * relative to the root of cgroup file system) as the argument.
4889 *
4890 * Most likely, this user command will try to rmdir this cgroup.
4891 *
4892 * This races with the possibility that some other task will be
4893 * attached to this cgroup before it is removed, or that some other
4894 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4895 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4896 * unused, and this cgroup will be reprieved from its death sentence,
4897 * to continue to serve a useful existence. Next time it's released,
4898 * we will get notified again, if it still has 'notify_on_release' set.
4899 *
4900 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4901 * means only wait until the task is successfully execve()'d. The
4902 * separate release agent task is forked by call_usermodehelper(),
4903 * then control in this thread returns here, without waiting for the
4904 * release agent task. We don't bother to wait because the caller of
4905 * this routine has no use for the exit status of the release agent
4906 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004907 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004908static void cgroup_release_agent(struct work_struct *work)
4909{
4910 BUG_ON(work != &release_agent_work);
4911 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004912 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004913 while (!list_empty(&release_list)) {
4914 char *argv[3], *envp[3];
4915 int i;
Paul Menagee788e062008-07-25 01:46:59 -07004916 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004917 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004918 struct cgroup,
4919 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004920 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004921 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004922 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004923 if (!pathbuf)
4924 goto continue_free;
4925 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
4926 goto continue_free;
4927 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4928 if (!agentbuf)
4929 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004930
4931 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004932 argv[i++] = agentbuf;
4933 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004934 argv[i] = NULL;
4935
4936 i = 0;
4937 /* minimal command environment */
4938 envp[i++] = "HOME=/";
4939 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4940 envp[i] = NULL;
4941
4942 /* Drop the lock while we invoke the usermode helper,
4943 * since the exec could involve hitting disk and hence
4944 * be a slow process */
4945 mutex_unlock(&cgroup_mutex);
4946 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004947 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004948 continue_free:
4949 kfree(pathbuf);
4950 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004951 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004952 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004953 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004954 mutex_unlock(&cgroup_mutex);
4955}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004956
4957static int __init cgroup_disable(char *str)
4958{
Tejun Heo30159ec2013-06-25 11:53:37 -07004959 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004960 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07004961 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004962
4963 while ((token = strsep(&str, ",")) != NULL) {
4964 if (!*token)
4965 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004966
Tejun Heo3ed80a62014-02-08 10:36:58 -05004967 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004968 if (!strcmp(token, ss->name)) {
4969 ss->disabled = 1;
4970 printk(KERN_INFO "Disabling %s control group"
4971 " subsystem\n", ss->name);
4972 break;
4973 }
4974 }
4975 }
4976 return 1;
4977}
4978__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004979
Tejun Heob77d7b62013-08-13 11:01:54 -04004980/**
Tejun Heo5a17f542014-02-11 11:52:47 -05004981 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04004982 * @dentry: directory dentry of interest
4983 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04004984 *
Tejun Heo5a17f542014-02-11 11:52:47 -05004985 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4986 * to get the corresponding css and return it. If such css doesn't exist
4987 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02004988 */
Tejun Heo5a17f542014-02-11 11:52:47 -05004989struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4990 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004991{
4992 struct cgroup *cgrp;
Tejun Heo5a17f542014-02-11 11:52:47 -05004993 struct cgroup_subsys_state *css;
Tejun Heob77d7b62013-08-13 11:01:54 -04004994
Tejun Heo35cf0832013-08-26 18:40:56 -04004995 /* is @dentry a cgroup dir? */
4996 if (!dentry->d_inode ||
4997 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004998 return ERR_PTR(-EBADF);
4999
Tejun Heo5a17f542014-02-11 11:52:47 -05005000 rcu_read_lock();
5001
Tejun Heo35cf0832013-08-26 18:40:56 -04005002 cgrp = __d_cgrp(dentry);
Tejun Heo5a17f542014-02-11 11:52:47 -05005003 css = cgroup_css(cgrp, ss);
5004
5005 if (!css || !css_tryget(css))
5006 css = ERR_PTR(-ENOENT);
5007
5008 rcu_read_unlock();
5009 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005010}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005011
Li Zefan1cb650b2013-08-19 10:05:24 +08005012/**
5013 * css_from_id - lookup css by id
5014 * @id: the cgroup id
5015 * @ss: cgroup subsys to be looked into
5016 *
5017 * Returns the css if there's valid one with @id, otherwise returns NULL.
5018 * Should be called under rcu_read_lock().
5019 */
5020struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5021{
5022 struct cgroup *cgrp;
5023
Tejun Heo87fb54f2013-12-06 15:11:55 -05005024 cgroup_assert_mutex_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08005025
5026 cgrp = idr_find(&ss->root->cgroup_idr, id);
5027 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005028 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005029 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005030}
5031
Paul Menagefe693432009-09-23 15:56:20 -07005032#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005033static struct cgroup_subsys_state *
5034debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005035{
5036 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5037
5038 if (!css)
5039 return ERR_PTR(-ENOMEM);
5040
5041 return css;
5042}
5043
Tejun Heoeb954192013-08-08 20:11:23 -04005044static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005045{
Tejun Heoeb954192013-08-08 20:11:23 -04005046 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005047}
5048
Tejun Heo182446d2013-08-08 20:11:24 -04005049static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5050 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005051{
Tejun Heo182446d2013-08-08 20:11:24 -04005052 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005053}
5054
Tejun Heo182446d2013-08-08 20:11:24 -04005055static u64 current_css_set_read(struct cgroup_subsys_state *css,
5056 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005057{
5058 return (u64)(unsigned long)current->cgroups;
5059}
5060
Tejun Heo182446d2013-08-08 20:11:24 -04005061static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005062 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005063{
5064 u64 count;
5065
5066 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005067 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005068 rcu_read_unlock();
5069 return count;
5070}
5071
Tejun Heo2da8ca82013-12-05 12:28:04 -05005072static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005073{
Tejun Heo69d02062013-06-12 21:04:50 -07005074 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005075 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005076
5077 read_lock(&css_set_lock);
5078 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005079 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005080 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005081 struct cgroup *c = link->cgrp;
5082 const char *name;
5083
5084 if (c->dentry)
5085 name = c->dentry->d_name.name;
5086 else
5087 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005088 seq_printf(seq, "Root %d group %s\n",
5089 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005090 }
5091 rcu_read_unlock();
5092 read_unlock(&css_set_lock);
5093 return 0;
5094}
5095
5096#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005097static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005098{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005099 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005100 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005101
5102 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005103 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005104 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005105 struct task_struct *task;
5106 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005107 seq_printf(seq, "css_set %p\n", cset);
5108 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005109 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5110 seq_puts(seq, " ...\n");
5111 break;
5112 } else {
5113 seq_printf(seq, " task %d\n",
5114 task_pid_vnr(task));
5115 }
5116 }
5117 }
5118 read_unlock(&css_set_lock);
5119 return 0;
5120}
5121
Tejun Heo182446d2013-08-08 20:11:24 -04005122static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005123{
Tejun Heo182446d2013-08-08 20:11:24 -04005124 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005125}
5126
5127static struct cftype debug_files[] = {
5128 {
Paul Menagefe693432009-09-23 15:56:20 -07005129 .name = "taskcount",
5130 .read_u64 = debug_taskcount_read,
5131 },
5132
5133 {
5134 .name = "current_css_set",
5135 .read_u64 = current_css_set_read,
5136 },
5137
5138 {
5139 .name = "current_css_set_refcount",
5140 .read_u64 = current_css_set_refcount_read,
5141 },
5142
5143 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005144 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005145 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005146 },
5147
5148 {
5149 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005150 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005151 },
5152
5153 {
Paul Menagefe693432009-09-23 15:56:20 -07005154 .name = "releasable",
5155 .read_u64 = releasable_read,
5156 },
Paul Menagefe693432009-09-23 15:56:20 -07005157
Tejun Heo4baf6e32012-04-01 12:09:55 -07005158 { } /* terminate */
5159};
Paul Menagefe693432009-09-23 15:56:20 -07005160
Tejun Heo073219e2014-02-08 10:36:58 -05005161struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005162 .css_alloc = debug_css_alloc,
5163 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005164 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005165};
5166#endif /* CONFIG_CGROUP_DEBUG */