blob: 2e9b387971d1760452027c052bd0a32edde90a83 [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/*
95 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020096 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080097 * registered after that. The mutable section of this array is protected by
98 * cgroup_mutex.
99 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200100#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200101#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -0800102static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103#include <linux/cgroup_subsys.h>
104};
105
Paul Menageddbcc7e2007-10-18 23:39:30 -0700106/*
107 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
108 * subsystems that are otherwise unattached - it never has more than a
109 * single cgroup, and all tasks are part of that cgroup.
110 */
111static struct cgroupfs_root rootnode;
112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700120
121 /* file xattrs */
122 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123};
124
125/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
128 */
129#define CSS_ID_MAX (65535)
130struct css_id {
131 /*
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100138 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 /*
140 * ID of this css.
141 */
142 unsigned short id;
143 /*
144 * Depth in hierarchy which this ID belongs to.
145 */
146 unsigned short depth;
147 /*
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
149 */
150 struct rcu_head rcu_head;
151 /*
152 * Hierarchy of CSS ID belongs to.
153 */
154 unsigned short stack[0]; /* Array of Length (depth+1) */
155};
156
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800157/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159 */
160struct cgroup_event {
161 /*
162 * Cgroup which the event belongs to.
163 */
164 struct cgroup *cgrp;
165 /*
166 * Control file which the event associated.
167 */
168 struct cftype *cft;
169 /*
170 * eventfd to signal userspace about the event.
171 */
172 struct eventfd_ctx *eventfd;
173 /*
174 * Each of these stored in a list by the cgroup.
175 */
176 struct list_head list;
177 /*
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
180 */
181 poll_table pt;
182 wait_queue_head_t *wqh;
183 wait_queue_t wait;
184 struct work_struct remove;
185};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* The list of hierarchy roots */
188
189static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700190static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700192static DEFINE_IDA(hierarchy_ida);
193static int next_hierarchy_id;
194static DEFINE_SPINLOCK(hierarchy_id_lock);
195
Paul Menageddbcc7e2007-10-18 23:39:30 -0700196/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
197#define dummytop (&rootnode.top_cgroup)
198
Li Zefan65dff752013-03-01 15:01:56 +0800199static struct cgroup_name root_cgroup_name = { .name = "/" };
200
Paul Menageddbcc7e2007-10-18 23:39:30 -0700201/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800202 * check for fork/exit handlers to call. This avoids us having to do
203 * extra work in the fork/exit path if none of the subsystems need to
204 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700205 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700206static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700207
Tejun Heo42809dd2012-11-19 08:13:37 -0800208static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800209static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
210 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800211
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700212static int css_unbias_refcnt(int refcnt)
213{
214 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
215}
216
Tejun Heo28b4c272012-04-01 12:09:56 -0700217/* the current nr of refs, always >= 0 whether @css is deactivated or not */
218static int css_refcnt(struct cgroup_subsys_state *css)
219{
220 int v = atomic_read(&css->refcnt);
221
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700222 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700223}
224
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700226inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700227{
Paul Menagebd89aab2007-10-18 23:40:44 -0700228 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229}
230
Li Zefan78574cf2013-04-08 19:00:38 -0700231/**
232 * cgroup_is_descendant - test ancestry
233 * @cgrp: the cgroup to be tested
234 * @ancestor: possible ancestor of @cgrp
235 *
236 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
237 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
238 * and @ancestor are accessible.
239 */
240bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
241{
242 while (cgrp) {
243 if (cgrp == ancestor)
244 return true;
245 cgrp = cgrp->parent;
246 }
247 return false;
248}
249EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700250
Adrian Bunke9685a02008-02-07 00:13:46 -0800251static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700252{
253 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700254 (1 << CGRP_RELEASABLE) |
255 (1 << CGRP_NOTIFY_ON_RELEASE);
256 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700257}
258
Adrian Bunke9685a02008-02-07 00:13:46 -0800259static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260{
Paul Menagebd89aab2007-10-18 23:40:44 -0700261 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700262}
263
Paul Menageddbcc7e2007-10-18 23:39:30 -0700264/*
265 * for_each_subsys() allows you to iterate on each subsystem attached to
266 * an active hierarchy
267 */
268#define for_each_subsys(_root, _ss) \
269list_for_each_entry(_ss, &_root->subsys_list, sibling)
270
Li Zefane5f6a862009-01-07 18:07:41 -0800271/* for_each_active_root() allows you to iterate across the active hierarchies */
272#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700273list_for_each_entry(_root, &roots, root_list)
274
Tejun Heof6ea9372012-04-01 12:09:55 -0700275static inline struct cgroup *__d_cgrp(struct dentry *dentry)
276{
277 return dentry->d_fsdata;
278}
279
Tejun Heo05ef1d72012-04-01 12:09:56 -0700280static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700281{
282 return dentry->d_fsdata;
283}
284
Tejun Heo05ef1d72012-04-01 12:09:56 -0700285static inline struct cftype *__d_cft(struct dentry *dentry)
286{
287 return __d_cfe(dentry)->type;
288}
289
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700290/**
291 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
292 * @cgrp: the cgroup to be checked for liveness
293 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700294 * On success, returns true; the mutex should be later unlocked. On
295 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700296 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700297static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700298{
299 mutex_lock(&cgroup_mutex);
300 if (cgroup_is_removed(cgrp)) {
301 mutex_unlock(&cgroup_mutex);
302 return false;
303 }
304 return true;
305}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700306
Paul Menage81a6a5c2007-10-18 23:39:38 -0700307/* the list of cgroups eligible for automatic release. Protected by
308 * release_list_lock */
309static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200310static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700311static void cgroup_release_agent(struct work_struct *work);
312static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700313static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700314
Paul Menage817929e2007-10-18 23:39:36 -0700315/* Link structure for associating css_set objects with cgroups */
316struct cg_cgroup_link {
317 /*
318 * List running through cg_cgroup_links associated with a
319 * cgroup, anchored on cgroup->css_sets
320 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700321 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700322 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700323 /*
324 * List running through cg_cgroup_links pointing at a
325 * single css_set object, anchored on css_set->cg_links
326 */
327 struct list_head cg_link_list;
328 struct css_set *cg;
329};
330
331/* The default css_set - used by init and its children prior to any
332 * hierarchies being mounted. It contains a pointer to the root state
333 * for each subsystem. Also used to anchor the list of css_sets. Not
334 * reference-counted, to improve performance when child cgroups
335 * haven't been created.
336 */
337
338static struct css_set init_css_set;
339static struct cg_cgroup_link init_css_set_link;
340
Ben Blume6a11052010-03-10 15:22:09 -0800341static int cgroup_init_idr(struct cgroup_subsys *ss,
342 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700343
Paul Menage817929e2007-10-18 23:39:36 -0700344/* css_set_lock protects the list of css_set objects, and the
345 * chain of tasks off each css_set. Nests outside task->alloc_lock
346 * due to cgroup_iter_start() */
347static DEFINE_RWLOCK(css_set_lock);
348static int css_set_count;
349
Paul Menage7717f7b2009-09-23 15:56:22 -0700350/*
351 * hash table for cgroup groups. This improves the performance to find
352 * an existing css_set. This hash doesn't (currently) take into
353 * account cgroups in empty hierarchies.
354 */
Li Zefan472b1052008-04-29 01:00:11 -0700355#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800356static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700357
Li Zefan0ac801f2013-01-10 11:49:27 +0800358static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700359{
360 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800361 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700362
363 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800364 key += (unsigned long)css[i];
365 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700366
Li Zefan0ac801f2013-01-10 11:49:27 +0800367 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700368}
369
Paul Menage817929e2007-10-18 23:39:36 -0700370/* We don't maintain the lists running through each css_set to its
371 * task until after the first call to cgroup_iter_start(). This
372 * reduces the fork()/exit() overhead for people who have cgroups
373 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700374static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700375
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700376static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700377{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700378 struct cg_cgroup_link *link;
379 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700380 /*
381 * Ensure that the refcount doesn't hit zero while any readers
382 * can see it. Similar to atomic_dec_and_lock(), but for an
383 * rwlock
384 */
385 if (atomic_add_unless(&cg->refcount, -1, 1))
386 return;
387 write_lock(&css_set_lock);
388 if (!atomic_dec_and_test(&cg->refcount)) {
389 write_unlock(&css_set_lock);
390 return;
391 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700392
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700393 /* This css_set is dead. unlink it and release cgroup refcounts */
Li Zefan0ac801f2013-01-10 11:49:27 +0800394 hash_del(&cg->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700395 css_set_count--;
396
397 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
398 cg_link_list) {
399 struct cgroup *cgrp = link->cgrp;
400 list_del(&link->cg_link_list);
401 list_del(&link->cgrp_link_list);
Li Zefan71b57072013-01-24 14:43:28 +0800402
403 /*
404 * We may not be holding cgroup_mutex, and if cgrp->count is
405 * dropped to 0 the cgroup can be destroyed at any time, hence
406 * rcu_read_lock is used to keep it alive.
407 */
408 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -0700409 if (atomic_dec_and_test(&cgrp->count) &&
410 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700411 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700412 set_bit(CGRP_RELEASABLE, &cgrp->flags);
413 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700414 }
Li Zefan71b57072013-01-24 14:43:28 +0800415 rcu_read_unlock();
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700416
417 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700418 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700419
420 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800421 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700422}
423
424/*
425 * refcounted get/put for css_set objects
426 */
427static inline void get_css_set(struct css_set *cg)
428{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700429 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700430}
431
432static inline void put_css_set(struct css_set *cg)
433{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700434 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700435}
436
Paul Menage81a6a5c2007-10-18 23:39:38 -0700437static inline void put_css_set_taskexit(struct css_set *cg)
438{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700439 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700440}
441
Paul Menage817929e2007-10-18 23:39:36 -0700442/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700443 * compare_css_sets - helper function for find_existing_css_set().
444 * @cg: candidate css_set being tested
445 * @old_cg: existing css_set for a task
446 * @new_cgrp: cgroup that's being entered by the task
447 * @template: desired set of css pointers in css_set (pre-calculated)
448 *
449 * Returns true if "cg" matches "old_cg" except for the hierarchy
450 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
451 */
452static bool compare_css_sets(struct css_set *cg,
453 struct css_set *old_cg,
454 struct cgroup *new_cgrp,
455 struct cgroup_subsys_state *template[])
456{
457 struct list_head *l1, *l2;
458
459 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
460 /* Not all subsystems matched */
461 return false;
462 }
463
464 /*
465 * Compare cgroup pointers in order to distinguish between
466 * different cgroups in heirarchies with no subsystems. We
467 * could get by with just this check alone (and skip the
468 * memcmp above) but on most setups the memcmp check will
469 * avoid the need for this more expensive check on almost all
470 * candidates.
471 */
472
473 l1 = &cg->cg_links;
474 l2 = &old_cg->cg_links;
475 while (1) {
476 struct cg_cgroup_link *cgl1, *cgl2;
477 struct cgroup *cg1, *cg2;
478
479 l1 = l1->next;
480 l2 = l2->next;
481 /* See if we reached the end - both lists are equal length. */
482 if (l1 == &cg->cg_links) {
483 BUG_ON(l2 != &old_cg->cg_links);
484 break;
485 } else {
486 BUG_ON(l2 == &old_cg->cg_links);
487 }
488 /* Locate the cgroups associated with these links. */
489 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
490 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
491 cg1 = cgl1->cgrp;
492 cg2 = cgl2->cgrp;
493 /* Hierarchies should be linked in the same order. */
494 BUG_ON(cg1->root != cg2->root);
495
496 /*
497 * If this hierarchy is the hierarchy of the cgroup
498 * that's changing, then we need to check that this
499 * css_set points to the new cgroup; if it's any other
500 * hierarchy, then this css_set should point to the
501 * same cgroup as the old css_set.
502 */
503 if (cg1->root == new_cgrp->root) {
504 if (cg1 != new_cgrp)
505 return false;
506 } else {
507 if (cg1 != cg2)
508 return false;
509 }
510 }
511 return true;
512}
513
514/*
Paul Menage817929e2007-10-18 23:39:36 -0700515 * find_existing_css_set() is a helper for
516 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700517 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700518 *
519 * oldcg: the cgroup group that we're using before the cgroup
520 * transition
521 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700522 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700523 *
524 * template: location in which to build the desired set of subsystem
525 * state objects for the new cgroup group
526 */
Paul Menage817929e2007-10-18 23:39:36 -0700527static struct css_set *find_existing_css_set(
528 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700529 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700530 struct cgroup_subsys_state *template[])
531{
532 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700533 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700534 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800535 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700536
Ben Blumaae8aab2010-03-10 15:22:07 -0800537 /*
538 * Build the set of subsystem state objects that we want to see in the
539 * new css_set. while subsystems can change globally, the entries here
540 * won't change, so no need for locking.
541 */
Paul Menage817929e2007-10-18 23:39:36 -0700542 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400543 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700544 /* Subsystem is in this hierarchy. So we want
545 * the subsystem state from the new
546 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700547 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700548 } else {
549 /* Subsystem is not in this hierarchy, so we
550 * don't want to change the subsystem state */
551 template[i] = oldcg->subsys[i];
552 }
553 }
554
Li Zefan0ac801f2013-01-10 11:49:27 +0800555 key = css_set_hash(template);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800556 hash_for_each_possible(css_set_table, cg, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700557 if (!compare_css_sets(cg, oldcg, cgrp, template))
558 continue;
559
560 /* This css_set matches what we need */
561 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700562 }
Paul Menage817929e2007-10-18 23:39:36 -0700563
564 /* No existing cgroup group matched */
565 return NULL;
566}
567
Paul Menage817929e2007-10-18 23:39:36 -0700568static void free_cg_links(struct list_head *tmp)
569{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700570 struct cg_cgroup_link *link;
571 struct cg_cgroup_link *saved_link;
572
573 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700574 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700575 kfree(link);
576 }
577}
578
579/*
Li Zefan36553432008-07-29 22:33:19 -0700580 * allocate_cg_links() allocates "count" cg_cgroup_link structures
581 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
582 * success or a negative error
583 */
584static int allocate_cg_links(int count, struct list_head *tmp)
585{
586 struct cg_cgroup_link *link;
587 int i;
588 INIT_LIST_HEAD(tmp);
589 for (i = 0; i < count; i++) {
590 link = kmalloc(sizeof(*link), GFP_KERNEL);
591 if (!link) {
592 free_cg_links(tmp);
593 return -ENOMEM;
594 }
595 list_add(&link->cgrp_link_list, tmp);
596 }
597 return 0;
598}
599
Li Zefanc12f65d2009-01-07 18:07:42 -0800600/**
601 * link_css_set - a helper function to link a css_set to a cgroup
602 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
603 * @cg: the css_set to be linked
604 * @cgrp: the destination cgroup
605 */
606static void link_css_set(struct list_head *tmp_cg_links,
607 struct css_set *cg, struct cgroup *cgrp)
608{
609 struct cg_cgroup_link *link;
610
611 BUG_ON(list_empty(tmp_cg_links));
612 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
613 cgrp_link_list);
614 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700615 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700616 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800617 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700618 /*
619 * Always add links to the tail of the list so that the list
620 * is sorted by order of hierarchy creation
621 */
622 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800623}
624
Li Zefan36553432008-07-29 22:33:19 -0700625/*
Paul Menage817929e2007-10-18 23:39:36 -0700626 * find_css_set() takes an existing cgroup group and a
627 * cgroup object, and returns a css_set object that's
628 * equivalent to the old group, but with the given cgroup
629 * substituted into the appropriate hierarchy. Must be called with
630 * cgroup_mutex held
631 */
Paul Menage817929e2007-10-18 23:39:36 -0700632static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700633 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700634{
635 struct css_set *res;
636 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700637
638 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700639
Paul Menage7717f7b2009-09-23 15:56:22 -0700640 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800641 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700642
Paul Menage817929e2007-10-18 23:39:36 -0700643 /* First see if we already have a cgroup group that matches
644 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700645 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700646 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700647 if (res)
648 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700649 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700650
651 if (res)
652 return res;
653
654 res = kmalloc(sizeof(*res), GFP_KERNEL);
655 if (!res)
656 return NULL;
657
658 /* Allocate all the cg_cgroup_link objects that we'll need */
659 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
660 kfree(res);
661 return NULL;
662 }
663
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700664 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700665 INIT_LIST_HEAD(&res->cg_links);
666 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700667 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700668
669 /* Copy the set of subsystem state objects generated in
670 * find_existing_css_set() */
671 memcpy(res->subsys, template, sizeof(res->subsys));
672
673 write_lock(&css_set_lock);
674 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700675 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
676 struct cgroup *c = link->cgrp;
677 if (c->root == cgrp->root)
678 c = cgrp;
679 link_css_set(&tmp_cg_links, res, c);
680 }
Paul Menage817929e2007-10-18 23:39:36 -0700681
682 BUG_ON(!list_empty(&tmp_cg_links));
683
Paul Menage817929e2007-10-18 23:39:36 -0700684 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700685
686 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800687 key = css_set_hash(res->subsys);
688 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700689
Paul Menage817929e2007-10-18 23:39:36 -0700690 write_unlock(&css_set_lock);
691
692 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700693}
694
Paul Menageddbcc7e2007-10-18 23:39:30 -0700695/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700696 * Return the cgroup for "task" from the given hierarchy. Must be
697 * called with cgroup_mutex held.
698 */
699static struct cgroup *task_cgroup_from_root(struct task_struct *task,
700 struct cgroupfs_root *root)
701{
702 struct css_set *css;
703 struct cgroup *res = NULL;
704
705 BUG_ON(!mutex_is_locked(&cgroup_mutex));
706 read_lock(&css_set_lock);
707 /*
708 * No need to lock the task - since we hold cgroup_mutex the
709 * task can't change groups, so the only thing that can happen
710 * is that it exits and its css is set back to init_css_set.
711 */
712 css = task->cgroups;
713 if (css == &init_css_set) {
714 res = &root->top_cgroup;
715 } else {
716 struct cg_cgroup_link *link;
717 list_for_each_entry(link, &css->cg_links, cg_link_list) {
718 struct cgroup *c = link->cgrp;
719 if (c->root == root) {
720 res = c;
721 break;
722 }
723 }
724 }
725 read_unlock(&css_set_lock);
726 BUG_ON(!res);
727 return res;
728}
729
730/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700731 * There is one global cgroup mutex. We also require taking
732 * task_lock() when dereferencing a task's cgroup subsys pointers.
733 * See "The task_lock() exception", at the end of this comment.
734 *
735 * A task must hold cgroup_mutex to modify cgroups.
736 *
737 * Any task can increment and decrement the count field without lock.
738 * So in general, code holding cgroup_mutex can't rely on the count
739 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800740 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700741 * means that no tasks are currently attached, therefore there is no
742 * way a task attached to that cgroup can fork (the other way to
743 * increment the count). So code holding cgroup_mutex can safely
744 * assume that if the count is zero, it will stay zero. Similarly, if
745 * a task holds cgroup_mutex on a cgroup with zero count, it
746 * knows that the cgroup won't be removed, as cgroup_rmdir()
747 * needs that mutex.
748 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700749 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
750 * (usually) take cgroup_mutex. These are the two most performance
751 * critical pieces of code here. The exception occurs on cgroup_exit(),
752 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
753 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800754 * to the release agent with the name of the cgroup (path relative to
755 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700756 *
757 * A cgroup can only be deleted if both its 'count' of using tasks
758 * is zero, and its list of 'children' cgroups is empty. Since all
759 * tasks in the system use _some_ cgroup, and since there is always at
760 * least one task in the system (init, pid == 1), therefore, top_cgroup
761 * always has either children cgroups and/or using tasks. So we don't
762 * need a special hack to ensure that top_cgroup cannot be deleted.
763 *
764 * The task_lock() exception
765 *
766 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800767 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800768 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700769 * several performance critical places that need to reference
770 * task->cgroup without the expense of grabbing a system global
771 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800772 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
774 * the task_struct routinely used for such matters.
775 *
776 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800777 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700778 */
779
Paul Menageddbcc7e2007-10-18 23:39:30 -0700780/*
781 * A couple of forward declarations required, due to cyclic reference loop:
782 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
783 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
784 * -> cgroup_mkdir.
785 */
786
Al Viro18bb1db2011-07-26 01:41:39 -0400787static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400788static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700789static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400790static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
791 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700792static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700793static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700794
795static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200796 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700797 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700798};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700800static int alloc_css_id(struct cgroup_subsys *ss,
801 struct cgroup *parent, struct cgroup *child);
802
Al Viroa5e7ed32011-07-26 01:55:55 -0400803static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700804{
805 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806
807 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400808 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100810 inode->i_uid = current_fsuid();
811 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700812 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
813 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
814 }
815 return inode;
816}
817
Li Zefan65dff752013-03-01 15:01:56 +0800818static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
819{
820 struct cgroup_name *name;
821
822 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
823 if (!name)
824 return NULL;
825 strcpy(name->name, dentry->d_name.name);
826 return name;
827}
828
Li Zefanbe445622013-01-24 14:31:42 +0800829static void cgroup_free_fn(struct work_struct *work)
830{
831 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
832 struct cgroup_subsys *ss;
833
834 mutex_lock(&cgroup_mutex);
835 /*
836 * Release the subsystem state objects.
837 */
838 for_each_subsys(cgrp->root, ss)
839 ss->css_free(cgrp);
840
841 cgrp->root->number_of_cgroups--;
842 mutex_unlock(&cgroup_mutex);
843
844 /*
Li Zefan415cf072013-04-08 14:35:02 +0800845 * We get a ref to the parent's dentry, and put the ref when
846 * this cgroup is being freed, so it's guaranteed that the
847 * parent won't be destroyed before its children.
848 */
849 dput(cgrp->parent->dentry);
850
Li Zefancc20e012013-04-26 11:58:02 -0700851 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
852
Li Zefan415cf072013-04-08 14:35:02 +0800853 /*
Li Zefanbe445622013-01-24 14:31:42 +0800854 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700855 * created the cgroup. This will free cgrp->root, if we are
856 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800857 */
858 deactivate_super(cgrp->root->sb);
859
860 /*
861 * if we're getting rid of the cgroup, refcount should ensure
862 * that there are no pidlists left.
863 */
864 BUG_ON(!list_empty(&cgrp->pidlists));
865
866 simple_xattrs_free(&cgrp->xattrs);
867
Li Zefan65dff752013-03-01 15:01:56 +0800868 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800869 kfree(cgrp);
870}
871
872static void cgroup_free_rcu(struct rcu_head *head)
873{
874 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
875
876 schedule_work(&cgrp->free_work);
877}
878
Paul Menageddbcc7e2007-10-18 23:39:30 -0700879static void cgroup_diput(struct dentry *dentry, struct inode *inode)
880{
881 /* is dentry a directory ? if so, kfree() associated cgroup */
882 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700883 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800884
Paul Menagebd89aab2007-10-18 23:40:44 -0700885 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800886 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700887 } else {
888 struct cfent *cfe = __d_cfe(dentry);
889 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
890
891 WARN_ONCE(!list_empty(&cfe->node) &&
892 cgrp != &cgrp->root->top_cgroup,
893 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700894 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700895 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700896 }
897 iput(inode);
898}
899
Al Viroc72a04e2011-01-14 05:31:45 +0000900static int cgroup_delete(const struct dentry *d)
901{
902 return 1;
903}
904
Paul Menageddbcc7e2007-10-18 23:39:30 -0700905static void remove_dir(struct dentry *d)
906{
907 struct dentry *parent = dget(d->d_parent);
908
909 d_delete(d);
910 simple_rmdir(parent->d_inode, d);
911 dput(parent);
912}
913
Li Zefan2739d3c2013-01-21 18:18:33 +0800914static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700915{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700916 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700917
Tejun Heo05ef1d72012-04-01 12:09:56 -0700918 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
919 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100920
Li Zefan2739d3c2013-01-21 18:18:33 +0800921 /*
922 * If we're doing cleanup due to failure of cgroup_create(),
923 * the corresponding @cfe may not exist.
924 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700925 list_for_each_entry(cfe, &cgrp->files, node) {
926 struct dentry *d = cfe->dentry;
927
928 if (cft && cfe->type != cft)
929 continue;
930
931 dget(d);
932 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700933 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700934 list_del_init(&cfe->node);
935 dput(d);
936
Li Zefan2739d3c2013-01-21 18:18:33 +0800937 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700938 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700939}
940
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400941/**
942 * cgroup_clear_directory - selective removal of base and subsystem files
943 * @dir: directory containing the files
944 * @base_files: true if the base files should be removed
945 * @subsys_mask: mask of the subsystem ids whose files should be removed
946 */
947static void cgroup_clear_directory(struct dentry *dir, bool base_files,
948 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700949{
950 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400951 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700952
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400953 for_each_subsys(cgrp->root, ss) {
954 struct cftype_set *set;
955 if (!test_bit(ss->subsys_id, &subsys_mask))
956 continue;
957 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800958 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400959 }
960 if (base_files) {
961 while (!list_empty(&cgrp->files))
962 cgroup_rm_file(cgrp, NULL);
963 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700964}
965
966/*
967 * NOTE : the dentry must have been dget()'ed
968 */
969static void cgroup_d_remove_dir(struct dentry *dentry)
970{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100971 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400972 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100973
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400974 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700975
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100976 parent = dentry->d_parent;
977 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800978 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700979 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100980 spin_unlock(&dentry->d_lock);
981 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700982 remove_dir(dentry);
983}
984
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700985/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800986 * Call with cgroup_mutex held. Drops reference counts on modules, including
987 * any duplicate ones that parse_cgroupfs_options took. If this function
988 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800989 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400991 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700992{
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400993 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -0700994 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995 int i;
996
Ben Blumaae8aab2010-03-10 15:22:07 -0800997 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800998 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800999
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001000 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1001 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002 /* Check that any added subsystems are currently free */
1003 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001004 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001005 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001006 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001008 /*
1009 * Nobody should tell us to do a subsys that doesn't exist:
1010 * parse_cgroupfs_options should catch that case and refcounts
1011 * ensure that subsystems won't disappear once selected.
1012 */
1013 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001014 if (ss->root != &rootnode) {
1015 /* Subsystem isn't free */
1016 return -EBUSY;
1017 }
1018 }
1019
1020 /* Currently we don't handle adding/removing subsystems when
1021 * any child cgroups exist. This is theoretically supportable
1022 * but involves complex error handling, so it's being left until
1023 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001024 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025 return -EBUSY;
1026
1027 /* Process each subsystem */
1028 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1029 struct cgroup_subsys *ss = subsys[i];
1030 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001031 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001033 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001034 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 BUG_ON(!dummytop->subsys[i]);
1036 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001037 cgrp->subsys[i] = dummytop->subsys[i];
1038 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001039 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001040 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001041 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001042 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001043 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001044 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001046 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001047 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1048 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001050 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001052 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001053 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001054 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001055 /* subsystem is now free - drop reference on module */
1056 module_put(ss->module);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001057 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001059 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001060 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001061 /*
1062 * a refcount was taken, but we already had one, so
1063 * drop the extra reference.
1064 */
1065 module_put(ss->module);
1066#ifdef CONFIG_MODULE_UNLOAD
1067 BUG_ON(ss->module && !module_refcount(ss->module));
1068#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001069 } else {
1070 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001071 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072 }
1073 }
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001074 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075
1076 return 0;
1077}
1078
Al Viro34c80b12011-12-08 21:32:45 -05001079static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080{
Al Viro34c80b12011-12-08 21:32:45 -05001081 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082 struct cgroup_subsys *ss;
1083
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001084 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085 for_each_subsys(root, ss)
1086 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001087 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1088 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001089 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001090 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001091 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001092 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001093 if (strlen(root->release_agent_path))
1094 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001095 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001096 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001097 if (strlen(root->name))
1098 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001099 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100 return 0;
1101}
1102
1103struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001104 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001105 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001106 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001107 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001108 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001109 /* User explicitly requested empty subsystem */
1110 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001111
1112 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001113
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114};
1115
Ben Blumaae8aab2010-03-10 15:22:07 -08001116/*
1117 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001118 * with cgroup_mutex held to protect the subsys[] array. This function takes
1119 * refcounts on subsystems to be used, unless it returns error, in which case
1120 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001121 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001122static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001123{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001124 char *token, *o = data;
1125 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001126 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001127 int i;
1128 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001129
Ben Blumaae8aab2010-03-10 15:22:07 -08001130 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1131
Li Zefanf9ab5b52009-06-17 16:26:33 -07001132#ifdef CONFIG_CPUSETS
1133 mask = ~(1UL << cpuset_subsys_id);
1134#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001135
Paul Menagec6d57f32009-09-23 15:56:19 -07001136 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137
1138 while ((token = strsep(&o, ",")) != NULL) {
1139 if (!*token)
1140 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001141 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001142 /* Explicitly have no subsystems */
1143 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001144 continue;
1145 }
1146 if (!strcmp(token, "all")) {
1147 /* Mutually exclusive option 'all' + subsystem name */
1148 if (one_ss)
1149 return -EINVAL;
1150 all_ss = true;
1151 continue;
1152 }
Tejun Heo873fe092013-04-14 20:15:26 -07001153 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1154 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1155 continue;
1156 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001157 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001158 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001159 continue;
1160 }
1161 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001162 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001163 continue;
1164 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001165 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001166 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001167 continue;
1168 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001169 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001170 /* Specifying two release agents is forbidden */
1171 if (opts->release_agent)
1172 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001173 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001174 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001175 if (!opts->release_agent)
1176 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001177 continue;
1178 }
1179 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001180 const char *name = token + 5;
1181 /* Can't specify an empty name */
1182 if (!strlen(name))
1183 return -EINVAL;
1184 /* Must match [\w.-]+ */
1185 for (i = 0; i < strlen(name); i++) {
1186 char c = name[i];
1187 if (isalnum(c))
1188 continue;
1189 if ((c == '.') || (c == '-') || (c == '_'))
1190 continue;
1191 return -EINVAL;
1192 }
1193 /* Specifying two names is forbidden */
1194 if (opts->name)
1195 return -EINVAL;
1196 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001197 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001198 GFP_KERNEL);
1199 if (!opts->name)
1200 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001201
1202 continue;
1203 }
1204
1205 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1206 struct cgroup_subsys *ss = subsys[i];
1207 if (ss == NULL)
1208 continue;
1209 if (strcmp(token, ss->name))
1210 continue;
1211 if (ss->disabled)
1212 continue;
1213
1214 /* Mutually exclusive option 'all' + subsystem name */
1215 if (all_ss)
1216 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001217 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001218 one_ss = true;
1219
1220 break;
1221 }
1222 if (i == CGROUP_SUBSYS_COUNT)
1223 return -ENOENT;
1224 }
1225
1226 /*
1227 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001228 * otherwise if 'none', 'name=' and a subsystem name options
1229 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001230 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001231 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001232 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1233 struct cgroup_subsys *ss = subsys[i];
1234 if (ss == NULL)
1235 continue;
1236 if (ss->disabled)
1237 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001238 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001239 }
1240 }
1241
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001242 /* Consistency checks */
1243
Tejun Heo873fe092013-04-14 20:15:26 -07001244 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1245 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1246
1247 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1248 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1249 return -EINVAL;
1250 }
1251
1252 if (opts->cpuset_clone_children) {
1253 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1254 return -EINVAL;
1255 }
1256 }
1257
Li Zefanf9ab5b52009-06-17 16:26:33 -07001258 /*
1259 * Option noprefix was introduced just for backward compatibility
1260 * with the old cpuset, so we allow noprefix only if mounting just
1261 * the cpuset subsystem.
1262 */
Tejun Heo93438622013-04-14 20:15:25 -07001263 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001264 return -EINVAL;
1265
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001266
1267 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001268 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001269 return -EINVAL;
1270
1271 /*
1272 * We either have to specify by name or by subsystems. (So all
1273 * empty hierarchies must have a name).
1274 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001275 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001276 return -EINVAL;
1277
Ben Blumcf5d5942010-03-10 15:22:09 -08001278 /*
1279 * Grab references on all the modules we'll need, so the subsystems
1280 * don't dance around before rebind_subsystems attaches them. This may
1281 * take duplicate reference counts on a subsystem that's already used,
1282 * but rebind_subsystems handles this case.
1283 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001284 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001285 unsigned long bit = 1UL << i;
1286
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001287 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001288 continue;
1289 if (!try_module_get(subsys[i]->module)) {
1290 module_pin_failed = true;
1291 break;
1292 }
1293 }
1294 if (module_pin_failed) {
1295 /*
1296 * oops, one of the modules was going away. this means that we
1297 * raced with a module_delete call, and to the user this is
1298 * essentially a "subsystem doesn't exist" case.
1299 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001300 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001301 /* drop refcounts only on the ones we took */
1302 unsigned long bit = 1UL << i;
1303
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001304 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001305 continue;
1306 module_put(subsys[i]->module);
1307 }
1308 return -ENOENT;
1309 }
1310
Paul Menageddbcc7e2007-10-18 23:39:30 -07001311 return 0;
1312}
1313
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001314static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001315{
1316 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001317 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001318 unsigned long bit = 1UL << i;
1319
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001320 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001321 continue;
1322 module_put(subsys[i]->module);
1323 }
1324}
1325
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1327{
1328 int ret = 0;
1329 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001330 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001332 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333
Tejun Heo873fe092013-04-14 20:15:26 -07001334 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1335 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1336 return -EINVAL;
1337 }
1338
Paul Menagebd89aab2007-10-18 23:40:44 -07001339 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001341 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342
1343 /* See what subsystems are wanted */
1344 ret = parse_cgroupfs_options(data, &opts);
1345 if (ret)
1346 goto out_unlock;
1347
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001348 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001349 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1350 task_tgid_nr(current), current->comm);
1351
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001352 added_mask = opts.subsys_mask & ~root->subsys_mask;
1353 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001354
Ben Blumcf5d5942010-03-10 15:22:09 -08001355 /* Don't allow flags or name to change at remount */
1356 if (opts.flags != root->flags ||
1357 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001358 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001359 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001360 goto out_unlock;
1361 }
1362
Gao feng7083d032012-12-03 09:28:18 +08001363 /*
1364 * Clear out the files of subsystems that should be removed, do
1365 * this before rebind_subsystems, since rebind_subsystems may
1366 * change this hierarchy's subsys_list.
1367 */
1368 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1369
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001370 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001371 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001372 /* rebind_subsystems failed, re-populate the removed files */
1373 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001374 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001375 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001376 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001377
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001378 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001379 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380
Paul Menage81a6a5c2007-10-18 23:39:38 -07001381 if (opts.release_agent)
1382 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001384 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001385 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001386 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001387 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001388 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389 return ret;
1390}
1391
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001392static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001393 .statfs = simple_statfs,
1394 .drop_inode = generic_delete_inode,
1395 .show_options = cgroup_show_options,
1396 .remount_fs = cgroup_remount,
1397};
1398
Paul Menagecc31edc2008-10-18 20:28:04 -07001399static void init_cgroup_housekeeping(struct cgroup *cgrp)
1400{
1401 INIT_LIST_HEAD(&cgrp->sibling);
1402 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001403 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001404 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001405 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001406 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001407 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001408 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001409 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001410 INIT_LIST_HEAD(&cgrp->event_list);
1411 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001412 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001413}
Paul Menagec6d57f32009-09-23 15:56:19 -07001414
Paul Menageddbcc7e2007-10-18 23:39:30 -07001415static void init_cgroup_root(struct cgroupfs_root *root)
1416{
Paul Menagebd89aab2007-10-18 23:40:44 -07001417 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001418
Paul Menageddbcc7e2007-10-18 23:39:30 -07001419 INIT_LIST_HEAD(&root->subsys_list);
1420 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001421 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001422 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001423 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001424 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001425 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001426 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001427}
1428
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001429static bool init_root_id(struct cgroupfs_root *root)
1430{
1431 int ret = 0;
1432
1433 do {
1434 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1435 return false;
1436 spin_lock(&hierarchy_id_lock);
1437 /* Try to allocate the next unused ID */
1438 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1439 &root->hierarchy_id);
1440 if (ret == -ENOSPC)
1441 /* Try again starting from 0 */
1442 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1443 if (!ret) {
1444 next_hierarchy_id = root->hierarchy_id + 1;
1445 } else if (ret != -EAGAIN) {
1446 /* Can only get here if the 31-bit IDR is full ... */
1447 BUG_ON(ret);
1448 }
1449 spin_unlock(&hierarchy_id_lock);
1450 } while (ret);
1451 return true;
1452}
1453
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454static int cgroup_test_super(struct super_block *sb, void *data)
1455{
Paul Menagec6d57f32009-09-23 15:56:19 -07001456 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001457 struct cgroupfs_root *root = sb->s_fs_info;
1458
Paul Menagec6d57f32009-09-23 15:56:19 -07001459 /* If we asked for a name then it must match */
1460 if (opts->name && strcmp(opts->name, root->name))
1461 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001462
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001463 /*
1464 * If we asked for subsystems (or explicitly for no
1465 * subsystems) then they must match
1466 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001467 if ((opts->subsys_mask || opts->none)
1468 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001469 return 0;
1470
1471 return 1;
1472}
1473
Paul Menagec6d57f32009-09-23 15:56:19 -07001474static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1475{
1476 struct cgroupfs_root *root;
1477
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001478 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001479 return NULL;
1480
1481 root = kzalloc(sizeof(*root), GFP_KERNEL);
1482 if (!root)
1483 return ERR_PTR(-ENOMEM);
1484
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001485 if (!init_root_id(root)) {
1486 kfree(root);
1487 return ERR_PTR(-ENOMEM);
1488 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001489 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001490
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001491 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001492 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001493 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001494 if (opts->release_agent)
1495 strcpy(root->release_agent_path, opts->release_agent);
1496 if (opts->name)
1497 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001498 if (opts->cpuset_clone_children)
1499 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001500 return root;
1501}
1502
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001503static void cgroup_drop_root(struct cgroupfs_root *root)
1504{
1505 if (!root)
1506 return;
1507
1508 BUG_ON(!root->hierarchy_id);
1509 spin_lock(&hierarchy_id_lock);
1510 ida_remove(&hierarchy_ida, root->hierarchy_id);
1511 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001512 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001513 kfree(root);
1514}
1515
Paul Menageddbcc7e2007-10-18 23:39:30 -07001516static int cgroup_set_super(struct super_block *sb, void *data)
1517{
1518 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001519 struct cgroup_sb_opts *opts = data;
1520
1521 /* If we don't have a new root, we can't set up a new sb */
1522 if (!opts->new_root)
1523 return -EINVAL;
1524
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001525 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526
1527 ret = set_anon_super(sb, NULL);
1528 if (ret)
1529 return ret;
1530
Paul Menagec6d57f32009-09-23 15:56:19 -07001531 sb->s_fs_info = opts->new_root;
1532 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533
1534 sb->s_blocksize = PAGE_CACHE_SIZE;
1535 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1536 sb->s_magic = CGROUP_SUPER_MAGIC;
1537 sb->s_op = &cgroup_ops;
1538
1539 return 0;
1540}
1541
1542static int cgroup_get_rootdir(struct super_block *sb)
1543{
Al Viro0df6a632010-12-21 13:29:29 -05001544 static const struct dentry_operations cgroup_dops = {
1545 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001546 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001547 };
1548
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549 struct inode *inode =
1550 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551
1552 if (!inode)
1553 return -ENOMEM;
1554
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555 inode->i_fop = &simple_dir_operations;
1556 inode->i_op = &cgroup_dir_inode_operations;
1557 /* directories start off with i_nlink == 2 (for "." entry) */
1558 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001559 sb->s_root = d_make_root(inode);
1560 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001562 /* for everything else we want ->d_op set */
1563 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 return 0;
1565}
1566
Al Virof7e83572010-07-26 13:23:11 +04001567static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001569 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001570{
1571 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001572 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573 int ret = 0;
1574 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001575 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001576 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577
1578 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001579 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001581 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001582 if (ret)
1583 goto out_err;
1584
1585 /*
1586 * Allocate a new cgroup root. We may not need it if we're
1587 * reusing an existing hierarchy.
1588 */
1589 new_root = cgroup_root_from_opts(&opts);
1590 if (IS_ERR(new_root)) {
1591 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001592 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001593 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001594 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595
Paul Menagec6d57f32009-09-23 15:56:19 -07001596 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001597 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001598 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001599 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001600 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001601 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001602 }
1603
Paul Menagec6d57f32009-09-23 15:56:19 -07001604 root = sb->s_fs_info;
1605 BUG_ON(!root);
1606 if (root == opts.new_root) {
1607 /* We used the new root structure, so this is a new hierarchy */
1608 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001609 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001610 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001611 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001612 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001613 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614
1615 BUG_ON(sb->s_root != NULL);
1616
1617 ret = cgroup_get_rootdir(sb);
1618 if (ret)
1619 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001620 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621
Paul Menage817929e2007-10-18 23:39:36 -07001622 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001624 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001625
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001626 /* Check for name clashes with existing mounts */
1627 ret = -EBUSY;
1628 if (strlen(root->name))
1629 for_each_active_root(existing_root)
1630 if (!strcmp(existing_root->name, root->name))
1631 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001632
Paul Menage817929e2007-10-18 23:39:36 -07001633 /*
1634 * We're accessing css_set_count without locking
1635 * css_set_lock here, but that's OK - it can only be
1636 * increased by someone holding cgroup_lock, and
1637 * that's us. The worst that can happen is that we
1638 * have some link structures left over
1639 */
1640 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001641 if (ret)
1642 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001643
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001644 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001645 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001646 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001647 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001648 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001649 /*
1650 * There must be no failure case after here, since rebinding
1651 * takes care of subsystems' refcounts, which are explicitly
1652 * dropped in the failure exit path.
1653 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654
1655 /* EBUSY should be the only error here */
1656 BUG_ON(ret);
1657
1658 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001659 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660
Li Zefanc12f65d2009-01-07 18:07:42 -08001661 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662 root->top_cgroup.dentry = sb->s_root;
1663
Paul Menage817929e2007-10-18 23:39:36 -07001664 /* Link the top cgroup in this hierarchy into all
1665 * the css_set objects */
1666 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001667 hash_for_each(css_set_table, i, cg, hlist)
Li Zefan0ac801f2013-01-10 11:49:27 +08001668 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001669 write_unlock(&css_set_lock);
1670
1671 free_cg_links(&tmp_cg_links);
1672
Li Zefanc12f65d2009-01-07 18:07:42 -08001673 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674 BUG_ON(root->number_of_cgroups != 1);
1675
eparis@redhat2ce97382011-06-02 21:20:51 +10001676 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001677 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001678 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001679 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001680 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001681 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001682 } else {
1683 /*
1684 * We re-used an existing hierarchy - the new root (if
1685 * any) is not needed
1686 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001687 cgroup_drop_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001688
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001689 if (root->flags != opts.flags) {
1690 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1691 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1692 ret = -EINVAL;
1693 goto drop_new_super;
1694 } else {
1695 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1696 }
Tejun Heo873fe092013-04-14 20:15:26 -07001697 }
1698
Ben Blumcf5d5942010-03-10 15:22:09 -08001699 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001700 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701 }
1702
Paul Menagec6d57f32009-09-23 15:56:19 -07001703 kfree(opts.release_agent);
1704 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001705 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001707 unlock_drop:
1708 mutex_unlock(&cgroup_root_mutex);
1709 mutex_unlock(&cgroup_mutex);
1710 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001711 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001712 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001713 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001714 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001715 out_err:
1716 kfree(opts.release_agent);
1717 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001718 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719}
1720
1721static void cgroup_kill_sb(struct super_block *sb) {
1722 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001723 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001725 struct cg_cgroup_link *link;
1726 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727
1728 BUG_ON(!root);
1729
1730 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001731 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732
1733 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001734 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001735
1736 /* Rebind all subsystems back to the default hierarchy */
1737 ret = rebind_subsystems(root, 0);
1738 /* Shouldn't be able to fail ... */
1739 BUG_ON(ret);
1740
Paul Menage817929e2007-10-18 23:39:36 -07001741 /*
1742 * Release all the links from css_sets to this hierarchy's
1743 * root cgroup
1744 */
1745 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001746
1747 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1748 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001749 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001750 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001751 kfree(link);
1752 }
1753 write_unlock(&css_set_lock);
1754
Paul Menage839ec542009-01-29 14:25:22 -08001755 if (!list_empty(&root->root_list)) {
1756 list_del(&root->root_list);
1757 root_count--;
1758 }
Li Zefane5f6a862009-01-07 18:07:41 -08001759
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001760 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 mutex_unlock(&cgroup_mutex);
1762
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001763 simple_xattrs_free(&cgrp->xattrs);
1764
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001766 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767}
1768
1769static struct file_system_type cgroup_fs_type = {
1770 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001771 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772 .kill_sb = cgroup_kill_sb,
1773};
1774
Greg KH676db4a2010-08-05 13:53:35 -07001775static struct kobject *cgroup_kobj;
1776
Li Zefana043e3b2008-02-23 15:24:09 -08001777/**
1778 * cgroup_path - generate the path of a cgroup
1779 * @cgrp: the cgroup in question
1780 * @buf: the buffer to write the path into
1781 * @buflen: the length of the buffer
1782 *
Li Zefan65dff752013-03-01 15:01:56 +08001783 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1784 *
1785 * We can't generate cgroup path using dentry->d_name, as accessing
1786 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1787 * inode's i_mutex, while on the other hand cgroup_path() can be called
1788 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001789 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001790int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791{
Li Zefan65dff752013-03-01 15:01:56 +08001792 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001794
Tejun Heoda1f2962013-04-14 10:32:19 -07001795 if (!cgrp->parent) {
1796 if (strlcpy(buf, "/", buflen) >= buflen)
1797 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001798 return 0;
1799 }
1800
Tao Ma316eb662012-11-08 21:36:38 +08001801 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001802 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001803
Li Zefan65dff752013-03-01 15:01:56 +08001804 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001805 do {
Li Zefan65dff752013-03-01 15:01:56 +08001806 const char *name = cgroup_name(cgrp);
1807 int len;
1808
1809 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001811 goto out;
1812 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001813
Paul Menageddbcc7e2007-10-18 23:39:30 -07001814 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001815 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001816 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001817
1818 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001819 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001820 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001821 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001822out:
1823 rcu_read_unlock();
1824 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001825}
Ben Blum67523c42010-03-10 15:22:11 -08001826EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001827
Ben Blum74a11662011-05-26 16:25:20 -07001828/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001829 * Control Group taskset
1830 */
Tejun Heo134d3372011-12-12 18:12:21 -08001831struct task_and_cgroup {
1832 struct task_struct *task;
1833 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001834 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001835};
1836
Tejun Heo2f7ee562011-12-12 18:12:21 -08001837struct cgroup_taskset {
1838 struct task_and_cgroup single;
1839 struct flex_array *tc_array;
1840 int tc_array_len;
1841 int idx;
1842 struct cgroup *cur_cgrp;
1843};
1844
1845/**
1846 * cgroup_taskset_first - reset taskset and return the first task
1847 * @tset: taskset of interest
1848 *
1849 * @tset iteration is initialized and the first task is returned.
1850 */
1851struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1852{
1853 if (tset->tc_array) {
1854 tset->idx = 0;
1855 return cgroup_taskset_next(tset);
1856 } else {
1857 tset->cur_cgrp = tset->single.cgrp;
1858 return tset->single.task;
1859 }
1860}
1861EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1862
1863/**
1864 * cgroup_taskset_next - iterate to the next task in taskset
1865 * @tset: taskset of interest
1866 *
1867 * Return the next task in @tset. Iteration must have been initialized
1868 * with cgroup_taskset_first().
1869 */
1870struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1871{
1872 struct task_and_cgroup *tc;
1873
1874 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1875 return NULL;
1876
1877 tc = flex_array_get(tset->tc_array, tset->idx++);
1878 tset->cur_cgrp = tc->cgrp;
1879 return tc->task;
1880}
1881EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1882
1883/**
1884 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1885 * @tset: taskset of interest
1886 *
1887 * Return the cgroup for the current (last returned) task of @tset. This
1888 * function must be preceded by either cgroup_taskset_first() or
1889 * cgroup_taskset_next().
1890 */
1891struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1892{
1893 return tset->cur_cgrp;
1894}
1895EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1896
1897/**
1898 * cgroup_taskset_size - return the number of tasks in taskset
1899 * @tset: taskset of interest
1900 */
1901int cgroup_taskset_size(struct cgroup_taskset *tset)
1902{
1903 return tset->tc_array ? tset->tc_array_len : 1;
1904}
1905EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1906
1907
Ben Blum74a11662011-05-26 16:25:20 -07001908/*
1909 * cgroup_task_migrate - move a task from one cgroup to another.
1910 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001911 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001912 */
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03001913static void cgroup_task_migrate(struct cgroup *oldcgrp,
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001914 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001915{
1916 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001917
1918 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001919 * We are synchronized through threadgroup_lock() against PF_EXITING
1920 * setting such that we can't race against cgroup_exit() changing the
1921 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001922 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001923 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001924 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001925
Ben Blum74a11662011-05-26 16:25:20 -07001926 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001927 rcu_assign_pointer(tsk->cgroups, newcg);
1928 task_unlock(tsk);
1929
1930 /* Update the css_set linked lists if we're using them */
1931 write_lock(&css_set_lock);
1932 if (!list_empty(&tsk->cg_list))
1933 list_move(&tsk->cg_list, &newcg->tasks);
1934 write_unlock(&css_set_lock);
1935
1936 /*
1937 * We just gained a reference on oldcg by taking it from the task. As
1938 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1939 * it here; it will be freed under RCU.
1940 */
Ben Blum74a11662011-05-26 16:25:20 -07001941 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001942 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001943}
1944
Li Zefana043e3b2008-02-23 15:24:09 -08001945/**
Li Zefan081aa452013-03-13 09:17:09 +08001946 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001947 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001948 * @tsk: the task or the leader of the threadgroup to be attached
1949 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001950 *
Tejun Heo257058a2011-12-12 18:12:21 -08001951 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001952 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001953 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001954static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1955 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001956{
1957 int retval, i, group_size;
1958 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001959 struct cgroupfs_root *root = cgrp->root;
1960 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001961 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001962 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001963 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001964 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001965
1966 /*
1967 * step 0: in order to do expensive, possibly blocking operations for
1968 * every thread, we cannot iterate the thread group list, since it needs
1969 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001970 * group - group_rwsem prevents new threads from appearing, and if
1971 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001972 */
Li Zefan081aa452013-03-13 09:17:09 +08001973 if (threadgroup)
1974 group_size = get_nr_threads(tsk);
1975 else
1976 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001977 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001978 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001979 if (!group)
1980 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001981 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001982 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001983 if (retval)
1984 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001985
Ben Blum74a11662011-05-26 16:25:20 -07001986 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001987 /*
1988 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1989 * already PF_EXITING could be freed from underneath us unless we
1990 * take an rcu_read_lock.
1991 */
1992 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001993 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001994 struct task_and_cgroup ent;
1995
Tejun Heocd3d0952011-12-12 18:12:21 -08001996 /* @tsk either already exited or can't exit until the end */
1997 if (tsk->flags & PF_EXITING)
1998 continue;
1999
Ben Blum74a11662011-05-26 16:25:20 -07002000 /* as per above, nr_threads may decrease, but not increase. */
2001 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002002 ent.task = tsk;
2003 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002004 /* nothing to do if this task is already in the cgroup */
2005 if (ent.cgrp == cgrp)
2006 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002007 /*
2008 * saying GFP_ATOMIC has no effect here because we did prealloc
2009 * earlier, but it's good form to communicate our expectations.
2010 */
Tejun Heo134d3372011-12-12 18:12:21 -08002011 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002012 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002013 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002014
2015 if (!threadgroup)
2016 break;
Ben Blum74a11662011-05-26 16:25:20 -07002017 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002018 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002019 /* remember the number of threads in the array for later. */
2020 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002021 tset.tc_array = group;
2022 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002023
Tejun Heo134d3372011-12-12 18:12:21 -08002024 /* methods shouldn't be called if no task is actually migrating */
2025 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002026 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002027 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002028
Ben Blum74a11662011-05-26 16:25:20 -07002029 /*
2030 * step 1: check that we can legitimately attach to the cgroup.
2031 */
2032 for_each_subsys(root, ss) {
2033 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002034 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002035 if (retval) {
2036 failed_ss = ss;
2037 goto out_cancel_attach;
2038 }
2039 }
Ben Blum74a11662011-05-26 16:25:20 -07002040 }
2041
2042 /*
2043 * step 2: make sure css_sets exist for all threads to be migrated.
2044 * we use find_css_set, which allocates a new one if necessary.
2045 */
Ben Blum74a11662011-05-26 16:25:20 -07002046 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002047 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002048 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2049 if (!tc->cg) {
2050 retval = -ENOMEM;
2051 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002052 }
2053 }
2054
2055 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002056 * step 3: now that we're guaranteed success wrt the css_sets,
2057 * proceed to move all tasks to the new cgroup. There are no
2058 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002059 */
Ben Blum74a11662011-05-26 16:25:20 -07002060 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002061 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002062 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002063 }
2064 /* nothing is sensitive to fork() after this point. */
2065
2066 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002067 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002068 */
2069 for_each_subsys(root, ss) {
2070 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002071 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002072 }
2073
2074 /*
2075 * step 5: success! and cleanup
2076 */
Ben Blum74a11662011-05-26 16:25:20 -07002077 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002078out_put_css_set_refs:
2079 if (retval) {
2080 for (i = 0; i < group_size; i++) {
2081 tc = flex_array_get(group, i);
2082 if (!tc->cg)
2083 break;
2084 put_css_set(tc->cg);
2085 }
Ben Blum74a11662011-05-26 16:25:20 -07002086 }
2087out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002088 if (retval) {
2089 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002090 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002091 break;
Ben Blum74a11662011-05-26 16:25:20 -07002092 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002093 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002094 }
2095 }
Ben Blum74a11662011-05-26 16:25:20 -07002096out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002097 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002098 return retval;
2099}
2100
2101/*
2102 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002103 * function to attach either it or all tasks in its threadgroup. Will lock
2104 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002105 */
2106static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002107{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002108 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002109 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002110 int ret;
2111
Ben Blum74a11662011-05-26 16:25:20 -07002112 if (!cgroup_lock_live_group(cgrp))
2113 return -ENODEV;
2114
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002115retry_find_task:
2116 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002117 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002118 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002119 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002120 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002121 ret= -ESRCH;
2122 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002123 }
Ben Blum74a11662011-05-26 16:25:20 -07002124 /*
2125 * even if we're attaching all tasks in the thread group, we
2126 * only need to check permissions on one of them.
2127 */
David Howellsc69e8d92008-11-14 10:39:19 +11002128 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002129 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2130 !uid_eq(cred->euid, tcred->uid) &&
2131 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002132 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002133 ret = -EACCES;
2134 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002135 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002136 } else
2137 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002138
2139 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002140 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002141
2142 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002143 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002144 * trapped in a cpuset, or RT worker may be born in a cgroup
2145 * with no rt_runtime allocated. Just say no.
2146 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002147 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002148 ret = -EINVAL;
2149 rcu_read_unlock();
2150 goto out_unlock_cgroup;
2151 }
2152
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002153 get_task_struct(tsk);
2154 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002155
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002156 threadgroup_lock(tsk);
2157 if (threadgroup) {
2158 if (!thread_group_leader(tsk)) {
2159 /*
2160 * a race with de_thread from another thread's exec()
2161 * may strip us of our leadership, if this happens,
2162 * there is no choice but to throw this task away and
2163 * try again; this is
2164 * "double-double-toil-and-trouble-check locking".
2165 */
2166 threadgroup_unlock(tsk);
2167 put_task_struct(tsk);
2168 goto retry_find_task;
2169 }
Li Zefan081aa452013-03-13 09:17:09 +08002170 }
2171
2172 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2173
Tejun Heocd3d0952011-12-12 18:12:21 -08002174 threadgroup_unlock(tsk);
2175
Paul Menagebbcb81d2007-10-18 23:39:32 -07002176 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002177out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002178 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002179 return ret;
2180}
2181
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002182/**
2183 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2184 * @from: attach to all cgroups of a given task
2185 * @tsk: the task to be attached
2186 */
2187int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2188{
2189 struct cgroupfs_root *root;
2190 int retval = 0;
2191
Tejun Heo47cfcd02013-04-07 09:29:51 -07002192 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002193 for_each_active_root(root) {
2194 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2195
2196 retval = cgroup_attach_task(from_cg, tsk, false);
2197 if (retval)
2198 break;
2199 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002200 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002201
2202 return retval;
2203}
2204EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2205
Paul Menageaf351022008-07-25 01:47:01 -07002206static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2207{
Ben Blum74a11662011-05-26 16:25:20 -07002208 return attach_task_by_pid(cgrp, pid, false);
2209}
2210
2211static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2212{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002213 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002214}
2215
Paul Menagee788e062008-07-25 01:46:59 -07002216static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2217 const char *buffer)
2218{
2219 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002220 if (strlen(buffer) >= PATH_MAX)
2221 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002222 if (!cgroup_lock_live_group(cgrp))
2223 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002224 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002225 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002226 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002227 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002228 return 0;
2229}
2230
2231static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2232 struct seq_file *seq)
2233{
2234 if (!cgroup_lock_live_group(cgrp))
2235 return -ENODEV;
2236 seq_puts(seq, cgrp->root->release_agent_path);
2237 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002238 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002239 return 0;
2240}
2241
Tejun Heo873fe092013-04-14 20:15:26 -07002242static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2243 struct seq_file *seq)
2244{
2245 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002246 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002247}
2248
Paul Menage84eea842008-07-25 01:47:00 -07002249/* A buffer size big enough for numbers or short strings */
2250#define CGROUP_LOCAL_BUFFER_SIZE 64
2251
Paul Menagee73d2c62008-04-29 01:00:06 -07002252static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002253 struct file *file,
2254 const char __user *userbuf,
2255 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002256{
Paul Menage84eea842008-07-25 01:47:00 -07002257 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002258 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002259 char *end;
2260
2261 if (!nbytes)
2262 return -EINVAL;
2263 if (nbytes >= sizeof(buffer))
2264 return -E2BIG;
2265 if (copy_from_user(buffer, userbuf, nbytes))
2266 return -EFAULT;
2267
2268 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002269 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002270 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002271 if (*end)
2272 return -EINVAL;
2273 retval = cft->write_u64(cgrp, cft, val);
2274 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002275 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002276 if (*end)
2277 return -EINVAL;
2278 retval = cft->write_s64(cgrp, cft, val);
2279 }
Paul Menage355e0c42007-10-18 23:39:33 -07002280 if (!retval)
2281 retval = nbytes;
2282 return retval;
2283}
2284
Paul Menagedb3b1492008-07-25 01:46:58 -07002285static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2286 struct file *file,
2287 const char __user *userbuf,
2288 size_t nbytes, loff_t *unused_ppos)
2289{
Paul Menage84eea842008-07-25 01:47:00 -07002290 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002291 int retval = 0;
2292 size_t max_bytes = cft->max_write_len;
2293 char *buffer = local_buffer;
2294
2295 if (!max_bytes)
2296 max_bytes = sizeof(local_buffer) - 1;
2297 if (nbytes >= max_bytes)
2298 return -E2BIG;
2299 /* Allocate a dynamic buffer if we need one */
2300 if (nbytes >= sizeof(local_buffer)) {
2301 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2302 if (buffer == NULL)
2303 return -ENOMEM;
2304 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002305 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2306 retval = -EFAULT;
2307 goto out;
2308 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002309
2310 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002311 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002312 if (!retval)
2313 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002314out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002315 if (buffer != local_buffer)
2316 kfree(buffer);
2317 return retval;
2318}
2319
Paul Menageddbcc7e2007-10-18 23:39:30 -07002320static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2321 size_t nbytes, loff_t *ppos)
2322{
2323 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002324 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002325
Li Zefan75139b82009-01-07 18:07:33 -08002326 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002327 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002328 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002329 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002330 if (cft->write_u64 || cft->write_s64)
2331 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002332 if (cft->write_string)
2333 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002334 if (cft->trigger) {
2335 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2336 return ret ? ret : nbytes;
2337 }
Paul Menage355e0c42007-10-18 23:39:33 -07002338 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002339}
2340
Paul Menagef4c753b2008-04-29 00:59:56 -07002341static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2342 struct file *file,
2343 char __user *buf, size_t nbytes,
2344 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002345{
Paul Menage84eea842008-07-25 01:47:00 -07002346 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002347 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002348 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2349
2350 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2351}
2352
Paul Menagee73d2c62008-04-29 01:00:06 -07002353static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2354 struct file *file,
2355 char __user *buf, size_t nbytes,
2356 loff_t *ppos)
2357{
Paul Menage84eea842008-07-25 01:47:00 -07002358 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002359 s64 val = cft->read_s64(cgrp, cft);
2360 int len = sprintf(tmp, "%lld\n", (long long) val);
2361
2362 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2363}
2364
Paul Menageddbcc7e2007-10-18 23:39:30 -07002365static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2366 size_t nbytes, loff_t *ppos)
2367{
2368 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002369 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002370
Li Zefan75139b82009-01-07 18:07:33 -08002371 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002372 return -ENODEV;
2373
2374 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002375 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002376 if (cft->read_u64)
2377 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002378 if (cft->read_s64)
2379 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002380 return -EINVAL;
2381}
2382
Paul Menage91796562008-04-29 01:00:01 -07002383/*
2384 * seqfile ops/methods for returning structured data. Currently just
2385 * supports string->u64 maps, but can be extended in future.
2386 */
2387
2388struct cgroup_seqfile_state {
2389 struct cftype *cft;
2390 struct cgroup *cgroup;
2391};
2392
2393static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2394{
2395 struct seq_file *sf = cb->state;
2396 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2397}
2398
2399static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2400{
2401 struct cgroup_seqfile_state *state = m->private;
2402 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002403 if (cft->read_map) {
2404 struct cgroup_map_cb cb = {
2405 .fill = cgroup_map_add,
2406 .state = m,
2407 };
2408 return cft->read_map(state->cgroup, cft, &cb);
2409 }
2410 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002411}
2412
Adrian Bunk96930a62008-07-25 19:46:21 -07002413static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002414{
2415 struct seq_file *seq = file->private_data;
2416 kfree(seq->private);
2417 return single_release(inode, file);
2418}
2419
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002420static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002421 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002422 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002423 .llseek = seq_lseek,
2424 .release = cgroup_seqfile_release,
2425};
2426
Paul Menageddbcc7e2007-10-18 23:39:30 -07002427static int cgroup_file_open(struct inode *inode, struct file *file)
2428{
2429 int err;
2430 struct cftype *cft;
2431
2432 err = generic_file_open(inode, file);
2433 if (err)
2434 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002435 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002436
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002437 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002438 struct cgroup_seqfile_state *state =
2439 kzalloc(sizeof(*state), GFP_USER);
2440 if (!state)
2441 return -ENOMEM;
2442 state->cft = cft;
2443 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2444 file->f_op = &cgroup_seqfile_operations;
2445 err = single_open(file, cgroup_seqfile_show, state);
2446 if (err < 0)
2447 kfree(state);
2448 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002449 err = cft->open(inode, file);
2450 else
2451 err = 0;
2452
2453 return err;
2454}
2455
2456static int cgroup_file_release(struct inode *inode, struct file *file)
2457{
2458 struct cftype *cft = __d_cft(file->f_dentry);
2459 if (cft->release)
2460 return cft->release(inode, file);
2461 return 0;
2462}
2463
2464/*
2465 * cgroup_rename - Only allow simple rename of directories in place.
2466 */
2467static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2468 struct inode *new_dir, struct dentry *new_dentry)
2469{
Li Zefan65dff752013-03-01 15:01:56 +08002470 int ret;
2471 struct cgroup_name *name, *old_name;
2472 struct cgroup *cgrp;
2473
2474 /*
2475 * It's convinient to use parent dir's i_mutex to protected
2476 * cgrp->name.
2477 */
2478 lockdep_assert_held(&old_dir->i_mutex);
2479
Paul Menageddbcc7e2007-10-18 23:39:30 -07002480 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2481 return -ENOTDIR;
2482 if (new_dentry->d_inode)
2483 return -EEXIST;
2484 if (old_dir != new_dir)
2485 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002486
2487 cgrp = __d_cgrp(old_dentry);
2488
2489 name = cgroup_alloc_name(new_dentry);
2490 if (!name)
2491 return -ENOMEM;
2492
2493 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2494 if (ret) {
2495 kfree(name);
2496 return ret;
2497 }
2498
2499 old_name = cgrp->name;
2500 rcu_assign_pointer(cgrp->name, name);
2501
2502 kfree_rcu(old_name, rcu_head);
2503 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002504}
2505
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002506static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2507{
2508 if (S_ISDIR(dentry->d_inode->i_mode))
2509 return &__d_cgrp(dentry)->xattrs;
2510 else
Li Zefan712317a2013-04-18 23:09:52 -07002511 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002512}
2513
2514static inline int xattr_enabled(struct dentry *dentry)
2515{
2516 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002517 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002518}
2519
2520static bool is_valid_xattr(const char *name)
2521{
2522 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2523 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2524 return true;
2525 return false;
2526}
2527
2528static int cgroup_setxattr(struct dentry *dentry, const char *name,
2529 const void *val, size_t size, int flags)
2530{
2531 if (!xattr_enabled(dentry))
2532 return -EOPNOTSUPP;
2533 if (!is_valid_xattr(name))
2534 return -EINVAL;
2535 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2536}
2537
2538static int cgroup_removexattr(struct dentry *dentry, const char *name)
2539{
2540 if (!xattr_enabled(dentry))
2541 return -EOPNOTSUPP;
2542 if (!is_valid_xattr(name))
2543 return -EINVAL;
2544 return simple_xattr_remove(__d_xattrs(dentry), name);
2545}
2546
2547static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2548 void *buf, size_t size)
2549{
2550 if (!xattr_enabled(dentry))
2551 return -EOPNOTSUPP;
2552 if (!is_valid_xattr(name))
2553 return -EINVAL;
2554 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2555}
2556
2557static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2558{
2559 if (!xattr_enabled(dentry))
2560 return -EOPNOTSUPP;
2561 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2562}
2563
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002564static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002565 .read = cgroup_file_read,
2566 .write = cgroup_file_write,
2567 .llseek = generic_file_llseek,
2568 .open = cgroup_file_open,
2569 .release = cgroup_file_release,
2570};
2571
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002572static const struct inode_operations cgroup_file_inode_operations = {
2573 .setxattr = cgroup_setxattr,
2574 .getxattr = cgroup_getxattr,
2575 .listxattr = cgroup_listxattr,
2576 .removexattr = cgroup_removexattr,
2577};
2578
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002579static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002580 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002581 .mkdir = cgroup_mkdir,
2582 .rmdir = cgroup_rmdir,
2583 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002584 .setxattr = cgroup_setxattr,
2585 .getxattr = cgroup_getxattr,
2586 .listxattr = cgroup_listxattr,
2587 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002588};
2589
Al Viro00cd8dd2012-06-10 17:13:09 -04002590static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002591{
2592 if (dentry->d_name.len > NAME_MAX)
2593 return ERR_PTR(-ENAMETOOLONG);
2594 d_add(dentry, NULL);
2595 return NULL;
2596}
2597
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002598/*
2599 * Check if a file is a control file
2600 */
2601static inline struct cftype *__file_cft(struct file *file)
2602{
Al Viro496ad9a2013-01-23 17:07:38 -05002603 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002604 return ERR_PTR(-EINVAL);
2605 return __d_cft(file->f_dentry);
2606}
2607
Al Viroa5e7ed32011-07-26 01:55:55 -04002608static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002609 struct super_block *sb)
2610{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002611 struct inode *inode;
2612
2613 if (!dentry)
2614 return -ENOENT;
2615 if (dentry->d_inode)
2616 return -EEXIST;
2617
2618 inode = cgroup_new_inode(mode, sb);
2619 if (!inode)
2620 return -ENOMEM;
2621
2622 if (S_ISDIR(mode)) {
2623 inode->i_op = &cgroup_dir_inode_operations;
2624 inode->i_fop = &simple_dir_operations;
2625
2626 /* start off with i_nlink == 2 (for "." entry) */
2627 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002628 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002629
Tejun Heob8a2df62012-11-19 08:13:37 -08002630 /*
2631 * Control reaches here with cgroup_mutex held.
2632 * @inode->i_mutex should nest outside cgroup_mutex but we
2633 * want to populate it immediately without releasing
2634 * cgroup_mutex. As @inode isn't visible to anyone else
2635 * yet, trylock will always succeed without affecting
2636 * lockdep checks.
2637 */
2638 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002639 } else if (S_ISREG(mode)) {
2640 inode->i_size = 0;
2641 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002642 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002643 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002644 d_instantiate(dentry, inode);
2645 dget(dentry); /* Extra count - pin the dentry in core */
2646 return 0;
2647}
2648
Li Zefan099fca32009-04-02 16:57:29 -07002649/**
2650 * cgroup_file_mode - deduce file mode of a control file
2651 * @cft: the control file in question
2652 *
2653 * returns cft->mode if ->mode is not 0
2654 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2655 * returns S_IRUGO if it has only a read handler
2656 * returns S_IWUSR if it has only a write hander
2657 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002658static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002659{
Al Viroa5e7ed32011-07-26 01:55:55 -04002660 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002661
2662 if (cft->mode)
2663 return cft->mode;
2664
2665 if (cft->read || cft->read_u64 || cft->read_s64 ||
2666 cft->read_map || cft->read_seq_string)
2667 mode |= S_IRUGO;
2668
2669 if (cft->write || cft->write_u64 || cft->write_s64 ||
2670 cft->write_string || cft->trigger)
2671 mode |= S_IWUSR;
2672
2673 return mode;
2674}
2675
Tejun Heodb0416b2012-04-01 12:09:55 -07002676static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002677 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002678{
Paul Menagebd89aab2007-10-18 23:40:44 -07002679 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002680 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002681 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002682 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002683 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002684 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002685 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002686
Tejun Heo93438622013-04-14 20:15:25 -07002687 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002688 strcpy(name, subsys->name);
2689 strcat(name, ".");
2690 }
2691 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002692
Paul Menageddbcc7e2007-10-18 23:39:30 -07002693 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002694
2695 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2696 if (!cfe)
2697 return -ENOMEM;
2698
Paul Menageddbcc7e2007-10-18 23:39:30 -07002699 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002700 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002701 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002702 goto out;
2703 }
2704
Li Zefand6cbf352013-05-14 19:44:20 +08002705 cfe->type = (void *)cft;
2706 cfe->dentry = dentry;
2707 dentry->d_fsdata = cfe;
2708 simple_xattrs_init(&cfe->xattrs);
2709
Tejun Heo05ef1d72012-04-01 12:09:56 -07002710 mode = cgroup_file_mode(cft);
2711 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2712 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002713 list_add_tail(&cfe->node, &parent->files);
2714 cfe = NULL;
2715 }
2716 dput(dentry);
2717out:
2718 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002719 return error;
2720}
2721
Tejun Heo79578622012-04-01 12:09:56 -07002722static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002723 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002725 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002726 int err, ret = 0;
2727
2728 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002729 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002730 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2731 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002732 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2733 continue;
2734 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2735 continue;
2736
Li Zefan2739d3c2013-01-21 18:18:33 +08002737 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002738 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002739 if (err)
2740 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2741 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002742 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002743 } else {
2744 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002745 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002746 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002747 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002748}
2749
Tejun Heo8e3f6542012-04-01 12:09:55 -07002750static DEFINE_MUTEX(cgroup_cft_mutex);
2751
2752static void cgroup_cfts_prepare(void)
2753 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2754{
2755 /*
2756 * Thanks to the entanglement with vfs inode locking, we can't walk
2757 * the existing cgroups under cgroup_mutex and create files.
2758 * Instead, we increment reference on all cgroups and build list of
2759 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2760 * exclusive access to the field.
2761 */
2762 mutex_lock(&cgroup_cft_mutex);
2763 mutex_lock(&cgroup_mutex);
2764}
2765
2766static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002767 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002768 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2769{
2770 LIST_HEAD(pending);
2771 struct cgroup *cgrp, *n;
Li Zefana09a1b82013-06-18 18:40:19 +08002772 struct super_block *sb = ss->root->sb;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002773
2774 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Li Zefana09a1b82013-06-18 18:40:19 +08002775 if (cfts && ss->root != &rootnode &&
2776 atomic_inc_not_zero(&sb->s_active)) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07002777 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2778 dget(cgrp->dentry);
2779 list_add_tail(&cgrp->cft_q_node, &pending);
2780 }
Li Zefana09a1b82013-06-18 18:40:19 +08002781 } else {
2782 sb = NULL;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002783 }
2784
2785 mutex_unlock(&cgroup_mutex);
2786
2787 /*
2788 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2789 * files for all cgroups which were created before.
2790 */
2791 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2792 struct inode *inode = cgrp->dentry->d_inode;
2793
2794 mutex_lock(&inode->i_mutex);
2795 mutex_lock(&cgroup_mutex);
2796 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002797 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002798 mutex_unlock(&cgroup_mutex);
2799 mutex_unlock(&inode->i_mutex);
2800
2801 list_del_init(&cgrp->cft_q_node);
2802 dput(cgrp->dentry);
2803 }
2804
Li Zefana09a1b82013-06-18 18:40:19 +08002805 if (sb)
2806 deactivate_super(sb);
2807
Tejun Heo8e3f6542012-04-01 12:09:55 -07002808 mutex_unlock(&cgroup_cft_mutex);
2809}
2810
2811/**
2812 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2813 * @ss: target cgroup subsystem
2814 * @cfts: zero-length name terminated array of cftypes
2815 *
2816 * Register @cfts to @ss. Files described by @cfts are created for all
2817 * existing cgroups to which @ss is attached and all future cgroups will
2818 * have them too. This function can be called anytime whether @ss is
2819 * attached or not.
2820 *
2821 * Returns 0 on successful registration, -errno on failure. Note that this
2822 * function currently returns 0 as long as @cfts registration is successful
2823 * even if some file creation attempts on existing cgroups fail.
2824 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002825int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002826{
2827 struct cftype_set *set;
2828
2829 set = kzalloc(sizeof(*set), GFP_KERNEL);
2830 if (!set)
2831 return -ENOMEM;
2832
2833 cgroup_cfts_prepare();
2834 set->cfts = cfts;
2835 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002836 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002837
2838 return 0;
2839}
2840EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2841
Li Zefana043e3b2008-02-23 15:24:09 -08002842/**
Tejun Heo79578622012-04-01 12:09:56 -07002843 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2844 * @ss: target cgroup subsystem
2845 * @cfts: zero-length name terminated array of cftypes
2846 *
2847 * Unregister @cfts from @ss. Files described by @cfts are removed from
2848 * all existing cgroups to which @ss is attached and all future cgroups
2849 * won't have them either. This function can be called anytime whether @ss
2850 * is attached or not.
2851 *
2852 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2853 * registered with @ss.
2854 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002855int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002856{
2857 struct cftype_set *set;
2858
2859 cgroup_cfts_prepare();
2860
2861 list_for_each_entry(set, &ss->cftsets, node) {
2862 if (set->cfts == cfts) {
2863 list_del_init(&set->node);
2864 cgroup_cfts_commit(ss, cfts, false);
2865 return 0;
2866 }
2867 }
2868
2869 cgroup_cfts_commit(ss, NULL, false);
2870 return -ENOENT;
2871}
2872
2873/**
Li Zefana043e3b2008-02-23 15:24:09 -08002874 * cgroup_task_count - count the number of tasks in a cgroup.
2875 * @cgrp: the cgroup in question
2876 *
2877 * Return the number of tasks in the cgroup.
2878 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002879int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002880{
2881 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002882 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002883
Paul Menage817929e2007-10-18 23:39:36 -07002884 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002885 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002886 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002887 }
2888 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002889 return count;
2890}
2891
2892/*
Paul Menage817929e2007-10-18 23:39:36 -07002893 * Advance a list_head iterator. The iterator should be positioned at
2894 * the start of a css_set
2895 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002896static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002897 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002898{
2899 struct list_head *l = it->cg_link;
2900 struct cg_cgroup_link *link;
2901 struct css_set *cg;
2902
2903 /* Advance to the next non-empty css_set */
2904 do {
2905 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002906 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002907 it->cg_link = NULL;
2908 return;
2909 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002910 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002911 cg = link->cg;
2912 } while (list_empty(&cg->tasks));
2913 it->cg_link = l;
2914 it->task = cg->tasks.next;
2915}
2916
Cliff Wickman31a7df02008-02-07 00:14:42 -08002917/*
2918 * To reduce the fork() overhead for systems that are not actually
2919 * using their cgroups capability, we don't maintain the lists running
2920 * through each css_set to its tasks until we see the list actually
2921 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002922 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002923static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002924{
2925 struct task_struct *p, *g;
2926 write_lock(&css_set_lock);
2927 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002928 /*
2929 * We need tasklist_lock because RCU is not safe against
2930 * while_each_thread(). Besides, a forking task that has passed
2931 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2932 * is not guaranteed to have its child immediately visible in the
2933 * tasklist if we walk through it with RCU.
2934 */
2935 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002936 do_each_thread(g, p) {
2937 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002938 /*
2939 * We should check if the process is exiting, otherwise
2940 * it will race with cgroup_exit() in that the list
2941 * entry won't be deleted though the process has exited.
2942 */
2943 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002944 list_add(&p->cg_list, &p->cgroups->tasks);
2945 task_unlock(p);
2946 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002947 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002948 write_unlock(&css_set_lock);
2949}
2950
Tejun Heo574bd9f2012-11-09 09:12:29 -08002951/**
2952 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2953 * @pos: the current position (%NULL to initiate traversal)
2954 * @cgroup: cgroup whose descendants to walk
2955 *
2956 * To be used by cgroup_for_each_descendant_pre(). Find the next
2957 * descendant to visit for pre-order traversal of @cgroup's descendants.
2958 */
2959struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2960 struct cgroup *cgroup)
2961{
2962 struct cgroup *next;
2963
2964 WARN_ON_ONCE(!rcu_read_lock_held());
2965
2966 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09002967 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002968 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002969
2970 /* visit the first child if exists */
2971 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2972 if (next)
2973 return next;
2974
2975 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09002976 while (pos != cgroup) {
Tejun Heo574bd9f2012-11-09 09:12:29 -08002977 next = list_entry_rcu(pos->sibling.next, struct cgroup,
2978 sibling);
2979 if (&next->sibling != &pos->parent->children)
2980 return next;
2981
2982 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09002983 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002984
2985 return NULL;
2986}
2987EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
2988
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002989/**
2990 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
2991 * @pos: cgroup of interest
2992 *
2993 * Return the rightmost descendant of @pos. If there's no descendant,
2994 * @pos is returned. This can be used during pre-order traversal to skip
2995 * subtree of @pos.
2996 */
2997struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
2998{
2999 struct cgroup *last, *tmp;
3000
3001 WARN_ON_ONCE(!rcu_read_lock_held());
3002
3003 do {
3004 last = pos;
3005 /* ->prev isn't RCU safe, walk ->next till the end */
3006 pos = NULL;
3007 list_for_each_entry_rcu(tmp, &last->children, sibling)
3008 pos = tmp;
3009 } while (pos);
3010
3011 return last;
3012}
3013EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3014
Tejun Heo574bd9f2012-11-09 09:12:29 -08003015static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3016{
3017 struct cgroup *last;
3018
3019 do {
3020 last = pos;
3021 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3022 sibling);
3023 } while (pos);
3024
3025 return last;
3026}
3027
3028/**
3029 * cgroup_next_descendant_post - find the next descendant for post-order walk
3030 * @pos: the current position (%NULL to initiate traversal)
3031 * @cgroup: cgroup whose descendants to walk
3032 *
3033 * To be used by cgroup_for_each_descendant_post(). Find the next
3034 * descendant to visit for post-order traversal of @cgroup's descendants.
3035 */
3036struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3037 struct cgroup *cgroup)
3038{
3039 struct cgroup *next;
3040
3041 WARN_ON_ONCE(!rcu_read_lock_held());
3042
3043 /* if first iteration, visit the leftmost descendant */
3044 if (!pos) {
3045 next = cgroup_leftmost_descendant(cgroup);
3046 return next != cgroup ? next : NULL;
3047 }
3048
3049 /* if there's an unvisited sibling, visit its leftmost descendant */
3050 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3051 if (&next->sibling != &pos->parent->children)
3052 return cgroup_leftmost_descendant(next);
3053
3054 /* no sibling left, visit parent */
3055 next = pos->parent;
3056 return next != cgroup ? next : NULL;
3057}
3058EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3059
Paul Menagebd89aab2007-10-18 23:40:44 -07003060void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003061 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003062{
3063 /*
3064 * The first time anyone tries to iterate across a cgroup,
3065 * we need to enable the list linking each css_set to its
3066 * tasks, and fix up all existing tasks.
3067 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003068 if (!use_task_css_set_links)
3069 cgroup_enable_task_cg_lists();
3070
Paul Menage817929e2007-10-18 23:39:36 -07003071 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003072 it->cg_link = &cgrp->css_sets;
3073 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003074}
3075
Paul Menagebd89aab2007-10-18 23:40:44 -07003076struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003077 struct cgroup_iter *it)
3078{
3079 struct task_struct *res;
3080 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003081 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003082
3083 /* If the iterator cg is NULL, we have no tasks */
3084 if (!it->cg_link)
3085 return NULL;
3086 res = list_entry(l, struct task_struct, cg_list);
3087 /* Advance iterator to find next entry */
3088 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003089 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3090 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003091 /* We reached the end of this task list - move on to
3092 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003093 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003094 } else {
3095 it->task = l;
3096 }
3097 return res;
3098}
3099
Paul Menagebd89aab2007-10-18 23:40:44 -07003100void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003101 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003102{
3103 read_unlock(&css_set_lock);
3104}
3105
Cliff Wickman31a7df02008-02-07 00:14:42 -08003106static inline int started_after_time(struct task_struct *t1,
3107 struct timespec *time,
3108 struct task_struct *t2)
3109{
3110 int start_diff = timespec_compare(&t1->start_time, time);
3111 if (start_diff > 0) {
3112 return 1;
3113 } else if (start_diff < 0) {
3114 return 0;
3115 } else {
3116 /*
3117 * Arbitrarily, if two processes started at the same
3118 * time, we'll say that the lower pointer value
3119 * started first. Note that t2 may have exited by now
3120 * so this may not be a valid pointer any longer, but
3121 * that's fine - it still serves to distinguish
3122 * between two tasks started (effectively) simultaneously.
3123 */
3124 return t1 > t2;
3125 }
3126}
3127
3128/*
3129 * This function is a callback from heap_insert() and is used to order
3130 * the heap.
3131 * In this case we order the heap in descending task start time.
3132 */
3133static inline int started_after(void *p1, void *p2)
3134{
3135 struct task_struct *t1 = p1;
3136 struct task_struct *t2 = p2;
3137 return started_after_time(t1, &t2->start_time, t2);
3138}
3139
3140/**
3141 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3142 * @scan: struct cgroup_scanner containing arguments for the scan
3143 *
3144 * Arguments include pointers to callback functions test_task() and
3145 * process_task().
3146 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3147 * and if it returns true, call process_task() for it also.
3148 * The test_task pointer may be NULL, meaning always true (select all tasks).
3149 * Effectively duplicates cgroup_iter_{start,next,end}()
3150 * but does not lock css_set_lock for the call to process_task().
3151 * The struct cgroup_scanner may be embedded in any structure of the caller's
3152 * creation.
3153 * It is guaranteed that process_task() will act on every task that
3154 * is a member of the cgroup for the duration of this call. This
3155 * function may or may not call process_task() for tasks that exit
3156 * or move to a different cgroup during the call, or are forked or
3157 * move into the cgroup during the call.
3158 *
3159 * Note that test_task() may be called with locks held, and may in some
3160 * situations be called multiple times for the same task, so it should
3161 * be cheap.
3162 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3163 * pre-allocated and will be used for heap operations (and its "gt" member will
3164 * be overwritten), else a temporary heap will be used (allocation of which
3165 * may cause this function to fail).
3166 */
3167int cgroup_scan_tasks(struct cgroup_scanner *scan)
3168{
3169 int retval, i;
3170 struct cgroup_iter it;
3171 struct task_struct *p, *dropped;
3172 /* Never dereference latest_task, since it's not refcounted */
3173 struct task_struct *latest_task = NULL;
3174 struct ptr_heap tmp_heap;
3175 struct ptr_heap *heap;
3176 struct timespec latest_time = { 0, 0 };
3177
3178 if (scan->heap) {
3179 /* The caller supplied our heap and pre-allocated its memory */
3180 heap = scan->heap;
3181 heap->gt = &started_after;
3182 } else {
3183 /* We need to allocate our own heap memory */
3184 heap = &tmp_heap;
3185 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3186 if (retval)
3187 /* cannot allocate the heap */
3188 return retval;
3189 }
3190
3191 again:
3192 /*
3193 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3194 * to determine which are of interest, and using the scanner's
3195 * "process_task" callback to process any of them that need an update.
3196 * Since we don't want to hold any locks during the task updates,
3197 * gather tasks to be processed in a heap structure.
3198 * The heap is sorted by descending task start time.
3199 * If the statically-sized heap fills up, we overflow tasks that
3200 * started later, and in future iterations only consider tasks that
3201 * started after the latest task in the previous pass. This
3202 * guarantees forward progress and that we don't miss any tasks.
3203 */
3204 heap->size = 0;
3205 cgroup_iter_start(scan->cg, &it);
3206 while ((p = cgroup_iter_next(scan->cg, &it))) {
3207 /*
3208 * Only affect tasks that qualify per the caller's callback,
3209 * if he provided one
3210 */
3211 if (scan->test_task && !scan->test_task(p, scan))
3212 continue;
3213 /*
3214 * Only process tasks that started after the last task
3215 * we processed
3216 */
3217 if (!started_after_time(p, &latest_time, latest_task))
3218 continue;
3219 dropped = heap_insert(heap, p);
3220 if (dropped == NULL) {
3221 /*
3222 * The new task was inserted; the heap wasn't
3223 * previously full
3224 */
3225 get_task_struct(p);
3226 } else if (dropped != p) {
3227 /*
3228 * The new task was inserted, and pushed out a
3229 * different task
3230 */
3231 get_task_struct(p);
3232 put_task_struct(dropped);
3233 }
3234 /*
3235 * Else the new task was newer than anything already in
3236 * the heap and wasn't inserted
3237 */
3238 }
3239 cgroup_iter_end(scan->cg, &it);
3240
3241 if (heap->size) {
3242 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003243 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003244 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003245 latest_time = q->start_time;
3246 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003247 }
3248 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003249 scan->process_task(q, scan);
3250 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003251 }
3252 /*
3253 * If we had to process any tasks at all, scan again
3254 * in case some of them were in the middle of forking
3255 * children that didn't get processed.
3256 * Not the most efficient way to do it, but it avoids
3257 * having to take callback_mutex in the fork path
3258 */
3259 goto again;
3260 }
3261 if (heap == &tmp_heap)
3262 heap_free(&tmp_heap);
3263 return 0;
3264}
3265
Tejun Heo8cc99342013-04-07 09:29:50 -07003266static void cgroup_transfer_one_task(struct task_struct *task,
3267 struct cgroup_scanner *scan)
3268{
3269 struct cgroup *new_cgroup = scan->data;
3270
Tejun Heo47cfcd02013-04-07 09:29:51 -07003271 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003272 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003273 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003274}
3275
3276/**
3277 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3278 * @to: cgroup to which the tasks will be moved
3279 * @from: cgroup in which the tasks currently reside
3280 */
3281int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3282{
3283 struct cgroup_scanner scan;
3284
3285 scan.cg = from;
3286 scan.test_task = NULL; /* select all tasks in cgroup */
3287 scan.process_task = cgroup_transfer_one_task;
3288 scan.heap = NULL;
3289 scan.data = to;
3290
3291 return cgroup_scan_tasks(&scan);
3292}
3293
Paul Menage817929e2007-10-18 23:39:36 -07003294/*
Ben Blum102a7752009-09-23 15:56:26 -07003295 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003296 *
3297 * Reading this file can return large amounts of data if a cgroup has
3298 * *lots* of attached tasks. So it may need several calls to read(),
3299 * but we cannot guarantee that the information we produce is correct
3300 * unless we produce it entirely atomically.
3301 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003302 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003303
Li Zefan24528252012-01-20 11:58:43 +08003304/* which pidlist file are we talking about? */
3305enum cgroup_filetype {
3306 CGROUP_FILE_PROCS,
3307 CGROUP_FILE_TASKS,
3308};
3309
3310/*
3311 * A pidlist is a list of pids that virtually represents the contents of one
3312 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3313 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3314 * to the cgroup.
3315 */
3316struct cgroup_pidlist {
3317 /*
3318 * used to find which pidlist is wanted. doesn't change as long as
3319 * this particular list stays in the list.
3320 */
3321 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3322 /* array of xids */
3323 pid_t *list;
3324 /* how many elements the above list has */
3325 int length;
3326 /* how many files are using the current array */
3327 int use_count;
3328 /* each of these stored in a list by its cgroup */
3329 struct list_head links;
3330 /* pointer to the cgroup we belong to, for list removal purposes */
3331 struct cgroup *owner;
3332 /* protects the other fields */
3333 struct rw_semaphore mutex;
3334};
3335
Paul Menagebbcb81d2007-10-18 23:39:32 -07003336/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003337 * The following two functions "fix" the issue where there are more pids
3338 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3339 * TODO: replace with a kernel-wide solution to this problem
3340 */
3341#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3342static void *pidlist_allocate(int count)
3343{
3344 if (PIDLIST_TOO_LARGE(count))
3345 return vmalloc(count * sizeof(pid_t));
3346 else
3347 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3348}
3349static void pidlist_free(void *p)
3350{
3351 if (is_vmalloc_addr(p))
3352 vfree(p);
3353 else
3354 kfree(p);
3355}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003356
3357/*
Ben Blum102a7752009-09-23 15:56:26 -07003358 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003359 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003360 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003361static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003362{
Ben Blum102a7752009-09-23 15:56:26 -07003363 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003364
3365 /*
3366 * we presume the 0th element is unique, so i starts at 1. trivial
3367 * edge cases first; no work needs to be done for either
3368 */
3369 if (length == 0 || length == 1)
3370 return length;
3371 /* src and dest walk down the list; dest counts unique elements */
3372 for (src = 1; src < length; src++) {
3373 /* find next unique element */
3374 while (list[src] == list[src-1]) {
3375 src++;
3376 if (src == length)
3377 goto after;
3378 }
3379 /* dest always points to where the next unique element goes */
3380 list[dest] = list[src];
3381 dest++;
3382 }
3383after:
Ben Blum102a7752009-09-23 15:56:26 -07003384 return dest;
3385}
3386
3387static int cmppid(const void *a, const void *b)
3388{
3389 return *(pid_t *)a - *(pid_t *)b;
3390}
3391
3392/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003393 * find the appropriate pidlist for our purpose (given procs vs tasks)
3394 * returns with the lock on that pidlist already held, and takes care
3395 * of the use count, or returns NULL with no locks held if we're out of
3396 * memory.
3397 */
3398static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3399 enum cgroup_filetype type)
3400{
3401 struct cgroup_pidlist *l;
3402 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003403 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003404
Ben Blum72a8cb32009-09-23 15:56:27 -07003405 /*
3406 * We can't drop the pidlist_mutex before taking the l->mutex in case
3407 * the last ref-holder is trying to remove l from the list at the same
3408 * time. Holding the pidlist_mutex precludes somebody taking whichever
3409 * list we find out from under us - compare release_pid_array().
3410 */
3411 mutex_lock(&cgrp->pidlist_mutex);
3412 list_for_each_entry(l, &cgrp->pidlists, links) {
3413 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003414 /* make sure l doesn't vanish out from under us */
3415 down_write(&l->mutex);
3416 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003417 return l;
3418 }
3419 }
3420 /* entry not found; create a new one */
3421 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3422 if (!l) {
3423 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003424 return l;
3425 }
3426 init_rwsem(&l->mutex);
3427 down_write(&l->mutex);
3428 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003429 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003430 l->use_count = 0; /* don't increment here */
3431 l->list = NULL;
3432 l->owner = cgrp;
3433 list_add(&l->links, &cgrp->pidlists);
3434 mutex_unlock(&cgrp->pidlist_mutex);
3435 return l;
3436}
3437
3438/*
Ben Blum102a7752009-09-23 15:56:26 -07003439 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3440 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003441static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3442 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003443{
3444 pid_t *array;
3445 int length;
3446 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003447 struct cgroup_iter it;
3448 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003449 struct cgroup_pidlist *l;
3450
3451 /*
3452 * If cgroup gets more users after we read count, we won't have
3453 * enough space - tough. This race is indistinguishable to the
3454 * caller from the case that the additional cgroup users didn't
3455 * show up until sometime later on.
3456 */
3457 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003458 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003459 if (!array)
3460 return -ENOMEM;
3461 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003462 cgroup_iter_start(cgrp, &it);
3463 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003464 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003465 break;
Ben Blum102a7752009-09-23 15:56:26 -07003466 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003467 if (type == CGROUP_FILE_PROCS)
3468 pid = task_tgid_vnr(tsk);
3469 else
3470 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003471 if (pid > 0) /* make sure to only use valid results */
3472 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003473 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003474 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003475 length = n;
3476 /* now sort & (if procs) strip out duplicates */
3477 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003478 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003479 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003480 l = cgroup_pidlist_find(cgrp, type);
3481 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003482 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003483 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003484 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003485 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003486 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003487 l->list = array;
3488 l->length = length;
3489 l->use_count++;
3490 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003491 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003492 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003493}
3494
Balbir Singh846c7bb2007-10-18 23:39:44 -07003495/**
Li Zefana043e3b2008-02-23 15:24:09 -08003496 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003497 * @stats: cgroupstats to fill information into
3498 * @dentry: A dentry entry belonging to the cgroup for which stats have
3499 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003500 *
3501 * Build and fill cgroupstats so that taskstats can export it to user
3502 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003503 */
3504int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3505{
3506 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003507 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003508 struct cgroup_iter it;
3509 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003510
Balbir Singh846c7bb2007-10-18 23:39:44 -07003511 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003512 * Validate dentry by checking the superblock operations,
3513 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003514 */
Li Zefan33d283b2008-11-19 15:36:48 -08003515 if (dentry->d_sb->s_op != &cgroup_ops ||
3516 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003517 goto err;
3518
3519 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003520 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003521
Paul Menagebd89aab2007-10-18 23:40:44 -07003522 cgroup_iter_start(cgrp, &it);
3523 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003524 switch (tsk->state) {
3525 case TASK_RUNNING:
3526 stats->nr_running++;
3527 break;
3528 case TASK_INTERRUPTIBLE:
3529 stats->nr_sleeping++;
3530 break;
3531 case TASK_UNINTERRUPTIBLE:
3532 stats->nr_uninterruptible++;
3533 break;
3534 case TASK_STOPPED:
3535 stats->nr_stopped++;
3536 break;
3537 default:
3538 if (delayacct_is_task_waiting_on_io(tsk))
3539 stats->nr_io_wait++;
3540 break;
3541 }
3542 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003543 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003544
Balbir Singh846c7bb2007-10-18 23:39:44 -07003545err:
3546 return ret;
3547}
3548
Paul Menage8f3ff202009-09-23 15:56:25 -07003549
Paul Menagecc31edc2008-10-18 20:28:04 -07003550/*
Ben Blum102a7752009-09-23 15:56:26 -07003551 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003552 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003553 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003554 */
3555
Ben Blum102a7752009-09-23 15:56:26 -07003556static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003557{
3558 /*
3559 * Initially we receive a position value that corresponds to
3560 * one more than the last pid shown (or 0 on the first call or
3561 * after a seek to the start). Use a binary-search to find the
3562 * next pid to display, if any
3563 */
Ben Blum102a7752009-09-23 15:56:26 -07003564 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003565 int index = 0, pid = *pos;
3566 int *iter;
3567
Ben Blum102a7752009-09-23 15:56:26 -07003568 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003569 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003570 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003571
Paul Menagecc31edc2008-10-18 20:28:04 -07003572 while (index < end) {
3573 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003574 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003575 index = mid;
3576 break;
Ben Blum102a7752009-09-23 15:56:26 -07003577 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003578 index = mid + 1;
3579 else
3580 end = mid;
3581 }
3582 }
3583 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003584 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003585 return NULL;
3586 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003587 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003588 *pos = *iter;
3589 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003590}
3591
Ben Blum102a7752009-09-23 15:56:26 -07003592static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003593{
Ben Blum102a7752009-09-23 15:56:26 -07003594 struct cgroup_pidlist *l = s->private;
3595 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003596}
3597
Ben Blum102a7752009-09-23 15:56:26 -07003598static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003599{
Ben Blum102a7752009-09-23 15:56:26 -07003600 struct cgroup_pidlist *l = s->private;
3601 pid_t *p = v;
3602 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003603 /*
3604 * Advance to the next pid in the array. If this goes off the
3605 * end, we're done
3606 */
3607 p++;
3608 if (p >= end) {
3609 return NULL;
3610 } else {
3611 *pos = *p;
3612 return p;
3613 }
3614}
3615
Ben Blum102a7752009-09-23 15:56:26 -07003616static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003617{
3618 return seq_printf(s, "%d\n", *(int *)v);
3619}
3620
Ben Blum102a7752009-09-23 15:56:26 -07003621/*
3622 * seq_operations functions for iterating on pidlists through seq_file -
3623 * independent of whether it's tasks or procs
3624 */
3625static const struct seq_operations cgroup_pidlist_seq_operations = {
3626 .start = cgroup_pidlist_start,
3627 .stop = cgroup_pidlist_stop,
3628 .next = cgroup_pidlist_next,
3629 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003630};
3631
Ben Blum102a7752009-09-23 15:56:26 -07003632static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003633{
Ben Blum72a8cb32009-09-23 15:56:27 -07003634 /*
3635 * the case where we're the last user of this particular pidlist will
3636 * have us remove it from the cgroup's list, which entails taking the
3637 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3638 * pidlist_mutex, we have to take pidlist_mutex first.
3639 */
3640 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003641 down_write(&l->mutex);
3642 BUG_ON(!l->use_count);
3643 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003644 /* we're the last user if refcount is 0; remove and free */
3645 list_del(&l->links);
3646 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003647 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003648 put_pid_ns(l->key.ns);
3649 up_write(&l->mutex);
3650 kfree(l);
3651 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003652 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003653 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003654 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003655}
3656
Ben Blum102a7752009-09-23 15:56:26 -07003657static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003658{
Ben Blum102a7752009-09-23 15:56:26 -07003659 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003660 if (!(file->f_mode & FMODE_READ))
3661 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003662 /*
3663 * the seq_file will only be initialized if the file was opened for
3664 * reading; hence we check if it's not null only in that case.
3665 */
3666 l = ((struct seq_file *)file->private_data)->private;
3667 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003668 return seq_release(inode, file);
3669}
3670
Ben Blum102a7752009-09-23 15:56:26 -07003671static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003672 .read = seq_read,
3673 .llseek = seq_lseek,
3674 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003675 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003676};
3677
3678/*
Ben Blum102a7752009-09-23 15:56:26 -07003679 * The following functions handle opens on a file that displays a pidlist
3680 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3681 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003682 */
Ben Blum102a7752009-09-23 15:56:26 -07003683/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003684static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003685{
3686 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003687 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003688 int retval;
3689
3690 /* Nothing to do for write-only files */
3691 if (!(file->f_mode & FMODE_READ))
3692 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003693
Ben Blum102a7752009-09-23 15:56:26 -07003694 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003695 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003696 if (retval)
3697 return retval;
3698 /* configure file information */
3699 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003700
Ben Blum102a7752009-09-23 15:56:26 -07003701 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003702 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003703 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003704 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003705 }
Ben Blum102a7752009-09-23 15:56:26 -07003706 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003707 return 0;
3708}
Ben Blum102a7752009-09-23 15:56:26 -07003709static int cgroup_tasks_open(struct inode *unused, struct file *file)
3710{
Ben Blum72a8cb32009-09-23 15:56:27 -07003711 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003712}
3713static int cgroup_procs_open(struct inode *unused, struct file *file)
3714{
Ben Blum72a8cb32009-09-23 15:56:27 -07003715 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003716}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003717
Paul Menagebd89aab2007-10-18 23:40:44 -07003718static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003719 struct cftype *cft)
3720{
Paul Menagebd89aab2007-10-18 23:40:44 -07003721 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003722}
3723
Paul Menage6379c102008-07-25 01:47:01 -07003724static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3725 struct cftype *cft,
3726 u64 val)
3727{
3728 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3729 if (val)
3730 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3731 else
3732 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3733 return 0;
3734}
3735
Paul Menagebbcb81d2007-10-18 23:39:32 -07003736/*
Li Zefan83d0eb72013-06-18 18:41:10 +08003737 * When dput() is called asynchronously, if umount has been done and
3738 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3739 * there's a small window that vfs will see the root dentry with non-zero
3740 * refcnt and trigger BUG().
3741 *
3742 * That's why we hold a reference before dput() and drop it right after.
3743 */
3744static void cgroup_dput(struct cgroup *cgrp)
3745{
3746 struct super_block *sb = cgrp->root->sb;
3747
3748 atomic_inc(&sb->s_active);
3749 dput(cgrp->dentry);
3750 deactivate_super(sb);
3751}
3752
3753/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003754 * Unregister event and free resources.
3755 *
3756 * Gets called from workqueue.
3757 */
3758static void cgroup_event_remove(struct work_struct *work)
3759{
3760 struct cgroup_event *event = container_of(work, struct cgroup_event,
3761 remove);
3762 struct cgroup *cgrp = event->cgrp;
3763
Li Zefan810cbee2013-02-18 18:56:14 +08003764 remove_wait_queue(event->wqh, &event->wait);
3765
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003766 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3767
Li Zefan810cbee2013-02-18 18:56:14 +08003768 /* Notify userspace the event is going away. */
3769 eventfd_signal(event->eventfd, 1);
3770
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003771 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003772 kfree(event);
Li Zefan83d0eb72013-06-18 18:41:10 +08003773 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003774}
3775
3776/*
3777 * Gets called on POLLHUP on eventfd when user closes it.
3778 *
3779 * Called with wqh->lock held and interrupts disabled.
3780 */
3781static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3782 int sync, void *key)
3783{
3784 struct cgroup_event *event = container_of(wait,
3785 struct cgroup_event, wait);
3786 struct cgroup *cgrp = event->cgrp;
3787 unsigned long flags = (unsigned long)key;
3788
3789 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003790 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003791 * If the event has been detached at cgroup removal, we
3792 * can simply return knowing the other side will cleanup
3793 * for us.
3794 *
3795 * We can't race against event freeing since the other
3796 * side will require wqh->lock via remove_wait_queue(),
3797 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003798 */
Li Zefan810cbee2013-02-18 18:56:14 +08003799 spin_lock(&cgrp->event_list_lock);
3800 if (!list_empty(&event->list)) {
3801 list_del_init(&event->list);
3802 /*
3803 * We are in atomic context, but cgroup_event_remove()
3804 * may sleep, so we have to call it in workqueue.
3805 */
3806 schedule_work(&event->remove);
3807 }
3808 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003809 }
3810
3811 return 0;
3812}
3813
3814static void cgroup_event_ptable_queue_proc(struct file *file,
3815 wait_queue_head_t *wqh, poll_table *pt)
3816{
3817 struct cgroup_event *event = container_of(pt,
3818 struct cgroup_event, pt);
3819
3820 event->wqh = wqh;
3821 add_wait_queue(wqh, &event->wait);
3822}
3823
3824/*
3825 * Parse input and register new cgroup event handler.
3826 *
3827 * Input must be in format '<event_fd> <control_fd> <args>'.
3828 * Interpretation of args is defined by control file implementation.
3829 */
3830static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3831 const char *buffer)
3832{
3833 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003834 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003835 unsigned int efd, cfd;
3836 struct file *efile = NULL;
3837 struct file *cfile = NULL;
3838 char *endp;
3839 int ret;
3840
3841 efd = simple_strtoul(buffer, &endp, 10);
3842 if (*endp != ' ')
3843 return -EINVAL;
3844 buffer = endp + 1;
3845
3846 cfd = simple_strtoul(buffer, &endp, 10);
3847 if ((*endp != ' ') && (*endp != '\0'))
3848 return -EINVAL;
3849 buffer = endp + 1;
3850
3851 event = kzalloc(sizeof(*event), GFP_KERNEL);
3852 if (!event)
3853 return -ENOMEM;
3854 event->cgrp = cgrp;
3855 INIT_LIST_HEAD(&event->list);
3856 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3857 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3858 INIT_WORK(&event->remove, cgroup_event_remove);
3859
3860 efile = eventfd_fget(efd);
3861 if (IS_ERR(efile)) {
3862 ret = PTR_ERR(efile);
3863 goto fail;
3864 }
3865
3866 event->eventfd = eventfd_ctx_fileget(efile);
3867 if (IS_ERR(event->eventfd)) {
3868 ret = PTR_ERR(event->eventfd);
3869 goto fail;
3870 }
3871
3872 cfile = fget(cfd);
3873 if (!cfile) {
3874 ret = -EBADF;
3875 goto fail;
3876 }
3877
3878 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003879 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003880 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003881 if (ret < 0)
3882 goto fail;
3883
3884 event->cft = __file_cft(cfile);
3885 if (IS_ERR(event->cft)) {
3886 ret = PTR_ERR(event->cft);
3887 goto fail;
3888 }
3889
Li Zefanf1690072013-02-18 14:13:35 +08003890 /*
3891 * The file to be monitored must be in the same cgroup as
3892 * cgroup.event_control is.
3893 */
3894 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3895 if (cgrp_cfile != cgrp) {
3896 ret = -EINVAL;
3897 goto fail;
3898 }
3899
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003900 if (!event->cft->register_event || !event->cft->unregister_event) {
3901 ret = -EINVAL;
3902 goto fail;
3903 }
3904
3905 ret = event->cft->register_event(cgrp, event->cft,
3906 event->eventfd, buffer);
3907 if (ret)
3908 goto fail;
3909
Li Zefan7ef70e42013-04-26 11:58:03 -07003910 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003911
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003912 /*
3913 * Events should be removed after rmdir of cgroup directory, but before
3914 * destroying subsystem state objects. Let's take reference to cgroup
3915 * directory dentry to do that.
3916 */
3917 dget(cgrp->dentry);
3918
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003919 spin_lock(&cgrp->event_list_lock);
3920 list_add(&event->list, &cgrp->event_list);
3921 spin_unlock(&cgrp->event_list_lock);
3922
3923 fput(cfile);
3924 fput(efile);
3925
3926 return 0;
3927
3928fail:
3929 if (cfile)
3930 fput(cfile);
3931
3932 if (event && event->eventfd && !IS_ERR(event->eventfd))
3933 eventfd_ctx_put(event->eventfd);
3934
3935 if (!IS_ERR_OR_NULL(efile))
3936 fput(efile);
3937
3938 kfree(event);
3939
3940 return ret;
3941}
3942
Daniel Lezcano97978e62010-10-27 15:33:35 -07003943static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3944 struct cftype *cft)
3945{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003946 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003947}
3948
3949static int cgroup_clone_children_write(struct cgroup *cgrp,
3950 struct cftype *cft,
3951 u64 val)
3952{
3953 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003954 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003955 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003956 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003957 return 0;
3958}
3959
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003960/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003961 * for the common functions, 'private' gives the type of file
3962 */
Ben Blum102a7752009-09-23 15:56:26 -07003963/* for hysterical raisins, we can't put this on the older files */
3964#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003965static struct cftype files[] = {
3966 {
3967 .name = "tasks",
3968 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003969 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003970 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003971 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003972 },
Ben Blum102a7752009-09-23 15:56:26 -07003973 {
3974 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3975 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003976 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003977 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003978 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003979 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003980 {
3981 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003982 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003983 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003984 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003985 {
3986 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3987 .write_string = cgroup_write_event_control,
3988 .mode = S_IWUGO,
3989 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003990 {
3991 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003992 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003993 .read_u64 = cgroup_clone_children_read,
3994 .write_u64 = cgroup_clone_children_write,
3995 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003996 {
Tejun Heo873fe092013-04-14 20:15:26 -07003997 .name = "cgroup.sane_behavior",
3998 .flags = CFTYPE_ONLY_ON_ROOT,
3999 .read_seq_string = cgroup_sane_behavior_show,
4000 },
4001 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004002 .name = "release_agent",
4003 .flags = CFTYPE_ONLY_ON_ROOT,
4004 .read_seq_string = cgroup_release_agent_show,
4005 .write_string = cgroup_release_agent_write,
4006 .max_write_len = PATH_MAX,
4007 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004008 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004009};
4010
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004011/**
4012 * cgroup_populate_dir - selectively creation of files in a directory
4013 * @cgrp: target cgroup
4014 * @base_files: true if the base files should be added
4015 * @subsys_mask: mask of the subsystem ids whose files should be added
4016 */
4017static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4018 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004019{
4020 int err;
4021 struct cgroup_subsys *ss;
4022
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004023 if (base_files) {
4024 err = cgroup_addrm_files(cgrp, NULL, files, true);
4025 if (err < 0)
4026 return err;
4027 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004028
Tejun Heo8e3f6542012-04-01 12:09:55 -07004029 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004030 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004031 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004032 if (!test_bit(ss->subsys_id, &subsys_mask))
4033 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004034
Tejun Heodb0416b2012-04-01 12:09:55 -07004035 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004036 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004037 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004038
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004039 /* This cgroup is ready now */
4040 for_each_subsys(cgrp->root, ss) {
4041 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4042 /*
4043 * Update id->css pointer and make this css visible from
4044 * CSS ID functions. This pointer will be dereferened
4045 * from RCU-read-side without locks.
4046 */
4047 if (css->id)
4048 rcu_assign_pointer(css->id->css, css);
4049 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004050
4051 return 0;
4052}
4053
Tejun Heo48ddbe12012-04-01 12:09:56 -07004054static void css_dput_fn(struct work_struct *work)
4055{
4056 struct cgroup_subsys_state *css =
4057 container_of(work, struct cgroup_subsys_state, dput_work);
4058
Li Zefan83d0eb72013-06-18 18:41:10 +08004059 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004060}
4061
Paul Menageddbcc7e2007-10-18 23:39:30 -07004062static void init_cgroup_css(struct cgroup_subsys_state *css,
4063 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004064 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004065{
Paul Menagebd89aab2007-10-18 23:40:44 -07004066 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004067 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004068 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004069 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004070 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004071 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004072 BUG_ON(cgrp->subsys[ss->subsys_id]);
4073 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004074
4075 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004076 * css holds an extra ref to @cgrp->dentry which is put on the last
4077 * css_put(). dput() requires process context, which css_put() may
4078 * be called without. @css->dput_work will be used to invoke
4079 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004080 */
4081 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004082}
4083
Tejun Heob1929db2012-11-19 08:13:38 -08004084/* invoke ->post_create() on a new CSS and mark it online if successful */
4085static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004086{
Tejun Heob1929db2012-11-19 08:13:38 -08004087 int ret = 0;
4088
Tejun Heoa31f2d32012-11-19 08:13:37 -08004089 lockdep_assert_held(&cgroup_mutex);
4090
Tejun Heo92fb9742012-11-19 08:13:38 -08004091 if (ss->css_online)
4092 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004093 if (!ret)
4094 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4095 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004096}
4097
4098/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4099static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4100 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4101{
4102 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4103
4104 lockdep_assert_held(&cgroup_mutex);
4105
4106 if (!(css->flags & CSS_ONLINE))
4107 return;
4108
Li Zefand7eeac12013-03-12 15:35:59 -07004109 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004110 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004111
4112 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4113}
4114
Paul Menageddbcc7e2007-10-18 23:39:30 -07004115/*
Li Zefana043e3b2008-02-23 15:24:09 -08004116 * cgroup_create - create a cgroup
4117 * @parent: cgroup that will be parent of the new cgroup
4118 * @dentry: dentry of the new cgroup
4119 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004120 *
Li Zefana043e3b2008-02-23 15:24:09 -08004121 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004122 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004123static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004124 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004125{
Paul Menagebd89aab2007-10-18 23:40:44 -07004126 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004127 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004128 struct cgroupfs_root *root = parent->root;
4129 int err = 0;
4130 struct cgroup_subsys *ss;
4131 struct super_block *sb = root->sb;
4132
Tejun Heo0a950f62012-11-19 09:02:12 -08004133 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004134 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4135 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004136 return -ENOMEM;
4137
Li Zefan65dff752013-03-01 15:01:56 +08004138 name = cgroup_alloc_name(dentry);
4139 if (!name)
4140 goto err_free_cgrp;
4141 rcu_assign_pointer(cgrp->name, name);
4142
Tejun Heo0a950f62012-11-19 09:02:12 -08004143 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4144 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004145 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004146
Tejun Heo976c06b2012-11-05 09:16:59 -08004147 /*
4148 * Only live parents can have children. Note that the liveliness
4149 * check isn't strictly necessary because cgroup_mkdir() and
4150 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4151 * anyway so that locking is contained inside cgroup proper and we
4152 * don't get nasty surprises if we ever grow another caller.
4153 */
4154 if (!cgroup_lock_live_group(parent)) {
4155 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004156 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004157 }
4158
Paul Menageddbcc7e2007-10-18 23:39:30 -07004159 /* Grab a reference on the superblock so the hierarchy doesn't
4160 * get deleted on unmount if there are child cgroups. This
4161 * can be done outside cgroup_mutex, since the sb can't
4162 * disappear while someone has an open control file on the
4163 * fs */
4164 atomic_inc(&sb->s_active);
4165
Paul Menagecc31edc2008-10-18 20:28:04 -07004166 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004167
Li Zefanfe1c06c2013-01-24 14:30:22 +08004168 dentry->d_fsdata = cgrp;
4169 cgrp->dentry = dentry;
4170
Paul Menagebd89aab2007-10-18 23:40:44 -07004171 cgrp->parent = parent;
4172 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004173
Li Zefanb6abdb02008-03-04 14:28:19 -08004174 if (notify_on_release(parent))
4175 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4176
Tejun Heo2260e7f2012-11-19 08:13:38 -08004177 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4178 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004179
Paul Menageddbcc7e2007-10-18 23:39:30 -07004180 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004181 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004182
Tejun Heo92fb9742012-11-19 08:13:38 -08004183 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004184 if (IS_ERR(css)) {
4185 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004186 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004187 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004188 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004189 if (ss->use_id) {
4190 err = alloc_css_id(ss, parent, cgrp);
4191 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004192 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004193 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004194 }
4195
Tejun Heo4e139af2012-11-19 08:13:36 -08004196 /*
4197 * Create directory. cgroup_create_file() returns with the new
4198 * directory locked on success so that it can be populated without
4199 * dropping cgroup_mutex.
4200 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004201 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004202 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004203 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004204 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004205
Tejun Heo4e139af2012-11-19 08:13:36 -08004206 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004207 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4208 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4209 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004210
Tejun Heob1929db2012-11-19 08:13:38 -08004211 /* each css holds a ref to the cgroup's dentry */
4212 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004213 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004214
Li Zefan415cf072013-04-08 14:35:02 +08004215 /* hold a ref to the parent's dentry */
4216 dget(parent->dentry);
4217
Tejun Heob1929db2012-11-19 08:13:38 -08004218 /* creation succeeded, notify subsystems */
4219 for_each_subsys(root, ss) {
4220 err = online_css(ss, cgrp);
4221 if (err)
4222 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004223
4224 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4225 parent->parent) {
4226 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",
4227 current->comm, current->pid, ss->name);
4228 if (!strcmp(ss->name, "memory"))
4229 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4230 ss->warned_broken_hierarchy = true;
4231 }
Tejun Heoa8638032012-11-09 09:12:29 -08004232 }
4233
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004234 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004235 if (err)
4236 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004237
4238 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004239 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004240
4241 return 0;
4242
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004243err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004244 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004245 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004246 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004247 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004248 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249 /* Release the reference count that we took on the superblock */
4250 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004251err_free_id:
4252 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004253err_free_name:
4254 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004255err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004256 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004257 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004258
4259err_destroy:
4260 cgroup_destroy_locked(cgrp);
4261 mutex_unlock(&cgroup_mutex);
4262 mutex_unlock(&dentry->d_inode->i_mutex);
4263 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004264}
4265
Al Viro18bb1db2011-07-26 01:41:39 -04004266static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004267{
4268 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4269
4270 /* the vfs holds inode->i_mutex already */
4271 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4272}
4273
Tejun Heo42809dd2012-11-19 08:13:37 -08004274static int cgroup_destroy_locked(struct cgroup *cgrp)
4275 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004276{
Tejun Heo42809dd2012-11-19 08:13:37 -08004277 struct dentry *d = cgrp->dentry;
4278 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004279 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004280 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004281
Tejun Heo42809dd2012-11-19 08:13:37 -08004282 lockdep_assert_held(&d->d_inode->i_mutex);
4283 lockdep_assert_held(&cgroup_mutex);
4284
4285 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004286 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004287
Tejun Heo1a90dd52012-11-05 09:16:59 -08004288 /*
4289 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4290 * removed. This makes future css_tryget() and child creation
4291 * attempts fail thus maintaining the removal conditions verified
4292 * above.
4293 */
Tejun Heoed9577932012-11-05 09:16:58 -08004294 for_each_subsys(cgrp->root, ss) {
4295 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4296
4297 WARN_ON(atomic_read(&css->refcnt) < 0);
4298 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004299 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004300 set_bit(CGRP_REMOVED, &cgrp->flags);
4301
Tejun Heoa31f2d32012-11-19 08:13:37 -08004302 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004303 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004304 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004305
4306 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004307 * Put all the base refs. Each css holds an extra reference to the
4308 * cgroup's dentry and cgroup removal proceeds regardless of css
4309 * refs. On the last put of each css, whenever that may be, the
4310 * extra dentry ref is put so that dentry destruction happens only
4311 * after all css's are released.
4312 */
Tejun Heoe9316082012-11-05 09:16:58 -08004313 for_each_subsys(cgrp->root, ss)
4314 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004315
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004316 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004317 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004318 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004319 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004320
Paul Menage999cd8a2009-01-07 18:08:36 -08004321 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004322 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004323 list_del_init(&cgrp->allcg_node);
4324
Tejun Heo42809dd2012-11-19 08:13:37 -08004325 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004326 cgroup_d_remove_dir(d);
4327 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004328
Paul Menagebd89aab2007-10-18 23:40:44 -07004329 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004330 check_for_release(parent);
4331
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004332 /*
4333 * Unregister events and notify userspace.
4334 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004335 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004336 */
4337 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004338 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004339 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004340 schedule_work(&event->remove);
4341 }
Li Zefan810cbee2013-02-18 18:56:14 +08004342 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004343
Paul Menageddbcc7e2007-10-18 23:39:30 -07004344 return 0;
4345}
4346
Tejun Heo42809dd2012-11-19 08:13:37 -08004347static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4348{
4349 int ret;
4350
4351 mutex_lock(&cgroup_mutex);
4352 ret = cgroup_destroy_locked(dentry->d_fsdata);
4353 mutex_unlock(&cgroup_mutex);
4354
4355 return ret;
4356}
4357
Tejun Heo8e3f6542012-04-01 12:09:55 -07004358static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4359{
4360 INIT_LIST_HEAD(&ss->cftsets);
4361
4362 /*
4363 * base_cftset is embedded in subsys itself, no need to worry about
4364 * deregistration.
4365 */
4366 if (ss->base_cftypes) {
4367 ss->base_cftset.cfts = ss->base_cftypes;
4368 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4369 }
4370}
4371
Li Zefan06a11922008-04-29 01:00:07 -07004372static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004373{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004374 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004375
4376 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004377
Tejun Heo648bb562012-11-19 08:13:36 -08004378 mutex_lock(&cgroup_mutex);
4379
Tejun Heo8e3f6542012-04-01 12:09:55 -07004380 /* init base cftset */
4381 cgroup_init_cftsets(ss);
4382
Paul Menageddbcc7e2007-10-18 23:39:30 -07004383 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004384 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004385 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004386 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004387 /* We don't handle early failures gracefully */
4388 BUG_ON(IS_ERR(css));
4389 init_cgroup_css(css, ss, dummytop);
4390
Li Zefane8d55fd2008-04-29 01:00:13 -07004391 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004392 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004393 * newly registered, all tasks and hence the
4394 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004395 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004396
4397 need_forkexit_callback |= ss->fork || ss->exit;
4398
Li Zefane8d55fd2008-04-29 01:00:13 -07004399 /* At system boot, before all subsystems have been
4400 * registered, no tasks have been forked, so we don't
4401 * need to invoke fork callbacks here. */
4402 BUG_ON(!list_empty(&init_task.tasks));
4403
Tejun Heob1929db2012-11-19 08:13:38 -08004404 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004405
Tejun Heo648bb562012-11-19 08:13:36 -08004406 mutex_unlock(&cgroup_mutex);
4407
Ben Blume6a11052010-03-10 15:22:09 -08004408 /* this function shouldn't be used with modular subsystems, since they
4409 * need to register a subsys_id, among other things */
4410 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004411}
4412
4413/**
Ben Blume6a11052010-03-10 15:22:09 -08004414 * cgroup_load_subsys: load and register a modular subsystem at runtime
4415 * @ss: the subsystem to load
4416 *
4417 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004418 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004419 * up for use. If the subsystem is built-in anyway, work is delegated to the
4420 * simpler cgroup_init_subsys.
4421 */
4422int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4423{
Ben Blume6a11052010-03-10 15:22:09 -08004424 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004425 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004426 struct hlist_node *tmp;
Li Zefan0ac801f2013-01-10 11:49:27 +08004427 struct css_set *cg;
4428 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004429
4430 /* check name and function validity */
4431 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004432 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004433 return -EINVAL;
4434
4435 /*
4436 * we don't support callbacks in modular subsystems. this check is
4437 * before the ss->module check for consistency; a subsystem that could
4438 * be a module should still have no callbacks even if the user isn't
4439 * compiling it as one.
4440 */
4441 if (ss->fork || ss->exit)
4442 return -EINVAL;
4443
4444 /*
4445 * an optionally modular subsystem is built-in: we want to do nothing,
4446 * since cgroup_init_subsys will have already taken care of it.
4447 */
4448 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004449 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004450 BUG_ON(subsys[ss->subsys_id] != ss);
4451 return 0;
4452 }
4453
Tejun Heo8e3f6542012-04-01 12:09:55 -07004454 /* init base cftset */
4455 cgroup_init_cftsets(ss);
4456
Ben Blume6a11052010-03-10 15:22:09 -08004457 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004458 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004459
4460 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004461 * no ss->css_alloc seems to need anything important in the ss
4462 * struct, so this can happen first (i.e. before the rootnode
4463 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004464 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004465 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004466 if (IS_ERR(css)) {
4467 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004468 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004469 mutex_unlock(&cgroup_mutex);
4470 return PTR_ERR(css);
4471 }
4472
4473 list_add(&ss->sibling, &rootnode.subsys_list);
4474 ss->root = &rootnode;
4475
4476 /* our new subsystem will be attached to the dummy hierarchy. */
4477 init_cgroup_css(css, ss, dummytop);
4478 /* init_idr must be after init_cgroup_css because it sets css->id. */
4479 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004480 ret = cgroup_init_idr(ss, css);
4481 if (ret)
4482 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004483 }
4484
4485 /*
4486 * Now we need to entangle the css into the existing css_sets. unlike
4487 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4488 * will need a new pointer to it; done by iterating the css_set_table.
4489 * furthermore, modifying the existing css_sets will corrupt the hash
4490 * table state, so each changed css_set will need its hash recomputed.
4491 * this is all done under the css_set_lock.
4492 */
4493 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004494 hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004495 /* skip entries that we already rehashed */
4496 if (cg->subsys[ss->subsys_id])
4497 continue;
4498 /* remove existing entry */
4499 hash_del(&cg->hlist);
4500 /* set new value */
4501 cg->subsys[ss->subsys_id] = css;
4502 /* recompute hash and restore entry */
4503 key = css_set_hash(cg->subsys);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004504 hash_add(css_set_table, &cg->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004505 }
4506 write_unlock(&css_set_lock);
4507
Tejun Heob1929db2012-11-19 08:13:38 -08004508 ret = online_css(ss, dummytop);
4509 if (ret)
4510 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004511
Ben Blume6a11052010-03-10 15:22:09 -08004512 /* success! */
4513 mutex_unlock(&cgroup_mutex);
4514 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004515
4516err_unload:
4517 mutex_unlock(&cgroup_mutex);
4518 /* @ss can't be mounted here as try_module_get() would fail */
4519 cgroup_unload_subsys(ss);
4520 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004521}
4522EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4523
4524/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004525 * cgroup_unload_subsys: unload a modular subsystem
4526 * @ss: the subsystem to unload
4527 *
4528 * This function should be called in a modular subsystem's exitcall. When this
4529 * function is invoked, the refcount on the subsystem's module will be 0, so
4530 * the subsystem will not be attached to any hierarchy.
4531 */
4532void cgroup_unload_subsys(struct cgroup_subsys *ss)
4533{
4534 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004535
4536 BUG_ON(ss->module == NULL);
4537
4538 /*
4539 * we shouldn't be called if the subsystem is in use, and the use of
4540 * try_module_get in parse_cgroupfs_options should ensure that it
4541 * doesn't start being used while we're killing it off.
4542 */
4543 BUG_ON(ss->root != &rootnode);
4544
4545 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004546
Tejun Heoa31f2d32012-11-19 08:13:37 -08004547 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004548
Tejun Heoc897ff62013-02-27 17:03:49 -08004549 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004550 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004551
Ben Blumcf5d5942010-03-10 15:22:09 -08004552 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004553 subsys[ss->subsys_id] = NULL;
4554
4555 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004556 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004557
4558 /*
4559 * disentangle the css from all css_sets attached to the dummytop. as
4560 * in loading, we need to pay our respects to the hashtable gods.
4561 */
4562 write_lock(&css_set_lock);
4563 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4564 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004565 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004566
Li Zefan0ac801f2013-01-10 11:49:27 +08004567 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004568 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004569 key = css_set_hash(cg->subsys);
4570 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004571 }
4572 write_unlock(&css_set_lock);
4573
4574 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004575 * remove subsystem's css from the dummytop and free it - need to
4576 * free before marking as null because ss->css_free needs the
4577 * cgrp->subsys pointer to find their state. note that this also
4578 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004579 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004580 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004581 dummytop->subsys[ss->subsys_id] = NULL;
4582
4583 mutex_unlock(&cgroup_mutex);
4584}
4585EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4586
4587/**
Li Zefana043e3b2008-02-23 15:24:09 -08004588 * cgroup_init_early - cgroup initialization at system boot
4589 *
4590 * Initialize cgroups at system boot, and initialize any
4591 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004592 */
4593int __init cgroup_init_early(void)
4594{
4595 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004596 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004597 INIT_LIST_HEAD(&init_css_set.cg_links);
4598 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004599 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004600 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004601 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004602 root_count = 1;
4603 init_task.cgroups = &init_css_set;
4604
4605 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004606 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004607 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004608 &rootnode.top_cgroup.css_sets);
4609 list_add(&init_css_set_link.cg_link_list,
4610 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004611
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004612 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004613 struct cgroup_subsys *ss = subsys[i];
4614
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004615 /* at bootup time, we don't worry about modular subsystems */
4616 if (!ss || ss->module)
4617 continue;
4618
Paul Menageddbcc7e2007-10-18 23:39:30 -07004619 BUG_ON(!ss->name);
4620 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004621 BUG_ON(!ss->css_alloc);
4622 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004623 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004624 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004625 ss->name, ss->subsys_id);
4626 BUG();
4627 }
4628
4629 if (ss->early_init)
4630 cgroup_init_subsys(ss);
4631 }
4632 return 0;
4633}
4634
4635/**
Li Zefana043e3b2008-02-23 15:24:09 -08004636 * cgroup_init - cgroup initialization
4637 *
4638 * Register cgroup filesystem and /proc file, and initialize
4639 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004640 */
4641int __init cgroup_init(void)
4642{
4643 int err;
4644 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004645 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004646
4647 err = bdi_init(&cgroup_backing_dev_info);
4648 if (err)
4649 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004650
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004651 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004652 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004653
4654 /* at bootup time, we don't worry about modular subsystems */
4655 if (!ss || ss->module)
4656 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004657 if (!ss->early_init)
4658 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004659 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004660 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004661 }
4662
Li Zefan472b1052008-04-29 01:00:11 -07004663 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004664 key = css_set_hash(init_css_set.subsys);
4665 hash_add(css_set_table, &init_css_set.hlist, key);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004666 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004667
4668 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4669 if (!cgroup_kobj) {
4670 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004671 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004672 }
4673
4674 err = register_filesystem(&cgroup_fs_type);
4675 if (err < 0) {
4676 kobject_put(cgroup_kobj);
4677 goto out;
4678 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004679
Li Zefan46ae2202008-04-29 01:00:08 -07004680 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004681
Paul Menageddbcc7e2007-10-18 23:39:30 -07004682out:
Paul Menagea4243162007-10-18 23:39:35 -07004683 if (err)
4684 bdi_destroy(&cgroup_backing_dev_info);
4685
Paul Menageddbcc7e2007-10-18 23:39:30 -07004686 return err;
4687}
Paul Menageb4f48b62007-10-18 23:39:33 -07004688
Paul Menagea4243162007-10-18 23:39:35 -07004689/*
4690 * proc_cgroup_show()
4691 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4692 * - Used for /proc/<pid>/cgroup.
4693 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4694 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004695 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004696 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4697 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4698 * cgroup to top_cgroup.
4699 */
4700
4701/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004702int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004703{
4704 struct pid *pid;
4705 struct task_struct *tsk;
4706 char *buf;
4707 int retval;
4708 struct cgroupfs_root *root;
4709
4710 retval = -ENOMEM;
4711 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4712 if (!buf)
4713 goto out;
4714
4715 retval = -ESRCH;
4716 pid = m->private;
4717 tsk = get_pid_task(pid, PIDTYPE_PID);
4718 if (!tsk)
4719 goto out_free;
4720
4721 retval = 0;
4722
4723 mutex_lock(&cgroup_mutex);
4724
Li Zefane5f6a862009-01-07 18:07:41 -08004725 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004726 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004727 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004728 int count = 0;
4729
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004730 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004731 for_each_subsys(root, ss)
4732 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004733 if (strlen(root->name))
4734 seq_printf(m, "%sname=%s", count ? "," : "",
4735 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004736 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004737 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004738 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004739 if (retval < 0)
4740 goto out_unlock;
4741 seq_puts(m, buf);
4742 seq_putc(m, '\n');
4743 }
4744
4745out_unlock:
4746 mutex_unlock(&cgroup_mutex);
4747 put_task_struct(tsk);
4748out_free:
4749 kfree(buf);
4750out:
4751 return retval;
4752}
4753
Paul Menagea4243162007-10-18 23:39:35 -07004754/* Display information about each subsystem and each hierarchy */
4755static int proc_cgroupstats_show(struct seq_file *m, void *v)
4756{
4757 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004758
Paul Menage8bab8dd2008-04-04 14:29:57 -07004759 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004760 /*
4761 * ideally we don't want subsystems moving around while we do this.
4762 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4763 * subsys/hierarchy state.
4764 */
Paul Menagea4243162007-10-18 23:39:35 -07004765 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004766 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4767 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004768 if (ss == NULL)
4769 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004770 seq_printf(m, "%s\t%d\t%d\t%d\n",
4771 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004772 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004773 }
4774 mutex_unlock(&cgroup_mutex);
4775 return 0;
4776}
4777
4778static int cgroupstats_open(struct inode *inode, struct file *file)
4779{
Al Viro9dce07f2008-03-29 03:07:28 +00004780 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004781}
4782
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004783static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004784 .open = cgroupstats_open,
4785 .read = seq_read,
4786 .llseek = seq_lseek,
4787 .release = single_release,
4788};
4789
Paul Menageb4f48b62007-10-18 23:39:33 -07004790/**
4791 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004792 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004793 *
4794 * Description: A task inherits its parent's cgroup at fork().
4795 *
4796 * A pointer to the shared css_set was automatically copied in
4797 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004798 * it was not made under the protection of RCU or cgroup_mutex, so
4799 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4800 * have already changed current->cgroups, allowing the previously
4801 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004802 *
4803 * At the point that cgroup_fork() is called, 'current' is the parent
4804 * task, and the passed argument 'child' points to the child task.
4805 */
4806void cgroup_fork(struct task_struct *child)
4807{
Tejun Heo9bb71302012-10-18 17:52:07 -07004808 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004809 child->cgroups = current->cgroups;
4810 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004811 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004812 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004813}
4814
4815/**
Li Zefana043e3b2008-02-23 15:24:09 -08004816 * cgroup_post_fork - called on a new task after adding it to the task list
4817 * @child: the task in question
4818 *
Tejun Heo5edee612012-10-16 15:03:14 -07004819 * Adds the task to the list running through its css_set if necessary and
4820 * call the subsystem fork() callbacks. Has to be after the task is
4821 * visible on the task list in case we race with the first call to
4822 * cgroup_iter_start() - to guarantee that the new task ends up on its
4823 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004824 */
Paul Menage817929e2007-10-18 23:39:36 -07004825void cgroup_post_fork(struct task_struct *child)
4826{
Tejun Heo5edee612012-10-16 15:03:14 -07004827 int i;
4828
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004829 /*
4830 * use_task_css_set_links is set to 1 before we walk the tasklist
4831 * under the tasklist_lock and we read it here after we added the child
4832 * to the tasklist under the tasklist_lock as well. If the child wasn't
4833 * yet in the tasklist when we walked through it from
4834 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4835 * should be visible now due to the paired locking and barriers implied
4836 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4837 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4838 * lock on fork.
4839 */
Paul Menage817929e2007-10-18 23:39:36 -07004840 if (use_task_css_set_links) {
4841 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004842 task_lock(child);
4843 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004844 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004845 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004846 write_unlock(&css_set_lock);
4847 }
Tejun Heo5edee612012-10-16 15:03:14 -07004848
4849 /*
4850 * Call ss->fork(). This must happen after @child is linked on
4851 * css_set; otherwise, @child might change state between ->fork()
4852 * and addition to css_set.
4853 */
4854 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004855 /*
4856 * fork/exit callbacks are supported only for builtin
4857 * subsystems, and the builtin section of the subsys
4858 * array is immutable, so we don't need to lock the
4859 * subsys array here. On the other hand, modular section
4860 * of the array can be freed at module unload, so we
4861 * can't touch that.
4862 */
4863 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004864 struct cgroup_subsys *ss = subsys[i];
4865
Tejun Heo5edee612012-10-16 15:03:14 -07004866 if (ss->fork)
4867 ss->fork(child);
4868 }
4869 }
Paul Menage817929e2007-10-18 23:39:36 -07004870}
Tejun Heo5edee612012-10-16 15:03:14 -07004871
Paul Menage817929e2007-10-18 23:39:36 -07004872/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004873 * cgroup_exit - detach cgroup from exiting task
4874 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004875 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004876 *
4877 * Description: Detach cgroup from @tsk and release it.
4878 *
4879 * Note that cgroups marked notify_on_release force every task in
4880 * them to take the global cgroup_mutex mutex when exiting.
4881 * This could impact scaling on very large systems. Be reluctant to
4882 * use notify_on_release cgroups where very high task exit scaling
4883 * is required on large systems.
4884 *
4885 * the_top_cgroup_hack:
4886 *
4887 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4888 *
4889 * We call cgroup_exit() while the task is still competent to
4890 * handle notify_on_release(), then leave the task attached to the
4891 * root cgroup in each hierarchy for the remainder of its exit.
4892 *
4893 * To do this properly, we would increment the reference count on
4894 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4895 * code we would add a second cgroup function call, to drop that
4896 * reference. This would just create an unnecessary hot spot on
4897 * the top_cgroup reference count, to no avail.
4898 *
4899 * Normally, holding a reference to a cgroup without bumping its
4900 * count is unsafe. The cgroup could go away, or someone could
4901 * attach us to a different cgroup, decrementing the count on
4902 * the first cgroup that we never incremented. But in this case,
4903 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004904 * which wards off any cgroup_attach_task() attempts, or task is a failed
4905 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004906 */
4907void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4908{
Paul Menage817929e2007-10-18 23:39:36 -07004909 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004910 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004911
4912 /*
4913 * Unlink from the css_set task list if necessary.
4914 * Optimistically check cg_list before taking
4915 * css_set_lock
4916 */
4917 if (!list_empty(&tsk->cg_list)) {
4918 write_lock(&css_set_lock);
4919 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004920 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004921 write_unlock(&css_set_lock);
4922 }
4923
Paul Menageb4f48b62007-10-18 23:39:33 -07004924 /* Reassign the task to the init_css_set. */
4925 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004926 cg = tsk->cgroups;
4927 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004928
4929 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004930 /*
4931 * fork/exit callbacks are supported only for builtin
4932 * subsystems, see cgroup_post_fork() for details.
4933 */
4934 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004935 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004936
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004937 if (ss->exit) {
4938 struct cgroup *old_cgrp =
4939 rcu_dereference_raw(cg->subsys[i])->cgroup;
4940 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004941 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004942 }
4943 }
4944 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004945 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004946
Li Zefanb5d646f2013-01-24 14:43:51 +08004947 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004948}
Paul Menage697f4162007-10-18 23:39:34 -07004949
Paul Menagebd89aab2007-10-18 23:40:44 -07004950static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004951{
4952 /* All of these checks rely on RCU to keep the cgroup
4953 * structure alive */
Li Zefanf50daa72013-03-01 15:06:07 +08004954 if (cgroup_is_releasable(cgrp) &&
4955 !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) {
4956 /*
4957 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004958 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004959 * it now
4960 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004961 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004962
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004963 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004964 if (!cgroup_is_removed(cgrp) &&
4965 list_empty(&cgrp->release_list)) {
4966 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004967 need_schedule_work = 1;
4968 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004969 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004970 if (need_schedule_work)
4971 schedule_work(&release_agent_work);
4972 }
4973}
4974
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004975/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07004976bool __css_tryget(struct cgroup_subsys_state *css)
4977{
Tejun Heoe9316082012-11-05 09:16:58 -08004978 while (true) {
4979 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004980
Tejun Heoe9316082012-11-05 09:16:58 -08004981 v = css_refcnt(css);
4982 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
4983 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07004984 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08004985 else if (t < 0)
4986 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07004987 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08004988 }
Tejun Heo28b4c272012-04-01 12:09:56 -07004989}
4990EXPORT_SYMBOL_GPL(__css_tryget);
4991
4992/* Caller must verify that the css is not for root cgroup */
4993void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004994{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004995 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004996
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004997 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08004998 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08004999 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005000}
Ben Blum67523c42010-03-10 15:22:11 -08005001EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005002
5003/*
5004 * Notify userspace when a cgroup is released, by running the
5005 * configured release agent with the name of the cgroup (path
5006 * relative to the root of cgroup file system) as the argument.
5007 *
5008 * Most likely, this user command will try to rmdir this cgroup.
5009 *
5010 * This races with the possibility that some other task will be
5011 * attached to this cgroup before it is removed, or that some other
5012 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5013 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5014 * unused, and this cgroup will be reprieved from its death sentence,
5015 * to continue to serve a useful existence. Next time it's released,
5016 * we will get notified again, if it still has 'notify_on_release' set.
5017 *
5018 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5019 * means only wait until the task is successfully execve()'d. The
5020 * separate release agent task is forked by call_usermodehelper(),
5021 * then control in this thread returns here, without waiting for the
5022 * release agent task. We don't bother to wait because the caller of
5023 * this routine has no use for the exit status of the release agent
5024 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005025 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005026static void cgroup_release_agent(struct work_struct *work)
5027{
5028 BUG_ON(work != &release_agent_work);
5029 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005030 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005031 while (!list_empty(&release_list)) {
5032 char *argv[3], *envp[3];
5033 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005034 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005035 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005036 struct cgroup,
5037 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005038 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005039 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005040 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005041 if (!pathbuf)
5042 goto continue_free;
5043 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5044 goto continue_free;
5045 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5046 if (!agentbuf)
5047 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005048
5049 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005050 argv[i++] = agentbuf;
5051 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005052 argv[i] = NULL;
5053
5054 i = 0;
5055 /* minimal command environment */
5056 envp[i++] = "HOME=/";
5057 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5058 envp[i] = NULL;
5059
5060 /* Drop the lock while we invoke the usermode helper,
5061 * since the exec could involve hitting disk and hence
5062 * be a slow process */
5063 mutex_unlock(&cgroup_mutex);
5064 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005065 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005066 continue_free:
5067 kfree(pathbuf);
5068 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005069 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005070 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005071 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005072 mutex_unlock(&cgroup_mutex);
5073}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005074
5075static int __init cgroup_disable(char *str)
5076{
5077 int i;
5078 char *token;
5079
5080 while ((token = strsep(&str, ",")) != NULL) {
5081 if (!*token)
5082 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005083 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005084 struct cgroup_subsys *ss = subsys[i];
5085
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005086 /*
5087 * cgroup_disable, being at boot time, can't
5088 * know about module subsystems, so we don't
5089 * worry about them.
5090 */
5091 if (!ss || ss->module)
5092 continue;
5093
Paul Menage8bab8dd2008-04-04 14:29:57 -07005094 if (!strcmp(token, ss->name)) {
5095 ss->disabled = 1;
5096 printk(KERN_INFO "Disabling %s control group"
5097 " subsystem\n", ss->name);
5098 break;
5099 }
5100 }
5101 }
5102 return 1;
5103}
5104__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005105
5106/*
5107 * Functons for CSS ID.
5108 */
5109
5110/*
5111 *To get ID other than 0, this should be called when !cgroup_is_removed().
5112 */
5113unsigned short css_id(struct cgroup_subsys_state *css)
5114{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005115 struct css_id *cssid;
5116
5117 /*
5118 * This css_id() can return correct value when somone has refcnt
5119 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5120 * it's unchanged until freed.
5121 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005122 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005123
5124 if (cssid)
5125 return cssid->id;
5126 return 0;
5127}
Ben Blum67523c42010-03-10 15:22:11 -08005128EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005129
5130unsigned short css_depth(struct cgroup_subsys_state *css)
5131{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005132 struct css_id *cssid;
5133
Tejun Heo28b4c272012-04-01 12:09:56 -07005134 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005135
5136 if (cssid)
5137 return cssid->depth;
5138 return 0;
5139}
Ben Blum67523c42010-03-10 15:22:11 -08005140EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005141
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005142/**
5143 * css_is_ancestor - test "root" css is an ancestor of "child"
5144 * @child: the css to be tested.
5145 * @root: the css supporsed to be an ancestor of the child.
5146 *
5147 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005148 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005149 * But, considering usual usage, the csses should be valid objects after test.
5150 * Assuming that the caller will do some action to the child if this returns
5151 * returns true, the caller must take "child";s reference count.
5152 * If "child" is valid object and this returns true, "root" is valid, too.
5153 */
5154
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005155bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005156 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005157{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005158 struct css_id *child_id;
5159 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005160
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005161 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005162 if (!child_id)
5163 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005164 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005165 if (!root_id)
5166 return false;
5167 if (child_id->depth < root_id->depth)
5168 return false;
5169 if (child_id->stack[root_id->depth] != root_id->id)
5170 return false;
5171 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005172}
5173
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005174void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5175{
5176 struct css_id *id = css->id;
5177 /* When this is called before css_id initialization, id can be NULL */
5178 if (!id)
5179 return;
5180
5181 BUG_ON(!ss->use_id);
5182
5183 rcu_assign_pointer(id->css, NULL);
5184 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005185 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005186 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005187 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005188 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005189}
Ben Blum67523c42010-03-10 15:22:11 -08005190EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005191
5192/*
5193 * This is called by init or create(). Then, calls to this function are
5194 * always serialized (By cgroup_mutex() at create()).
5195 */
5196
5197static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5198{
5199 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005200 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005201
5202 BUG_ON(!ss->use_id);
5203
5204 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5205 newid = kzalloc(size, GFP_KERNEL);
5206 if (!newid)
5207 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005208
5209 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005210 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005211 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005212 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005213 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005214 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005215
5216 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005217 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005218 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005219
Tejun Heod228d9e2013-02-27 17:04:54 -08005220 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005221 newid->depth = depth;
5222 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005223err_out:
5224 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005225 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005226
5227}
5228
Ben Blume6a11052010-03-10 15:22:09 -08005229static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5230 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005231{
5232 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005233
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005234 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005235 idr_init(&ss->idr);
5236
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005237 newid = get_new_cssid(ss, 0);
5238 if (IS_ERR(newid))
5239 return PTR_ERR(newid);
5240
5241 newid->stack[0] = newid->id;
5242 newid->css = rootcss;
5243 rootcss->id = newid;
5244 return 0;
5245}
5246
5247static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5248 struct cgroup *child)
5249{
5250 int subsys_id, i, depth = 0;
5251 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005252 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005253
5254 subsys_id = ss->subsys_id;
5255 parent_css = parent->subsys[subsys_id];
5256 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005257 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005258 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005259
5260 child_id = get_new_cssid(ss, depth);
5261 if (IS_ERR(child_id))
5262 return PTR_ERR(child_id);
5263
5264 for (i = 0; i < depth; i++)
5265 child_id->stack[i] = parent_id->stack[i];
5266 child_id->stack[depth] = child_id->id;
5267 /*
5268 * child_id->css pointer will be set after this cgroup is available
5269 * see cgroup_populate_dir()
5270 */
5271 rcu_assign_pointer(child_css->id, child_id);
5272
5273 return 0;
5274}
5275
5276/**
5277 * css_lookup - lookup css by id
5278 * @ss: cgroup subsys to be looked into.
5279 * @id: the id
5280 *
5281 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5282 * NULL if not. Should be called under rcu_read_lock()
5283 */
5284struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5285{
5286 struct css_id *cssid = NULL;
5287
5288 BUG_ON(!ss->use_id);
5289 cssid = idr_find(&ss->idr, id);
5290
5291 if (unlikely(!cssid))
5292 return NULL;
5293
5294 return rcu_dereference(cssid->css);
5295}
Ben Blum67523c42010-03-10 15:22:11 -08005296EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005297
Stephane Eraniane5d13672011-02-14 11:20:01 +02005298/*
5299 * get corresponding css from file open on cgroupfs directory
5300 */
5301struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5302{
5303 struct cgroup *cgrp;
5304 struct inode *inode;
5305 struct cgroup_subsys_state *css;
5306
Al Viro496ad9a2013-01-23 17:07:38 -05005307 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005308 /* check in cgroup filesystem dir */
5309 if (inode->i_op != &cgroup_dir_inode_operations)
5310 return ERR_PTR(-EBADF);
5311
5312 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5313 return ERR_PTR(-EINVAL);
5314
5315 /* get cgroup */
5316 cgrp = __d_cgrp(f->f_dentry);
5317 css = cgrp->subsys[id];
5318 return css ? css : ERR_PTR(-ENOENT);
5319}
5320
Paul Menagefe693432009-09-23 15:56:20 -07005321#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005322static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005323{
5324 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5325
5326 if (!css)
5327 return ERR_PTR(-ENOMEM);
5328
5329 return css;
5330}
5331
Tejun Heo92fb9742012-11-19 08:13:38 -08005332static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005333{
5334 kfree(cont->subsys[debug_subsys_id]);
5335}
5336
5337static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5338{
5339 return atomic_read(&cont->count);
5340}
5341
5342static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5343{
5344 return cgroup_task_count(cont);
5345}
5346
5347static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5348{
5349 return (u64)(unsigned long)current->cgroups;
5350}
5351
5352static u64 current_css_set_refcount_read(struct cgroup *cont,
5353 struct cftype *cft)
5354{
5355 u64 count;
5356
5357 rcu_read_lock();
5358 count = atomic_read(&current->cgroups->refcount);
5359 rcu_read_unlock();
5360 return count;
5361}
5362
Paul Menage7717f7b2009-09-23 15:56:22 -07005363static int current_css_set_cg_links_read(struct cgroup *cont,
5364 struct cftype *cft,
5365 struct seq_file *seq)
5366{
5367 struct cg_cgroup_link *link;
5368 struct css_set *cg;
5369
5370 read_lock(&css_set_lock);
5371 rcu_read_lock();
5372 cg = rcu_dereference(current->cgroups);
5373 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5374 struct cgroup *c = link->cgrp;
5375 const char *name;
5376
5377 if (c->dentry)
5378 name = c->dentry->d_name.name;
5379 else
5380 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005381 seq_printf(seq, "Root %d group %s\n",
5382 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005383 }
5384 rcu_read_unlock();
5385 read_unlock(&css_set_lock);
5386 return 0;
5387}
5388
5389#define MAX_TASKS_SHOWN_PER_CSS 25
5390static int cgroup_css_links_read(struct cgroup *cont,
5391 struct cftype *cft,
5392 struct seq_file *seq)
5393{
5394 struct cg_cgroup_link *link;
5395
5396 read_lock(&css_set_lock);
5397 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5398 struct css_set *cg = link->cg;
5399 struct task_struct *task;
5400 int count = 0;
5401 seq_printf(seq, "css_set %p\n", cg);
5402 list_for_each_entry(task, &cg->tasks, cg_list) {
5403 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5404 seq_puts(seq, " ...\n");
5405 break;
5406 } else {
5407 seq_printf(seq, " task %d\n",
5408 task_pid_vnr(task));
5409 }
5410 }
5411 }
5412 read_unlock(&css_set_lock);
5413 return 0;
5414}
5415
Paul Menagefe693432009-09-23 15:56:20 -07005416static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5417{
5418 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5419}
5420
5421static struct cftype debug_files[] = {
5422 {
5423 .name = "cgroup_refcount",
5424 .read_u64 = cgroup_refcount_read,
5425 },
5426 {
5427 .name = "taskcount",
5428 .read_u64 = debug_taskcount_read,
5429 },
5430
5431 {
5432 .name = "current_css_set",
5433 .read_u64 = current_css_set_read,
5434 },
5435
5436 {
5437 .name = "current_css_set_refcount",
5438 .read_u64 = current_css_set_refcount_read,
5439 },
5440
5441 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005442 .name = "current_css_set_cg_links",
5443 .read_seq_string = current_css_set_cg_links_read,
5444 },
5445
5446 {
5447 .name = "cgroup_css_links",
5448 .read_seq_string = cgroup_css_links_read,
5449 },
5450
5451 {
Paul Menagefe693432009-09-23 15:56:20 -07005452 .name = "releasable",
5453 .read_u64 = releasable_read,
5454 },
Paul Menagefe693432009-09-23 15:56:20 -07005455
Tejun Heo4baf6e32012-04-01 12:09:55 -07005456 { } /* terminate */
5457};
Paul Menagefe693432009-09-23 15:56:20 -07005458
5459struct cgroup_subsys debug_subsys = {
5460 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005461 .css_alloc = debug_css_alloc,
5462 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005463 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005464 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005465};
5466#endif /* CONFIG_CGROUP_DEBUG */