blob: d0def7fc2848dd899281b2bb2527f05fa79ec7a9 [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/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heo28b4c272012-04-01 12:09:56 -070066/* css deactivation bias, makes css->refcnt negative to deny new trygets */
67#define CSS_DEACT_BIAS INT_MIN
68
Tejun Heoe25e2cb2011-12-12 18:12:21 -080069/*
70 * cgroup_mutex is the master lock. Any modification to cgroup or its
71 * hierarchy must be performed while holding it.
72 *
73 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
74 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
75 * release_agent_path and so on. Modifying requires both cgroup_mutex and
76 * cgroup_root_mutex. Readers can acquire either of the two. This is to
77 * break the following locking order cycle.
78 *
79 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
80 * B. namespace_sem -> cgroup_mutex
81 *
82 * B happens only through cgroup_show_options() and using cgroup_root_mutex
83 * breaks it.
84 */
Tejun Heo22194492013-04-07 09:29:51 -070085#ifdef CONFIG_PROVE_RCU
86DEFINE_MUTEX(cgroup_mutex);
87EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
88#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070089static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070090#endif
91
Tejun Heoe25e2cb2011-12-12 18:12:21 -080092static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070093
Ben Blumaae8aab2010-03-10 15:22:07 -080094/*
Tejun Heoa6647e92013-11-22 17:14:39 -050095 * cgroup destruction makes heavy use of work items and there can be a lot
96 * of concurrent destructions. Use a separate workqueue so that cgroup
97 * destruction work items don't end up filling up max_active of system_wq
98 * which may lead to deadlock.
99 */
100static struct workqueue_struct *cgroup_destroy_wq;
101
102/*
Ben Blumaae8aab2010-03-10 15:22:07 -0800103 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +0200104 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -0800105 * registered after that. The mutable section of this array is protected by
106 * cgroup_mutex.
107 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200108#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200109#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -0800110static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700111#include <linux/cgroup_subsys.h>
112};
113
Paul Menageddbcc7e2007-10-18 23:39:30 -0700114/*
115 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
116 * subsystems that are otherwise unattached - it never has more than a
117 * single cgroup, and all tasks are part of that cgroup.
118 */
119static struct cgroupfs_root rootnode;
120
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700121/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700122 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
123 */
124struct cfent {
125 struct list_head node;
126 struct dentry *dentry;
127 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700128
129 /* file xattrs */
130 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700131};
132
133/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700134 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
135 * cgroup_subsys->use_id != 0.
136 */
137#define CSS_ID_MAX (65535)
138struct css_id {
139 /*
140 * The css to which this ID points. This pointer is set to valid value
141 * after cgroup is populated. If cgroup is removed, this will be NULL.
142 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800143 * is called after synchronize_rcu(). But for safe use, css_tryget()
144 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700145 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100146 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700147 /*
148 * ID of this css.
149 */
150 unsigned short id;
151 /*
152 * Depth in hierarchy which this ID belongs to.
153 */
154 unsigned short depth;
155 /*
156 * ID is freed by RCU. (and lookup routine is RCU safe.)
157 */
158 struct rcu_head rcu_head;
159 /*
160 * Hierarchy of CSS ID belongs to.
161 */
162 unsigned short stack[0]; /* Array of Length (depth+1) */
163};
164
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800165/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300166 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800167 */
168struct cgroup_event {
169 /*
170 * Cgroup which the event belongs to.
171 */
172 struct cgroup *cgrp;
173 /*
174 * Control file which the event associated.
175 */
176 struct cftype *cft;
177 /*
178 * eventfd to signal userspace about the event.
179 */
180 struct eventfd_ctx *eventfd;
181 /*
182 * Each of these stored in a list by the cgroup.
183 */
184 struct list_head list;
185 /*
186 * All fields below needed to unregister event when
187 * userspace closes eventfd.
188 */
189 poll_table pt;
190 wait_queue_head_t *wqh;
191 wait_queue_t wait;
192 struct work_struct remove;
193};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700194
Paul Menageddbcc7e2007-10-18 23:39:30 -0700195/* The list of hierarchy roots */
196
197static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700198static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700199
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700200static DEFINE_IDA(hierarchy_ida);
201static int next_hierarchy_id;
202static DEFINE_SPINLOCK(hierarchy_id_lock);
203
Paul Menageddbcc7e2007-10-18 23:39:30 -0700204/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
205#define dummytop (&rootnode.top_cgroup)
206
Li Zefan65dff752013-03-01 15:01:56 +0800207static struct cgroup_name root_cgroup_name = { .name = "/" };
208
Paul Menageddbcc7e2007-10-18 23:39:30 -0700209/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800210 * check for fork/exit handlers to call. This avoids us having to do
211 * extra work in the fork/exit path if none of the subsystems need to
212 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700213 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700214static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700215
Tejun Heo42809dd2012-11-19 08:13:37 -0800216static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800217static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
218 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800219
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700220static int css_unbias_refcnt(int refcnt)
221{
222 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
223}
224
Tejun Heo28b4c272012-04-01 12:09:56 -0700225/* the current nr of refs, always >= 0 whether @css is deactivated or not */
226static int css_refcnt(struct cgroup_subsys_state *css)
227{
228 int v = atomic_read(&css->refcnt);
229
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700230 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700231}
232
Paul Menageddbcc7e2007-10-18 23:39:30 -0700233/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700234inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700235{
Paul Menagebd89aab2007-10-18 23:40:44 -0700236 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700237}
238
Li Zefan78574cf2013-04-08 19:00:38 -0700239/**
240 * cgroup_is_descendant - test ancestry
241 * @cgrp: the cgroup to be tested
242 * @ancestor: possible ancestor of @cgrp
243 *
244 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
245 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
246 * and @ancestor are accessible.
247 */
248bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
249{
250 while (cgrp) {
251 if (cgrp == ancestor)
252 return true;
253 cgrp = cgrp->parent;
254 }
255 return false;
256}
257EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700258
Adrian Bunke9685a02008-02-07 00:13:46 -0800259static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260{
261 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700262 (1 << CGRP_RELEASABLE) |
263 (1 << CGRP_NOTIFY_ON_RELEASE);
264 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700265}
266
Adrian Bunke9685a02008-02-07 00:13:46 -0800267static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700268{
Paul Menagebd89aab2007-10-18 23:40:44 -0700269 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700270}
271
Paul Menageddbcc7e2007-10-18 23:39:30 -0700272/*
273 * for_each_subsys() allows you to iterate on each subsystem attached to
274 * an active hierarchy
275 */
276#define for_each_subsys(_root, _ss) \
277list_for_each_entry(_ss, &_root->subsys_list, sibling)
278
Li Zefane5f6a862009-01-07 18:07:41 -0800279/* for_each_active_root() allows you to iterate across the active hierarchies */
280#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700281list_for_each_entry(_root, &roots, root_list)
282
Tejun Heof6ea9372012-04-01 12:09:55 -0700283static inline struct cgroup *__d_cgrp(struct dentry *dentry)
284{
285 return dentry->d_fsdata;
286}
287
Tejun Heo05ef1d72012-04-01 12:09:56 -0700288static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700289{
290 return dentry->d_fsdata;
291}
292
Tejun Heo05ef1d72012-04-01 12:09:56 -0700293static inline struct cftype *__d_cft(struct dentry *dentry)
294{
295 return __d_cfe(dentry)->type;
296}
297
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700298/**
299 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
300 * @cgrp: the cgroup to be checked for liveness
301 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700302 * On success, returns true; the mutex should be later unlocked. On
303 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700304 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700305static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700306{
307 mutex_lock(&cgroup_mutex);
308 if (cgroup_is_removed(cgrp)) {
309 mutex_unlock(&cgroup_mutex);
310 return false;
311 }
312 return true;
313}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700314
Paul Menage81a6a5c2007-10-18 23:39:38 -0700315/* the list of cgroups eligible for automatic release. Protected by
316 * release_list_lock */
317static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200318static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700319static void cgroup_release_agent(struct work_struct *work);
320static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700321static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700322
Paul Menage817929e2007-10-18 23:39:36 -0700323/* Link structure for associating css_set objects with cgroups */
324struct cg_cgroup_link {
325 /*
326 * List running through cg_cgroup_links associated with a
327 * cgroup, anchored on cgroup->css_sets
328 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700329 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700330 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700331 /*
332 * List running through cg_cgroup_links pointing at a
333 * single css_set object, anchored on css_set->cg_links
334 */
335 struct list_head cg_link_list;
336 struct css_set *cg;
337};
338
339/* The default css_set - used by init and its children prior to any
340 * hierarchies being mounted. It contains a pointer to the root state
341 * for each subsystem. Also used to anchor the list of css_sets. Not
342 * reference-counted, to improve performance when child cgroups
343 * haven't been created.
344 */
345
346static struct css_set init_css_set;
347static struct cg_cgroup_link init_css_set_link;
348
Ben Blume6a11052010-03-10 15:22:09 -0800349static int cgroup_init_idr(struct cgroup_subsys *ss,
350 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700351
Paul Menage817929e2007-10-18 23:39:36 -0700352/* css_set_lock protects the list of css_set objects, and the
353 * chain of tasks off each css_set. Nests outside task->alloc_lock
354 * due to cgroup_iter_start() */
355static DEFINE_RWLOCK(css_set_lock);
356static int css_set_count;
357
Paul Menage7717f7b2009-09-23 15:56:22 -0700358/*
359 * hash table for cgroup groups. This improves the performance to find
360 * an existing css_set. This hash doesn't (currently) take into
361 * account cgroups in empty hierarchies.
362 */
Li Zefan472b1052008-04-29 01:00:11 -0700363#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800364static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700365
Li Zefan0ac801f2013-01-10 11:49:27 +0800366static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700367{
368 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800369 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700370
371 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800372 key += (unsigned long)css[i];
373 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700374
Li Zefan0ac801f2013-01-10 11:49:27 +0800375 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700376}
377
Paul Menage817929e2007-10-18 23:39:36 -0700378/* We don't maintain the lists running through each css_set to its
379 * task until after the first call to cgroup_iter_start(). This
380 * reduces the fork()/exit() overhead for people who have cgroups
381 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700382static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700383
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700384static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700385{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700386 struct cg_cgroup_link *link;
387 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700388 /*
389 * Ensure that the refcount doesn't hit zero while any readers
390 * can see it. Similar to atomic_dec_and_lock(), but for an
391 * rwlock
392 */
393 if (atomic_add_unless(&cg->refcount, -1, 1))
394 return;
395 write_lock(&css_set_lock);
396 if (!atomic_dec_and_test(&cg->refcount)) {
397 write_unlock(&css_set_lock);
398 return;
399 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700400
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700401 /* This css_set is dead. unlink it and release cgroup refcounts */
Li Zefan0ac801f2013-01-10 11:49:27 +0800402 hash_del(&cg->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700403 css_set_count--;
404
405 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
406 cg_link_list) {
407 struct cgroup *cgrp = link->cgrp;
408 list_del(&link->cg_link_list);
409 list_del(&link->cgrp_link_list);
Li Zefan71b57072013-01-24 14:43:28 +0800410
411 /*
412 * We may not be holding cgroup_mutex, and if cgrp->count is
413 * dropped to 0 the cgroup can be destroyed at any time, hence
414 * rcu_read_lock is used to keep it alive.
415 */
416 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -0700417 if (atomic_dec_and_test(&cgrp->count) &&
418 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700419 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700420 set_bit(CGRP_RELEASABLE, &cgrp->flags);
421 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700422 }
Li Zefan71b57072013-01-24 14:43:28 +0800423 rcu_read_unlock();
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700424
425 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700426 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700427
428 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800429 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700430}
431
432/*
433 * refcounted get/put for css_set objects
434 */
435static inline void get_css_set(struct css_set *cg)
436{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700437 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700438}
439
440static inline void put_css_set(struct css_set *cg)
441{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700442 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700443}
444
Paul Menage81a6a5c2007-10-18 23:39:38 -0700445static inline void put_css_set_taskexit(struct css_set *cg)
446{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700447 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700448}
449
Paul Menage817929e2007-10-18 23:39:36 -0700450/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700451 * compare_css_sets - helper function for find_existing_css_set().
452 * @cg: candidate css_set being tested
453 * @old_cg: existing css_set for a task
454 * @new_cgrp: cgroup that's being entered by the task
455 * @template: desired set of css pointers in css_set (pre-calculated)
456 *
457 * Returns true if "cg" matches "old_cg" except for the hierarchy
458 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
459 */
460static bool compare_css_sets(struct css_set *cg,
461 struct css_set *old_cg,
462 struct cgroup *new_cgrp,
463 struct cgroup_subsys_state *template[])
464{
465 struct list_head *l1, *l2;
466
467 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
468 /* Not all subsystems matched */
469 return false;
470 }
471
472 /*
473 * Compare cgroup pointers in order to distinguish between
474 * different cgroups in heirarchies with no subsystems. We
475 * could get by with just this check alone (and skip the
476 * memcmp above) but on most setups the memcmp check will
477 * avoid the need for this more expensive check on almost all
478 * candidates.
479 */
480
481 l1 = &cg->cg_links;
482 l2 = &old_cg->cg_links;
483 while (1) {
484 struct cg_cgroup_link *cgl1, *cgl2;
485 struct cgroup *cg1, *cg2;
486
487 l1 = l1->next;
488 l2 = l2->next;
489 /* See if we reached the end - both lists are equal length. */
490 if (l1 == &cg->cg_links) {
491 BUG_ON(l2 != &old_cg->cg_links);
492 break;
493 } else {
494 BUG_ON(l2 == &old_cg->cg_links);
495 }
496 /* Locate the cgroups associated with these links. */
497 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
498 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
499 cg1 = cgl1->cgrp;
500 cg2 = cgl2->cgrp;
501 /* Hierarchies should be linked in the same order. */
502 BUG_ON(cg1->root != cg2->root);
503
504 /*
505 * If this hierarchy is the hierarchy of the cgroup
506 * that's changing, then we need to check that this
507 * css_set points to the new cgroup; if it's any other
508 * hierarchy, then this css_set should point to the
509 * same cgroup as the old css_set.
510 */
511 if (cg1->root == new_cgrp->root) {
512 if (cg1 != new_cgrp)
513 return false;
514 } else {
515 if (cg1 != cg2)
516 return false;
517 }
518 }
519 return true;
520}
521
522/*
Paul Menage817929e2007-10-18 23:39:36 -0700523 * find_existing_css_set() is a helper for
524 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700525 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700526 *
527 * oldcg: the cgroup group that we're using before the cgroup
528 * transition
529 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700530 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700531 *
532 * template: location in which to build the desired set of subsystem
533 * state objects for the new cgroup group
534 */
Paul Menage817929e2007-10-18 23:39:36 -0700535static struct css_set *find_existing_css_set(
536 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700537 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700538 struct cgroup_subsys_state *template[])
539{
540 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700541 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700542 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800543 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700544
Ben Blumaae8aab2010-03-10 15:22:07 -0800545 /*
546 * Build the set of subsystem state objects that we want to see in the
547 * new css_set. while subsystems can change globally, the entries here
548 * won't change, so no need for locking.
549 */
Paul Menage817929e2007-10-18 23:39:36 -0700550 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400551 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700552 /* Subsystem is in this hierarchy. So we want
553 * the subsystem state from the new
554 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700555 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700556 } else {
557 /* Subsystem is not in this hierarchy, so we
558 * don't want to change the subsystem state */
559 template[i] = oldcg->subsys[i];
560 }
561 }
562
Li Zefan0ac801f2013-01-10 11:49:27 +0800563 key = css_set_hash(template);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800564 hash_for_each_possible(css_set_table, cg, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700565 if (!compare_css_sets(cg, oldcg, cgrp, template))
566 continue;
567
568 /* This css_set matches what we need */
569 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700570 }
Paul Menage817929e2007-10-18 23:39:36 -0700571
572 /* No existing cgroup group matched */
573 return NULL;
574}
575
Paul Menage817929e2007-10-18 23:39:36 -0700576static void free_cg_links(struct list_head *tmp)
577{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700578 struct cg_cgroup_link *link;
579 struct cg_cgroup_link *saved_link;
580
581 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700582 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700583 kfree(link);
584 }
585}
586
587/*
Li Zefan36553432008-07-29 22:33:19 -0700588 * allocate_cg_links() allocates "count" cg_cgroup_link structures
589 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
590 * success or a negative error
591 */
592static int allocate_cg_links(int count, struct list_head *tmp)
593{
594 struct cg_cgroup_link *link;
595 int i;
596 INIT_LIST_HEAD(tmp);
597 for (i = 0; i < count; i++) {
598 link = kmalloc(sizeof(*link), GFP_KERNEL);
599 if (!link) {
600 free_cg_links(tmp);
601 return -ENOMEM;
602 }
603 list_add(&link->cgrp_link_list, tmp);
604 }
605 return 0;
606}
607
Li Zefanc12f65d2009-01-07 18:07:42 -0800608/**
609 * link_css_set - a helper function to link a css_set to a cgroup
610 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
611 * @cg: the css_set to be linked
612 * @cgrp: the destination cgroup
613 */
614static void link_css_set(struct list_head *tmp_cg_links,
615 struct css_set *cg, struct cgroup *cgrp)
616{
617 struct cg_cgroup_link *link;
618
619 BUG_ON(list_empty(tmp_cg_links));
620 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
621 cgrp_link_list);
622 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700623 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700624 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800625 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700626 /*
627 * Always add links to the tail of the list so that the list
628 * is sorted by order of hierarchy creation
629 */
630 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800631}
632
Li Zefan36553432008-07-29 22:33:19 -0700633/*
Paul Menage817929e2007-10-18 23:39:36 -0700634 * find_css_set() takes an existing cgroup group and a
635 * cgroup object, and returns a css_set object that's
636 * equivalent to the old group, but with the given cgroup
637 * substituted into the appropriate hierarchy. Must be called with
638 * cgroup_mutex held
639 */
Paul Menage817929e2007-10-18 23:39:36 -0700640static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700641 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700642{
643 struct css_set *res;
644 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700645
646 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700647
Paul Menage7717f7b2009-09-23 15:56:22 -0700648 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800649 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700650
Paul Menage817929e2007-10-18 23:39:36 -0700651 /* First see if we already have a cgroup group that matches
652 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700653 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700654 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700655 if (res)
656 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700657 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700658
659 if (res)
660 return res;
661
662 res = kmalloc(sizeof(*res), GFP_KERNEL);
663 if (!res)
664 return NULL;
665
666 /* Allocate all the cg_cgroup_link objects that we'll need */
667 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
668 kfree(res);
669 return NULL;
670 }
671
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700672 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700673 INIT_LIST_HEAD(&res->cg_links);
674 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700675 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700676
677 /* Copy the set of subsystem state objects generated in
678 * find_existing_css_set() */
679 memcpy(res->subsys, template, sizeof(res->subsys));
680
681 write_lock(&css_set_lock);
682 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700683 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
684 struct cgroup *c = link->cgrp;
685 if (c->root == cgrp->root)
686 c = cgrp;
687 link_css_set(&tmp_cg_links, res, c);
688 }
Paul Menage817929e2007-10-18 23:39:36 -0700689
690 BUG_ON(!list_empty(&tmp_cg_links));
691
Paul Menage817929e2007-10-18 23:39:36 -0700692 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700693
694 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800695 key = css_set_hash(res->subsys);
696 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700697
Paul Menage817929e2007-10-18 23:39:36 -0700698 write_unlock(&css_set_lock);
699
700 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700701}
702
Paul Menageddbcc7e2007-10-18 23:39:30 -0700703/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700704 * Return the cgroup for "task" from the given hierarchy. Must be
705 * called with cgroup_mutex held.
706 */
707static struct cgroup *task_cgroup_from_root(struct task_struct *task,
708 struct cgroupfs_root *root)
709{
710 struct css_set *css;
711 struct cgroup *res = NULL;
712
713 BUG_ON(!mutex_is_locked(&cgroup_mutex));
714 read_lock(&css_set_lock);
715 /*
716 * No need to lock the task - since we hold cgroup_mutex the
717 * task can't change groups, so the only thing that can happen
718 * is that it exits and its css is set back to init_css_set.
719 */
720 css = task->cgroups;
721 if (css == &init_css_set) {
722 res = &root->top_cgroup;
723 } else {
724 struct cg_cgroup_link *link;
725 list_for_each_entry(link, &css->cg_links, cg_link_list) {
726 struct cgroup *c = link->cgrp;
727 if (c->root == root) {
728 res = c;
729 break;
730 }
731 }
732 }
733 read_unlock(&css_set_lock);
734 BUG_ON(!res);
735 return res;
736}
737
738/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700739 * There is one global cgroup mutex. We also require taking
740 * task_lock() when dereferencing a task's cgroup subsys pointers.
741 * See "The task_lock() exception", at the end of this comment.
742 *
743 * A task must hold cgroup_mutex to modify cgroups.
744 *
745 * Any task can increment and decrement the count field without lock.
746 * So in general, code holding cgroup_mutex can't rely on the count
747 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800748 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700749 * means that no tasks are currently attached, therefore there is no
750 * way a task attached to that cgroup can fork (the other way to
751 * increment the count). So code holding cgroup_mutex can safely
752 * assume that if the count is zero, it will stay zero. Similarly, if
753 * a task holds cgroup_mutex on a cgroup with zero count, it
754 * knows that the cgroup won't be removed, as cgroup_rmdir()
755 * needs that mutex.
756 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700757 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
758 * (usually) take cgroup_mutex. These are the two most performance
759 * critical pieces of code here. The exception occurs on cgroup_exit(),
760 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
761 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800762 * to the release agent with the name of the cgroup (path relative to
763 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700764 *
765 * A cgroup can only be deleted if both its 'count' of using tasks
766 * is zero, and its list of 'children' cgroups is empty. Since all
767 * tasks in the system use _some_ cgroup, and since there is always at
768 * least one task in the system (init, pid == 1), therefore, top_cgroup
769 * always has either children cgroups and/or using tasks. So we don't
770 * need a special hack to ensure that top_cgroup cannot be deleted.
771 *
772 * The task_lock() exception
773 *
774 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800775 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800776 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700777 * several performance critical places that need to reference
778 * task->cgroup without the expense of grabbing a system global
779 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800780 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700781 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
782 * the task_struct routinely used for such matters.
783 *
784 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800785 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700786 */
787
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788/*
789 * A couple of forward declarations required, due to cyclic reference loop:
790 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
791 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
792 * -> cgroup_mkdir.
793 */
794
Al Viro18bb1db2011-07-26 01:41:39 -0400795static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400796static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700797static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400798static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
799 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700800static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700801static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700802
803static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200804 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700805 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700806};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700807
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700808static int alloc_css_id(struct cgroup_subsys *ss,
809 struct cgroup *parent, struct cgroup *child);
810
Al Viroa5e7ed32011-07-26 01:55:55 -0400811static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700812{
813 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700814
815 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400816 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700817 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100818 inode->i_uid = current_fsuid();
819 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700820 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
821 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
822 }
823 return inode;
824}
825
Li Zefan65dff752013-03-01 15:01:56 +0800826static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
827{
828 struct cgroup_name *name;
829
830 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
831 if (!name)
832 return NULL;
833 strcpy(name->name, dentry->d_name.name);
834 return name;
835}
836
Li Zefanbe445622013-01-24 14:31:42 +0800837static void cgroup_free_fn(struct work_struct *work)
838{
839 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
840 struct cgroup_subsys *ss;
841
842 mutex_lock(&cgroup_mutex);
843 /*
844 * Release the subsystem state objects.
845 */
846 for_each_subsys(cgrp->root, ss)
847 ss->css_free(cgrp);
848
849 cgrp->root->number_of_cgroups--;
850 mutex_unlock(&cgroup_mutex);
851
852 /*
Li Zefan415cf072013-04-08 14:35:02 +0800853 * We get a ref to the parent's dentry, and put the ref when
854 * this cgroup is being freed, so it's guaranteed that the
855 * parent won't be destroyed before its children.
856 */
857 dput(cgrp->parent->dentry);
858
Li Zefancc20e012013-04-26 11:58:02 -0700859 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
860
Li Zefan415cf072013-04-08 14:35:02 +0800861 /*
Li Zefanbe445622013-01-24 14:31:42 +0800862 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700863 * created the cgroup. This will free cgrp->root, if we are
864 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800865 */
866 deactivate_super(cgrp->root->sb);
867
868 /*
869 * if we're getting rid of the cgroup, refcount should ensure
870 * that there are no pidlists left.
871 */
872 BUG_ON(!list_empty(&cgrp->pidlists));
873
874 simple_xattrs_free(&cgrp->xattrs);
875
Li Zefan65dff752013-03-01 15:01:56 +0800876 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800877 kfree(cgrp);
878}
879
880static void cgroup_free_rcu(struct rcu_head *head)
881{
882 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
883
Tejun Heoa6647e92013-11-22 17:14:39 -0500884 queue_work(cgroup_destroy_wq, &cgrp->free_work);
Li Zefanbe445622013-01-24 14:31:42 +0800885}
886
Paul Menageddbcc7e2007-10-18 23:39:30 -0700887static void cgroup_diput(struct dentry *dentry, struct inode *inode)
888{
889 /* is dentry a directory ? if so, kfree() associated cgroup */
890 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700891 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800892
Paul Menagebd89aab2007-10-18 23:40:44 -0700893 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800894 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700895 } else {
896 struct cfent *cfe = __d_cfe(dentry);
897 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
898
899 WARN_ONCE(!list_empty(&cfe->node) &&
900 cgrp != &cgrp->root->top_cgroup,
901 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700902 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700903 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700904 }
905 iput(inode);
906}
907
Al Viroc72a04e2011-01-14 05:31:45 +0000908static int cgroup_delete(const struct dentry *d)
909{
910 return 1;
911}
912
Paul Menageddbcc7e2007-10-18 23:39:30 -0700913static void remove_dir(struct dentry *d)
914{
915 struct dentry *parent = dget(d->d_parent);
916
917 d_delete(d);
918 simple_rmdir(parent->d_inode, d);
919 dput(parent);
920}
921
Li Zefan2739d3c2013-01-21 18:18:33 +0800922static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700923{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700924 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700925
Tejun Heo05ef1d72012-04-01 12:09:56 -0700926 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
927 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100928
Li Zefan2739d3c2013-01-21 18:18:33 +0800929 /*
930 * If we're doing cleanup due to failure of cgroup_create(),
931 * the corresponding @cfe may not exist.
932 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700933 list_for_each_entry(cfe, &cgrp->files, node) {
934 struct dentry *d = cfe->dentry;
935
936 if (cft && cfe->type != cft)
937 continue;
938
939 dget(d);
940 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700941 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700942 list_del_init(&cfe->node);
943 dput(d);
944
Li Zefan2739d3c2013-01-21 18:18:33 +0800945 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700946 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700947}
948
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400949/**
950 * cgroup_clear_directory - selective removal of base and subsystem files
951 * @dir: directory containing the files
952 * @base_files: true if the base files should be removed
953 * @subsys_mask: mask of the subsystem ids whose files should be removed
954 */
955static void cgroup_clear_directory(struct dentry *dir, bool base_files,
956 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700957{
958 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400959 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700960
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400961 for_each_subsys(cgrp->root, ss) {
962 struct cftype_set *set;
963 if (!test_bit(ss->subsys_id, &subsys_mask))
964 continue;
965 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800966 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400967 }
968 if (base_files) {
969 while (!list_empty(&cgrp->files))
970 cgroup_rm_file(cgrp, NULL);
971 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972}
973
974/*
975 * NOTE : the dentry must have been dget()'ed
976 */
977static void cgroup_d_remove_dir(struct dentry *dentry)
978{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100979 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400980 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100981
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400982 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100984 parent = dentry->d_parent;
985 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800986 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100988 spin_unlock(&dentry->d_lock);
989 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990 remove_dir(dentry);
991}
992
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700993/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800994 * Call with cgroup_mutex held. Drops reference counts on modules, including
995 * any duplicate ones that parse_cgroupfs_options took. If this function
996 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800997 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400999 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000{
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001001 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001002 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 int i;
1004
Ben Blumaae8aab2010-03-10 15:22:07 -08001005 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001006 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001007
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001008 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1009 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010 /* Check that any added subsystems are currently free */
1011 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001012 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001014 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001016 /*
1017 * Nobody should tell us to do a subsys that doesn't exist:
1018 * parse_cgroupfs_options should catch that case and refcounts
1019 * ensure that subsystems won't disappear once selected.
1020 */
1021 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001022 if (ss->root != &rootnode) {
1023 /* Subsystem isn't free */
1024 return -EBUSY;
1025 }
1026 }
1027
1028 /* Currently we don't handle adding/removing subsystems when
1029 * any child cgroups exist. This is theoretically supportable
1030 * but involves complex error handling, so it's being left until
1031 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001032 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 return -EBUSY;
1034
1035 /* Process each subsystem */
1036 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1037 struct cgroup_subsys *ss = subsys[i];
1038 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001039 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001040 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001041 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001042 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 BUG_ON(!dummytop->subsys[i]);
1044 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001045 cgrp->subsys[i] = dummytop->subsys[i];
1046 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001047 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001048 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001050 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001051 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001052 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001054 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001055 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1056 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001058 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001060 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001061 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001062 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001063 /* subsystem is now free - drop reference on module */
1064 module_put(ss->module);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001065 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001067 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001068 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001069 /*
1070 * a refcount was taken, but we already had one, so
1071 * drop the extra reference.
1072 */
1073 module_put(ss->module);
1074#ifdef CONFIG_MODULE_UNLOAD
1075 BUG_ON(ss->module && !module_refcount(ss->module));
1076#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077 } else {
1078 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001079 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080 }
1081 }
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001082 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083
1084 return 0;
1085}
1086
Al Viro34c80b12011-12-08 21:32:45 -05001087static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088{
Al Viro34c80b12011-12-08 21:32:45 -05001089 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001090 struct cgroup_subsys *ss;
1091
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001092 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093 for_each_subsys(root, ss)
1094 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001095 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1096 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001097 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001098 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001099 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001100 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001101 if (strlen(root->release_agent_path))
1102 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001103 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001104 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001105 if (strlen(root->name))
1106 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001107 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108 return 0;
1109}
1110
1111struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001112 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001113 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001114 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001115 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001116 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001117 /* User explicitly requested empty subsystem */
1118 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001119
1120 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001121
Paul Menageddbcc7e2007-10-18 23:39:30 -07001122};
1123
Ben Blumaae8aab2010-03-10 15:22:07 -08001124/*
1125 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001126 * with cgroup_mutex held to protect the subsys[] array. This function takes
1127 * refcounts on subsystems to be used, unless it returns error, in which case
1128 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001129 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001130static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001132 char *token, *o = data;
1133 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001134 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001135 int i;
1136 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001137
Ben Blumaae8aab2010-03-10 15:22:07 -08001138 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1139
Li Zefanf9ab5b52009-06-17 16:26:33 -07001140#ifdef CONFIG_CPUSETS
1141 mask = ~(1UL << cpuset_subsys_id);
1142#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001143
Paul Menagec6d57f32009-09-23 15:56:19 -07001144 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001145
1146 while ((token = strsep(&o, ",")) != NULL) {
1147 if (!*token)
1148 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001149 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001150 /* Explicitly have no subsystems */
1151 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001152 continue;
1153 }
1154 if (!strcmp(token, "all")) {
1155 /* Mutually exclusive option 'all' + subsystem name */
1156 if (one_ss)
1157 return -EINVAL;
1158 all_ss = true;
1159 continue;
1160 }
Tejun Heo873fe092013-04-14 20:15:26 -07001161 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1162 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1163 continue;
1164 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001165 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001166 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001167 continue;
1168 }
1169 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001170 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001171 continue;
1172 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001173 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001174 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001175 continue;
1176 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001177 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001178 /* Specifying two release agents is forbidden */
1179 if (opts->release_agent)
1180 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001181 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001182 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001183 if (!opts->release_agent)
1184 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001185 continue;
1186 }
1187 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001188 const char *name = token + 5;
1189 /* Can't specify an empty name */
1190 if (!strlen(name))
1191 return -EINVAL;
1192 /* Must match [\w.-]+ */
1193 for (i = 0; i < strlen(name); i++) {
1194 char c = name[i];
1195 if (isalnum(c))
1196 continue;
1197 if ((c == '.') || (c == '-') || (c == '_'))
1198 continue;
1199 return -EINVAL;
1200 }
1201 /* Specifying two names is forbidden */
1202 if (opts->name)
1203 return -EINVAL;
1204 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001205 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001206 GFP_KERNEL);
1207 if (!opts->name)
1208 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001209
1210 continue;
1211 }
1212
1213 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1214 struct cgroup_subsys *ss = subsys[i];
1215 if (ss == NULL)
1216 continue;
1217 if (strcmp(token, ss->name))
1218 continue;
1219 if (ss->disabled)
1220 continue;
1221
1222 /* Mutually exclusive option 'all' + subsystem name */
1223 if (all_ss)
1224 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001225 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001226 one_ss = true;
1227
1228 break;
1229 }
1230 if (i == CGROUP_SUBSYS_COUNT)
1231 return -ENOENT;
1232 }
1233
1234 /*
1235 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001236 * otherwise if 'none', 'name=' and a subsystem name options
1237 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001238 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001239 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001240 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1241 struct cgroup_subsys *ss = subsys[i];
1242 if (ss == NULL)
1243 continue;
1244 if (ss->disabled)
1245 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001246 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001247 }
1248 }
1249
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001250 /* Consistency checks */
1251
Tejun Heo873fe092013-04-14 20:15:26 -07001252 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1253 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1254
1255 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1256 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1257 return -EINVAL;
1258 }
1259
1260 if (opts->cpuset_clone_children) {
1261 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1262 return -EINVAL;
1263 }
1264 }
1265
Li Zefanf9ab5b52009-06-17 16:26:33 -07001266 /*
1267 * Option noprefix was introduced just for backward compatibility
1268 * with the old cpuset, so we allow noprefix only if mounting just
1269 * the cpuset subsystem.
1270 */
Tejun Heo93438622013-04-14 20:15:25 -07001271 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001272 return -EINVAL;
1273
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001274
1275 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001276 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001277 return -EINVAL;
1278
1279 /*
1280 * We either have to specify by name or by subsystems. (So all
1281 * empty hierarchies must have a name).
1282 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001283 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001284 return -EINVAL;
1285
Ben Blumcf5d5942010-03-10 15:22:09 -08001286 /*
1287 * Grab references on all the modules we'll need, so the subsystems
1288 * don't dance around before rebind_subsystems attaches them. This may
1289 * take duplicate reference counts on a subsystem that's already used,
1290 * but rebind_subsystems handles this case.
1291 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001292 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001293 unsigned long bit = 1UL << i;
1294
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001295 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001296 continue;
1297 if (!try_module_get(subsys[i]->module)) {
1298 module_pin_failed = true;
1299 break;
1300 }
1301 }
1302 if (module_pin_failed) {
1303 /*
1304 * oops, one of the modules was going away. this means that we
1305 * raced with a module_delete call, and to the user this is
1306 * essentially a "subsystem doesn't exist" case.
1307 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001308 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001309 /* drop refcounts only on the ones we took */
1310 unsigned long bit = 1UL << i;
1311
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001312 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001313 continue;
1314 module_put(subsys[i]->module);
1315 }
1316 return -ENOENT;
1317 }
1318
Paul Menageddbcc7e2007-10-18 23:39:30 -07001319 return 0;
1320}
1321
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001322static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001323{
1324 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001325 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001326 unsigned long bit = 1UL << i;
1327
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001328 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001329 continue;
1330 module_put(subsys[i]->module);
1331 }
1332}
1333
Paul Menageddbcc7e2007-10-18 23:39:30 -07001334static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1335{
1336 int ret = 0;
1337 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001338 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001339 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001340 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001341
Tejun Heo873fe092013-04-14 20:15:26 -07001342 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1343 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1344 return -EINVAL;
1345 }
1346
Paul Menagebd89aab2007-10-18 23:40:44 -07001347 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001348 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001349 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001350
1351 /* See what subsystems are wanted */
1352 ret = parse_cgroupfs_options(data, &opts);
1353 if (ret)
1354 goto out_unlock;
1355
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001356 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001357 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1358 task_tgid_nr(current), current->comm);
1359
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001360 added_mask = opts.subsys_mask & ~root->subsys_mask;
1361 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001362
Ben Blumcf5d5942010-03-10 15:22:09 -08001363 /* Don't allow flags or name to change at remount */
1364 if (opts.flags != root->flags ||
1365 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001366 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001367 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001368 goto out_unlock;
1369 }
1370
Gao feng7083d032012-12-03 09:28:18 +08001371 /*
1372 * Clear out the files of subsystems that should be removed, do
1373 * this before rebind_subsystems, since rebind_subsystems may
1374 * change this hierarchy's subsys_list.
1375 */
1376 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1377
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001378 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001379 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001380 /* rebind_subsystems failed, re-populate the removed files */
1381 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001382 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001383 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001384 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001385
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001386 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001387 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001388
Paul Menage81a6a5c2007-10-18 23:39:38 -07001389 if (opts.release_agent)
1390 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001391 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001392 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001393 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001394 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001395 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001396 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001397 return ret;
1398}
1399
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001400static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401 .statfs = simple_statfs,
1402 .drop_inode = generic_delete_inode,
1403 .show_options = cgroup_show_options,
1404 .remount_fs = cgroup_remount,
1405};
1406
Paul Menagecc31edc2008-10-18 20:28:04 -07001407static void init_cgroup_housekeeping(struct cgroup *cgrp)
1408{
1409 INIT_LIST_HEAD(&cgrp->sibling);
1410 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001411 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001412 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001413 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001414 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001415 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001416 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001417 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001418 INIT_LIST_HEAD(&cgrp->event_list);
1419 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001420 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001421}
Paul Menagec6d57f32009-09-23 15:56:19 -07001422
Paul Menageddbcc7e2007-10-18 23:39:30 -07001423static void init_cgroup_root(struct cgroupfs_root *root)
1424{
Paul Menagebd89aab2007-10-18 23:40:44 -07001425 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001426
Paul Menageddbcc7e2007-10-18 23:39:30 -07001427 INIT_LIST_HEAD(&root->subsys_list);
1428 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001429 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001430 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001431 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001432 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001433 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001434 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001435}
1436
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001437static bool init_root_id(struct cgroupfs_root *root)
1438{
1439 int ret = 0;
1440
1441 do {
1442 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1443 return false;
1444 spin_lock(&hierarchy_id_lock);
1445 /* Try to allocate the next unused ID */
1446 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1447 &root->hierarchy_id);
1448 if (ret == -ENOSPC)
1449 /* Try again starting from 0 */
1450 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1451 if (!ret) {
1452 next_hierarchy_id = root->hierarchy_id + 1;
1453 } else if (ret != -EAGAIN) {
1454 /* Can only get here if the 31-bit IDR is full ... */
1455 BUG_ON(ret);
1456 }
1457 spin_unlock(&hierarchy_id_lock);
1458 } while (ret);
1459 return true;
1460}
1461
Paul Menageddbcc7e2007-10-18 23:39:30 -07001462static int cgroup_test_super(struct super_block *sb, void *data)
1463{
Paul Menagec6d57f32009-09-23 15:56:19 -07001464 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001465 struct cgroupfs_root *root = sb->s_fs_info;
1466
Paul Menagec6d57f32009-09-23 15:56:19 -07001467 /* If we asked for a name then it must match */
1468 if (opts->name && strcmp(opts->name, root->name))
1469 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001470
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001471 /*
1472 * If we asked for subsystems (or explicitly for no
1473 * subsystems) then they must match
1474 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001475 if ((opts->subsys_mask || opts->none)
1476 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001477 return 0;
1478
1479 return 1;
1480}
1481
Paul Menagec6d57f32009-09-23 15:56:19 -07001482static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1483{
1484 struct cgroupfs_root *root;
1485
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001486 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001487 return NULL;
1488
1489 root = kzalloc(sizeof(*root), GFP_KERNEL);
1490 if (!root)
1491 return ERR_PTR(-ENOMEM);
1492
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001493 if (!init_root_id(root)) {
1494 kfree(root);
1495 return ERR_PTR(-ENOMEM);
1496 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001497 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001498
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001499 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001500 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001501 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001502 if (opts->release_agent)
1503 strcpy(root->release_agent_path, opts->release_agent);
1504 if (opts->name)
1505 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001506 if (opts->cpuset_clone_children)
1507 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001508 return root;
1509}
1510
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001511static void cgroup_drop_root(struct cgroupfs_root *root)
1512{
1513 if (!root)
1514 return;
1515
1516 BUG_ON(!root->hierarchy_id);
1517 spin_lock(&hierarchy_id_lock);
1518 ida_remove(&hierarchy_ida, root->hierarchy_id);
1519 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001520 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001521 kfree(root);
1522}
1523
Paul Menageddbcc7e2007-10-18 23:39:30 -07001524static int cgroup_set_super(struct super_block *sb, void *data)
1525{
1526 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001527 struct cgroup_sb_opts *opts = data;
1528
1529 /* If we don't have a new root, we can't set up a new sb */
1530 if (!opts->new_root)
1531 return -EINVAL;
1532
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001533 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001534
1535 ret = set_anon_super(sb, NULL);
1536 if (ret)
1537 return ret;
1538
Paul Menagec6d57f32009-09-23 15:56:19 -07001539 sb->s_fs_info = opts->new_root;
1540 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541
1542 sb->s_blocksize = PAGE_CACHE_SIZE;
1543 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1544 sb->s_magic = CGROUP_SUPER_MAGIC;
1545 sb->s_op = &cgroup_ops;
1546
1547 return 0;
1548}
1549
1550static int cgroup_get_rootdir(struct super_block *sb)
1551{
Al Viro0df6a632010-12-21 13:29:29 -05001552 static const struct dentry_operations cgroup_dops = {
1553 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001554 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001555 };
1556
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557 struct inode *inode =
1558 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559
1560 if (!inode)
1561 return -ENOMEM;
1562
Paul Menageddbcc7e2007-10-18 23:39:30 -07001563 inode->i_fop = &simple_dir_operations;
1564 inode->i_op = &cgroup_dir_inode_operations;
1565 /* directories start off with i_nlink == 2 (for "." entry) */
1566 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001567 sb->s_root = d_make_root(inode);
1568 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001570 /* for everything else we want ->d_op set */
1571 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001572 return 0;
1573}
1574
Al Virof7e83572010-07-26 13:23:11 +04001575static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001577 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001578{
1579 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001580 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001581 int ret = 0;
1582 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001583 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001584 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001585
1586 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001587 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001589 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001590 if (ret)
1591 goto out_err;
1592
1593 /*
1594 * Allocate a new cgroup root. We may not need it if we're
1595 * reusing an existing hierarchy.
1596 */
1597 new_root = cgroup_root_from_opts(&opts);
1598 if (IS_ERR(new_root)) {
1599 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001600 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001601 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001602 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001603
Paul Menagec6d57f32009-09-23 15:56:19 -07001604 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001605 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001606 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001607 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001608 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001609 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610 }
1611
Paul Menagec6d57f32009-09-23 15:56:19 -07001612 root = sb->s_fs_info;
1613 BUG_ON(!root);
1614 if (root == opts.new_root) {
1615 /* We used the new root structure, so this is a new hierarchy */
1616 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001617 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001618 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001619 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001620 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001621 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001622
1623 BUG_ON(sb->s_root != NULL);
1624
1625 ret = cgroup_get_rootdir(sb);
1626 if (ret)
1627 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001628 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001629
Paul Menage817929e2007-10-18 23:39:36 -07001630 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001632 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001633
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001634 /* Check for name clashes with existing mounts */
1635 ret = -EBUSY;
1636 if (strlen(root->name))
1637 for_each_active_root(existing_root)
1638 if (!strcmp(existing_root->name, root->name))
1639 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001640
Paul Menage817929e2007-10-18 23:39:36 -07001641 /*
1642 * We're accessing css_set_count without locking
1643 * css_set_lock here, but that's OK - it can only be
1644 * increased by someone holding cgroup_lock, and
1645 * that's us. The worst that can happen is that we
1646 * have some link structures left over
1647 */
1648 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001649 if (ret)
1650 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001651
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001652 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001653 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001654 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001655 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001656 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001657 /*
1658 * There must be no failure case after here, since rebinding
1659 * takes care of subsystems' refcounts, which are explicitly
1660 * dropped in the failure exit path.
1661 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662
1663 /* EBUSY should be the only error here */
1664 BUG_ON(ret);
1665
1666 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001667 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001668
Li Zefanc12f65d2009-01-07 18:07:42 -08001669 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001670 root->top_cgroup.dentry = sb->s_root;
1671
Paul Menage817929e2007-10-18 23:39:36 -07001672 /* Link the top cgroup in this hierarchy into all
1673 * the css_set objects */
1674 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001675 hash_for_each(css_set_table, i, cg, hlist)
Li Zefan0ac801f2013-01-10 11:49:27 +08001676 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001677 write_unlock(&css_set_lock);
1678
1679 free_cg_links(&tmp_cg_links);
1680
Li Zefanc12f65d2009-01-07 18:07:42 -08001681 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682 BUG_ON(root->number_of_cgroups != 1);
1683
eparis@redhat2ce97382011-06-02 21:20:51 +10001684 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001685 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001686 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001687 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001688 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001689 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001690 } else {
1691 /*
1692 * We re-used an existing hierarchy - the new root (if
1693 * any) is not needed
1694 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001695 cgroup_drop_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001696
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001697 if (root->flags != opts.flags) {
1698 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1699 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1700 ret = -EINVAL;
1701 goto drop_new_super;
1702 } else {
1703 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1704 }
Tejun Heo873fe092013-04-14 20:15:26 -07001705 }
1706
Ben Blumcf5d5942010-03-10 15:22:09 -08001707 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001708 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001709 }
1710
Paul Menagec6d57f32009-09-23 15:56:19 -07001711 kfree(opts.release_agent);
1712 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001713 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001714
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001715 unlock_drop:
1716 mutex_unlock(&cgroup_root_mutex);
1717 mutex_unlock(&cgroup_mutex);
1718 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001720 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001721 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001722 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001723 out_err:
1724 kfree(opts.release_agent);
1725 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001726 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727}
1728
1729static void cgroup_kill_sb(struct super_block *sb) {
1730 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001731 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001733 struct cg_cgroup_link *link;
1734 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001735
1736 BUG_ON(!root);
1737
1738 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001739 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740
1741 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001742 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001743
1744 /* Rebind all subsystems back to the default hierarchy */
1745 ret = rebind_subsystems(root, 0);
1746 /* Shouldn't be able to fail ... */
1747 BUG_ON(ret);
1748
Paul Menage817929e2007-10-18 23:39:36 -07001749 /*
1750 * Release all the links from css_sets to this hierarchy's
1751 * root cgroup
1752 */
1753 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001754
1755 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1756 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001757 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001758 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001759 kfree(link);
1760 }
1761 write_unlock(&css_set_lock);
1762
Paul Menage839ec542009-01-29 14:25:22 -08001763 if (!list_empty(&root->root_list)) {
1764 list_del(&root->root_list);
1765 root_count--;
1766 }
Li Zefane5f6a862009-01-07 18:07:41 -08001767
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001768 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769 mutex_unlock(&cgroup_mutex);
1770
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001771 simple_xattrs_free(&cgrp->xattrs);
1772
Paul Menageddbcc7e2007-10-18 23:39:30 -07001773 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001774 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001775}
1776
1777static struct file_system_type cgroup_fs_type = {
1778 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001779 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001780 .kill_sb = cgroup_kill_sb,
1781};
1782
Greg KH676db4a2010-08-05 13:53:35 -07001783static struct kobject *cgroup_kobj;
1784
Li Zefana043e3b2008-02-23 15:24:09 -08001785/**
1786 * cgroup_path - generate the path of a cgroup
1787 * @cgrp: the cgroup in question
1788 * @buf: the buffer to write the path into
1789 * @buflen: the length of the buffer
1790 *
Li Zefan65dff752013-03-01 15:01:56 +08001791 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1792 *
1793 * We can't generate cgroup path using dentry->d_name, as accessing
1794 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1795 * inode's i_mutex, while on the other hand cgroup_path() can be called
1796 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001798int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001799{
Li Zefan65dff752013-03-01 15:01:56 +08001800 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001801 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001802
Tejun Heoda1f2962013-04-14 10:32:19 -07001803 if (!cgrp->parent) {
1804 if (strlcpy(buf, "/", buflen) >= buflen)
1805 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001806 return 0;
1807 }
1808
Tao Ma316eb662012-11-08 21:36:38 +08001809 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001810 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001811
Li Zefan65dff752013-03-01 15:01:56 +08001812 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001813 do {
Li Zefan65dff752013-03-01 15:01:56 +08001814 const char *name = cgroup_name(cgrp);
1815 int len;
1816
1817 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001818 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001819 goto out;
1820 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001821
Paul Menageddbcc7e2007-10-18 23:39:30 -07001822 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001823 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001824 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001825
1826 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001827 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001828 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001829 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001830out:
1831 rcu_read_unlock();
1832 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001833}
Ben Blum67523c42010-03-10 15:22:11 -08001834EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001835
Ben Blum74a11662011-05-26 16:25:20 -07001836/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001837 * Control Group taskset
1838 */
Tejun Heo134d3372011-12-12 18:12:21 -08001839struct task_and_cgroup {
1840 struct task_struct *task;
1841 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001842 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001843};
1844
Tejun Heo2f7ee562011-12-12 18:12:21 -08001845struct cgroup_taskset {
1846 struct task_and_cgroup single;
1847 struct flex_array *tc_array;
1848 int tc_array_len;
1849 int idx;
1850 struct cgroup *cur_cgrp;
1851};
1852
1853/**
1854 * cgroup_taskset_first - reset taskset and return the first task
1855 * @tset: taskset of interest
1856 *
1857 * @tset iteration is initialized and the first task is returned.
1858 */
1859struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1860{
1861 if (tset->tc_array) {
1862 tset->idx = 0;
1863 return cgroup_taskset_next(tset);
1864 } else {
1865 tset->cur_cgrp = tset->single.cgrp;
1866 return tset->single.task;
1867 }
1868}
1869EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1870
1871/**
1872 * cgroup_taskset_next - iterate to the next task in taskset
1873 * @tset: taskset of interest
1874 *
1875 * Return the next task in @tset. Iteration must have been initialized
1876 * with cgroup_taskset_first().
1877 */
1878struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1879{
1880 struct task_and_cgroup *tc;
1881
1882 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1883 return NULL;
1884
1885 tc = flex_array_get(tset->tc_array, tset->idx++);
1886 tset->cur_cgrp = tc->cgrp;
1887 return tc->task;
1888}
1889EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1890
1891/**
1892 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1893 * @tset: taskset of interest
1894 *
1895 * Return the cgroup for the current (last returned) task of @tset. This
1896 * function must be preceded by either cgroup_taskset_first() or
1897 * cgroup_taskset_next().
1898 */
1899struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1900{
1901 return tset->cur_cgrp;
1902}
1903EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1904
1905/**
1906 * cgroup_taskset_size - return the number of tasks in taskset
1907 * @tset: taskset of interest
1908 */
1909int cgroup_taskset_size(struct cgroup_taskset *tset)
1910{
1911 return tset->tc_array ? tset->tc_array_len : 1;
1912}
1913EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1914
1915
Ben Blum74a11662011-05-26 16:25:20 -07001916/*
1917 * cgroup_task_migrate - move a task from one cgroup to another.
1918 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001919 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001920 */
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03001921static void cgroup_task_migrate(struct cgroup *oldcgrp,
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001922 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001923{
1924 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001925
1926 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001927 * We are synchronized through threadgroup_lock() against PF_EXITING
1928 * setting such that we can't race against cgroup_exit() changing the
1929 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001930 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001931 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001932 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001933
Ben Blum74a11662011-05-26 16:25:20 -07001934 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001935 rcu_assign_pointer(tsk->cgroups, newcg);
1936 task_unlock(tsk);
1937
1938 /* Update the css_set linked lists if we're using them */
1939 write_lock(&css_set_lock);
1940 if (!list_empty(&tsk->cg_list))
1941 list_move(&tsk->cg_list, &newcg->tasks);
1942 write_unlock(&css_set_lock);
1943
1944 /*
1945 * We just gained a reference on oldcg by taking it from the task. As
1946 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1947 * it here; it will be freed under RCU.
1948 */
Ben Blum74a11662011-05-26 16:25:20 -07001949 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001950 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001951}
1952
Li Zefana043e3b2008-02-23 15:24:09 -08001953/**
Li Zefan081aa452013-03-13 09:17:09 +08001954 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001955 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001956 * @tsk: the task or the leader of the threadgroup to be attached
1957 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001958 *
Tejun Heo257058a2011-12-12 18:12:21 -08001959 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001960 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001961 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001962static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1963 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001964{
1965 int retval, i, group_size;
1966 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001967 struct cgroupfs_root *root = cgrp->root;
1968 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001969 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001970 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001971 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001972 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001973
1974 /*
1975 * step 0: in order to do expensive, possibly blocking operations for
1976 * every thread, we cannot iterate the thread group list, since it needs
1977 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001978 * group - group_rwsem prevents new threads from appearing, and if
1979 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001980 */
Li Zefan081aa452013-03-13 09:17:09 +08001981 if (threadgroup)
1982 group_size = get_nr_threads(tsk);
1983 else
1984 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001985 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001986 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001987 if (!group)
1988 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001989 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001990 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001991 if (retval)
1992 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001993
Ben Blum74a11662011-05-26 16:25:20 -07001994 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001995 /*
1996 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1997 * already PF_EXITING could be freed from underneath us unless we
1998 * take an rcu_read_lock.
1999 */
2000 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002001 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002002 struct task_and_cgroup ent;
2003
Tejun Heocd3d0952011-12-12 18:12:21 -08002004 /* @tsk either already exited or can't exit until the end */
2005 if (tsk->flags & PF_EXITING)
Anjana V Kumar5be794d2013-10-12 10:59:17 +08002006 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002007
Ben Blum74a11662011-05-26 16:25:20 -07002008 /* as per above, nr_threads may decrease, but not increase. */
2009 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002010 ent.task = tsk;
2011 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002012 /* nothing to do if this task is already in the cgroup */
2013 if (ent.cgrp == cgrp)
Anjana V Kumar5be794d2013-10-12 10:59:17 +08002014 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002015 /*
2016 * saying GFP_ATOMIC has no effect here because we did prealloc
2017 * earlier, but it's good form to communicate our expectations.
2018 */
Tejun Heo134d3372011-12-12 18:12:21 -08002019 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002020 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002021 i++;
Anjana V Kumar5be794d2013-10-12 10:59:17 +08002022 next:
Li Zefan081aa452013-03-13 09:17:09 +08002023 if (!threadgroup)
2024 break;
Ben Blum74a11662011-05-26 16:25:20 -07002025 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002026 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002027 /* remember the number of threads in the array for later. */
2028 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002029 tset.tc_array = group;
2030 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002031
Tejun Heo134d3372011-12-12 18:12:21 -08002032 /* methods shouldn't be called if no task is actually migrating */
2033 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002034 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002035 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002036
Ben Blum74a11662011-05-26 16:25:20 -07002037 /*
2038 * step 1: check that we can legitimately attach to the cgroup.
2039 */
2040 for_each_subsys(root, ss) {
2041 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002042 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002043 if (retval) {
2044 failed_ss = ss;
2045 goto out_cancel_attach;
2046 }
2047 }
Ben Blum74a11662011-05-26 16:25:20 -07002048 }
2049
2050 /*
2051 * step 2: make sure css_sets exist for all threads to be migrated.
2052 * we use find_css_set, which allocates a new one if necessary.
2053 */
Ben Blum74a11662011-05-26 16:25:20 -07002054 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002055 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002056 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2057 if (!tc->cg) {
2058 retval = -ENOMEM;
2059 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002060 }
2061 }
2062
2063 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002064 * step 3: now that we're guaranteed success wrt the css_sets,
2065 * proceed to move all tasks to the new cgroup. There are no
2066 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002067 */
Ben Blum74a11662011-05-26 16:25:20 -07002068 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002069 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002070 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002071 }
2072 /* nothing is sensitive to fork() after this point. */
2073
2074 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002075 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002076 */
2077 for_each_subsys(root, ss) {
2078 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002079 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002080 }
2081
2082 /*
2083 * step 5: success! and cleanup
2084 */
Ben Blum74a11662011-05-26 16:25:20 -07002085 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002086out_put_css_set_refs:
2087 if (retval) {
2088 for (i = 0; i < group_size; i++) {
2089 tc = flex_array_get(group, i);
2090 if (!tc->cg)
2091 break;
2092 put_css_set(tc->cg);
2093 }
Ben Blum74a11662011-05-26 16:25:20 -07002094 }
2095out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002096 if (retval) {
2097 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002098 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002099 break;
Ben Blum74a11662011-05-26 16:25:20 -07002100 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002101 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002102 }
2103 }
Ben Blum74a11662011-05-26 16:25:20 -07002104out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002105 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002106 return retval;
2107}
2108
2109/*
2110 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002111 * function to attach either it or all tasks in its threadgroup. Will lock
2112 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002113 */
2114static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002115{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002116 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002117 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002118 int ret;
2119
Ben Blum74a11662011-05-26 16:25:20 -07002120 if (!cgroup_lock_live_group(cgrp))
2121 return -ENODEV;
2122
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002123retry_find_task:
2124 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002125 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002126 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002127 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002128 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002129 ret= -ESRCH;
2130 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002131 }
Ben Blum74a11662011-05-26 16:25:20 -07002132 /*
2133 * even if we're attaching all tasks in the thread group, we
2134 * only need to check permissions on one of them.
2135 */
David Howellsc69e8d92008-11-14 10:39:19 +11002136 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002137 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2138 !uid_eq(cred->euid, tcred->uid) &&
2139 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002140 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002141 ret = -EACCES;
2142 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002143 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002144 } else
2145 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002146
2147 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002148 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002149
2150 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002151 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002152 * trapped in a cpuset, or RT worker may be born in a cgroup
2153 * with no rt_runtime allocated. Just say no.
2154 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002155 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002156 ret = -EINVAL;
2157 rcu_read_unlock();
2158 goto out_unlock_cgroup;
2159 }
2160
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002161 get_task_struct(tsk);
2162 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002163
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002164 threadgroup_lock(tsk);
2165 if (threadgroup) {
2166 if (!thread_group_leader(tsk)) {
2167 /*
2168 * a race with de_thread from another thread's exec()
2169 * may strip us of our leadership, if this happens,
2170 * there is no choice but to throw this task away and
2171 * try again; this is
2172 * "double-double-toil-and-trouble-check locking".
2173 */
2174 threadgroup_unlock(tsk);
2175 put_task_struct(tsk);
2176 goto retry_find_task;
2177 }
Li Zefan081aa452013-03-13 09:17:09 +08002178 }
2179
2180 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2181
Tejun Heocd3d0952011-12-12 18:12:21 -08002182 threadgroup_unlock(tsk);
2183
Paul Menagebbcb81d2007-10-18 23:39:32 -07002184 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002185out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002186 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002187 return ret;
2188}
2189
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002190/**
2191 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2192 * @from: attach to all cgroups of a given task
2193 * @tsk: the task to be attached
2194 */
2195int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2196{
2197 struct cgroupfs_root *root;
2198 int retval = 0;
2199
Tejun Heo47cfcd02013-04-07 09:29:51 -07002200 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002201 for_each_active_root(root) {
2202 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2203
2204 retval = cgroup_attach_task(from_cg, tsk, false);
2205 if (retval)
2206 break;
2207 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002208 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002209
2210 return retval;
2211}
2212EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2213
Paul Menageaf351022008-07-25 01:47:01 -07002214static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2215{
Ben Blum74a11662011-05-26 16:25:20 -07002216 return attach_task_by_pid(cgrp, pid, false);
2217}
2218
2219static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2220{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002221 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002222}
2223
Paul Menagee788e062008-07-25 01:46:59 -07002224static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2225 const char *buffer)
2226{
2227 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002228 if (strlen(buffer) >= PATH_MAX)
2229 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002230 if (!cgroup_lock_live_group(cgrp))
2231 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002232 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002233 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002234 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002235 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002236 return 0;
2237}
2238
2239static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2240 struct seq_file *seq)
2241{
2242 if (!cgroup_lock_live_group(cgrp))
2243 return -ENODEV;
2244 seq_puts(seq, cgrp->root->release_agent_path);
2245 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002246 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002247 return 0;
2248}
2249
Tejun Heo873fe092013-04-14 20:15:26 -07002250static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2251 struct seq_file *seq)
2252{
2253 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002254 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002255}
2256
Paul Menage84eea842008-07-25 01:47:00 -07002257/* A buffer size big enough for numbers or short strings */
2258#define CGROUP_LOCAL_BUFFER_SIZE 64
2259
Paul Menagee73d2c62008-04-29 01:00:06 -07002260static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002261 struct file *file,
2262 const char __user *userbuf,
2263 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002264{
Paul Menage84eea842008-07-25 01:47:00 -07002265 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002266 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002267 char *end;
2268
2269 if (!nbytes)
2270 return -EINVAL;
2271 if (nbytes >= sizeof(buffer))
2272 return -E2BIG;
2273 if (copy_from_user(buffer, userbuf, nbytes))
2274 return -EFAULT;
2275
2276 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002277 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002278 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002279 if (*end)
2280 return -EINVAL;
2281 retval = cft->write_u64(cgrp, cft, val);
2282 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002283 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002284 if (*end)
2285 return -EINVAL;
2286 retval = cft->write_s64(cgrp, cft, val);
2287 }
Paul Menage355e0c42007-10-18 23:39:33 -07002288 if (!retval)
2289 retval = nbytes;
2290 return retval;
2291}
2292
Paul Menagedb3b1492008-07-25 01:46:58 -07002293static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2294 struct file *file,
2295 const char __user *userbuf,
2296 size_t nbytes, loff_t *unused_ppos)
2297{
Paul Menage84eea842008-07-25 01:47:00 -07002298 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002299 int retval = 0;
2300 size_t max_bytes = cft->max_write_len;
2301 char *buffer = local_buffer;
2302
2303 if (!max_bytes)
2304 max_bytes = sizeof(local_buffer) - 1;
2305 if (nbytes >= max_bytes)
2306 return -E2BIG;
2307 /* Allocate a dynamic buffer if we need one */
2308 if (nbytes >= sizeof(local_buffer)) {
2309 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2310 if (buffer == NULL)
2311 return -ENOMEM;
2312 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002313 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2314 retval = -EFAULT;
2315 goto out;
2316 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002317
2318 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002319 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002320 if (!retval)
2321 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002322out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002323 if (buffer != local_buffer)
2324 kfree(buffer);
2325 return retval;
2326}
2327
Paul Menageddbcc7e2007-10-18 23:39:30 -07002328static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2329 size_t nbytes, loff_t *ppos)
2330{
2331 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002332 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002333
Li Zefan75139b82009-01-07 18:07:33 -08002334 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002335 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002336 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002337 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002338 if (cft->write_u64 || cft->write_s64)
2339 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002340 if (cft->write_string)
2341 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002342 if (cft->trigger) {
2343 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2344 return ret ? ret : nbytes;
2345 }
Paul Menage355e0c42007-10-18 23:39:33 -07002346 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002347}
2348
Paul Menagef4c753b2008-04-29 00:59:56 -07002349static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2350 struct file *file,
2351 char __user *buf, size_t nbytes,
2352 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002353{
Paul Menage84eea842008-07-25 01:47:00 -07002354 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002355 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002356 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2357
2358 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2359}
2360
Paul Menagee73d2c62008-04-29 01:00:06 -07002361static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2362 struct file *file,
2363 char __user *buf, size_t nbytes,
2364 loff_t *ppos)
2365{
Paul Menage84eea842008-07-25 01:47:00 -07002366 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002367 s64 val = cft->read_s64(cgrp, cft);
2368 int len = sprintf(tmp, "%lld\n", (long long) val);
2369
2370 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2371}
2372
Paul Menageddbcc7e2007-10-18 23:39:30 -07002373static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2374 size_t nbytes, loff_t *ppos)
2375{
2376 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002377 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002378
Li Zefan75139b82009-01-07 18:07:33 -08002379 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002380 return -ENODEV;
2381
2382 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002383 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002384 if (cft->read_u64)
2385 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002386 if (cft->read_s64)
2387 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002388 return -EINVAL;
2389}
2390
Paul Menage91796562008-04-29 01:00:01 -07002391/*
2392 * seqfile ops/methods for returning structured data. Currently just
2393 * supports string->u64 maps, but can be extended in future.
2394 */
2395
2396struct cgroup_seqfile_state {
2397 struct cftype *cft;
2398 struct cgroup *cgroup;
2399};
2400
2401static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2402{
2403 struct seq_file *sf = cb->state;
2404 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2405}
2406
2407static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2408{
2409 struct cgroup_seqfile_state *state = m->private;
2410 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002411 if (cft->read_map) {
2412 struct cgroup_map_cb cb = {
2413 .fill = cgroup_map_add,
2414 .state = m,
2415 };
2416 return cft->read_map(state->cgroup, cft, &cb);
2417 }
2418 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002419}
2420
Adrian Bunk96930a62008-07-25 19:46:21 -07002421static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002422{
2423 struct seq_file *seq = file->private_data;
2424 kfree(seq->private);
2425 return single_release(inode, file);
2426}
2427
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002428static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002429 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002430 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002431 .llseek = seq_lseek,
2432 .release = cgroup_seqfile_release,
2433};
2434
Paul Menageddbcc7e2007-10-18 23:39:30 -07002435static int cgroup_file_open(struct inode *inode, struct file *file)
2436{
2437 int err;
2438 struct cftype *cft;
2439
2440 err = generic_file_open(inode, file);
2441 if (err)
2442 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002443 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002444
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002445 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002446 struct cgroup_seqfile_state *state =
2447 kzalloc(sizeof(*state), GFP_USER);
2448 if (!state)
2449 return -ENOMEM;
2450 state->cft = cft;
2451 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2452 file->f_op = &cgroup_seqfile_operations;
2453 err = single_open(file, cgroup_seqfile_show, state);
2454 if (err < 0)
2455 kfree(state);
2456 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002457 err = cft->open(inode, file);
2458 else
2459 err = 0;
2460
2461 return err;
2462}
2463
2464static int cgroup_file_release(struct inode *inode, struct file *file)
2465{
2466 struct cftype *cft = __d_cft(file->f_dentry);
2467 if (cft->release)
2468 return cft->release(inode, file);
2469 return 0;
2470}
2471
2472/*
2473 * cgroup_rename - Only allow simple rename of directories in place.
2474 */
2475static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2476 struct inode *new_dir, struct dentry *new_dentry)
2477{
Li Zefan65dff752013-03-01 15:01:56 +08002478 int ret;
2479 struct cgroup_name *name, *old_name;
2480 struct cgroup *cgrp;
2481
2482 /*
2483 * It's convinient to use parent dir's i_mutex to protected
2484 * cgrp->name.
2485 */
2486 lockdep_assert_held(&old_dir->i_mutex);
2487
Paul Menageddbcc7e2007-10-18 23:39:30 -07002488 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2489 return -ENOTDIR;
2490 if (new_dentry->d_inode)
2491 return -EEXIST;
2492 if (old_dir != new_dir)
2493 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002494
2495 cgrp = __d_cgrp(old_dentry);
2496
2497 name = cgroup_alloc_name(new_dentry);
2498 if (!name)
2499 return -ENOMEM;
2500
2501 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2502 if (ret) {
2503 kfree(name);
2504 return ret;
2505 }
2506
2507 old_name = cgrp->name;
2508 rcu_assign_pointer(cgrp->name, name);
2509
2510 kfree_rcu(old_name, rcu_head);
2511 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002512}
2513
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002514static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2515{
2516 if (S_ISDIR(dentry->d_inode->i_mode))
2517 return &__d_cgrp(dentry)->xattrs;
2518 else
Li Zefan712317a2013-04-18 23:09:52 -07002519 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002520}
2521
2522static inline int xattr_enabled(struct dentry *dentry)
2523{
2524 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002525 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002526}
2527
2528static bool is_valid_xattr(const char *name)
2529{
2530 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2531 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2532 return true;
2533 return false;
2534}
2535
2536static int cgroup_setxattr(struct dentry *dentry, const char *name,
2537 const void *val, size_t size, int flags)
2538{
2539 if (!xattr_enabled(dentry))
2540 return -EOPNOTSUPP;
2541 if (!is_valid_xattr(name))
2542 return -EINVAL;
2543 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2544}
2545
2546static int cgroup_removexattr(struct dentry *dentry, const char *name)
2547{
2548 if (!xattr_enabled(dentry))
2549 return -EOPNOTSUPP;
2550 if (!is_valid_xattr(name))
2551 return -EINVAL;
2552 return simple_xattr_remove(__d_xattrs(dentry), name);
2553}
2554
2555static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2556 void *buf, size_t size)
2557{
2558 if (!xattr_enabled(dentry))
2559 return -EOPNOTSUPP;
2560 if (!is_valid_xattr(name))
2561 return -EINVAL;
2562 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2563}
2564
2565static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2566{
2567 if (!xattr_enabled(dentry))
2568 return -EOPNOTSUPP;
2569 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2570}
2571
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002572static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002573 .read = cgroup_file_read,
2574 .write = cgroup_file_write,
2575 .llseek = generic_file_llseek,
2576 .open = cgroup_file_open,
2577 .release = cgroup_file_release,
2578};
2579
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002580static const struct inode_operations cgroup_file_inode_operations = {
2581 .setxattr = cgroup_setxattr,
2582 .getxattr = cgroup_getxattr,
2583 .listxattr = cgroup_listxattr,
2584 .removexattr = cgroup_removexattr,
2585};
2586
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002587static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002588 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002589 .mkdir = cgroup_mkdir,
2590 .rmdir = cgroup_rmdir,
2591 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002592 .setxattr = cgroup_setxattr,
2593 .getxattr = cgroup_getxattr,
2594 .listxattr = cgroup_listxattr,
2595 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002596};
2597
Al Viro00cd8dd2012-06-10 17:13:09 -04002598static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002599{
2600 if (dentry->d_name.len > NAME_MAX)
2601 return ERR_PTR(-ENAMETOOLONG);
2602 d_add(dentry, NULL);
2603 return NULL;
2604}
2605
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002606/*
2607 * Check if a file is a control file
2608 */
2609static inline struct cftype *__file_cft(struct file *file)
2610{
Al Viro496ad9a2013-01-23 17:07:38 -05002611 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002612 return ERR_PTR(-EINVAL);
2613 return __d_cft(file->f_dentry);
2614}
2615
Al Viroa5e7ed32011-07-26 01:55:55 -04002616static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002617 struct super_block *sb)
2618{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002619 struct inode *inode;
2620
2621 if (!dentry)
2622 return -ENOENT;
2623 if (dentry->d_inode)
2624 return -EEXIST;
2625
2626 inode = cgroup_new_inode(mode, sb);
2627 if (!inode)
2628 return -ENOMEM;
2629
2630 if (S_ISDIR(mode)) {
2631 inode->i_op = &cgroup_dir_inode_operations;
2632 inode->i_fop = &simple_dir_operations;
2633
2634 /* start off with i_nlink == 2 (for "." entry) */
2635 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002636 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002637
Tejun Heob8a2df62012-11-19 08:13:37 -08002638 /*
2639 * Control reaches here with cgroup_mutex held.
2640 * @inode->i_mutex should nest outside cgroup_mutex but we
2641 * want to populate it immediately without releasing
2642 * cgroup_mutex. As @inode isn't visible to anyone else
2643 * yet, trylock will always succeed without affecting
2644 * lockdep checks.
2645 */
2646 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002647 } else if (S_ISREG(mode)) {
2648 inode->i_size = 0;
2649 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002650 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002651 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002652 d_instantiate(dentry, inode);
2653 dget(dentry); /* Extra count - pin the dentry in core */
2654 return 0;
2655}
2656
Li Zefan099fca32009-04-02 16:57:29 -07002657/**
2658 * cgroup_file_mode - deduce file mode of a control file
2659 * @cft: the control file in question
2660 *
2661 * returns cft->mode if ->mode is not 0
2662 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2663 * returns S_IRUGO if it has only a read handler
2664 * returns S_IWUSR if it has only a write hander
2665 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002666static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002667{
Al Viroa5e7ed32011-07-26 01:55:55 -04002668 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002669
2670 if (cft->mode)
2671 return cft->mode;
2672
2673 if (cft->read || cft->read_u64 || cft->read_s64 ||
2674 cft->read_map || cft->read_seq_string)
2675 mode |= S_IRUGO;
2676
2677 if (cft->write || cft->write_u64 || cft->write_s64 ||
2678 cft->write_string || cft->trigger)
2679 mode |= S_IWUSR;
2680
2681 return mode;
2682}
2683
Tejun Heodb0416b2012-04-01 12:09:55 -07002684static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002685 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002686{
Paul Menagebd89aab2007-10-18 23:40:44 -07002687 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002688 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002689 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002690 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002691 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002692 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002693 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002694
Tejun Heo93438622013-04-14 20:15:25 -07002695 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002696 strcpy(name, subsys->name);
2697 strcat(name, ".");
2698 }
2699 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002700
Paul Menageddbcc7e2007-10-18 23:39:30 -07002701 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002702
2703 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2704 if (!cfe)
2705 return -ENOMEM;
2706
Paul Menageddbcc7e2007-10-18 23:39:30 -07002707 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002708 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002709 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002710 goto out;
2711 }
2712
Li Zefand6cbf352013-05-14 19:44:20 +08002713 cfe->type = (void *)cft;
2714 cfe->dentry = dentry;
2715 dentry->d_fsdata = cfe;
2716 simple_xattrs_init(&cfe->xattrs);
2717
Tejun Heo05ef1d72012-04-01 12:09:56 -07002718 mode = cgroup_file_mode(cft);
2719 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2720 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002721 list_add_tail(&cfe->node, &parent->files);
2722 cfe = NULL;
2723 }
2724 dput(dentry);
2725out:
2726 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002727 return error;
2728}
2729
Tejun Heo79578622012-04-01 12:09:56 -07002730static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002731 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002732{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002733 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002734 int err, ret = 0;
2735
2736 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002737 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002738 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2739 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002740 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2741 continue;
2742 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2743 continue;
2744
Li Zefan2739d3c2013-01-21 18:18:33 +08002745 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002746 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002747 if (err)
2748 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2749 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002750 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002751 } else {
2752 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002753 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002754 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002755 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002756}
2757
Tejun Heo8e3f6542012-04-01 12:09:55 -07002758static DEFINE_MUTEX(cgroup_cft_mutex);
2759
2760static void cgroup_cfts_prepare(void)
2761 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2762{
2763 /*
2764 * Thanks to the entanglement with vfs inode locking, we can't walk
2765 * the existing cgroups under cgroup_mutex and create files.
2766 * Instead, we increment reference on all cgroups and build list of
2767 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2768 * exclusive access to the field.
2769 */
2770 mutex_lock(&cgroup_cft_mutex);
2771 mutex_lock(&cgroup_mutex);
2772}
2773
2774static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002775 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002776 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2777{
2778 LIST_HEAD(pending);
2779 struct cgroup *cgrp, *n;
Li Zefana09a1b82013-06-18 18:40:19 +08002780 struct super_block *sb = ss->root->sb;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002781
2782 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Li Zefana09a1b82013-06-18 18:40:19 +08002783 if (cfts && ss->root != &rootnode &&
2784 atomic_inc_not_zero(&sb->s_active)) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07002785 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2786 dget(cgrp->dentry);
2787 list_add_tail(&cgrp->cft_q_node, &pending);
2788 }
Li Zefana09a1b82013-06-18 18:40:19 +08002789 } else {
2790 sb = NULL;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002791 }
2792
2793 mutex_unlock(&cgroup_mutex);
2794
2795 /*
2796 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2797 * files for all cgroups which were created before.
2798 */
2799 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2800 struct inode *inode = cgrp->dentry->d_inode;
2801
2802 mutex_lock(&inode->i_mutex);
2803 mutex_lock(&cgroup_mutex);
2804 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002805 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002806 mutex_unlock(&cgroup_mutex);
2807 mutex_unlock(&inode->i_mutex);
2808
2809 list_del_init(&cgrp->cft_q_node);
2810 dput(cgrp->dentry);
2811 }
2812
Li Zefana09a1b82013-06-18 18:40:19 +08002813 if (sb)
2814 deactivate_super(sb);
2815
Tejun Heo8e3f6542012-04-01 12:09:55 -07002816 mutex_unlock(&cgroup_cft_mutex);
2817}
2818
2819/**
2820 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2821 * @ss: target cgroup subsystem
2822 * @cfts: zero-length name terminated array of cftypes
2823 *
2824 * Register @cfts to @ss. Files described by @cfts are created for all
2825 * existing cgroups to which @ss is attached and all future cgroups will
2826 * have them too. This function can be called anytime whether @ss is
2827 * attached or not.
2828 *
2829 * Returns 0 on successful registration, -errno on failure. Note that this
2830 * function currently returns 0 as long as @cfts registration is successful
2831 * even if some file creation attempts on existing cgroups fail.
2832 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002833int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002834{
2835 struct cftype_set *set;
2836
2837 set = kzalloc(sizeof(*set), GFP_KERNEL);
2838 if (!set)
2839 return -ENOMEM;
2840
2841 cgroup_cfts_prepare();
2842 set->cfts = cfts;
2843 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002844 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002845
2846 return 0;
2847}
2848EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2849
Li Zefana043e3b2008-02-23 15:24:09 -08002850/**
Tejun Heo79578622012-04-01 12:09:56 -07002851 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2852 * @ss: target cgroup subsystem
2853 * @cfts: zero-length name terminated array of cftypes
2854 *
2855 * Unregister @cfts from @ss. Files described by @cfts are removed from
2856 * all existing cgroups to which @ss is attached and all future cgroups
2857 * won't have them either. This function can be called anytime whether @ss
2858 * is attached or not.
2859 *
2860 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2861 * registered with @ss.
2862 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002863int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002864{
2865 struct cftype_set *set;
2866
2867 cgroup_cfts_prepare();
2868
2869 list_for_each_entry(set, &ss->cftsets, node) {
2870 if (set->cfts == cfts) {
2871 list_del_init(&set->node);
2872 cgroup_cfts_commit(ss, cfts, false);
2873 return 0;
2874 }
2875 }
2876
2877 cgroup_cfts_commit(ss, NULL, false);
2878 return -ENOENT;
2879}
2880
2881/**
Li Zefana043e3b2008-02-23 15:24:09 -08002882 * cgroup_task_count - count the number of tasks in a cgroup.
2883 * @cgrp: the cgroup in question
2884 *
2885 * Return the number of tasks in the cgroup.
2886 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002887int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002888{
2889 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002890 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002891
Paul Menage817929e2007-10-18 23:39:36 -07002892 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002893 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002894 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002895 }
2896 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002897 return count;
2898}
2899
2900/*
Paul Menage817929e2007-10-18 23:39:36 -07002901 * Advance a list_head iterator. The iterator should be positioned at
2902 * the start of a css_set
2903 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002904static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002905 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002906{
2907 struct list_head *l = it->cg_link;
2908 struct cg_cgroup_link *link;
2909 struct css_set *cg;
2910
2911 /* Advance to the next non-empty css_set */
2912 do {
2913 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002914 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002915 it->cg_link = NULL;
2916 return;
2917 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002918 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002919 cg = link->cg;
2920 } while (list_empty(&cg->tasks));
2921 it->cg_link = l;
2922 it->task = cg->tasks.next;
2923}
2924
Cliff Wickman31a7df02008-02-07 00:14:42 -08002925/*
2926 * To reduce the fork() overhead for systems that are not actually
2927 * using their cgroups capability, we don't maintain the lists running
2928 * through each css_set to its tasks until we see the list actually
2929 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002930 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002931static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002932{
2933 struct task_struct *p, *g;
2934 write_lock(&css_set_lock);
2935 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002936 /*
2937 * We need tasklist_lock because RCU is not safe against
2938 * while_each_thread(). Besides, a forking task that has passed
2939 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2940 * is not guaranteed to have its child immediately visible in the
2941 * tasklist if we walk through it with RCU.
2942 */
2943 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002944 do_each_thread(g, p) {
2945 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002946 /*
2947 * We should check if the process is exiting, otherwise
2948 * it will race with cgroup_exit() in that the list
2949 * entry won't be deleted though the process has exited.
2950 */
2951 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002952 list_add(&p->cg_list, &p->cgroups->tasks);
2953 task_unlock(p);
2954 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002955 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002956 write_unlock(&css_set_lock);
2957}
2958
Tejun Heo574bd9f2012-11-09 09:12:29 -08002959/**
2960 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2961 * @pos: the current position (%NULL to initiate traversal)
2962 * @cgroup: cgroup whose descendants to walk
2963 *
2964 * To be used by cgroup_for_each_descendant_pre(). Find the next
2965 * descendant to visit for pre-order traversal of @cgroup's descendants.
2966 */
2967struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2968 struct cgroup *cgroup)
2969{
2970 struct cgroup *next;
2971
2972 WARN_ON_ONCE(!rcu_read_lock_held());
2973
2974 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09002975 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002976 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002977
2978 /* visit the first child if exists */
2979 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2980 if (next)
2981 return next;
2982
2983 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09002984 while (pos != cgroup) {
Tejun Heo574bd9f2012-11-09 09:12:29 -08002985 next = list_entry_rcu(pos->sibling.next, struct cgroup,
2986 sibling);
2987 if (&next->sibling != &pos->parent->children)
2988 return next;
2989
2990 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09002991 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002992
2993 return NULL;
2994}
2995EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
2996
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002997/**
2998 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
2999 * @pos: cgroup of interest
3000 *
3001 * Return the rightmost descendant of @pos. If there's no descendant,
3002 * @pos is returned. This can be used during pre-order traversal to skip
3003 * subtree of @pos.
3004 */
3005struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3006{
3007 struct cgroup *last, *tmp;
3008
3009 WARN_ON_ONCE(!rcu_read_lock_held());
3010
3011 do {
3012 last = pos;
3013 /* ->prev isn't RCU safe, walk ->next till the end */
3014 pos = NULL;
3015 list_for_each_entry_rcu(tmp, &last->children, sibling)
3016 pos = tmp;
3017 } while (pos);
3018
3019 return last;
3020}
3021EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3022
Tejun Heo574bd9f2012-11-09 09:12:29 -08003023static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3024{
3025 struct cgroup *last;
3026
3027 do {
3028 last = pos;
3029 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3030 sibling);
3031 } while (pos);
3032
3033 return last;
3034}
3035
3036/**
3037 * cgroup_next_descendant_post - find the next descendant for post-order walk
3038 * @pos: the current position (%NULL to initiate traversal)
3039 * @cgroup: cgroup whose descendants to walk
3040 *
3041 * To be used by cgroup_for_each_descendant_post(). Find the next
3042 * descendant to visit for post-order traversal of @cgroup's descendants.
3043 */
3044struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3045 struct cgroup *cgroup)
3046{
3047 struct cgroup *next;
3048
3049 WARN_ON_ONCE(!rcu_read_lock_held());
3050
3051 /* if first iteration, visit the leftmost descendant */
3052 if (!pos) {
3053 next = cgroup_leftmost_descendant(cgroup);
3054 return next != cgroup ? next : NULL;
3055 }
3056
3057 /* if there's an unvisited sibling, visit its leftmost descendant */
3058 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3059 if (&next->sibling != &pos->parent->children)
3060 return cgroup_leftmost_descendant(next);
3061
3062 /* no sibling left, visit parent */
3063 next = pos->parent;
3064 return next != cgroup ? next : NULL;
3065}
3066EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3067
Paul Menagebd89aab2007-10-18 23:40:44 -07003068void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003069 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003070{
3071 /*
3072 * The first time anyone tries to iterate across a cgroup,
3073 * we need to enable the list linking each css_set to its
3074 * tasks, and fix up all existing tasks.
3075 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003076 if (!use_task_css_set_links)
3077 cgroup_enable_task_cg_lists();
3078
Paul Menage817929e2007-10-18 23:39:36 -07003079 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003080 it->cg_link = &cgrp->css_sets;
3081 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003082}
3083
Paul Menagebd89aab2007-10-18 23:40:44 -07003084struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003085 struct cgroup_iter *it)
3086{
3087 struct task_struct *res;
3088 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003089 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003090
3091 /* If the iterator cg is NULL, we have no tasks */
3092 if (!it->cg_link)
3093 return NULL;
3094 res = list_entry(l, struct task_struct, cg_list);
3095 /* Advance iterator to find next entry */
3096 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003097 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3098 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003099 /* We reached the end of this task list - move on to
3100 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003101 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003102 } else {
3103 it->task = l;
3104 }
3105 return res;
3106}
3107
Paul Menagebd89aab2007-10-18 23:40:44 -07003108void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003109 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003110{
3111 read_unlock(&css_set_lock);
3112}
3113
Cliff Wickman31a7df02008-02-07 00:14:42 -08003114static inline int started_after_time(struct task_struct *t1,
3115 struct timespec *time,
3116 struct task_struct *t2)
3117{
3118 int start_diff = timespec_compare(&t1->start_time, time);
3119 if (start_diff > 0) {
3120 return 1;
3121 } else if (start_diff < 0) {
3122 return 0;
3123 } else {
3124 /*
3125 * Arbitrarily, if two processes started at the same
3126 * time, we'll say that the lower pointer value
3127 * started first. Note that t2 may have exited by now
3128 * so this may not be a valid pointer any longer, but
3129 * that's fine - it still serves to distinguish
3130 * between two tasks started (effectively) simultaneously.
3131 */
3132 return t1 > t2;
3133 }
3134}
3135
3136/*
3137 * This function is a callback from heap_insert() and is used to order
3138 * the heap.
3139 * In this case we order the heap in descending task start time.
3140 */
3141static inline int started_after(void *p1, void *p2)
3142{
3143 struct task_struct *t1 = p1;
3144 struct task_struct *t2 = p2;
3145 return started_after_time(t1, &t2->start_time, t2);
3146}
3147
3148/**
3149 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3150 * @scan: struct cgroup_scanner containing arguments for the scan
3151 *
3152 * Arguments include pointers to callback functions test_task() and
3153 * process_task().
3154 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3155 * and if it returns true, call process_task() for it also.
3156 * The test_task pointer may be NULL, meaning always true (select all tasks).
3157 * Effectively duplicates cgroup_iter_{start,next,end}()
3158 * but does not lock css_set_lock for the call to process_task().
3159 * The struct cgroup_scanner may be embedded in any structure of the caller's
3160 * creation.
3161 * It is guaranteed that process_task() will act on every task that
3162 * is a member of the cgroup for the duration of this call. This
3163 * function may or may not call process_task() for tasks that exit
3164 * or move to a different cgroup during the call, or are forked or
3165 * move into the cgroup during the call.
3166 *
3167 * Note that test_task() may be called with locks held, and may in some
3168 * situations be called multiple times for the same task, so it should
3169 * be cheap.
3170 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3171 * pre-allocated and will be used for heap operations (and its "gt" member will
3172 * be overwritten), else a temporary heap will be used (allocation of which
3173 * may cause this function to fail).
3174 */
3175int cgroup_scan_tasks(struct cgroup_scanner *scan)
3176{
3177 int retval, i;
3178 struct cgroup_iter it;
3179 struct task_struct *p, *dropped;
3180 /* Never dereference latest_task, since it's not refcounted */
3181 struct task_struct *latest_task = NULL;
3182 struct ptr_heap tmp_heap;
3183 struct ptr_heap *heap;
3184 struct timespec latest_time = { 0, 0 };
3185
3186 if (scan->heap) {
3187 /* The caller supplied our heap and pre-allocated its memory */
3188 heap = scan->heap;
3189 heap->gt = &started_after;
3190 } else {
3191 /* We need to allocate our own heap memory */
3192 heap = &tmp_heap;
3193 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3194 if (retval)
3195 /* cannot allocate the heap */
3196 return retval;
3197 }
3198
3199 again:
3200 /*
3201 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3202 * to determine which are of interest, and using the scanner's
3203 * "process_task" callback to process any of them that need an update.
3204 * Since we don't want to hold any locks during the task updates,
3205 * gather tasks to be processed in a heap structure.
3206 * The heap is sorted by descending task start time.
3207 * If the statically-sized heap fills up, we overflow tasks that
3208 * started later, and in future iterations only consider tasks that
3209 * started after the latest task in the previous pass. This
3210 * guarantees forward progress and that we don't miss any tasks.
3211 */
3212 heap->size = 0;
3213 cgroup_iter_start(scan->cg, &it);
3214 while ((p = cgroup_iter_next(scan->cg, &it))) {
3215 /*
3216 * Only affect tasks that qualify per the caller's callback,
3217 * if he provided one
3218 */
3219 if (scan->test_task && !scan->test_task(p, scan))
3220 continue;
3221 /*
3222 * Only process tasks that started after the last task
3223 * we processed
3224 */
3225 if (!started_after_time(p, &latest_time, latest_task))
3226 continue;
3227 dropped = heap_insert(heap, p);
3228 if (dropped == NULL) {
3229 /*
3230 * The new task was inserted; the heap wasn't
3231 * previously full
3232 */
3233 get_task_struct(p);
3234 } else if (dropped != p) {
3235 /*
3236 * The new task was inserted, and pushed out a
3237 * different task
3238 */
3239 get_task_struct(p);
3240 put_task_struct(dropped);
3241 }
3242 /*
3243 * Else the new task was newer than anything already in
3244 * the heap and wasn't inserted
3245 */
3246 }
3247 cgroup_iter_end(scan->cg, &it);
3248
3249 if (heap->size) {
3250 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003251 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003252 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003253 latest_time = q->start_time;
3254 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003255 }
3256 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003257 scan->process_task(q, scan);
3258 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003259 }
3260 /*
3261 * If we had to process any tasks at all, scan again
3262 * in case some of them were in the middle of forking
3263 * children that didn't get processed.
3264 * Not the most efficient way to do it, but it avoids
3265 * having to take callback_mutex in the fork path
3266 */
3267 goto again;
3268 }
3269 if (heap == &tmp_heap)
3270 heap_free(&tmp_heap);
3271 return 0;
3272}
3273
Tejun Heo8cc99342013-04-07 09:29:50 -07003274static void cgroup_transfer_one_task(struct task_struct *task,
3275 struct cgroup_scanner *scan)
3276{
3277 struct cgroup *new_cgroup = scan->data;
3278
Tejun Heo47cfcd02013-04-07 09:29:51 -07003279 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003280 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003281 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003282}
3283
3284/**
3285 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3286 * @to: cgroup to which the tasks will be moved
3287 * @from: cgroup in which the tasks currently reside
3288 */
3289int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3290{
3291 struct cgroup_scanner scan;
3292
3293 scan.cg = from;
3294 scan.test_task = NULL; /* select all tasks in cgroup */
3295 scan.process_task = cgroup_transfer_one_task;
3296 scan.heap = NULL;
3297 scan.data = to;
3298
3299 return cgroup_scan_tasks(&scan);
3300}
3301
Paul Menage817929e2007-10-18 23:39:36 -07003302/*
Ben Blum102a7752009-09-23 15:56:26 -07003303 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003304 *
3305 * Reading this file can return large amounts of data if a cgroup has
3306 * *lots* of attached tasks. So it may need several calls to read(),
3307 * but we cannot guarantee that the information we produce is correct
3308 * unless we produce it entirely atomically.
3309 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003310 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003311
Li Zefan24528252012-01-20 11:58:43 +08003312/* which pidlist file are we talking about? */
3313enum cgroup_filetype {
3314 CGROUP_FILE_PROCS,
3315 CGROUP_FILE_TASKS,
3316};
3317
3318/*
3319 * A pidlist is a list of pids that virtually represents the contents of one
3320 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3321 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3322 * to the cgroup.
3323 */
3324struct cgroup_pidlist {
3325 /*
3326 * used to find which pidlist is wanted. doesn't change as long as
3327 * this particular list stays in the list.
3328 */
3329 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3330 /* array of xids */
3331 pid_t *list;
3332 /* how many elements the above list has */
3333 int length;
3334 /* how many files are using the current array */
3335 int use_count;
3336 /* each of these stored in a list by its cgroup */
3337 struct list_head links;
3338 /* pointer to the cgroup we belong to, for list removal purposes */
3339 struct cgroup *owner;
3340 /* protects the other fields */
3341 struct rw_semaphore mutex;
3342};
3343
Paul Menagebbcb81d2007-10-18 23:39:32 -07003344/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003345 * The following two functions "fix" the issue where there are more pids
3346 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3347 * TODO: replace with a kernel-wide solution to this problem
3348 */
3349#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3350static void *pidlist_allocate(int count)
3351{
3352 if (PIDLIST_TOO_LARGE(count))
3353 return vmalloc(count * sizeof(pid_t));
3354 else
3355 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3356}
3357static void pidlist_free(void *p)
3358{
3359 if (is_vmalloc_addr(p))
3360 vfree(p);
3361 else
3362 kfree(p);
3363}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003364
3365/*
Ben Blum102a7752009-09-23 15:56:26 -07003366 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003367 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003368 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003369static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003370{
Ben Blum102a7752009-09-23 15:56:26 -07003371 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003372
3373 /*
3374 * we presume the 0th element is unique, so i starts at 1. trivial
3375 * edge cases first; no work needs to be done for either
3376 */
3377 if (length == 0 || length == 1)
3378 return length;
3379 /* src and dest walk down the list; dest counts unique elements */
3380 for (src = 1; src < length; src++) {
3381 /* find next unique element */
3382 while (list[src] == list[src-1]) {
3383 src++;
3384 if (src == length)
3385 goto after;
3386 }
3387 /* dest always points to where the next unique element goes */
3388 list[dest] = list[src];
3389 dest++;
3390 }
3391after:
Ben Blum102a7752009-09-23 15:56:26 -07003392 return dest;
3393}
3394
3395static int cmppid(const void *a, const void *b)
3396{
3397 return *(pid_t *)a - *(pid_t *)b;
3398}
3399
3400/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003401 * find the appropriate pidlist for our purpose (given procs vs tasks)
3402 * returns with the lock on that pidlist already held, and takes care
3403 * of the use count, or returns NULL with no locks held if we're out of
3404 * memory.
3405 */
3406static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3407 enum cgroup_filetype type)
3408{
3409 struct cgroup_pidlist *l;
3410 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003411 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003412
Ben Blum72a8cb32009-09-23 15:56:27 -07003413 /*
3414 * We can't drop the pidlist_mutex before taking the l->mutex in case
3415 * the last ref-holder is trying to remove l from the list at the same
3416 * time. Holding the pidlist_mutex precludes somebody taking whichever
3417 * list we find out from under us - compare release_pid_array().
3418 */
3419 mutex_lock(&cgrp->pidlist_mutex);
3420 list_for_each_entry(l, &cgrp->pidlists, links) {
3421 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003422 /* make sure l doesn't vanish out from under us */
3423 down_write(&l->mutex);
3424 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003425 return l;
3426 }
3427 }
3428 /* entry not found; create a new one */
3429 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3430 if (!l) {
3431 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003432 return l;
3433 }
3434 init_rwsem(&l->mutex);
3435 down_write(&l->mutex);
3436 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003437 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003438 l->use_count = 0; /* don't increment here */
3439 l->list = NULL;
3440 l->owner = cgrp;
3441 list_add(&l->links, &cgrp->pidlists);
3442 mutex_unlock(&cgrp->pidlist_mutex);
3443 return l;
3444}
3445
3446/*
Ben Blum102a7752009-09-23 15:56:26 -07003447 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3448 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003449static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3450 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003451{
3452 pid_t *array;
3453 int length;
3454 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003455 struct cgroup_iter it;
3456 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003457 struct cgroup_pidlist *l;
3458
3459 /*
3460 * If cgroup gets more users after we read count, we won't have
3461 * enough space - tough. This race is indistinguishable to the
3462 * caller from the case that the additional cgroup users didn't
3463 * show up until sometime later on.
3464 */
3465 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003466 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003467 if (!array)
3468 return -ENOMEM;
3469 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003470 cgroup_iter_start(cgrp, &it);
3471 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003472 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003473 break;
Ben Blum102a7752009-09-23 15:56:26 -07003474 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003475 if (type == CGROUP_FILE_PROCS)
3476 pid = task_tgid_vnr(tsk);
3477 else
3478 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003479 if (pid > 0) /* make sure to only use valid results */
3480 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003481 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003482 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003483 length = n;
3484 /* now sort & (if procs) strip out duplicates */
3485 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003486 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003487 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003488 l = cgroup_pidlist_find(cgrp, type);
3489 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003490 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003491 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003492 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003493 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003494 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003495 l->list = array;
3496 l->length = length;
3497 l->use_count++;
3498 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003499 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003500 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003501}
3502
Balbir Singh846c7bb2007-10-18 23:39:44 -07003503/**
Li Zefana043e3b2008-02-23 15:24:09 -08003504 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003505 * @stats: cgroupstats to fill information into
3506 * @dentry: A dentry entry belonging to the cgroup for which stats have
3507 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003508 *
3509 * Build and fill cgroupstats so that taskstats can export it to user
3510 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003511 */
3512int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3513{
3514 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003515 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003516 struct cgroup_iter it;
3517 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003518
Balbir Singh846c7bb2007-10-18 23:39:44 -07003519 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003520 * Validate dentry by checking the superblock operations,
3521 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003522 */
Li Zefan33d283b2008-11-19 15:36:48 -08003523 if (dentry->d_sb->s_op != &cgroup_ops ||
3524 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003525 goto err;
3526
3527 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003528 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003529
Paul Menagebd89aab2007-10-18 23:40:44 -07003530 cgroup_iter_start(cgrp, &it);
3531 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003532 switch (tsk->state) {
3533 case TASK_RUNNING:
3534 stats->nr_running++;
3535 break;
3536 case TASK_INTERRUPTIBLE:
3537 stats->nr_sleeping++;
3538 break;
3539 case TASK_UNINTERRUPTIBLE:
3540 stats->nr_uninterruptible++;
3541 break;
3542 case TASK_STOPPED:
3543 stats->nr_stopped++;
3544 break;
3545 default:
3546 if (delayacct_is_task_waiting_on_io(tsk))
3547 stats->nr_io_wait++;
3548 break;
3549 }
3550 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003551 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003552
Balbir Singh846c7bb2007-10-18 23:39:44 -07003553err:
3554 return ret;
3555}
3556
Paul Menage8f3ff202009-09-23 15:56:25 -07003557
Paul Menagecc31edc2008-10-18 20:28:04 -07003558/*
Ben Blum102a7752009-09-23 15:56:26 -07003559 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003560 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003561 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003562 */
3563
Ben Blum102a7752009-09-23 15:56:26 -07003564static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003565{
3566 /*
3567 * Initially we receive a position value that corresponds to
3568 * one more than the last pid shown (or 0 on the first call or
3569 * after a seek to the start). Use a binary-search to find the
3570 * next pid to display, if any
3571 */
Ben Blum102a7752009-09-23 15:56:26 -07003572 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003573 int index = 0, pid = *pos;
3574 int *iter;
3575
Ben Blum102a7752009-09-23 15:56:26 -07003576 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003577 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003578 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003579
Paul Menagecc31edc2008-10-18 20:28:04 -07003580 while (index < end) {
3581 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003582 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003583 index = mid;
3584 break;
Ben Blum102a7752009-09-23 15:56:26 -07003585 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003586 index = mid + 1;
3587 else
3588 end = mid;
3589 }
3590 }
3591 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003592 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003593 return NULL;
3594 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003595 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003596 *pos = *iter;
3597 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003598}
3599
Ben Blum102a7752009-09-23 15:56:26 -07003600static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003601{
Ben Blum102a7752009-09-23 15:56:26 -07003602 struct cgroup_pidlist *l = s->private;
3603 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003604}
3605
Ben Blum102a7752009-09-23 15:56:26 -07003606static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003607{
Ben Blum102a7752009-09-23 15:56:26 -07003608 struct cgroup_pidlist *l = s->private;
3609 pid_t *p = v;
3610 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003611 /*
3612 * Advance to the next pid in the array. If this goes off the
3613 * end, we're done
3614 */
3615 p++;
3616 if (p >= end) {
3617 return NULL;
3618 } else {
3619 *pos = *p;
3620 return p;
3621 }
3622}
3623
Ben Blum102a7752009-09-23 15:56:26 -07003624static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003625{
3626 return seq_printf(s, "%d\n", *(int *)v);
3627}
3628
Ben Blum102a7752009-09-23 15:56:26 -07003629/*
3630 * seq_operations functions for iterating on pidlists through seq_file -
3631 * independent of whether it's tasks or procs
3632 */
3633static const struct seq_operations cgroup_pidlist_seq_operations = {
3634 .start = cgroup_pidlist_start,
3635 .stop = cgroup_pidlist_stop,
3636 .next = cgroup_pidlist_next,
3637 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003638};
3639
Ben Blum102a7752009-09-23 15:56:26 -07003640static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003641{
Ben Blum72a8cb32009-09-23 15:56:27 -07003642 /*
3643 * the case where we're the last user of this particular pidlist will
3644 * have us remove it from the cgroup's list, which entails taking the
3645 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3646 * pidlist_mutex, we have to take pidlist_mutex first.
3647 */
3648 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003649 down_write(&l->mutex);
3650 BUG_ON(!l->use_count);
3651 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003652 /* we're the last user if refcount is 0; remove and free */
3653 list_del(&l->links);
3654 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003655 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003656 put_pid_ns(l->key.ns);
3657 up_write(&l->mutex);
3658 kfree(l);
3659 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003660 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003661 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003662 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003663}
3664
Ben Blum102a7752009-09-23 15:56:26 -07003665static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003666{
Ben Blum102a7752009-09-23 15:56:26 -07003667 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003668 if (!(file->f_mode & FMODE_READ))
3669 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003670 /*
3671 * the seq_file will only be initialized if the file was opened for
3672 * reading; hence we check if it's not null only in that case.
3673 */
3674 l = ((struct seq_file *)file->private_data)->private;
3675 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003676 return seq_release(inode, file);
3677}
3678
Ben Blum102a7752009-09-23 15:56:26 -07003679static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003680 .read = seq_read,
3681 .llseek = seq_lseek,
3682 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003683 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003684};
3685
3686/*
Ben Blum102a7752009-09-23 15:56:26 -07003687 * The following functions handle opens on a file that displays a pidlist
3688 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3689 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003690 */
Ben Blum102a7752009-09-23 15:56:26 -07003691/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003692static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003693{
3694 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003695 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003696 int retval;
3697
3698 /* Nothing to do for write-only files */
3699 if (!(file->f_mode & FMODE_READ))
3700 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003701
Ben Blum102a7752009-09-23 15:56:26 -07003702 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003703 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003704 if (retval)
3705 return retval;
3706 /* configure file information */
3707 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003708
Ben Blum102a7752009-09-23 15:56:26 -07003709 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003710 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003711 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003712 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003713 }
Ben Blum102a7752009-09-23 15:56:26 -07003714 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003715 return 0;
3716}
Ben Blum102a7752009-09-23 15:56:26 -07003717static int cgroup_tasks_open(struct inode *unused, struct file *file)
3718{
Ben Blum72a8cb32009-09-23 15:56:27 -07003719 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003720}
3721static int cgroup_procs_open(struct inode *unused, struct file *file)
3722{
Ben Blum72a8cb32009-09-23 15:56:27 -07003723 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003724}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003725
Paul Menagebd89aab2007-10-18 23:40:44 -07003726static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003727 struct cftype *cft)
3728{
Paul Menagebd89aab2007-10-18 23:40:44 -07003729 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003730}
3731
Paul Menage6379c102008-07-25 01:47:01 -07003732static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3733 struct cftype *cft,
3734 u64 val)
3735{
3736 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3737 if (val)
3738 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3739 else
3740 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3741 return 0;
3742}
3743
Paul Menagebbcb81d2007-10-18 23:39:32 -07003744/*
Li Zefan83d0eb72013-06-18 18:41:10 +08003745 * When dput() is called asynchronously, if umount has been done and
3746 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3747 * there's a small window that vfs will see the root dentry with non-zero
3748 * refcnt and trigger BUG().
3749 *
3750 * That's why we hold a reference before dput() and drop it right after.
3751 */
3752static void cgroup_dput(struct cgroup *cgrp)
3753{
3754 struct super_block *sb = cgrp->root->sb;
3755
3756 atomic_inc(&sb->s_active);
3757 dput(cgrp->dentry);
3758 deactivate_super(sb);
3759}
3760
3761/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003762 * Unregister event and free resources.
3763 *
3764 * Gets called from workqueue.
3765 */
3766static void cgroup_event_remove(struct work_struct *work)
3767{
3768 struct cgroup_event *event = container_of(work, struct cgroup_event,
3769 remove);
3770 struct cgroup *cgrp = event->cgrp;
3771
Li Zefan810cbee2013-02-18 18:56:14 +08003772 remove_wait_queue(event->wqh, &event->wait);
3773
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003774 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3775
Li Zefan810cbee2013-02-18 18:56:14 +08003776 /* Notify userspace the event is going away. */
3777 eventfd_signal(event->eventfd, 1);
3778
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003779 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003780 kfree(event);
Li Zefan83d0eb72013-06-18 18:41:10 +08003781 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003782}
3783
3784/*
3785 * Gets called on POLLHUP on eventfd when user closes it.
3786 *
3787 * Called with wqh->lock held and interrupts disabled.
3788 */
3789static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3790 int sync, void *key)
3791{
3792 struct cgroup_event *event = container_of(wait,
3793 struct cgroup_event, wait);
3794 struct cgroup *cgrp = event->cgrp;
3795 unsigned long flags = (unsigned long)key;
3796
3797 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003798 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003799 * If the event has been detached at cgroup removal, we
3800 * can simply return knowing the other side will cleanup
3801 * for us.
3802 *
3803 * We can't race against event freeing since the other
3804 * side will require wqh->lock via remove_wait_queue(),
3805 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003806 */
Li Zefan810cbee2013-02-18 18:56:14 +08003807 spin_lock(&cgrp->event_list_lock);
3808 if (!list_empty(&event->list)) {
3809 list_del_init(&event->list);
3810 /*
3811 * We are in atomic context, but cgroup_event_remove()
3812 * may sleep, so we have to call it in workqueue.
3813 */
3814 schedule_work(&event->remove);
3815 }
3816 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003817 }
3818
3819 return 0;
3820}
3821
3822static void cgroup_event_ptable_queue_proc(struct file *file,
3823 wait_queue_head_t *wqh, poll_table *pt)
3824{
3825 struct cgroup_event *event = container_of(pt,
3826 struct cgroup_event, pt);
3827
3828 event->wqh = wqh;
3829 add_wait_queue(wqh, &event->wait);
3830}
3831
3832/*
3833 * Parse input and register new cgroup event handler.
3834 *
3835 * Input must be in format '<event_fd> <control_fd> <args>'.
3836 * Interpretation of args is defined by control file implementation.
3837 */
3838static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3839 const char *buffer)
3840{
3841 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003842 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003843 unsigned int efd, cfd;
3844 struct file *efile = NULL;
3845 struct file *cfile = NULL;
3846 char *endp;
3847 int ret;
3848
3849 efd = simple_strtoul(buffer, &endp, 10);
3850 if (*endp != ' ')
3851 return -EINVAL;
3852 buffer = endp + 1;
3853
3854 cfd = simple_strtoul(buffer, &endp, 10);
3855 if ((*endp != ' ') && (*endp != '\0'))
3856 return -EINVAL;
3857 buffer = endp + 1;
3858
3859 event = kzalloc(sizeof(*event), GFP_KERNEL);
3860 if (!event)
3861 return -ENOMEM;
3862 event->cgrp = cgrp;
3863 INIT_LIST_HEAD(&event->list);
3864 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3865 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3866 INIT_WORK(&event->remove, cgroup_event_remove);
3867
3868 efile = eventfd_fget(efd);
3869 if (IS_ERR(efile)) {
3870 ret = PTR_ERR(efile);
3871 goto fail;
3872 }
3873
3874 event->eventfd = eventfd_ctx_fileget(efile);
3875 if (IS_ERR(event->eventfd)) {
3876 ret = PTR_ERR(event->eventfd);
3877 goto fail;
3878 }
3879
3880 cfile = fget(cfd);
3881 if (!cfile) {
3882 ret = -EBADF;
3883 goto fail;
3884 }
3885
3886 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003887 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003888 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003889 if (ret < 0)
3890 goto fail;
3891
3892 event->cft = __file_cft(cfile);
3893 if (IS_ERR(event->cft)) {
3894 ret = PTR_ERR(event->cft);
3895 goto fail;
3896 }
3897
Li Zefanf1690072013-02-18 14:13:35 +08003898 /*
3899 * The file to be monitored must be in the same cgroup as
3900 * cgroup.event_control is.
3901 */
3902 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3903 if (cgrp_cfile != cgrp) {
3904 ret = -EINVAL;
3905 goto fail;
3906 }
3907
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003908 if (!event->cft->register_event || !event->cft->unregister_event) {
3909 ret = -EINVAL;
3910 goto fail;
3911 }
3912
3913 ret = event->cft->register_event(cgrp, event->cft,
3914 event->eventfd, buffer);
3915 if (ret)
3916 goto fail;
3917
Li Zefan7ef70e42013-04-26 11:58:03 -07003918 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003919
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003920 /*
3921 * Events should be removed after rmdir of cgroup directory, but before
3922 * destroying subsystem state objects. Let's take reference to cgroup
3923 * directory dentry to do that.
3924 */
3925 dget(cgrp->dentry);
3926
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003927 spin_lock(&cgrp->event_list_lock);
3928 list_add(&event->list, &cgrp->event_list);
3929 spin_unlock(&cgrp->event_list_lock);
3930
3931 fput(cfile);
3932 fput(efile);
3933
3934 return 0;
3935
3936fail:
3937 if (cfile)
3938 fput(cfile);
3939
3940 if (event && event->eventfd && !IS_ERR(event->eventfd))
3941 eventfd_ctx_put(event->eventfd);
3942
3943 if (!IS_ERR_OR_NULL(efile))
3944 fput(efile);
3945
3946 kfree(event);
3947
3948 return ret;
3949}
3950
Daniel Lezcano97978e62010-10-27 15:33:35 -07003951static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3952 struct cftype *cft)
3953{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003954 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003955}
3956
3957static int cgroup_clone_children_write(struct cgroup *cgrp,
3958 struct cftype *cft,
3959 u64 val)
3960{
3961 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003962 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003963 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003964 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003965 return 0;
3966}
3967
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003968/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003969 * for the common functions, 'private' gives the type of file
3970 */
Ben Blum102a7752009-09-23 15:56:26 -07003971/* for hysterical raisins, we can't put this on the older files */
3972#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003973static struct cftype files[] = {
3974 {
3975 .name = "tasks",
3976 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003977 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003978 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003979 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003980 },
Ben Blum102a7752009-09-23 15:56:26 -07003981 {
3982 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3983 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003984 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003985 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003986 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003987 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003988 {
3989 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003990 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003991 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003992 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003993 {
3994 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3995 .write_string = cgroup_write_event_control,
3996 .mode = S_IWUGO,
3997 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003998 {
3999 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004000 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004001 .read_u64 = cgroup_clone_children_read,
4002 .write_u64 = cgroup_clone_children_write,
4003 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004004 {
Tejun Heo873fe092013-04-14 20:15:26 -07004005 .name = "cgroup.sane_behavior",
4006 .flags = CFTYPE_ONLY_ON_ROOT,
4007 .read_seq_string = cgroup_sane_behavior_show,
4008 },
4009 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004010 .name = "release_agent",
4011 .flags = CFTYPE_ONLY_ON_ROOT,
4012 .read_seq_string = cgroup_release_agent_show,
4013 .write_string = cgroup_release_agent_write,
4014 .max_write_len = PATH_MAX,
4015 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004016 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004017};
4018
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004019/**
4020 * cgroup_populate_dir - selectively creation of files in a directory
4021 * @cgrp: target cgroup
4022 * @base_files: true if the base files should be added
4023 * @subsys_mask: mask of the subsystem ids whose files should be added
4024 */
4025static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4026 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004027{
4028 int err;
4029 struct cgroup_subsys *ss;
4030
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004031 if (base_files) {
4032 err = cgroup_addrm_files(cgrp, NULL, files, true);
4033 if (err < 0)
4034 return err;
4035 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004036
Tejun Heo8e3f6542012-04-01 12:09:55 -07004037 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004038 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004039 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004040 if (!test_bit(ss->subsys_id, &subsys_mask))
4041 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004042
Tejun Heodb0416b2012-04-01 12:09:55 -07004043 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004044 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004045 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004046
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004047 /* This cgroup is ready now */
4048 for_each_subsys(cgrp->root, ss) {
4049 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4050 /*
4051 * Update id->css pointer and make this css visible from
4052 * CSS ID functions. This pointer will be dereferened
4053 * from RCU-read-side without locks.
4054 */
4055 if (css->id)
4056 rcu_assign_pointer(css->id->css, css);
4057 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004058
4059 return 0;
4060}
4061
Tejun Heo48ddbe12012-04-01 12:09:56 -07004062static void css_dput_fn(struct work_struct *work)
4063{
4064 struct cgroup_subsys_state *css =
4065 container_of(work, struct cgroup_subsys_state, dput_work);
4066
Li Zefan83d0eb72013-06-18 18:41:10 +08004067 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004068}
4069
Paul Menageddbcc7e2007-10-18 23:39:30 -07004070static void init_cgroup_css(struct cgroup_subsys_state *css,
4071 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004072 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004073{
Paul Menagebd89aab2007-10-18 23:40:44 -07004074 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004075 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004076 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004077 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004078 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004079 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004080 BUG_ON(cgrp->subsys[ss->subsys_id]);
4081 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004082
4083 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004084 * css holds an extra ref to @cgrp->dentry which is put on the last
4085 * css_put(). dput() requires process context, which css_put() may
4086 * be called without. @css->dput_work will be used to invoke
4087 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004088 */
4089 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004090}
4091
Tejun Heob1929db2012-11-19 08:13:38 -08004092/* invoke ->post_create() on a new CSS and mark it online if successful */
4093static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004094{
Tejun Heob1929db2012-11-19 08:13:38 -08004095 int ret = 0;
4096
Tejun Heoa31f2d32012-11-19 08:13:37 -08004097 lockdep_assert_held(&cgroup_mutex);
4098
Tejun Heo92fb9742012-11-19 08:13:38 -08004099 if (ss->css_online)
4100 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004101 if (!ret)
4102 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4103 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004104}
4105
4106/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4107static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4108 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4109{
4110 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4111
4112 lockdep_assert_held(&cgroup_mutex);
4113
4114 if (!(css->flags & CSS_ONLINE))
4115 return;
4116
Li Zefand7eeac12013-03-12 15:35:59 -07004117 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004118 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004119
4120 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4121}
4122
Paul Menageddbcc7e2007-10-18 23:39:30 -07004123/*
Li Zefana043e3b2008-02-23 15:24:09 -08004124 * cgroup_create - create a cgroup
4125 * @parent: cgroup that will be parent of the new cgroup
4126 * @dentry: dentry of the new cgroup
4127 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004128 *
Li Zefana043e3b2008-02-23 15:24:09 -08004129 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004130 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004131static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004132 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004133{
Paul Menagebd89aab2007-10-18 23:40:44 -07004134 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004135 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004136 struct cgroupfs_root *root = parent->root;
4137 int err = 0;
4138 struct cgroup_subsys *ss;
4139 struct super_block *sb = root->sb;
4140
Tejun Heo0a950f62012-11-19 09:02:12 -08004141 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004142 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4143 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004144 return -ENOMEM;
4145
Li Zefan65dff752013-03-01 15:01:56 +08004146 name = cgroup_alloc_name(dentry);
4147 if (!name)
4148 goto err_free_cgrp;
4149 rcu_assign_pointer(cgrp->name, name);
4150
Tejun Heo0a950f62012-11-19 09:02:12 -08004151 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4152 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004153 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004154
Tejun Heo976c06b2012-11-05 09:16:59 -08004155 /*
4156 * Only live parents can have children. Note that the liveliness
4157 * check isn't strictly necessary because cgroup_mkdir() and
4158 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4159 * anyway so that locking is contained inside cgroup proper and we
4160 * don't get nasty surprises if we ever grow another caller.
4161 */
4162 if (!cgroup_lock_live_group(parent)) {
4163 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004164 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004165 }
4166
Paul Menageddbcc7e2007-10-18 23:39:30 -07004167 /* Grab a reference on the superblock so the hierarchy doesn't
4168 * get deleted on unmount if there are child cgroups. This
4169 * can be done outside cgroup_mutex, since the sb can't
4170 * disappear while someone has an open control file on the
4171 * fs */
4172 atomic_inc(&sb->s_active);
4173
Paul Menagecc31edc2008-10-18 20:28:04 -07004174 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004175
Li Zefanfe1c06c2013-01-24 14:30:22 +08004176 dentry->d_fsdata = cgrp;
4177 cgrp->dentry = dentry;
4178
Paul Menagebd89aab2007-10-18 23:40:44 -07004179 cgrp->parent = parent;
4180 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004181
Li Zefanb6abdb02008-03-04 14:28:19 -08004182 if (notify_on_release(parent))
4183 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4184
Tejun Heo2260e7f2012-11-19 08:13:38 -08004185 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4186 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004187
Paul Menageddbcc7e2007-10-18 23:39:30 -07004188 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004189 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004190
Tejun Heo92fb9742012-11-19 08:13:38 -08004191 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004192 if (IS_ERR(css)) {
4193 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004194 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004195 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004196 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004197 if (ss->use_id) {
4198 err = alloc_css_id(ss, parent, cgrp);
4199 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004200 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004201 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004202 }
4203
Tejun Heo4e139af2012-11-19 08:13:36 -08004204 /*
4205 * Create directory. cgroup_create_file() returns with the new
4206 * directory locked on success so that it can be populated without
4207 * dropping cgroup_mutex.
4208 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004209 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004210 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004211 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004212 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004213
Tejun Heo4e139af2012-11-19 08:13:36 -08004214 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004215 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4216 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4217 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004218
Tejun Heob1929db2012-11-19 08:13:38 -08004219 /* each css holds a ref to the cgroup's dentry */
4220 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004221 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004222
Li Zefan415cf072013-04-08 14:35:02 +08004223 /* hold a ref to the parent's dentry */
4224 dget(parent->dentry);
4225
Tejun Heob1929db2012-11-19 08:13:38 -08004226 /* creation succeeded, notify subsystems */
4227 for_each_subsys(root, ss) {
4228 err = online_css(ss, cgrp);
4229 if (err)
4230 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004231
4232 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4233 parent->parent) {
4234 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",
4235 current->comm, current->pid, ss->name);
4236 if (!strcmp(ss->name, "memory"))
4237 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4238 ss->warned_broken_hierarchy = true;
4239 }
Tejun Heoa8638032012-11-09 09:12:29 -08004240 }
4241
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004242 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004243 if (err)
4244 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004245
4246 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004247 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004248
4249 return 0;
4250
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004251err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004252 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004253 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004254 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004255 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004256 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004257 /* Release the reference count that we took on the superblock */
4258 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004259err_free_id:
4260 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004261err_free_name:
4262 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004263err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004264 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004265 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004266
4267err_destroy:
4268 cgroup_destroy_locked(cgrp);
4269 mutex_unlock(&cgroup_mutex);
4270 mutex_unlock(&dentry->d_inode->i_mutex);
4271 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004272}
4273
Al Viro18bb1db2011-07-26 01:41:39 -04004274static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004275{
4276 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4277
4278 /* the vfs holds inode->i_mutex already */
4279 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4280}
4281
Tejun Heo42809dd2012-11-19 08:13:37 -08004282static int cgroup_destroy_locked(struct cgroup *cgrp)
4283 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004284{
Tejun Heo42809dd2012-11-19 08:13:37 -08004285 struct dentry *d = cgrp->dentry;
4286 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004287 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004288 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004289
Tejun Heo42809dd2012-11-19 08:13:37 -08004290 lockdep_assert_held(&d->d_inode->i_mutex);
4291 lockdep_assert_held(&cgroup_mutex);
4292
4293 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004294 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004295
Tejun Heo1a90dd52012-11-05 09:16:59 -08004296 /*
4297 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4298 * removed. This makes future css_tryget() and child creation
4299 * attempts fail thus maintaining the removal conditions verified
4300 * above.
4301 */
Tejun Heoed9577932012-11-05 09:16:58 -08004302 for_each_subsys(cgrp->root, ss) {
4303 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4304
4305 WARN_ON(atomic_read(&css->refcnt) < 0);
4306 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004307 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004308 set_bit(CGRP_REMOVED, &cgrp->flags);
4309
Tejun Heoa31f2d32012-11-19 08:13:37 -08004310 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004311 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004312 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004313
4314 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004315 * Put all the base refs. Each css holds an extra reference to the
4316 * cgroup's dentry and cgroup removal proceeds regardless of css
4317 * refs. On the last put of each css, whenever that may be, the
4318 * extra dentry ref is put so that dentry destruction happens only
4319 * after all css's are released.
4320 */
Tejun Heoe9316082012-11-05 09:16:58 -08004321 for_each_subsys(cgrp->root, ss)
4322 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004323
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004324 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004325 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004326 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004327 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004328
Paul Menage999cd8a2009-01-07 18:08:36 -08004329 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004330 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004331 list_del_init(&cgrp->allcg_node);
4332
Tejun Heo42809dd2012-11-19 08:13:37 -08004333 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004334 cgroup_d_remove_dir(d);
4335 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004336
Paul Menagebd89aab2007-10-18 23:40:44 -07004337 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004338 check_for_release(parent);
4339
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004340 /*
4341 * Unregister events and notify userspace.
4342 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004343 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004344 */
4345 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004346 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004347 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004348 schedule_work(&event->remove);
4349 }
Li Zefan810cbee2013-02-18 18:56:14 +08004350 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004351
Paul Menageddbcc7e2007-10-18 23:39:30 -07004352 return 0;
4353}
4354
Tejun Heo42809dd2012-11-19 08:13:37 -08004355static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4356{
4357 int ret;
4358
4359 mutex_lock(&cgroup_mutex);
4360 ret = cgroup_destroy_locked(dentry->d_fsdata);
4361 mutex_unlock(&cgroup_mutex);
4362
4363 return ret;
4364}
4365
Tejun Heo8e3f6542012-04-01 12:09:55 -07004366static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4367{
4368 INIT_LIST_HEAD(&ss->cftsets);
4369
4370 /*
4371 * base_cftset is embedded in subsys itself, no need to worry about
4372 * deregistration.
4373 */
4374 if (ss->base_cftypes) {
4375 ss->base_cftset.cfts = ss->base_cftypes;
4376 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4377 }
4378}
4379
Li Zefan06a11922008-04-29 01:00:07 -07004380static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004381{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004382 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004383
4384 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004385
Tejun Heo648bb562012-11-19 08:13:36 -08004386 mutex_lock(&cgroup_mutex);
4387
Tejun Heo8e3f6542012-04-01 12:09:55 -07004388 /* init base cftset */
4389 cgroup_init_cftsets(ss);
4390
Paul Menageddbcc7e2007-10-18 23:39:30 -07004391 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004392 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004393 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004394 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004395 /* We don't handle early failures gracefully */
4396 BUG_ON(IS_ERR(css));
4397 init_cgroup_css(css, ss, dummytop);
4398
Li Zefane8d55fd2008-04-29 01:00:13 -07004399 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004400 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004401 * newly registered, all tasks and hence the
4402 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004403 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004404
4405 need_forkexit_callback |= ss->fork || ss->exit;
4406
Li Zefane8d55fd2008-04-29 01:00:13 -07004407 /* At system boot, before all subsystems have been
4408 * registered, no tasks have been forked, so we don't
4409 * need to invoke fork callbacks here. */
4410 BUG_ON(!list_empty(&init_task.tasks));
4411
Tejun Heob1929db2012-11-19 08:13:38 -08004412 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004413
Tejun Heo648bb562012-11-19 08:13:36 -08004414 mutex_unlock(&cgroup_mutex);
4415
Ben Blume6a11052010-03-10 15:22:09 -08004416 /* this function shouldn't be used with modular subsystems, since they
4417 * need to register a subsys_id, among other things */
4418 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004419}
4420
4421/**
Ben Blume6a11052010-03-10 15:22:09 -08004422 * cgroup_load_subsys: load and register a modular subsystem at runtime
4423 * @ss: the subsystem to load
4424 *
4425 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004426 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004427 * up for use. If the subsystem is built-in anyway, work is delegated to the
4428 * simpler cgroup_init_subsys.
4429 */
4430int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4431{
Ben Blume6a11052010-03-10 15:22:09 -08004432 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004433 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004434 struct hlist_node *tmp;
Li Zefan0ac801f2013-01-10 11:49:27 +08004435 struct css_set *cg;
4436 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004437
4438 /* check name and function validity */
4439 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004440 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004441 return -EINVAL;
4442
4443 /*
4444 * we don't support callbacks in modular subsystems. this check is
4445 * before the ss->module check for consistency; a subsystem that could
4446 * be a module should still have no callbacks even if the user isn't
4447 * compiling it as one.
4448 */
4449 if (ss->fork || ss->exit)
4450 return -EINVAL;
4451
4452 /*
4453 * an optionally modular subsystem is built-in: we want to do nothing,
4454 * since cgroup_init_subsys will have already taken care of it.
4455 */
4456 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004457 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004458 BUG_ON(subsys[ss->subsys_id] != ss);
4459 return 0;
4460 }
4461
Tejun Heo8e3f6542012-04-01 12:09:55 -07004462 /* init base cftset */
4463 cgroup_init_cftsets(ss);
4464
Ben Blume6a11052010-03-10 15:22:09 -08004465 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004466 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004467
4468 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004469 * no ss->css_alloc seems to need anything important in the ss
4470 * struct, so this can happen first (i.e. before the rootnode
4471 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004472 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004473 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004474 if (IS_ERR(css)) {
4475 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004476 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004477 mutex_unlock(&cgroup_mutex);
4478 return PTR_ERR(css);
4479 }
4480
4481 list_add(&ss->sibling, &rootnode.subsys_list);
4482 ss->root = &rootnode;
4483
4484 /* our new subsystem will be attached to the dummy hierarchy. */
4485 init_cgroup_css(css, ss, dummytop);
4486 /* init_idr must be after init_cgroup_css because it sets css->id. */
4487 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004488 ret = cgroup_init_idr(ss, css);
4489 if (ret)
4490 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004491 }
4492
4493 /*
4494 * Now we need to entangle the css into the existing css_sets. unlike
4495 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4496 * will need a new pointer to it; done by iterating the css_set_table.
4497 * furthermore, modifying the existing css_sets will corrupt the hash
4498 * table state, so each changed css_set will need its hash recomputed.
4499 * this is all done under the css_set_lock.
4500 */
4501 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004502 hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004503 /* skip entries that we already rehashed */
4504 if (cg->subsys[ss->subsys_id])
4505 continue;
4506 /* remove existing entry */
4507 hash_del(&cg->hlist);
4508 /* set new value */
4509 cg->subsys[ss->subsys_id] = css;
4510 /* recompute hash and restore entry */
4511 key = css_set_hash(cg->subsys);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004512 hash_add(css_set_table, &cg->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004513 }
4514 write_unlock(&css_set_lock);
4515
Tejun Heob1929db2012-11-19 08:13:38 -08004516 ret = online_css(ss, dummytop);
4517 if (ret)
4518 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004519
Ben Blume6a11052010-03-10 15:22:09 -08004520 /* success! */
4521 mutex_unlock(&cgroup_mutex);
4522 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004523
4524err_unload:
4525 mutex_unlock(&cgroup_mutex);
4526 /* @ss can't be mounted here as try_module_get() would fail */
4527 cgroup_unload_subsys(ss);
4528 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004529}
4530EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4531
4532/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004533 * cgroup_unload_subsys: unload a modular subsystem
4534 * @ss: the subsystem to unload
4535 *
4536 * This function should be called in a modular subsystem's exitcall. When this
4537 * function is invoked, the refcount on the subsystem's module will be 0, so
4538 * the subsystem will not be attached to any hierarchy.
4539 */
4540void cgroup_unload_subsys(struct cgroup_subsys *ss)
4541{
4542 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004543
4544 BUG_ON(ss->module == NULL);
4545
4546 /*
4547 * we shouldn't be called if the subsystem is in use, and the use of
4548 * try_module_get in parse_cgroupfs_options should ensure that it
4549 * doesn't start being used while we're killing it off.
4550 */
4551 BUG_ON(ss->root != &rootnode);
4552
4553 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004554
Tejun Heoa31f2d32012-11-19 08:13:37 -08004555 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004556
Tejun Heoc897ff62013-02-27 17:03:49 -08004557 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004558 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004559
Ben Blumcf5d5942010-03-10 15:22:09 -08004560 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004561 subsys[ss->subsys_id] = NULL;
4562
4563 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004564 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004565
4566 /*
4567 * disentangle the css from all css_sets attached to the dummytop. as
4568 * in loading, we need to pay our respects to the hashtable gods.
4569 */
4570 write_lock(&css_set_lock);
4571 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4572 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004573 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004574
Li Zefan0ac801f2013-01-10 11:49:27 +08004575 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004576 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004577 key = css_set_hash(cg->subsys);
4578 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004579 }
4580 write_unlock(&css_set_lock);
4581
4582 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004583 * remove subsystem's css from the dummytop and free it - need to
4584 * free before marking as null because ss->css_free needs the
4585 * cgrp->subsys pointer to find their state. note that this also
4586 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004587 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004588 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004589 dummytop->subsys[ss->subsys_id] = NULL;
4590
4591 mutex_unlock(&cgroup_mutex);
4592}
4593EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4594
4595/**
Li Zefana043e3b2008-02-23 15:24:09 -08004596 * cgroup_init_early - cgroup initialization at system boot
4597 *
4598 * Initialize cgroups at system boot, and initialize any
4599 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004600 */
4601int __init cgroup_init_early(void)
4602{
4603 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004604 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004605 INIT_LIST_HEAD(&init_css_set.cg_links);
4606 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004607 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004608 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004609 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004610 root_count = 1;
4611 init_task.cgroups = &init_css_set;
4612
4613 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004614 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004615 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004616 &rootnode.top_cgroup.css_sets);
4617 list_add(&init_css_set_link.cg_link_list,
4618 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004619
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004620 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004621 struct cgroup_subsys *ss = subsys[i];
4622
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004623 /* at bootup time, we don't worry about modular subsystems */
4624 if (!ss || ss->module)
4625 continue;
4626
Paul Menageddbcc7e2007-10-18 23:39:30 -07004627 BUG_ON(!ss->name);
4628 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004629 BUG_ON(!ss->css_alloc);
4630 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004631 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004632 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004633 ss->name, ss->subsys_id);
4634 BUG();
4635 }
4636
4637 if (ss->early_init)
4638 cgroup_init_subsys(ss);
4639 }
4640 return 0;
4641}
4642
4643/**
Li Zefana043e3b2008-02-23 15:24:09 -08004644 * cgroup_init - cgroup initialization
4645 *
4646 * Register cgroup filesystem and /proc file, and initialize
4647 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004648 */
4649int __init cgroup_init(void)
4650{
4651 int err;
4652 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004653 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004654
4655 err = bdi_init(&cgroup_backing_dev_info);
4656 if (err)
4657 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004658
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004659 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004660 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004661
4662 /* at bootup time, we don't worry about modular subsystems */
4663 if (!ss || ss->module)
4664 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004665 if (!ss->early_init)
4666 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004667 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004668 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004669 }
4670
Li Zefan472b1052008-04-29 01:00:11 -07004671 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004672 key = css_set_hash(init_css_set.subsys);
4673 hash_add(css_set_table, &init_css_set.hlist, key);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004674 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004675
4676 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4677 if (!cgroup_kobj) {
4678 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004679 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004680 }
4681
4682 err = register_filesystem(&cgroup_fs_type);
4683 if (err < 0) {
4684 kobject_put(cgroup_kobj);
4685 goto out;
4686 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004687
Li Zefan46ae2202008-04-29 01:00:08 -07004688 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004689
Paul Menageddbcc7e2007-10-18 23:39:30 -07004690out:
Paul Menagea4243162007-10-18 23:39:35 -07004691 if (err)
4692 bdi_destroy(&cgroup_backing_dev_info);
4693
Paul Menageddbcc7e2007-10-18 23:39:30 -07004694 return err;
4695}
Paul Menageb4f48b62007-10-18 23:39:33 -07004696
Tejun Heoa6647e92013-11-22 17:14:39 -05004697static int __init cgroup_wq_init(void)
4698{
4699 /*
4700 * There isn't much point in executing destruction path in
4701 * parallel. Good chunk is serialized with cgroup_mutex anyway.
4702 * Use 1 for @max_active.
4703 *
4704 * We would prefer to do this in cgroup_init() above, but that
4705 * is called before init_workqueues(): so leave this until after.
4706 */
4707 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
4708 BUG_ON(!cgroup_destroy_wq);
4709 return 0;
4710}
4711core_initcall(cgroup_wq_init);
4712
Paul Menagea4243162007-10-18 23:39:35 -07004713/*
4714 * proc_cgroup_show()
4715 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4716 * - Used for /proc/<pid>/cgroup.
4717 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4718 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004719 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004720 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4721 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4722 * cgroup to top_cgroup.
4723 */
4724
4725/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004726int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004727{
4728 struct pid *pid;
4729 struct task_struct *tsk;
4730 char *buf;
4731 int retval;
4732 struct cgroupfs_root *root;
4733
4734 retval = -ENOMEM;
4735 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4736 if (!buf)
4737 goto out;
4738
4739 retval = -ESRCH;
4740 pid = m->private;
4741 tsk = get_pid_task(pid, PIDTYPE_PID);
4742 if (!tsk)
4743 goto out_free;
4744
4745 retval = 0;
4746
4747 mutex_lock(&cgroup_mutex);
4748
Li Zefane5f6a862009-01-07 18:07:41 -08004749 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004750 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004751 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004752 int count = 0;
4753
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004754 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004755 for_each_subsys(root, ss)
4756 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004757 if (strlen(root->name))
4758 seq_printf(m, "%sname=%s", count ? "," : "",
4759 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004760 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004761 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004762 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004763 if (retval < 0)
4764 goto out_unlock;
4765 seq_puts(m, buf);
4766 seq_putc(m, '\n');
4767 }
4768
4769out_unlock:
4770 mutex_unlock(&cgroup_mutex);
4771 put_task_struct(tsk);
4772out_free:
4773 kfree(buf);
4774out:
4775 return retval;
4776}
4777
Paul Menagea4243162007-10-18 23:39:35 -07004778/* Display information about each subsystem and each hierarchy */
4779static int proc_cgroupstats_show(struct seq_file *m, void *v)
4780{
4781 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004782
Paul Menage8bab8dd2008-04-04 14:29:57 -07004783 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004784 /*
4785 * ideally we don't want subsystems moving around while we do this.
4786 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4787 * subsys/hierarchy state.
4788 */
Paul Menagea4243162007-10-18 23:39:35 -07004789 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004790 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4791 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004792 if (ss == NULL)
4793 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004794 seq_printf(m, "%s\t%d\t%d\t%d\n",
4795 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004796 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004797 }
4798 mutex_unlock(&cgroup_mutex);
4799 return 0;
4800}
4801
4802static int cgroupstats_open(struct inode *inode, struct file *file)
4803{
Al Viro9dce07f2008-03-29 03:07:28 +00004804 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004805}
4806
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004807static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004808 .open = cgroupstats_open,
4809 .read = seq_read,
4810 .llseek = seq_lseek,
4811 .release = single_release,
4812};
4813
Paul Menageb4f48b62007-10-18 23:39:33 -07004814/**
4815 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004816 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004817 *
4818 * Description: A task inherits its parent's cgroup at fork().
4819 *
4820 * A pointer to the shared css_set was automatically copied in
4821 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004822 * it was not made under the protection of RCU or cgroup_mutex, so
4823 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4824 * have already changed current->cgroups, allowing the previously
4825 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004826 *
4827 * At the point that cgroup_fork() is called, 'current' is the parent
4828 * task, and the passed argument 'child' points to the child task.
4829 */
4830void cgroup_fork(struct task_struct *child)
4831{
Tejun Heo9bb71302012-10-18 17:52:07 -07004832 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004833 child->cgroups = current->cgroups;
4834 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004835 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004836 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004837}
4838
4839/**
Li Zefana043e3b2008-02-23 15:24:09 -08004840 * cgroup_post_fork - called on a new task after adding it to the task list
4841 * @child: the task in question
4842 *
Tejun Heo5edee612012-10-16 15:03:14 -07004843 * Adds the task to the list running through its css_set if necessary and
4844 * call the subsystem fork() callbacks. Has to be after the task is
4845 * visible on the task list in case we race with the first call to
4846 * cgroup_iter_start() - to guarantee that the new task ends up on its
4847 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004848 */
Paul Menage817929e2007-10-18 23:39:36 -07004849void cgroup_post_fork(struct task_struct *child)
4850{
Tejun Heo5edee612012-10-16 15:03:14 -07004851 int i;
4852
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004853 /*
4854 * use_task_css_set_links is set to 1 before we walk the tasklist
4855 * under the tasklist_lock and we read it here after we added the child
4856 * to the tasklist under the tasklist_lock as well. If the child wasn't
4857 * yet in the tasklist when we walked through it from
4858 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4859 * should be visible now due to the paired locking and barriers implied
4860 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4861 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4862 * lock on fork.
4863 */
Paul Menage817929e2007-10-18 23:39:36 -07004864 if (use_task_css_set_links) {
4865 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004866 task_lock(child);
4867 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004868 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004869 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004870 write_unlock(&css_set_lock);
4871 }
Tejun Heo5edee612012-10-16 15:03:14 -07004872
4873 /*
4874 * Call ss->fork(). This must happen after @child is linked on
4875 * css_set; otherwise, @child might change state between ->fork()
4876 * and addition to css_set.
4877 */
4878 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004879 /*
4880 * fork/exit callbacks are supported only for builtin
4881 * subsystems, and the builtin section of the subsys
4882 * array is immutable, so we don't need to lock the
4883 * subsys array here. On the other hand, modular section
4884 * of the array can be freed at module unload, so we
4885 * can't touch that.
4886 */
4887 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004888 struct cgroup_subsys *ss = subsys[i];
4889
Tejun Heo5edee612012-10-16 15:03:14 -07004890 if (ss->fork)
4891 ss->fork(child);
4892 }
4893 }
Paul Menage817929e2007-10-18 23:39:36 -07004894}
Tejun Heo5edee612012-10-16 15:03:14 -07004895
Paul Menage817929e2007-10-18 23:39:36 -07004896/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004897 * cgroup_exit - detach cgroup from exiting task
4898 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004899 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004900 *
4901 * Description: Detach cgroup from @tsk and release it.
4902 *
4903 * Note that cgroups marked notify_on_release force every task in
4904 * them to take the global cgroup_mutex mutex when exiting.
4905 * This could impact scaling on very large systems. Be reluctant to
4906 * use notify_on_release cgroups where very high task exit scaling
4907 * is required on large systems.
4908 *
4909 * the_top_cgroup_hack:
4910 *
4911 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4912 *
4913 * We call cgroup_exit() while the task is still competent to
4914 * handle notify_on_release(), then leave the task attached to the
4915 * root cgroup in each hierarchy for the remainder of its exit.
4916 *
4917 * To do this properly, we would increment the reference count on
4918 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4919 * code we would add a second cgroup function call, to drop that
4920 * reference. This would just create an unnecessary hot spot on
4921 * the top_cgroup reference count, to no avail.
4922 *
4923 * Normally, holding a reference to a cgroup without bumping its
4924 * count is unsafe. The cgroup could go away, or someone could
4925 * attach us to a different cgroup, decrementing the count on
4926 * the first cgroup that we never incremented. But in this case,
4927 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004928 * which wards off any cgroup_attach_task() attempts, or task is a failed
4929 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004930 */
4931void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4932{
Paul Menage817929e2007-10-18 23:39:36 -07004933 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004934 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004935
4936 /*
4937 * Unlink from the css_set task list if necessary.
4938 * Optimistically check cg_list before taking
4939 * css_set_lock
4940 */
4941 if (!list_empty(&tsk->cg_list)) {
4942 write_lock(&css_set_lock);
4943 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004944 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004945 write_unlock(&css_set_lock);
4946 }
4947
Paul Menageb4f48b62007-10-18 23:39:33 -07004948 /* Reassign the task to the init_css_set. */
4949 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004950 cg = tsk->cgroups;
4951 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004952
4953 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004954 /*
4955 * fork/exit callbacks are supported only for builtin
4956 * subsystems, see cgroup_post_fork() for details.
4957 */
4958 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004959 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004960
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004961 if (ss->exit) {
4962 struct cgroup *old_cgrp =
4963 rcu_dereference_raw(cg->subsys[i])->cgroup;
4964 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004965 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004966 }
4967 }
4968 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004969 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004970
Li Zefanb5d646f2013-01-24 14:43:51 +08004971 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004972}
Paul Menage697f4162007-10-18 23:39:34 -07004973
Paul Menagebd89aab2007-10-18 23:40:44 -07004974static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004975{
4976 /* All of these checks rely on RCU to keep the cgroup
4977 * structure alive */
Li Zefanf50daa72013-03-01 15:06:07 +08004978 if (cgroup_is_releasable(cgrp) &&
4979 !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) {
4980 /*
4981 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004982 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004983 * it now
4984 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004985 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004986
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004987 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004988 if (!cgroup_is_removed(cgrp) &&
4989 list_empty(&cgrp->release_list)) {
4990 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004991 need_schedule_work = 1;
4992 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004993 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004994 if (need_schedule_work)
4995 schedule_work(&release_agent_work);
4996 }
4997}
4998
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004999/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005000bool __css_tryget(struct cgroup_subsys_state *css)
5001{
Tejun Heoe9316082012-11-05 09:16:58 -08005002 while (true) {
5003 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005004
Tejun Heoe9316082012-11-05 09:16:58 -08005005 v = css_refcnt(css);
5006 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5007 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005008 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005009 else if (t < 0)
5010 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005011 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005012 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005013}
5014EXPORT_SYMBOL_GPL(__css_tryget);
5015
5016/* Caller must verify that the css is not for root cgroup */
5017void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005018{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005019 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005020
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005021 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08005022 if (v == 0)
Tejun Heoa6647e92013-11-22 17:14:39 -05005023 queue_work(cgroup_destroy_wq, &css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005024}
Ben Blum67523c42010-03-10 15:22:11 -08005025EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005026
5027/*
5028 * Notify userspace when a cgroup is released, by running the
5029 * configured release agent with the name of the cgroup (path
5030 * relative to the root of cgroup file system) as the argument.
5031 *
5032 * Most likely, this user command will try to rmdir this cgroup.
5033 *
5034 * This races with the possibility that some other task will be
5035 * attached to this cgroup before it is removed, or that some other
5036 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5037 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5038 * unused, and this cgroup will be reprieved from its death sentence,
5039 * to continue to serve a useful existence. Next time it's released,
5040 * we will get notified again, if it still has 'notify_on_release' set.
5041 *
5042 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5043 * means only wait until the task is successfully execve()'d. The
5044 * separate release agent task is forked by call_usermodehelper(),
5045 * then control in this thread returns here, without waiting for the
5046 * release agent task. We don't bother to wait because the caller of
5047 * this routine has no use for the exit status of the release agent
5048 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005049 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005050static void cgroup_release_agent(struct work_struct *work)
5051{
5052 BUG_ON(work != &release_agent_work);
5053 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005054 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005055 while (!list_empty(&release_list)) {
5056 char *argv[3], *envp[3];
5057 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005058 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005059 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005060 struct cgroup,
5061 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005062 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005063 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005064 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005065 if (!pathbuf)
5066 goto continue_free;
5067 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5068 goto continue_free;
5069 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5070 if (!agentbuf)
5071 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005072
5073 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005074 argv[i++] = agentbuf;
5075 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005076 argv[i] = NULL;
5077
5078 i = 0;
5079 /* minimal command environment */
5080 envp[i++] = "HOME=/";
5081 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5082 envp[i] = NULL;
5083
5084 /* Drop the lock while we invoke the usermode helper,
5085 * since the exec could involve hitting disk and hence
5086 * be a slow process */
5087 mutex_unlock(&cgroup_mutex);
5088 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005089 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005090 continue_free:
5091 kfree(pathbuf);
5092 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005093 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005094 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005095 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005096 mutex_unlock(&cgroup_mutex);
5097}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005098
5099static int __init cgroup_disable(char *str)
5100{
5101 int i;
5102 char *token;
5103
5104 while ((token = strsep(&str, ",")) != NULL) {
5105 if (!*token)
5106 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005107 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005108 struct cgroup_subsys *ss = subsys[i];
5109
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005110 /*
5111 * cgroup_disable, being at boot time, can't
5112 * know about module subsystems, so we don't
5113 * worry about them.
5114 */
5115 if (!ss || ss->module)
5116 continue;
5117
Paul Menage8bab8dd2008-04-04 14:29:57 -07005118 if (!strcmp(token, ss->name)) {
5119 ss->disabled = 1;
5120 printk(KERN_INFO "Disabling %s control group"
5121 " subsystem\n", ss->name);
5122 break;
5123 }
5124 }
5125 }
5126 return 1;
5127}
5128__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005129
5130/*
5131 * Functons for CSS ID.
5132 */
5133
5134/*
5135 *To get ID other than 0, this should be called when !cgroup_is_removed().
5136 */
5137unsigned short css_id(struct cgroup_subsys_state *css)
5138{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005139 struct css_id *cssid;
5140
5141 /*
5142 * This css_id() can return correct value when somone has refcnt
5143 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5144 * it's unchanged until freed.
5145 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005146 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005147
5148 if (cssid)
5149 return cssid->id;
5150 return 0;
5151}
Ben Blum67523c42010-03-10 15:22:11 -08005152EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005153
5154unsigned short css_depth(struct cgroup_subsys_state *css)
5155{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005156 struct css_id *cssid;
5157
Tejun Heo28b4c272012-04-01 12:09:56 -07005158 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005159
5160 if (cssid)
5161 return cssid->depth;
5162 return 0;
5163}
Ben Blum67523c42010-03-10 15:22:11 -08005164EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005165
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005166/**
5167 * css_is_ancestor - test "root" css is an ancestor of "child"
5168 * @child: the css to be tested.
5169 * @root: the css supporsed to be an ancestor of the child.
5170 *
5171 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005172 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005173 * But, considering usual usage, the csses should be valid objects after test.
5174 * Assuming that the caller will do some action to the child if this returns
5175 * returns true, the caller must take "child";s reference count.
5176 * If "child" is valid object and this returns true, "root" is valid, too.
5177 */
5178
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005179bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005180 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005181{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005182 struct css_id *child_id;
5183 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005184
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005185 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005186 if (!child_id)
5187 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005188 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005189 if (!root_id)
5190 return false;
5191 if (child_id->depth < root_id->depth)
5192 return false;
5193 if (child_id->stack[root_id->depth] != root_id->id)
5194 return false;
5195 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005196}
5197
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005198void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5199{
5200 struct css_id *id = css->id;
5201 /* When this is called before css_id initialization, id can be NULL */
5202 if (!id)
5203 return;
5204
5205 BUG_ON(!ss->use_id);
5206
5207 rcu_assign_pointer(id->css, NULL);
5208 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005209 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005210 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005211 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005212 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005213}
Ben Blum67523c42010-03-10 15:22:11 -08005214EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005215
5216/*
5217 * This is called by init or create(). Then, calls to this function are
5218 * always serialized (By cgroup_mutex() at create()).
5219 */
5220
5221static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5222{
5223 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005224 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005225
5226 BUG_ON(!ss->use_id);
5227
5228 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5229 newid = kzalloc(size, GFP_KERNEL);
5230 if (!newid)
5231 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005232
5233 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005234 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005235 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005236 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005237 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005238 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005239
5240 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005241 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005242 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005243
Tejun Heod228d9e2013-02-27 17:04:54 -08005244 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005245 newid->depth = depth;
5246 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005247err_out:
5248 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005249 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005250
5251}
5252
Ben Blume6a11052010-03-10 15:22:09 -08005253static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5254 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005255{
5256 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005257
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005258 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005259 idr_init(&ss->idr);
5260
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005261 newid = get_new_cssid(ss, 0);
5262 if (IS_ERR(newid))
5263 return PTR_ERR(newid);
5264
5265 newid->stack[0] = newid->id;
5266 newid->css = rootcss;
5267 rootcss->id = newid;
5268 return 0;
5269}
5270
5271static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5272 struct cgroup *child)
5273{
5274 int subsys_id, i, depth = 0;
5275 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005276 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005277
5278 subsys_id = ss->subsys_id;
5279 parent_css = parent->subsys[subsys_id];
5280 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005281 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005282 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005283
5284 child_id = get_new_cssid(ss, depth);
5285 if (IS_ERR(child_id))
5286 return PTR_ERR(child_id);
5287
5288 for (i = 0; i < depth; i++)
5289 child_id->stack[i] = parent_id->stack[i];
5290 child_id->stack[depth] = child_id->id;
5291 /*
5292 * child_id->css pointer will be set after this cgroup is available
5293 * see cgroup_populate_dir()
5294 */
5295 rcu_assign_pointer(child_css->id, child_id);
5296
5297 return 0;
5298}
5299
5300/**
5301 * css_lookup - lookup css by id
5302 * @ss: cgroup subsys to be looked into.
5303 * @id: the id
5304 *
5305 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5306 * NULL if not. Should be called under rcu_read_lock()
5307 */
5308struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5309{
5310 struct css_id *cssid = NULL;
5311
5312 BUG_ON(!ss->use_id);
5313 cssid = idr_find(&ss->idr, id);
5314
5315 if (unlikely(!cssid))
5316 return NULL;
5317
5318 return rcu_dereference(cssid->css);
5319}
Ben Blum67523c42010-03-10 15:22:11 -08005320EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005321
Stephane Eraniane5d13672011-02-14 11:20:01 +02005322/*
5323 * get corresponding css from file open on cgroupfs directory
5324 */
5325struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5326{
5327 struct cgroup *cgrp;
5328 struct inode *inode;
5329 struct cgroup_subsys_state *css;
5330
Al Viro496ad9a2013-01-23 17:07:38 -05005331 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005332 /* check in cgroup filesystem dir */
5333 if (inode->i_op != &cgroup_dir_inode_operations)
5334 return ERR_PTR(-EBADF);
5335
5336 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5337 return ERR_PTR(-EINVAL);
5338
5339 /* get cgroup */
5340 cgrp = __d_cgrp(f->f_dentry);
5341 css = cgrp->subsys[id];
5342 return css ? css : ERR_PTR(-ENOENT);
5343}
5344
Paul Menagefe693432009-09-23 15:56:20 -07005345#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005346static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005347{
5348 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5349
5350 if (!css)
5351 return ERR_PTR(-ENOMEM);
5352
5353 return css;
5354}
5355
Tejun Heo92fb9742012-11-19 08:13:38 -08005356static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005357{
5358 kfree(cont->subsys[debug_subsys_id]);
5359}
5360
5361static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5362{
5363 return atomic_read(&cont->count);
5364}
5365
5366static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5367{
5368 return cgroup_task_count(cont);
5369}
5370
5371static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5372{
5373 return (u64)(unsigned long)current->cgroups;
5374}
5375
5376static u64 current_css_set_refcount_read(struct cgroup *cont,
5377 struct cftype *cft)
5378{
5379 u64 count;
5380
5381 rcu_read_lock();
5382 count = atomic_read(&current->cgroups->refcount);
5383 rcu_read_unlock();
5384 return count;
5385}
5386
Paul Menage7717f7b2009-09-23 15:56:22 -07005387static int current_css_set_cg_links_read(struct cgroup *cont,
5388 struct cftype *cft,
5389 struct seq_file *seq)
5390{
5391 struct cg_cgroup_link *link;
5392 struct css_set *cg;
5393
5394 read_lock(&css_set_lock);
5395 rcu_read_lock();
5396 cg = rcu_dereference(current->cgroups);
5397 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5398 struct cgroup *c = link->cgrp;
5399 const char *name;
5400
5401 if (c->dentry)
5402 name = c->dentry->d_name.name;
5403 else
5404 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005405 seq_printf(seq, "Root %d group %s\n",
5406 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005407 }
5408 rcu_read_unlock();
5409 read_unlock(&css_set_lock);
5410 return 0;
5411}
5412
5413#define MAX_TASKS_SHOWN_PER_CSS 25
5414static int cgroup_css_links_read(struct cgroup *cont,
5415 struct cftype *cft,
5416 struct seq_file *seq)
5417{
5418 struct cg_cgroup_link *link;
5419
5420 read_lock(&css_set_lock);
5421 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5422 struct css_set *cg = link->cg;
5423 struct task_struct *task;
5424 int count = 0;
5425 seq_printf(seq, "css_set %p\n", cg);
5426 list_for_each_entry(task, &cg->tasks, cg_list) {
5427 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5428 seq_puts(seq, " ...\n");
5429 break;
5430 } else {
5431 seq_printf(seq, " task %d\n",
5432 task_pid_vnr(task));
5433 }
5434 }
5435 }
5436 read_unlock(&css_set_lock);
5437 return 0;
5438}
5439
Paul Menagefe693432009-09-23 15:56:20 -07005440static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5441{
5442 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5443}
5444
5445static struct cftype debug_files[] = {
5446 {
5447 .name = "cgroup_refcount",
5448 .read_u64 = cgroup_refcount_read,
5449 },
5450 {
5451 .name = "taskcount",
5452 .read_u64 = debug_taskcount_read,
5453 },
5454
5455 {
5456 .name = "current_css_set",
5457 .read_u64 = current_css_set_read,
5458 },
5459
5460 {
5461 .name = "current_css_set_refcount",
5462 .read_u64 = current_css_set_refcount_read,
5463 },
5464
5465 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005466 .name = "current_css_set_cg_links",
5467 .read_seq_string = current_css_set_cg_links_read,
5468 },
5469
5470 {
5471 .name = "cgroup_css_links",
5472 .read_seq_string = cgroup_css_links_read,
5473 },
5474
5475 {
Paul Menagefe693432009-09-23 15:56:20 -07005476 .name = "releasable",
5477 .read_u64 = releasable_read,
5478 },
Paul Menagefe693432009-09-23 15:56:20 -07005479
Tejun Heo4baf6e32012-04-01 12:09:55 -07005480 { } /* terminate */
5481};
Paul Menagefe693432009-09-23 15:56:20 -07005482
5483struct cgroup_subsys debug_subsys = {
5484 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005485 .css_alloc = debug_css_alloc,
5486 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005487 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005488 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005489};
5490#endif /* CONFIG_CGROUP_DEBUG */