blob: 42e588ef62d1ffb8d497952396cc8644065b8ca3 [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
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050070#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
71 MAX_CFTYPE_NAME + 2)
72
Tejun Heob1a21362013-11-29 10:42:58 -050073/*
Tejun Heoace2bee2014-02-11 11:52:47 -050074 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
75 * creation/removal and hierarchy changing operations including cgroup
76 * creation, removal, css association and controller rebinding. This outer
77 * lock is needed mainly to resolve the circular dependency between kernfs
78 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
79 */
80static DEFINE_MUTEX(cgroup_tree_mutex);
81
82/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080083 * cgroup_mutex is the master lock. Any modification to cgroup or its
84 * hierarchy must be performed while holding it.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080085 */
Tejun Heo22194492013-04-07 09:29:51 -070086#ifdef CONFIG_PROVE_RCU
87DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040088EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070089#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070090static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070091#endif
92
Tejun Heo69e943b2014-02-08 10:36:58 -050093/*
94 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
95 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
96 */
97static DEFINE_SPINLOCK(release_agent_path_lock);
98
Tejun Heoace2bee2014-02-11 11:52:47 -050099#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500100 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -0500101 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500102 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500103 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo87fb54f2013-12-06 15:11:55 -0500104
Ben Blumaae8aab2010-03-10 15:22:07 -0800105/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500106 * cgroup destruction makes heavy use of work items and there can be a lot
107 * of concurrent destructions. Use a separate workqueue so that cgroup
108 * destruction work items don't end up filling up max_active of system_wq
109 * which may lead to deadlock.
110 */
111static struct workqueue_struct *cgroup_destroy_wq;
112
113/*
Tejun Heob1a21362013-11-29 10:42:58 -0500114 * pidlist destructions need to be flushed on cgroup destruction. Use a
115 * separate workqueue as flush domain.
116 */
117static struct workqueue_struct *cgroup_pidlist_destroy_wq;
118
Tejun Heo3ed80a62014-02-08 10:36:58 -0500119/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500120#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500121static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122#include <linux/cgroup_subsys.h>
123};
Tejun Heo073219e2014-02-08 10:36:58 -0500124#undef SUBSYS
125
126/* array of cgroup subsystem names */
127#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
128static const char *cgroup_subsys_name[] = {
129#include <linux/cgroup_subsys.h>
130};
131#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700132
Paul Menageddbcc7e2007-10-18 23:39:30 -0700133/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700134 * The dummy hierarchy, reserved for the subsystems that are otherwise
135 * unattached - it never has more than a single cgroup, and all tasks are
136 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700137 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700138static struct cgroupfs_root cgroup_dummy_root;
139
140/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
141static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700142
143/* The list of hierarchy roots */
144
Tejun Heo9871bf92013-06-24 15:21:47 -0700145static LIST_HEAD(cgroup_roots);
146static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700147
Tejun Heo3417ae12014-02-08 10:37:01 -0500148/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700149static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700150
Li Zefan65dff752013-03-01 15:01:56 +0800151static struct cgroup_name root_cgroup_name = { .name = "/" };
152
Li Zefan794611a2013-06-18 18:53:53 +0800153/*
154 * Assign a monotonically increasing serial number to cgroups. It
155 * guarantees cgroups with bigger numbers are newer than those with smaller
156 * numbers. Also, as cgroups are always appended to the parent's
157 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700158 * the ascending serial number order on the list. Protected by
159 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800160 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700161static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800162
Paul Menageddbcc7e2007-10-18 23:39:30 -0700163/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800164 * check for fork/exit handlers to call. This avoids us having to do
165 * extra work in the fork/exit path if none of the subsystems need to
166 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700167 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700168static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700169
Tejun Heo628f7cd2013-06-28 16:24:11 -0700170static struct cftype cgroup_base_files[];
171
Tejun Heof20104d2013-08-13 20:22:50 -0400172static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800173static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400174static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
175 bool is_add);
Tejun Heoe605b362013-11-27 18:16:21 -0500176static int cgroup_file_release(struct inode *inode, struct file *file);
Tejun Heob1a21362013-11-29 10:42:58 -0500177static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800178
Tejun Heo95109b62013-08-08 20:11:27 -0400179/**
180 * cgroup_css - obtain a cgroup's css for the specified subsystem
181 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400182 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400183 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400184 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
185 * function must be called either under cgroup_mutex or rcu_read_lock() and
186 * the caller is responsible for pinning the returned css if it wants to
187 * keep accessing it outside the said locks. This function may return
188 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400189 */
190static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400191 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400192{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400193 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500194 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500195 lockdep_is_held(&cgroup_tree_mutex) ||
196 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400197 else
198 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400199}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700200
Paul Menageddbcc7e2007-10-18 23:39:30 -0700201/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700202static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700203{
Tejun Heo54766d42013-06-12 21:04:53 -0700204 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700205}
206
Li Zefan78574cf2013-04-08 19:00:38 -0700207/**
208 * cgroup_is_descendant - test ancestry
209 * @cgrp: the cgroup to be tested
210 * @ancestor: possible ancestor of @cgrp
211 *
212 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
213 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
214 * and @ancestor are accessible.
215 */
216bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
217{
218 while (cgrp) {
219 if (cgrp == ancestor)
220 return true;
221 cgrp = cgrp->parent;
222 }
223 return false;
224}
225EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700226
Adrian Bunke9685a02008-02-07 00:13:46 -0800227static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700228{
229 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700230 (1 << CGRP_RELEASABLE) |
231 (1 << CGRP_NOTIFY_ON_RELEASE);
232 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700233}
234
Adrian Bunke9685a02008-02-07 00:13:46 -0800235static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700236{
Paul Menagebd89aab2007-10-18 23:40:44 -0700237 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700238}
239
Tejun Heo30159ec2013-06-25 11:53:37 -0700240/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500241 * for_each_css - iterate all css's of a cgroup
242 * @css: the iteration cursor
243 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
244 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700245 *
246 * Should be called under cgroup_mutex.
247 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500248#define for_each_css(css, ssid, cgrp) \
249 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
250 if (!((css) = rcu_dereference_check( \
251 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500252 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500253 lockdep_is_held(&cgroup_mutex)))) { } \
254 else
255
256/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500257 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700258 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500259 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700260 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500261#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500262 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
263 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700264
Tejun Heo5549c492013-06-24 15:21:48 -0700265/* iterate across the active hierarchies */
266#define for_each_active_root(root) \
267 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700268
Tejun Heof6ea9372012-04-01 12:09:55 -0700269static inline struct cgroup *__d_cgrp(struct dentry *dentry)
270{
271 return dentry->d_fsdata;
272}
273
Tejun Heo05ef1d72012-04-01 12:09:56 -0700274static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700275{
276 return dentry->d_fsdata;
277}
278
Tejun Heo05ef1d72012-04-01 12:09:56 -0700279static inline struct cftype *__d_cft(struct dentry *dentry)
280{
281 return __d_cfe(dentry)->type;
282}
283
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700284/**
285 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
286 * @cgrp: the cgroup to be checked for liveness
287 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700288 * On success, returns true; the mutex should be later unlocked. On
289 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700290 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700291static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700292{
293 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700294 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700295 mutex_unlock(&cgroup_mutex);
296 return false;
297 }
298 return true;
299}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700300
Paul Menage81a6a5c2007-10-18 23:39:38 -0700301/* the list of cgroups eligible for automatic release. Protected by
302 * release_list_lock */
303static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200304static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700305static void cgroup_release_agent(struct work_struct *work);
306static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700307static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700308
Tejun Heo69d02062013-06-12 21:04:50 -0700309/*
310 * A cgroup can be associated with multiple css_sets as different tasks may
311 * belong to different cgroups on different hierarchies. In the other
312 * direction, a css_set is naturally associated with multiple cgroups.
313 * This M:N relationship is represented by the following link structure
314 * which exists for each association and allows traversing the associations
315 * from both sides.
316 */
317struct cgrp_cset_link {
318 /* the cgroup and css_set this link associates */
319 struct cgroup *cgrp;
320 struct css_set *cset;
321
322 /* list of cgrp_cset_links anchored at cgrp->cset_links */
323 struct list_head cset_link;
324
325 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
326 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700327};
328
329/* The default css_set - used by init and its children prior to any
330 * hierarchies being mounted. It contains a pointer to the root state
331 * for each subsystem. Also used to anchor the list of css_sets. Not
332 * reference-counted, to improve performance when child cgroups
333 * haven't been created.
334 */
335
336static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700337static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700338
Tejun Heo0942eee2013-08-08 20:11:26 -0400339/*
340 * css_set_lock protects the list of css_set objects, and the chain of
341 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400342 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400343 */
Paul Menage817929e2007-10-18 23:39:36 -0700344static DEFINE_RWLOCK(css_set_lock);
345static int css_set_count;
346
Paul Menage7717f7b2009-09-23 15:56:22 -0700347/*
348 * hash table for cgroup groups. This improves the performance to find
349 * an existing css_set. This hash doesn't (currently) take into
350 * account cgroups in empty hierarchies.
351 */
Li Zefan472b1052008-04-29 01:00:11 -0700352#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800353static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700354
Li Zefan0ac801f2013-01-10 11:49:27 +0800355static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700356{
Li Zefan0ac801f2013-01-10 11:49:27 +0800357 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700358 struct cgroup_subsys *ss;
359 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700360
Tejun Heo30159ec2013-06-25 11:53:37 -0700361 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800362 key += (unsigned long)css[i];
363 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700364
Li Zefan0ac801f2013-01-10 11:49:27 +0800365 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700366}
367
Tejun Heo0942eee2013-08-08 20:11:26 -0400368/*
369 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400370 * until after the first call to css_task_iter_start(). This reduces the
371 * fork()/exit() overhead for people who have cgroups compiled into their
372 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400373 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700374static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700375
Tejun Heo5abb8852013-06-12 21:04:49 -0700376static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700377{
Tejun Heo69d02062013-06-12 21:04:50 -0700378 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700379
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700380 /*
381 * Ensure that the refcount doesn't hit zero while any readers
382 * can see it. Similar to atomic_dec_and_lock(), but for an
383 * rwlock
384 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700385 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700386 return;
387 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700388 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700389 write_unlock(&css_set_lock);
390 return;
391 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700392
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700393 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700394 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700395 css_set_count--;
396
Tejun Heo69d02062013-06-12 21:04:50 -0700397 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700398 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700399
Tejun Heo69d02062013-06-12 21:04:50 -0700400 list_del(&link->cset_link);
401 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800402
Tejun Heoddd69142013-06-12 21:04:54 -0700403 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700404 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700405 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700406 set_bit(CGRP_RELEASABLE, &cgrp->flags);
407 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700408 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700409
410 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700411 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700412
413 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700414 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700415}
416
417/*
418 * refcounted get/put for css_set objects
419 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700420static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700421{
Tejun Heo5abb8852013-06-12 21:04:49 -0700422 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700423}
424
Tejun Heo5abb8852013-06-12 21:04:49 -0700425static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700426{
Tejun Heo5abb8852013-06-12 21:04:49 -0700427 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700428}
429
Tejun Heo5abb8852013-06-12 21:04:49 -0700430static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700431{
Tejun Heo5abb8852013-06-12 21:04:49 -0700432 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700433}
434
Tejun Heob326f9d2013-06-24 15:21:48 -0700435/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700436 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700437 * @cset: candidate css_set being tested
438 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700439 * @new_cgrp: cgroup that's being entered by the task
440 * @template: desired set of css pointers in css_set (pre-calculated)
441 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800442 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700443 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
444 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700445static bool compare_css_sets(struct css_set *cset,
446 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700447 struct cgroup *new_cgrp,
448 struct cgroup_subsys_state *template[])
449{
450 struct list_head *l1, *l2;
451
Tejun Heo5abb8852013-06-12 21:04:49 -0700452 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700453 /* Not all subsystems matched */
454 return false;
455 }
456
457 /*
458 * Compare cgroup pointers in order to distinguish between
459 * different cgroups in heirarchies with no subsystems. We
460 * could get by with just this check alone (and skip the
461 * memcmp above) but on most setups the memcmp check will
462 * avoid the need for this more expensive check on almost all
463 * candidates.
464 */
465
Tejun Heo69d02062013-06-12 21:04:50 -0700466 l1 = &cset->cgrp_links;
467 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700468 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700469 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700470 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700471
472 l1 = l1->next;
473 l2 = l2->next;
474 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700475 if (l1 == &cset->cgrp_links) {
476 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700477 break;
478 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700479 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700480 }
481 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700482 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
483 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
484 cgrp1 = link1->cgrp;
485 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700486 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700487 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700488
489 /*
490 * If this hierarchy is the hierarchy of the cgroup
491 * that's changing, then we need to check that this
492 * css_set points to the new cgroup; if it's any other
493 * hierarchy, then this css_set should point to the
494 * same cgroup as the old css_set.
495 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700496 if (cgrp1->root == new_cgrp->root) {
497 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700498 return false;
499 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700500 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700501 return false;
502 }
503 }
504 return true;
505}
506
Tejun Heob326f9d2013-06-24 15:21:48 -0700507/**
508 * find_existing_css_set - init css array and find the matching css_set
509 * @old_cset: the css_set that we're using before the cgroup transition
510 * @cgrp: the cgroup that we're moving into
511 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700512 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700513static struct css_set *find_existing_css_set(struct css_set *old_cset,
514 struct cgroup *cgrp,
515 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700516{
Paul Menagebd89aab2007-10-18 23:40:44 -0700517 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700518 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700519 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800520 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700521 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700522
Ben Blumaae8aab2010-03-10 15:22:07 -0800523 /*
524 * Build the set of subsystem state objects that we want to see in the
525 * new css_set. while subsystems can change globally, the entries here
526 * won't change, so no need for locking.
527 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700528 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400529 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700530 /* Subsystem is in this hierarchy. So we want
531 * the subsystem state from the new
532 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400533 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700534 } else {
535 /* Subsystem is not in this hierarchy, so we
536 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700537 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700538 }
539 }
540
Li Zefan0ac801f2013-01-10 11:49:27 +0800541 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700542 hash_for_each_possible(css_set_table, cset, hlist, key) {
543 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700544 continue;
545
546 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700547 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700548 }
Paul Menage817929e2007-10-18 23:39:36 -0700549
550 /* No existing cgroup group matched */
551 return NULL;
552}
553
Tejun Heo69d02062013-06-12 21:04:50 -0700554static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700555{
Tejun Heo69d02062013-06-12 21:04:50 -0700556 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700557
Tejun Heo69d02062013-06-12 21:04:50 -0700558 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
559 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700560 kfree(link);
561 }
562}
563
Tejun Heo69d02062013-06-12 21:04:50 -0700564/**
565 * allocate_cgrp_cset_links - allocate cgrp_cset_links
566 * @count: the number of links to allocate
567 * @tmp_links: list_head the allocated links are put on
568 *
569 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
570 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700571 */
Tejun Heo69d02062013-06-12 21:04:50 -0700572static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700573{
Tejun Heo69d02062013-06-12 21:04:50 -0700574 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700575 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700576
577 INIT_LIST_HEAD(tmp_links);
578
Li Zefan36553432008-07-29 22:33:19 -0700579 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700580 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700581 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700582 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700583 return -ENOMEM;
584 }
Tejun Heo69d02062013-06-12 21:04:50 -0700585 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700586 }
587 return 0;
588}
589
Li Zefanc12f65d2009-01-07 18:07:42 -0800590/**
591 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700592 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700593 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800594 * @cgrp: the destination cgroup
595 */
Tejun Heo69d02062013-06-12 21:04:50 -0700596static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
597 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800598{
Tejun Heo69d02062013-06-12 21:04:50 -0700599 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800600
Tejun Heo69d02062013-06-12 21:04:50 -0700601 BUG_ON(list_empty(tmp_links));
602 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
603 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700604 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700605 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700606 /*
607 * Always add links to the tail of the list so that the list
608 * is sorted by order of hierarchy creation
609 */
Tejun Heo69d02062013-06-12 21:04:50 -0700610 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800611}
612
Tejun Heob326f9d2013-06-24 15:21:48 -0700613/**
614 * find_css_set - return a new css_set with one cgroup updated
615 * @old_cset: the baseline css_set
616 * @cgrp: the cgroup to be updated
617 *
618 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
619 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700620 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700621static struct css_set *find_css_set(struct css_set *old_cset,
622 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700623{
Tejun Heob326f9d2013-06-24 15:21:48 -0700624 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700625 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700626 struct list_head tmp_links;
627 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800628 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700629
Tejun Heob326f9d2013-06-24 15:21:48 -0700630 lockdep_assert_held(&cgroup_mutex);
631
Paul Menage817929e2007-10-18 23:39:36 -0700632 /* First see if we already have a cgroup group that matches
633 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700634 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700635 cset = find_existing_css_set(old_cset, cgrp, template);
636 if (cset)
637 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700638 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700639
Tejun Heo5abb8852013-06-12 21:04:49 -0700640 if (cset)
641 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700642
Tejun Heof4f4be22013-06-12 21:04:51 -0700643 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700644 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700645 return NULL;
646
Tejun Heo69d02062013-06-12 21:04:50 -0700647 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700648 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700649 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700650 return NULL;
651 }
652
Tejun Heo5abb8852013-06-12 21:04:49 -0700653 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700654 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700655 INIT_LIST_HEAD(&cset->tasks);
656 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700657
658 /* Copy the set of subsystem state objects generated in
659 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700660 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700661
662 write_lock(&css_set_lock);
663 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700664 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700665 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700666
Paul Menage7717f7b2009-09-23 15:56:22 -0700667 if (c->root == cgrp->root)
668 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700669 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700670 }
Paul Menage817929e2007-10-18 23:39:36 -0700671
Tejun Heo69d02062013-06-12 21:04:50 -0700672 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700673
Paul Menage817929e2007-10-18 23:39:36 -0700674 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700675
676 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700677 key = css_set_hash(cset->subsys);
678 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700679
Paul Menage817929e2007-10-18 23:39:36 -0700680 write_unlock(&css_set_lock);
681
Tejun Heo5abb8852013-06-12 21:04:49 -0700682 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700683}
684
Paul Menageddbcc7e2007-10-18 23:39:30 -0700685/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700686 * Return the cgroup for "task" from the given hierarchy. Must be
687 * called with cgroup_mutex held.
688 */
689static struct cgroup *task_cgroup_from_root(struct task_struct *task,
690 struct cgroupfs_root *root)
691{
Tejun Heo5abb8852013-06-12 21:04:49 -0700692 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700693 struct cgroup *res = NULL;
694
695 BUG_ON(!mutex_is_locked(&cgroup_mutex));
696 read_lock(&css_set_lock);
697 /*
698 * No need to lock the task - since we hold cgroup_mutex the
699 * task can't change groups, so the only thing that can happen
700 * is that it exits and its css is set back to init_css_set.
701 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700702 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700703 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700704 res = &root->top_cgroup;
705 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700706 struct cgrp_cset_link *link;
707
708 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700709 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700710
Paul Menage7717f7b2009-09-23 15:56:22 -0700711 if (c->root == root) {
712 res = c;
713 break;
714 }
715 }
716 }
717 read_unlock(&css_set_lock);
718 BUG_ON(!res);
719 return res;
720}
721
722/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700723 * There is one global cgroup mutex. We also require taking
724 * task_lock() when dereferencing a task's cgroup subsys pointers.
725 * See "The task_lock() exception", at the end of this comment.
726 *
727 * A task must hold cgroup_mutex to modify cgroups.
728 *
729 * Any task can increment and decrement the count field without lock.
730 * So in general, code holding cgroup_mutex can't rely on the count
731 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800732 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700733 * means that no tasks are currently attached, therefore there is no
734 * way a task attached to that cgroup can fork (the other way to
735 * increment the count). So code holding cgroup_mutex can safely
736 * assume that if the count is zero, it will stay zero. Similarly, if
737 * a task holds cgroup_mutex on a cgroup with zero count, it
738 * knows that the cgroup won't be removed, as cgroup_rmdir()
739 * needs that mutex.
740 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700741 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
742 * (usually) take cgroup_mutex. These are the two most performance
743 * critical pieces of code here. The exception occurs on cgroup_exit(),
744 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
745 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800746 * to the release agent with the name of the cgroup (path relative to
747 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700748 *
749 * A cgroup can only be deleted if both its 'count' of using tasks
750 * is zero, and its list of 'children' cgroups is empty. Since all
751 * tasks in the system use _some_ cgroup, and since there is always at
752 * least one task in the system (init, pid == 1), therefore, top_cgroup
753 * always has either children cgroups and/or using tasks. So we don't
754 * need a special hack to ensure that top_cgroup cannot be deleted.
755 *
756 * The task_lock() exception
757 *
758 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800759 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800760 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700761 * several performance critical places that need to reference
762 * task->cgroup without the expense of grabbing a system global
763 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800764 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700765 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
766 * the task_struct routinely used for such matters.
767 *
768 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800769 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700770 */
771
Paul Menageddbcc7e2007-10-18 23:39:30 -0700772/*
773 * A couple of forward declarations required, due to cyclic reference loop:
774 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
775 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
776 * -> cgroup_mkdir.
777 */
778
Al Viro18bb1db2011-07-26 01:41:39 -0400779static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700780static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700781static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700782static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700783static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700784
785static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200786 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700787 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700788};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700789
Al Viroa5e7ed32011-07-26 01:55:55 -0400790static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791{
792 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793
794 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400795 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100797 inode->i_uid = current_fsuid();
798 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
800 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
801 }
802 return inode;
803}
804
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500805static struct cgroup_name *cgroup_alloc_name(const char *name_str)
Li Zefan65dff752013-03-01 15:01:56 +0800806{
807 struct cgroup_name *name;
808
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500809 name = kmalloc(sizeof(*name) + strlen(name_str) + 1, GFP_KERNEL);
Li Zefan65dff752013-03-01 15:01:56 +0800810 if (!name)
811 return NULL;
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500812 strcpy(name->name, name_str);
Li Zefan65dff752013-03-01 15:01:56 +0800813 return name;
814}
815
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500816static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
817 char *buf)
818{
819 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
820 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
821 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
822 cft->ss->name, cft->name);
823 else
824 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
825 return buf;
826}
827
Li Zefanbe445622013-01-24 14:31:42 +0800828static void cgroup_free_fn(struct work_struct *work)
829{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700830 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800831
832 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800833 cgrp->root->number_of_cgroups--;
834 mutex_unlock(&cgroup_mutex);
835
836 /*
Li Zefan415cf072013-04-08 14:35:02 +0800837 * We get a ref to the parent's dentry, and put the ref when
838 * this cgroup is being freed, so it's guaranteed that the
839 * parent won't be destroyed before its children.
840 */
841 dput(cgrp->parent->dentry);
842
843 /*
Li Zefanbe445622013-01-24 14:31:42 +0800844 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700845 * created the cgroup. This will free cgrp->root, if we are
846 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800847 */
848 deactivate_super(cgrp->root->sb);
849
Tejun Heob1a21362013-11-29 10:42:58 -0500850 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800851
852 simple_xattrs_free(&cgrp->xattrs);
853
Li Zefan65dff752013-03-01 15:01:56 +0800854 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800855 kfree(cgrp);
856}
857
858static void cgroup_free_rcu(struct rcu_head *head)
859{
860 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
861
Tejun Heoea15f8c2013-06-13 19:27:42 -0700862 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500863 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800864}
865
Paul Menageddbcc7e2007-10-18 23:39:30 -0700866static void cgroup_diput(struct dentry *dentry, struct inode *inode)
867{
868 /* is dentry a directory ? if so, kfree() associated cgroup */
869 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700870 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800871
Tejun Heo54766d42013-06-12 21:04:53 -0700872 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanc1a71502013-12-17 11:13:39 +0800873
874 /*
875 * XXX: cgrp->id is only used to look up css's. As cgroup
876 * and css's lifetimes will be decoupled, it should be made
877 * per-subsystem and moved to css->id so that lookups are
878 * successful until the target css is released.
879 */
Li Zefan0ab02ca2014-02-11 16:05:46 +0800880 mutex_lock(&cgroup_mutex);
Li Zefanc1a71502013-12-17 11:13:39 +0800881 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +0800882 mutex_unlock(&cgroup_mutex);
Li Zefanc1a71502013-12-17 11:13:39 +0800883 cgrp->id = -1;
884
Li Zefanbe445622013-01-24 14:31:42 +0800885 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700886 } else {
887 struct cfent *cfe = __d_cfe(dentry);
888 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
889
890 WARN_ONCE(!list_empty(&cfe->node) &&
891 cgrp != &cgrp->root->top_cgroup,
892 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700893 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700894 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700895 }
896 iput(inode);
897}
898
899static void remove_dir(struct dentry *d)
900{
901 struct dentry *parent = dget(d->d_parent);
902
903 d_delete(d);
904 simple_rmdir(parent->d_inode, d);
905 dput(parent);
906}
907
Li Zefan2739d3c2013-01-21 18:18:33 +0800908static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700909{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700910 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700911
Tejun Heo05ef1d72012-04-01 12:09:56 -0700912 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -0500913 lockdep_assert_held(&cgroup_tree_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100914
Li Zefan2739d3c2013-01-21 18:18:33 +0800915 /*
916 * If we're doing cleanup due to failure of cgroup_create(),
917 * the corresponding @cfe may not exist.
918 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700919 list_for_each_entry(cfe, &cgrp->files, node) {
920 struct dentry *d = cfe->dentry;
921
922 if (cft && cfe->type != cft)
923 continue;
924
925 dget(d);
926 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700927 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700928 list_del_init(&cfe->node);
929 dput(d);
930
Li Zefan2739d3c2013-01-21 18:18:33 +0800931 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700932 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700933}
934
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400935/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700936 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700937 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400938 * @subsys_mask: mask of the subsystem ids whose files should be removed
939 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700940static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700941{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400942 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700943 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700944
Tejun Heob420ba72013-07-12 12:34:02 -0700945 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400946 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700947
948 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400949 continue;
950 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400951 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400952 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700953}
954
955/*
956 * NOTE : the dentry must have been dget()'ed
957 */
958static void cgroup_d_remove_dir(struct dentry *dentry)
959{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100960 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700961
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100962 parent = dentry->d_parent;
963 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800964 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700965 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100966 spin_unlock(&dentry->d_lock);
967 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968 remove_dir(dentry);
969}
970
971static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700972 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973{
Paul Menagebd89aab2007-10-18 23:40:44 -0700974 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700975 struct cgroup_subsys *ss;
Tejun Heo31261212013-06-28 17:07:30 -0700976 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700977
Tejun Heoace2bee2014-02-11 11:52:47 -0500978 lockdep_assert_held(&cgroup_tree_mutex);
979 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -0800980
Paul Menageddbcc7e2007-10-18 23:39:30 -0700981 /* Check that any added subsystems are currently free */
Tejun Heo3ed80a62014-02-08 10:36:58 -0500982 for_each_subsys(ss, i)
983 if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
984 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700985
Tejun Heo31261212013-06-28 17:07:30 -0700986 ret = cgroup_populate_dir(cgrp, added_mask);
987 if (ret)
Tejun Heo3ed80a62014-02-08 10:36:58 -0500988 return ret;
Tejun Heo31261212013-06-28 17:07:30 -0700989
990 /*
991 * Nothing can fail from this point on. Remove files for the
992 * removed subsystems and rebind each subsystem.
993 */
Tejun Heo4ac06012014-02-11 11:52:47 -0500994 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -0700995 cgroup_clear_dir(cgrp, removed_mask);
Tejun Heo4ac06012014-02-11 11:52:47 -0500996 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -0700997
Tejun Heo30159ec2013-06-25 11:53:37 -0700998 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001000
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001001 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001003 BUG_ON(cgroup_css(cgrp, ss));
1004 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1005 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001006
Tejun Heo73e80ed2013-08-13 11:01:55 -04001007 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001008 cgroup_css(cgroup_dummy_top, ss));
1009 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001010
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001011 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001013 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001014
Ben Blumcf5d5942010-03-10 15:22:09 -08001015 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001016 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001017 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001019 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1020 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001021
Paul Menageddbcc7e2007-10-18 23:39:30 -07001022 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001023 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001024
Tejun Heoca8bdca2013-08-26 18:40:56 -04001025 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001026 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1027
Tejun Heo9871bf92013-06-24 15:21:47 -07001028 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001029 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030 }
1031 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032
Tejun Heo1672d042013-06-25 18:04:54 -07001033 /*
1034 * Mark @root has finished binding subsystems. @root->subsys_mask
1035 * now matches the bound subsystems.
1036 */
1037 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1038
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 return 0;
1040}
1041
Al Viro34c80b12011-12-08 21:32:45 -05001042static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043{
Al Viro34c80b12011-12-08 21:32:45 -05001044 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001046 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047
Tejun Heob85d2042013-12-06 15:11:57 -05001048 for_each_subsys(ss, ssid)
1049 if (root->subsys_mask & (1 << ssid))
1050 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001051 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1052 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001053 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001055 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001056 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001057
1058 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001059 if (strlen(root->release_agent_path))
1060 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001061 spin_unlock(&release_agent_path_lock);
1062
Tejun Heo2260e7f2012-11-19 08:13:38 -08001063 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001064 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001065 if (strlen(root->name))
1066 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067 return 0;
1068}
1069
1070struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001071 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001073 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001074 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001075 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001076 /* User explicitly requested empty subsystem */
1077 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001078
1079 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001080
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081};
1082
Ben Blumaae8aab2010-03-10 15:22:07 -08001083/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001084 * Convert a hierarchy specifier into a bitmask of subsystems and
1085 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1086 * array. This function takes refcounts on subsystems to be used, unless it
1087 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001088 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001089static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001090{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001091 char *token, *o = data;
1092 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001093 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001094 struct cgroup_subsys *ss;
1095 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001096
Ben Blumaae8aab2010-03-10 15:22:07 -08001097 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1098
Li Zefanf9ab5b52009-06-17 16:26:33 -07001099#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001100 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001101#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102
Paul Menagec6d57f32009-09-23 15:56:19 -07001103 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104
1105 while ((token = strsep(&o, ",")) != NULL) {
1106 if (!*token)
1107 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001108 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001109 /* Explicitly have no subsystems */
1110 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001111 continue;
1112 }
1113 if (!strcmp(token, "all")) {
1114 /* Mutually exclusive option 'all' + subsystem name */
1115 if (one_ss)
1116 return -EINVAL;
1117 all_ss = true;
1118 continue;
1119 }
Tejun Heo873fe092013-04-14 20:15:26 -07001120 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1121 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1122 continue;
1123 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001124 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001125 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001126 continue;
1127 }
1128 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001129 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001130 continue;
1131 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001132 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001133 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001134 continue;
1135 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001136 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001137 /* Specifying two release agents is forbidden */
1138 if (opts->release_agent)
1139 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001140 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001141 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001142 if (!opts->release_agent)
1143 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001144 continue;
1145 }
1146 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001147 const char *name = token + 5;
1148 /* Can't specify an empty name */
1149 if (!strlen(name))
1150 return -EINVAL;
1151 /* Must match [\w.-]+ */
1152 for (i = 0; i < strlen(name); i++) {
1153 char c = name[i];
1154 if (isalnum(c))
1155 continue;
1156 if ((c == '.') || (c == '-') || (c == '_'))
1157 continue;
1158 return -EINVAL;
1159 }
1160 /* Specifying two names is forbidden */
1161 if (opts->name)
1162 return -EINVAL;
1163 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001164 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001165 GFP_KERNEL);
1166 if (!opts->name)
1167 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001168
1169 continue;
1170 }
1171
Tejun Heo30159ec2013-06-25 11:53:37 -07001172 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001173 if (strcmp(token, ss->name))
1174 continue;
1175 if (ss->disabled)
1176 continue;
1177
1178 /* Mutually exclusive option 'all' + subsystem name */
1179 if (all_ss)
1180 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001181 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001182 one_ss = true;
1183
1184 break;
1185 }
1186 if (i == CGROUP_SUBSYS_COUNT)
1187 return -ENOENT;
1188 }
1189
1190 /*
1191 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001192 * otherwise if 'none', 'name=' and a subsystem name options
1193 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001194 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001195 if (all_ss || (!one_ss && !opts->none && !opts->name))
1196 for_each_subsys(ss, i)
1197 if (!ss->disabled)
1198 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001199
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001200 /* Consistency checks */
1201
Tejun Heo873fe092013-04-14 20:15:26 -07001202 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1203 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1204
1205 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1206 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1207 return -EINVAL;
1208 }
1209
1210 if (opts->cpuset_clone_children) {
1211 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1212 return -EINVAL;
1213 }
1214 }
1215
Li Zefanf9ab5b52009-06-17 16:26:33 -07001216 /*
1217 * Option noprefix was introduced just for backward compatibility
1218 * with the old cpuset, so we allow noprefix only if mounting just
1219 * the cpuset subsystem.
1220 */
Tejun Heo93438622013-04-14 20:15:25 -07001221 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001222 return -EINVAL;
1223
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001224
1225 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001226 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001227 return -EINVAL;
1228
1229 /*
1230 * We either have to specify by name or by subsystems. (So all
1231 * empty hierarchies must have a name).
1232 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001233 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001234 return -EINVAL;
1235
1236 return 0;
1237}
1238
1239static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1240{
1241 int ret = 0;
1242 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001243 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001244 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001245 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001246
Tejun Heo873fe092013-04-14 20:15:26 -07001247 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1248 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1249 return -EINVAL;
1250 }
1251
Paul Menagebd89aab2007-10-18 23:40:44 -07001252 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001253 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001254 mutex_lock(&cgroup_mutex);
1255
1256 /* See what subsystems are wanted */
1257 ret = parse_cgroupfs_options(data, &opts);
1258 if (ret)
1259 goto out_unlock;
1260
Tejun Heoa8a648c2013-06-24 15:21:47 -07001261 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001262 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1263 task_tgid_nr(current), current->comm);
1264
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001265 added_mask = opts.subsys_mask & ~root->subsys_mask;
1266 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001267
Ben Blumcf5d5942010-03-10 15:22:09 -08001268 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001269 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001270 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001271 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1272 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1273 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001274 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001275 goto out_unlock;
1276 }
1277
Tejun Heof172e672013-06-28 17:07:30 -07001278 /* remounting is not allowed for populated hierarchies */
1279 if (root->number_of_cgroups > 1) {
1280 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001281 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001282 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001283
Paul Menageddbcc7e2007-10-18 23:39:30 -07001284 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001285 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001286 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001287
Tejun Heo69e943b2014-02-08 10:36:58 -05001288 if (opts.release_agent) {
1289 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001290 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001291 spin_unlock(&release_agent_path_lock);
1292 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001293 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001294 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001295 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001296 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001297 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001298 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001299 return ret;
1300}
1301
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001302static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001303 .statfs = simple_statfs,
1304 .drop_inode = generic_delete_inode,
1305 .show_options = cgroup_show_options,
1306 .remount_fs = cgroup_remount,
1307};
1308
Paul Menagecc31edc2008-10-18 20:28:04 -07001309static void init_cgroup_housekeeping(struct cgroup *cgrp)
1310{
1311 INIT_LIST_HEAD(&cgrp->sibling);
1312 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001313 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001314 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001315 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001316 INIT_LIST_HEAD(&cgrp->pidlists);
1317 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001318 cgrp->dummy_css.cgroup = cgrp;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001319 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001320}
Paul Menagec6d57f32009-09-23 15:56:19 -07001321
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322static void init_cgroup_root(struct cgroupfs_root *root)
1323{
Paul Menagebd89aab2007-10-18 23:40:44 -07001324 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001325
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326 INIT_LIST_HEAD(&root->root_list);
1327 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001328 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001329 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001330 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001331 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001332}
1333
Tejun Heofc76df72013-06-25 11:53:37 -07001334static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001335{
Tejun Heo1a574232013-04-14 11:36:58 -07001336 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001337
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001338 lockdep_assert_held(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001339
Tejun Heofc76df72013-06-25 11:53:37 -07001340 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1341 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001342 if (id < 0)
1343 return id;
1344
1345 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001346 return 0;
1347}
1348
1349static void cgroup_exit_root_id(struct cgroupfs_root *root)
1350{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001351 lockdep_assert_held(&cgroup_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001352
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001353 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001354 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001355 root->hierarchy_id = 0;
1356 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001357}
1358
Paul Menageddbcc7e2007-10-18 23:39:30 -07001359static int cgroup_test_super(struct super_block *sb, void *data)
1360{
Paul Menagec6d57f32009-09-23 15:56:19 -07001361 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001362 struct cgroupfs_root *root = sb->s_fs_info;
1363
Paul Menagec6d57f32009-09-23 15:56:19 -07001364 /* If we asked for a name then it must match */
1365 if (opts->name && strcmp(opts->name, root->name))
1366 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001368 /*
1369 * If we asked for subsystems (or explicitly for no
1370 * subsystems) then they must match
1371 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001372 if ((opts->subsys_mask || opts->none)
1373 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374 return 0;
1375
1376 return 1;
1377}
1378
Paul Menagec6d57f32009-09-23 15:56:19 -07001379static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1380{
1381 struct cgroupfs_root *root;
1382
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001383 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001384 return NULL;
1385
1386 root = kzalloc(sizeof(*root), GFP_KERNEL);
1387 if (!root)
1388 return ERR_PTR(-ENOMEM);
1389
1390 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001391
Tejun Heo1672d042013-06-25 18:04:54 -07001392 /*
1393 * We need to set @root->subsys_mask now so that @root can be
1394 * matched by cgroup_test_super() before it finishes
1395 * initialization; otherwise, competing mounts with the same
1396 * options may try to bind the same subsystems instead of waiting
1397 * for the first one leading to unexpected mount errors.
1398 * SUBSYS_BOUND will be set once actual binding is complete.
1399 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001400 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001401 root->flags = opts->flags;
1402 if (opts->release_agent)
1403 strcpy(root->release_agent_path, opts->release_agent);
1404 if (opts->name)
1405 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001406 if (opts->cpuset_clone_children)
1407 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001408 return root;
1409}
1410
Tejun Heofa3ca072013-04-14 11:36:56 -07001411static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001412{
Tejun Heofa3ca072013-04-14 11:36:56 -07001413 if (root) {
1414 /* hierarhcy ID shoulid already have been released */
1415 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001416
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001417 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001418 kfree(root);
1419 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001420}
1421
Paul Menageddbcc7e2007-10-18 23:39:30 -07001422static int cgroup_set_super(struct super_block *sb, void *data)
1423{
1424 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001425 struct cgroup_sb_opts *opts = data;
1426
1427 /* If we don't have a new root, we can't set up a new sb */
1428 if (!opts->new_root)
1429 return -EINVAL;
1430
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001431 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001432
1433 ret = set_anon_super(sb, NULL);
1434 if (ret)
1435 return ret;
1436
Paul Menagec6d57f32009-09-23 15:56:19 -07001437 sb->s_fs_info = opts->new_root;
1438 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001439
1440 sb->s_blocksize = PAGE_CACHE_SIZE;
1441 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1442 sb->s_magic = CGROUP_SUPER_MAGIC;
1443 sb->s_op = &cgroup_ops;
1444
1445 return 0;
1446}
1447
1448static int cgroup_get_rootdir(struct super_block *sb)
1449{
Al Viro0df6a632010-12-21 13:29:29 -05001450 static const struct dentry_operations cgroup_dops = {
1451 .d_iput = cgroup_diput,
Al Virob26d4cd2013-10-25 18:47:37 -04001452 .d_delete = always_delete_dentry,
Al Viro0df6a632010-12-21 13:29:29 -05001453 };
1454
Paul Menageddbcc7e2007-10-18 23:39:30 -07001455 struct inode *inode =
1456 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001457
1458 if (!inode)
1459 return -ENOMEM;
1460
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461 inode->i_fop = &simple_dir_operations;
1462 inode->i_op = &cgroup_dir_inode_operations;
1463 /* directories start off with i_nlink == 2 (for "." entry) */
1464 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001465 sb->s_root = d_make_root(inode);
1466 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001467 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001468 /* for everything else we want ->d_op set */
1469 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001470 return 0;
1471}
1472
Tejun Heod427dfe2014-02-11 11:52:48 -05001473static int cgroup_setup_root(struct cgroupfs_root *root)
1474{
1475 LIST_HEAD(tmp_links);
1476 struct super_block *sb = root->sb;
1477 struct cgroup *root_cgrp = &root->top_cgroup;
1478 struct cgroupfs_root *existing_root;
1479 struct css_set *cset;
1480 struct inode *inode;
1481 const struct cred *cred;
1482 int i, ret;
1483
1484 lockdep_assert_held(&cgroup_tree_mutex);
1485 lockdep_assert_held(&cgroup_mutex);
1486 BUG_ON(sb->s_root != NULL);
1487
1488 mutex_unlock(&cgroup_mutex);
1489 mutex_unlock(&cgroup_tree_mutex);
1490
1491 ret = cgroup_get_rootdir(sb);
1492 if (ret) {
1493 mutex_lock(&cgroup_tree_mutex);
1494 mutex_lock(&cgroup_mutex);
1495 return ret;
1496 }
1497 inode = sb->s_root->d_inode;
1498
1499 mutex_lock(&inode->i_mutex);
1500 mutex_lock(&cgroup_tree_mutex);
1501 mutex_lock(&cgroup_mutex);
1502
1503 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1504 if (ret < 0)
1505 goto out_unlock;
1506 root_cgrp->id = ret;
1507
1508 /* check for name clashes with existing mounts */
1509 ret = -EBUSY;
1510 if (strlen(root->name))
1511 for_each_active_root(existing_root)
1512 if (!strcmp(existing_root->name, root->name))
1513 goto out_unlock;
1514
1515 /*
1516 * We're accessing css_set_count without locking css_set_lock here,
1517 * but that's OK - it can only be increased by someone holding
1518 * cgroup_lock, and that's us. The worst that can happen is that we
1519 * have some link structures left over
1520 */
1521 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1522 if (ret)
1523 goto out_unlock;
1524
1525 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1526 ret = cgroup_init_root_id(root, 2, 0);
1527 if (ret)
1528 goto out_unlock;
1529
1530 sb->s_root->d_fsdata = root_cgrp;
1531 root_cgrp->dentry = sb->s_root;
1532
1533 /*
1534 * We're inside get_sb() and will call lookup_one_len() to create
1535 * the root files, which doesn't work if SELinux is in use. The
1536 * following cred dancing somehow works around it. See 2ce9738ba
1537 * ("cgroupfs: use init_cred when populating new cgroupfs mount")
1538 * for more details.
1539 */
1540 cred = override_creds(&init_cred);
1541
1542 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1543 if (ret)
1544 goto rm_base_files;
1545
1546 ret = rebind_subsystems(root, root->subsys_mask, 0);
1547 if (ret)
1548 goto rm_base_files;
1549
1550 revert_creds(cred);
1551
1552 /*
1553 * There must be no failure case after here, since rebinding takes
1554 * care of subsystems' refcounts, which are explicitly dropped in
1555 * the failure exit path.
1556 */
1557 list_add(&root->root_list, &cgroup_roots);
1558 cgroup_root_count++;
1559
1560 /*
1561 * Link the top cgroup in this hierarchy into all the css_set
1562 * objects.
1563 */
1564 write_lock(&css_set_lock);
1565 hash_for_each(css_set_table, i, cset, hlist)
1566 link_css_set(&tmp_links, cset, root_cgrp);
1567 write_unlock(&css_set_lock);
1568
1569 BUG_ON(!list_empty(&root_cgrp->children));
1570 BUG_ON(root->number_of_cgroups != 1);
1571
1572 ret = 0;
1573 goto out_unlock;
1574
1575rm_base_files:
1576 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
1577 revert_creds(cred);
1578 cgroup_exit_root_id(root);
1579out_unlock:
1580 mutex_unlock(&inode->i_mutex);
1581 free_cgrp_cset_links(&tmp_links);
1582 return ret;
1583}
1584
Al Virof7e83572010-07-26 13:23:11 +04001585static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001587 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588{
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001589 struct super_block *sb = NULL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001590 struct cgroupfs_root *root = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001591 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001592 struct cgroupfs_root *new_root;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001593 int ret;
1594
1595 mutex_lock(&cgroup_tree_mutex);
1596 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001597
1598 /* First find the desired set of subsystems */
1599 ret = parse_cgroupfs_options(data, &opts);
Paul Menagec6d57f32009-09-23 15:56:19 -07001600 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001601 goto out_unlock;
Paul Menagec6d57f32009-09-23 15:56:19 -07001602
1603 /*
1604 * Allocate a new cgroup root. We may not need it if we're
1605 * reusing an existing hierarchy.
1606 */
1607 new_root = cgroup_root_from_opts(&opts);
1608 if (IS_ERR(new_root)) {
1609 ret = PTR_ERR(new_root);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001610 goto out_unlock;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001611 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001612 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613
Paul Menagec6d57f32009-09-23 15:56:19 -07001614 /* Locate an existing or new sb for this hierarchy */
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001615 mutex_unlock(&cgroup_mutex);
1616 mutex_unlock(&cgroup_tree_mutex);
David Howells9249e172012-06-25 12:55:37 +01001617 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001618 mutex_lock(&cgroup_tree_mutex);
1619 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001620 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001621 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001622 cgroup_free_root(opts.new_root);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001623 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624 }
1625
Paul Menagec6d57f32009-09-23 15:56:19 -07001626 root = sb->s_fs_info;
1627 BUG_ON(!root);
1628 if (root == opts.new_root) {
Tejun Heod427dfe2014-02-11 11:52:48 -05001629 ret = cgroup_setup_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001631 goto out_unlock;
Paul Menagec6d57f32009-09-23 15:56:19 -07001632 } else {
1633 /*
1634 * We re-used an existing hierarchy - the new root (if
1635 * any) is not needed
1636 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001637 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001638
Tejun Heoc7ba8282013-06-29 14:06:10 -07001639 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001640 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1641 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1642 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001643 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001644 } else {
1645 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1646 }
Tejun Heo873fe092013-04-14 20:15:26 -07001647 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001648 }
1649
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001650 ret = 0;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001651out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001652 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001653 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001654
1655 if (ret && !IS_ERR_OR_NULL(sb))
1656 deactivate_locked_super(sb);
1657
Paul Menagec6d57f32009-09-23 15:56:19 -07001658 kfree(opts.release_agent);
1659 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001660
1661 if (!ret)
1662 return dget(sb->s_root);
1663 else
1664 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665}
1666
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001667static void cgroup_kill_sb(struct super_block *sb)
1668{
Paul Menageddbcc7e2007-10-18 23:39:30 -07001669 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001670 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001671 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672 int ret;
1673
1674 BUG_ON(!root);
1675
1676 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001677 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001678
Tejun Heo31261212013-06-28 17:07:30 -07001679 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001680 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001681 mutex_lock(&cgroup_mutex);
1682
1683 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001684 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1685 ret = rebind_subsystems(root, 0, root->subsys_mask);
1686 /* Shouldn't be able to fail ... */
1687 BUG_ON(ret);
1688 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001689
Paul Menage817929e2007-10-18 23:39:36 -07001690 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001691 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001692 * root cgroup
1693 */
1694 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001695
Tejun Heo69d02062013-06-12 21:04:50 -07001696 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1697 list_del(&link->cset_link);
1698 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001699 kfree(link);
1700 }
1701 write_unlock(&css_set_lock);
1702
Paul Menage839ec542009-01-29 14:25:22 -08001703 if (!list_empty(&root->root_list)) {
1704 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001705 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001706 }
Li Zefane5f6a862009-01-07 18:07:41 -08001707
Tejun Heofa3ca072013-04-14 11:36:56 -07001708 cgroup_exit_root_id(root);
1709
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001711 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001712 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001713
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001714 simple_xattrs_free(&cgrp->xattrs);
1715
Paul Menageddbcc7e2007-10-18 23:39:30 -07001716 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001717 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718}
1719
1720static struct file_system_type cgroup_fs_type = {
1721 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001722 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723 .kill_sb = cgroup_kill_sb,
1724};
1725
Greg KH676db4a2010-08-05 13:53:35 -07001726static struct kobject *cgroup_kobj;
1727
Li Zefana043e3b2008-02-23 15:24:09 -08001728/**
1729 * cgroup_path - generate the path of a cgroup
1730 * @cgrp: the cgroup in question
1731 * @buf: the buffer to write the path into
1732 * @buflen: the length of the buffer
1733 *
Li Zefan65dff752013-03-01 15:01:56 +08001734 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1735 *
1736 * We can't generate cgroup path using dentry->d_name, as accessing
1737 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1738 * inode's i_mutex, while on the other hand cgroup_path() can be called
1739 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001741int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001742{
Li Zefan65dff752013-03-01 15:01:56 +08001743 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001745
Tejun Heoda1f2962013-04-14 10:32:19 -07001746 if (!cgrp->parent) {
1747 if (strlcpy(buf, "/", buflen) >= buflen)
1748 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749 return 0;
1750 }
1751
Tao Ma316eb662012-11-08 21:36:38 +08001752 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001753 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001754
Li Zefan65dff752013-03-01 15:01:56 +08001755 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001756 do {
Li Zefan65dff752013-03-01 15:01:56 +08001757 const char *name = cgroup_name(cgrp);
1758 int len;
1759
1760 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001762 goto out;
1763 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001764
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001766 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001768
1769 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001770 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001771 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001773out:
1774 rcu_read_unlock();
1775 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776}
Ben Blum67523c42010-03-10 15:22:11 -08001777EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778
Tejun Heo857a2be2013-04-14 20:50:08 -07001779/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001780 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001781 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001782 * @buf: the buffer to write the path into
1783 * @buflen: the length of the buffer
1784 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001785 * Determine @task's cgroup on the first (the one with the lowest non-zero
1786 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1787 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1788 * cgroup controller callbacks.
1789 *
1790 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001791 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001792int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001793{
1794 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001795 struct cgroup *cgrp;
1796 int hierarchy_id = 1, ret = 0;
1797
1798 if (buflen < 2)
1799 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001800
1801 mutex_lock(&cgroup_mutex);
1802
Tejun Heo913ffdb2013-07-11 16:34:48 -07001803 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1804
Tejun Heo857a2be2013-04-14 20:50:08 -07001805 if (root) {
1806 cgrp = task_cgroup_from_root(task, root);
1807 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001808 } else {
1809 /* if no hierarchy exists, everyone is in "/" */
1810 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001811 }
1812
1813 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001814 return ret;
1815}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001816EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001817
Ben Blum74a11662011-05-26 16:25:20 -07001818/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001819 * Control Group taskset
1820 */
Tejun Heo134d3372011-12-12 18:12:21 -08001821struct task_and_cgroup {
1822 struct task_struct *task;
1823 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001824 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001825};
1826
Tejun Heo2f7ee562011-12-12 18:12:21 -08001827struct cgroup_taskset {
1828 struct task_and_cgroup single;
1829 struct flex_array *tc_array;
1830 int tc_array_len;
1831 int idx;
1832 struct cgroup *cur_cgrp;
1833};
1834
1835/**
1836 * cgroup_taskset_first - reset taskset and return the first task
1837 * @tset: taskset of interest
1838 *
1839 * @tset iteration is initialized and the first task is returned.
1840 */
1841struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1842{
1843 if (tset->tc_array) {
1844 tset->idx = 0;
1845 return cgroup_taskset_next(tset);
1846 } else {
1847 tset->cur_cgrp = tset->single.cgrp;
1848 return tset->single.task;
1849 }
1850}
1851EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1852
1853/**
1854 * cgroup_taskset_next - iterate to the next task in taskset
1855 * @tset: taskset of interest
1856 *
1857 * Return the next task in @tset. Iteration must have been initialized
1858 * with cgroup_taskset_first().
1859 */
1860struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1861{
1862 struct task_and_cgroup *tc;
1863
1864 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1865 return NULL;
1866
1867 tc = flex_array_get(tset->tc_array, tset->idx++);
1868 tset->cur_cgrp = tc->cgrp;
1869 return tc->task;
1870}
1871EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1872
1873/**
Tejun Heod99c8722013-08-08 20:11:27 -04001874 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001875 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001876 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001877 *
Tejun Heod99c8722013-08-08 20:11:27 -04001878 * Return the css for the current (last returned) task of @tset for
1879 * subsystem specified by @subsys_id. This function must be preceded by
1880 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001881 */
Tejun Heod99c8722013-08-08 20:11:27 -04001882struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1883 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001884{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001885 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001886}
Tejun Heod99c8722013-08-08 20:11:27 -04001887EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001888
1889/**
1890 * cgroup_taskset_size - return the number of tasks in taskset
1891 * @tset: taskset of interest
1892 */
1893int cgroup_taskset_size(struct cgroup_taskset *tset)
1894{
1895 return tset->tc_array ? tset->tc_array_len : 1;
1896}
1897EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1898
1899
Ben Blum74a11662011-05-26 16:25:20 -07001900/*
1901 * cgroup_task_migrate - move a task from one cgroup to another.
1902 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001903 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001904 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001905static void cgroup_task_migrate(struct cgroup *old_cgrp,
1906 struct task_struct *tsk,
1907 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001908{
Tejun Heo5abb8852013-06-12 21:04:49 -07001909 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001910
1911 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001912 * We are synchronized through threadgroup_lock() against PF_EXITING
1913 * setting such that we can't race against cgroup_exit() changing the
1914 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001915 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001916 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001917 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001918
Ben Blum74a11662011-05-26 16:25:20 -07001919 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001920 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001921 task_unlock(tsk);
1922
1923 /* Update the css_set linked lists if we're using them */
1924 write_lock(&css_set_lock);
1925 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001926 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001927 write_unlock(&css_set_lock);
1928
1929 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001930 * We just gained a reference on old_cset by taking it from the
1931 * task. As trading it for new_cset is protected by cgroup_mutex,
1932 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001933 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001934 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1935 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001936}
1937
Li Zefana043e3b2008-02-23 15:24:09 -08001938/**
Li Zefan081aa452013-03-13 09:17:09 +08001939 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001940 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001941 * @tsk: the task or the leader of the threadgroup to be attached
1942 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001943 *
Tejun Heo257058a2011-12-12 18:12:21 -08001944 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001945 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001946 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001947static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1948 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001949{
1950 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001951 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001952 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001953 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001954 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001955 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001956 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001957 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001958
1959 /*
1960 * step 0: in order to do expensive, possibly blocking operations for
1961 * every thread, we cannot iterate the thread group list, since it needs
1962 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001963 * group - group_rwsem prevents new threads from appearing, and if
1964 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001965 */
Li Zefan081aa452013-03-13 09:17:09 +08001966 if (threadgroup)
1967 group_size = get_nr_threads(tsk);
1968 else
1969 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001970 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001971 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001972 if (!group)
1973 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001974 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001975 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001976 if (retval)
1977 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001978
Ben Blum74a11662011-05-26 16:25:20 -07001979 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001980 /*
1981 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1982 * already PF_EXITING could be freed from underneath us unless we
1983 * take an rcu_read_lock.
1984 */
1985 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001986 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001987 struct task_and_cgroup ent;
1988
Tejun Heocd3d0952011-12-12 18:12:21 -08001989 /* @tsk either already exited or can't exit until the end */
1990 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001991 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001992
Ben Blum74a11662011-05-26 16:25:20 -07001993 /* as per above, nr_threads may decrease, but not increase. */
1994 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001995 ent.task = tsk;
1996 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001997 /* nothing to do if this task is already in the cgroup */
1998 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001999 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002000 /*
2001 * saying GFP_ATOMIC has no effect here because we did prealloc
2002 * earlier, but it's good form to communicate our expectations.
2003 */
Tejun Heo134d3372011-12-12 18:12:21 -08002004 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002005 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002006 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08002007 next:
Li Zefan081aa452013-03-13 09:17:09 +08002008 if (!threadgroup)
2009 break;
Ben Blum74a11662011-05-26 16:25:20 -07002010 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002011 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002012 /* remember the number of threads in the array for later. */
2013 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002014 tset.tc_array = group;
2015 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002016
Tejun Heo134d3372011-12-12 18:12:21 -08002017 /* methods shouldn't be called if no task is actually migrating */
2018 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002019 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002020 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002021
Ben Blum74a11662011-05-26 16:25:20 -07002022 /*
2023 * step 1: check that we can legitimately attach to the cgroup.
2024 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05002025 for_each_css(css, i, cgrp) {
2026 if (css->ss->can_attach) {
2027 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002028 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002029 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002030 goto out_cancel_attach;
2031 }
2032 }
Ben Blum74a11662011-05-26 16:25:20 -07002033 }
2034
2035 /*
2036 * step 2: make sure css_sets exist for all threads to be migrated.
2037 * we use find_css_set, which allocates a new one if necessary.
2038 */
Ben Blum74a11662011-05-26 16:25:20 -07002039 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002040 struct css_set *old_cset;
2041
Tejun Heo134d3372011-12-12 18:12:21 -08002042 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002043 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002044 tc->cset = find_css_set(old_cset, cgrp);
2045 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002046 retval = -ENOMEM;
2047 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002048 }
2049 }
2050
2051 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002052 * step 3: now that we're guaranteed success wrt the css_sets,
2053 * proceed to move all tasks to the new cgroup. There are no
2054 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002055 */
Ben Blum74a11662011-05-26 16:25:20 -07002056 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002057 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002058 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002059 }
2060 /* nothing is sensitive to fork() after this point. */
2061
2062 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002063 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002064 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05002065 for_each_css(css, i, cgrp)
2066 if (css->ss->attach)
2067 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002068
2069 /*
2070 * step 5: success! and cleanup
2071 */
Ben Blum74a11662011-05-26 16:25:20 -07002072 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002073out_put_css_set_refs:
2074 if (retval) {
2075 for (i = 0; i < group_size; i++) {
2076 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002077 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002078 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002079 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002080 }
Ben Blum74a11662011-05-26 16:25:20 -07002081 }
2082out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002083 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002084 for_each_css(css, i, cgrp) {
2085 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07002086 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05002087 if (css->ss->cancel_attach)
2088 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002089 }
2090 }
Ben Blum74a11662011-05-26 16:25:20 -07002091out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002092 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002093 return retval;
2094}
2095
2096/*
2097 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002098 * function to attach either it or all tasks in its threadgroup. Will lock
2099 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002100 */
2101static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002102{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002103 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002104 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002105 int ret;
2106
Ben Blum74a11662011-05-26 16:25:20 -07002107 if (!cgroup_lock_live_group(cgrp))
2108 return -ENODEV;
2109
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002110retry_find_task:
2111 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002112 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002113 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002114 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002115 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002116 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002117 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002118 }
Ben Blum74a11662011-05-26 16:25:20 -07002119 /*
2120 * even if we're attaching all tasks in the thread group, we
2121 * only need to check permissions on one of them.
2122 */
David Howellsc69e8d92008-11-14 10:39:19 +11002123 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002124 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2125 !uid_eq(cred->euid, tcred->uid) &&
2126 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002127 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002128 ret = -EACCES;
2129 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002130 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002131 } else
2132 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002133
2134 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002135 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002136
2137 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002138 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002139 * trapped in a cpuset, or RT worker may be born in a cgroup
2140 * with no rt_runtime allocated. Just say no.
2141 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002142 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002143 ret = -EINVAL;
2144 rcu_read_unlock();
2145 goto out_unlock_cgroup;
2146 }
2147
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002148 get_task_struct(tsk);
2149 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002150
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002151 threadgroup_lock(tsk);
2152 if (threadgroup) {
2153 if (!thread_group_leader(tsk)) {
2154 /*
2155 * a race with de_thread from another thread's exec()
2156 * may strip us of our leadership, if this happens,
2157 * there is no choice but to throw this task away and
2158 * try again; this is
2159 * "double-double-toil-and-trouble-check locking".
2160 */
2161 threadgroup_unlock(tsk);
2162 put_task_struct(tsk);
2163 goto retry_find_task;
2164 }
Li Zefan081aa452013-03-13 09:17:09 +08002165 }
2166
2167 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2168
Tejun Heocd3d0952011-12-12 18:12:21 -08002169 threadgroup_unlock(tsk);
2170
Paul Menagebbcb81d2007-10-18 23:39:32 -07002171 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002172out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002173 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002174 return ret;
2175}
2176
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002177/**
2178 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2179 * @from: attach to all cgroups of a given task
2180 * @tsk: the task to be attached
2181 */
2182int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2183{
2184 struct cgroupfs_root *root;
2185 int retval = 0;
2186
Tejun Heo47cfcd02013-04-07 09:29:51 -07002187 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002188 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002189 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002190
Li Zefan6f4b7e62013-07-31 16:18:36 +08002191 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002192 if (retval)
2193 break;
2194 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002195 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002196
2197 return retval;
2198}
2199EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2200
Tejun Heo182446d2013-08-08 20:11:24 -04002201static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2202 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002203{
Tejun Heo182446d2013-08-08 20:11:24 -04002204 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002205}
2206
Tejun Heo182446d2013-08-08 20:11:24 -04002207static int cgroup_procs_write(struct cgroup_subsys_state *css,
2208 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002209{
Tejun Heo182446d2013-08-08 20:11:24 -04002210 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002211}
2212
Tejun Heo182446d2013-08-08 20:11:24 -04002213static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2214 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002215{
Tejun Heo5f469902014-02-11 11:52:48 -05002216 struct cgroupfs_root *root = css->cgroup->root;
2217
2218 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002219 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002220 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002221 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002222 strlcpy(root->release_agent_path, buffer,
2223 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002224 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002225 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002226 return 0;
2227}
2228
Tejun Heo2da8ca82013-12-05 12:28:04 -05002229static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002230{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002231 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002232
Paul Menagee788e062008-07-25 01:46:59 -07002233 if (!cgroup_lock_live_group(cgrp))
2234 return -ENODEV;
2235 seq_puts(seq, cgrp->root->release_agent_path);
2236 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002237 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002238 return 0;
2239}
2240
Tejun Heo2da8ca82013-12-05 12:28:04 -05002241static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002242{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002243 struct cgroup *cgrp = seq_css(seq)->cgroup;
2244
2245 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002246 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002247}
2248
Tejun Heoa742c592013-12-05 12:28:03 -05002249static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
Tejun Heo182446d2013-08-08 20:11:24 -04002250 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002251{
Tejun Heo182446d2013-08-08 20:11:24 -04002252 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002253 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002254 struct cgroup_subsys_state *css = cfe->css;
Tejun Heo5f469902014-02-11 11:52:48 -05002255 size_t max_bytes = max(cft->max_write_len, PAGE_SIZE);
Tejun Heoa742c592013-12-05 12:28:03 -05002256 char *buf;
2257 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002258
Tejun Heo5f469902014-02-11 11:52:48 -05002259 if (nbytes > max_bytes)
Tejun Heoa742c592013-12-05 12:28:03 -05002260 return -E2BIG;
2261
2262 buf = kmalloc(nbytes + 1, GFP_KERNEL);
2263 if (!buf)
2264 return -ENOMEM;
2265
2266 if (copy_from_user(buf, userbuf, nbytes)) {
2267 ret = -EFAULT;
2268 goto out_free;
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002269 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002270
Tejun Heoa742c592013-12-05 12:28:03 -05002271 buf[nbytes] = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07002272
Tejun Heoa742c592013-12-05 12:28:03 -05002273 if (cft->write_string) {
2274 ret = cft->write_string(css, cft, strstrip(buf));
2275 } else if (cft->write_u64) {
2276 unsigned long long v;
2277 ret = kstrtoull(buf, 0, &v);
2278 if (!ret)
2279 ret = cft->write_u64(css, cft, v);
2280 } else if (cft->write_s64) {
2281 long long v;
2282 ret = kstrtoll(buf, 0, &v);
2283 if (!ret)
2284 ret = cft->write_s64(css, cft, v);
2285 } else if (cft->trigger) {
2286 ret = cft->trigger(css, (unsigned int)cft->private);
2287 } else {
2288 ret = -EINVAL;
2289 }
2290out_free:
2291 kfree(buf);
2292 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002293}
2294
Paul Menage91796562008-04-29 01:00:01 -07002295/*
2296 * seqfile ops/methods for returning structured data. Currently just
2297 * supports string->u64 maps, but can be extended in future.
2298 */
2299
Tejun Heo6612f052013-12-05 12:28:04 -05002300static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002301{
Tejun Heo6612f052013-12-05 12:28:04 -05002302 struct cftype *cft = seq_cft(seq);
2303
2304 if (cft->seq_start) {
2305 return cft->seq_start(seq, ppos);
2306 } else {
2307 /*
2308 * The same behavior and code as single_open(). Returns
2309 * !NULL if pos is at the beginning; otherwise, NULL.
2310 */
2311 return NULL + !*ppos;
2312 }
2313}
2314
2315static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2316{
2317 struct cftype *cft = seq_cft(seq);
2318
2319 if (cft->seq_next) {
2320 return cft->seq_next(seq, v, ppos);
2321 } else {
2322 /*
2323 * The same behavior and code as single_open(), always
2324 * terminate after the initial read.
2325 */
2326 ++*ppos;
2327 return NULL;
2328 }
2329}
2330
2331static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2332{
2333 struct cftype *cft = seq_cft(seq);
2334
2335 if (cft->seq_stop)
2336 cft->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002337}
2338
2339static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2340{
Tejun Heo7da11272013-12-05 12:28:04 -05002341 struct cftype *cft = seq_cft(m);
2342 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002343
Tejun Heo2da8ca82013-12-05 12:28:04 -05002344 if (cft->seq_show)
2345 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002346
Tejun Heo896f5192013-12-05 12:28:04 -05002347 if (cft->read_u64)
2348 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2349 else if (cft->read_s64)
2350 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2351 else
2352 return -EINVAL;
2353 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002354}
2355
Tejun Heo6612f052013-12-05 12:28:04 -05002356static struct seq_operations cgroup_seq_operations = {
2357 .start = cgroup_seqfile_start,
2358 .next = cgroup_seqfile_next,
2359 .stop = cgroup_seqfile_stop,
2360 .show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002361};
2362
Paul Menageddbcc7e2007-10-18 23:39:30 -07002363static int cgroup_file_open(struct inode *inode, struct file *file)
2364{
Tejun Heof7d58812013-08-08 20:11:23 -04002365 struct cfent *cfe = __d_cfe(file->f_dentry);
2366 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002367 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2368 struct cgroup_subsys_state *css;
Tejun Heo6612f052013-12-05 12:28:04 -05002369 struct cgroup_open_file *of;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002370 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002371
2372 err = generic_file_open(inode, file);
2373 if (err)
2374 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002375
2376 /*
2377 * If the file belongs to a subsystem, pin the css. Will be
2378 * unpinned either on open failure or release. This ensures that
2379 * @css stays alive for all file operations.
2380 */
Tejun Heo105347b2013-08-13 11:01:55 -04002381 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002382 css = cgroup_css(cgrp, cft->ss);
2383 if (cft->ss && !css_tryget(css))
2384 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002385 rcu_read_unlock();
2386
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002387 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002388 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002389
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002390 /*
2391 * @cfe->css is used by read/write/close to determine the
2392 * associated css. @file->private_data would be a better place but
2393 * that's already used by seqfile. Multiple accessors may use it
2394 * simultaneously which is okay as the association never changes.
2395 */
2396 WARN_ON_ONCE(cfe->css && cfe->css != css);
2397 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002398
Tejun Heo6612f052013-12-05 12:28:04 -05002399 of = __seq_open_private(file, &cgroup_seq_operations,
2400 sizeof(struct cgroup_open_file));
2401 if (of) {
2402 of->cfe = cfe;
2403 return 0;
Li Zefane0798ce2013-07-31 17:36:25 +08002404 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002405
Tejun Heo6612f052013-12-05 12:28:04 -05002406 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002407 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002408 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002409}
2410
2411static int cgroup_file_release(struct inode *inode, struct file *file)
2412{
Tejun Heof7d58812013-08-08 20:11:23 -04002413 struct cfent *cfe = __d_cfe(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002414 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002415
Tejun Heo67f4c362013-08-08 20:11:24 -04002416 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002417 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002418 return seq_release_private(inode, file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002419}
2420
2421/*
2422 * cgroup_rename - Only allow simple rename of directories in place.
2423 */
2424static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2425 struct inode *new_dir, struct dentry *new_dentry)
2426{
Li Zefan65dff752013-03-01 15:01:56 +08002427 int ret;
2428 struct cgroup_name *name, *old_name;
2429 struct cgroup *cgrp;
2430
2431 /*
2432 * It's convinient to use parent dir's i_mutex to protected
2433 * cgrp->name.
2434 */
2435 lockdep_assert_held(&old_dir->i_mutex);
2436
Paul Menageddbcc7e2007-10-18 23:39:30 -07002437 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2438 return -ENOTDIR;
2439 if (new_dentry->d_inode)
2440 return -EEXIST;
2441 if (old_dir != new_dir)
2442 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002443
2444 cgrp = __d_cgrp(old_dentry);
2445
Tejun Heo6db8e852013-06-14 11:18:22 -07002446 /*
2447 * This isn't a proper migration and its usefulness is very
2448 * limited. Disallow if sane_behavior.
2449 */
2450 if (cgroup_sane_behavior(cgrp))
2451 return -EPERM;
2452
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002453 name = cgroup_alloc_name(new_dentry->d_name.name);
Li Zefan65dff752013-03-01 15:01:56 +08002454 if (!name)
2455 return -ENOMEM;
2456
2457 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2458 if (ret) {
2459 kfree(name);
2460 return ret;
2461 }
2462
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002463 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002464 rcu_assign_pointer(cgrp->name, name);
2465
2466 kfree_rcu(old_name, rcu_head);
2467 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002468}
2469
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002470static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2471{
2472 if (S_ISDIR(dentry->d_inode->i_mode))
2473 return &__d_cgrp(dentry)->xattrs;
2474 else
Li Zefan712317a2013-04-18 23:09:52 -07002475 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002476}
2477
2478static inline int xattr_enabled(struct dentry *dentry)
2479{
2480 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002481 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002482}
2483
2484static bool is_valid_xattr(const char *name)
2485{
2486 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2487 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2488 return true;
2489 return false;
2490}
2491
2492static int cgroup_setxattr(struct dentry *dentry, const char *name,
2493 const void *val, size_t size, int flags)
2494{
2495 if (!xattr_enabled(dentry))
2496 return -EOPNOTSUPP;
2497 if (!is_valid_xattr(name))
2498 return -EINVAL;
2499 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2500}
2501
2502static int cgroup_removexattr(struct dentry *dentry, const char *name)
2503{
2504 if (!xattr_enabled(dentry))
2505 return -EOPNOTSUPP;
2506 if (!is_valid_xattr(name))
2507 return -EINVAL;
2508 return simple_xattr_remove(__d_xattrs(dentry), name);
2509}
2510
2511static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2512 void *buf, size_t size)
2513{
2514 if (!xattr_enabled(dentry))
2515 return -EOPNOTSUPP;
2516 if (!is_valid_xattr(name))
2517 return -EINVAL;
2518 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2519}
2520
2521static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2522{
2523 if (!xattr_enabled(dentry))
2524 return -EOPNOTSUPP;
2525 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2526}
2527
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002528static const struct file_operations cgroup_file_operations = {
Tejun Heo896f5192013-12-05 12:28:04 -05002529 .read = seq_read,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002530 .write = cgroup_file_write,
2531 .llseek = generic_file_llseek,
2532 .open = cgroup_file_open,
2533 .release = cgroup_file_release,
2534};
2535
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002536static const struct inode_operations cgroup_file_inode_operations = {
2537 .setxattr = cgroup_setxattr,
2538 .getxattr = cgroup_getxattr,
2539 .listxattr = cgroup_listxattr,
2540 .removexattr = cgroup_removexattr,
2541};
2542
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002543static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002544 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002545 .mkdir = cgroup_mkdir,
2546 .rmdir = cgroup_rmdir,
2547 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002548 .setxattr = cgroup_setxattr,
2549 .getxattr = cgroup_getxattr,
2550 .listxattr = cgroup_listxattr,
2551 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002552};
2553
Al Viroa5e7ed32011-07-26 01:55:55 -04002554static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002555 struct super_block *sb)
2556{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002557 struct inode *inode;
2558
2559 if (!dentry)
2560 return -ENOENT;
2561 if (dentry->d_inode)
2562 return -EEXIST;
2563
2564 inode = cgroup_new_inode(mode, sb);
2565 if (!inode)
2566 return -ENOMEM;
2567
2568 if (S_ISDIR(mode)) {
2569 inode->i_op = &cgroup_dir_inode_operations;
2570 inode->i_fop = &simple_dir_operations;
2571
2572 /* start off with i_nlink == 2 (for "." entry) */
2573 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002574 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002575
Tejun Heob8a2df62012-11-19 08:13:37 -08002576 /*
2577 * Control reaches here with cgroup_mutex held.
2578 * @inode->i_mutex should nest outside cgroup_mutex but we
2579 * want to populate it immediately without releasing
2580 * cgroup_mutex. As @inode isn't visible to anyone else
2581 * yet, trylock will always succeed without affecting
2582 * lockdep checks.
2583 */
2584 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002585 } else if (S_ISREG(mode)) {
2586 inode->i_size = 0;
2587 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002588 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002589 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002590 d_instantiate(dentry, inode);
2591 dget(dentry); /* Extra count - pin the dentry in core */
2592 return 0;
2593}
2594
Li Zefan099fca32009-04-02 16:57:29 -07002595/**
2596 * cgroup_file_mode - deduce file mode of a control file
2597 * @cft: the control file in question
2598 *
2599 * returns cft->mode if ->mode is not 0
2600 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2601 * returns S_IRUGO if it has only a read handler
2602 * returns S_IWUSR if it has only a write hander
2603 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002604static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002605{
Al Viroa5e7ed32011-07-26 01:55:55 -04002606 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002607
2608 if (cft->mode)
2609 return cft->mode;
2610
Tejun Heo2da8ca82013-12-05 12:28:04 -05002611 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
Li Zefan099fca32009-04-02 16:57:29 -07002612 mode |= S_IRUGO;
2613
Tejun Heo6e0755b2013-12-05 12:28:03 -05002614 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
2615 cft->trigger)
Li Zefan099fca32009-04-02 16:57:29 -07002616 mode |= S_IWUSR;
2617
2618 return mode;
2619}
2620
Tejun Heo2bb566c2013-08-08 20:11:23 -04002621static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002622{
Paul Menagebd89aab2007-10-18 23:40:44 -07002623 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002624 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002625 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002626 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002627 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002628 umode_t mode;
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002629 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo05ef1d72012-04-01 12:09:56 -07002630
Paul Menageddbcc7e2007-10-18 23:39:30 -07002631 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002632
2633 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2634 if (!cfe)
2635 return -ENOMEM;
2636
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002637 cgroup_file_name(cgrp, cft, name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002638 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002639 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002640 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002641 goto out;
2642 }
2643
Li Zefand6cbf352013-05-14 19:44:20 +08002644 cfe->type = (void *)cft;
2645 cfe->dentry = dentry;
2646 dentry->d_fsdata = cfe;
2647 simple_xattrs_init(&cfe->xattrs);
2648
Tejun Heo05ef1d72012-04-01 12:09:56 -07002649 mode = cgroup_file_mode(cft);
2650 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2651 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002652 list_add_tail(&cfe->node, &parent->files);
2653 cfe = NULL;
2654 }
2655 dput(dentry);
2656out:
2657 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002658 return error;
2659}
2660
Tejun Heob1f28d32013-06-28 16:24:10 -07002661/**
2662 * cgroup_addrm_files - add or remove files to a cgroup directory
2663 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002664 * @cfts: array of cftypes to be added
2665 * @is_add: whether to add or remove
2666 *
2667 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002668 * For removals, this function never fails. If addition fails, this
2669 * function doesn't remove files already added. The caller is responsible
2670 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002671 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002672static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2673 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002675 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002676 int ret;
2677
2678 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05002679 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002680
2681 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002682 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002683 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2684 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002685 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2686 continue;
2687 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2688 continue;
2689
Li Zefan2739d3c2013-01-21 18:18:33 +08002690 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002691 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002692 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002693 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002694 cft->name, ret);
2695 return ret;
2696 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002697 } else {
2698 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002699 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002700 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002701 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002702}
2703
Tejun Heo8e3f6542012-04-01 12:09:55 -07002704static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002705 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002706{
2707 /*
2708 * Thanks to the entanglement with vfs inode locking, we can't walk
2709 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002710 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2711 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002712 */
Tejun Heoace2bee2014-02-11 11:52:47 -05002713 mutex_lock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002714 mutex_lock(&cgroup_mutex);
2715}
2716
Tejun Heo2bb566c2013-08-08 20:11:23 -04002717static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002718 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002719{
2720 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002721 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002722 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002723 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002724 struct dentry *prev = NULL;
2725 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002726 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002727 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002728 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002729
Tejun Heo4ac06012014-02-11 11:52:47 -05002730 mutex_unlock(&cgroup_mutex);
2731
Tejun Heo8e3f6542012-04-01 12:09:55 -07002732 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002733 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002734 !atomic_inc_not_zero(&sb->s_active)) {
Tejun Heoace2bee2014-02-11 11:52:47 -05002735 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002736 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002737 }
2738
Li Zefane8c82d22013-06-18 18:48:37 +08002739 /*
2740 * All cgroups which are created after we drop cgroup_mutex will
2741 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002742 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002743 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002744 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002745
Li Zefane8c82d22013-06-18 18:48:37 +08002746 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002747 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002748 struct cgroup *cgrp = css->cgroup;
2749
Li Zefane8c82d22013-06-18 18:48:37 +08002750 if (cgroup_is_dead(cgrp))
2751 continue;
2752
2753 inode = cgrp->dentry->d_inode;
2754 dget(cgrp->dentry);
Li Zefane8c82d22013-06-18 18:48:37 +08002755 dput(prev);
2756 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002757
Tejun Heoace2bee2014-02-11 11:52:47 -05002758 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002759 mutex_lock(&inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05002760 mutex_lock(&cgroup_tree_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002761 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002762 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002763 mutex_unlock(&inode->i_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002764 if (ret)
2765 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002766 }
Tejun Heoace2bee2014-02-11 11:52:47 -05002767 mutex_unlock(&cgroup_tree_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08002768 dput(prev);
2769 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002770 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002771}
2772
Tejun Heo2da440a2014-02-11 11:52:48 -05002773static void cgroup_exit_cftypes(struct cftype *cfts)
2774{
2775 struct cftype *cft;
2776
2777 for (cft = cfts; cft->name[0] != '\0'; cft++)
2778 cft->ss = NULL;
2779}
2780
2781static void cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
2782{
2783 struct cftype *cft;
2784
2785 for (cft = cfts; cft->name[0] != '\0'; cft++)
2786 cft->ss = ss;
2787}
2788
Tejun Heo8e3f6542012-04-01 12:09:55 -07002789/**
2790 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2791 * @ss: target cgroup subsystem
2792 * @cfts: zero-length name terminated array of cftypes
2793 *
2794 * Register @cfts to @ss. Files described by @cfts are created for all
2795 * existing cgroups to which @ss is attached and all future cgroups will
2796 * have them too. This function can be called anytime whether @ss is
2797 * attached or not.
2798 *
2799 * Returns 0 on successful registration, -errno on failure. Note that this
2800 * function currently returns 0 as long as @cfts registration is successful
2801 * even if some file creation attempts on existing cgroups fail.
2802 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002803int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002804{
2805 struct cftype_set *set;
Tejun Heo9ccece82013-06-28 16:24:11 -07002806 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002807
2808 set = kzalloc(sizeof(*set), GFP_KERNEL);
2809 if (!set)
2810 return -ENOMEM;
2811
Tejun Heo2da440a2014-02-11 11:52:48 -05002812 cgroup_init_cftypes(ss, cfts);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002813
Tejun Heo8e3f6542012-04-01 12:09:55 -07002814 cgroup_cfts_prepare();
2815 set->cfts = cfts;
2816 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002817 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002818 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002819 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002820 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002821}
2822EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2823
Li Zefana043e3b2008-02-23 15:24:09 -08002824/**
Tejun Heo79578622012-04-01 12:09:56 -07002825 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002826 * @cfts: zero-length name terminated array of cftypes
2827 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002828 * Unregister @cfts. Files described by @cfts are removed from all
2829 * existing cgroups and all future cgroups won't have them either. This
2830 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002831 *
2832 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002833 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002834 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002835int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002836{
Tejun Heo2da440a2014-02-11 11:52:48 -05002837 struct cftype *found = NULL;
Tejun Heo79578622012-04-01 12:09:56 -07002838 struct cftype_set *set;
2839
Tejun Heo2bb566c2013-08-08 20:11:23 -04002840 if (!cfts || !cfts[0].ss)
2841 return -ENOENT;
2842
Tejun Heo79578622012-04-01 12:09:56 -07002843 cgroup_cfts_prepare();
2844
Tejun Heo2bb566c2013-08-08 20:11:23 -04002845 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002846 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002847 list_del(&set->node);
2848 kfree(set);
Tejun Heo2da440a2014-02-11 11:52:48 -05002849 found = cfts;
2850 break;
Tejun Heo79578622012-04-01 12:09:56 -07002851 }
2852 }
2853
Tejun Heo2da440a2014-02-11 11:52:48 -05002854 cgroup_cfts_commit(found, false);
2855 cgroup_exit_cftypes(cfts);
2856 return found ? 0 : -ENOENT;
Tejun Heo79578622012-04-01 12:09:56 -07002857}
2858
2859/**
Li Zefana043e3b2008-02-23 15:24:09 -08002860 * cgroup_task_count - count the number of tasks in a cgroup.
2861 * @cgrp: the cgroup in question
2862 *
2863 * Return the number of tasks in the cgroup.
2864 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002865int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002866{
2867 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002868 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002869
Paul Menage817929e2007-10-18 23:39:36 -07002870 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002871 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2872 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002873 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002874 return count;
2875}
2876
2877/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002878 * To reduce the fork() overhead for systems that are not actually using
2879 * their cgroups capability, we don't maintain the lists running through
2880 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002881 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002882 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002883static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002884{
2885 struct task_struct *p, *g;
2886 write_lock(&css_set_lock);
2887 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002888 /*
2889 * We need tasklist_lock because RCU is not safe against
2890 * while_each_thread(). Besides, a forking task that has passed
2891 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2892 * is not guaranteed to have its child immediately visible in the
2893 * tasklist if we walk through it with RCU.
2894 */
2895 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002896 do_each_thread(g, p) {
2897 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002898 /*
2899 * We should check if the process is exiting, otherwise
2900 * it will race with cgroup_exit() in that the list
2901 * entry won't be deleted though the process has exited.
2902 */
2903 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002904 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002905 task_unlock(p);
2906 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002907 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002908 write_unlock(&css_set_lock);
2909}
2910
Tejun Heo574bd9f2012-11-09 09:12:29 -08002911/**
Tejun Heo492eb212013-08-08 20:11:25 -04002912 * css_next_child - find the next child of a given css
2913 * @pos_css: the current position (%NULL to initiate traversal)
2914 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002915 *
Tejun Heo492eb212013-08-08 20:11:25 -04002916 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002917 * under either cgroup_mutex or RCU read lock. The only requirement is
2918 * that @parent_css and @pos_css are accessible. The next sibling is
2919 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002920 */
Tejun Heo492eb212013-08-08 20:11:25 -04002921struct cgroup_subsys_state *
2922css_next_child(struct cgroup_subsys_state *pos_css,
2923 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002924{
Tejun Heo492eb212013-08-08 20:11:25 -04002925 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2926 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002927 struct cgroup *next;
2928
Tejun Heoace2bee2014-02-11 11:52:47 -05002929 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002930
2931 /*
2932 * @pos could already have been removed. Once a cgroup is removed,
2933 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002934 * changes. As CGRP_DEAD assertion is serialized and happens
2935 * before the cgroup is taken off the ->sibling list, if we see it
2936 * unasserted, it's guaranteed that the next sibling hasn't
2937 * finished its grace period even if it's already removed, and thus
2938 * safe to dereference from this RCU critical section. If
2939 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2940 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002941 *
2942 * If @pos is dead, its next pointer can't be dereferenced;
2943 * however, as each cgroup is given a monotonically increasing
2944 * unique serial number and always appended to the sibling list,
2945 * the next one can be found by walking the parent's children until
2946 * we see a cgroup with higher serial number than @pos's. While
2947 * this path can be slower, it's taken only when either the current
2948 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002949 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002950 if (!pos) {
2951 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2952 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002953 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002954 } else {
2955 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2956 if (next->serial_nr > pos->serial_nr)
2957 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002958 }
2959
Tejun Heo492eb212013-08-08 20:11:25 -04002960 if (&next->sibling == &cgrp->children)
2961 return NULL;
2962
Tejun Heoca8bdca2013-08-26 18:40:56 -04002963 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002964}
Tejun Heo492eb212013-08-08 20:11:25 -04002965EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002966
2967/**
Tejun Heo492eb212013-08-08 20:11:25 -04002968 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002969 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002970 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002971 *
Tejun Heo492eb212013-08-08 20:11:25 -04002972 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002973 * to visit for pre-order traversal of @root's descendants. @root is
2974 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002975 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002976 * While this function requires cgroup_mutex or RCU read locking, it
2977 * doesn't require the whole traversal to be contained in a single critical
2978 * section. This function will return the correct next descendant as long
2979 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002980 */
Tejun Heo492eb212013-08-08 20:11:25 -04002981struct cgroup_subsys_state *
2982css_next_descendant_pre(struct cgroup_subsys_state *pos,
2983 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002984{
Tejun Heo492eb212013-08-08 20:11:25 -04002985 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002986
Tejun Heoace2bee2014-02-11 11:52:47 -05002987 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002988
Tejun Heobd8815a2013-08-08 20:11:27 -04002989 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002990 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002991 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002992
2993 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002994 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002995 if (next)
2996 return next;
2997
2998 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002999 while (pos != root) {
3000 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003001 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003002 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003003 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003004 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003005
3006 return NULL;
3007}
Tejun Heo492eb212013-08-08 20:11:25 -04003008EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003009
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003010/**
Tejun Heo492eb212013-08-08 20:11:25 -04003011 * css_rightmost_descendant - return the rightmost descendant of a css
3012 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003013 *
Tejun Heo492eb212013-08-08 20:11:25 -04003014 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3015 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003016 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003017 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003018 * While this function requires cgroup_mutex or RCU read locking, it
3019 * doesn't require the whole traversal to be contained in a single critical
3020 * section. This function will return the correct rightmost descendant as
3021 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003022 */
Tejun Heo492eb212013-08-08 20:11:25 -04003023struct cgroup_subsys_state *
3024css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003025{
Tejun Heo492eb212013-08-08 20:11:25 -04003026 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003027
Tejun Heoace2bee2014-02-11 11:52:47 -05003028 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003029
3030 do {
3031 last = pos;
3032 /* ->prev isn't RCU safe, walk ->next till the end */
3033 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003034 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003035 pos = tmp;
3036 } while (pos);
3037
3038 return last;
3039}
Tejun Heo492eb212013-08-08 20:11:25 -04003040EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003041
Tejun Heo492eb212013-08-08 20:11:25 -04003042static struct cgroup_subsys_state *
3043css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003044{
Tejun Heo492eb212013-08-08 20:11:25 -04003045 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003046
3047 do {
3048 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003049 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003050 } while (pos);
3051
3052 return last;
3053}
3054
3055/**
Tejun Heo492eb212013-08-08 20:11:25 -04003056 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003057 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003058 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003059 *
Tejun Heo492eb212013-08-08 20:11:25 -04003060 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003061 * to visit for post-order traversal of @root's descendants. @root is
3062 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003063 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003064 * While this function requires cgroup_mutex or RCU read locking, it
3065 * doesn't require the whole traversal to be contained in a single critical
3066 * section. This function will return the correct next descendant as long
3067 * as both @pos and @cgroup are accessible and @pos is a descendant of
3068 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003069 */
Tejun Heo492eb212013-08-08 20:11:25 -04003070struct cgroup_subsys_state *
3071css_next_descendant_post(struct cgroup_subsys_state *pos,
3072 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003073{
Tejun Heo492eb212013-08-08 20:11:25 -04003074 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003075
Tejun Heoace2bee2014-02-11 11:52:47 -05003076 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003077
Tejun Heo58b79a92013-09-06 15:31:08 -04003078 /* if first iteration, visit leftmost descendant which may be @root */
3079 if (!pos)
3080 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003081
Tejun Heobd8815a2013-08-08 20:11:27 -04003082 /* if we visited @root, we're done */
3083 if (pos == root)
3084 return NULL;
3085
Tejun Heo574bd9f2012-11-09 09:12:29 -08003086 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003087 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003088 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003089 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003090
3091 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003092 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003093}
Tejun Heo492eb212013-08-08 20:11:25 -04003094EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003095
Tejun Heo0942eee2013-08-08 20:11:26 -04003096/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003097 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003098 * @it: the iterator to advance
3099 *
3100 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003101 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003102static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003103{
3104 struct list_head *l = it->cset_link;
3105 struct cgrp_cset_link *link;
3106 struct css_set *cset;
3107
3108 /* Advance to the next non-empty css_set */
3109 do {
3110 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003111 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003112 it->cset_link = NULL;
3113 return;
3114 }
3115 link = list_entry(l, struct cgrp_cset_link, cset_link);
3116 cset = link->cset;
3117 } while (list_empty(&cset->tasks));
3118 it->cset_link = l;
3119 it->task = cset->tasks.next;
3120}
3121
Tejun Heo0942eee2013-08-08 20:11:26 -04003122/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003123 * css_task_iter_start - initiate task iteration
3124 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003125 * @it: the task iterator to use
3126 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003127 * Initiate iteration through the tasks of @css. The caller can call
3128 * css_task_iter_next() to walk through the tasks until the function
3129 * returns NULL. On completion of iteration, css_task_iter_end() must be
3130 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003131 *
3132 * Note that this function acquires a lock which is released when the
3133 * iteration finishes. The caller can't sleep while iteration is in
3134 * progress.
3135 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003136void css_task_iter_start(struct cgroup_subsys_state *css,
3137 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003138 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003139{
3140 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003141 * The first time anyone tries to iterate across a css, we need to
3142 * enable the list linking each css_set to its tasks, and fix up
3143 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003144 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003145 if (!use_task_css_set_links)
3146 cgroup_enable_task_cg_lists();
3147
Paul Menage817929e2007-10-18 23:39:36 -07003148 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003149
Tejun Heo72ec7022013-08-08 20:11:26 -04003150 it->origin_css = css;
3151 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003152
Tejun Heo72ec7022013-08-08 20:11:26 -04003153 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003154}
3155
Tejun Heo0942eee2013-08-08 20:11:26 -04003156/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003157 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003158 * @it: the task iterator being iterated
3159 *
3160 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003161 * initialized via css_task_iter_start(). Returns NULL when the iteration
3162 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003163 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003164struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003165{
3166 struct task_struct *res;
3167 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003168 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003169
3170 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003171 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003172 return NULL;
3173 res = list_entry(l, struct task_struct, cg_list);
3174 /* Advance iterator to find next entry */
3175 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003176 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3177 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003178 /*
3179 * We reached the end of this task list - move on to the
3180 * next cgrp_cset_link.
3181 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003182 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003183 } else {
3184 it->task = l;
3185 }
3186 return res;
3187}
3188
Tejun Heo0942eee2013-08-08 20:11:26 -04003189/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003190 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003191 * @it: the task iterator to finish
3192 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003193 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003194 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003195void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003196 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003197{
3198 read_unlock(&css_set_lock);
3199}
3200
Cliff Wickman31a7df02008-02-07 00:14:42 -08003201static inline int started_after_time(struct task_struct *t1,
3202 struct timespec *time,
3203 struct task_struct *t2)
3204{
3205 int start_diff = timespec_compare(&t1->start_time, time);
3206 if (start_diff > 0) {
3207 return 1;
3208 } else if (start_diff < 0) {
3209 return 0;
3210 } else {
3211 /*
3212 * Arbitrarily, if two processes started at the same
3213 * time, we'll say that the lower pointer value
3214 * started first. Note that t2 may have exited by now
3215 * so this may not be a valid pointer any longer, but
3216 * that's fine - it still serves to distinguish
3217 * between two tasks started (effectively) simultaneously.
3218 */
3219 return t1 > t2;
3220 }
3221}
3222
3223/*
3224 * This function is a callback from heap_insert() and is used to order
3225 * the heap.
3226 * In this case we order the heap in descending task start time.
3227 */
3228static inline int started_after(void *p1, void *p2)
3229{
3230 struct task_struct *t1 = p1;
3231 struct task_struct *t2 = p2;
3232 return started_after_time(t1, &t2->start_time, t2);
3233}
3234
3235/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003236 * css_scan_tasks - iterate though all the tasks in a css
3237 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003238 * @test: optional test callback
3239 * @process: process callback
3240 * @data: data passed to @test and @process
3241 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003242 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003243 * Iterate through all the tasks in @css, calling @test for each, and if it
3244 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003245 *
Tejun Heoe5358372013-08-08 20:11:26 -04003246 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003247 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003248 * lock css_set_lock for the call to @process.
3249 *
3250 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003251 * of @css for the duration of this call. This function may or may not
3252 * call @process for tasks that exit or move to a different css during the
3253 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003254 *
3255 * Note that @test may be called with locks held, and may in some
3256 * situations be called multiple times for the same task, so it should be
3257 * cheap.
3258 *
3259 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3260 * heap operations (and its "gt" member will be overwritten), else a
3261 * temporary heap will be used (allocation of which may cause this function
3262 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003263 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003264int css_scan_tasks(struct cgroup_subsys_state *css,
3265 bool (*test)(struct task_struct *, void *),
3266 void (*process)(struct task_struct *, void *),
3267 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003268{
3269 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003270 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003271 struct task_struct *p, *dropped;
3272 /* Never dereference latest_task, since it's not refcounted */
3273 struct task_struct *latest_task = NULL;
3274 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003275 struct timespec latest_time = { 0, 0 };
3276
Tejun Heoe5358372013-08-08 20:11:26 -04003277 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003278 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003279 heap->gt = &started_after;
3280 } else {
3281 /* We need to allocate our own heap memory */
3282 heap = &tmp_heap;
3283 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3284 if (retval)
3285 /* cannot allocate the heap */
3286 return retval;
3287 }
3288
3289 again:
3290 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003291 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003292 * which are of interest, and invoking @process callback on the
3293 * ones which need an update. Since we don't want to hold any
3294 * locks during the task updates, gather tasks to be processed in a
3295 * heap structure. The heap is sorted by descending task start
3296 * time. If the statically-sized heap fills up, we overflow tasks
3297 * that started later, and in future iterations only consider tasks
3298 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003299 * guarantees forward progress and that we don't miss any tasks.
3300 */
3301 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003302 css_task_iter_start(css, &it);
3303 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003304 /*
3305 * Only affect tasks that qualify per the caller's callback,
3306 * if he provided one
3307 */
Tejun Heoe5358372013-08-08 20:11:26 -04003308 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003309 continue;
3310 /*
3311 * Only process tasks that started after the last task
3312 * we processed
3313 */
3314 if (!started_after_time(p, &latest_time, latest_task))
3315 continue;
3316 dropped = heap_insert(heap, p);
3317 if (dropped == NULL) {
3318 /*
3319 * The new task was inserted; the heap wasn't
3320 * previously full
3321 */
3322 get_task_struct(p);
3323 } else if (dropped != p) {
3324 /*
3325 * The new task was inserted, and pushed out a
3326 * different task
3327 */
3328 get_task_struct(p);
3329 put_task_struct(dropped);
3330 }
3331 /*
3332 * Else the new task was newer than anything already in
3333 * the heap and wasn't inserted
3334 */
3335 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003336 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003337
3338 if (heap->size) {
3339 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003340 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003341 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003342 latest_time = q->start_time;
3343 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003344 }
3345 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003346 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003347 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003348 }
3349 /*
3350 * If we had to process any tasks at all, scan again
3351 * in case some of them were in the middle of forking
3352 * children that didn't get processed.
3353 * Not the most efficient way to do it, but it avoids
3354 * having to take callback_mutex in the fork path
3355 */
3356 goto again;
3357 }
3358 if (heap == &tmp_heap)
3359 heap_free(&tmp_heap);
3360 return 0;
3361}
3362
Tejun Heoe5358372013-08-08 20:11:26 -04003363static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003364{
Tejun Heoe5358372013-08-08 20:11:26 -04003365 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003366
Tejun Heo47cfcd02013-04-07 09:29:51 -07003367 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003368 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003369 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003370}
3371
3372/**
3373 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3374 * @to: cgroup to which the tasks will be moved
3375 * @from: cgroup in which the tasks currently reside
3376 */
3377int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3378{
Tejun Heo72ec7022013-08-08 20:11:26 -04003379 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3380 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003381}
3382
Paul Menage817929e2007-10-18 23:39:36 -07003383/*
Ben Blum102a7752009-09-23 15:56:26 -07003384 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003385 *
3386 * Reading this file can return large amounts of data if a cgroup has
3387 * *lots* of attached tasks. So it may need several calls to read(),
3388 * but we cannot guarantee that the information we produce is correct
3389 * unless we produce it entirely atomically.
3390 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003391 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003392
Li Zefan24528252012-01-20 11:58:43 +08003393/* which pidlist file are we talking about? */
3394enum cgroup_filetype {
3395 CGROUP_FILE_PROCS,
3396 CGROUP_FILE_TASKS,
3397};
3398
3399/*
3400 * A pidlist is a list of pids that virtually represents the contents of one
3401 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3402 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3403 * to the cgroup.
3404 */
3405struct cgroup_pidlist {
3406 /*
3407 * used to find which pidlist is wanted. doesn't change as long as
3408 * this particular list stays in the list.
3409 */
3410 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3411 /* array of xids */
3412 pid_t *list;
3413 /* how many elements the above list has */
3414 int length;
Li Zefan24528252012-01-20 11:58:43 +08003415 /* each of these stored in a list by its cgroup */
3416 struct list_head links;
3417 /* pointer to the cgroup we belong to, for list removal purposes */
3418 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003419 /* for delayed destruction */
3420 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003421};
3422
Paul Menagebbcb81d2007-10-18 23:39:32 -07003423/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003424 * The following two functions "fix" the issue where there are more pids
3425 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3426 * TODO: replace with a kernel-wide solution to this problem
3427 */
3428#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3429static void *pidlist_allocate(int count)
3430{
3431 if (PIDLIST_TOO_LARGE(count))
3432 return vmalloc(count * sizeof(pid_t));
3433 else
3434 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3435}
Tejun Heob1a21362013-11-29 10:42:58 -05003436
Ben Blumd1d9fd32009-09-23 15:56:28 -07003437static void pidlist_free(void *p)
3438{
3439 if (is_vmalloc_addr(p))
3440 vfree(p);
3441 else
3442 kfree(p);
3443}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003444
3445/*
Tejun Heob1a21362013-11-29 10:42:58 -05003446 * Used to destroy all pidlists lingering waiting for destroy timer. None
3447 * should be left afterwards.
3448 */
3449static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3450{
3451 struct cgroup_pidlist *l, *tmp_l;
3452
3453 mutex_lock(&cgrp->pidlist_mutex);
3454 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3455 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3456 mutex_unlock(&cgrp->pidlist_mutex);
3457
3458 flush_workqueue(cgroup_pidlist_destroy_wq);
3459 BUG_ON(!list_empty(&cgrp->pidlists));
3460}
3461
3462static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3463{
3464 struct delayed_work *dwork = to_delayed_work(work);
3465 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3466 destroy_dwork);
3467 struct cgroup_pidlist *tofree = NULL;
3468
3469 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003470
3471 /*
Tejun Heo04502362013-11-29 10:42:59 -05003472 * Destroy iff we didn't get queued again. The state won't change
3473 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003474 */
Tejun Heo04502362013-11-29 10:42:59 -05003475 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003476 list_del(&l->links);
3477 pidlist_free(l->list);
3478 put_pid_ns(l->key.ns);
3479 tofree = l;
3480 }
3481
Tejun Heob1a21362013-11-29 10:42:58 -05003482 mutex_unlock(&l->owner->pidlist_mutex);
3483 kfree(tofree);
3484}
3485
3486/*
Ben Blum102a7752009-09-23 15:56:26 -07003487 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003488 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003489 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003490static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003491{
Ben Blum102a7752009-09-23 15:56:26 -07003492 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003493
3494 /*
3495 * we presume the 0th element is unique, so i starts at 1. trivial
3496 * edge cases first; no work needs to be done for either
3497 */
3498 if (length == 0 || length == 1)
3499 return length;
3500 /* src and dest walk down the list; dest counts unique elements */
3501 for (src = 1; src < length; src++) {
3502 /* find next unique element */
3503 while (list[src] == list[src-1]) {
3504 src++;
3505 if (src == length)
3506 goto after;
3507 }
3508 /* dest always points to where the next unique element goes */
3509 list[dest] = list[src];
3510 dest++;
3511 }
3512after:
Ben Blum102a7752009-09-23 15:56:26 -07003513 return dest;
3514}
3515
Tejun Heoafb2bc12013-11-29 10:42:59 -05003516/*
3517 * The two pid files - task and cgroup.procs - guaranteed that the result
3518 * is sorted, which forced this whole pidlist fiasco. As pid order is
3519 * different per namespace, each namespace needs differently sorted list,
3520 * making it impossible to use, for example, single rbtree of member tasks
3521 * sorted by task pointer. As pidlists can be fairly large, allocating one
3522 * per open file is dangerous, so cgroup had to implement shared pool of
3523 * pidlists keyed by cgroup and namespace.
3524 *
3525 * All this extra complexity was caused by the original implementation
3526 * committing to an entirely unnecessary property. In the long term, we
3527 * want to do away with it. Explicitly scramble sort order if
3528 * sane_behavior so that no such expectation exists in the new interface.
3529 *
3530 * Scrambling is done by swapping every two consecutive bits, which is
3531 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3532 */
3533static pid_t pid_fry(pid_t pid)
3534{
3535 unsigned a = pid & 0x55555555;
3536 unsigned b = pid & 0xAAAAAAAA;
3537
3538 return (a << 1) | (b >> 1);
3539}
3540
3541static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3542{
3543 if (cgroup_sane_behavior(cgrp))
3544 return pid_fry(pid);
3545 else
3546 return pid;
3547}
3548
Ben Blum102a7752009-09-23 15:56:26 -07003549static int cmppid(const void *a, const void *b)
3550{
3551 return *(pid_t *)a - *(pid_t *)b;
3552}
3553
Tejun Heoafb2bc12013-11-29 10:42:59 -05003554static int fried_cmppid(const void *a, const void *b)
3555{
3556 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3557}
3558
Ben Blum72a8cb32009-09-23 15:56:27 -07003559static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3560 enum cgroup_filetype type)
3561{
3562 struct cgroup_pidlist *l;
3563 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003564 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003565
Tejun Heoe6b81712013-11-29 10:42:59 -05003566 lockdep_assert_held(&cgrp->pidlist_mutex);
3567
3568 list_for_each_entry(l, &cgrp->pidlists, links)
3569 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003570 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003571 return NULL;
3572}
3573
Ben Blum72a8cb32009-09-23 15:56:27 -07003574/*
3575 * find the appropriate pidlist for our purpose (given procs vs tasks)
3576 * returns with the lock on that pidlist already held, and takes care
3577 * of the use count, or returns NULL with no locks held if we're out of
3578 * memory.
3579 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003580static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3581 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003582{
3583 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003584
Tejun Heoe6b81712013-11-29 10:42:59 -05003585 lockdep_assert_held(&cgrp->pidlist_mutex);
3586
3587 l = cgroup_pidlist_find(cgrp, type);
3588 if (l)
3589 return l;
3590
Ben Blum72a8cb32009-09-23 15:56:27 -07003591 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003592 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003593 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003594 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003595
Tejun Heob1a21362013-11-29 10:42:58 -05003596 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003597 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003598 /* don't need task_nsproxy() if we're looking at ourself */
3599 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003600 l->owner = cgrp;
3601 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003602 return l;
3603}
3604
3605/*
Ben Blum102a7752009-09-23 15:56:26 -07003606 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3607 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003608static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3609 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003610{
3611 pid_t *array;
3612 int length;
3613 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003614 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003615 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003616 struct cgroup_pidlist *l;
3617
Tejun Heo4bac00d2013-11-29 10:42:59 -05003618 lockdep_assert_held(&cgrp->pidlist_mutex);
3619
Ben Blum102a7752009-09-23 15:56:26 -07003620 /*
3621 * If cgroup gets more users after we read count, we won't have
3622 * enough space - tough. This race is indistinguishable to the
3623 * caller from the case that the additional cgroup users didn't
3624 * show up until sometime later on.
3625 */
3626 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003627 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003628 if (!array)
3629 return -ENOMEM;
3630 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003631 css_task_iter_start(&cgrp->dummy_css, &it);
3632 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003633 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003634 break;
Ben Blum102a7752009-09-23 15:56:26 -07003635 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003636 if (type == CGROUP_FILE_PROCS)
3637 pid = task_tgid_vnr(tsk);
3638 else
3639 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003640 if (pid > 0) /* make sure to only use valid results */
3641 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003642 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003643 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003644 length = n;
3645 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003646 if (cgroup_sane_behavior(cgrp))
3647 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3648 else
3649 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003650 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003651 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003652
Tejun Heoe6b81712013-11-29 10:42:59 -05003653 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003654 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003655 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003656 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003657 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003658 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003659
3660 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003661 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003662 l->list = array;
3663 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003664 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003665 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003666}
3667
Balbir Singh846c7bb2007-10-18 23:39:44 -07003668/**
Li Zefana043e3b2008-02-23 15:24:09 -08003669 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003670 * @stats: cgroupstats to fill information into
3671 * @dentry: A dentry entry belonging to the cgroup for which stats have
3672 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003673 *
3674 * Build and fill cgroupstats so that taskstats can export it to user
3675 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003676 */
3677int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3678{
3679 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003680 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003681 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003682 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003683
Balbir Singh846c7bb2007-10-18 23:39:44 -07003684 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003685 * Validate dentry by checking the superblock operations,
3686 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003687 */
Li Zefan33d283b2008-11-19 15:36:48 -08003688 if (dentry->d_sb->s_op != &cgroup_ops ||
3689 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003690 goto err;
3691
3692 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003693 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003694
Tejun Heo72ec7022013-08-08 20:11:26 -04003695 css_task_iter_start(&cgrp->dummy_css, &it);
3696 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003697 switch (tsk->state) {
3698 case TASK_RUNNING:
3699 stats->nr_running++;
3700 break;
3701 case TASK_INTERRUPTIBLE:
3702 stats->nr_sleeping++;
3703 break;
3704 case TASK_UNINTERRUPTIBLE:
3705 stats->nr_uninterruptible++;
3706 break;
3707 case TASK_STOPPED:
3708 stats->nr_stopped++;
3709 break;
3710 default:
3711 if (delayacct_is_task_waiting_on_io(tsk))
3712 stats->nr_io_wait++;
3713 break;
3714 }
3715 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003716 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003717
Balbir Singh846c7bb2007-10-18 23:39:44 -07003718err:
3719 return ret;
3720}
3721
Paul Menage8f3ff202009-09-23 15:56:25 -07003722
Paul Menagecc31edc2008-10-18 20:28:04 -07003723/*
Ben Blum102a7752009-09-23 15:56:26 -07003724 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003725 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003726 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003727 */
3728
Ben Blum102a7752009-09-23 15:56:26 -07003729static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003730{
3731 /*
3732 * Initially we receive a position value that corresponds to
3733 * one more than the last pid shown (or 0 on the first call or
3734 * after a seek to the start). Use a binary-search to find the
3735 * next pid to display, if any
3736 */
Tejun Heo5d224442013-12-05 12:28:04 -05003737 struct cgroup_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003738 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003739 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003740 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003741 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003742 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003743
Tejun Heo4bac00d2013-11-29 10:42:59 -05003744 mutex_lock(&cgrp->pidlist_mutex);
3745
3746 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003747 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003748 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003749 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003750 * could already have been destroyed.
3751 */
Tejun Heo5d224442013-12-05 12:28:04 -05003752 if (of->priv)
3753 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003754
3755 /*
3756 * Either this is the first start() after open or the matching
3757 * pidlist has been destroyed inbetween. Create a new one.
3758 */
Tejun Heo5d224442013-12-05 12:28:04 -05003759 if (!of->priv) {
3760 ret = pidlist_array_load(cgrp, type,
3761 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003762 if (ret)
3763 return ERR_PTR(ret);
3764 }
Tejun Heo5d224442013-12-05 12:28:04 -05003765 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003766
Paul Menagecc31edc2008-10-18 20:28:04 -07003767 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003768 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003769
Paul Menagecc31edc2008-10-18 20:28:04 -07003770 while (index < end) {
3771 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003772 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003773 index = mid;
3774 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003775 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003776 index = mid + 1;
3777 else
3778 end = mid;
3779 }
3780 }
3781 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003782 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003783 return NULL;
3784 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003785 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003786 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003787 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003788}
3789
Ben Blum102a7752009-09-23 15:56:26 -07003790static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003791{
Tejun Heo5d224442013-12-05 12:28:04 -05003792 struct cgroup_open_file *of = s->private;
3793 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003794
Tejun Heo5d224442013-12-05 12:28:04 -05003795 if (l)
3796 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003797 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003798 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003799}
3800
Ben Blum102a7752009-09-23 15:56:26 -07003801static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003802{
Tejun Heo5d224442013-12-05 12:28:04 -05003803 struct cgroup_open_file *of = s->private;
3804 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003805 pid_t *p = v;
3806 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003807 /*
3808 * Advance to the next pid in the array. If this goes off the
3809 * end, we're done
3810 */
3811 p++;
3812 if (p >= end) {
3813 return NULL;
3814 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003815 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003816 return p;
3817 }
3818}
3819
Ben Blum102a7752009-09-23 15:56:26 -07003820static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003821{
3822 return seq_printf(s, "%d\n", *(int *)v);
3823}
3824
Ben Blum102a7752009-09-23 15:56:26 -07003825/*
3826 * seq_operations functions for iterating on pidlists through seq_file -
3827 * independent of whether it's tasks or procs
3828 */
3829static const struct seq_operations cgroup_pidlist_seq_operations = {
3830 .start = cgroup_pidlist_start,
3831 .stop = cgroup_pidlist_stop,
3832 .next = cgroup_pidlist_next,
3833 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003834};
3835
Tejun Heo182446d2013-08-08 20:11:24 -04003836static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3837 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003838{
Tejun Heo182446d2013-08-08 20:11:24 -04003839 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003840}
3841
Tejun Heo182446d2013-08-08 20:11:24 -04003842static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3843 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003844{
Tejun Heo182446d2013-08-08 20:11:24 -04003845 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003846 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003847 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003848 else
Tejun Heo182446d2013-08-08 20:11:24 -04003849 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003850 return 0;
3851}
3852
Paul Menagebbcb81d2007-10-18 23:39:32 -07003853/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003854 * When dput() is called asynchronously, if umount has been done and
3855 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3856 * there's a small window that vfs will see the root dentry with non-zero
3857 * refcnt and trigger BUG().
3858 *
3859 * That's why we hold a reference before dput() and drop it right after.
3860 */
3861static void cgroup_dput(struct cgroup *cgrp)
3862{
3863 struct super_block *sb = cgrp->root->sb;
3864
3865 atomic_inc(&sb->s_active);
3866 dput(cgrp->dentry);
3867 deactivate_super(sb);
3868}
3869
Tejun Heo182446d2013-08-08 20:11:24 -04003870static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3871 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003872{
Tejun Heo182446d2013-08-08 20:11:24 -04003873 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003874}
3875
Tejun Heo182446d2013-08-08 20:11:24 -04003876static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3877 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003878{
3879 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003880 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003881 else
Tejun Heo182446d2013-08-08 20:11:24 -04003882 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003883 return 0;
3884}
3885
Tejun Heod5c56ce2013-06-03 19:14:34 -07003886static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003887 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003888 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003889 .seq_start = cgroup_pidlist_start,
3890 .seq_next = cgroup_pidlist_next,
3891 .seq_stop = cgroup_pidlist_stop,
3892 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003893 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003894 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003895 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003896 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003897 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003898 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003899 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003900 .read_u64 = cgroup_clone_children_read,
3901 .write_u64 = cgroup_clone_children_write,
3902 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003903 {
Tejun Heo873fe092013-04-14 20:15:26 -07003904 .name = "cgroup.sane_behavior",
3905 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003906 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003907 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003908
3909 /*
3910 * Historical crazy stuff. These don't have "cgroup." prefix and
3911 * don't exist if sane_behavior. If you're depending on these, be
3912 * prepared to be burned.
3913 */
3914 {
3915 .name = "tasks",
3916 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003917 .seq_start = cgroup_pidlist_start,
3918 .seq_next = cgroup_pidlist_next,
3919 .seq_stop = cgroup_pidlist_stop,
3920 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003921 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003922 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003923 .mode = S_IRUGO | S_IWUSR,
3924 },
3925 {
3926 .name = "notify_on_release",
3927 .flags = CFTYPE_INSANE,
3928 .read_u64 = cgroup_read_notify_on_release,
3929 .write_u64 = cgroup_write_notify_on_release,
3930 },
Tejun Heo873fe092013-04-14 20:15:26 -07003931 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003932 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003933 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003934 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003935 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003936 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003937 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003938 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003939};
3940
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003941/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003942 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003943 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003944 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003945 *
3946 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003947 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003948static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003949{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003950 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003951 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003952
Tejun Heo8e3f6542012-04-01 12:09:55 -07003953 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003954 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003955 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07003956
3957 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003958 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003959
Tejun Heobee55092013-06-28 16:24:11 -07003960 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003961 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003962 if (ret < 0)
3963 goto err;
3964 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003965 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003966 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003967err:
3968 cgroup_clear_dir(cgrp, subsys_mask);
3969 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003970}
3971
Tejun Heo0c21ead2013-08-13 20:22:51 -04003972/*
3973 * css destruction is four-stage process.
3974 *
3975 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3976 * Implemented in kill_css().
3977 *
3978 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3979 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3980 * by invoking offline_css(). After offlining, the base ref is put.
3981 * Implemented in css_killed_work_fn().
3982 *
3983 * 3. When the percpu_ref reaches zero, the only possible remaining
3984 * accessors are inside RCU read sections. css_release() schedules the
3985 * RCU callback.
3986 *
3987 * 4. After the grace period, the css can be freed. Implemented in
3988 * css_free_work_fn().
3989 *
3990 * It is actually hairier because both step 2 and 4 require process context
3991 * and thus involve punting to css->destroy_work adding two additional
3992 * steps to the already complex sequence.
3993 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003994static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003995{
3996 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003997 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003998 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003999
Tejun Heo0ae78e02013-08-13 11:01:54 -04004000 if (css->parent)
4001 css_put(css->parent);
4002
Tejun Heo0c21ead2013-08-13 20:22:51 -04004003 css->ss->css_free(css);
4004 cgroup_dput(cgrp);
4005}
4006
4007static void css_free_rcu_fn(struct rcu_head *rcu_head)
4008{
4009 struct cgroup_subsys_state *css =
4010 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4011
4012 /*
4013 * css holds an extra ref to @cgrp->dentry which is put on the last
4014 * css_put(). dput() requires process context which we don't have.
4015 */
4016 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004017 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004018}
4019
Tejun Heod3daf282013-06-13 19:39:16 -07004020static void css_release(struct percpu_ref *ref)
4021{
4022 struct cgroup_subsys_state *css =
4023 container_of(ref, struct cgroup_subsys_state, refcnt);
4024
Tejun Heoaec25022014-02-08 10:36:58 -05004025 rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004026 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004027}
4028
Tejun Heo623f9262013-08-13 11:01:55 -04004029static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4030 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004031{
Paul Menagebd89aab2007-10-18 23:40:44 -07004032 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004033 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004034 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004035
Tejun Heo0ae78e02013-08-13 11:01:54 -04004036 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004037 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004038 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004039 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004040
Tejun Heoca8bdca2013-08-26 18:40:56 -04004041 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004042}
4043
Li Zefan2a4ac632013-07-31 16:16:40 +08004044/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004045static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004046{
Tejun Heo623f9262013-08-13 11:01:55 -04004047 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004048 int ret = 0;
4049
Tejun Heoace2bee2014-02-11 11:52:47 -05004050 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004051 lockdep_assert_held(&cgroup_mutex);
4052
Tejun Heo92fb9742012-11-19 08:13:38 -08004053 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004054 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004055 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004056 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004057 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05004058 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004059 }
Tejun Heob1929db2012-11-19 08:13:38 -08004060 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004061}
4062
Li Zefan2a4ac632013-07-31 16:16:40 +08004063/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004064static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004065{
Tejun Heo623f9262013-08-13 11:01:55 -04004066 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004067
Tejun Heoace2bee2014-02-11 11:52:47 -05004068 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004069 lockdep_assert_held(&cgroup_mutex);
4070
4071 if (!(css->flags & CSS_ONLINE))
4072 return;
4073
Li Zefand7eeac12013-03-12 15:35:59 -07004074 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004075 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004076
Tejun Heoeb954192013-08-08 20:11:23 -04004077 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004078 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05004079 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004080}
4081
Tejun Heoc81c925a2013-12-06 15:11:56 -05004082/**
4083 * create_css - create a cgroup_subsys_state
4084 * @cgrp: the cgroup new css will be associated with
4085 * @ss: the subsys of new css
4086 *
4087 * Create a new css associated with @cgrp - @ss pair. On success, the new
4088 * css is online and installed in @cgrp with all interface files created.
4089 * Returns 0 on success, -errno on failure.
4090 */
4091static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4092{
4093 struct cgroup *parent = cgrp->parent;
4094 struct cgroup_subsys_state *css;
4095 int err;
4096
4097 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
4098 lockdep_assert_held(&cgroup_mutex);
4099
4100 css = ss->css_alloc(cgroup_css(parent, ss));
4101 if (IS_ERR(css))
4102 return PTR_ERR(css);
4103
4104 err = percpu_ref_init(&css->refcnt, css_release);
4105 if (err)
4106 goto err_free;
4107
4108 init_css(css, ss, cgrp);
4109
Tejun Heoaec25022014-02-08 10:36:58 -05004110 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004111 if (err)
4112 goto err_free;
4113
4114 err = online_css(css);
4115 if (err)
4116 goto err_free;
4117
4118 dget(cgrp->dentry);
4119 css_get(css->parent);
4120
4121 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4122 parent->parent) {
4123 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",
4124 current->comm, current->pid, ss->name);
4125 if (!strcmp(ss->name, "memory"))
4126 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4127 ss->warned_broken_hierarchy = true;
4128 }
4129
4130 return 0;
4131
4132err_free:
4133 percpu_ref_cancel_init(&css->refcnt);
4134 ss->css_free(css);
4135 return err;
4136}
4137
Paul Menageddbcc7e2007-10-18 23:39:30 -07004138/*
Li Zefana043e3b2008-02-23 15:24:09 -08004139 * cgroup_create - create a cgroup
4140 * @parent: cgroup that will be parent of the new cgroup
4141 * @dentry: dentry of the new cgroup
4142 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004143 *
Li Zefana043e3b2008-02-23 15:24:09 -08004144 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004145 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004146static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004147 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004148{
Paul Menagebd89aab2007-10-18 23:40:44 -07004149 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004150 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004151 struct cgroupfs_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05004152 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004153 struct cgroup_subsys *ss;
4154 struct super_block *sb = root->sb;
4155
Tejun Heo0a950f62012-11-19 09:02:12 -08004156 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004157 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4158 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004159 return -ENOMEM;
4160
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05004161 name = cgroup_alloc_name(dentry->d_name.name);
Tejun Heob58c8992014-02-08 10:26:33 -05004162 if (!name) {
4163 err = -ENOMEM;
Li Zefan65dff752013-03-01 15:01:56 +08004164 goto err_free_cgrp;
Tejun Heob58c8992014-02-08 10:26:33 -05004165 }
Li Zefan65dff752013-03-01 15:01:56 +08004166 rcu_assign_pointer(cgrp->name, name);
4167
Tejun Heoace2bee2014-02-11 11:52:47 -05004168 mutex_lock(&cgroup_tree_mutex);
4169
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004170 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08004171 * Only live parents can have children. Note that the liveliness
4172 * check isn't strictly necessary because cgroup_mkdir() and
4173 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4174 * anyway so that locking is contained inside cgroup proper and we
4175 * don't get nasty surprises if we ever grow another caller.
4176 */
4177 if (!cgroup_lock_live_group(parent)) {
4178 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05004179 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004180 }
4181
4182 /*
4183 * Temporarily set the pointer to NULL, so idr_find() won't return
4184 * a half-baked cgroup.
4185 */
4186 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
4187 if (cgrp->id < 0) {
4188 err = -ENOMEM;
4189 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08004190 }
4191
Paul Menageddbcc7e2007-10-18 23:39:30 -07004192 /* Grab a reference on the superblock so the hierarchy doesn't
4193 * get deleted on unmount if there are child cgroups. This
4194 * can be done outside cgroup_mutex, since the sb can't
4195 * disappear while someone has an open control file on the
4196 * fs */
4197 atomic_inc(&sb->s_active);
4198
Paul Menagecc31edc2008-10-18 20:28:04 -07004199 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004200
Li Zefanfe1c06c2013-01-24 14:30:22 +08004201 dentry->d_fsdata = cgrp;
4202 cgrp->dentry = dentry;
4203
Paul Menagebd89aab2007-10-18 23:40:44 -07004204 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004205 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004206 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004207
Li Zefanb6abdb02008-03-04 14:28:19 -08004208 if (notify_on_release(parent))
4209 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4210
Tejun Heo2260e7f2012-11-19 08:13:38 -08004211 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4212 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004213
Tejun Heo4e139af2012-11-19 08:13:36 -08004214 /*
4215 * Create directory. cgroup_create_file() returns with the new
4216 * directory locked on success so that it can be populated without
4217 * dropping cgroup_mutex.
4218 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004219 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004220 if (err < 0)
Li Zefan0ab02ca2014-02-11 16:05:46 +08004221 goto err_free_id;
Tejun Heo4e139af2012-11-19 08:13:36 -08004222 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004223
Tejun Heo00356bd2013-06-18 11:14:22 -07004224 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004225
Tejun Heo4e139af2012-11-19 08:13:36 -08004226 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004227 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4228 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004229
Li Zefan415cf072013-04-08 14:35:02 +08004230 /* hold a ref to the parent's dentry */
4231 dget(parent->dentry);
4232
Tejun Heo0d802552013-12-06 15:11:56 -05004233 /*
4234 * @cgrp is now fully operational. If something fails after this
4235 * point, it'll be released via the normal destruction path.
4236 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004237 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4238
Tejun Heo2bb566c2013-08-08 20:11:23 -04004239 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004240 if (err)
4241 goto err_destroy;
4242
Tejun Heo9d403e92013-12-06 15:11:56 -05004243 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004244 for_each_subsys(ss, ssid) {
4245 if (root->subsys_mask & (1 << ssid)) {
4246 err = create_css(cgrp, ss);
4247 if (err)
4248 goto err_destroy;
4249 }
Tejun Heoa8638032012-11-09 09:12:29 -08004250 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004251
4252 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004253 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004254 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004255
4256 return 0;
4257
Tejun Heo0a950f62012-11-19 09:02:12 -08004258err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004259 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004260 /* Release the reference count that we took on the superblock */
4261 deactivate_super(sb);
4262err_unlock:
4263 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004264err_unlock_tree:
4265 mutex_unlock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08004266 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004267err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004268 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004269 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004270
4271err_destroy:
4272 cgroup_destroy_locked(cgrp);
4273 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004274 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004275 mutex_unlock(&dentry->d_inode->i_mutex);
4276 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004277}
4278
Al Viro18bb1db2011-07-26 01:41:39 -04004279static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004280{
4281 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4282
4283 /* the vfs holds inode->i_mutex already */
4284 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4285}
4286
Tejun Heo223dbc32013-08-13 20:22:50 -04004287/*
4288 * This is called when the refcnt of a css is confirmed to be killed.
4289 * css_tryget() is now guaranteed to fail.
4290 */
4291static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004292{
Tejun Heo223dbc32013-08-13 20:22:50 -04004293 struct cgroup_subsys_state *css =
4294 container_of(work, struct cgroup_subsys_state, destroy_work);
4295 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004296
Tejun Heoace2bee2014-02-11 11:52:47 -05004297 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004298 mutex_lock(&cgroup_mutex);
4299
4300 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004301 * css_tryget() is guaranteed to fail now. Tell subsystems to
4302 * initate destruction.
4303 */
4304 offline_css(css);
4305
4306 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004307 * If @cgrp is marked dead, it's waiting for refs of all css's to
4308 * be disabled before proceeding to the second phase of cgroup
4309 * destruction. If we are the last one, kick it off.
4310 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004311 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004312 cgroup_destroy_css_killed(cgrp);
4313
4314 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004315 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004316
4317 /*
4318 * Put the css refs from kill_css(). Each css holds an extra
4319 * reference to the cgroup's dentry and cgroup removal proceeds
4320 * regardless of css refs. On the last put of each css, whenever
4321 * that may be, the extra dentry ref is put so that dentry
4322 * destruction happens only after all css's are released.
4323 */
4324 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004325}
4326
Tejun Heo223dbc32013-08-13 20:22:50 -04004327/* css kill confirmation processing requires process context, bounce */
4328static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004329{
4330 struct cgroup_subsys_state *css =
4331 container_of(ref, struct cgroup_subsys_state, refcnt);
4332
Tejun Heo223dbc32013-08-13 20:22:50 -04004333 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004334 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004335}
4336
4337/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004338 * kill_css - destroy a css
4339 * @css: css to destroy
4340 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004341 * This function initiates destruction of @css by removing cgroup interface
4342 * files and putting its base reference. ->css_offline() will be invoked
4343 * asynchronously once css_tryget() is guaranteed to fail and when the
4344 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004345 */
4346static void kill_css(struct cgroup_subsys_state *css)
4347{
Tejun Heoaec25022014-02-08 10:36:58 -05004348 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004349
Tejun Heoedae0c32013-08-13 20:22:51 -04004350 /*
4351 * Killing would put the base ref, but we need to keep it alive
4352 * until after ->css_offline().
4353 */
4354 css_get(css);
4355
4356 /*
4357 * cgroup core guarantees that, by the time ->css_offline() is
4358 * invoked, no new css reference will be given out via
4359 * css_tryget(). We can't simply call percpu_ref_kill() and
4360 * proceed to offlining css's because percpu_ref_kill() doesn't
4361 * guarantee that the ref is seen as killed on all CPUs on return.
4362 *
4363 * Use percpu_ref_kill_and_confirm() to get notifications as each
4364 * css is confirmed to be seen as killed on all CPUs.
4365 */
4366 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004367}
4368
4369/**
4370 * cgroup_destroy_locked - the first stage of cgroup destruction
4371 * @cgrp: cgroup to be destroyed
4372 *
4373 * css's make use of percpu refcnts whose killing latency shouldn't be
4374 * exposed to userland and are RCU protected. Also, cgroup core needs to
4375 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4376 * invoked. To satisfy all the requirements, destruction is implemented in
4377 * the following two steps.
4378 *
4379 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4380 * userland visible parts and start killing the percpu refcnts of
4381 * css's. Set up so that the next stage will be kicked off once all
4382 * the percpu refcnts are confirmed to be killed.
4383 *
4384 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4385 * rest of destruction. Once all cgroup references are gone, the
4386 * cgroup is RCU-freed.
4387 *
4388 * This function implements s1. After this step, @cgrp is gone as far as
4389 * the userland is concerned and a new cgroup with the same name may be
4390 * created. As cgroup doesn't care about the names internally, this
4391 * doesn't cause any problem.
4392 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004393static int cgroup_destroy_locked(struct cgroup *cgrp)
4394 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004395{
Tejun Heo42809dd2012-11-19 08:13:37 -08004396 struct dentry *d = cgrp->dentry;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004397 struct cgroup_subsys_state *css;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004398 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004399 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004400 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004401
Tejun Heo42809dd2012-11-19 08:13:37 -08004402 lockdep_assert_held(&d->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004403 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004404 lockdep_assert_held(&cgroup_mutex);
4405
Tejun Heoddd69142013-06-12 21:04:54 -07004406 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004407 * css_set_lock synchronizes access to ->cset_links and prevents
4408 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004409 */
4410 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004411 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004412 read_unlock(&css_set_lock);
4413 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004414 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004415
Tejun Heo1a90dd52012-11-05 09:16:59 -08004416 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004417 * Make sure there's no live children. We can't test ->children
4418 * emptiness as dead children linger on it while being destroyed;
4419 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4420 */
4421 empty = true;
4422 rcu_read_lock();
4423 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4424 empty = cgroup_is_dead(child);
4425 if (!empty)
4426 break;
4427 }
4428 rcu_read_unlock();
4429 if (!empty)
4430 return -EBUSY;
4431
4432 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004433 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4434 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004435 * percpu refs of all css's are confirmed to be killed. This
4436 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004437 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004438 mutex_unlock(&cgroup_mutex);
Tejun Heo1c6727a2013-12-06 15:11:56 -05004439 for_each_css(css, ssid, cgrp)
4440 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004441 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004442
4443 /*
4444 * Mark @cgrp dead. This prevents further task migration and child
4445 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004446 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004447 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004448 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004449 */
Tejun Heo54766d42013-06-12 21:04:53 -07004450 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004451
Tejun Heo455050d2013-06-13 19:27:41 -07004452 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4453 raw_spin_lock(&release_list_lock);
4454 if (!list_empty(&cgrp->release_list))
4455 list_del_init(&cgrp->release_list);
4456 raw_spin_unlock(&release_list_lock);
4457
4458 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004459 * If @cgrp has css's attached, the second stage of cgroup
4460 * destruction is kicked off from css_killed_work_fn() after the
4461 * refs of all attached css's are killed. If @cgrp doesn't have
4462 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004463 */
Tejun Heof20104d2013-08-13 20:22:50 -04004464 if (!cgrp->nr_css)
4465 cgroup_destroy_css_killed(cgrp);
4466
4467 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004468 * Clear the base files and remove @cgrp directory. The removal
4469 * puts the base ref but we aren't quite done with @cgrp yet, so
4470 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004471 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004472 mutex_unlock(&cgroup_mutex);
Tejun Heo2bb566c2013-08-08 20:11:23 -04004473 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004474 dget(d);
4475 cgroup_d_remove_dir(d);
Tejun Heo4ac06012014-02-11 11:52:47 -05004476 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004477
Tejun Heoea15f8c2013-06-13 19:27:42 -07004478 return 0;
4479};
4480
Tejun Heod3daf282013-06-13 19:39:16 -07004481/**
Tejun Heof20104d2013-08-13 20:22:50 -04004482 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004483 * @work: cgroup->destroy_free_work
4484 *
4485 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004486 * destroyed after all css's are offlined and performs the rest of
4487 * destruction. This is the second step of destruction described in the
4488 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004489 */
Tejun Heof20104d2013-08-13 20:22:50 -04004490static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004491{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004492 struct cgroup *parent = cgrp->parent;
4493 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004494
Tejun Heoace2bee2014-02-11 11:52:47 -05004495 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004496 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004497
Paul Menage999cd8a2009-01-07 18:08:36 -08004498 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004499 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004500
Paul Menageddbcc7e2007-10-18 23:39:30 -07004501 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004502
Paul Menagebd89aab2007-10-18 23:40:44 -07004503 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004504 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004505}
4506
Tejun Heo42809dd2012-11-19 08:13:37 -08004507static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4508{
4509 int ret;
4510
Tejun Heoace2bee2014-02-11 11:52:47 -05004511 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004512 mutex_lock(&cgroup_mutex);
4513 ret = cgroup_destroy_locked(dentry->d_fsdata);
4514 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004515 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004516
4517 return ret;
4518}
4519
Li Zefan06a11922008-04-29 01:00:07 -07004520static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004521{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004522 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004523
4524 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004525
Tejun Heoace2bee2014-02-11 11:52:47 -05004526 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004527 mutex_lock(&cgroup_mutex);
4528
Tejun Heode00ffa2014-02-11 11:52:48 -05004529 INIT_LIST_HEAD(&ss->cftsets);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004530
Paul Menageddbcc7e2007-10-18 23:39:30 -07004531 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004532 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004533 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004534 /* We don't handle early failures gracefully */
4535 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004536 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004537
Li Zefane8d55fd2008-04-29 01:00:13 -07004538 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004539 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004540 * newly registered, all tasks and hence the
4541 * init_css_set is in the subsystem's top cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004542 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004543
4544 need_forkexit_callback |= ss->fork || ss->exit;
4545
Li Zefane8d55fd2008-04-29 01:00:13 -07004546 /* At system boot, before all subsystems have been
4547 * registered, no tasks have been forked, so we don't
4548 * need to invoke fork callbacks here. */
4549 BUG_ON(!list_empty(&init_task.tasks));
4550
Tejun Heoae7f1642013-08-13 20:22:50 -04004551 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004552
Tejun Heo648bb562012-11-19 08:13:36 -08004553 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004554 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004555}
4556
4557/**
Li Zefana043e3b2008-02-23 15:24:09 -08004558 * cgroup_init_early - cgroup initialization at system boot
4559 *
4560 * Initialize cgroups at system boot, and initialize any
4561 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004562 */
4563int __init cgroup_init_early(void)
4564{
Tejun Heo30159ec2013-06-25 11:53:37 -07004565 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004566 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004567
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004568 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004569 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004570 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004571 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004572 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004573 init_cgroup_root(&cgroup_dummy_root);
4574 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004575 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004576
Tejun Heo69d02062013-06-12 21:04:50 -07004577 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004578 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4579 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004580 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004581
Tejun Heo3ed80a62014-02-08 10:36:58 -05004582 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004583 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004584 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4585 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004586 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004587 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4588 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4589
Tejun Heoaec25022014-02-08 10:36:58 -05004590 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004591 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004592
4593 if (ss->early_init)
4594 cgroup_init_subsys(ss);
4595 }
4596 return 0;
4597}
4598
4599/**
Li Zefana043e3b2008-02-23 15:24:09 -08004600 * cgroup_init - cgroup initialization
4601 *
4602 * Register cgroup filesystem and /proc file, and initialize
4603 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004604 */
4605int __init cgroup_init(void)
4606{
Tejun Heo30159ec2013-06-25 11:53:37 -07004607 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004608 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004609 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004610
4611 err = bdi_init(&cgroup_backing_dev_info);
4612 if (err)
4613 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004614
Tejun Heo2da440a2014-02-11 11:52:48 -05004615 cgroup_init_cftypes(NULL, cgroup_base_files);
4616
Tejun Heo3ed80a62014-02-08 10:36:58 -05004617 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004618 if (!ss->early_init)
4619 cgroup_init_subsys(ss);
Tejun Heode00ffa2014-02-11 11:52:48 -05004620
4621 /*
4622 * cftype registration needs kmalloc and can't be done
4623 * during early_init. Register base cftypes separately.
4624 */
4625 if (ss->base_cftypes)
4626 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004627 }
4628
Tejun Heofa3ca072013-04-14 11:36:56 -07004629 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004630 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004631
Tejun Heo82fe9b02013-06-25 11:53:37 -07004632 /* Add init_css_set to the hash table */
4633 key = css_set_hash(init_css_set.subsys);
4634 hash_add(css_set_table, &init_css_set.hlist, key);
4635
Tejun Heofc76df72013-06-25 11:53:37 -07004636 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004637
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004638 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4639 0, 1, GFP_KERNEL);
4640 BUG_ON(err < 0);
4641
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004642 mutex_unlock(&cgroup_mutex);
4643
Greg KH676db4a2010-08-05 13:53:35 -07004644 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4645 if (!cgroup_kobj) {
4646 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004647 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004648 }
4649
4650 err = register_filesystem(&cgroup_fs_type);
4651 if (err < 0) {
4652 kobject_put(cgroup_kobj);
4653 goto out;
4654 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004655
Li Zefan46ae2202008-04-29 01:00:08 -07004656 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004657
Paul Menageddbcc7e2007-10-18 23:39:30 -07004658out:
Paul Menagea4243162007-10-18 23:39:35 -07004659 if (err)
4660 bdi_destroy(&cgroup_backing_dev_info);
4661
Paul Menageddbcc7e2007-10-18 23:39:30 -07004662 return err;
4663}
Paul Menageb4f48b62007-10-18 23:39:33 -07004664
Tejun Heoe5fca242013-11-22 17:14:39 -05004665static int __init cgroup_wq_init(void)
4666{
4667 /*
4668 * There isn't much point in executing destruction path in
4669 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004670 *
4671 * XXX: Must be ordered to make sure parent is offlined after
4672 * children. The ordering requirement is for memcg where a
4673 * parent's offline may wait for a child's leading to deadlock. In
4674 * the long term, this should be fixed from memcg side.
Tejun Heoe5fca242013-11-22 17:14:39 -05004675 *
4676 * We would prefer to do this in cgroup_init() above, but that
4677 * is called before init_workqueues(): so leave this until after.
4678 */
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004679 cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0);
Tejun Heoe5fca242013-11-22 17:14:39 -05004680 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004681
4682 /*
4683 * Used to destroy pidlists and separate to serve as flush domain.
4684 * Cap @max_active to 1 too.
4685 */
4686 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4687 0, 1);
4688 BUG_ON(!cgroup_pidlist_destroy_wq);
4689
Tejun Heoe5fca242013-11-22 17:14:39 -05004690 return 0;
4691}
4692core_initcall(cgroup_wq_init);
4693
Paul Menagea4243162007-10-18 23:39:35 -07004694/*
4695 * proc_cgroup_show()
4696 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4697 * - Used for /proc/<pid>/cgroup.
4698 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4699 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004700 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004701 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4702 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4703 * cgroup to top_cgroup.
4704 */
4705
4706/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004707int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004708{
4709 struct pid *pid;
4710 struct task_struct *tsk;
4711 char *buf;
4712 int retval;
4713 struct cgroupfs_root *root;
4714
4715 retval = -ENOMEM;
4716 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4717 if (!buf)
4718 goto out;
4719
4720 retval = -ESRCH;
4721 pid = m->private;
4722 tsk = get_pid_task(pid, PIDTYPE_PID);
4723 if (!tsk)
4724 goto out_free;
4725
4726 retval = 0;
4727
4728 mutex_lock(&cgroup_mutex);
4729
Li Zefane5f6a862009-01-07 18:07:41 -08004730 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004731 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004732 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004733 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004734
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004735 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004736 for_each_subsys(ss, ssid)
4737 if (root->subsys_mask & (1 << ssid))
4738 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004739 if (strlen(root->name))
4740 seq_printf(m, "%sname=%s", count ? "," : "",
4741 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004742 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004743 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004744 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004745 if (retval < 0)
4746 goto out_unlock;
4747 seq_puts(m, buf);
4748 seq_putc(m, '\n');
4749 }
4750
4751out_unlock:
4752 mutex_unlock(&cgroup_mutex);
4753 put_task_struct(tsk);
4754out_free:
4755 kfree(buf);
4756out:
4757 return retval;
4758}
4759
Paul Menagea4243162007-10-18 23:39:35 -07004760/* Display information about each subsystem and each hierarchy */
4761static int proc_cgroupstats_show(struct seq_file *m, void *v)
4762{
Tejun Heo30159ec2013-06-25 11:53:37 -07004763 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004764 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004765
Paul Menage8bab8dd2008-04-04 14:29:57 -07004766 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004767 /*
4768 * ideally we don't want subsystems moving around while we do this.
4769 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4770 * subsys/hierarchy state.
4771 */
Paul Menagea4243162007-10-18 23:39:35 -07004772 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004773
4774 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004775 seq_printf(m, "%s\t%d\t%d\t%d\n",
4776 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004777 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004778
Paul Menagea4243162007-10-18 23:39:35 -07004779 mutex_unlock(&cgroup_mutex);
4780 return 0;
4781}
4782
4783static int cgroupstats_open(struct inode *inode, struct file *file)
4784{
Al Viro9dce07f2008-03-29 03:07:28 +00004785 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004786}
4787
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004788static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004789 .open = cgroupstats_open,
4790 .read = seq_read,
4791 .llseek = seq_lseek,
4792 .release = single_release,
4793};
4794
Paul Menageb4f48b62007-10-18 23:39:33 -07004795/**
4796 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004797 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004798 *
4799 * Description: A task inherits its parent's cgroup at fork().
4800 *
4801 * A pointer to the shared css_set was automatically copied in
4802 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004803 * it was not made under the protection of RCU or cgroup_mutex, so
4804 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4805 * have already changed current->cgroups, allowing the previously
4806 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004807 *
4808 * At the point that cgroup_fork() is called, 'current' is the parent
4809 * task, and the passed argument 'child' points to the child task.
4810 */
4811void cgroup_fork(struct task_struct *child)
4812{
Tejun Heo9bb71302012-10-18 17:52:07 -07004813 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004814 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004815 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004816 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004817 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004818}
4819
4820/**
Li Zefana043e3b2008-02-23 15:24:09 -08004821 * cgroup_post_fork - called on a new task after adding it to the task list
4822 * @child: the task in question
4823 *
Tejun Heo5edee612012-10-16 15:03:14 -07004824 * Adds the task to the list running through its css_set if necessary and
4825 * call the subsystem fork() callbacks. Has to be after the task is
4826 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004827 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004828 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004829 */
Paul Menage817929e2007-10-18 23:39:36 -07004830void cgroup_post_fork(struct task_struct *child)
4831{
Tejun Heo30159ec2013-06-25 11:53:37 -07004832 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004833 int i;
4834
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004835 /*
4836 * use_task_css_set_links is set to 1 before we walk the tasklist
4837 * under the tasklist_lock and we read it here after we added the child
4838 * to the tasklist under the tasklist_lock as well. If the child wasn't
4839 * yet in the tasklist when we walked through it from
4840 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4841 * should be visible now due to the paired locking and barriers implied
4842 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4843 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4844 * lock on fork.
4845 */
Paul Menage817929e2007-10-18 23:39:36 -07004846 if (use_task_css_set_links) {
4847 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004848 task_lock(child);
4849 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004850 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004851 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004852 write_unlock(&css_set_lock);
4853 }
Tejun Heo5edee612012-10-16 15:03:14 -07004854
4855 /*
4856 * Call ss->fork(). This must happen after @child is linked on
4857 * css_set; otherwise, @child might change state between ->fork()
4858 * and addition to css_set.
4859 */
4860 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004861 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004862 if (ss->fork)
4863 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004864 }
Paul Menage817929e2007-10-18 23:39:36 -07004865}
Tejun Heo5edee612012-10-16 15:03:14 -07004866
Paul Menage817929e2007-10-18 23:39:36 -07004867/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004868 * cgroup_exit - detach cgroup from exiting task
4869 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004870 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004871 *
4872 * Description: Detach cgroup from @tsk and release it.
4873 *
4874 * Note that cgroups marked notify_on_release force every task in
4875 * them to take the global cgroup_mutex mutex when exiting.
4876 * This could impact scaling on very large systems. Be reluctant to
4877 * use notify_on_release cgroups where very high task exit scaling
4878 * is required on large systems.
4879 *
4880 * the_top_cgroup_hack:
4881 *
4882 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4883 *
4884 * We call cgroup_exit() while the task is still competent to
4885 * handle notify_on_release(), then leave the task attached to the
4886 * root cgroup in each hierarchy for the remainder of its exit.
4887 *
4888 * To do this properly, we would increment the reference count on
4889 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4890 * code we would add a second cgroup function call, to drop that
4891 * reference. This would just create an unnecessary hot spot on
4892 * the top_cgroup reference count, to no avail.
4893 *
4894 * Normally, holding a reference to a cgroup without bumping its
4895 * count is unsafe. The cgroup could go away, or someone could
4896 * attach us to a different cgroup, decrementing the count on
4897 * the first cgroup that we never incremented. But in this case,
4898 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004899 * which wards off any cgroup_attach_task() attempts, or task is a failed
4900 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004901 */
4902void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4903{
Tejun Heo30159ec2013-06-25 11:53:37 -07004904 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004905 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004906 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004907
4908 /*
4909 * Unlink from the css_set task list if necessary.
4910 * Optimistically check cg_list before taking
4911 * css_set_lock
4912 */
4913 if (!list_empty(&tsk->cg_list)) {
4914 write_lock(&css_set_lock);
4915 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004916 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004917 write_unlock(&css_set_lock);
4918 }
4919
Paul Menageb4f48b62007-10-18 23:39:33 -07004920 /* Reassign the task to the init_css_set. */
4921 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004922 cset = task_css_set(tsk);
4923 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004924
4925 if (run_callbacks && need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004926 /* see cgroup_post_fork() for details */
4927 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004928 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004929 struct cgroup_subsys_state *old_css = cset->subsys[i];
4930 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004931
Tejun Heoeb954192013-08-08 20:11:23 -04004932 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004933 }
4934 }
4935 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004936 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004937
Tejun Heo5abb8852013-06-12 21:04:49 -07004938 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07004939}
Paul Menage697f4162007-10-18 23:39:34 -07004940
Paul Menagebd89aab2007-10-18 23:40:44 -07004941static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004942{
Li Zefanf50daa72013-03-01 15:06:07 +08004943 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004944 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004945 /*
4946 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004947 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004948 * it now
4949 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004950 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004951
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004952 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004953 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004954 list_empty(&cgrp->release_list)) {
4955 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004956 need_schedule_work = 1;
4957 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004958 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004959 if (need_schedule_work)
4960 schedule_work(&release_agent_work);
4961 }
4962}
4963
Paul Menage81a6a5c2007-10-18 23:39:38 -07004964/*
4965 * Notify userspace when a cgroup is released, by running the
4966 * configured release agent with the name of the cgroup (path
4967 * relative to the root of cgroup file system) as the argument.
4968 *
4969 * Most likely, this user command will try to rmdir this cgroup.
4970 *
4971 * This races with the possibility that some other task will be
4972 * attached to this cgroup before it is removed, or that some other
4973 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4974 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4975 * unused, and this cgroup will be reprieved from its death sentence,
4976 * to continue to serve a useful existence. Next time it's released,
4977 * we will get notified again, if it still has 'notify_on_release' set.
4978 *
4979 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4980 * means only wait until the task is successfully execve()'d. The
4981 * separate release agent task is forked by call_usermodehelper(),
4982 * then control in this thread returns here, without waiting for the
4983 * release agent task. We don't bother to wait because the caller of
4984 * this routine has no use for the exit status of the release agent
4985 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004986 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004987static void cgroup_release_agent(struct work_struct *work)
4988{
4989 BUG_ON(work != &release_agent_work);
4990 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004991 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004992 while (!list_empty(&release_list)) {
4993 char *argv[3], *envp[3];
4994 int i;
Paul Menagee788e062008-07-25 01:46:59 -07004995 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004996 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004997 struct cgroup,
4998 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004999 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005000 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005001 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005002 if (!pathbuf)
5003 goto continue_free;
5004 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5005 goto continue_free;
5006 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5007 if (!agentbuf)
5008 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005009
5010 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005011 argv[i++] = agentbuf;
5012 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005013 argv[i] = NULL;
5014
5015 i = 0;
5016 /* minimal command environment */
5017 envp[i++] = "HOME=/";
5018 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5019 envp[i] = NULL;
5020
5021 /* Drop the lock while we invoke the usermode helper,
5022 * since the exec could involve hitting disk and hence
5023 * be a slow process */
5024 mutex_unlock(&cgroup_mutex);
5025 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005026 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005027 continue_free:
5028 kfree(pathbuf);
5029 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005030 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005031 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005032 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005033 mutex_unlock(&cgroup_mutex);
5034}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005035
5036static int __init cgroup_disable(char *str)
5037{
Tejun Heo30159ec2013-06-25 11:53:37 -07005038 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005039 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005040 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005041
5042 while ((token = strsep(&str, ",")) != NULL) {
5043 if (!*token)
5044 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005045
Tejun Heo3ed80a62014-02-08 10:36:58 -05005046 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005047 if (!strcmp(token, ss->name)) {
5048 ss->disabled = 1;
5049 printk(KERN_INFO "Disabling %s control group"
5050 " subsystem\n", ss->name);
5051 break;
5052 }
5053 }
5054 }
5055 return 1;
5056}
5057__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005058
Tejun Heob77d7b62013-08-13 11:01:54 -04005059/**
Tejun Heo5a17f542014-02-11 11:52:47 -05005060 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04005061 * @dentry: directory dentry of interest
5062 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005063 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005064 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5065 * to get the corresponding css and return it. If such css doesn't exist
5066 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005067 */
Tejun Heo5a17f542014-02-11 11:52:47 -05005068struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
5069 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005070{
5071 struct cgroup *cgrp;
Tejun Heo5a17f542014-02-11 11:52:47 -05005072 struct cgroup_subsys_state *css;
Tejun Heob77d7b62013-08-13 11:01:54 -04005073
Tejun Heo35cf0832013-08-26 18:40:56 -04005074 /* is @dentry a cgroup dir? */
5075 if (!dentry->d_inode ||
5076 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005077 return ERR_PTR(-EBADF);
5078
Tejun Heo5a17f542014-02-11 11:52:47 -05005079 rcu_read_lock();
5080
Tejun Heo35cf0832013-08-26 18:40:56 -04005081 cgrp = __d_cgrp(dentry);
Tejun Heo5a17f542014-02-11 11:52:47 -05005082 css = cgroup_css(cgrp, ss);
5083
5084 if (!css || !css_tryget(css))
5085 css = ERR_PTR(-ENOENT);
5086
5087 rcu_read_unlock();
5088 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005089}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005090
Li Zefan1cb650b2013-08-19 10:05:24 +08005091/**
5092 * css_from_id - lookup css by id
5093 * @id: the cgroup id
5094 * @ss: cgroup subsys to be looked into
5095 *
5096 * Returns the css if there's valid one with @id, otherwise returns NULL.
5097 * Should be called under rcu_read_lock().
5098 */
5099struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5100{
5101 struct cgroup *cgrp;
5102
Tejun Heoace2bee2014-02-11 11:52:47 -05005103 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08005104
5105 cgrp = idr_find(&ss->root->cgroup_idr, id);
5106 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005107 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005108 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005109}
5110
Paul Menagefe693432009-09-23 15:56:20 -07005111#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005112static struct cgroup_subsys_state *
5113debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005114{
5115 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5116
5117 if (!css)
5118 return ERR_PTR(-ENOMEM);
5119
5120 return css;
5121}
5122
Tejun Heoeb954192013-08-08 20:11:23 -04005123static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005124{
Tejun Heoeb954192013-08-08 20:11:23 -04005125 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005126}
5127
Tejun Heo182446d2013-08-08 20:11:24 -04005128static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5129 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005130{
Tejun Heo182446d2013-08-08 20:11:24 -04005131 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005132}
5133
Tejun Heo182446d2013-08-08 20:11:24 -04005134static u64 current_css_set_read(struct cgroup_subsys_state *css,
5135 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005136{
5137 return (u64)(unsigned long)current->cgroups;
5138}
5139
Tejun Heo182446d2013-08-08 20:11:24 -04005140static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005141 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005142{
5143 u64 count;
5144
5145 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005146 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005147 rcu_read_unlock();
5148 return count;
5149}
5150
Tejun Heo2da8ca82013-12-05 12:28:04 -05005151static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005152{
Tejun Heo69d02062013-06-12 21:04:50 -07005153 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005154 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005155
5156 read_lock(&css_set_lock);
5157 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005158 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005159 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005160 struct cgroup *c = link->cgrp;
5161 const char *name;
5162
5163 if (c->dentry)
5164 name = c->dentry->d_name.name;
5165 else
5166 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005167 seq_printf(seq, "Root %d group %s\n",
5168 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005169 }
5170 rcu_read_unlock();
5171 read_unlock(&css_set_lock);
5172 return 0;
5173}
5174
5175#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005176static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005177{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005178 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005179 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005180
5181 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005182 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005183 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005184 struct task_struct *task;
5185 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005186 seq_printf(seq, "css_set %p\n", cset);
5187 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005188 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5189 seq_puts(seq, " ...\n");
5190 break;
5191 } else {
5192 seq_printf(seq, " task %d\n",
5193 task_pid_vnr(task));
5194 }
5195 }
5196 }
5197 read_unlock(&css_set_lock);
5198 return 0;
5199}
5200
Tejun Heo182446d2013-08-08 20:11:24 -04005201static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005202{
Tejun Heo182446d2013-08-08 20:11:24 -04005203 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005204}
5205
5206static struct cftype debug_files[] = {
5207 {
Paul Menagefe693432009-09-23 15:56:20 -07005208 .name = "taskcount",
5209 .read_u64 = debug_taskcount_read,
5210 },
5211
5212 {
5213 .name = "current_css_set",
5214 .read_u64 = current_css_set_read,
5215 },
5216
5217 {
5218 .name = "current_css_set_refcount",
5219 .read_u64 = current_css_set_refcount_read,
5220 },
5221
5222 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005223 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005224 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005225 },
5226
5227 {
5228 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005229 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005230 },
5231
5232 {
Paul Menagefe693432009-09-23 15:56:20 -07005233 .name = "releasable",
5234 .read_u64 = releasable_read,
5235 },
Paul Menagefe693432009-09-23 15:56:20 -07005236
Tejun Heo4baf6e32012-04-01 12:09:55 -07005237 { } /* terminate */
5238};
Paul Menagefe693432009-09-23 15:56:20 -07005239
Tejun Heo073219e2014-02-08 10:36:58 -05005240struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005241 .css_alloc = debug_css_alloc,
5242 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005243 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005244};
5245#endif /* CONFIG_CGROUP_DEBUG */