blob: d28cf75f33c1cb3ec39b701c79c050a005861e33 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/slab.h>
45#include <linux/magic.h>
46#include <linux/spinlock.h>
47#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070048#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070049#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070050#include <linux/delayacct.h>
51#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080052#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040053#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070054#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070055#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070056#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Li Zefan081aa452013-03-13 09:17:09 +080057#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020058#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070059
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070061
Tejun Heoe25e2cb2011-12-12 18:12:21 -080062/*
Tejun Heob1a21362013-11-29 10:42:58 -050063 * pidlists linger the following amount before being destroyed. The goal
64 * is avoiding frequent destruction in the middle of consecutive read calls
65 * Expiring in the middle is a performance problem not a correctness one.
66 * 1 sec should be enough.
67 */
68#define CGROUP_PIDLIST_DESTROY_DELAY HZ
69
70/*
Tejun Heoace2bee2014-02-11 11:52:47 -050071 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
72 * creation/removal and hierarchy changing operations including cgroup
73 * creation, removal, css association and controller rebinding. This outer
74 * lock is needed mainly to resolve the circular dependency between kernfs
75 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
76 */
77static DEFINE_MUTEX(cgroup_tree_mutex);
78
79/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080080 * cgroup_mutex is the master lock. Any modification to cgroup or its
81 * hierarchy must be performed while holding it.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080082 */
Tejun Heo22194492013-04-07 09:29:51 -070083#ifdef CONFIG_PROVE_RCU
84DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040085EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070086#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070087static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070088#endif
89
Tejun Heo69e943b2014-02-08 10:36:58 -050090/*
91 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
92 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
93 */
94static DEFINE_SPINLOCK(release_agent_path_lock);
95
Tejun Heoace2bee2014-02-11 11:52:47 -050096#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -050097 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -050098 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -050099 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500100 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo87fb54f2013-12-06 15:11:55 -0500101
Ben Blumaae8aab2010-03-10 15:22:07 -0800102/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500103 * cgroup destruction makes heavy use of work items and there can be a lot
104 * of concurrent destructions. Use a separate workqueue so that cgroup
105 * destruction work items don't end up filling up max_active of system_wq
106 * which may lead to deadlock.
107 */
108static struct workqueue_struct *cgroup_destroy_wq;
109
110/*
Tejun Heob1a21362013-11-29 10:42:58 -0500111 * pidlist destructions need to be flushed on cgroup destruction. Use a
112 * separate workqueue as flush domain.
113 */
114static struct workqueue_struct *cgroup_pidlist_destroy_wq;
115
Tejun Heo3ed80a62014-02-08 10:36:58 -0500116/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500117#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500118static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700119#include <linux/cgroup_subsys.h>
120};
Tejun Heo073219e2014-02-08 10:36:58 -0500121#undef SUBSYS
122
123/* array of cgroup subsystem names */
124#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
125static const char *cgroup_subsys_name[] = {
126#include <linux/cgroup_subsys.h>
127};
128#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700129
Paul Menageddbcc7e2007-10-18 23:39:30 -0700130/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700131 * The dummy hierarchy, reserved for the subsystems that are otherwise
132 * unattached - it never has more than a single cgroup, and all tasks are
133 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700134 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700135static struct cgroupfs_root cgroup_dummy_root;
136
137/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
138static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700139
140/* The list of hierarchy roots */
141
Tejun Heo9871bf92013-06-24 15:21:47 -0700142static LIST_HEAD(cgroup_roots);
143static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700144
Tejun Heo3417ae12014-02-08 10:37:01 -0500145/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700146static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700147
Li Zefan65dff752013-03-01 15:01:56 +0800148static struct cgroup_name root_cgroup_name = { .name = "/" };
149
Li Zefan794611a2013-06-18 18:53:53 +0800150/*
151 * Assign a monotonically increasing serial number to cgroups. It
152 * guarantees cgroups with bigger numbers are newer than those with smaller
153 * numbers. Also, as cgroups are always appended to the parent's
154 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700155 * the ascending serial number order on the list. Protected by
156 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800157 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700158static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800159
Paul Menageddbcc7e2007-10-18 23:39:30 -0700160/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800161 * check for fork/exit handlers to call. This avoids us having to do
162 * extra work in the fork/exit path if none of the subsystems need to
163 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700164 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700165static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700166
Tejun Heo628f7cd2013-06-28 16:24:11 -0700167static struct cftype cgroup_base_files[];
168
Tejun Heof20104d2013-08-13 20:22:50 -0400169static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800170static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400171static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
172 bool is_add);
Tejun Heoe605b362013-11-27 18:16:21 -0500173static int cgroup_file_release(struct inode *inode, struct file *file);
Tejun Heob1a21362013-11-29 10:42:58 -0500174static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800175
Tejun Heo95109b62013-08-08 20:11:27 -0400176/**
177 * cgroup_css - obtain a cgroup's css for the specified subsystem
178 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400179 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400180 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400181 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
182 * function must be called either under cgroup_mutex or rcu_read_lock() and
183 * the caller is responsible for pinning the returned css if it wants to
184 * keep accessing it outside the said locks. This function may return
185 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400186 */
187static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400188 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400189{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400190 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500191 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500192 lockdep_is_held(&cgroup_tree_mutex) ||
193 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400194 else
195 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400196}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700197
Paul Menageddbcc7e2007-10-18 23:39:30 -0700198/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700199static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700200{
Tejun Heo54766d42013-06-12 21:04:53 -0700201 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700202}
203
Li Zefan78574cf2013-04-08 19:00:38 -0700204/**
205 * cgroup_is_descendant - test ancestry
206 * @cgrp: the cgroup to be tested
207 * @ancestor: possible ancestor of @cgrp
208 *
209 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
210 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
211 * and @ancestor are accessible.
212 */
213bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
214{
215 while (cgrp) {
216 if (cgrp == ancestor)
217 return true;
218 cgrp = cgrp->parent;
219 }
220 return false;
221}
222EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700223
Adrian Bunke9685a02008-02-07 00:13:46 -0800224static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700225{
226 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700227 (1 << CGRP_RELEASABLE) |
228 (1 << CGRP_NOTIFY_ON_RELEASE);
229 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700230}
231
Adrian Bunke9685a02008-02-07 00:13:46 -0800232static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700233{
Paul Menagebd89aab2007-10-18 23:40:44 -0700234 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700235}
236
Tejun Heo30159ec2013-06-25 11:53:37 -0700237/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500238 * for_each_css - iterate all css's of a cgroup
239 * @css: the iteration cursor
240 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
241 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700242 *
243 * Should be called under cgroup_mutex.
244 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500245#define for_each_css(css, ssid, cgrp) \
246 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
247 if (!((css) = rcu_dereference_check( \
248 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500249 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500250 lockdep_is_held(&cgroup_mutex)))) { } \
251 else
252
253/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500254 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700255 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500256 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700257 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500258#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500259 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
260 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700261
Tejun Heo5549c492013-06-24 15:21:48 -0700262/* iterate across the active hierarchies */
263#define for_each_active_root(root) \
264 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700265
Tejun Heof6ea9372012-04-01 12:09:55 -0700266static inline struct cgroup *__d_cgrp(struct dentry *dentry)
267{
268 return dentry->d_fsdata;
269}
270
Tejun Heo05ef1d72012-04-01 12:09:56 -0700271static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700272{
273 return dentry->d_fsdata;
274}
275
Tejun Heo05ef1d72012-04-01 12:09:56 -0700276static inline struct cftype *__d_cft(struct dentry *dentry)
277{
278 return __d_cfe(dentry)->type;
279}
280
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700281/**
282 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
283 * @cgrp: the cgroup to be checked for liveness
284 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700285 * On success, returns true; the mutex should be later unlocked. On
286 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700287 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700288static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700289{
290 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700291 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700292 mutex_unlock(&cgroup_mutex);
293 return false;
294 }
295 return true;
296}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700297
Paul Menage81a6a5c2007-10-18 23:39:38 -0700298/* the list of cgroups eligible for automatic release. Protected by
299 * release_list_lock */
300static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200301static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700302static void cgroup_release_agent(struct work_struct *work);
303static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700304static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700305
Tejun Heo69d02062013-06-12 21:04:50 -0700306/*
307 * A cgroup can be associated with multiple css_sets as different tasks may
308 * belong to different cgroups on different hierarchies. In the other
309 * direction, a css_set is naturally associated with multiple cgroups.
310 * This M:N relationship is represented by the following link structure
311 * which exists for each association and allows traversing the associations
312 * from both sides.
313 */
314struct cgrp_cset_link {
315 /* the cgroup and css_set this link associates */
316 struct cgroup *cgrp;
317 struct css_set *cset;
318
319 /* list of cgrp_cset_links anchored at cgrp->cset_links */
320 struct list_head cset_link;
321
322 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
323 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700324};
325
326/* The default css_set - used by init and its children prior to any
327 * hierarchies being mounted. It contains a pointer to the root state
328 * for each subsystem. Also used to anchor the list of css_sets. Not
329 * reference-counted, to improve performance when child cgroups
330 * haven't been created.
331 */
332
333static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700334static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700335
Tejun Heo0942eee2013-08-08 20:11:26 -0400336/*
337 * css_set_lock protects the list of css_set objects, and the chain of
338 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400339 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400340 */
Paul Menage817929e2007-10-18 23:39:36 -0700341static DEFINE_RWLOCK(css_set_lock);
342static int css_set_count;
343
Paul Menage7717f7b2009-09-23 15:56:22 -0700344/*
345 * hash table for cgroup groups. This improves the performance to find
346 * an existing css_set. This hash doesn't (currently) take into
347 * account cgroups in empty hierarchies.
348 */
Li Zefan472b1052008-04-29 01:00:11 -0700349#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800350static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700351
Li Zefan0ac801f2013-01-10 11:49:27 +0800352static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700353{
Li Zefan0ac801f2013-01-10 11:49:27 +0800354 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700355 struct cgroup_subsys *ss;
356 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700357
Tejun Heo30159ec2013-06-25 11:53:37 -0700358 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800359 key += (unsigned long)css[i];
360 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700361
Li Zefan0ac801f2013-01-10 11:49:27 +0800362 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700363}
364
Tejun Heo0942eee2013-08-08 20:11:26 -0400365/*
366 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400367 * until after the first call to css_task_iter_start(). This reduces the
368 * fork()/exit() overhead for people who have cgroups compiled into their
369 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400370 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700371static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700372
Tejun Heo5abb8852013-06-12 21:04:49 -0700373static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700374{
Tejun Heo69d02062013-06-12 21:04:50 -0700375 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700376
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700377 /*
378 * Ensure that the refcount doesn't hit zero while any readers
379 * can see it. Similar to atomic_dec_and_lock(), but for an
380 * rwlock
381 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700382 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700383 return;
384 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700385 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700386 write_unlock(&css_set_lock);
387 return;
388 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700389
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700390 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700391 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700392 css_set_count--;
393
Tejun Heo69d02062013-06-12 21:04:50 -0700394 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700395 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700396
Tejun Heo69d02062013-06-12 21:04:50 -0700397 list_del(&link->cset_link);
398 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800399
Tejun Heoddd69142013-06-12 21:04:54 -0700400 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700401 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700402 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700403 set_bit(CGRP_RELEASABLE, &cgrp->flags);
404 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700405 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700406
407 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700408 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700409
410 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700411 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700412}
413
414/*
415 * refcounted get/put for css_set objects
416 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700417static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700418{
Tejun Heo5abb8852013-06-12 21:04:49 -0700419 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700420}
421
Tejun Heo5abb8852013-06-12 21:04:49 -0700422static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700423{
Tejun Heo5abb8852013-06-12 21:04:49 -0700424 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700425}
426
Tejun Heo5abb8852013-06-12 21:04:49 -0700427static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700428{
Tejun Heo5abb8852013-06-12 21:04:49 -0700429 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700430}
431
Tejun Heob326f9d2013-06-24 15:21:48 -0700432/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700433 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700434 * @cset: candidate css_set being tested
435 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700436 * @new_cgrp: cgroup that's being entered by the task
437 * @template: desired set of css pointers in css_set (pre-calculated)
438 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800439 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700440 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
441 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700442static bool compare_css_sets(struct css_set *cset,
443 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700444 struct cgroup *new_cgrp,
445 struct cgroup_subsys_state *template[])
446{
447 struct list_head *l1, *l2;
448
Tejun Heo5abb8852013-06-12 21:04:49 -0700449 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700450 /* Not all subsystems matched */
451 return false;
452 }
453
454 /*
455 * Compare cgroup pointers in order to distinguish between
456 * different cgroups in heirarchies with no subsystems. We
457 * could get by with just this check alone (and skip the
458 * memcmp above) but on most setups the memcmp check will
459 * avoid the need for this more expensive check on almost all
460 * candidates.
461 */
462
Tejun Heo69d02062013-06-12 21:04:50 -0700463 l1 = &cset->cgrp_links;
464 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700465 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700466 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700467 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700468
469 l1 = l1->next;
470 l2 = l2->next;
471 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700472 if (l1 == &cset->cgrp_links) {
473 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700474 break;
475 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700476 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700477 }
478 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700479 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
480 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
481 cgrp1 = link1->cgrp;
482 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700483 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700484 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700485
486 /*
487 * If this hierarchy is the hierarchy of the cgroup
488 * that's changing, then we need to check that this
489 * css_set points to the new cgroup; if it's any other
490 * hierarchy, then this css_set should point to the
491 * same cgroup as the old css_set.
492 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700493 if (cgrp1->root == new_cgrp->root) {
494 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700495 return false;
496 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700497 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700498 return false;
499 }
500 }
501 return true;
502}
503
Tejun Heob326f9d2013-06-24 15:21:48 -0700504/**
505 * find_existing_css_set - init css array and find the matching css_set
506 * @old_cset: the css_set that we're using before the cgroup transition
507 * @cgrp: the cgroup that we're moving into
508 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700509 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700510static struct css_set *find_existing_css_set(struct css_set *old_cset,
511 struct cgroup *cgrp,
512 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700513{
Paul Menagebd89aab2007-10-18 23:40:44 -0700514 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700515 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700516 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800517 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700518 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700519
Ben Blumaae8aab2010-03-10 15:22:07 -0800520 /*
521 * Build the set of subsystem state objects that we want to see in the
522 * new css_set. while subsystems can change globally, the entries here
523 * won't change, so no need for locking.
524 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700525 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400526 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700527 /* Subsystem is in this hierarchy. So we want
528 * the subsystem state from the new
529 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400530 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700531 } else {
532 /* Subsystem is not in this hierarchy, so we
533 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700534 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700535 }
536 }
537
Li Zefan0ac801f2013-01-10 11:49:27 +0800538 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700539 hash_for_each_possible(css_set_table, cset, hlist, key) {
540 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700541 continue;
542
543 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700544 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700545 }
Paul Menage817929e2007-10-18 23:39:36 -0700546
547 /* No existing cgroup group matched */
548 return NULL;
549}
550
Tejun Heo69d02062013-06-12 21:04:50 -0700551static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700552{
Tejun Heo69d02062013-06-12 21:04:50 -0700553 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700554
Tejun Heo69d02062013-06-12 21:04:50 -0700555 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
556 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700557 kfree(link);
558 }
559}
560
Tejun Heo69d02062013-06-12 21:04:50 -0700561/**
562 * allocate_cgrp_cset_links - allocate cgrp_cset_links
563 * @count: the number of links to allocate
564 * @tmp_links: list_head the allocated links are put on
565 *
566 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
567 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700568 */
Tejun Heo69d02062013-06-12 21:04:50 -0700569static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700570{
Tejun Heo69d02062013-06-12 21:04:50 -0700571 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700572 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700573
574 INIT_LIST_HEAD(tmp_links);
575
Li Zefan36553432008-07-29 22:33:19 -0700576 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700577 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700578 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700579 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700580 return -ENOMEM;
581 }
Tejun Heo69d02062013-06-12 21:04:50 -0700582 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700583 }
584 return 0;
585}
586
Li Zefanc12f65d2009-01-07 18:07:42 -0800587/**
588 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700589 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700590 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800591 * @cgrp: the destination cgroup
592 */
Tejun Heo69d02062013-06-12 21:04:50 -0700593static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
594 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800595{
Tejun Heo69d02062013-06-12 21:04:50 -0700596 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800597
Tejun Heo69d02062013-06-12 21:04:50 -0700598 BUG_ON(list_empty(tmp_links));
599 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
600 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700601 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700602 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700603 /*
604 * Always add links to the tail of the list so that the list
605 * is sorted by order of hierarchy creation
606 */
Tejun Heo69d02062013-06-12 21:04:50 -0700607 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800608}
609
Tejun Heob326f9d2013-06-24 15:21:48 -0700610/**
611 * find_css_set - return a new css_set with one cgroup updated
612 * @old_cset: the baseline css_set
613 * @cgrp: the cgroup to be updated
614 *
615 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
616 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700617 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700618static struct css_set *find_css_set(struct css_set *old_cset,
619 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700620{
Tejun Heob326f9d2013-06-24 15:21:48 -0700621 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700622 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700623 struct list_head tmp_links;
624 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800625 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700626
Tejun Heob326f9d2013-06-24 15:21:48 -0700627 lockdep_assert_held(&cgroup_mutex);
628
Paul Menage817929e2007-10-18 23:39:36 -0700629 /* First see if we already have a cgroup group that matches
630 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700631 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700632 cset = find_existing_css_set(old_cset, cgrp, template);
633 if (cset)
634 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700635 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700636
Tejun Heo5abb8852013-06-12 21:04:49 -0700637 if (cset)
638 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700639
Tejun Heof4f4be22013-06-12 21:04:51 -0700640 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700641 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700642 return NULL;
643
Tejun Heo69d02062013-06-12 21:04:50 -0700644 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700645 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700646 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700647 return NULL;
648 }
649
Tejun Heo5abb8852013-06-12 21:04:49 -0700650 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700651 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700652 INIT_LIST_HEAD(&cset->tasks);
653 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700654
655 /* Copy the set of subsystem state objects generated in
656 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700657 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700658
659 write_lock(&css_set_lock);
660 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700661 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700662 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700663
Paul Menage7717f7b2009-09-23 15:56:22 -0700664 if (c->root == cgrp->root)
665 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700666 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700667 }
Paul Menage817929e2007-10-18 23:39:36 -0700668
Tejun Heo69d02062013-06-12 21:04:50 -0700669 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700670
Paul Menage817929e2007-10-18 23:39:36 -0700671 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700672
673 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700674 key = css_set_hash(cset->subsys);
675 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700676
Paul Menage817929e2007-10-18 23:39:36 -0700677 write_unlock(&css_set_lock);
678
Tejun Heo5abb8852013-06-12 21:04:49 -0700679 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700680}
681
Paul Menageddbcc7e2007-10-18 23:39:30 -0700682/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700683 * Return the cgroup for "task" from the given hierarchy. Must be
684 * called with cgroup_mutex held.
685 */
686static struct cgroup *task_cgroup_from_root(struct task_struct *task,
687 struct cgroupfs_root *root)
688{
Tejun Heo5abb8852013-06-12 21:04:49 -0700689 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700690 struct cgroup *res = NULL;
691
692 BUG_ON(!mutex_is_locked(&cgroup_mutex));
693 read_lock(&css_set_lock);
694 /*
695 * No need to lock the task - since we hold cgroup_mutex the
696 * task can't change groups, so the only thing that can happen
697 * is that it exits and its css is set back to init_css_set.
698 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700699 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700700 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700701 res = &root->top_cgroup;
702 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700703 struct cgrp_cset_link *link;
704
705 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700706 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700707
Paul Menage7717f7b2009-09-23 15:56:22 -0700708 if (c->root == root) {
709 res = c;
710 break;
711 }
712 }
713 }
714 read_unlock(&css_set_lock);
715 BUG_ON(!res);
716 return res;
717}
718
719/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700720 * There is one global cgroup mutex. We also require taking
721 * task_lock() when dereferencing a task's cgroup subsys pointers.
722 * See "The task_lock() exception", at the end of this comment.
723 *
724 * A task must hold cgroup_mutex to modify cgroups.
725 *
726 * Any task can increment and decrement the count field without lock.
727 * So in general, code holding cgroup_mutex can't rely on the count
728 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800729 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700730 * means that no tasks are currently attached, therefore there is no
731 * way a task attached to that cgroup can fork (the other way to
732 * increment the count). So code holding cgroup_mutex can safely
733 * assume that if the count is zero, it will stay zero. Similarly, if
734 * a task holds cgroup_mutex on a cgroup with zero count, it
735 * knows that the cgroup won't be removed, as cgroup_rmdir()
736 * needs that mutex.
737 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700738 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
739 * (usually) take cgroup_mutex. These are the two most performance
740 * critical pieces of code here. The exception occurs on cgroup_exit(),
741 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
742 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800743 * to the release agent with the name of the cgroup (path relative to
744 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700745 *
746 * A cgroup can only be deleted if both its 'count' of using tasks
747 * is zero, and its list of 'children' cgroups is empty. Since all
748 * tasks in the system use _some_ cgroup, and since there is always at
749 * least one task in the system (init, pid == 1), therefore, top_cgroup
750 * always has either children cgroups and/or using tasks. So we don't
751 * need a special hack to ensure that top_cgroup cannot be deleted.
752 *
753 * The task_lock() exception
754 *
755 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800756 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800757 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700758 * several performance critical places that need to reference
759 * task->cgroup without the expense of grabbing a system global
760 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800761 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700762 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
763 * the task_struct routinely used for such matters.
764 *
765 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800766 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700767 */
768
Paul Menageddbcc7e2007-10-18 23:39:30 -0700769/*
770 * A couple of forward declarations required, due to cyclic reference loop:
771 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
772 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
773 * -> cgroup_mkdir.
774 */
775
Al Viro18bb1db2011-07-26 01:41:39 -0400776static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700777static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700778static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700779static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700780static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700781
782static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200783 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700784 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700785};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700786
Al Viroa5e7ed32011-07-26 01:55:55 -0400787static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788{
789 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700790
791 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400792 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100794 inode->i_uid = current_fsuid();
795 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
797 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
798 }
799 return inode;
800}
801
Li Zefan65dff752013-03-01 15:01:56 +0800802static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
803{
804 struct cgroup_name *name;
805
806 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
807 if (!name)
808 return NULL;
809 strcpy(name->name, dentry->d_name.name);
810 return name;
811}
812
Li Zefanbe445622013-01-24 14:31:42 +0800813static void cgroup_free_fn(struct work_struct *work)
814{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700815 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800816
817 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800818 cgrp->root->number_of_cgroups--;
819 mutex_unlock(&cgroup_mutex);
820
821 /*
Li Zefan415cf072013-04-08 14:35:02 +0800822 * We get a ref to the parent's dentry, and put the ref when
823 * this cgroup is being freed, so it's guaranteed that the
824 * parent won't be destroyed before its children.
825 */
826 dput(cgrp->parent->dentry);
827
828 /*
Li Zefanbe445622013-01-24 14:31:42 +0800829 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700830 * created the cgroup. This will free cgrp->root, if we are
831 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800832 */
833 deactivate_super(cgrp->root->sb);
834
Tejun Heob1a21362013-11-29 10:42:58 -0500835 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800836
837 simple_xattrs_free(&cgrp->xattrs);
838
Li Zefan65dff752013-03-01 15:01:56 +0800839 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800840 kfree(cgrp);
841}
842
843static void cgroup_free_rcu(struct rcu_head *head)
844{
845 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
846
Tejun Heoea15f8c2013-06-13 19:27:42 -0700847 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500848 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800849}
850
Paul Menageddbcc7e2007-10-18 23:39:30 -0700851static void cgroup_diput(struct dentry *dentry, struct inode *inode)
852{
853 /* is dentry a directory ? if so, kfree() associated cgroup */
854 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700855 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800856
Tejun Heo54766d42013-06-12 21:04:53 -0700857 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanc1a71502013-12-17 11:13:39 +0800858
859 /*
860 * XXX: cgrp->id is only used to look up css's. As cgroup
861 * and css's lifetimes will be decoupled, it should be made
862 * per-subsystem and moved to css->id so that lookups are
863 * successful until the target css is released.
864 */
Li Zefan0ab02ca2014-02-11 16:05:46 +0800865 mutex_lock(&cgroup_mutex);
Li Zefanc1a71502013-12-17 11:13:39 +0800866 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +0800867 mutex_unlock(&cgroup_mutex);
Li Zefanc1a71502013-12-17 11:13:39 +0800868 cgrp->id = -1;
869
Li Zefanbe445622013-01-24 14:31:42 +0800870 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700871 } else {
872 struct cfent *cfe = __d_cfe(dentry);
873 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
874
875 WARN_ONCE(!list_empty(&cfe->node) &&
876 cgrp != &cgrp->root->top_cgroup,
877 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700878 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700879 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700880 }
881 iput(inode);
882}
883
884static void remove_dir(struct dentry *d)
885{
886 struct dentry *parent = dget(d->d_parent);
887
888 d_delete(d);
889 simple_rmdir(parent->d_inode, d);
890 dput(parent);
891}
892
Li Zefan2739d3c2013-01-21 18:18:33 +0800893static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700894{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700895 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700896
Tejun Heo05ef1d72012-04-01 12:09:56 -0700897 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -0500898 lockdep_assert_held(&cgroup_tree_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100899
Li Zefan2739d3c2013-01-21 18:18:33 +0800900 /*
901 * If we're doing cleanup due to failure of cgroup_create(),
902 * the corresponding @cfe may not exist.
903 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700904 list_for_each_entry(cfe, &cgrp->files, node) {
905 struct dentry *d = cfe->dentry;
906
907 if (cft && cfe->type != cft)
908 continue;
909
910 dget(d);
911 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700912 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700913 list_del_init(&cfe->node);
914 dput(d);
915
Li Zefan2739d3c2013-01-21 18:18:33 +0800916 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700917 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700918}
919
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400920/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700921 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700922 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400923 * @subsys_mask: mask of the subsystem ids whose files should be removed
924 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700925static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700926{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400927 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700928 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700929
Tejun Heob420ba72013-07-12 12:34:02 -0700930 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400931 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700932
933 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400934 continue;
935 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400936 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400937 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700938}
939
940/*
941 * NOTE : the dentry must have been dget()'ed
942 */
943static void cgroup_d_remove_dir(struct dentry *dentry)
944{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100945 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700946
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100947 parent = dentry->d_parent;
948 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800949 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700950 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100951 spin_unlock(&dentry->d_lock);
952 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700953 remove_dir(dentry);
954}
955
956static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700957 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700958{
Paul Menagebd89aab2007-10-18 23:40:44 -0700959 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700960 struct cgroup_subsys *ss;
Tejun Heo31261212013-06-28 17:07:30 -0700961 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700962
Tejun Heoace2bee2014-02-11 11:52:47 -0500963 lockdep_assert_held(&cgroup_tree_mutex);
964 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -0800965
Paul Menageddbcc7e2007-10-18 23:39:30 -0700966 /* Check that any added subsystems are currently free */
Tejun Heo3ed80a62014-02-08 10:36:58 -0500967 for_each_subsys(ss, i)
968 if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
969 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700970
Tejun Heo31261212013-06-28 17:07:30 -0700971 ret = cgroup_populate_dir(cgrp, added_mask);
972 if (ret)
Tejun Heo3ed80a62014-02-08 10:36:58 -0500973 return ret;
Tejun Heo31261212013-06-28 17:07:30 -0700974
975 /*
976 * Nothing can fail from this point on. Remove files for the
977 * removed subsystems and rebind each subsystem.
978 */
Tejun Heo4ac06012014-02-11 11:52:47 -0500979 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -0700980 cgroup_clear_dir(cgrp, removed_mask);
Tejun Heo4ac06012014-02-11 11:52:47 -0500981 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -0700982
Tejun Heo30159ec2013-06-25 11:53:37 -0700983 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -0700985
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400986 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400988 BUG_ON(cgroup_css(cgrp, ss));
989 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
990 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -0700991
Tejun Heo73e80ed2013-08-13 11:01:55 -0400992 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -0400993 cgroup_css(cgroup_dummy_top, ss));
994 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -0400995
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800996 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -0400998 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -0700999
Ben Blumcf5d5942010-03-10 15:22:09 -08001000 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001001 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001002 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001004 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1005 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001006
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001008 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001009
Tejun Heoca8bdca2013-08-26 18:40:56 -04001010 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001011 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1012
Tejun Heo9871bf92013-06-24 15:21:47 -07001013 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001014 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015 }
1016 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017
Tejun Heo1672d042013-06-25 18:04:54 -07001018 /*
1019 * Mark @root has finished binding subsystems. @root->subsys_mask
1020 * now matches the bound subsystems.
1021 */
1022 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1023
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024 return 0;
1025}
1026
Al Viro34c80b12011-12-08 21:32:45 -05001027static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028{
Al Viro34c80b12011-12-08 21:32:45 -05001029 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001031 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032
Tejun Heob85d2042013-12-06 15:11:57 -05001033 for_each_subsys(ss, ssid)
1034 if (root->subsys_mask & (1 << ssid))
1035 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001036 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1037 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001038 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001040 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001041 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001042
1043 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001044 if (strlen(root->release_agent_path))
1045 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001046 spin_unlock(&release_agent_path_lock);
1047
Tejun Heo2260e7f2012-11-19 08:13:38 -08001048 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001049 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001050 if (strlen(root->name))
1051 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052 return 0;
1053}
1054
1055struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001056 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001058 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001059 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001060 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001061 /* User explicitly requested empty subsystem */
1062 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001063
1064 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001065
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066};
1067
Ben Blumaae8aab2010-03-10 15:22:07 -08001068/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001069 * Convert a hierarchy specifier into a bitmask of subsystems and
1070 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1071 * array. This function takes refcounts on subsystems to be used, unless it
1072 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001073 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001074static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001076 char *token, *o = data;
1077 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001078 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001079 struct cgroup_subsys *ss;
1080 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001081
Ben Blumaae8aab2010-03-10 15:22:07 -08001082 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1083
Li Zefanf9ab5b52009-06-17 16:26:33 -07001084#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001085 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001086#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087
Paul Menagec6d57f32009-09-23 15:56:19 -07001088 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089
1090 while ((token = strsep(&o, ",")) != NULL) {
1091 if (!*token)
1092 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001093 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001094 /* Explicitly have no subsystems */
1095 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001096 continue;
1097 }
1098 if (!strcmp(token, "all")) {
1099 /* Mutually exclusive option 'all' + subsystem name */
1100 if (one_ss)
1101 return -EINVAL;
1102 all_ss = true;
1103 continue;
1104 }
Tejun Heo873fe092013-04-14 20:15:26 -07001105 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1106 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1107 continue;
1108 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001109 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001110 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001111 continue;
1112 }
1113 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001114 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001115 continue;
1116 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001117 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001118 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001119 continue;
1120 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001121 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001122 /* Specifying two release agents is forbidden */
1123 if (opts->release_agent)
1124 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001125 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001126 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001127 if (!opts->release_agent)
1128 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001129 continue;
1130 }
1131 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001132 const char *name = token + 5;
1133 /* Can't specify an empty name */
1134 if (!strlen(name))
1135 return -EINVAL;
1136 /* Must match [\w.-]+ */
1137 for (i = 0; i < strlen(name); i++) {
1138 char c = name[i];
1139 if (isalnum(c))
1140 continue;
1141 if ((c == '.') || (c == '-') || (c == '_'))
1142 continue;
1143 return -EINVAL;
1144 }
1145 /* Specifying two names is forbidden */
1146 if (opts->name)
1147 return -EINVAL;
1148 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001149 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001150 GFP_KERNEL);
1151 if (!opts->name)
1152 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001153
1154 continue;
1155 }
1156
Tejun Heo30159ec2013-06-25 11:53:37 -07001157 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001158 if (strcmp(token, ss->name))
1159 continue;
1160 if (ss->disabled)
1161 continue;
1162
1163 /* Mutually exclusive option 'all' + subsystem name */
1164 if (all_ss)
1165 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001166 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001167 one_ss = true;
1168
1169 break;
1170 }
1171 if (i == CGROUP_SUBSYS_COUNT)
1172 return -ENOENT;
1173 }
1174
1175 /*
1176 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001177 * otherwise if 'none', 'name=' and a subsystem name options
1178 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001179 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001180 if (all_ss || (!one_ss && !opts->none && !opts->name))
1181 for_each_subsys(ss, i)
1182 if (!ss->disabled)
1183 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001184
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001185 /* Consistency checks */
1186
Tejun Heo873fe092013-04-14 20:15:26 -07001187 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1188 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1189
1190 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1191 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1192 return -EINVAL;
1193 }
1194
1195 if (opts->cpuset_clone_children) {
1196 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1197 return -EINVAL;
1198 }
1199 }
1200
Li Zefanf9ab5b52009-06-17 16:26:33 -07001201 /*
1202 * Option noprefix was introduced just for backward compatibility
1203 * with the old cpuset, so we allow noprefix only if mounting just
1204 * the cpuset subsystem.
1205 */
Tejun Heo93438622013-04-14 20:15:25 -07001206 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001207 return -EINVAL;
1208
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001209
1210 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001211 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001212 return -EINVAL;
1213
1214 /*
1215 * We either have to specify by name or by subsystems. (So all
1216 * empty hierarchies must have a name).
1217 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001218 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001219 return -EINVAL;
1220
1221 return 0;
1222}
1223
1224static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1225{
1226 int ret = 0;
1227 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001228 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001229 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001230 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001231
Tejun Heo873fe092013-04-14 20:15:26 -07001232 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1233 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1234 return -EINVAL;
1235 }
1236
Paul Menagebd89aab2007-10-18 23:40:44 -07001237 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001238 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001239 mutex_lock(&cgroup_mutex);
1240
1241 /* See what subsystems are wanted */
1242 ret = parse_cgroupfs_options(data, &opts);
1243 if (ret)
1244 goto out_unlock;
1245
Tejun Heoa8a648c2013-06-24 15:21:47 -07001246 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001247 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1248 task_tgid_nr(current), current->comm);
1249
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001250 added_mask = opts.subsys_mask & ~root->subsys_mask;
1251 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001252
Ben Blumcf5d5942010-03-10 15:22:09 -08001253 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001254 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001255 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001256 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1257 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1258 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001259 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001260 goto out_unlock;
1261 }
1262
Tejun Heof172e672013-06-28 17:07:30 -07001263 /* remounting is not allowed for populated hierarchies */
1264 if (root->number_of_cgroups > 1) {
1265 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001266 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001267 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001268
Paul Menageddbcc7e2007-10-18 23:39:30 -07001269 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001270 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001271 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001272
Tejun Heo69e943b2014-02-08 10:36:58 -05001273 if (opts.release_agent) {
1274 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001275 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001276 spin_unlock(&release_agent_path_lock);
1277 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001278 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001279 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001280 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001281 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001282 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001283 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001284 return ret;
1285}
1286
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001287static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001288 .statfs = simple_statfs,
1289 .drop_inode = generic_delete_inode,
1290 .show_options = cgroup_show_options,
1291 .remount_fs = cgroup_remount,
1292};
1293
Paul Menagecc31edc2008-10-18 20:28:04 -07001294static void init_cgroup_housekeeping(struct cgroup *cgrp)
1295{
1296 INIT_LIST_HEAD(&cgrp->sibling);
1297 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001298 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001299 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001300 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001301 INIT_LIST_HEAD(&cgrp->pidlists);
1302 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001303 cgrp->dummy_css.cgroup = cgrp;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001304 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001305}
Paul Menagec6d57f32009-09-23 15:56:19 -07001306
Paul Menageddbcc7e2007-10-18 23:39:30 -07001307static void init_cgroup_root(struct cgroupfs_root *root)
1308{
Paul Menagebd89aab2007-10-18 23:40:44 -07001309 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001310
Paul Menageddbcc7e2007-10-18 23:39:30 -07001311 INIT_LIST_HEAD(&root->root_list);
1312 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001313 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001314 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001315 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001316 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001317}
1318
Tejun Heofc76df72013-06-25 11:53:37 -07001319static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001320{
Tejun Heo1a574232013-04-14 11:36:58 -07001321 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001322
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001323 lockdep_assert_held(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001324
Tejun Heofc76df72013-06-25 11:53:37 -07001325 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1326 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001327 if (id < 0)
1328 return id;
1329
1330 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001331 return 0;
1332}
1333
1334static void cgroup_exit_root_id(struct cgroupfs_root *root)
1335{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001336 lockdep_assert_held(&cgroup_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001337
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001338 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001339 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001340 root->hierarchy_id = 0;
1341 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001342}
1343
Paul Menageddbcc7e2007-10-18 23:39:30 -07001344static int cgroup_test_super(struct super_block *sb, void *data)
1345{
Paul Menagec6d57f32009-09-23 15:56:19 -07001346 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001347 struct cgroupfs_root *root = sb->s_fs_info;
1348
Paul Menagec6d57f32009-09-23 15:56:19 -07001349 /* If we asked for a name then it must match */
1350 if (opts->name && strcmp(opts->name, root->name))
1351 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001352
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001353 /*
1354 * If we asked for subsystems (or explicitly for no
1355 * subsystems) then they must match
1356 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001357 if ((opts->subsys_mask || opts->none)
1358 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001359 return 0;
1360
1361 return 1;
1362}
1363
Paul Menagec6d57f32009-09-23 15:56:19 -07001364static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1365{
1366 struct cgroupfs_root *root;
1367
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001368 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001369 return NULL;
1370
1371 root = kzalloc(sizeof(*root), GFP_KERNEL);
1372 if (!root)
1373 return ERR_PTR(-ENOMEM);
1374
1375 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001376
Tejun Heo1672d042013-06-25 18:04:54 -07001377 /*
1378 * We need to set @root->subsys_mask now so that @root can be
1379 * matched by cgroup_test_super() before it finishes
1380 * initialization; otherwise, competing mounts with the same
1381 * options may try to bind the same subsystems instead of waiting
1382 * for the first one leading to unexpected mount errors.
1383 * SUBSYS_BOUND will be set once actual binding is complete.
1384 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001385 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001386 root->flags = opts->flags;
1387 if (opts->release_agent)
1388 strcpy(root->release_agent_path, opts->release_agent);
1389 if (opts->name)
1390 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001391 if (opts->cpuset_clone_children)
1392 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001393 return root;
1394}
1395
Tejun Heofa3ca072013-04-14 11:36:56 -07001396static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001397{
Tejun Heofa3ca072013-04-14 11:36:56 -07001398 if (root) {
1399 /* hierarhcy ID shoulid already have been released */
1400 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001401
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001402 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001403 kfree(root);
1404 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001405}
1406
Paul Menageddbcc7e2007-10-18 23:39:30 -07001407static int cgroup_set_super(struct super_block *sb, void *data)
1408{
1409 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001410 struct cgroup_sb_opts *opts = data;
1411
1412 /* If we don't have a new root, we can't set up a new sb */
1413 if (!opts->new_root)
1414 return -EINVAL;
1415
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001416 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001417
1418 ret = set_anon_super(sb, NULL);
1419 if (ret)
1420 return ret;
1421
Paul Menagec6d57f32009-09-23 15:56:19 -07001422 sb->s_fs_info = opts->new_root;
1423 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001424
1425 sb->s_blocksize = PAGE_CACHE_SIZE;
1426 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1427 sb->s_magic = CGROUP_SUPER_MAGIC;
1428 sb->s_op = &cgroup_ops;
1429
1430 return 0;
1431}
1432
1433static int cgroup_get_rootdir(struct super_block *sb)
1434{
Al Viro0df6a632010-12-21 13:29:29 -05001435 static const struct dentry_operations cgroup_dops = {
1436 .d_iput = cgroup_diput,
Al Virob26d4cd2013-10-25 18:47:37 -04001437 .d_delete = always_delete_dentry,
Al Viro0df6a632010-12-21 13:29:29 -05001438 };
1439
Paul Menageddbcc7e2007-10-18 23:39:30 -07001440 struct inode *inode =
1441 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442
1443 if (!inode)
1444 return -ENOMEM;
1445
Paul Menageddbcc7e2007-10-18 23:39:30 -07001446 inode->i_fop = &simple_dir_operations;
1447 inode->i_op = &cgroup_dir_inode_operations;
1448 /* directories start off with i_nlink == 2 (for "." entry) */
1449 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001450 sb->s_root = d_make_root(inode);
1451 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001452 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001453 /* for everything else we want ->d_op set */
1454 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001455 return 0;
1456}
1457
Al Virof7e83572010-07-26 13:23:11 +04001458static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001459 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001460 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461{
1462 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001463 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001464 int ret = 0;
1465 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001466 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001467 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001468 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001469 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001470
1471 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001472 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001473 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001474 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001475 if (ret)
1476 goto out_err;
1477
1478 /*
1479 * Allocate a new cgroup root. We may not need it if we're
1480 * reusing an existing hierarchy.
1481 */
1482 new_root = cgroup_root_from_opts(&opts);
1483 if (IS_ERR(new_root)) {
1484 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001485 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001486 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001487 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001488
Paul Menagec6d57f32009-09-23 15:56:19 -07001489 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001490 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001491 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001492 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001493 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001494 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001495 }
1496
Paul Menagec6d57f32009-09-23 15:56:19 -07001497 root = sb->s_fs_info;
1498 BUG_ON(!root);
1499 if (root == opts.new_root) {
1500 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001501 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001502 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001503 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001504 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001505
1506 BUG_ON(sb->s_root != NULL);
1507
1508 ret = cgroup_get_rootdir(sb);
1509 if (ret)
1510 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001511 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512
Paul Menage817929e2007-10-18 23:39:36 -07001513 mutex_lock(&inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001514 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001515 mutex_lock(&cgroup_mutex);
1516
Tejun Heoeb46bf82014-02-08 10:26:33 -05001517 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1518 if (ret < 0)
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001519 goto unlock_drop;
Tejun Heoeb46bf82014-02-08 10:26:33 -05001520 root_cgrp->id = ret;
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001521
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001522 /* Check for name clashes with existing mounts */
1523 ret = -EBUSY;
1524 if (strlen(root->name))
1525 for_each_active_root(existing_root)
1526 if (!strcmp(existing_root->name, root->name))
1527 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001528
Paul Menage817929e2007-10-18 23:39:36 -07001529 /*
1530 * We're accessing css_set_count without locking
1531 * css_set_lock here, but that's OK - it can only be
1532 * increased by someone holding cgroup_lock, and
1533 * that's us. The worst that can happen is that we
1534 * have some link structures left over
1535 */
Tejun Heo69d02062013-06-12 21:04:50 -07001536 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001537 if (ret)
1538 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001539
Tejun Heofc76df72013-06-25 11:53:37 -07001540 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1541 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001542 if (ret)
1543 goto unlock_drop;
1544
Tejun Heo31261212013-06-28 17:07:30 -07001545 sb->s_root->d_fsdata = root_cgrp;
1546 root_cgrp->dentry = sb->s_root;
1547
1548 /*
1549 * We're inside get_sb() and will call lookup_one_len() to
1550 * create the root files, which doesn't work if SELinux is
1551 * in use. The following cred dancing somehow works around
1552 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1553 * populating new cgroupfs mount") for more details.
1554 */
1555 cred = override_creds(&init_cred);
1556
Tejun Heo2bb566c2013-08-08 20:11:23 -04001557 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001558 if (ret)
1559 goto rm_base_files;
1560
Tejun Heoa8a648c2013-06-24 15:21:47 -07001561 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001562 if (ret)
1563 goto rm_base_files;
1564
1565 revert_creds(cred);
1566
Ben Blumcf5d5942010-03-10 15:22:09 -08001567 /*
1568 * There must be no failure case after here, since rebinding
1569 * takes care of subsystems' refcounts, which are explicitly
1570 * dropped in the failure exit path.
1571 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001572
Tejun Heo9871bf92013-06-24 15:21:47 -07001573 list_add(&root->root_list, &cgroup_roots);
1574 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575
Paul Menage817929e2007-10-18 23:39:36 -07001576 /* Link the top cgroup in this hierarchy into all
1577 * the css_set objects */
1578 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001579 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001580 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001581 write_unlock(&css_set_lock);
1582
Tejun Heo69d02062013-06-12 21:04:50 -07001583 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001584
Li Zefanc12f65d2009-01-07 18:07:42 -08001585 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586 BUG_ON(root->number_of_cgroups != 1);
1587
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001589 mutex_unlock(&cgroup_tree_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001590 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001591 } else {
1592 /*
1593 * We re-used an existing hierarchy - the new root (if
1594 * any) is not needed
1595 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001596 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001597
Tejun Heoc7ba8282013-06-29 14:06:10 -07001598 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001599 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1600 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1601 ret = -EINVAL;
1602 goto drop_new_super;
1603 } else {
1604 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1605 }
Tejun Heo873fe092013-04-14 20:15:26 -07001606 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607 }
1608
Paul Menagec6d57f32009-09-23 15:56:19 -07001609 kfree(opts.release_agent);
1610 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001611 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612
Tejun Heo31261212013-06-28 17:07:30 -07001613 rm_base_files:
1614 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001615 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001616 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001617 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001618 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001619 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001620 mutex_unlock(&cgroup_tree_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001621 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001622 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001623 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001624 out_err:
1625 kfree(opts.release_agent);
1626 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001627 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628}
1629
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001630static void cgroup_kill_sb(struct super_block *sb)
1631{
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001633 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001634 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001635 int ret;
1636
1637 BUG_ON(!root);
1638
1639 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001640 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001641
Tejun Heo31261212013-06-28 17:07:30 -07001642 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001643 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644 mutex_lock(&cgroup_mutex);
1645
1646 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001647 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1648 ret = rebind_subsystems(root, 0, root->subsys_mask);
1649 /* Shouldn't be able to fail ... */
1650 BUG_ON(ret);
1651 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652
Paul Menage817929e2007-10-18 23:39:36 -07001653 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001654 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001655 * root cgroup
1656 */
1657 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001658
Tejun Heo69d02062013-06-12 21:04:50 -07001659 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1660 list_del(&link->cset_link);
1661 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001662 kfree(link);
1663 }
1664 write_unlock(&css_set_lock);
1665
Paul Menage839ec542009-01-29 14:25:22 -08001666 if (!list_empty(&root->root_list)) {
1667 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001668 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001669 }
Li Zefane5f6a862009-01-07 18:07:41 -08001670
Tejun Heofa3ca072013-04-14 11:36:56 -07001671 cgroup_exit_root_id(root);
1672
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001674 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001675 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001677 simple_xattrs_free(&cgrp->xattrs);
1678
Paul Menageddbcc7e2007-10-18 23:39:30 -07001679 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001680 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001681}
1682
1683static struct file_system_type cgroup_fs_type = {
1684 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001685 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001686 .kill_sb = cgroup_kill_sb,
1687};
1688
Greg KH676db4a2010-08-05 13:53:35 -07001689static struct kobject *cgroup_kobj;
1690
Li Zefana043e3b2008-02-23 15:24:09 -08001691/**
1692 * cgroup_path - generate the path of a cgroup
1693 * @cgrp: the cgroup in question
1694 * @buf: the buffer to write the path into
1695 * @buflen: the length of the buffer
1696 *
Li Zefan65dff752013-03-01 15:01:56 +08001697 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1698 *
1699 * We can't generate cgroup path using dentry->d_name, as accessing
1700 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1701 * inode's i_mutex, while on the other hand cgroup_path() can be called
1702 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001704int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705{
Li Zefan65dff752013-03-01 15:01:56 +08001706 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001707 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001708
Tejun Heoda1f2962013-04-14 10:32:19 -07001709 if (!cgrp->parent) {
1710 if (strlcpy(buf, "/", buflen) >= buflen)
1711 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001712 return 0;
1713 }
1714
Tao Ma316eb662012-11-08 21:36:38 +08001715 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001716 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001717
Li Zefan65dff752013-03-01 15:01:56 +08001718 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001719 do {
Li Zefan65dff752013-03-01 15:01:56 +08001720 const char *name = cgroup_name(cgrp);
1721 int len;
1722
1723 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001725 goto out;
1726 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001727
Paul Menageddbcc7e2007-10-18 23:39:30 -07001728 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001729 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001731
1732 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001733 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001734 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001735 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001736out:
1737 rcu_read_unlock();
1738 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739}
Ben Blum67523c42010-03-10 15:22:11 -08001740EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001741
Tejun Heo857a2be2013-04-14 20:50:08 -07001742/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001743 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001744 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001745 * @buf: the buffer to write the path into
1746 * @buflen: the length of the buffer
1747 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001748 * Determine @task's cgroup on the first (the one with the lowest non-zero
1749 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1750 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1751 * cgroup controller callbacks.
1752 *
1753 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001754 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001755int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001756{
1757 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001758 struct cgroup *cgrp;
1759 int hierarchy_id = 1, ret = 0;
1760
1761 if (buflen < 2)
1762 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001763
1764 mutex_lock(&cgroup_mutex);
1765
Tejun Heo913ffdb2013-07-11 16:34:48 -07001766 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1767
Tejun Heo857a2be2013-04-14 20:50:08 -07001768 if (root) {
1769 cgrp = task_cgroup_from_root(task, root);
1770 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001771 } else {
1772 /* if no hierarchy exists, everyone is in "/" */
1773 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001774 }
1775
1776 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001777 return ret;
1778}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001779EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001780
Ben Blum74a11662011-05-26 16:25:20 -07001781/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001782 * Control Group taskset
1783 */
Tejun Heo134d3372011-12-12 18:12:21 -08001784struct task_and_cgroup {
1785 struct task_struct *task;
1786 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001787 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001788};
1789
Tejun Heo2f7ee562011-12-12 18:12:21 -08001790struct cgroup_taskset {
1791 struct task_and_cgroup single;
1792 struct flex_array *tc_array;
1793 int tc_array_len;
1794 int idx;
1795 struct cgroup *cur_cgrp;
1796};
1797
1798/**
1799 * cgroup_taskset_first - reset taskset and return the first task
1800 * @tset: taskset of interest
1801 *
1802 * @tset iteration is initialized and the first task is returned.
1803 */
1804struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1805{
1806 if (tset->tc_array) {
1807 tset->idx = 0;
1808 return cgroup_taskset_next(tset);
1809 } else {
1810 tset->cur_cgrp = tset->single.cgrp;
1811 return tset->single.task;
1812 }
1813}
1814EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1815
1816/**
1817 * cgroup_taskset_next - iterate to the next task in taskset
1818 * @tset: taskset of interest
1819 *
1820 * Return the next task in @tset. Iteration must have been initialized
1821 * with cgroup_taskset_first().
1822 */
1823struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1824{
1825 struct task_and_cgroup *tc;
1826
1827 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1828 return NULL;
1829
1830 tc = flex_array_get(tset->tc_array, tset->idx++);
1831 tset->cur_cgrp = tc->cgrp;
1832 return tc->task;
1833}
1834EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1835
1836/**
Tejun Heod99c8722013-08-08 20:11:27 -04001837 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001838 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001839 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001840 *
Tejun Heod99c8722013-08-08 20:11:27 -04001841 * Return the css for the current (last returned) task of @tset for
1842 * subsystem specified by @subsys_id. This function must be preceded by
1843 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001844 */
Tejun Heod99c8722013-08-08 20:11:27 -04001845struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1846 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001847{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001848 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001849}
Tejun Heod99c8722013-08-08 20:11:27 -04001850EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001851
1852/**
1853 * cgroup_taskset_size - return the number of tasks in taskset
1854 * @tset: taskset of interest
1855 */
1856int cgroup_taskset_size(struct cgroup_taskset *tset)
1857{
1858 return tset->tc_array ? tset->tc_array_len : 1;
1859}
1860EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1861
1862
Ben Blum74a11662011-05-26 16:25:20 -07001863/*
1864 * cgroup_task_migrate - move a task from one cgroup to another.
1865 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001866 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001867 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001868static void cgroup_task_migrate(struct cgroup *old_cgrp,
1869 struct task_struct *tsk,
1870 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001871{
Tejun Heo5abb8852013-06-12 21:04:49 -07001872 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001873
1874 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001875 * We are synchronized through threadgroup_lock() against PF_EXITING
1876 * setting such that we can't race against cgroup_exit() changing the
1877 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001878 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001879 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001880 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001881
Ben Blum74a11662011-05-26 16:25:20 -07001882 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001883 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001884 task_unlock(tsk);
1885
1886 /* Update the css_set linked lists if we're using them */
1887 write_lock(&css_set_lock);
1888 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001889 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001890 write_unlock(&css_set_lock);
1891
1892 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001893 * We just gained a reference on old_cset by taking it from the
1894 * task. As trading it for new_cset is protected by cgroup_mutex,
1895 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001896 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001897 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1898 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001899}
1900
Li Zefana043e3b2008-02-23 15:24:09 -08001901/**
Li Zefan081aa452013-03-13 09:17:09 +08001902 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001903 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001904 * @tsk: the task or the leader of the threadgroup to be attached
1905 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001906 *
Tejun Heo257058a2011-12-12 18:12:21 -08001907 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001908 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001909 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001910static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1911 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001912{
1913 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001914 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001915 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001916 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001917 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001918 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001919 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001920 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001921
1922 /*
1923 * step 0: in order to do expensive, possibly blocking operations for
1924 * every thread, we cannot iterate the thread group list, since it needs
1925 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001926 * group - group_rwsem prevents new threads from appearing, and if
1927 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001928 */
Li Zefan081aa452013-03-13 09:17:09 +08001929 if (threadgroup)
1930 group_size = get_nr_threads(tsk);
1931 else
1932 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001933 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001934 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001935 if (!group)
1936 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001937 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001938 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001939 if (retval)
1940 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001941
Ben Blum74a11662011-05-26 16:25:20 -07001942 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001943 /*
1944 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1945 * already PF_EXITING could be freed from underneath us unless we
1946 * take an rcu_read_lock.
1947 */
1948 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001949 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001950 struct task_and_cgroup ent;
1951
Tejun Heocd3d0952011-12-12 18:12:21 -08001952 /* @tsk either already exited or can't exit until the end */
1953 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001954 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001955
Ben Blum74a11662011-05-26 16:25:20 -07001956 /* as per above, nr_threads may decrease, but not increase. */
1957 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001958 ent.task = tsk;
1959 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001960 /* nothing to do if this task is already in the cgroup */
1961 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001962 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001963 /*
1964 * saying GFP_ATOMIC has no effect here because we did prealloc
1965 * earlier, but it's good form to communicate our expectations.
1966 */
Tejun Heo134d3372011-12-12 18:12:21 -08001967 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07001968 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07001969 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08001970 next:
Li Zefan081aa452013-03-13 09:17:09 +08001971 if (!threadgroup)
1972 break;
Ben Blum74a11662011-05-26 16:25:20 -07001973 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001974 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07001975 /* remember the number of threads in the array for later. */
1976 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001977 tset.tc_array = group;
1978 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001979
Tejun Heo134d3372011-12-12 18:12:21 -08001980 /* methods shouldn't be called if no task is actually migrating */
1981 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001982 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08001983 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08001984
Ben Blum74a11662011-05-26 16:25:20 -07001985 /*
1986 * step 1: check that we can legitimately attach to the cgroup.
1987 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001988 for_each_css(css, i, cgrp) {
1989 if (css->ss->can_attach) {
1990 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001991 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001992 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07001993 goto out_cancel_attach;
1994 }
1995 }
Ben Blum74a11662011-05-26 16:25:20 -07001996 }
1997
1998 /*
1999 * step 2: make sure css_sets exist for all threads to be migrated.
2000 * we use find_css_set, which allocates a new one if necessary.
2001 */
Ben Blum74a11662011-05-26 16:25:20 -07002002 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002003 struct css_set *old_cset;
2004
Tejun Heo134d3372011-12-12 18:12:21 -08002005 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002006 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002007 tc->cset = find_css_set(old_cset, cgrp);
2008 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002009 retval = -ENOMEM;
2010 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002011 }
2012 }
2013
2014 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002015 * step 3: now that we're guaranteed success wrt the css_sets,
2016 * proceed to move all tasks to the new cgroup. There are no
2017 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002018 */
Ben Blum74a11662011-05-26 16:25:20 -07002019 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002020 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002021 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002022 }
2023 /* nothing is sensitive to fork() after this point. */
2024
2025 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002026 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002027 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05002028 for_each_css(css, i, cgrp)
2029 if (css->ss->attach)
2030 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002031
2032 /*
2033 * step 5: success! and cleanup
2034 */
Ben Blum74a11662011-05-26 16:25:20 -07002035 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002036out_put_css_set_refs:
2037 if (retval) {
2038 for (i = 0; i < group_size; i++) {
2039 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002040 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002041 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002042 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002043 }
Ben Blum74a11662011-05-26 16:25:20 -07002044 }
2045out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002046 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002047 for_each_css(css, i, cgrp) {
2048 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07002049 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05002050 if (css->ss->cancel_attach)
2051 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002052 }
2053 }
Ben Blum74a11662011-05-26 16:25:20 -07002054out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002055 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002056 return retval;
2057}
2058
2059/*
2060 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002061 * function to attach either it or all tasks in its threadgroup. Will lock
2062 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002063 */
2064static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002065{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002066 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002067 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002068 int ret;
2069
Ben Blum74a11662011-05-26 16:25:20 -07002070 if (!cgroup_lock_live_group(cgrp))
2071 return -ENODEV;
2072
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002073retry_find_task:
2074 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002075 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002076 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002077 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002078 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002079 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002080 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002081 }
Ben Blum74a11662011-05-26 16:25:20 -07002082 /*
2083 * even if we're attaching all tasks in the thread group, we
2084 * only need to check permissions on one of them.
2085 */
David Howellsc69e8d92008-11-14 10:39:19 +11002086 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002087 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2088 !uid_eq(cred->euid, tcred->uid) &&
2089 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002090 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002091 ret = -EACCES;
2092 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002093 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002094 } else
2095 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002096
2097 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002098 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002099
2100 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002101 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002102 * trapped in a cpuset, or RT worker may be born in a cgroup
2103 * with no rt_runtime allocated. Just say no.
2104 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002105 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002106 ret = -EINVAL;
2107 rcu_read_unlock();
2108 goto out_unlock_cgroup;
2109 }
2110
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002111 get_task_struct(tsk);
2112 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002113
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002114 threadgroup_lock(tsk);
2115 if (threadgroup) {
2116 if (!thread_group_leader(tsk)) {
2117 /*
2118 * a race with de_thread from another thread's exec()
2119 * may strip us of our leadership, if this happens,
2120 * there is no choice but to throw this task away and
2121 * try again; this is
2122 * "double-double-toil-and-trouble-check locking".
2123 */
2124 threadgroup_unlock(tsk);
2125 put_task_struct(tsk);
2126 goto retry_find_task;
2127 }
Li Zefan081aa452013-03-13 09:17:09 +08002128 }
2129
2130 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2131
Tejun Heocd3d0952011-12-12 18:12:21 -08002132 threadgroup_unlock(tsk);
2133
Paul Menagebbcb81d2007-10-18 23:39:32 -07002134 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002135out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002136 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002137 return ret;
2138}
2139
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002140/**
2141 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2142 * @from: attach to all cgroups of a given task
2143 * @tsk: the task to be attached
2144 */
2145int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2146{
2147 struct cgroupfs_root *root;
2148 int retval = 0;
2149
Tejun Heo47cfcd02013-04-07 09:29:51 -07002150 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002151 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002152 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002153
Li Zefan6f4b7e62013-07-31 16:18:36 +08002154 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002155 if (retval)
2156 break;
2157 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002158 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002159
2160 return retval;
2161}
2162EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2163
Tejun Heo182446d2013-08-08 20:11:24 -04002164static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2165 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002166{
Tejun Heo182446d2013-08-08 20:11:24 -04002167 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002168}
2169
Tejun Heo182446d2013-08-08 20:11:24 -04002170static int cgroup_procs_write(struct cgroup_subsys_state *css,
2171 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002172{
Tejun Heo182446d2013-08-08 20:11:24 -04002173 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002174}
2175
Tejun Heo182446d2013-08-08 20:11:24 -04002176static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2177 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002178{
Tejun Heo182446d2013-08-08 20:11:24 -04002179 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002180 if (strlen(buffer) >= PATH_MAX)
2181 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002182 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002183 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002184 spin_lock(&release_agent_path_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04002185 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heo69e943b2014-02-08 10:36:58 -05002186 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002187 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002188 return 0;
2189}
2190
Tejun Heo2da8ca82013-12-05 12:28:04 -05002191static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002192{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002193 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002194
Paul Menagee788e062008-07-25 01:46:59 -07002195 if (!cgroup_lock_live_group(cgrp))
2196 return -ENODEV;
2197 seq_puts(seq, cgrp->root->release_agent_path);
2198 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002199 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002200 return 0;
2201}
2202
Tejun Heo2da8ca82013-12-05 12:28:04 -05002203static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002204{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002205 struct cgroup *cgrp = seq_css(seq)->cgroup;
2206
2207 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002208 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002209}
2210
Paul Menage84eea842008-07-25 01:47:00 -07002211/* A buffer size big enough for numbers or short strings */
2212#define CGROUP_LOCAL_BUFFER_SIZE 64
2213
Tejun Heoa742c592013-12-05 12:28:03 -05002214static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
Tejun Heo182446d2013-08-08 20:11:24 -04002215 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002216{
Tejun Heo182446d2013-08-08 20:11:24 -04002217 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002218 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002219 struct cgroup_subsys_state *css = cfe->css;
Tejun Heoa742c592013-12-05 12:28:03 -05002220 size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1;
2221 char *buf;
2222 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002223
Tejun Heoa742c592013-12-05 12:28:03 -05002224 if (nbytes >= max_bytes)
2225 return -E2BIG;
2226
2227 buf = kmalloc(nbytes + 1, GFP_KERNEL);
2228 if (!buf)
2229 return -ENOMEM;
2230
2231 if (copy_from_user(buf, userbuf, nbytes)) {
2232 ret = -EFAULT;
2233 goto out_free;
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002234 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002235
Tejun Heoa742c592013-12-05 12:28:03 -05002236 buf[nbytes] = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07002237
Tejun Heoa742c592013-12-05 12:28:03 -05002238 if (cft->write_string) {
2239 ret = cft->write_string(css, cft, strstrip(buf));
2240 } else if (cft->write_u64) {
2241 unsigned long long v;
2242 ret = kstrtoull(buf, 0, &v);
2243 if (!ret)
2244 ret = cft->write_u64(css, cft, v);
2245 } else if (cft->write_s64) {
2246 long long v;
2247 ret = kstrtoll(buf, 0, &v);
2248 if (!ret)
2249 ret = cft->write_s64(css, cft, v);
2250 } else if (cft->trigger) {
2251 ret = cft->trigger(css, (unsigned int)cft->private);
2252 } else {
2253 ret = -EINVAL;
2254 }
2255out_free:
2256 kfree(buf);
2257 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002258}
2259
Paul Menage91796562008-04-29 01:00:01 -07002260/*
2261 * seqfile ops/methods for returning structured data. Currently just
2262 * supports string->u64 maps, but can be extended in future.
2263 */
2264
Tejun Heo6612f052013-12-05 12:28:04 -05002265static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002266{
Tejun Heo6612f052013-12-05 12:28:04 -05002267 struct cftype *cft = seq_cft(seq);
2268
2269 if (cft->seq_start) {
2270 return cft->seq_start(seq, ppos);
2271 } else {
2272 /*
2273 * The same behavior and code as single_open(). Returns
2274 * !NULL if pos is at the beginning; otherwise, NULL.
2275 */
2276 return NULL + !*ppos;
2277 }
2278}
2279
2280static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2281{
2282 struct cftype *cft = seq_cft(seq);
2283
2284 if (cft->seq_next) {
2285 return cft->seq_next(seq, v, ppos);
2286 } else {
2287 /*
2288 * The same behavior and code as single_open(), always
2289 * terminate after the initial read.
2290 */
2291 ++*ppos;
2292 return NULL;
2293 }
2294}
2295
2296static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2297{
2298 struct cftype *cft = seq_cft(seq);
2299
2300 if (cft->seq_stop)
2301 cft->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002302}
2303
2304static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2305{
Tejun Heo7da11272013-12-05 12:28:04 -05002306 struct cftype *cft = seq_cft(m);
2307 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002308
Tejun Heo2da8ca82013-12-05 12:28:04 -05002309 if (cft->seq_show)
2310 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002311
Tejun Heo896f5192013-12-05 12:28:04 -05002312 if (cft->read_u64)
2313 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2314 else if (cft->read_s64)
2315 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2316 else
2317 return -EINVAL;
2318 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002319}
2320
Tejun Heo6612f052013-12-05 12:28:04 -05002321static struct seq_operations cgroup_seq_operations = {
2322 .start = cgroup_seqfile_start,
2323 .next = cgroup_seqfile_next,
2324 .stop = cgroup_seqfile_stop,
2325 .show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002326};
2327
Paul Menageddbcc7e2007-10-18 23:39:30 -07002328static int cgroup_file_open(struct inode *inode, struct file *file)
2329{
Tejun Heof7d58812013-08-08 20:11:23 -04002330 struct cfent *cfe = __d_cfe(file->f_dentry);
2331 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002332 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2333 struct cgroup_subsys_state *css;
Tejun Heo6612f052013-12-05 12:28:04 -05002334 struct cgroup_open_file *of;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002335 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002336
2337 err = generic_file_open(inode, file);
2338 if (err)
2339 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002340
2341 /*
2342 * If the file belongs to a subsystem, pin the css. Will be
2343 * unpinned either on open failure or release. This ensures that
2344 * @css stays alive for all file operations.
2345 */
Tejun Heo105347b2013-08-13 11:01:55 -04002346 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002347 css = cgroup_css(cgrp, cft->ss);
2348 if (cft->ss && !css_tryget(css))
2349 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002350 rcu_read_unlock();
2351
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002352 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002353 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002354
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002355 /*
2356 * @cfe->css is used by read/write/close to determine the
2357 * associated css. @file->private_data would be a better place but
2358 * that's already used by seqfile. Multiple accessors may use it
2359 * simultaneously which is okay as the association never changes.
2360 */
2361 WARN_ON_ONCE(cfe->css && cfe->css != css);
2362 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002363
Tejun Heo6612f052013-12-05 12:28:04 -05002364 of = __seq_open_private(file, &cgroup_seq_operations,
2365 sizeof(struct cgroup_open_file));
2366 if (of) {
2367 of->cfe = cfe;
2368 return 0;
Li Zefane0798ce2013-07-31 17:36:25 +08002369 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002370
Tejun Heo6612f052013-12-05 12:28:04 -05002371 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002372 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002373 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002374}
2375
2376static int cgroup_file_release(struct inode *inode, struct file *file)
2377{
Tejun Heof7d58812013-08-08 20:11:23 -04002378 struct cfent *cfe = __d_cfe(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002379 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002380
Tejun Heo67f4c362013-08-08 20:11:24 -04002381 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002382 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002383 return seq_release_private(inode, file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002384}
2385
2386/*
2387 * cgroup_rename - Only allow simple rename of directories in place.
2388 */
2389static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2390 struct inode *new_dir, struct dentry *new_dentry)
2391{
Li Zefan65dff752013-03-01 15:01:56 +08002392 int ret;
2393 struct cgroup_name *name, *old_name;
2394 struct cgroup *cgrp;
2395
2396 /*
2397 * It's convinient to use parent dir's i_mutex to protected
2398 * cgrp->name.
2399 */
2400 lockdep_assert_held(&old_dir->i_mutex);
2401
Paul Menageddbcc7e2007-10-18 23:39:30 -07002402 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2403 return -ENOTDIR;
2404 if (new_dentry->d_inode)
2405 return -EEXIST;
2406 if (old_dir != new_dir)
2407 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002408
2409 cgrp = __d_cgrp(old_dentry);
2410
Tejun Heo6db8e852013-06-14 11:18:22 -07002411 /*
2412 * This isn't a proper migration and its usefulness is very
2413 * limited. Disallow if sane_behavior.
2414 */
2415 if (cgroup_sane_behavior(cgrp))
2416 return -EPERM;
2417
Li Zefan65dff752013-03-01 15:01:56 +08002418 name = cgroup_alloc_name(new_dentry);
2419 if (!name)
2420 return -ENOMEM;
2421
2422 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2423 if (ret) {
2424 kfree(name);
2425 return ret;
2426 }
2427
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002428 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002429 rcu_assign_pointer(cgrp->name, name);
2430
2431 kfree_rcu(old_name, rcu_head);
2432 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002433}
2434
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002435static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2436{
2437 if (S_ISDIR(dentry->d_inode->i_mode))
2438 return &__d_cgrp(dentry)->xattrs;
2439 else
Li Zefan712317a2013-04-18 23:09:52 -07002440 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002441}
2442
2443static inline int xattr_enabled(struct dentry *dentry)
2444{
2445 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002446 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002447}
2448
2449static bool is_valid_xattr(const char *name)
2450{
2451 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2452 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2453 return true;
2454 return false;
2455}
2456
2457static int cgroup_setxattr(struct dentry *dentry, const char *name,
2458 const void *val, size_t size, int flags)
2459{
2460 if (!xattr_enabled(dentry))
2461 return -EOPNOTSUPP;
2462 if (!is_valid_xattr(name))
2463 return -EINVAL;
2464 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2465}
2466
2467static int cgroup_removexattr(struct dentry *dentry, const char *name)
2468{
2469 if (!xattr_enabled(dentry))
2470 return -EOPNOTSUPP;
2471 if (!is_valid_xattr(name))
2472 return -EINVAL;
2473 return simple_xattr_remove(__d_xattrs(dentry), name);
2474}
2475
2476static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2477 void *buf, size_t size)
2478{
2479 if (!xattr_enabled(dentry))
2480 return -EOPNOTSUPP;
2481 if (!is_valid_xattr(name))
2482 return -EINVAL;
2483 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2484}
2485
2486static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2487{
2488 if (!xattr_enabled(dentry))
2489 return -EOPNOTSUPP;
2490 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2491}
2492
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002493static const struct file_operations cgroup_file_operations = {
Tejun Heo896f5192013-12-05 12:28:04 -05002494 .read = seq_read,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002495 .write = cgroup_file_write,
2496 .llseek = generic_file_llseek,
2497 .open = cgroup_file_open,
2498 .release = cgroup_file_release,
2499};
2500
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002501static const struct inode_operations cgroup_file_inode_operations = {
2502 .setxattr = cgroup_setxattr,
2503 .getxattr = cgroup_getxattr,
2504 .listxattr = cgroup_listxattr,
2505 .removexattr = cgroup_removexattr,
2506};
2507
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002508static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002509 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002510 .mkdir = cgroup_mkdir,
2511 .rmdir = cgroup_rmdir,
2512 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002513 .setxattr = cgroup_setxattr,
2514 .getxattr = cgroup_getxattr,
2515 .listxattr = cgroup_listxattr,
2516 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002517};
2518
Al Viroa5e7ed32011-07-26 01:55:55 -04002519static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002520 struct super_block *sb)
2521{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002522 struct inode *inode;
2523
2524 if (!dentry)
2525 return -ENOENT;
2526 if (dentry->d_inode)
2527 return -EEXIST;
2528
2529 inode = cgroup_new_inode(mode, sb);
2530 if (!inode)
2531 return -ENOMEM;
2532
2533 if (S_ISDIR(mode)) {
2534 inode->i_op = &cgroup_dir_inode_operations;
2535 inode->i_fop = &simple_dir_operations;
2536
2537 /* start off with i_nlink == 2 (for "." entry) */
2538 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002539 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002540
Tejun Heob8a2df62012-11-19 08:13:37 -08002541 /*
2542 * Control reaches here with cgroup_mutex held.
2543 * @inode->i_mutex should nest outside cgroup_mutex but we
2544 * want to populate it immediately without releasing
2545 * cgroup_mutex. As @inode isn't visible to anyone else
2546 * yet, trylock will always succeed without affecting
2547 * lockdep checks.
2548 */
2549 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002550 } else if (S_ISREG(mode)) {
2551 inode->i_size = 0;
2552 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002553 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002554 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002555 d_instantiate(dentry, inode);
2556 dget(dentry); /* Extra count - pin the dentry in core */
2557 return 0;
2558}
2559
Li Zefan099fca32009-04-02 16:57:29 -07002560/**
2561 * cgroup_file_mode - deduce file mode of a control file
2562 * @cft: the control file in question
2563 *
2564 * returns cft->mode if ->mode is not 0
2565 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2566 * returns S_IRUGO if it has only a read handler
2567 * returns S_IWUSR if it has only a write hander
2568 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002569static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002570{
Al Viroa5e7ed32011-07-26 01:55:55 -04002571 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002572
2573 if (cft->mode)
2574 return cft->mode;
2575
Tejun Heo2da8ca82013-12-05 12:28:04 -05002576 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
Li Zefan099fca32009-04-02 16:57:29 -07002577 mode |= S_IRUGO;
2578
Tejun Heo6e0755b2013-12-05 12:28:03 -05002579 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
2580 cft->trigger)
Li Zefan099fca32009-04-02 16:57:29 -07002581 mode |= S_IWUSR;
2582
2583 return mode;
2584}
2585
Tejun Heo2bb566c2013-08-08 20:11:23 -04002586static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002587{
Paul Menagebd89aab2007-10-18 23:40:44 -07002588 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002589 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002590 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002591 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002592 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002593 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002594 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002595
Tejun Heo9fa4db32013-08-26 18:40:56 -04002596 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2597 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002598 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002599 strcat(name, ".");
2600 }
2601 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002602
Paul Menageddbcc7e2007-10-18 23:39:30 -07002603 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002604
2605 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2606 if (!cfe)
2607 return -ENOMEM;
2608
Paul Menageddbcc7e2007-10-18 23:39:30 -07002609 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002610 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002611 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002612 goto out;
2613 }
2614
Li Zefand6cbf352013-05-14 19:44:20 +08002615 cfe->type = (void *)cft;
2616 cfe->dentry = dentry;
2617 dentry->d_fsdata = cfe;
2618 simple_xattrs_init(&cfe->xattrs);
2619
Tejun Heo05ef1d72012-04-01 12:09:56 -07002620 mode = cgroup_file_mode(cft);
2621 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2622 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002623 list_add_tail(&cfe->node, &parent->files);
2624 cfe = NULL;
2625 }
2626 dput(dentry);
2627out:
2628 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002629 return error;
2630}
2631
Tejun Heob1f28d32013-06-28 16:24:10 -07002632/**
2633 * cgroup_addrm_files - add or remove files to a cgroup directory
2634 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002635 * @cfts: array of cftypes to be added
2636 * @is_add: whether to add or remove
2637 *
2638 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002639 * For removals, this function never fails. If addition fails, this
2640 * function doesn't remove files already added. The caller is responsible
2641 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002642 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002643static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2644 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002645{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002646 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002647 int ret;
2648
2649 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05002650 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002651
2652 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002653 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002654 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2655 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002656 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2657 continue;
2658 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2659 continue;
2660
Li Zefan2739d3c2013-01-21 18:18:33 +08002661 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002662 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002663 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002664 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002665 cft->name, ret);
2666 return ret;
2667 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002668 } else {
2669 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002670 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002671 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002672 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002673}
2674
Tejun Heo8e3f6542012-04-01 12:09:55 -07002675static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002676 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002677{
2678 /*
2679 * Thanks to the entanglement with vfs inode locking, we can't walk
2680 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002681 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2682 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002683 */
Tejun Heoace2bee2014-02-11 11:52:47 -05002684 mutex_lock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002685 mutex_lock(&cgroup_mutex);
2686}
2687
Tejun Heo2bb566c2013-08-08 20:11:23 -04002688static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002689 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002690{
2691 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002692 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002693 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002694 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002695 struct dentry *prev = NULL;
2696 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002697 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002698 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002699 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002700
Tejun Heo4ac06012014-02-11 11:52:47 -05002701 mutex_unlock(&cgroup_mutex);
2702
Tejun Heo8e3f6542012-04-01 12:09:55 -07002703 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002704 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002705 !atomic_inc_not_zero(&sb->s_active)) {
Tejun Heoace2bee2014-02-11 11:52:47 -05002706 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002707 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002708 }
2709
Li Zefane8c82d22013-06-18 18:48:37 +08002710 /*
2711 * All cgroups which are created after we drop cgroup_mutex will
2712 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002713 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002714 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002715 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002716
Li Zefane8c82d22013-06-18 18:48:37 +08002717 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002718 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002719 struct cgroup *cgrp = css->cgroup;
2720
Li Zefane8c82d22013-06-18 18:48:37 +08002721 if (cgroup_is_dead(cgrp))
2722 continue;
2723
2724 inode = cgrp->dentry->d_inode;
2725 dget(cgrp->dentry);
Li Zefane8c82d22013-06-18 18:48:37 +08002726 dput(prev);
2727 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002728
Tejun Heoace2bee2014-02-11 11:52:47 -05002729 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002730 mutex_lock(&inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05002731 mutex_lock(&cgroup_tree_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002732 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002733 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002734 mutex_unlock(&inode->i_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002735 if (ret)
2736 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002737 }
Tejun Heoace2bee2014-02-11 11:52:47 -05002738 mutex_unlock(&cgroup_tree_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08002739 dput(prev);
2740 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002741 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002742}
2743
2744/**
2745 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2746 * @ss: target cgroup subsystem
2747 * @cfts: zero-length name terminated array of cftypes
2748 *
2749 * Register @cfts to @ss. Files described by @cfts are created for all
2750 * existing cgroups to which @ss is attached and all future cgroups will
2751 * have them too. This function can be called anytime whether @ss is
2752 * attached or not.
2753 *
2754 * Returns 0 on successful registration, -errno on failure. Note that this
2755 * function currently returns 0 as long as @cfts registration is successful
2756 * even if some file creation attempts on existing cgroups fail.
2757 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002758int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002759{
2760 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002761 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002762 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002763
2764 set = kzalloc(sizeof(*set), GFP_KERNEL);
2765 if (!set)
2766 return -ENOMEM;
2767
Tejun Heo2bb566c2013-08-08 20:11:23 -04002768 for (cft = cfts; cft->name[0] != '\0'; cft++)
2769 cft->ss = ss;
2770
Tejun Heo8e3f6542012-04-01 12:09:55 -07002771 cgroup_cfts_prepare();
2772 set->cfts = cfts;
2773 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002774 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002775 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002776 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002777 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002778}
2779EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2780
Li Zefana043e3b2008-02-23 15:24:09 -08002781/**
Tejun Heo79578622012-04-01 12:09:56 -07002782 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002783 * @cfts: zero-length name terminated array of cftypes
2784 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002785 * Unregister @cfts. Files described by @cfts are removed from all
2786 * existing cgroups and all future cgroups won't have them either. This
2787 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002788 *
2789 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002790 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002791 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002792int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002793{
2794 struct cftype_set *set;
2795
Tejun Heo2bb566c2013-08-08 20:11:23 -04002796 if (!cfts || !cfts[0].ss)
2797 return -ENOENT;
2798
Tejun Heo79578622012-04-01 12:09:56 -07002799 cgroup_cfts_prepare();
2800
Tejun Heo2bb566c2013-08-08 20:11:23 -04002801 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002802 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002803 list_del(&set->node);
2804 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002805 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002806 return 0;
2807 }
2808 }
2809
Tejun Heo2bb566c2013-08-08 20:11:23 -04002810 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002811 return -ENOENT;
2812}
2813
2814/**
Li Zefana043e3b2008-02-23 15:24:09 -08002815 * cgroup_task_count - count the number of tasks in a cgroup.
2816 * @cgrp: the cgroup in question
2817 *
2818 * Return the number of tasks in the cgroup.
2819 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002820int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002821{
2822 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002823 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002824
Paul Menage817929e2007-10-18 23:39:36 -07002825 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002826 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2827 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002828 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002829 return count;
2830}
2831
2832/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002833 * To reduce the fork() overhead for systems that are not actually using
2834 * their cgroups capability, we don't maintain the lists running through
2835 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002836 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002837 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002838static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002839{
2840 struct task_struct *p, *g;
2841 write_lock(&css_set_lock);
2842 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002843 /*
2844 * We need tasklist_lock because RCU is not safe against
2845 * while_each_thread(). Besides, a forking task that has passed
2846 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2847 * is not guaranteed to have its child immediately visible in the
2848 * tasklist if we walk through it with RCU.
2849 */
2850 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002851 do_each_thread(g, p) {
2852 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002853 /*
2854 * We should check if the process is exiting, otherwise
2855 * it will race with cgroup_exit() in that the list
2856 * entry won't be deleted though the process has exited.
2857 */
2858 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002859 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002860 task_unlock(p);
2861 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002862 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002863 write_unlock(&css_set_lock);
2864}
2865
Tejun Heo574bd9f2012-11-09 09:12:29 -08002866/**
Tejun Heo492eb212013-08-08 20:11:25 -04002867 * css_next_child - find the next child of a given css
2868 * @pos_css: the current position (%NULL to initiate traversal)
2869 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002870 *
Tejun Heo492eb212013-08-08 20:11:25 -04002871 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002872 * under either cgroup_mutex or RCU read lock. The only requirement is
2873 * that @parent_css and @pos_css are accessible. The next sibling is
2874 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002875 */
Tejun Heo492eb212013-08-08 20:11:25 -04002876struct cgroup_subsys_state *
2877css_next_child(struct cgroup_subsys_state *pos_css,
2878 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002879{
Tejun Heo492eb212013-08-08 20:11:25 -04002880 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2881 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002882 struct cgroup *next;
2883
Tejun Heoace2bee2014-02-11 11:52:47 -05002884 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002885
2886 /*
2887 * @pos could already have been removed. Once a cgroup is removed,
2888 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002889 * changes. As CGRP_DEAD assertion is serialized and happens
2890 * before the cgroup is taken off the ->sibling list, if we see it
2891 * unasserted, it's guaranteed that the next sibling hasn't
2892 * finished its grace period even if it's already removed, and thus
2893 * safe to dereference from this RCU critical section. If
2894 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2895 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002896 *
2897 * If @pos is dead, its next pointer can't be dereferenced;
2898 * however, as each cgroup is given a monotonically increasing
2899 * unique serial number and always appended to the sibling list,
2900 * the next one can be found by walking the parent's children until
2901 * we see a cgroup with higher serial number than @pos's. While
2902 * this path can be slower, it's taken only when either the current
2903 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002904 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002905 if (!pos) {
2906 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2907 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002908 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002909 } else {
2910 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2911 if (next->serial_nr > pos->serial_nr)
2912 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002913 }
2914
Tejun Heo492eb212013-08-08 20:11:25 -04002915 if (&next->sibling == &cgrp->children)
2916 return NULL;
2917
Tejun Heoca8bdca2013-08-26 18:40:56 -04002918 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002919}
Tejun Heo492eb212013-08-08 20:11:25 -04002920EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002921
2922/**
Tejun Heo492eb212013-08-08 20:11:25 -04002923 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002924 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002925 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002926 *
Tejun Heo492eb212013-08-08 20:11:25 -04002927 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002928 * to visit for pre-order traversal of @root's descendants. @root is
2929 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002930 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002931 * While this function requires cgroup_mutex or RCU read locking, it
2932 * doesn't require the whole traversal to be contained in a single critical
2933 * section. This function will return the correct next descendant as long
2934 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002935 */
Tejun Heo492eb212013-08-08 20:11:25 -04002936struct cgroup_subsys_state *
2937css_next_descendant_pre(struct cgroup_subsys_state *pos,
2938 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002939{
Tejun Heo492eb212013-08-08 20:11:25 -04002940 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002941
Tejun Heoace2bee2014-02-11 11:52:47 -05002942 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002943
Tejun Heobd8815a2013-08-08 20:11:27 -04002944 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002945 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002946 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002947
2948 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002949 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002950 if (next)
2951 return next;
2952
2953 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002954 while (pos != root) {
2955 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002956 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002957 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002958 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002959 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002960
2961 return NULL;
2962}
Tejun Heo492eb212013-08-08 20:11:25 -04002963EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002964
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002965/**
Tejun Heo492eb212013-08-08 20:11:25 -04002966 * css_rightmost_descendant - return the rightmost descendant of a css
2967 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002968 *
Tejun Heo492eb212013-08-08 20:11:25 -04002969 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2970 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002971 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002972 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002973 * While this function requires cgroup_mutex or RCU read locking, it
2974 * doesn't require the whole traversal to be contained in a single critical
2975 * section. This function will return the correct rightmost descendant as
2976 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002977 */
Tejun Heo492eb212013-08-08 20:11:25 -04002978struct cgroup_subsys_state *
2979css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002980{
Tejun Heo492eb212013-08-08 20:11:25 -04002981 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002982
Tejun Heoace2bee2014-02-11 11:52:47 -05002983 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002984
2985 do {
2986 last = pos;
2987 /* ->prev isn't RCU safe, walk ->next till the end */
2988 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002989 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002990 pos = tmp;
2991 } while (pos);
2992
2993 return last;
2994}
Tejun Heo492eb212013-08-08 20:11:25 -04002995EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002996
Tejun Heo492eb212013-08-08 20:11:25 -04002997static struct cgroup_subsys_state *
2998css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002999{
Tejun Heo492eb212013-08-08 20:11:25 -04003000 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003001
3002 do {
3003 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003004 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003005 } while (pos);
3006
3007 return last;
3008}
3009
3010/**
Tejun Heo492eb212013-08-08 20:11:25 -04003011 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003012 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003013 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003014 *
Tejun Heo492eb212013-08-08 20:11:25 -04003015 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003016 * to visit for post-order traversal of @root's descendants. @root is
3017 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003018 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003019 * While this function requires cgroup_mutex or RCU read locking, it
3020 * doesn't require the whole traversal to be contained in a single critical
3021 * section. This function will return the correct next descendant as long
3022 * as both @pos and @cgroup are accessible and @pos is a descendant of
3023 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003024 */
Tejun Heo492eb212013-08-08 20:11:25 -04003025struct cgroup_subsys_state *
3026css_next_descendant_post(struct cgroup_subsys_state *pos,
3027 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003028{
Tejun Heo492eb212013-08-08 20:11:25 -04003029 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003030
Tejun Heoace2bee2014-02-11 11:52:47 -05003031 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003032
Tejun Heo58b79a92013-09-06 15:31:08 -04003033 /* if first iteration, visit leftmost descendant which may be @root */
3034 if (!pos)
3035 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003036
Tejun Heobd8815a2013-08-08 20:11:27 -04003037 /* if we visited @root, we're done */
3038 if (pos == root)
3039 return NULL;
3040
Tejun Heo574bd9f2012-11-09 09:12:29 -08003041 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003042 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003043 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003044 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003045
3046 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003047 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003048}
Tejun Heo492eb212013-08-08 20:11:25 -04003049EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003050
Tejun Heo0942eee2013-08-08 20:11:26 -04003051/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003052 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003053 * @it: the iterator to advance
3054 *
3055 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003056 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003057static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003058{
3059 struct list_head *l = it->cset_link;
3060 struct cgrp_cset_link *link;
3061 struct css_set *cset;
3062
3063 /* Advance to the next non-empty css_set */
3064 do {
3065 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003066 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003067 it->cset_link = NULL;
3068 return;
3069 }
3070 link = list_entry(l, struct cgrp_cset_link, cset_link);
3071 cset = link->cset;
3072 } while (list_empty(&cset->tasks));
3073 it->cset_link = l;
3074 it->task = cset->tasks.next;
3075}
3076
Tejun Heo0942eee2013-08-08 20:11:26 -04003077/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003078 * css_task_iter_start - initiate task iteration
3079 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003080 * @it: the task iterator to use
3081 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003082 * Initiate iteration through the tasks of @css. The caller can call
3083 * css_task_iter_next() to walk through the tasks until the function
3084 * returns NULL. On completion of iteration, css_task_iter_end() must be
3085 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003086 *
3087 * Note that this function acquires a lock which is released when the
3088 * iteration finishes. The caller can't sleep while iteration is in
3089 * progress.
3090 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003091void css_task_iter_start(struct cgroup_subsys_state *css,
3092 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003093 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003094{
3095 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003096 * The first time anyone tries to iterate across a css, we need to
3097 * enable the list linking each css_set to its tasks, and fix up
3098 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003099 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003100 if (!use_task_css_set_links)
3101 cgroup_enable_task_cg_lists();
3102
Paul Menage817929e2007-10-18 23:39:36 -07003103 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003104
Tejun Heo72ec7022013-08-08 20:11:26 -04003105 it->origin_css = css;
3106 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003107
Tejun Heo72ec7022013-08-08 20:11:26 -04003108 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003109}
3110
Tejun Heo0942eee2013-08-08 20:11:26 -04003111/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003112 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003113 * @it: the task iterator being iterated
3114 *
3115 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003116 * initialized via css_task_iter_start(). Returns NULL when the iteration
3117 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003118 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003119struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003120{
3121 struct task_struct *res;
3122 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003123 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003124
3125 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003126 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003127 return NULL;
3128 res = list_entry(l, struct task_struct, cg_list);
3129 /* Advance iterator to find next entry */
3130 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003131 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3132 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003133 /*
3134 * We reached the end of this task list - move on to the
3135 * next cgrp_cset_link.
3136 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003137 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003138 } else {
3139 it->task = l;
3140 }
3141 return res;
3142}
3143
Tejun Heo0942eee2013-08-08 20:11:26 -04003144/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003145 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003146 * @it: the task iterator to finish
3147 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003148 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003149 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003150void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003151 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003152{
3153 read_unlock(&css_set_lock);
3154}
3155
Cliff Wickman31a7df02008-02-07 00:14:42 -08003156static inline int started_after_time(struct task_struct *t1,
3157 struct timespec *time,
3158 struct task_struct *t2)
3159{
3160 int start_diff = timespec_compare(&t1->start_time, time);
3161 if (start_diff > 0) {
3162 return 1;
3163 } else if (start_diff < 0) {
3164 return 0;
3165 } else {
3166 /*
3167 * Arbitrarily, if two processes started at the same
3168 * time, we'll say that the lower pointer value
3169 * started first. Note that t2 may have exited by now
3170 * so this may not be a valid pointer any longer, but
3171 * that's fine - it still serves to distinguish
3172 * between two tasks started (effectively) simultaneously.
3173 */
3174 return t1 > t2;
3175 }
3176}
3177
3178/*
3179 * This function is a callback from heap_insert() and is used to order
3180 * the heap.
3181 * In this case we order the heap in descending task start time.
3182 */
3183static inline int started_after(void *p1, void *p2)
3184{
3185 struct task_struct *t1 = p1;
3186 struct task_struct *t2 = p2;
3187 return started_after_time(t1, &t2->start_time, t2);
3188}
3189
3190/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003191 * css_scan_tasks - iterate though all the tasks in a css
3192 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003193 * @test: optional test callback
3194 * @process: process callback
3195 * @data: data passed to @test and @process
3196 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003197 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003198 * Iterate through all the tasks in @css, calling @test for each, and if it
3199 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003200 *
Tejun Heoe5358372013-08-08 20:11:26 -04003201 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003202 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003203 * lock css_set_lock for the call to @process.
3204 *
3205 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003206 * of @css for the duration of this call. This function may or may not
3207 * call @process for tasks that exit or move to a different css during the
3208 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003209 *
3210 * Note that @test may be called with locks held, and may in some
3211 * situations be called multiple times for the same task, so it should be
3212 * cheap.
3213 *
3214 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3215 * heap operations (and its "gt" member will be overwritten), else a
3216 * temporary heap will be used (allocation of which may cause this function
3217 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003218 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003219int css_scan_tasks(struct cgroup_subsys_state *css,
3220 bool (*test)(struct task_struct *, void *),
3221 void (*process)(struct task_struct *, void *),
3222 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003223{
3224 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003225 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003226 struct task_struct *p, *dropped;
3227 /* Never dereference latest_task, since it's not refcounted */
3228 struct task_struct *latest_task = NULL;
3229 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003230 struct timespec latest_time = { 0, 0 };
3231
Tejun Heoe5358372013-08-08 20:11:26 -04003232 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003233 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003234 heap->gt = &started_after;
3235 } else {
3236 /* We need to allocate our own heap memory */
3237 heap = &tmp_heap;
3238 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3239 if (retval)
3240 /* cannot allocate the heap */
3241 return retval;
3242 }
3243
3244 again:
3245 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003246 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003247 * which are of interest, and invoking @process callback on the
3248 * ones which need an update. Since we don't want to hold any
3249 * locks during the task updates, gather tasks to be processed in a
3250 * heap structure. The heap is sorted by descending task start
3251 * time. If the statically-sized heap fills up, we overflow tasks
3252 * that started later, and in future iterations only consider tasks
3253 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003254 * guarantees forward progress and that we don't miss any tasks.
3255 */
3256 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003257 css_task_iter_start(css, &it);
3258 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003259 /*
3260 * Only affect tasks that qualify per the caller's callback,
3261 * if he provided one
3262 */
Tejun Heoe5358372013-08-08 20:11:26 -04003263 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003264 continue;
3265 /*
3266 * Only process tasks that started after the last task
3267 * we processed
3268 */
3269 if (!started_after_time(p, &latest_time, latest_task))
3270 continue;
3271 dropped = heap_insert(heap, p);
3272 if (dropped == NULL) {
3273 /*
3274 * The new task was inserted; the heap wasn't
3275 * previously full
3276 */
3277 get_task_struct(p);
3278 } else if (dropped != p) {
3279 /*
3280 * The new task was inserted, and pushed out a
3281 * different task
3282 */
3283 get_task_struct(p);
3284 put_task_struct(dropped);
3285 }
3286 /*
3287 * Else the new task was newer than anything already in
3288 * the heap and wasn't inserted
3289 */
3290 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003291 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003292
3293 if (heap->size) {
3294 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003295 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003296 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003297 latest_time = q->start_time;
3298 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003299 }
3300 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003301 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003302 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003303 }
3304 /*
3305 * If we had to process any tasks at all, scan again
3306 * in case some of them were in the middle of forking
3307 * children that didn't get processed.
3308 * Not the most efficient way to do it, but it avoids
3309 * having to take callback_mutex in the fork path
3310 */
3311 goto again;
3312 }
3313 if (heap == &tmp_heap)
3314 heap_free(&tmp_heap);
3315 return 0;
3316}
3317
Tejun Heoe5358372013-08-08 20:11:26 -04003318static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003319{
Tejun Heoe5358372013-08-08 20:11:26 -04003320 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003321
Tejun Heo47cfcd02013-04-07 09:29:51 -07003322 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003323 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003324 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003325}
3326
3327/**
3328 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3329 * @to: cgroup to which the tasks will be moved
3330 * @from: cgroup in which the tasks currently reside
3331 */
3332int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3333{
Tejun Heo72ec7022013-08-08 20:11:26 -04003334 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3335 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003336}
3337
Paul Menage817929e2007-10-18 23:39:36 -07003338/*
Ben Blum102a7752009-09-23 15:56:26 -07003339 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003340 *
3341 * Reading this file can return large amounts of data if a cgroup has
3342 * *lots* of attached tasks. So it may need several calls to read(),
3343 * but we cannot guarantee that the information we produce is correct
3344 * unless we produce it entirely atomically.
3345 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003346 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003347
Li Zefan24528252012-01-20 11:58:43 +08003348/* which pidlist file are we talking about? */
3349enum cgroup_filetype {
3350 CGROUP_FILE_PROCS,
3351 CGROUP_FILE_TASKS,
3352};
3353
3354/*
3355 * A pidlist is a list of pids that virtually represents the contents of one
3356 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3357 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3358 * to the cgroup.
3359 */
3360struct cgroup_pidlist {
3361 /*
3362 * used to find which pidlist is wanted. doesn't change as long as
3363 * this particular list stays in the list.
3364 */
3365 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3366 /* array of xids */
3367 pid_t *list;
3368 /* how many elements the above list has */
3369 int length;
Li Zefan24528252012-01-20 11:58:43 +08003370 /* each of these stored in a list by its cgroup */
3371 struct list_head links;
3372 /* pointer to the cgroup we belong to, for list removal purposes */
3373 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003374 /* for delayed destruction */
3375 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003376};
3377
Paul Menagebbcb81d2007-10-18 23:39:32 -07003378/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003379 * The following two functions "fix" the issue where there are more pids
3380 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3381 * TODO: replace with a kernel-wide solution to this problem
3382 */
3383#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3384static void *pidlist_allocate(int count)
3385{
3386 if (PIDLIST_TOO_LARGE(count))
3387 return vmalloc(count * sizeof(pid_t));
3388 else
3389 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3390}
Tejun Heob1a21362013-11-29 10:42:58 -05003391
Ben Blumd1d9fd32009-09-23 15:56:28 -07003392static void pidlist_free(void *p)
3393{
3394 if (is_vmalloc_addr(p))
3395 vfree(p);
3396 else
3397 kfree(p);
3398}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003399
3400/*
Tejun Heob1a21362013-11-29 10:42:58 -05003401 * Used to destroy all pidlists lingering waiting for destroy timer. None
3402 * should be left afterwards.
3403 */
3404static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3405{
3406 struct cgroup_pidlist *l, *tmp_l;
3407
3408 mutex_lock(&cgrp->pidlist_mutex);
3409 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3410 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3411 mutex_unlock(&cgrp->pidlist_mutex);
3412
3413 flush_workqueue(cgroup_pidlist_destroy_wq);
3414 BUG_ON(!list_empty(&cgrp->pidlists));
3415}
3416
3417static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3418{
3419 struct delayed_work *dwork = to_delayed_work(work);
3420 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3421 destroy_dwork);
3422 struct cgroup_pidlist *tofree = NULL;
3423
3424 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003425
3426 /*
Tejun Heo04502362013-11-29 10:42:59 -05003427 * Destroy iff we didn't get queued again. The state won't change
3428 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003429 */
Tejun Heo04502362013-11-29 10:42:59 -05003430 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003431 list_del(&l->links);
3432 pidlist_free(l->list);
3433 put_pid_ns(l->key.ns);
3434 tofree = l;
3435 }
3436
Tejun Heob1a21362013-11-29 10:42:58 -05003437 mutex_unlock(&l->owner->pidlist_mutex);
3438 kfree(tofree);
3439}
3440
3441/*
Ben Blum102a7752009-09-23 15:56:26 -07003442 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003443 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003444 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003445static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003446{
Ben Blum102a7752009-09-23 15:56:26 -07003447 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003448
3449 /*
3450 * we presume the 0th element is unique, so i starts at 1. trivial
3451 * edge cases first; no work needs to be done for either
3452 */
3453 if (length == 0 || length == 1)
3454 return length;
3455 /* src and dest walk down the list; dest counts unique elements */
3456 for (src = 1; src < length; src++) {
3457 /* find next unique element */
3458 while (list[src] == list[src-1]) {
3459 src++;
3460 if (src == length)
3461 goto after;
3462 }
3463 /* dest always points to where the next unique element goes */
3464 list[dest] = list[src];
3465 dest++;
3466 }
3467after:
Ben Blum102a7752009-09-23 15:56:26 -07003468 return dest;
3469}
3470
Tejun Heoafb2bc12013-11-29 10:42:59 -05003471/*
3472 * The two pid files - task and cgroup.procs - guaranteed that the result
3473 * is sorted, which forced this whole pidlist fiasco. As pid order is
3474 * different per namespace, each namespace needs differently sorted list,
3475 * making it impossible to use, for example, single rbtree of member tasks
3476 * sorted by task pointer. As pidlists can be fairly large, allocating one
3477 * per open file is dangerous, so cgroup had to implement shared pool of
3478 * pidlists keyed by cgroup and namespace.
3479 *
3480 * All this extra complexity was caused by the original implementation
3481 * committing to an entirely unnecessary property. In the long term, we
3482 * want to do away with it. Explicitly scramble sort order if
3483 * sane_behavior so that no such expectation exists in the new interface.
3484 *
3485 * Scrambling is done by swapping every two consecutive bits, which is
3486 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3487 */
3488static pid_t pid_fry(pid_t pid)
3489{
3490 unsigned a = pid & 0x55555555;
3491 unsigned b = pid & 0xAAAAAAAA;
3492
3493 return (a << 1) | (b >> 1);
3494}
3495
3496static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3497{
3498 if (cgroup_sane_behavior(cgrp))
3499 return pid_fry(pid);
3500 else
3501 return pid;
3502}
3503
Ben Blum102a7752009-09-23 15:56:26 -07003504static int cmppid(const void *a, const void *b)
3505{
3506 return *(pid_t *)a - *(pid_t *)b;
3507}
3508
Tejun Heoafb2bc12013-11-29 10:42:59 -05003509static int fried_cmppid(const void *a, const void *b)
3510{
3511 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3512}
3513
Ben Blum72a8cb32009-09-23 15:56:27 -07003514static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3515 enum cgroup_filetype type)
3516{
3517 struct cgroup_pidlist *l;
3518 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003519 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003520
Tejun Heoe6b81712013-11-29 10:42:59 -05003521 lockdep_assert_held(&cgrp->pidlist_mutex);
3522
3523 list_for_each_entry(l, &cgrp->pidlists, links)
3524 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003525 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003526 return NULL;
3527}
3528
Ben Blum72a8cb32009-09-23 15:56:27 -07003529/*
3530 * find the appropriate pidlist for our purpose (given procs vs tasks)
3531 * returns with the lock on that pidlist already held, and takes care
3532 * of the use count, or returns NULL with no locks held if we're out of
3533 * memory.
3534 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003535static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3536 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003537{
3538 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003539
Tejun Heoe6b81712013-11-29 10:42:59 -05003540 lockdep_assert_held(&cgrp->pidlist_mutex);
3541
3542 l = cgroup_pidlist_find(cgrp, type);
3543 if (l)
3544 return l;
3545
Ben Blum72a8cb32009-09-23 15:56:27 -07003546 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003547 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003548 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003549 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003550
Tejun Heob1a21362013-11-29 10:42:58 -05003551 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003552 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003553 /* don't need task_nsproxy() if we're looking at ourself */
3554 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003555 l->owner = cgrp;
3556 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003557 return l;
3558}
3559
3560/*
Ben Blum102a7752009-09-23 15:56:26 -07003561 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3562 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003563static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3564 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003565{
3566 pid_t *array;
3567 int length;
3568 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003569 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003570 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003571 struct cgroup_pidlist *l;
3572
Tejun Heo4bac00d2013-11-29 10:42:59 -05003573 lockdep_assert_held(&cgrp->pidlist_mutex);
3574
Ben Blum102a7752009-09-23 15:56:26 -07003575 /*
3576 * If cgroup gets more users after we read count, we won't have
3577 * enough space - tough. This race is indistinguishable to the
3578 * caller from the case that the additional cgroup users didn't
3579 * show up until sometime later on.
3580 */
3581 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003582 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003583 if (!array)
3584 return -ENOMEM;
3585 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003586 css_task_iter_start(&cgrp->dummy_css, &it);
3587 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003588 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003589 break;
Ben Blum102a7752009-09-23 15:56:26 -07003590 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003591 if (type == CGROUP_FILE_PROCS)
3592 pid = task_tgid_vnr(tsk);
3593 else
3594 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003595 if (pid > 0) /* make sure to only use valid results */
3596 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003597 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003598 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003599 length = n;
3600 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003601 if (cgroup_sane_behavior(cgrp))
3602 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3603 else
3604 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003605 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003606 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003607
Tejun Heoe6b81712013-11-29 10:42:59 -05003608 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003609 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003610 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003611 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003612 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003613 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003614
3615 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003616 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003617 l->list = array;
3618 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003619 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003620 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003621}
3622
Balbir Singh846c7bb2007-10-18 23:39:44 -07003623/**
Li Zefana043e3b2008-02-23 15:24:09 -08003624 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003625 * @stats: cgroupstats to fill information into
3626 * @dentry: A dentry entry belonging to the cgroup for which stats have
3627 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003628 *
3629 * Build and fill cgroupstats so that taskstats can export it to user
3630 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003631 */
3632int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3633{
3634 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003635 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003636 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003637 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003638
Balbir Singh846c7bb2007-10-18 23:39:44 -07003639 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003640 * Validate dentry by checking the superblock operations,
3641 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003642 */
Li Zefan33d283b2008-11-19 15:36:48 -08003643 if (dentry->d_sb->s_op != &cgroup_ops ||
3644 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003645 goto err;
3646
3647 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003648 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003649
Tejun Heo72ec7022013-08-08 20:11:26 -04003650 css_task_iter_start(&cgrp->dummy_css, &it);
3651 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003652 switch (tsk->state) {
3653 case TASK_RUNNING:
3654 stats->nr_running++;
3655 break;
3656 case TASK_INTERRUPTIBLE:
3657 stats->nr_sleeping++;
3658 break;
3659 case TASK_UNINTERRUPTIBLE:
3660 stats->nr_uninterruptible++;
3661 break;
3662 case TASK_STOPPED:
3663 stats->nr_stopped++;
3664 break;
3665 default:
3666 if (delayacct_is_task_waiting_on_io(tsk))
3667 stats->nr_io_wait++;
3668 break;
3669 }
3670 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003671 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003672
Balbir Singh846c7bb2007-10-18 23:39:44 -07003673err:
3674 return ret;
3675}
3676
Paul Menage8f3ff202009-09-23 15:56:25 -07003677
Paul Menagecc31edc2008-10-18 20:28:04 -07003678/*
Ben Blum102a7752009-09-23 15:56:26 -07003679 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003680 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003681 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003682 */
3683
Ben Blum102a7752009-09-23 15:56:26 -07003684static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003685{
3686 /*
3687 * Initially we receive a position value that corresponds to
3688 * one more than the last pid shown (or 0 on the first call or
3689 * after a seek to the start). Use a binary-search to find the
3690 * next pid to display, if any
3691 */
Tejun Heo5d224442013-12-05 12:28:04 -05003692 struct cgroup_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003693 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003694 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003695 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003696 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003697 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003698
Tejun Heo4bac00d2013-11-29 10:42:59 -05003699 mutex_lock(&cgrp->pidlist_mutex);
3700
3701 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003702 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003703 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003704 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003705 * could already have been destroyed.
3706 */
Tejun Heo5d224442013-12-05 12:28:04 -05003707 if (of->priv)
3708 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003709
3710 /*
3711 * Either this is the first start() after open or the matching
3712 * pidlist has been destroyed inbetween. Create a new one.
3713 */
Tejun Heo5d224442013-12-05 12:28:04 -05003714 if (!of->priv) {
3715 ret = pidlist_array_load(cgrp, type,
3716 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003717 if (ret)
3718 return ERR_PTR(ret);
3719 }
Tejun Heo5d224442013-12-05 12:28:04 -05003720 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003721
Paul Menagecc31edc2008-10-18 20:28:04 -07003722 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003723 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003724
Paul Menagecc31edc2008-10-18 20:28:04 -07003725 while (index < end) {
3726 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003727 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003728 index = mid;
3729 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003730 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003731 index = mid + 1;
3732 else
3733 end = mid;
3734 }
3735 }
3736 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003737 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003738 return NULL;
3739 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003740 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003741 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003742 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003743}
3744
Ben Blum102a7752009-09-23 15:56:26 -07003745static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003746{
Tejun Heo5d224442013-12-05 12:28:04 -05003747 struct cgroup_open_file *of = s->private;
3748 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003749
Tejun Heo5d224442013-12-05 12:28:04 -05003750 if (l)
3751 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003752 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003753 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003754}
3755
Ben Blum102a7752009-09-23 15:56:26 -07003756static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003757{
Tejun Heo5d224442013-12-05 12:28:04 -05003758 struct cgroup_open_file *of = s->private;
3759 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003760 pid_t *p = v;
3761 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003762 /*
3763 * Advance to the next pid in the array. If this goes off the
3764 * end, we're done
3765 */
3766 p++;
3767 if (p >= end) {
3768 return NULL;
3769 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003770 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003771 return p;
3772 }
3773}
3774
Ben Blum102a7752009-09-23 15:56:26 -07003775static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003776{
3777 return seq_printf(s, "%d\n", *(int *)v);
3778}
3779
Ben Blum102a7752009-09-23 15:56:26 -07003780/*
3781 * seq_operations functions for iterating on pidlists through seq_file -
3782 * independent of whether it's tasks or procs
3783 */
3784static const struct seq_operations cgroup_pidlist_seq_operations = {
3785 .start = cgroup_pidlist_start,
3786 .stop = cgroup_pidlist_stop,
3787 .next = cgroup_pidlist_next,
3788 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003789};
3790
Tejun Heo182446d2013-08-08 20:11:24 -04003791static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3792 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003793{
Tejun Heo182446d2013-08-08 20:11:24 -04003794 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003795}
3796
Tejun Heo182446d2013-08-08 20:11:24 -04003797static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3798 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003799{
Tejun Heo182446d2013-08-08 20:11:24 -04003800 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003801 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003802 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003803 else
Tejun Heo182446d2013-08-08 20:11:24 -04003804 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003805 return 0;
3806}
3807
Paul Menagebbcb81d2007-10-18 23:39:32 -07003808/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003809 * When dput() is called asynchronously, if umount has been done and
3810 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3811 * there's a small window that vfs will see the root dentry with non-zero
3812 * refcnt and trigger BUG().
3813 *
3814 * That's why we hold a reference before dput() and drop it right after.
3815 */
3816static void cgroup_dput(struct cgroup *cgrp)
3817{
3818 struct super_block *sb = cgrp->root->sb;
3819
3820 atomic_inc(&sb->s_active);
3821 dput(cgrp->dentry);
3822 deactivate_super(sb);
3823}
3824
Tejun Heo182446d2013-08-08 20:11:24 -04003825static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3826 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003827{
Tejun Heo182446d2013-08-08 20:11:24 -04003828 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003829}
3830
Tejun Heo182446d2013-08-08 20:11:24 -04003831static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3832 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003833{
3834 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003835 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003836 else
Tejun Heo182446d2013-08-08 20:11:24 -04003837 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003838 return 0;
3839}
3840
Tejun Heod5c56ce2013-06-03 19:14:34 -07003841static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003842 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003843 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003844 .seq_start = cgroup_pidlist_start,
3845 .seq_next = cgroup_pidlist_next,
3846 .seq_stop = cgroup_pidlist_stop,
3847 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003848 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003849 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003850 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003851 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003852 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003853 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003854 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003855 .read_u64 = cgroup_clone_children_read,
3856 .write_u64 = cgroup_clone_children_write,
3857 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003858 {
Tejun Heo873fe092013-04-14 20:15:26 -07003859 .name = "cgroup.sane_behavior",
3860 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003861 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003862 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003863
3864 /*
3865 * Historical crazy stuff. These don't have "cgroup." prefix and
3866 * don't exist if sane_behavior. If you're depending on these, be
3867 * prepared to be burned.
3868 */
3869 {
3870 .name = "tasks",
3871 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003872 .seq_start = cgroup_pidlist_start,
3873 .seq_next = cgroup_pidlist_next,
3874 .seq_stop = cgroup_pidlist_stop,
3875 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003876 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003877 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003878 .mode = S_IRUGO | S_IWUSR,
3879 },
3880 {
3881 .name = "notify_on_release",
3882 .flags = CFTYPE_INSANE,
3883 .read_u64 = cgroup_read_notify_on_release,
3884 .write_u64 = cgroup_write_notify_on_release,
3885 },
Tejun Heo873fe092013-04-14 20:15:26 -07003886 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003887 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003888 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003889 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003890 .write_string = cgroup_release_agent_write,
3891 .max_write_len = PATH_MAX,
3892 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003893 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003894};
3895
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003896/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003897 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003898 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003899 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003900 *
3901 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003902 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003903static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003904{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003905 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003906 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003907
Tejun Heo8e3f6542012-04-01 12:09:55 -07003908 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003909 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003910 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07003911
3912 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003913 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003914
Tejun Heobee55092013-06-28 16:24:11 -07003915 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003916 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003917 if (ret < 0)
3918 goto err;
3919 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003920 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003921 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003922err:
3923 cgroup_clear_dir(cgrp, subsys_mask);
3924 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003925}
3926
Tejun Heo0c21ead2013-08-13 20:22:51 -04003927/*
3928 * css destruction is four-stage process.
3929 *
3930 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3931 * Implemented in kill_css().
3932 *
3933 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3934 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3935 * by invoking offline_css(). After offlining, the base ref is put.
3936 * Implemented in css_killed_work_fn().
3937 *
3938 * 3. When the percpu_ref reaches zero, the only possible remaining
3939 * accessors are inside RCU read sections. css_release() schedules the
3940 * RCU callback.
3941 *
3942 * 4. After the grace period, the css can be freed. Implemented in
3943 * css_free_work_fn().
3944 *
3945 * It is actually hairier because both step 2 and 4 require process context
3946 * and thus involve punting to css->destroy_work adding two additional
3947 * steps to the already complex sequence.
3948 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003949static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003950{
3951 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003952 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003953 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003954
Tejun Heo0ae78e02013-08-13 11:01:54 -04003955 if (css->parent)
3956 css_put(css->parent);
3957
Tejun Heo0c21ead2013-08-13 20:22:51 -04003958 css->ss->css_free(css);
3959 cgroup_dput(cgrp);
3960}
3961
3962static void css_free_rcu_fn(struct rcu_head *rcu_head)
3963{
3964 struct cgroup_subsys_state *css =
3965 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3966
3967 /*
3968 * css holds an extra ref to @cgrp->dentry which is put on the last
3969 * css_put(). dput() requires process context which we don't have.
3970 */
3971 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003972 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003973}
3974
Tejun Heod3daf282013-06-13 19:39:16 -07003975static void css_release(struct percpu_ref *ref)
3976{
3977 struct cgroup_subsys_state *css =
3978 container_of(ref, struct cgroup_subsys_state, refcnt);
3979
Tejun Heoaec25022014-02-08 10:36:58 -05003980 rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003981 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003982}
3983
Tejun Heo623f9262013-08-13 11:01:55 -04003984static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3985 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003986{
Paul Menagebd89aab2007-10-18 23:40:44 -07003987 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003988 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003989 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003990
Tejun Heo0ae78e02013-08-13 11:01:54 -04003991 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003992 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003993 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003994 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003995
Tejun Heoca8bdca2013-08-26 18:40:56 -04003996 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003997}
3998
Li Zefan2a4ac632013-07-31 16:16:40 +08003999/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004000static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004001{
Tejun Heo623f9262013-08-13 11:01:55 -04004002 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004003 int ret = 0;
4004
Tejun Heoace2bee2014-02-11 11:52:47 -05004005 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004006 lockdep_assert_held(&cgroup_mutex);
4007
Tejun Heo92fb9742012-11-19 08:13:38 -08004008 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004009 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004010 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004011 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004012 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05004013 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004014 }
Tejun Heob1929db2012-11-19 08:13:38 -08004015 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004016}
4017
Li Zefan2a4ac632013-07-31 16:16:40 +08004018/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004019static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004020{
Tejun Heo623f9262013-08-13 11:01:55 -04004021 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004022
Tejun Heoace2bee2014-02-11 11:52:47 -05004023 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004024 lockdep_assert_held(&cgroup_mutex);
4025
4026 if (!(css->flags & CSS_ONLINE))
4027 return;
4028
Li Zefand7eeac12013-03-12 15:35:59 -07004029 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004030 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004031
Tejun Heoeb954192013-08-08 20:11:23 -04004032 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004033 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05004034 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004035}
4036
Tejun Heoc81c925a2013-12-06 15:11:56 -05004037/**
4038 * create_css - create a cgroup_subsys_state
4039 * @cgrp: the cgroup new css will be associated with
4040 * @ss: the subsys of new css
4041 *
4042 * Create a new css associated with @cgrp - @ss pair. On success, the new
4043 * css is online and installed in @cgrp with all interface files created.
4044 * Returns 0 on success, -errno on failure.
4045 */
4046static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4047{
4048 struct cgroup *parent = cgrp->parent;
4049 struct cgroup_subsys_state *css;
4050 int err;
4051
4052 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
4053 lockdep_assert_held(&cgroup_mutex);
4054
4055 css = ss->css_alloc(cgroup_css(parent, ss));
4056 if (IS_ERR(css))
4057 return PTR_ERR(css);
4058
4059 err = percpu_ref_init(&css->refcnt, css_release);
4060 if (err)
4061 goto err_free;
4062
4063 init_css(css, ss, cgrp);
4064
Tejun Heoaec25022014-02-08 10:36:58 -05004065 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004066 if (err)
4067 goto err_free;
4068
4069 err = online_css(css);
4070 if (err)
4071 goto err_free;
4072
4073 dget(cgrp->dentry);
4074 css_get(css->parent);
4075
4076 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4077 parent->parent) {
4078 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",
4079 current->comm, current->pid, ss->name);
4080 if (!strcmp(ss->name, "memory"))
4081 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4082 ss->warned_broken_hierarchy = true;
4083 }
4084
4085 return 0;
4086
4087err_free:
4088 percpu_ref_cancel_init(&css->refcnt);
4089 ss->css_free(css);
4090 return err;
4091}
4092
Paul Menageddbcc7e2007-10-18 23:39:30 -07004093/*
Li Zefana043e3b2008-02-23 15:24:09 -08004094 * cgroup_create - create a cgroup
4095 * @parent: cgroup that will be parent of the new cgroup
4096 * @dentry: dentry of the new cgroup
4097 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004098 *
Li Zefana043e3b2008-02-23 15:24:09 -08004099 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004100 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004101static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004102 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004103{
Paul Menagebd89aab2007-10-18 23:40:44 -07004104 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004105 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004106 struct cgroupfs_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05004107 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004108 struct cgroup_subsys *ss;
4109 struct super_block *sb = root->sb;
4110
Tejun Heo0a950f62012-11-19 09:02:12 -08004111 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004112 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4113 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004114 return -ENOMEM;
4115
Li Zefan65dff752013-03-01 15:01:56 +08004116 name = cgroup_alloc_name(dentry);
Tejun Heob58c8992014-02-08 10:26:33 -05004117 if (!name) {
4118 err = -ENOMEM;
Li Zefan65dff752013-03-01 15:01:56 +08004119 goto err_free_cgrp;
Tejun Heob58c8992014-02-08 10:26:33 -05004120 }
Li Zefan65dff752013-03-01 15:01:56 +08004121 rcu_assign_pointer(cgrp->name, name);
4122
Tejun Heoace2bee2014-02-11 11:52:47 -05004123 mutex_lock(&cgroup_tree_mutex);
4124
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004125 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08004126 * Only live parents can have children. Note that the liveliness
4127 * check isn't strictly necessary because cgroup_mkdir() and
4128 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4129 * anyway so that locking is contained inside cgroup proper and we
4130 * don't get nasty surprises if we ever grow another caller.
4131 */
4132 if (!cgroup_lock_live_group(parent)) {
4133 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05004134 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004135 }
4136
4137 /*
4138 * Temporarily set the pointer to NULL, so idr_find() won't return
4139 * a half-baked cgroup.
4140 */
4141 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
4142 if (cgrp->id < 0) {
4143 err = -ENOMEM;
4144 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08004145 }
4146
Paul Menageddbcc7e2007-10-18 23:39:30 -07004147 /* Grab a reference on the superblock so the hierarchy doesn't
4148 * get deleted on unmount if there are child cgroups. This
4149 * can be done outside cgroup_mutex, since the sb can't
4150 * disappear while someone has an open control file on the
4151 * fs */
4152 atomic_inc(&sb->s_active);
4153
Paul Menagecc31edc2008-10-18 20:28:04 -07004154 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004155
Li Zefanfe1c06c2013-01-24 14:30:22 +08004156 dentry->d_fsdata = cgrp;
4157 cgrp->dentry = dentry;
4158
Paul Menagebd89aab2007-10-18 23:40:44 -07004159 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004160 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004161 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004162
Li Zefanb6abdb02008-03-04 14:28:19 -08004163 if (notify_on_release(parent))
4164 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4165
Tejun Heo2260e7f2012-11-19 08:13:38 -08004166 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4167 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004168
Tejun Heo4e139af2012-11-19 08:13:36 -08004169 /*
4170 * Create directory. cgroup_create_file() returns with the new
4171 * directory locked on success so that it can be populated without
4172 * dropping cgroup_mutex.
4173 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004174 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004175 if (err < 0)
Li Zefan0ab02ca2014-02-11 16:05:46 +08004176 goto err_free_id;
Tejun Heo4e139af2012-11-19 08:13:36 -08004177 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004178
Tejun Heo00356bd2013-06-18 11:14:22 -07004179 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004180
Tejun Heo4e139af2012-11-19 08:13:36 -08004181 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004182 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4183 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004184
Li Zefan415cf072013-04-08 14:35:02 +08004185 /* hold a ref to the parent's dentry */
4186 dget(parent->dentry);
4187
Tejun Heo0d802552013-12-06 15:11:56 -05004188 /*
4189 * @cgrp is now fully operational. If something fails after this
4190 * point, it'll be released via the normal destruction path.
4191 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004192 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4193
Tejun Heo2bb566c2013-08-08 20:11:23 -04004194 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004195 if (err)
4196 goto err_destroy;
4197
Tejun Heo9d403e92013-12-06 15:11:56 -05004198 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004199 for_each_subsys(ss, ssid) {
4200 if (root->subsys_mask & (1 << ssid)) {
4201 err = create_css(cgrp, ss);
4202 if (err)
4203 goto err_destroy;
4204 }
Tejun Heoa8638032012-11-09 09:12:29 -08004205 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004206
4207 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004208 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004209 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004210
4211 return 0;
4212
Tejun Heo0a950f62012-11-19 09:02:12 -08004213err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004214 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004215 /* Release the reference count that we took on the superblock */
4216 deactivate_super(sb);
4217err_unlock:
4218 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004219err_unlock_tree:
4220 mutex_unlock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08004221 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004222err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004223 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004224 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004225
4226err_destroy:
4227 cgroup_destroy_locked(cgrp);
4228 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004229 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004230 mutex_unlock(&dentry->d_inode->i_mutex);
4231 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004232}
4233
Al Viro18bb1db2011-07-26 01:41:39 -04004234static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004235{
4236 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4237
4238 /* the vfs holds inode->i_mutex already */
4239 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4240}
4241
Tejun Heo223dbc32013-08-13 20:22:50 -04004242/*
4243 * This is called when the refcnt of a css is confirmed to be killed.
4244 * css_tryget() is now guaranteed to fail.
4245 */
4246static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004247{
Tejun Heo223dbc32013-08-13 20:22:50 -04004248 struct cgroup_subsys_state *css =
4249 container_of(work, struct cgroup_subsys_state, destroy_work);
4250 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004251
Tejun Heoace2bee2014-02-11 11:52:47 -05004252 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004253 mutex_lock(&cgroup_mutex);
4254
4255 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004256 * css_tryget() is guaranteed to fail now. Tell subsystems to
4257 * initate destruction.
4258 */
4259 offline_css(css);
4260
4261 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004262 * If @cgrp is marked dead, it's waiting for refs of all css's to
4263 * be disabled before proceeding to the second phase of cgroup
4264 * destruction. If we are the last one, kick it off.
4265 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004266 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004267 cgroup_destroy_css_killed(cgrp);
4268
4269 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004270 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004271
4272 /*
4273 * Put the css refs from kill_css(). Each css holds an extra
4274 * reference to the cgroup's dentry and cgroup removal proceeds
4275 * regardless of css refs. On the last put of each css, whenever
4276 * that may be, the extra dentry ref is put so that dentry
4277 * destruction happens only after all css's are released.
4278 */
4279 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004280}
4281
Tejun Heo223dbc32013-08-13 20:22:50 -04004282/* css kill confirmation processing requires process context, bounce */
4283static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004284{
4285 struct cgroup_subsys_state *css =
4286 container_of(ref, struct cgroup_subsys_state, refcnt);
4287
Tejun Heo223dbc32013-08-13 20:22:50 -04004288 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004289 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004290}
4291
4292/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004293 * kill_css - destroy a css
4294 * @css: css to destroy
4295 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004296 * This function initiates destruction of @css by removing cgroup interface
4297 * files and putting its base reference. ->css_offline() will be invoked
4298 * asynchronously once css_tryget() is guaranteed to fail and when the
4299 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004300 */
4301static void kill_css(struct cgroup_subsys_state *css)
4302{
Tejun Heoaec25022014-02-08 10:36:58 -05004303 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004304
Tejun Heoedae0c32013-08-13 20:22:51 -04004305 /*
4306 * Killing would put the base ref, but we need to keep it alive
4307 * until after ->css_offline().
4308 */
4309 css_get(css);
4310
4311 /*
4312 * cgroup core guarantees that, by the time ->css_offline() is
4313 * invoked, no new css reference will be given out via
4314 * css_tryget(). We can't simply call percpu_ref_kill() and
4315 * proceed to offlining css's because percpu_ref_kill() doesn't
4316 * guarantee that the ref is seen as killed on all CPUs on return.
4317 *
4318 * Use percpu_ref_kill_and_confirm() to get notifications as each
4319 * css is confirmed to be seen as killed on all CPUs.
4320 */
4321 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004322}
4323
4324/**
4325 * cgroup_destroy_locked - the first stage of cgroup destruction
4326 * @cgrp: cgroup to be destroyed
4327 *
4328 * css's make use of percpu refcnts whose killing latency shouldn't be
4329 * exposed to userland and are RCU protected. Also, cgroup core needs to
4330 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4331 * invoked. To satisfy all the requirements, destruction is implemented in
4332 * the following two steps.
4333 *
4334 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4335 * userland visible parts and start killing the percpu refcnts of
4336 * css's. Set up so that the next stage will be kicked off once all
4337 * the percpu refcnts are confirmed to be killed.
4338 *
4339 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4340 * rest of destruction. Once all cgroup references are gone, the
4341 * cgroup is RCU-freed.
4342 *
4343 * This function implements s1. After this step, @cgrp is gone as far as
4344 * the userland is concerned and a new cgroup with the same name may be
4345 * created. As cgroup doesn't care about the names internally, this
4346 * doesn't cause any problem.
4347 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004348static int cgroup_destroy_locked(struct cgroup *cgrp)
4349 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004350{
Tejun Heo42809dd2012-11-19 08:13:37 -08004351 struct dentry *d = cgrp->dentry;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004352 struct cgroup_subsys_state *css;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004353 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004354 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004355 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004356
Tejun Heo42809dd2012-11-19 08:13:37 -08004357 lockdep_assert_held(&d->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004358 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004359 lockdep_assert_held(&cgroup_mutex);
4360
Tejun Heoddd69142013-06-12 21:04:54 -07004361 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004362 * css_set_lock synchronizes access to ->cset_links and prevents
4363 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004364 */
4365 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004366 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004367 read_unlock(&css_set_lock);
4368 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004369 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004370
Tejun Heo1a90dd52012-11-05 09:16:59 -08004371 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004372 * Make sure there's no live children. We can't test ->children
4373 * emptiness as dead children linger on it while being destroyed;
4374 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4375 */
4376 empty = true;
4377 rcu_read_lock();
4378 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4379 empty = cgroup_is_dead(child);
4380 if (!empty)
4381 break;
4382 }
4383 rcu_read_unlock();
4384 if (!empty)
4385 return -EBUSY;
4386
4387 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004388 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4389 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004390 * percpu refs of all css's are confirmed to be killed. This
4391 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004392 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004393 mutex_unlock(&cgroup_mutex);
Tejun Heo1c6727a2013-12-06 15:11:56 -05004394 for_each_css(css, ssid, cgrp)
4395 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004396 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004397
4398 /*
4399 * Mark @cgrp dead. This prevents further task migration and child
4400 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004401 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004402 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004403 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004404 */
Tejun Heo54766d42013-06-12 21:04:53 -07004405 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004406
Tejun Heo455050d2013-06-13 19:27:41 -07004407 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4408 raw_spin_lock(&release_list_lock);
4409 if (!list_empty(&cgrp->release_list))
4410 list_del_init(&cgrp->release_list);
4411 raw_spin_unlock(&release_list_lock);
4412
4413 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004414 * If @cgrp has css's attached, the second stage of cgroup
4415 * destruction is kicked off from css_killed_work_fn() after the
4416 * refs of all attached css's are killed. If @cgrp doesn't have
4417 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004418 */
Tejun Heof20104d2013-08-13 20:22:50 -04004419 if (!cgrp->nr_css)
4420 cgroup_destroy_css_killed(cgrp);
4421
4422 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004423 * Clear the base files and remove @cgrp directory. The removal
4424 * puts the base ref but we aren't quite done with @cgrp yet, so
4425 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004426 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004427 mutex_unlock(&cgroup_mutex);
Tejun Heo2bb566c2013-08-08 20:11:23 -04004428 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004429 dget(d);
4430 cgroup_d_remove_dir(d);
Tejun Heo4ac06012014-02-11 11:52:47 -05004431 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004432
Tejun Heoea15f8c2013-06-13 19:27:42 -07004433 return 0;
4434};
4435
Tejun Heod3daf282013-06-13 19:39:16 -07004436/**
Tejun Heof20104d2013-08-13 20:22:50 -04004437 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004438 * @work: cgroup->destroy_free_work
4439 *
4440 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004441 * destroyed after all css's are offlined and performs the rest of
4442 * destruction. This is the second step of destruction described in the
4443 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004444 */
Tejun Heof20104d2013-08-13 20:22:50 -04004445static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004446{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004447 struct cgroup *parent = cgrp->parent;
4448 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004449
Tejun Heoace2bee2014-02-11 11:52:47 -05004450 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004451 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004452
Paul Menage999cd8a2009-01-07 18:08:36 -08004453 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004454 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004455
Paul Menageddbcc7e2007-10-18 23:39:30 -07004456 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004457
Paul Menagebd89aab2007-10-18 23:40:44 -07004458 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004459 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004460}
4461
Tejun Heo42809dd2012-11-19 08:13:37 -08004462static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4463{
4464 int ret;
4465
Tejun Heoace2bee2014-02-11 11:52:47 -05004466 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004467 mutex_lock(&cgroup_mutex);
4468 ret = cgroup_destroy_locked(dentry->d_fsdata);
4469 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004470 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004471
4472 return ret;
4473}
4474
Tejun Heo3ed80a62014-02-08 10:36:58 -05004475static void __init cgroup_init_cftsets(struct cgroup_subsys *ss)
Tejun Heo8e3f6542012-04-01 12:09:55 -07004476{
4477 INIT_LIST_HEAD(&ss->cftsets);
4478
4479 /*
4480 * base_cftset is embedded in subsys itself, no need to worry about
4481 * deregistration.
4482 */
4483 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004484 struct cftype *cft;
4485
4486 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4487 cft->ss = ss;
4488
Tejun Heo8e3f6542012-04-01 12:09:55 -07004489 ss->base_cftset.cfts = ss->base_cftypes;
4490 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4491 }
4492}
4493
Li Zefan06a11922008-04-29 01:00:07 -07004494static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004495{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004496 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004497
4498 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004499
Tejun Heoace2bee2014-02-11 11:52:47 -05004500 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004501 mutex_lock(&cgroup_mutex);
4502
Tejun Heo8e3f6542012-04-01 12:09:55 -07004503 /* init base cftset */
4504 cgroup_init_cftsets(ss);
4505
Paul Menageddbcc7e2007-10-18 23:39:30 -07004506 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004507 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004508 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004509 /* We don't handle early failures gracefully */
4510 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004511 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004512
Li Zefane8d55fd2008-04-29 01:00:13 -07004513 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004514 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004515 * newly registered, all tasks and hence the
4516 * init_css_set is in the subsystem's top cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004517 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004518
4519 need_forkexit_callback |= ss->fork || ss->exit;
4520
Li Zefane8d55fd2008-04-29 01:00:13 -07004521 /* At system boot, before all subsystems have been
4522 * registered, no tasks have been forked, so we don't
4523 * need to invoke fork callbacks here. */
4524 BUG_ON(!list_empty(&init_task.tasks));
4525
Tejun Heoae7f1642013-08-13 20:22:50 -04004526 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004527
Tejun Heo648bb562012-11-19 08:13:36 -08004528 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004529 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004530}
4531
4532/**
Li Zefana043e3b2008-02-23 15:24:09 -08004533 * cgroup_init_early - cgroup initialization at system boot
4534 *
4535 * Initialize cgroups at system boot, and initialize any
4536 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004537 */
4538int __init cgroup_init_early(void)
4539{
Tejun Heo30159ec2013-06-25 11:53:37 -07004540 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004541 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004542
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004543 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004544 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004545 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004546 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004547 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004548 init_cgroup_root(&cgroup_dummy_root);
4549 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004550 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004551
Tejun Heo69d02062013-06-12 21:04:50 -07004552 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004553 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4554 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004555 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004556
Tejun Heo3ed80a62014-02-08 10:36:58 -05004557 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004558 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004559 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4560 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004561 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004562 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4563 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4564
Tejun Heoaec25022014-02-08 10:36:58 -05004565 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004566 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004567
4568 if (ss->early_init)
4569 cgroup_init_subsys(ss);
4570 }
4571 return 0;
4572}
4573
4574/**
Li Zefana043e3b2008-02-23 15:24:09 -08004575 * cgroup_init - cgroup initialization
4576 *
4577 * Register cgroup filesystem and /proc file, and initialize
4578 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004579 */
4580int __init cgroup_init(void)
4581{
Tejun Heo30159ec2013-06-25 11:53:37 -07004582 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004583 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004584 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004585
4586 err = bdi_init(&cgroup_backing_dev_info);
4587 if (err)
4588 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004589
Tejun Heo3ed80a62014-02-08 10:36:58 -05004590 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004591 if (!ss->early_init)
4592 cgroup_init_subsys(ss);
4593 }
4594
Tejun Heofa3ca072013-04-14 11:36:56 -07004595 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004596 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004597
Tejun Heo82fe9b02013-06-25 11:53:37 -07004598 /* Add init_css_set to the hash table */
4599 key = css_set_hash(init_css_set.subsys);
4600 hash_add(css_set_table, &init_css_set.hlist, key);
4601
Tejun Heofc76df72013-06-25 11:53:37 -07004602 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004603
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004604 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4605 0, 1, GFP_KERNEL);
4606 BUG_ON(err < 0);
4607
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004608 mutex_unlock(&cgroup_mutex);
4609
Greg KH676db4a2010-08-05 13:53:35 -07004610 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4611 if (!cgroup_kobj) {
4612 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004613 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004614 }
4615
4616 err = register_filesystem(&cgroup_fs_type);
4617 if (err < 0) {
4618 kobject_put(cgroup_kobj);
4619 goto out;
4620 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004621
Li Zefan46ae2202008-04-29 01:00:08 -07004622 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004623
Paul Menageddbcc7e2007-10-18 23:39:30 -07004624out:
Paul Menagea4243162007-10-18 23:39:35 -07004625 if (err)
4626 bdi_destroy(&cgroup_backing_dev_info);
4627
Paul Menageddbcc7e2007-10-18 23:39:30 -07004628 return err;
4629}
Paul Menageb4f48b62007-10-18 23:39:33 -07004630
Tejun Heoe5fca242013-11-22 17:14:39 -05004631static int __init cgroup_wq_init(void)
4632{
4633 /*
4634 * There isn't much point in executing destruction path in
4635 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004636 *
4637 * XXX: Must be ordered to make sure parent is offlined after
4638 * children. The ordering requirement is for memcg where a
4639 * parent's offline may wait for a child's leading to deadlock. In
4640 * the long term, this should be fixed from memcg side.
Tejun Heoe5fca242013-11-22 17:14:39 -05004641 *
4642 * We would prefer to do this in cgroup_init() above, but that
4643 * is called before init_workqueues(): so leave this until after.
4644 */
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004645 cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0);
Tejun Heoe5fca242013-11-22 17:14:39 -05004646 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004647
4648 /*
4649 * Used to destroy pidlists and separate to serve as flush domain.
4650 * Cap @max_active to 1 too.
4651 */
4652 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4653 0, 1);
4654 BUG_ON(!cgroup_pidlist_destroy_wq);
4655
Tejun Heoe5fca242013-11-22 17:14:39 -05004656 return 0;
4657}
4658core_initcall(cgroup_wq_init);
4659
Paul Menagea4243162007-10-18 23:39:35 -07004660/*
4661 * proc_cgroup_show()
4662 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4663 * - Used for /proc/<pid>/cgroup.
4664 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4665 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004666 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004667 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4668 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4669 * cgroup to top_cgroup.
4670 */
4671
4672/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004673int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004674{
4675 struct pid *pid;
4676 struct task_struct *tsk;
4677 char *buf;
4678 int retval;
4679 struct cgroupfs_root *root;
4680
4681 retval = -ENOMEM;
4682 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4683 if (!buf)
4684 goto out;
4685
4686 retval = -ESRCH;
4687 pid = m->private;
4688 tsk = get_pid_task(pid, PIDTYPE_PID);
4689 if (!tsk)
4690 goto out_free;
4691
4692 retval = 0;
4693
4694 mutex_lock(&cgroup_mutex);
4695
Li Zefane5f6a862009-01-07 18:07:41 -08004696 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004697 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004698 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004699 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004700
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004701 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004702 for_each_subsys(ss, ssid)
4703 if (root->subsys_mask & (1 << ssid))
4704 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004705 if (strlen(root->name))
4706 seq_printf(m, "%sname=%s", count ? "," : "",
4707 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004708 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004709 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004710 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004711 if (retval < 0)
4712 goto out_unlock;
4713 seq_puts(m, buf);
4714 seq_putc(m, '\n');
4715 }
4716
4717out_unlock:
4718 mutex_unlock(&cgroup_mutex);
4719 put_task_struct(tsk);
4720out_free:
4721 kfree(buf);
4722out:
4723 return retval;
4724}
4725
Paul Menagea4243162007-10-18 23:39:35 -07004726/* Display information about each subsystem and each hierarchy */
4727static int proc_cgroupstats_show(struct seq_file *m, void *v)
4728{
Tejun Heo30159ec2013-06-25 11:53:37 -07004729 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004730 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004731
Paul Menage8bab8dd2008-04-04 14:29:57 -07004732 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004733 /*
4734 * ideally we don't want subsystems moving around while we do this.
4735 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4736 * subsys/hierarchy state.
4737 */
Paul Menagea4243162007-10-18 23:39:35 -07004738 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004739
4740 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004741 seq_printf(m, "%s\t%d\t%d\t%d\n",
4742 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004743 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004744
Paul Menagea4243162007-10-18 23:39:35 -07004745 mutex_unlock(&cgroup_mutex);
4746 return 0;
4747}
4748
4749static int cgroupstats_open(struct inode *inode, struct file *file)
4750{
Al Viro9dce07f2008-03-29 03:07:28 +00004751 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004752}
4753
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004754static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004755 .open = cgroupstats_open,
4756 .read = seq_read,
4757 .llseek = seq_lseek,
4758 .release = single_release,
4759};
4760
Paul Menageb4f48b62007-10-18 23:39:33 -07004761/**
4762 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004763 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004764 *
4765 * Description: A task inherits its parent's cgroup at fork().
4766 *
4767 * A pointer to the shared css_set was automatically copied in
4768 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004769 * it was not made under the protection of RCU or cgroup_mutex, so
4770 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4771 * have already changed current->cgroups, allowing the previously
4772 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004773 *
4774 * At the point that cgroup_fork() is called, 'current' is the parent
4775 * task, and the passed argument 'child' points to the child task.
4776 */
4777void cgroup_fork(struct task_struct *child)
4778{
Tejun Heo9bb71302012-10-18 17:52:07 -07004779 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004780 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004781 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004782 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004783 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004784}
4785
4786/**
Li Zefana043e3b2008-02-23 15:24:09 -08004787 * cgroup_post_fork - called on a new task after adding it to the task list
4788 * @child: the task in question
4789 *
Tejun Heo5edee612012-10-16 15:03:14 -07004790 * Adds the task to the list running through its css_set if necessary and
4791 * call the subsystem fork() callbacks. Has to be after the task is
4792 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004793 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004794 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004795 */
Paul Menage817929e2007-10-18 23:39:36 -07004796void cgroup_post_fork(struct task_struct *child)
4797{
Tejun Heo30159ec2013-06-25 11:53:37 -07004798 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004799 int i;
4800
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004801 /*
4802 * use_task_css_set_links is set to 1 before we walk the tasklist
4803 * under the tasklist_lock and we read it here after we added the child
4804 * to the tasklist under the tasklist_lock as well. If the child wasn't
4805 * yet in the tasklist when we walked through it from
4806 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4807 * should be visible now due to the paired locking and barriers implied
4808 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4809 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4810 * lock on fork.
4811 */
Paul Menage817929e2007-10-18 23:39:36 -07004812 if (use_task_css_set_links) {
4813 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004814 task_lock(child);
4815 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004816 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004817 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004818 write_unlock(&css_set_lock);
4819 }
Tejun Heo5edee612012-10-16 15:03:14 -07004820
4821 /*
4822 * Call ss->fork(). This must happen after @child is linked on
4823 * css_set; otherwise, @child might change state between ->fork()
4824 * and addition to css_set.
4825 */
4826 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004827 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004828 if (ss->fork)
4829 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004830 }
Paul Menage817929e2007-10-18 23:39:36 -07004831}
Tejun Heo5edee612012-10-16 15:03:14 -07004832
Paul Menage817929e2007-10-18 23:39:36 -07004833/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004834 * cgroup_exit - detach cgroup from exiting task
4835 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004836 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004837 *
4838 * Description: Detach cgroup from @tsk and release it.
4839 *
4840 * Note that cgroups marked notify_on_release force every task in
4841 * them to take the global cgroup_mutex mutex when exiting.
4842 * This could impact scaling on very large systems. Be reluctant to
4843 * use notify_on_release cgroups where very high task exit scaling
4844 * is required on large systems.
4845 *
4846 * the_top_cgroup_hack:
4847 *
4848 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4849 *
4850 * We call cgroup_exit() while the task is still competent to
4851 * handle notify_on_release(), then leave the task attached to the
4852 * root cgroup in each hierarchy for the remainder of its exit.
4853 *
4854 * To do this properly, we would increment the reference count on
4855 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4856 * code we would add a second cgroup function call, to drop that
4857 * reference. This would just create an unnecessary hot spot on
4858 * the top_cgroup reference count, to no avail.
4859 *
4860 * Normally, holding a reference to a cgroup without bumping its
4861 * count is unsafe. The cgroup could go away, or someone could
4862 * attach us to a different cgroup, decrementing the count on
4863 * the first cgroup that we never incremented. But in this case,
4864 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004865 * which wards off any cgroup_attach_task() attempts, or task is a failed
4866 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004867 */
4868void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4869{
Tejun Heo30159ec2013-06-25 11:53:37 -07004870 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004871 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004872 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004873
4874 /*
4875 * Unlink from the css_set task list if necessary.
4876 * Optimistically check cg_list before taking
4877 * css_set_lock
4878 */
4879 if (!list_empty(&tsk->cg_list)) {
4880 write_lock(&css_set_lock);
4881 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004882 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004883 write_unlock(&css_set_lock);
4884 }
4885
Paul Menageb4f48b62007-10-18 23:39:33 -07004886 /* Reassign the task to the init_css_set. */
4887 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004888 cset = task_css_set(tsk);
4889 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004890
4891 if (run_callbacks && need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004892 /* see cgroup_post_fork() for details */
4893 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004894 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004895 struct cgroup_subsys_state *old_css = cset->subsys[i];
4896 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004897
Tejun Heoeb954192013-08-08 20:11:23 -04004898 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004899 }
4900 }
4901 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004902 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004903
Tejun Heo5abb8852013-06-12 21:04:49 -07004904 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07004905}
Paul Menage697f4162007-10-18 23:39:34 -07004906
Paul Menagebd89aab2007-10-18 23:40:44 -07004907static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004908{
Li Zefanf50daa72013-03-01 15:06:07 +08004909 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004910 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004911 /*
4912 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004913 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004914 * it now
4915 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004916 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004917
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004918 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004919 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004920 list_empty(&cgrp->release_list)) {
4921 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004922 need_schedule_work = 1;
4923 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004924 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004925 if (need_schedule_work)
4926 schedule_work(&release_agent_work);
4927 }
4928}
4929
Paul Menage81a6a5c2007-10-18 23:39:38 -07004930/*
4931 * Notify userspace when a cgroup is released, by running the
4932 * configured release agent with the name of the cgroup (path
4933 * relative to the root of cgroup file system) as the argument.
4934 *
4935 * Most likely, this user command will try to rmdir this cgroup.
4936 *
4937 * This races with the possibility that some other task will be
4938 * attached to this cgroup before it is removed, or that some other
4939 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4940 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4941 * unused, and this cgroup will be reprieved from its death sentence,
4942 * to continue to serve a useful existence. Next time it's released,
4943 * we will get notified again, if it still has 'notify_on_release' set.
4944 *
4945 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4946 * means only wait until the task is successfully execve()'d. The
4947 * separate release agent task is forked by call_usermodehelper(),
4948 * then control in this thread returns here, without waiting for the
4949 * release agent task. We don't bother to wait because the caller of
4950 * this routine has no use for the exit status of the release agent
4951 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004952 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004953static void cgroup_release_agent(struct work_struct *work)
4954{
4955 BUG_ON(work != &release_agent_work);
4956 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004957 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004958 while (!list_empty(&release_list)) {
4959 char *argv[3], *envp[3];
4960 int i;
Paul Menagee788e062008-07-25 01:46:59 -07004961 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004962 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004963 struct cgroup,
4964 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004965 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004966 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004967 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004968 if (!pathbuf)
4969 goto continue_free;
4970 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
4971 goto continue_free;
4972 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4973 if (!agentbuf)
4974 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004975
4976 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004977 argv[i++] = agentbuf;
4978 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004979 argv[i] = NULL;
4980
4981 i = 0;
4982 /* minimal command environment */
4983 envp[i++] = "HOME=/";
4984 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4985 envp[i] = NULL;
4986
4987 /* Drop the lock while we invoke the usermode helper,
4988 * since the exec could involve hitting disk and hence
4989 * be a slow process */
4990 mutex_unlock(&cgroup_mutex);
4991 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004992 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004993 continue_free:
4994 kfree(pathbuf);
4995 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004996 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004997 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004998 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004999 mutex_unlock(&cgroup_mutex);
5000}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005001
5002static int __init cgroup_disable(char *str)
5003{
Tejun Heo30159ec2013-06-25 11:53:37 -07005004 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005005 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005006 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005007
5008 while ((token = strsep(&str, ",")) != NULL) {
5009 if (!*token)
5010 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005011
Tejun Heo3ed80a62014-02-08 10:36:58 -05005012 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005013 if (!strcmp(token, ss->name)) {
5014 ss->disabled = 1;
5015 printk(KERN_INFO "Disabling %s control group"
5016 " subsystem\n", ss->name);
5017 break;
5018 }
5019 }
5020 }
5021 return 1;
5022}
5023__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005024
Tejun Heob77d7b62013-08-13 11:01:54 -04005025/**
Tejun Heo5a17f542014-02-11 11:52:47 -05005026 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04005027 * @dentry: directory dentry of interest
5028 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005029 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005030 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5031 * to get the corresponding css and return it. If such css doesn't exist
5032 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005033 */
Tejun Heo5a17f542014-02-11 11:52:47 -05005034struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
5035 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005036{
5037 struct cgroup *cgrp;
Tejun Heo5a17f542014-02-11 11:52:47 -05005038 struct cgroup_subsys_state *css;
Tejun Heob77d7b62013-08-13 11:01:54 -04005039
Tejun Heo35cf0832013-08-26 18:40:56 -04005040 /* is @dentry a cgroup dir? */
5041 if (!dentry->d_inode ||
5042 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005043 return ERR_PTR(-EBADF);
5044
Tejun Heo5a17f542014-02-11 11:52:47 -05005045 rcu_read_lock();
5046
Tejun Heo35cf0832013-08-26 18:40:56 -04005047 cgrp = __d_cgrp(dentry);
Tejun Heo5a17f542014-02-11 11:52:47 -05005048 css = cgroup_css(cgrp, ss);
5049
5050 if (!css || !css_tryget(css))
5051 css = ERR_PTR(-ENOENT);
5052
5053 rcu_read_unlock();
5054 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005055}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005056
Li Zefan1cb650b2013-08-19 10:05:24 +08005057/**
5058 * css_from_id - lookup css by id
5059 * @id: the cgroup id
5060 * @ss: cgroup subsys to be looked into
5061 *
5062 * Returns the css if there's valid one with @id, otherwise returns NULL.
5063 * Should be called under rcu_read_lock().
5064 */
5065struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5066{
5067 struct cgroup *cgrp;
5068
Tejun Heoace2bee2014-02-11 11:52:47 -05005069 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08005070
5071 cgrp = idr_find(&ss->root->cgroup_idr, id);
5072 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005073 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005074 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005075}
5076
Paul Menagefe693432009-09-23 15:56:20 -07005077#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005078static struct cgroup_subsys_state *
5079debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005080{
5081 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5082
5083 if (!css)
5084 return ERR_PTR(-ENOMEM);
5085
5086 return css;
5087}
5088
Tejun Heoeb954192013-08-08 20:11:23 -04005089static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005090{
Tejun Heoeb954192013-08-08 20:11:23 -04005091 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005092}
5093
Tejun Heo182446d2013-08-08 20:11:24 -04005094static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5095 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005096{
Tejun Heo182446d2013-08-08 20:11:24 -04005097 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005098}
5099
Tejun Heo182446d2013-08-08 20:11:24 -04005100static u64 current_css_set_read(struct cgroup_subsys_state *css,
5101 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005102{
5103 return (u64)(unsigned long)current->cgroups;
5104}
5105
Tejun Heo182446d2013-08-08 20:11:24 -04005106static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005107 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005108{
5109 u64 count;
5110
5111 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005112 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005113 rcu_read_unlock();
5114 return count;
5115}
5116
Tejun Heo2da8ca82013-12-05 12:28:04 -05005117static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005118{
Tejun Heo69d02062013-06-12 21:04:50 -07005119 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005120 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005121
5122 read_lock(&css_set_lock);
5123 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005124 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005125 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005126 struct cgroup *c = link->cgrp;
5127 const char *name;
5128
5129 if (c->dentry)
5130 name = c->dentry->d_name.name;
5131 else
5132 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005133 seq_printf(seq, "Root %d group %s\n",
5134 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005135 }
5136 rcu_read_unlock();
5137 read_unlock(&css_set_lock);
5138 return 0;
5139}
5140
5141#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005142static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005143{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005144 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005145 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005146
5147 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005148 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005149 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005150 struct task_struct *task;
5151 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005152 seq_printf(seq, "css_set %p\n", cset);
5153 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005154 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5155 seq_puts(seq, " ...\n");
5156 break;
5157 } else {
5158 seq_printf(seq, " task %d\n",
5159 task_pid_vnr(task));
5160 }
5161 }
5162 }
5163 read_unlock(&css_set_lock);
5164 return 0;
5165}
5166
Tejun Heo182446d2013-08-08 20:11:24 -04005167static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005168{
Tejun Heo182446d2013-08-08 20:11:24 -04005169 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005170}
5171
5172static struct cftype debug_files[] = {
5173 {
Paul Menagefe693432009-09-23 15:56:20 -07005174 .name = "taskcount",
5175 .read_u64 = debug_taskcount_read,
5176 },
5177
5178 {
5179 .name = "current_css_set",
5180 .read_u64 = current_css_set_read,
5181 },
5182
5183 {
5184 .name = "current_css_set_refcount",
5185 .read_u64 = current_css_set_refcount_read,
5186 },
5187
5188 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005189 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005190 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005191 },
5192
5193 {
5194 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005195 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005196 },
5197
5198 {
Paul Menagefe693432009-09-23 15:56:20 -07005199 .name = "releasable",
5200 .read_u64 = releasable_read,
5201 },
Paul Menagefe693432009-09-23 15:56:20 -07005202
Tejun Heo4baf6e32012-04-01 12:09:55 -07005203 { } /* terminate */
5204};
Paul Menagefe693432009-09-23 15:56:20 -07005205
Tejun Heo073219e2014-02-08 10:36:58 -05005206struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005207 .css_alloc = debug_css_alloc,
5208 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005209 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005210};
5211#endif /* CONFIG_CGROUP_DEBUG */