blob: 2a9926275f806f41e7c15b6eed584acaf8796bc0 [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 Gleixnercdcc136f2009-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
1689 if (((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) &&
1690 root->flags != opts.flags) {
1691 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1692 ret = -EINVAL;
1693 goto drop_new_super;
1694 }
1695
Ben Blumcf5d5942010-03-10 15:22:09 -08001696 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001697 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698 }
1699
Paul Menagec6d57f32009-09-23 15:56:19 -07001700 kfree(opts.release_agent);
1701 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001702 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001704 unlock_drop:
1705 mutex_unlock(&cgroup_root_mutex);
1706 mutex_unlock(&cgroup_mutex);
1707 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001709 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001710 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001711 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001712 out_err:
1713 kfree(opts.release_agent);
1714 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001715 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001716}
1717
1718static void cgroup_kill_sb(struct super_block *sb) {
1719 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001720 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001721 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001722 struct cg_cgroup_link *link;
1723 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724
1725 BUG_ON(!root);
1726
1727 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001728 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001729
1730 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001731 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732
1733 /* Rebind all subsystems back to the default hierarchy */
1734 ret = rebind_subsystems(root, 0);
1735 /* Shouldn't be able to fail ... */
1736 BUG_ON(ret);
1737
Paul Menage817929e2007-10-18 23:39:36 -07001738 /*
1739 * Release all the links from css_sets to this hierarchy's
1740 * root cgroup
1741 */
1742 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001743
1744 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1745 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001746 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001747 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001748 kfree(link);
1749 }
1750 write_unlock(&css_set_lock);
1751
Paul Menage839ec542009-01-29 14:25:22 -08001752 if (!list_empty(&root->root_list)) {
1753 list_del(&root->root_list);
1754 root_count--;
1755 }
Li Zefane5f6a862009-01-07 18:07:41 -08001756
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001757 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758 mutex_unlock(&cgroup_mutex);
1759
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001760 simple_xattrs_free(&cgrp->xattrs);
1761
Paul Menageddbcc7e2007-10-18 23:39:30 -07001762 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001763 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001764}
1765
1766static struct file_system_type cgroup_fs_type = {
1767 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001768 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769 .kill_sb = cgroup_kill_sb,
1770};
1771
Greg KH676db4a2010-08-05 13:53:35 -07001772static struct kobject *cgroup_kobj;
1773
Li Zefana043e3b2008-02-23 15:24:09 -08001774/**
1775 * cgroup_path - generate the path of a cgroup
1776 * @cgrp: the cgroup in question
1777 * @buf: the buffer to write the path into
1778 * @buflen: the length of the buffer
1779 *
Li Zefan65dff752013-03-01 15:01:56 +08001780 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1781 *
1782 * We can't generate cgroup path using dentry->d_name, as accessing
1783 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1784 * inode's i_mutex, while on the other hand cgroup_path() can be called
1785 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001787int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001788{
Li Zefan65dff752013-03-01 15:01:56 +08001789 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001790 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001791
Tejun Heoda1f2962013-04-14 10:32:19 -07001792 if (!cgrp->parent) {
1793 if (strlcpy(buf, "/", buflen) >= buflen)
1794 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795 return 0;
1796 }
1797
Tao Ma316eb662012-11-08 21:36:38 +08001798 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001799 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001800
Li Zefan65dff752013-03-01 15:01:56 +08001801 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001802 do {
Li Zefan65dff752013-03-01 15:01:56 +08001803 const char *name = cgroup_name(cgrp);
1804 int len;
1805
1806 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001807 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001808 goto out;
1809 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001810
Paul Menageddbcc7e2007-10-18 23:39:30 -07001811 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001812 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001814
1815 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001816 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001817 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001818 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001819out:
1820 rcu_read_unlock();
1821 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001822}
Ben Blum67523c42010-03-10 15:22:11 -08001823EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001824
Ben Blum74a11662011-05-26 16:25:20 -07001825/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001826 * Control Group taskset
1827 */
Tejun Heo134d3372011-12-12 18:12:21 -08001828struct task_and_cgroup {
1829 struct task_struct *task;
1830 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001831 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001832};
1833
Tejun Heo2f7ee562011-12-12 18:12:21 -08001834struct cgroup_taskset {
1835 struct task_and_cgroup single;
1836 struct flex_array *tc_array;
1837 int tc_array_len;
1838 int idx;
1839 struct cgroup *cur_cgrp;
1840};
1841
1842/**
1843 * cgroup_taskset_first - reset taskset and return the first task
1844 * @tset: taskset of interest
1845 *
1846 * @tset iteration is initialized and the first task is returned.
1847 */
1848struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1849{
1850 if (tset->tc_array) {
1851 tset->idx = 0;
1852 return cgroup_taskset_next(tset);
1853 } else {
1854 tset->cur_cgrp = tset->single.cgrp;
1855 return tset->single.task;
1856 }
1857}
1858EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1859
1860/**
1861 * cgroup_taskset_next - iterate to the next task in taskset
1862 * @tset: taskset of interest
1863 *
1864 * Return the next task in @tset. Iteration must have been initialized
1865 * with cgroup_taskset_first().
1866 */
1867struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1868{
1869 struct task_and_cgroup *tc;
1870
1871 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1872 return NULL;
1873
1874 tc = flex_array_get(tset->tc_array, tset->idx++);
1875 tset->cur_cgrp = tc->cgrp;
1876 return tc->task;
1877}
1878EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1879
1880/**
1881 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1882 * @tset: taskset of interest
1883 *
1884 * Return the cgroup for the current (last returned) task of @tset. This
1885 * function must be preceded by either cgroup_taskset_first() or
1886 * cgroup_taskset_next().
1887 */
1888struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1889{
1890 return tset->cur_cgrp;
1891}
1892EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1893
1894/**
1895 * cgroup_taskset_size - return the number of tasks in taskset
1896 * @tset: taskset of interest
1897 */
1898int cgroup_taskset_size(struct cgroup_taskset *tset)
1899{
1900 return tset->tc_array ? tset->tc_array_len : 1;
1901}
1902EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1903
1904
Ben Blum74a11662011-05-26 16:25:20 -07001905/*
1906 * cgroup_task_migrate - move a task from one cgroup to another.
1907 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001908 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001909 */
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03001910static void cgroup_task_migrate(struct cgroup *oldcgrp,
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001911 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001912{
1913 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001914
1915 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001916 * We are synchronized through threadgroup_lock() against PF_EXITING
1917 * setting such that we can't race against cgroup_exit() changing the
1918 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001919 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001920 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001921 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001922
Ben Blum74a11662011-05-26 16:25:20 -07001923 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001924 rcu_assign_pointer(tsk->cgroups, newcg);
1925 task_unlock(tsk);
1926
1927 /* Update the css_set linked lists if we're using them */
1928 write_lock(&css_set_lock);
1929 if (!list_empty(&tsk->cg_list))
1930 list_move(&tsk->cg_list, &newcg->tasks);
1931 write_unlock(&css_set_lock);
1932
1933 /*
1934 * We just gained a reference on oldcg by taking it from the task. As
1935 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1936 * it here; it will be freed under RCU.
1937 */
Ben Blum74a11662011-05-26 16:25:20 -07001938 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001939 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001940}
1941
Li Zefana043e3b2008-02-23 15:24:09 -08001942/**
Li Zefan081aa452013-03-13 09:17:09 +08001943 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001944 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001945 * @tsk: the task or the leader of the threadgroup to be attached
1946 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001947 *
Tejun Heo257058a2011-12-12 18:12:21 -08001948 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001949 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001950 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001951static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1952 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001953{
1954 int retval, i, group_size;
1955 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001956 struct cgroupfs_root *root = cgrp->root;
1957 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001958 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001959 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001960 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001961 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001962
1963 /*
1964 * step 0: in order to do expensive, possibly blocking operations for
1965 * every thread, we cannot iterate the thread group list, since it needs
1966 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001967 * group - group_rwsem prevents new threads from appearing, and if
1968 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001969 */
Li Zefan081aa452013-03-13 09:17:09 +08001970 if (threadgroup)
1971 group_size = get_nr_threads(tsk);
1972 else
1973 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001974 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001975 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001976 if (!group)
1977 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001978 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001979 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001980 if (retval)
1981 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001982
Ben Blum74a11662011-05-26 16:25:20 -07001983 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001984 /*
1985 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1986 * already PF_EXITING could be freed from underneath us unless we
1987 * take an rcu_read_lock.
1988 */
1989 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001990 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001991 struct task_and_cgroup ent;
1992
Tejun Heocd3d0952011-12-12 18:12:21 -08001993 /* @tsk either already exited or can't exit until the end */
1994 if (tsk->flags & PF_EXITING)
1995 continue;
1996
Ben Blum74a11662011-05-26 16:25:20 -07001997 /* as per above, nr_threads may decrease, but not increase. */
1998 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001999 ent.task = tsk;
2000 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002001 /* nothing to do if this task is already in the cgroup */
2002 if (ent.cgrp == cgrp)
2003 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002004 /*
2005 * saying GFP_ATOMIC has no effect here because we did prealloc
2006 * earlier, but it's good form to communicate our expectations.
2007 */
Tejun Heo134d3372011-12-12 18:12:21 -08002008 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002009 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002010 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002011
2012 if (!threadgroup)
2013 break;
Ben Blum74a11662011-05-26 16:25:20 -07002014 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002015 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002016 /* remember the number of threads in the array for later. */
2017 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002018 tset.tc_array = group;
2019 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002020
Tejun Heo134d3372011-12-12 18:12:21 -08002021 /* methods shouldn't be called if no task is actually migrating */
2022 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002023 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002024 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002025
Ben Blum74a11662011-05-26 16:25:20 -07002026 /*
2027 * step 1: check that we can legitimately attach to the cgroup.
2028 */
2029 for_each_subsys(root, ss) {
2030 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002031 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002032 if (retval) {
2033 failed_ss = ss;
2034 goto out_cancel_attach;
2035 }
2036 }
Ben Blum74a11662011-05-26 16:25:20 -07002037 }
2038
2039 /*
2040 * step 2: make sure css_sets exist for all threads to be migrated.
2041 * we use find_css_set, which allocates a new one if necessary.
2042 */
Ben Blum74a11662011-05-26 16:25:20 -07002043 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002044 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002045 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2046 if (!tc->cg) {
2047 retval = -ENOMEM;
2048 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002049 }
2050 }
2051
2052 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002053 * step 3: now that we're guaranteed success wrt the css_sets,
2054 * proceed to move all tasks to the new cgroup. There are no
2055 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002056 */
Ben Blum74a11662011-05-26 16:25:20 -07002057 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002058 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002059 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002060 }
2061 /* nothing is sensitive to fork() after this point. */
2062
2063 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002064 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002065 */
2066 for_each_subsys(root, ss) {
2067 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002068 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002069 }
2070
2071 /*
2072 * step 5: success! and cleanup
2073 */
Ben Blum74a11662011-05-26 16:25:20 -07002074 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002075out_put_css_set_refs:
2076 if (retval) {
2077 for (i = 0; i < group_size; i++) {
2078 tc = flex_array_get(group, i);
2079 if (!tc->cg)
2080 break;
2081 put_css_set(tc->cg);
2082 }
Ben Blum74a11662011-05-26 16:25:20 -07002083 }
2084out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002085 if (retval) {
2086 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002087 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002088 break;
Ben Blum74a11662011-05-26 16:25:20 -07002089 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002090 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002091 }
2092 }
Ben Blum74a11662011-05-26 16:25:20 -07002093out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002094 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002095 return retval;
2096}
2097
2098/*
2099 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002100 * function to attach either it or all tasks in its threadgroup. Will lock
2101 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002102 */
2103static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002104{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002105 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002106 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002107 int ret;
2108
Ben Blum74a11662011-05-26 16:25:20 -07002109 if (!cgroup_lock_live_group(cgrp))
2110 return -ENODEV;
2111
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002112retry_find_task:
2113 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002114 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002115 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002116 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002117 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002118 ret= -ESRCH;
2119 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002120 }
Ben Blum74a11662011-05-26 16:25:20 -07002121 /*
2122 * even if we're attaching all tasks in the thread group, we
2123 * only need to check permissions on one of them.
2124 */
David Howellsc69e8d92008-11-14 10:39:19 +11002125 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002126 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2127 !uid_eq(cred->euid, tcred->uid) &&
2128 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002129 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002130 ret = -EACCES;
2131 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002132 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002133 } else
2134 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002135
2136 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002137 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002138
2139 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002140 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002141 * trapped in a cpuset, or RT worker may be born in a cgroup
2142 * with no rt_runtime allocated. Just say no.
2143 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002144 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002145 ret = -EINVAL;
2146 rcu_read_unlock();
2147 goto out_unlock_cgroup;
2148 }
2149
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002150 get_task_struct(tsk);
2151 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002152
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002153 threadgroup_lock(tsk);
2154 if (threadgroup) {
2155 if (!thread_group_leader(tsk)) {
2156 /*
2157 * a race with de_thread from another thread's exec()
2158 * may strip us of our leadership, if this happens,
2159 * there is no choice but to throw this task away and
2160 * try again; this is
2161 * "double-double-toil-and-trouble-check locking".
2162 */
2163 threadgroup_unlock(tsk);
2164 put_task_struct(tsk);
2165 goto retry_find_task;
2166 }
Li Zefan081aa452013-03-13 09:17:09 +08002167 }
2168
2169 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2170
Tejun Heocd3d0952011-12-12 18:12:21 -08002171 threadgroup_unlock(tsk);
2172
Paul Menagebbcb81d2007-10-18 23:39:32 -07002173 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002174out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002175 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002176 return ret;
2177}
2178
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002179/**
2180 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2181 * @from: attach to all cgroups of a given task
2182 * @tsk: the task to be attached
2183 */
2184int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2185{
2186 struct cgroupfs_root *root;
2187 int retval = 0;
2188
Tejun Heo47cfcd02013-04-07 09:29:51 -07002189 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002190 for_each_active_root(root) {
2191 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2192
2193 retval = cgroup_attach_task(from_cg, tsk, false);
2194 if (retval)
2195 break;
2196 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002197 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002198
2199 return retval;
2200}
2201EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2202
Paul Menageaf351022008-07-25 01:47:01 -07002203static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2204{
Ben Blum74a11662011-05-26 16:25:20 -07002205 return attach_task_by_pid(cgrp, pid, false);
2206}
2207
2208static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2209{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002210 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002211}
2212
Paul Menagee788e062008-07-25 01:46:59 -07002213static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2214 const char *buffer)
2215{
2216 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002217 if (strlen(buffer) >= PATH_MAX)
2218 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002219 if (!cgroup_lock_live_group(cgrp))
2220 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002221 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002222 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002223 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002224 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002225 return 0;
2226}
2227
2228static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2229 struct seq_file *seq)
2230{
2231 if (!cgroup_lock_live_group(cgrp))
2232 return -ENODEV;
2233 seq_puts(seq, cgrp->root->release_agent_path);
2234 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002235 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002236 return 0;
2237}
2238
Tejun Heo873fe092013-04-14 20:15:26 -07002239static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2240 struct seq_file *seq)
2241{
2242 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002243 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002244}
2245
Paul Menage84eea842008-07-25 01:47:00 -07002246/* A buffer size big enough for numbers or short strings */
2247#define CGROUP_LOCAL_BUFFER_SIZE 64
2248
Paul Menagee73d2c62008-04-29 01:00:06 -07002249static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002250 struct file *file,
2251 const char __user *userbuf,
2252 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002253{
Paul Menage84eea842008-07-25 01:47:00 -07002254 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002255 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002256 char *end;
2257
2258 if (!nbytes)
2259 return -EINVAL;
2260 if (nbytes >= sizeof(buffer))
2261 return -E2BIG;
2262 if (copy_from_user(buffer, userbuf, nbytes))
2263 return -EFAULT;
2264
2265 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002266 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002267 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002268 if (*end)
2269 return -EINVAL;
2270 retval = cft->write_u64(cgrp, cft, val);
2271 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002272 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002273 if (*end)
2274 return -EINVAL;
2275 retval = cft->write_s64(cgrp, cft, val);
2276 }
Paul Menage355e0c42007-10-18 23:39:33 -07002277 if (!retval)
2278 retval = nbytes;
2279 return retval;
2280}
2281
Paul Menagedb3b1492008-07-25 01:46:58 -07002282static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2283 struct file *file,
2284 const char __user *userbuf,
2285 size_t nbytes, loff_t *unused_ppos)
2286{
Paul Menage84eea842008-07-25 01:47:00 -07002287 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002288 int retval = 0;
2289 size_t max_bytes = cft->max_write_len;
2290 char *buffer = local_buffer;
2291
2292 if (!max_bytes)
2293 max_bytes = sizeof(local_buffer) - 1;
2294 if (nbytes >= max_bytes)
2295 return -E2BIG;
2296 /* Allocate a dynamic buffer if we need one */
2297 if (nbytes >= sizeof(local_buffer)) {
2298 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2299 if (buffer == NULL)
2300 return -ENOMEM;
2301 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002302 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2303 retval = -EFAULT;
2304 goto out;
2305 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002306
2307 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002308 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002309 if (!retval)
2310 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002311out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002312 if (buffer != local_buffer)
2313 kfree(buffer);
2314 return retval;
2315}
2316
Paul Menageddbcc7e2007-10-18 23:39:30 -07002317static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2318 size_t nbytes, loff_t *ppos)
2319{
2320 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002321 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002322
Li Zefan75139b82009-01-07 18:07:33 -08002323 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002324 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002325 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002326 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002327 if (cft->write_u64 || cft->write_s64)
2328 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002329 if (cft->write_string)
2330 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002331 if (cft->trigger) {
2332 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2333 return ret ? ret : nbytes;
2334 }
Paul Menage355e0c42007-10-18 23:39:33 -07002335 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002336}
2337
Paul Menagef4c753b2008-04-29 00:59:56 -07002338static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2339 struct file *file,
2340 char __user *buf, size_t nbytes,
2341 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002342{
Paul Menage84eea842008-07-25 01:47:00 -07002343 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002344 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002345 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2346
2347 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2348}
2349
Paul Menagee73d2c62008-04-29 01:00:06 -07002350static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2351 struct file *file,
2352 char __user *buf, size_t nbytes,
2353 loff_t *ppos)
2354{
Paul Menage84eea842008-07-25 01:47:00 -07002355 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002356 s64 val = cft->read_s64(cgrp, cft);
2357 int len = sprintf(tmp, "%lld\n", (long long) val);
2358
2359 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2360}
2361
Paul Menageddbcc7e2007-10-18 23:39:30 -07002362static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2363 size_t nbytes, loff_t *ppos)
2364{
2365 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002366 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002367
Li Zefan75139b82009-01-07 18:07:33 -08002368 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002369 return -ENODEV;
2370
2371 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002372 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002373 if (cft->read_u64)
2374 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002375 if (cft->read_s64)
2376 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377 return -EINVAL;
2378}
2379
Paul Menage91796562008-04-29 01:00:01 -07002380/*
2381 * seqfile ops/methods for returning structured data. Currently just
2382 * supports string->u64 maps, but can be extended in future.
2383 */
2384
2385struct cgroup_seqfile_state {
2386 struct cftype *cft;
2387 struct cgroup *cgroup;
2388};
2389
2390static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2391{
2392 struct seq_file *sf = cb->state;
2393 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2394}
2395
2396static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2397{
2398 struct cgroup_seqfile_state *state = m->private;
2399 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002400 if (cft->read_map) {
2401 struct cgroup_map_cb cb = {
2402 .fill = cgroup_map_add,
2403 .state = m,
2404 };
2405 return cft->read_map(state->cgroup, cft, &cb);
2406 }
2407 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002408}
2409
Adrian Bunk96930a62008-07-25 19:46:21 -07002410static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002411{
2412 struct seq_file *seq = file->private_data;
2413 kfree(seq->private);
2414 return single_release(inode, file);
2415}
2416
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002417static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002418 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002419 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002420 .llseek = seq_lseek,
2421 .release = cgroup_seqfile_release,
2422};
2423
Paul Menageddbcc7e2007-10-18 23:39:30 -07002424static int cgroup_file_open(struct inode *inode, struct file *file)
2425{
2426 int err;
2427 struct cftype *cft;
2428
2429 err = generic_file_open(inode, file);
2430 if (err)
2431 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002433
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002434 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002435 struct cgroup_seqfile_state *state =
2436 kzalloc(sizeof(*state), GFP_USER);
2437 if (!state)
2438 return -ENOMEM;
2439 state->cft = cft;
2440 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2441 file->f_op = &cgroup_seqfile_operations;
2442 err = single_open(file, cgroup_seqfile_show, state);
2443 if (err < 0)
2444 kfree(state);
2445 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002446 err = cft->open(inode, file);
2447 else
2448 err = 0;
2449
2450 return err;
2451}
2452
2453static int cgroup_file_release(struct inode *inode, struct file *file)
2454{
2455 struct cftype *cft = __d_cft(file->f_dentry);
2456 if (cft->release)
2457 return cft->release(inode, file);
2458 return 0;
2459}
2460
2461/*
2462 * cgroup_rename - Only allow simple rename of directories in place.
2463 */
2464static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2465 struct inode *new_dir, struct dentry *new_dentry)
2466{
Li Zefan65dff752013-03-01 15:01:56 +08002467 int ret;
2468 struct cgroup_name *name, *old_name;
2469 struct cgroup *cgrp;
2470
2471 /*
2472 * It's convinient to use parent dir's i_mutex to protected
2473 * cgrp->name.
2474 */
2475 lockdep_assert_held(&old_dir->i_mutex);
2476
Paul Menageddbcc7e2007-10-18 23:39:30 -07002477 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2478 return -ENOTDIR;
2479 if (new_dentry->d_inode)
2480 return -EEXIST;
2481 if (old_dir != new_dir)
2482 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002483
2484 cgrp = __d_cgrp(old_dentry);
2485
2486 name = cgroup_alloc_name(new_dentry);
2487 if (!name)
2488 return -ENOMEM;
2489
2490 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2491 if (ret) {
2492 kfree(name);
2493 return ret;
2494 }
2495
2496 old_name = cgrp->name;
2497 rcu_assign_pointer(cgrp->name, name);
2498
2499 kfree_rcu(old_name, rcu_head);
2500 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002501}
2502
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002503static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2504{
2505 if (S_ISDIR(dentry->d_inode->i_mode))
2506 return &__d_cgrp(dentry)->xattrs;
2507 else
Li Zefan712317a2013-04-18 23:09:52 -07002508 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002509}
2510
2511static inline int xattr_enabled(struct dentry *dentry)
2512{
2513 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002514 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002515}
2516
2517static bool is_valid_xattr(const char *name)
2518{
2519 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2520 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2521 return true;
2522 return false;
2523}
2524
2525static int cgroup_setxattr(struct dentry *dentry, const char *name,
2526 const void *val, size_t size, int flags)
2527{
2528 if (!xattr_enabled(dentry))
2529 return -EOPNOTSUPP;
2530 if (!is_valid_xattr(name))
2531 return -EINVAL;
2532 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2533}
2534
2535static int cgroup_removexattr(struct dentry *dentry, const char *name)
2536{
2537 if (!xattr_enabled(dentry))
2538 return -EOPNOTSUPP;
2539 if (!is_valid_xattr(name))
2540 return -EINVAL;
2541 return simple_xattr_remove(__d_xattrs(dentry), name);
2542}
2543
2544static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2545 void *buf, size_t size)
2546{
2547 if (!xattr_enabled(dentry))
2548 return -EOPNOTSUPP;
2549 if (!is_valid_xattr(name))
2550 return -EINVAL;
2551 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2552}
2553
2554static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2555{
2556 if (!xattr_enabled(dentry))
2557 return -EOPNOTSUPP;
2558 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2559}
2560
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002561static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002562 .read = cgroup_file_read,
2563 .write = cgroup_file_write,
2564 .llseek = generic_file_llseek,
2565 .open = cgroup_file_open,
2566 .release = cgroup_file_release,
2567};
2568
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002569static const struct inode_operations cgroup_file_inode_operations = {
2570 .setxattr = cgroup_setxattr,
2571 .getxattr = cgroup_getxattr,
2572 .listxattr = cgroup_listxattr,
2573 .removexattr = cgroup_removexattr,
2574};
2575
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002576static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002577 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002578 .mkdir = cgroup_mkdir,
2579 .rmdir = cgroup_rmdir,
2580 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002581 .setxattr = cgroup_setxattr,
2582 .getxattr = cgroup_getxattr,
2583 .listxattr = cgroup_listxattr,
2584 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002585};
2586
Al Viro00cd8dd2012-06-10 17:13:09 -04002587static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002588{
2589 if (dentry->d_name.len > NAME_MAX)
2590 return ERR_PTR(-ENAMETOOLONG);
2591 d_add(dentry, NULL);
2592 return NULL;
2593}
2594
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002595/*
2596 * Check if a file is a control file
2597 */
2598static inline struct cftype *__file_cft(struct file *file)
2599{
Al Viro496ad9a2013-01-23 17:07:38 -05002600 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002601 return ERR_PTR(-EINVAL);
2602 return __d_cft(file->f_dentry);
2603}
2604
Al Viroa5e7ed32011-07-26 01:55:55 -04002605static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002606 struct super_block *sb)
2607{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002608 struct inode *inode;
2609
2610 if (!dentry)
2611 return -ENOENT;
2612 if (dentry->d_inode)
2613 return -EEXIST;
2614
2615 inode = cgroup_new_inode(mode, sb);
2616 if (!inode)
2617 return -ENOMEM;
2618
2619 if (S_ISDIR(mode)) {
2620 inode->i_op = &cgroup_dir_inode_operations;
2621 inode->i_fop = &simple_dir_operations;
2622
2623 /* start off with i_nlink == 2 (for "." entry) */
2624 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002625 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002626
Tejun Heob8a2df62012-11-19 08:13:37 -08002627 /*
2628 * Control reaches here with cgroup_mutex held.
2629 * @inode->i_mutex should nest outside cgroup_mutex but we
2630 * want to populate it immediately without releasing
2631 * cgroup_mutex. As @inode isn't visible to anyone else
2632 * yet, trylock will always succeed without affecting
2633 * lockdep checks.
2634 */
2635 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002636 } else if (S_ISREG(mode)) {
2637 inode->i_size = 0;
2638 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002639 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002640 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002641 d_instantiate(dentry, inode);
2642 dget(dentry); /* Extra count - pin the dentry in core */
2643 return 0;
2644}
2645
Li Zefan099fca32009-04-02 16:57:29 -07002646/**
2647 * cgroup_file_mode - deduce file mode of a control file
2648 * @cft: the control file in question
2649 *
2650 * returns cft->mode if ->mode is not 0
2651 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2652 * returns S_IRUGO if it has only a read handler
2653 * returns S_IWUSR if it has only a write hander
2654 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002655static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002656{
Al Viroa5e7ed32011-07-26 01:55:55 -04002657 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002658
2659 if (cft->mode)
2660 return cft->mode;
2661
2662 if (cft->read || cft->read_u64 || cft->read_s64 ||
2663 cft->read_map || cft->read_seq_string)
2664 mode |= S_IRUGO;
2665
2666 if (cft->write || cft->write_u64 || cft->write_s64 ||
2667 cft->write_string || cft->trigger)
2668 mode |= S_IWUSR;
2669
2670 return mode;
2671}
2672
Tejun Heodb0416b2012-04-01 12:09:55 -07002673static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002674 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002675{
Paul Menagebd89aab2007-10-18 23:40:44 -07002676 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002677 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002678 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002679 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002680 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002681 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002682 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002683
Tejun Heo93438622013-04-14 20:15:25 -07002684 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002685 strcpy(name, subsys->name);
2686 strcat(name, ".");
2687 }
2688 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002689
Paul Menageddbcc7e2007-10-18 23:39:30 -07002690 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002691
2692 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2693 if (!cfe)
2694 return -ENOMEM;
2695
Paul Menageddbcc7e2007-10-18 23:39:30 -07002696 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002697 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002698 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002699 goto out;
2700 }
2701
2702 mode = cgroup_file_mode(cft);
2703 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2704 if (!error) {
2705 cfe->type = (void *)cft;
2706 cfe->dentry = dentry;
2707 dentry->d_fsdata = cfe;
Li Zefan712317a2013-04-18 23:09:52 -07002708 simple_xattrs_init(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002709 list_add_tail(&cfe->node, &parent->files);
2710 cfe = NULL;
2711 }
2712 dput(dentry);
2713out:
2714 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002715 return error;
2716}
2717
Tejun Heo79578622012-04-01 12:09:56 -07002718static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002719 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002720{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002721 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002722 int err, ret = 0;
2723
2724 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002725 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002726 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2727 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002728 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2729 continue;
2730 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2731 continue;
2732
Li Zefan2739d3c2013-01-21 18:18:33 +08002733 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002734 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002735 if (err)
2736 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2737 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002738 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002739 } else {
2740 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002741 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002742 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002743 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002744}
2745
Tejun Heo8e3f6542012-04-01 12:09:55 -07002746static DEFINE_MUTEX(cgroup_cft_mutex);
2747
2748static void cgroup_cfts_prepare(void)
2749 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2750{
2751 /*
2752 * Thanks to the entanglement with vfs inode locking, we can't walk
2753 * the existing cgroups under cgroup_mutex and create files.
2754 * Instead, we increment reference on all cgroups and build list of
2755 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2756 * exclusive access to the field.
2757 */
2758 mutex_lock(&cgroup_cft_mutex);
2759 mutex_lock(&cgroup_mutex);
2760}
2761
2762static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002763 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002764 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2765{
2766 LIST_HEAD(pending);
2767 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002768
2769 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2770 if (cfts && ss->root != &rootnode) {
2771 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2772 dget(cgrp->dentry);
2773 list_add_tail(&cgrp->cft_q_node, &pending);
2774 }
2775 }
2776
2777 mutex_unlock(&cgroup_mutex);
2778
2779 /*
2780 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2781 * files for all cgroups which were created before.
2782 */
2783 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2784 struct inode *inode = cgrp->dentry->d_inode;
2785
2786 mutex_lock(&inode->i_mutex);
2787 mutex_lock(&cgroup_mutex);
2788 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002789 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002790 mutex_unlock(&cgroup_mutex);
2791 mutex_unlock(&inode->i_mutex);
2792
2793 list_del_init(&cgrp->cft_q_node);
2794 dput(cgrp->dentry);
2795 }
2796
2797 mutex_unlock(&cgroup_cft_mutex);
2798}
2799
2800/**
2801 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2802 * @ss: target cgroup subsystem
2803 * @cfts: zero-length name terminated array of cftypes
2804 *
2805 * Register @cfts to @ss. Files described by @cfts are created for all
2806 * existing cgroups to which @ss is attached and all future cgroups will
2807 * have them too. This function can be called anytime whether @ss is
2808 * attached or not.
2809 *
2810 * Returns 0 on successful registration, -errno on failure. Note that this
2811 * function currently returns 0 as long as @cfts registration is successful
2812 * even if some file creation attempts on existing cgroups fail.
2813 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002814int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002815{
2816 struct cftype_set *set;
2817
2818 set = kzalloc(sizeof(*set), GFP_KERNEL);
2819 if (!set)
2820 return -ENOMEM;
2821
2822 cgroup_cfts_prepare();
2823 set->cfts = cfts;
2824 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002825 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002826
2827 return 0;
2828}
2829EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2830
Li Zefana043e3b2008-02-23 15:24:09 -08002831/**
Tejun Heo79578622012-04-01 12:09:56 -07002832 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2833 * @ss: target cgroup subsystem
2834 * @cfts: zero-length name terminated array of cftypes
2835 *
2836 * Unregister @cfts from @ss. Files described by @cfts are removed from
2837 * all existing cgroups to which @ss is attached and all future cgroups
2838 * won't have them either. This function can be called anytime whether @ss
2839 * is attached or not.
2840 *
2841 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2842 * registered with @ss.
2843 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002844int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002845{
2846 struct cftype_set *set;
2847
2848 cgroup_cfts_prepare();
2849
2850 list_for_each_entry(set, &ss->cftsets, node) {
2851 if (set->cfts == cfts) {
2852 list_del_init(&set->node);
2853 cgroup_cfts_commit(ss, cfts, false);
2854 return 0;
2855 }
2856 }
2857
2858 cgroup_cfts_commit(ss, NULL, false);
2859 return -ENOENT;
2860}
2861
2862/**
Li Zefana043e3b2008-02-23 15:24:09 -08002863 * cgroup_task_count - count the number of tasks in a cgroup.
2864 * @cgrp: the cgroup in question
2865 *
2866 * Return the number of tasks in the cgroup.
2867 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002868int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002869{
2870 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002871 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002872
Paul Menage817929e2007-10-18 23:39:36 -07002873 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002874 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002875 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002876 }
2877 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002878 return count;
2879}
2880
2881/*
Paul Menage817929e2007-10-18 23:39:36 -07002882 * Advance a list_head iterator. The iterator should be positioned at
2883 * the start of a css_set
2884 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002885static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002886 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002887{
2888 struct list_head *l = it->cg_link;
2889 struct cg_cgroup_link *link;
2890 struct css_set *cg;
2891
2892 /* Advance to the next non-empty css_set */
2893 do {
2894 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002895 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002896 it->cg_link = NULL;
2897 return;
2898 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002899 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002900 cg = link->cg;
2901 } while (list_empty(&cg->tasks));
2902 it->cg_link = l;
2903 it->task = cg->tasks.next;
2904}
2905
Cliff Wickman31a7df02008-02-07 00:14:42 -08002906/*
2907 * To reduce the fork() overhead for systems that are not actually
2908 * using their cgroups capability, we don't maintain the lists running
2909 * through each css_set to its tasks until we see the list actually
2910 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002911 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002912static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002913{
2914 struct task_struct *p, *g;
2915 write_lock(&css_set_lock);
2916 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002917 /*
2918 * We need tasklist_lock because RCU is not safe against
2919 * while_each_thread(). Besides, a forking task that has passed
2920 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2921 * is not guaranteed to have its child immediately visible in the
2922 * tasklist if we walk through it with RCU.
2923 */
2924 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002925 do_each_thread(g, p) {
2926 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002927 /*
2928 * We should check if the process is exiting, otherwise
2929 * it will race with cgroup_exit() in that the list
2930 * entry won't be deleted though the process has exited.
2931 */
2932 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002933 list_add(&p->cg_list, &p->cgroups->tasks);
2934 task_unlock(p);
2935 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002936 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002937 write_unlock(&css_set_lock);
2938}
2939
Tejun Heo574bd9f2012-11-09 09:12:29 -08002940/**
2941 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2942 * @pos: the current position (%NULL to initiate traversal)
2943 * @cgroup: cgroup whose descendants to walk
2944 *
2945 * To be used by cgroup_for_each_descendant_pre(). Find the next
2946 * descendant to visit for pre-order traversal of @cgroup's descendants.
2947 */
2948struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2949 struct cgroup *cgroup)
2950{
2951 struct cgroup *next;
2952
2953 WARN_ON_ONCE(!rcu_read_lock_held());
2954
2955 /* if first iteration, pretend we just visited @cgroup */
2956 if (!pos) {
2957 if (list_empty(&cgroup->children))
2958 return NULL;
2959 pos = cgroup;
2960 }
2961
2962 /* visit the first child if exists */
2963 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2964 if (next)
2965 return next;
2966
2967 /* no child, visit my or the closest ancestor's next sibling */
2968 do {
2969 next = list_entry_rcu(pos->sibling.next, struct cgroup,
2970 sibling);
2971 if (&next->sibling != &pos->parent->children)
2972 return next;
2973
2974 pos = pos->parent;
2975 } while (pos != cgroup);
2976
2977 return NULL;
2978}
2979EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
2980
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002981/**
2982 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
2983 * @pos: cgroup of interest
2984 *
2985 * Return the rightmost descendant of @pos. If there's no descendant,
2986 * @pos is returned. This can be used during pre-order traversal to skip
2987 * subtree of @pos.
2988 */
2989struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
2990{
2991 struct cgroup *last, *tmp;
2992
2993 WARN_ON_ONCE(!rcu_read_lock_held());
2994
2995 do {
2996 last = pos;
2997 /* ->prev isn't RCU safe, walk ->next till the end */
2998 pos = NULL;
2999 list_for_each_entry_rcu(tmp, &last->children, sibling)
3000 pos = tmp;
3001 } while (pos);
3002
3003 return last;
3004}
3005EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3006
Tejun Heo574bd9f2012-11-09 09:12:29 -08003007static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3008{
3009 struct cgroup *last;
3010
3011 do {
3012 last = pos;
3013 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3014 sibling);
3015 } while (pos);
3016
3017 return last;
3018}
3019
3020/**
3021 * cgroup_next_descendant_post - find the next descendant for post-order walk
3022 * @pos: the current position (%NULL to initiate traversal)
3023 * @cgroup: cgroup whose descendants to walk
3024 *
3025 * To be used by cgroup_for_each_descendant_post(). Find the next
3026 * descendant to visit for post-order traversal of @cgroup's descendants.
3027 */
3028struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3029 struct cgroup *cgroup)
3030{
3031 struct cgroup *next;
3032
3033 WARN_ON_ONCE(!rcu_read_lock_held());
3034
3035 /* if first iteration, visit the leftmost descendant */
3036 if (!pos) {
3037 next = cgroup_leftmost_descendant(cgroup);
3038 return next != cgroup ? next : NULL;
3039 }
3040
3041 /* if there's an unvisited sibling, visit its leftmost descendant */
3042 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3043 if (&next->sibling != &pos->parent->children)
3044 return cgroup_leftmost_descendant(next);
3045
3046 /* no sibling left, visit parent */
3047 next = pos->parent;
3048 return next != cgroup ? next : NULL;
3049}
3050EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3051
Paul Menagebd89aab2007-10-18 23:40:44 -07003052void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003053 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003054{
3055 /*
3056 * The first time anyone tries to iterate across a cgroup,
3057 * we need to enable the list linking each css_set to its
3058 * tasks, and fix up all existing tasks.
3059 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003060 if (!use_task_css_set_links)
3061 cgroup_enable_task_cg_lists();
3062
Paul Menage817929e2007-10-18 23:39:36 -07003063 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003064 it->cg_link = &cgrp->css_sets;
3065 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003066}
3067
Paul Menagebd89aab2007-10-18 23:40:44 -07003068struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003069 struct cgroup_iter *it)
3070{
3071 struct task_struct *res;
3072 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003073 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003074
3075 /* If the iterator cg is NULL, we have no tasks */
3076 if (!it->cg_link)
3077 return NULL;
3078 res = list_entry(l, struct task_struct, cg_list);
3079 /* Advance iterator to find next entry */
3080 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003081 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3082 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003083 /* We reached the end of this task list - move on to
3084 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003085 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003086 } else {
3087 it->task = l;
3088 }
3089 return res;
3090}
3091
Paul Menagebd89aab2007-10-18 23:40:44 -07003092void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003093 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003094{
3095 read_unlock(&css_set_lock);
3096}
3097
Cliff Wickman31a7df02008-02-07 00:14:42 -08003098static inline int started_after_time(struct task_struct *t1,
3099 struct timespec *time,
3100 struct task_struct *t2)
3101{
3102 int start_diff = timespec_compare(&t1->start_time, time);
3103 if (start_diff > 0) {
3104 return 1;
3105 } else if (start_diff < 0) {
3106 return 0;
3107 } else {
3108 /*
3109 * Arbitrarily, if two processes started at the same
3110 * time, we'll say that the lower pointer value
3111 * started first. Note that t2 may have exited by now
3112 * so this may not be a valid pointer any longer, but
3113 * that's fine - it still serves to distinguish
3114 * between two tasks started (effectively) simultaneously.
3115 */
3116 return t1 > t2;
3117 }
3118}
3119
3120/*
3121 * This function is a callback from heap_insert() and is used to order
3122 * the heap.
3123 * In this case we order the heap in descending task start time.
3124 */
3125static inline int started_after(void *p1, void *p2)
3126{
3127 struct task_struct *t1 = p1;
3128 struct task_struct *t2 = p2;
3129 return started_after_time(t1, &t2->start_time, t2);
3130}
3131
3132/**
3133 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3134 * @scan: struct cgroup_scanner containing arguments for the scan
3135 *
3136 * Arguments include pointers to callback functions test_task() and
3137 * process_task().
3138 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3139 * and if it returns true, call process_task() for it also.
3140 * The test_task pointer may be NULL, meaning always true (select all tasks).
3141 * Effectively duplicates cgroup_iter_{start,next,end}()
3142 * but does not lock css_set_lock for the call to process_task().
3143 * The struct cgroup_scanner may be embedded in any structure of the caller's
3144 * creation.
3145 * It is guaranteed that process_task() will act on every task that
3146 * is a member of the cgroup for the duration of this call. This
3147 * function may or may not call process_task() for tasks that exit
3148 * or move to a different cgroup during the call, or are forked or
3149 * move into the cgroup during the call.
3150 *
3151 * Note that test_task() may be called with locks held, and may in some
3152 * situations be called multiple times for the same task, so it should
3153 * be cheap.
3154 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3155 * pre-allocated and will be used for heap operations (and its "gt" member will
3156 * be overwritten), else a temporary heap will be used (allocation of which
3157 * may cause this function to fail).
3158 */
3159int cgroup_scan_tasks(struct cgroup_scanner *scan)
3160{
3161 int retval, i;
3162 struct cgroup_iter it;
3163 struct task_struct *p, *dropped;
3164 /* Never dereference latest_task, since it's not refcounted */
3165 struct task_struct *latest_task = NULL;
3166 struct ptr_heap tmp_heap;
3167 struct ptr_heap *heap;
3168 struct timespec latest_time = { 0, 0 };
3169
3170 if (scan->heap) {
3171 /* The caller supplied our heap and pre-allocated its memory */
3172 heap = scan->heap;
3173 heap->gt = &started_after;
3174 } else {
3175 /* We need to allocate our own heap memory */
3176 heap = &tmp_heap;
3177 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3178 if (retval)
3179 /* cannot allocate the heap */
3180 return retval;
3181 }
3182
3183 again:
3184 /*
3185 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3186 * to determine which are of interest, and using the scanner's
3187 * "process_task" callback to process any of them that need an update.
3188 * Since we don't want to hold any locks during the task updates,
3189 * gather tasks to be processed in a heap structure.
3190 * The heap is sorted by descending task start time.
3191 * If the statically-sized heap fills up, we overflow tasks that
3192 * started later, and in future iterations only consider tasks that
3193 * started after the latest task in the previous pass. This
3194 * guarantees forward progress and that we don't miss any tasks.
3195 */
3196 heap->size = 0;
3197 cgroup_iter_start(scan->cg, &it);
3198 while ((p = cgroup_iter_next(scan->cg, &it))) {
3199 /*
3200 * Only affect tasks that qualify per the caller's callback,
3201 * if he provided one
3202 */
3203 if (scan->test_task && !scan->test_task(p, scan))
3204 continue;
3205 /*
3206 * Only process tasks that started after the last task
3207 * we processed
3208 */
3209 if (!started_after_time(p, &latest_time, latest_task))
3210 continue;
3211 dropped = heap_insert(heap, p);
3212 if (dropped == NULL) {
3213 /*
3214 * The new task was inserted; the heap wasn't
3215 * previously full
3216 */
3217 get_task_struct(p);
3218 } else if (dropped != p) {
3219 /*
3220 * The new task was inserted, and pushed out a
3221 * different task
3222 */
3223 get_task_struct(p);
3224 put_task_struct(dropped);
3225 }
3226 /*
3227 * Else the new task was newer than anything already in
3228 * the heap and wasn't inserted
3229 */
3230 }
3231 cgroup_iter_end(scan->cg, &it);
3232
3233 if (heap->size) {
3234 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003235 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003236 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003237 latest_time = q->start_time;
3238 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003239 }
3240 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003241 scan->process_task(q, scan);
3242 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003243 }
3244 /*
3245 * If we had to process any tasks at all, scan again
3246 * in case some of them were in the middle of forking
3247 * children that didn't get processed.
3248 * Not the most efficient way to do it, but it avoids
3249 * having to take callback_mutex in the fork path
3250 */
3251 goto again;
3252 }
3253 if (heap == &tmp_heap)
3254 heap_free(&tmp_heap);
3255 return 0;
3256}
3257
Tejun Heo8cc99342013-04-07 09:29:50 -07003258static void cgroup_transfer_one_task(struct task_struct *task,
3259 struct cgroup_scanner *scan)
3260{
3261 struct cgroup *new_cgroup = scan->data;
3262
Tejun Heo47cfcd02013-04-07 09:29:51 -07003263 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003264 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003265 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003266}
3267
3268/**
3269 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3270 * @to: cgroup to which the tasks will be moved
3271 * @from: cgroup in which the tasks currently reside
3272 */
3273int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3274{
3275 struct cgroup_scanner scan;
3276
3277 scan.cg = from;
3278 scan.test_task = NULL; /* select all tasks in cgroup */
3279 scan.process_task = cgroup_transfer_one_task;
3280 scan.heap = NULL;
3281 scan.data = to;
3282
3283 return cgroup_scan_tasks(&scan);
3284}
3285
Paul Menage817929e2007-10-18 23:39:36 -07003286/*
Ben Blum102a7752009-09-23 15:56:26 -07003287 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003288 *
3289 * Reading this file can return large amounts of data if a cgroup has
3290 * *lots* of attached tasks. So it may need several calls to read(),
3291 * but we cannot guarantee that the information we produce is correct
3292 * unless we produce it entirely atomically.
3293 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003294 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003295
Li Zefan24528252012-01-20 11:58:43 +08003296/* which pidlist file are we talking about? */
3297enum cgroup_filetype {
3298 CGROUP_FILE_PROCS,
3299 CGROUP_FILE_TASKS,
3300};
3301
3302/*
3303 * A pidlist is a list of pids that virtually represents the contents of one
3304 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3305 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3306 * to the cgroup.
3307 */
3308struct cgroup_pidlist {
3309 /*
3310 * used to find which pidlist is wanted. doesn't change as long as
3311 * this particular list stays in the list.
3312 */
3313 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3314 /* array of xids */
3315 pid_t *list;
3316 /* how many elements the above list has */
3317 int length;
3318 /* how many files are using the current array */
3319 int use_count;
3320 /* each of these stored in a list by its cgroup */
3321 struct list_head links;
3322 /* pointer to the cgroup we belong to, for list removal purposes */
3323 struct cgroup *owner;
3324 /* protects the other fields */
3325 struct rw_semaphore mutex;
3326};
3327
Paul Menagebbcb81d2007-10-18 23:39:32 -07003328/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003329 * The following two functions "fix" the issue where there are more pids
3330 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3331 * TODO: replace with a kernel-wide solution to this problem
3332 */
3333#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3334static void *pidlist_allocate(int count)
3335{
3336 if (PIDLIST_TOO_LARGE(count))
3337 return vmalloc(count * sizeof(pid_t));
3338 else
3339 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3340}
3341static void pidlist_free(void *p)
3342{
3343 if (is_vmalloc_addr(p))
3344 vfree(p);
3345 else
3346 kfree(p);
3347}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003348
3349/*
Ben Blum102a7752009-09-23 15:56:26 -07003350 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003351 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003352 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003353static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003354{
Ben Blum102a7752009-09-23 15:56:26 -07003355 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003356
3357 /*
3358 * we presume the 0th element is unique, so i starts at 1. trivial
3359 * edge cases first; no work needs to be done for either
3360 */
3361 if (length == 0 || length == 1)
3362 return length;
3363 /* src and dest walk down the list; dest counts unique elements */
3364 for (src = 1; src < length; src++) {
3365 /* find next unique element */
3366 while (list[src] == list[src-1]) {
3367 src++;
3368 if (src == length)
3369 goto after;
3370 }
3371 /* dest always points to where the next unique element goes */
3372 list[dest] = list[src];
3373 dest++;
3374 }
3375after:
Ben Blum102a7752009-09-23 15:56:26 -07003376 return dest;
3377}
3378
3379static int cmppid(const void *a, const void *b)
3380{
3381 return *(pid_t *)a - *(pid_t *)b;
3382}
3383
3384/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003385 * find the appropriate pidlist for our purpose (given procs vs tasks)
3386 * returns with the lock on that pidlist already held, and takes care
3387 * of the use count, or returns NULL with no locks held if we're out of
3388 * memory.
3389 */
3390static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3391 enum cgroup_filetype type)
3392{
3393 struct cgroup_pidlist *l;
3394 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003395 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003396
Ben Blum72a8cb32009-09-23 15:56:27 -07003397 /*
3398 * We can't drop the pidlist_mutex before taking the l->mutex in case
3399 * the last ref-holder is trying to remove l from the list at the same
3400 * time. Holding the pidlist_mutex precludes somebody taking whichever
3401 * list we find out from under us - compare release_pid_array().
3402 */
3403 mutex_lock(&cgrp->pidlist_mutex);
3404 list_for_each_entry(l, &cgrp->pidlists, links) {
3405 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003406 /* make sure l doesn't vanish out from under us */
3407 down_write(&l->mutex);
3408 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003409 return l;
3410 }
3411 }
3412 /* entry not found; create a new one */
3413 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3414 if (!l) {
3415 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003416 return l;
3417 }
3418 init_rwsem(&l->mutex);
3419 down_write(&l->mutex);
3420 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003421 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003422 l->use_count = 0; /* don't increment here */
3423 l->list = NULL;
3424 l->owner = cgrp;
3425 list_add(&l->links, &cgrp->pidlists);
3426 mutex_unlock(&cgrp->pidlist_mutex);
3427 return l;
3428}
3429
3430/*
Ben Blum102a7752009-09-23 15:56:26 -07003431 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3432 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003433static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3434 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003435{
3436 pid_t *array;
3437 int length;
3438 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003439 struct cgroup_iter it;
3440 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003441 struct cgroup_pidlist *l;
3442
3443 /*
3444 * If cgroup gets more users after we read count, we won't have
3445 * enough space - tough. This race is indistinguishable to the
3446 * caller from the case that the additional cgroup users didn't
3447 * show up until sometime later on.
3448 */
3449 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003450 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003451 if (!array)
3452 return -ENOMEM;
3453 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003454 cgroup_iter_start(cgrp, &it);
3455 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003456 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003457 break;
Ben Blum102a7752009-09-23 15:56:26 -07003458 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003459 if (type == CGROUP_FILE_PROCS)
3460 pid = task_tgid_vnr(tsk);
3461 else
3462 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003463 if (pid > 0) /* make sure to only use valid results */
3464 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003465 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003466 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003467 length = n;
3468 /* now sort & (if procs) strip out duplicates */
3469 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003470 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003471 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003472 l = cgroup_pidlist_find(cgrp, type);
3473 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003474 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003475 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003476 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003477 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003478 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003479 l->list = array;
3480 l->length = length;
3481 l->use_count++;
3482 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003483 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003484 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003485}
3486
Balbir Singh846c7bb2007-10-18 23:39:44 -07003487/**
Li Zefana043e3b2008-02-23 15:24:09 -08003488 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003489 * @stats: cgroupstats to fill information into
3490 * @dentry: A dentry entry belonging to the cgroup for which stats have
3491 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003492 *
3493 * Build and fill cgroupstats so that taskstats can export it to user
3494 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003495 */
3496int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3497{
3498 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003499 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003500 struct cgroup_iter it;
3501 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003502
Balbir Singh846c7bb2007-10-18 23:39:44 -07003503 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003504 * Validate dentry by checking the superblock operations,
3505 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003506 */
Li Zefan33d283b2008-11-19 15:36:48 -08003507 if (dentry->d_sb->s_op != &cgroup_ops ||
3508 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003509 goto err;
3510
3511 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003512 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003513
Paul Menagebd89aab2007-10-18 23:40:44 -07003514 cgroup_iter_start(cgrp, &it);
3515 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003516 switch (tsk->state) {
3517 case TASK_RUNNING:
3518 stats->nr_running++;
3519 break;
3520 case TASK_INTERRUPTIBLE:
3521 stats->nr_sleeping++;
3522 break;
3523 case TASK_UNINTERRUPTIBLE:
3524 stats->nr_uninterruptible++;
3525 break;
3526 case TASK_STOPPED:
3527 stats->nr_stopped++;
3528 break;
3529 default:
3530 if (delayacct_is_task_waiting_on_io(tsk))
3531 stats->nr_io_wait++;
3532 break;
3533 }
3534 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003535 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003536
Balbir Singh846c7bb2007-10-18 23:39:44 -07003537err:
3538 return ret;
3539}
3540
Paul Menage8f3ff202009-09-23 15:56:25 -07003541
Paul Menagecc31edc2008-10-18 20:28:04 -07003542/*
Ben Blum102a7752009-09-23 15:56:26 -07003543 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003544 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003545 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003546 */
3547
Ben Blum102a7752009-09-23 15:56:26 -07003548static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003549{
3550 /*
3551 * Initially we receive a position value that corresponds to
3552 * one more than the last pid shown (or 0 on the first call or
3553 * after a seek to the start). Use a binary-search to find the
3554 * next pid to display, if any
3555 */
Ben Blum102a7752009-09-23 15:56:26 -07003556 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003557 int index = 0, pid = *pos;
3558 int *iter;
3559
Ben Blum102a7752009-09-23 15:56:26 -07003560 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003561 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003562 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003563
Paul Menagecc31edc2008-10-18 20:28:04 -07003564 while (index < end) {
3565 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003566 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003567 index = mid;
3568 break;
Ben Blum102a7752009-09-23 15:56:26 -07003569 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003570 index = mid + 1;
3571 else
3572 end = mid;
3573 }
3574 }
3575 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003576 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003577 return NULL;
3578 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003579 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003580 *pos = *iter;
3581 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003582}
3583
Ben Blum102a7752009-09-23 15:56:26 -07003584static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003585{
Ben Blum102a7752009-09-23 15:56:26 -07003586 struct cgroup_pidlist *l = s->private;
3587 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003588}
3589
Ben Blum102a7752009-09-23 15:56:26 -07003590static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003591{
Ben Blum102a7752009-09-23 15:56:26 -07003592 struct cgroup_pidlist *l = s->private;
3593 pid_t *p = v;
3594 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003595 /*
3596 * Advance to the next pid in the array. If this goes off the
3597 * end, we're done
3598 */
3599 p++;
3600 if (p >= end) {
3601 return NULL;
3602 } else {
3603 *pos = *p;
3604 return p;
3605 }
3606}
3607
Ben Blum102a7752009-09-23 15:56:26 -07003608static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003609{
3610 return seq_printf(s, "%d\n", *(int *)v);
3611}
3612
Ben Blum102a7752009-09-23 15:56:26 -07003613/*
3614 * seq_operations functions for iterating on pidlists through seq_file -
3615 * independent of whether it's tasks or procs
3616 */
3617static const struct seq_operations cgroup_pidlist_seq_operations = {
3618 .start = cgroup_pidlist_start,
3619 .stop = cgroup_pidlist_stop,
3620 .next = cgroup_pidlist_next,
3621 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003622};
3623
Ben Blum102a7752009-09-23 15:56:26 -07003624static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003625{
Ben Blum72a8cb32009-09-23 15:56:27 -07003626 /*
3627 * the case where we're the last user of this particular pidlist will
3628 * have us remove it from the cgroup's list, which entails taking the
3629 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3630 * pidlist_mutex, we have to take pidlist_mutex first.
3631 */
3632 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003633 down_write(&l->mutex);
3634 BUG_ON(!l->use_count);
3635 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003636 /* we're the last user if refcount is 0; remove and free */
3637 list_del(&l->links);
3638 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003639 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003640 put_pid_ns(l->key.ns);
3641 up_write(&l->mutex);
3642 kfree(l);
3643 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003644 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003645 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003646 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003647}
3648
Ben Blum102a7752009-09-23 15:56:26 -07003649static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003650{
Ben Blum102a7752009-09-23 15:56:26 -07003651 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003652 if (!(file->f_mode & FMODE_READ))
3653 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003654 /*
3655 * the seq_file will only be initialized if the file was opened for
3656 * reading; hence we check if it's not null only in that case.
3657 */
3658 l = ((struct seq_file *)file->private_data)->private;
3659 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003660 return seq_release(inode, file);
3661}
3662
Ben Blum102a7752009-09-23 15:56:26 -07003663static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003664 .read = seq_read,
3665 .llseek = seq_lseek,
3666 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003667 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003668};
3669
3670/*
Ben Blum102a7752009-09-23 15:56:26 -07003671 * The following functions handle opens on a file that displays a pidlist
3672 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3673 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003674 */
Ben Blum102a7752009-09-23 15:56:26 -07003675/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003676static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003677{
3678 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003679 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003680 int retval;
3681
3682 /* Nothing to do for write-only files */
3683 if (!(file->f_mode & FMODE_READ))
3684 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003685
Ben Blum102a7752009-09-23 15:56:26 -07003686 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003687 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003688 if (retval)
3689 return retval;
3690 /* configure file information */
3691 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003692
Ben Blum102a7752009-09-23 15:56:26 -07003693 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003694 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003695 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003696 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003697 }
Ben Blum102a7752009-09-23 15:56:26 -07003698 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003699 return 0;
3700}
Ben Blum102a7752009-09-23 15:56:26 -07003701static int cgroup_tasks_open(struct inode *unused, struct file *file)
3702{
Ben Blum72a8cb32009-09-23 15:56:27 -07003703 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003704}
3705static int cgroup_procs_open(struct inode *unused, struct file *file)
3706{
Ben Blum72a8cb32009-09-23 15:56:27 -07003707 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003708}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003709
Paul Menagebd89aab2007-10-18 23:40:44 -07003710static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003711 struct cftype *cft)
3712{
Paul Menagebd89aab2007-10-18 23:40:44 -07003713 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003714}
3715
Paul Menage6379c102008-07-25 01:47:01 -07003716static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3717 struct cftype *cft,
3718 u64 val)
3719{
3720 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3721 if (val)
3722 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3723 else
3724 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3725 return 0;
3726}
3727
Paul Menagebbcb81d2007-10-18 23:39:32 -07003728/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003729 * Unregister event and free resources.
3730 *
3731 * Gets called from workqueue.
3732 */
3733static void cgroup_event_remove(struct work_struct *work)
3734{
3735 struct cgroup_event *event = container_of(work, struct cgroup_event,
3736 remove);
3737 struct cgroup *cgrp = event->cgrp;
3738
Li Zefan810cbee2013-02-18 18:56:14 +08003739 remove_wait_queue(event->wqh, &event->wait);
3740
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003741 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3742
Li Zefan810cbee2013-02-18 18:56:14 +08003743 /* Notify userspace the event is going away. */
3744 eventfd_signal(event->eventfd, 1);
3745
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003746 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003747 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003748 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003749}
3750
3751/*
3752 * Gets called on POLLHUP on eventfd when user closes it.
3753 *
3754 * Called with wqh->lock held and interrupts disabled.
3755 */
3756static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3757 int sync, void *key)
3758{
3759 struct cgroup_event *event = container_of(wait,
3760 struct cgroup_event, wait);
3761 struct cgroup *cgrp = event->cgrp;
3762 unsigned long flags = (unsigned long)key;
3763
3764 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003765 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003766 * If the event has been detached at cgroup removal, we
3767 * can simply return knowing the other side will cleanup
3768 * for us.
3769 *
3770 * We can't race against event freeing since the other
3771 * side will require wqh->lock via remove_wait_queue(),
3772 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003773 */
Li Zefan810cbee2013-02-18 18:56:14 +08003774 spin_lock(&cgrp->event_list_lock);
3775 if (!list_empty(&event->list)) {
3776 list_del_init(&event->list);
3777 /*
3778 * We are in atomic context, but cgroup_event_remove()
3779 * may sleep, so we have to call it in workqueue.
3780 */
3781 schedule_work(&event->remove);
3782 }
3783 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003784 }
3785
3786 return 0;
3787}
3788
3789static void cgroup_event_ptable_queue_proc(struct file *file,
3790 wait_queue_head_t *wqh, poll_table *pt)
3791{
3792 struct cgroup_event *event = container_of(pt,
3793 struct cgroup_event, pt);
3794
3795 event->wqh = wqh;
3796 add_wait_queue(wqh, &event->wait);
3797}
3798
3799/*
3800 * Parse input and register new cgroup event handler.
3801 *
3802 * Input must be in format '<event_fd> <control_fd> <args>'.
3803 * Interpretation of args is defined by control file implementation.
3804 */
3805static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3806 const char *buffer)
3807{
3808 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003809 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003810 unsigned int efd, cfd;
3811 struct file *efile = NULL;
3812 struct file *cfile = NULL;
3813 char *endp;
3814 int ret;
3815
3816 efd = simple_strtoul(buffer, &endp, 10);
3817 if (*endp != ' ')
3818 return -EINVAL;
3819 buffer = endp + 1;
3820
3821 cfd = simple_strtoul(buffer, &endp, 10);
3822 if ((*endp != ' ') && (*endp != '\0'))
3823 return -EINVAL;
3824 buffer = endp + 1;
3825
3826 event = kzalloc(sizeof(*event), GFP_KERNEL);
3827 if (!event)
3828 return -ENOMEM;
3829 event->cgrp = cgrp;
3830 INIT_LIST_HEAD(&event->list);
3831 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3832 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3833 INIT_WORK(&event->remove, cgroup_event_remove);
3834
3835 efile = eventfd_fget(efd);
3836 if (IS_ERR(efile)) {
3837 ret = PTR_ERR(efile);
3838 goto fail;
3839 }
3840
3841 event->eventfd = eventfd_ctx_fileget(efile);
3842 if (IS_ERR(event->eventfd)) {
3843 ret = PTR_ERR(event->eventfd);
3844 goto fail;
3845 }
3846
3847 cfile = fget(cfd);
3848 if (!cfile) {
3849 ret = -EBADF;
3850 goto fail;
3851 }
3852
3853 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003854 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003855 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003856 if (ret < 0)
3857 goto fail;
3858
3859 event->cft = __file_cft(cfile);
3860 if (IS_ERR(event->cft)) {
3861 ret = PTR_ERR(event->cft);
3862 goto fail;
3863 }
3864
Li Zefanf1690072013-02-18 14:13:35 +08003865 /*
3866 * The file to be monitored must be in the same cgroup as
3867 * cgroup.event_control is.
3868 */
3869 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3870 if (cgrp_cfile != cgrp) {
3871 ret = -EINVAL;
3872 goto fail;
3873 }
3874
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003875 if (!event->cft->register_event || !event->cft->unregister_event) {
3876 ret = -EINVAL;
3877 goto fail;
3878 }
3879
3880 ret = event->cft->register_event(cgrp, event->cft,
3881 event->eventfd, buffer);
3882 if (ret)
3883 goto fail;
3884
Li Zefan7ef70e42013-04-26 11:58:03 -07003885 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003886
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003887 /*
3888 * Events should be removed after rmdir of cgroup directory, but before
3889 * destroying subsystem state objects. Let's take reference to cgroup
3890 * directory dentry to do that.
3891 */
3892 dget(cgrp->dentry);
3893
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003894 spin_lock(&cgrp->event_list_lock);
3895 list_add(&event->list, &cgrp->event_list);
3896 spin_unlock(&cgrp->event_list_lock);
3897
3898 fput(cfile);
3899 fput(efile);
3900
3901 return 0;
3902
3903fail:
3904 if (cfile)
3905 fput(cfile);
3906
3907 if (event && event->eventfd && !IS_ERR(event->eventfd))
3908 eventfd_ctx_put(event->eventfd);
3909
3910 if (!IS_ERR_OR_NULL(efile))
3911 fput(efile);
3912
3913 kfree(event);
3914
3915 return ret;
3916}
3917
Daniel Lezcano97978e62010-10-27 15:33:35 -07003918static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3919 struct cftype *cft)
3920{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003921 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003922}
3923
3924static int cgroup_clone_children_write(struct cgroup *cgrp,
3925 struct cftype *cft,
3926 u64 val)
3927{
3928 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003929 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003930 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003931 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003932 return 0;
3933}
3934
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003935/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003936 * for the common functions, 'private' gives the type of file
3937 */
Ben Blum102a7752009-09-23 15:56:26 -07003938/* for hysterical raisins, we can't put this on the older files */
3939#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003940static struct cftype files[] = {
3941 {
3942 .name = "tasks",
3943 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003944 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003945 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003946 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003947 },
Ben Blum102a7752009-09-23 15:56:26 -07003948 {
3949 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3950 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003951 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003952 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003953 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003954 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003955 {
3956 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003957 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003958 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003959 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003960 {
3961 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3962 .write_string = cgroup_write_event_control,
3963 .mode = S_IWUGO,
3964 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003965 {
3966 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003967 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003968 .read_u64 = cgroup_clone_children_read,
3969 .write_u64 = cgroup_clone_children_write,
3970 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003971 {
Tejun Heo873fe092013-04-14 20:15:26 -07003972 .name = "cgroup.sane_behavior",
3973 .flags = CFTYPE_ONLY_ON_ROOT,
3974 .read_seq_string = cgroup_sane_behavior_show,
3975 },
3976 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003977 .name = "release_agent",
3978 .flags = CFTYPE_ONLY_ON_ROOT,
3979 .read_seq_string = cgroup_release_agent_show,
3980 .write_string = cgroup_release_agent_write,
3981 .max_write_len = PATH_MAX,
3982 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003983 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003984};
3985
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003986/**
3987 * cgroup_populate_dir - selectively creation of files in a directory
3988 * @cgrp: target cgroup
3989 * @base_files: true if the base files should be added
3990 * @subsys_mask: mask of the subsystem ids whose files should be added
3991 */
3992static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3993 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003994{
3995 int err;
3996 struct cgroup_subsys *ss;
3997
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003998 if (base_files) {
3999 err = cgroup_addrm_files(cgrp, NULL, files, true);
4000 if (err < 0)
4001 return err;
4002 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004003
Tejun Heo8e3f6542012-04-01 12:09:55 -07004004 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004005 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004006 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004007 if (!test_bit(ss->subsys_id, &subsys_mask))
4008 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004009
Tejun Heodb0416b2012-04-01 12:09:55 -07004010 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004011 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004012 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004013
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004014 /* This cgroup is ready now */
4015 for_each_subsys(cgrp->root, ss) {
4016 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4017 /*
4018 * Update id->css pointer and make this css visible from
4019 * CSS ID functions. This pointer will be dereferened
4020 * from RCU-read-side without locks.
4021 */
4022 if (css->id)
4023 rcu_assign_pointer(css->id->css, css);
4024 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004025
4026 return 0;
4027}
4028
Tejun Heo48ddbe12012-04-01 12:09:56 -07004029static void css_dput_fn(struct work_struct *work)
4030{
4031 struct cgroup_subsys_state *css =
4032 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004033 struct dentry *dentry = css->cgroup->dentry;
4034 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004035
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004036 atomic_inc(&sb->s_active);
4037 dput(dentry);
4038 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004039}
4040
Paul Menageddbcc7e2007-10-18 23:39:30 -07004041static void init_cgroup_css(struct cgroup_subsys_state *css,
4042 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004043 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004044{
Paul Menagebd89aab2007-10-18 23:40:44 -07004045 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004046 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004047 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004048 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004049 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004050 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004051 BUG_ON(cgrp->subsys[ss->subsys_id]);
4052 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004053
4054 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004055 * css holds an extra ref to @cgrp->dentry which is put on the last
4056 * css_put(). dput() requires process context, which css_put() may
4057 * be called without. @css->dput_work will be used to invoke
4058 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004059 */
4060 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004061}
4062
Tejun Heob1929db2012-11-19 08:13:38 -08004063/* invoke ->post_create() on a new CSS and mark it online if successful */
4064static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004065{
Tejun Heob1929db2012-11-19 08:13:38 -08004066 int ret = 0;
4067
Tejun Heoa31f2d32012-11-19 08:13:37 -08004068 lockdep_assert_held(&cgroup_mutex);
4069
Tejun Heo92fb9742012-11-19 08:13:38 -08004070 if (ss->css_online)
4071 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004072 if (!ret)
4073 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4074 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004075}
4076
4077/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4078static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4079 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4080{
4081 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4082
4083 lockdep_assert_held(&cgroup_mutex);
4084
4085 if (!(css->flags & CSS_ONLINE))
4086 return;
4087
Li Zefand7eeac12013-03-12 15:35:59 -07004088 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004089 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004090
4091 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4092}
4093
Paul Menageddbcc7e2007-10-18 23:39:30 -07004094/*
Li Zefana043e3b2008-02-23 15:24:09 -08004095 * cgroup_create - create a cgroup
4096 * @parent: cgroup that will be parent of the new cgroup
4097 * @dentry: dentry of the new cgroup
4098 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004099 *
Li Zefana043e3b2008-02-23 15:24:09 -08004100 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004101 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004102static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004103 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004104{
Paul Menagebd89aab2007-10-18 23:40:44 -07004105 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004106 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004107 struct cgroupfs_root *root = parent->root;
4108 int err = 0;
4109 struct cgroup_subsys *ss;
4110 struct super_block *sb = root->sb;
4111
Tejun Heo0a950f62012-11-19 09:02:12 -08004112 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004113 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4114 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004115 return -ENOMEM;
4116
Li Zefan65dff752013-03-01 15:01:56 +08004117 name = cgroup_alloc_name(dentry);
4118 if (!name)
4119 goto err_free_cgrp;
4120 rcu_assign_pointer(cgrp->name, name);
4121
Tejun Heo0a950f62012-11-19 09:02:12 -08004122 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4123 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004124 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004125
Tejun Heo976c06b2012-11-05 09:16:59 -08004126 /*
4127 * Only live parents can have children. Note that the liveliness
4128 * check isn't strictly necessary because cgroup_mkdir() and
4129 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4130 * anyway so that locking is contained inside cgroup proper and we
4131 * don't get nasty surprises if we ever grow another caller.
4132 */
4133 if (!cgroup_lock_live_group(parent)) {
4134 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004135 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004136 }
4137
Paul Menageddbcc7e2007-10-18 23:39:30 -07004138 /* Grab a reference on the superblock so the hierarchy doesn't
4139 * get deleted on unmount if there are child cgroups. This
4140 * can be done outside cgroup_mutex, since the sb can't
4141 * disappear while someone has an open control file on the
4142 * fs */
4143 atomic_inc(&sb->s_active);
4144
Paul Menagecc31edc2008-10-18 20:28:04 -07004145 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004146
Li Zefanfe1c06c2013-01-24 14:30:22 +08004147 dentry->d_fsdata = cgrp;
4148 cgrp->dentry = dentry;
4149
Paul Menagebd89aab2007-10-18 23:40:44 -07004150 cgrp->parent = parent;
4151 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004152
Li Zefanb6abdb02008-03-04 14:28:19 -08004153 if (notify_on_release(parent))
4154 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4155
Tejun Heo2260e7f2012-11-19 08:13:38 -08004156 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4157 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004158
Paul Menageddbcc7e2007-10-18 23:39:30 -07004159 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004160 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004161
Tejun Heo92fb9742012-11-19 08:13:38 -08004162 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004163 if (IS_ERR(css)) {
4164 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004165 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004166 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004167 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004168 if (ss->use_id) {
4169 err = alloc_css_id(ss, parent, cgrp);
4170 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004171 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004172 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004173 }
4174
Tejun Heo4e139af2012-11-19 08:13:36 -08004175 /*
4176 * Create directory. cgroup_create_file() returns with the new
4177 * directory locked on success so that it can be populated without
4178 * dropping cgroup_mutex.
4179 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004180 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004181 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004182 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004183 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004184
Tejun Heo4e139af2012-11-19 08:13:36 -08004185 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004186 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4187 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4188 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004189
Tejun Heob1929db2012-11-19 08:13:38 -08004190 /* each css holds a ref to the cgroup's dentry */
4191 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004192 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004193
Li Zefan415cf072013-04-08 14:35:02 +08004194 /* hold a ref to the parent's dentry */
4195 dget(parent->dentry);
4196
Tejun Heob1929db2012-11-19 08:13:38 -08004197 /* creation succeeded, notify subsystems */
4198 for_each_subsys(root, ss) {
4199 err = online_css(ss, cgrp);
4200 if (err)
4201 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004202
4203 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4204 parent->parent) {
4205 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",
4206 current->comm, current->pid, ss->name);
4207 if (!strcmp(ss->name, "memory"))
4208 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4209 ss->warned_broken_hierarchy = true;
4210 }
Tejun Heoa8638032012-11-09 09:12:29 -08004211 }
4212
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004213 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004214 if (err)
4215 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004216
4217 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004218 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004219
4220 return 0;
4221
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004222err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004223 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004224 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004225 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004226 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004227 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004228 /* Release the reference count that we took on the superblock */
4229 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004230err_free_id:
4231 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004232err_free_name:
4233 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004234err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004235 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004236 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004237
4238err_destroy:
4239 cgroup_destroy_locked(cgrp);
4240 mutex_unlock(&cgroup_mutex);
4241 mutex_unlock(&dentry->d_inode->i_mutex);
4242 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004243}
4244
Al Viro18bb1db2011-07-26 01:41:39 -04004245static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004246{
4247 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4248
4249 /* the vfs holds inode->i_mutex already */
4250 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4251}
4252
Tejun Heo42809dd2012-11-19 08:13:37 -08004253static int cgroup_destroy_locked(struct cgroup *cgrp)
4254 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004255{
Tejun Heo42809dd2012-11-19 08:13:37 -08004256 struct dentry *d = cgrp->dentry;
4257 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004258 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004259 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004260
Tejun Heo42809dd2012-11-19 08:13:37 -08004261 lockdep_assert_held(&d->d_inode->i_mutex);
4262 lockdep_assert_held(&cgroup_mutex);
4263
4264 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004265 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004266
Tejun Heo1a90dd52012-11-05 09:16:59 -08004267 /*
4268 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4269 * removed. This makes future css_tryget() and child creation
4270 * attempts fail thus maintaining the removal conditions verified
4271 * above.
4272 */
Tejun Heoed9577932012-11-05 09:16:58 -08004273 for_each_subsys(cgrp->root, ss) {
4274 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4275
4276 WARN_ON(atomic_read(&css->refcnt) < 0);
4277 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004278 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004279 set_bit(CGRP_REMOVED, &cgrp->flags);
4280
Tejun Heoa31f2d32012-11-19 08:13:37 -08004281 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004282 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004283 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004284
4285 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004286 * Put all the base refs. Each css holds an extra reference to the
4287 * cgroup's dentry and cgroup removal proceeds regardless of css
4288 * refs. On the last put of each css, whenever that may be, the
4289 * extra dentry ref is put so that dentry destruction happens only
4290 * after all css's are released.
4291 */
Tejun Heoe9316082012-11-05 09:16:58 -08004292 for_each_subsys(cgrp->root, ss)
4293 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004294
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004295 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004296 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004297 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004298 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004299
Paul Menage999cd8a2009-01-07 18:08:36 -08004300 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004301 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004302 list_del_init(&cgrp->allcg_node);
4303
Tejun Heo42809dd2012-11-19 08:13:37 -08004304 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004305 cgroup_d_remove_dir(d);
4306 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004307
Paul Menagebd89aab2007-10-18 23:40:44 -07004308 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004309 check_for_release(parent);
4310
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004311 /*
4312 * Unregister events and notify userspace.
4313 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004314 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004315 */
4316 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004317 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004318 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004319 schedule_work(&event->remove);
4320 }
Li Zefan810cbee2013-02-18 18:56:14 +08004321 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004322
Paul Menageddbcc7e2007-10-18 23:39:30 -07004323 return 0;
4324}
4325
Tejun Heo42809dd2012-11-19 08:13:37 -08004326static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4327{
4328 int ret;
4329
4330 mutex_lock(&cgroup_mutex);
4331 ret = cgroup_destroy_locked(dentry->d_fsdata);
4332 mutex_unlock(&cgroup_mutex);
4333
4334 return ret;
4335}
4336
Tejun Heo8e3f6542012-04-01 12:09:55 -07004337static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4338{
4339 INIT_LIST_HEAD(&ss->cftsets);
4340
4341 /*
4342 * base_cftset is embedded in subsys itself, no need to worry about
4343 * deregistration.
4344 */
4345 if (ss->base_cftypes) {
4346 ss->base_cftset.cfts = ss->base_cftypes;
4347 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4348 }
4349}
4350
Li Zefan06a11922008-04-29 01:00:07 -07004351static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004352{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004353 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004354
4355 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004356
Tejun Heo648bb562012-11-19 08:13:36 -08004357 mutex_lock(&cgroup_mutex);
4358
Tejun Heo8e3f6542012-04-01 12:09:55 -07004359 /* init base cftset */
4360 cgroup_init_cftsets(ss);
4361
Paul Menageddbcc7e2007-10-18 23:39:30 -07004362 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004363 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004364 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004365 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004366 /* We don't handle early failures gracefully */
4367 BUG_ON(IS_ERR(css));
4368 init_cgroup_css(css, ss, dummytop);
4369
Li Zefane8d55fd2008-04-29 01:00:13 -07004370 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004371 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004372 * newly registered, all tasks and hence the
4373 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004374 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004375
4376 need_forkexit_callback |= ss->fork || ss->exit;
4377
Li Zefane8d55fd2008-04-29 01:00:13 -07004378 /* At system boot, before all subsystems have been
4379 * registered, no tasks have been forked, so we don't
4380 * need to invoke fork callbacks here. */
4381 BUG_ON(!list_empty(&init_task.tasks));
4382
Tejun Heob1929db2012-11-19 08:13:38 -08004383 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004384
Tejun Heo648bb562012-11-19 08:13:36 -08004385 mutex_unlock(&cgroup_mutex);
4386
Ben Blume6a11052010-03-10 15:22:09 -08004387 /* this function shouldn't be used with modular subsystems, since they
4388 * need to register a subsys_id, among other things */
4389 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004390}
4391
4392/**
Ben Blume6a11052010-03-10 15:22:09 -08004393 * cgroup_load_subsys: load and register a modular subsystem at runtime
4394 * @ss: the subsystem to load
4395 *
4396 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004397 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004398 * up for use. If the subsystem is built-in anyway, work is delegated to the
4399 * simpler cgroup_init_subsys.
4400 */
4401int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4402{
Ben Blume6a11052010-03-10 15:22:09 -08004403 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004404 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004405 struct hlist_node *tmp;
Li Zefan0ac801f2013-01-10 11:49:27 +08004406 struct css_set *cg;
4407 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004408
4409 /* check name and function validity */
4410 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004411 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004412 return -EINVAL;
4413
4414 /*
4415 * we don't support callbacks in modular subsystems. this check is
4416 * before the ss->module check for consistency; a subsystem that could
4417 * be a module should still have no callbacks even if the user isn't
4418 * compiling it as one.
4419 */
4420 if (ss->fork || ss->exit)
4421 return -EINVAL;
4422
4423 /*
4424 * an optionally modular subsystem is built-in: we want to do nothing,
4425 * since cgroup_init_subsys will have already taken care of it.
4426 */
4427 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004428 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004429 BUG_ON(subsys[ss->subsys_id] != ss);
4430 return 0;
4431 }
4432
Tejun Heo8e3f6542012-04-01 12:09:55 -07004433 /* init base cftset */
4434 cgroup_init_cftsets(ss);
4435
Ben Blume6a11052010-03-10 15:22:09 -08004436 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004437 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004438
4439 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004440 * no ss->css_alloc seems to need anything important in the ss
4441 * struct, so this can happen first (i.e. before the rootnode
4442 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004443 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004444 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004445 if (IS_ERR(css)) {
4446 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004447 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004448 mutex_unlock(&cgroup_mutex);
4449 return PTR_ERR(css);
4450 }
4451
4452 list_add(&ss->sibling, &rootnode.subsys_list);
4453 ss->root = &rootnode;
4454
4455 /* our new subsystem will be attached to the dummy hierarchy. */
4456 init_cgroup_css(css, ss, dummytop);
4457 /* init_idr must be after init_cgroup_css because it sets css->id. */
4458 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004459 ret = cgroup_init_idr(ss, css);
4460 if (ret)
4461 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004462 }
4463
4464 /*
4465 * Now we need to entangle the css into the existing css_sets. unlike
4466 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4467 * will need a new pointer to it; done by iterating the css_set_table.
4468 * furthermore, modifying the existing css_sets will corrupt the hash
4469 * table state, so each changed css_set will need its hash recomputed.
4470 * this is all done under the css_set_lock.
4471 */
4472 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004473 hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004474 /* skip entries that we already rehashed */
4475 if (cg->subsys[ss->subsys_id])
4476 continue;
4477 /* remove existing entry */
4478 hash_del(&cg->hlist);
4479 /* set new value */
4480 cg->subsys[ss->subsys_id] = css;
4481 /* recompute hash and restore entry */
4482 key = css_set_hash(cg->subsys);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004483 hash_add(css_set_table, &cg->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004484 }
4485 write_unlock(&css_set_lock);
4486
Tejun Heob1929db2012-11-19 08:13:38 -08004487 ret = online_css(ss, dummytop);
4488 if (ret)
4489 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004490
Ben Blume6a11052010-03-10 15:22:09 -08004491 /* success! */
4492 mutex_unlock(&cgroup_mutex);
4493 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004494
4495err_unload:
4496 mutex_unlock(&cgroup_mutex);
4497 /* @ss can't be mounted here as try_module_get() would fail */
4498 cgroup_unload_subsys(ss);
4499 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004500}
4501EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4502
4503/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004504 * cgroup_unload_subsys: unload a modular subsystem
4505 * @ss: the subsystem to unload
4506 *
4507 * This function should be called in a modular subsystem's exitcall. When this
4508 * function is invoked, the refcount on the subsystem's module will be 0, so
4509 * the subsystem will not be attached to any hierarchy.
4510 */
4511void cgroup_unload_subsys(struct cgroup_subsys *ss)
4512{
4513 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004514
4515 BUG_ON(ss->module == NULL);
4516
4517 /*
4518 * we shouldn't be called if the subsystem is in use, and the use of
4519 * try_module_get in parse_cgroupfs_options should ensure that it
4520 * doesn't start being used while we're killing it off.
4521 */
4522 BUG_ON(ss->root != &rootnode);
4523
4524 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004525
Tejun Heoa31f2d32012-11-19 08:13:37 -08004526 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004527
Tejun Heoc897ff62013-02-27 17:03:49 -08004528 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004529 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004530
Ben Blumcf5d5942010-03-10 15:22:09 -08004531 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004532 subsys[ss->subsys_id] = NULL;
4533
4534 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004535 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004536
4537 /*
4538 * disentangle the css from all css_sets attached to the dummytop. as
4539 * in loading, we need to pay our respects to the hashtable gods.
4540 */
4541 write_lock(&css_set_lock);
4542 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4543 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004544 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004545
Li Zefan0ac801f2013-01-10 11:49:27 +08004546 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004547 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004548 key = css_set_hash(cg->subsys);
4549 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004550 }
4551 write_unlock(&css_set_lock);
4552
4553 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004554 * remove subsystem's css from the dummytop and free it - need to
4555 * free before marking as null because ss->css_free needs the
4556 * cgrp->subsys pointer to find their state. note that this also
4557 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004558 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004559 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004560 dummytop->subsys[ss->subsys_id] = NULL;
4561
4562 mutex_unlock(&cgroup_mutex);
4563}
4564EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4565
4566/**
Li Zefana043e3b2008-02-23 15:24:09 -08004567 * cgroup_init_early - cgroup initialization at system boot
4568 *
4569 * Initialize cgroups at system boot, and initialize any
4570 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004571 */
4572int __init cgroup_init_early(void)
4573{
4574 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004575 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004576 INIT_LIST_HEAD(&init_css_set.cg_links);
4577 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004578 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004579 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004580 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004581 root_count = 1;
4582 init_task.cgroups = &init_css_set;
4583
4584 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004585 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004586 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004587 &rootnode.top_cgroup.css_sets);
4588 list_add(&init_css_set_link.cg_link_list,
4589 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004590
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004591 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004592 struct cgroup_subsys *ss = subsys[i];
4593
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004594 /* at bootup time, we don't worry about modular subsystems */
4595 if (!ss || ss->module)
4596 continue;
4597
Paul Menageddbcc7e2007-10-18 23:39:30 -07004598 BUG_ON(!ss->name);
4599 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004600 BUG_ON(!ss->css_alloc);
4601 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004602 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004603 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004604 ss->name, ss->subsys_id);
4605 BUG();
4606 }
4607
4608 if (ss->early_init)
4609 cgroup_init_subsys(ss);
4610 }
4611 return 0;
4612}
4613
4614/**
Li Zefana043e3b2008-02-23 15:24:09 -08004615 * cgroup_init - cgroup initialization
4616 *
4617 * Register cgroup filesystem and /proc file, and initialize
4618 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004619 */
4620int __init cgroup_init(void)
4621{
4622 int err;
4623 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004624 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004625
4626 err = bdi_init(&cgroup_backing_dev_info);
4627 if (err)
4628 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004629
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004630 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004631 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004632
4633 /* at bootup time, we don't worry about modular subsystems */
4634 if (!ss || ss->module)
4635 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004636 if (!ss->early_init)
4637 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004638 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004639 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004640 }
4641
Li Zefan472b1052008-04-29 01:00:11 -07004642 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004643 key = css_set_hash(init_css_set.subsys);
4644 hash_add(css_set_table, &init_css_set.hlist, key);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004645 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004646
4647 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4648 if (!cgroup_kobj) {
4649 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004650 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004651 }
4652
4653 err = register_filesystem(&cgroup_fs_type);
4654 if (err < 0) {
4655 kobject_put(cgroup_kobj);
4656 goto out;
4657 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004658
Li Zefan46ae2202008-04-29 01:00:08 -07004659 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004660
Paul Menageddbcc7e2007-10-18 23:39:30 -07004661out:
Paul Menagea4243162007-10-18 23:39:35 -07004662 if (err)
4663 bdi_destroy(&cgroup_backing_dev_info);
4664
Paul Menageddbcc7e2007-10-18 23:39:30 -07004665 return err;
4666}
Paul Menageb4f48b62007-10-18 23:39:33 -07004667
Paul Menagea4243162007-10-18 23:39:35 -07004668/*
4669 * proc_cgroup_show()
4670 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4671 * - Used for /proc/<pid>/cgroup.
4672 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4673 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004674 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004675 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4676 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4677 * cgroup to top_cgroup.
4678 */
4679
4680/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004681int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004682{
4683 struct pid *pid;
4684 struct task_struct *tsk;
4685 char *buf;
4686 int retval;
4687 struct cgroupfs_root *root;
4688
4689 retval = -ENOMEM;
4690 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4691 if (!buf)
4692 goto out;
4693
4694 retval = -ESRCH;
4695 pid = m->private;
4696 tsk = get_pid_task(pid, PIDTYPE_PID);
4697 if (!tsk)
4698 goto out_free;
4699
4700 retval = 0;
4701
4702 mutex_lock(&cgroup_mutex);
4703
Li Zefane5f6a862009-01-07 18:07:41 -08004704 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004705 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004706 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004707 int count = 0;
4708
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004709 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004710 for_each_subsys(root, ss)
4711 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004712 if (strlen(root->name))
4713 seq_printf(m, "%sname=%s", count ? "," : "",
4714 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004715 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004716 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004717 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004718 if (retval < 0)
4719 goto out_unlock;
4720 seq_puts(m, buf);
4721 seq_putc(m, '\n');
4722 }
4723
4724out_unlock:
4725 mutex_unlock(&cgroup_mutex);
4726 put_task_struct(tsk);
4727out_free:
4728 kfree(buf);
4729out:
4730 return retval;
4731}
4732
Paul Menagea4243162007-10-18 23:39:35 -07004733/* Display information about each subsystem and each hierarchy */
4734static int proc_cgroupstats_show(struct seq_file *m, void *v)
4735{
4736 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004737
Paul Menage8bab8dd2008-04-04 14:29:57 -07004738 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004739 /*
4740 * ideally we don't want subsystems moving around while we do this.
4741 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4742 * subsys/hierarchy state.
4743 */
Paul Menagea4243162007-10-18 23:39:35 -07004744 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004745 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4746 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004747 if (ss == NULL)
4748 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004749 seq_printf(m, "%s\t%d\t%d\t%d\n",
4750 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004751 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004752 }
4753 mutex_unlock(&cgroup_mutex);
4754 return 0;
4755}
4756
4757static int cgroupstats_open(struct inode *inode, struct file *file)
4758{
Al Viro9dce07f2008-03-29 03:07:28 +00004759 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004760}
4761
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004762static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004763 .open = cgroupstats_open,
4764 .read = seq_read,
4765 .llseek = seq_lseek,
4766 .release = single_release,
4767};
4768
Paul Menageb4f48b62007-10-18 23:39:33 -07004769/**
4770 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004771 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004772 *
4773 * Description: A task inherits its parent's cgroup at fork().
4774 *
4775 * A pointer to the shared css_set was automatically copied in
4776 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004777 * it was not made under the protection of RCU or cgroup_mutex, so
4778 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4779 * have already changed current->cgroups, allowing the previously
4780 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004781 *
4782 * At the point that cgroup_fork() is called, 'current' is the parent
4783 * task, and the passed argument 'child' points to the child task.
4784 */
4785void cgroup_fork(struct task_struct *child)
4786{
Tejun Heo9bb71302012-10-18 17:52:07 -07004787 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004788 child->cgroups = current->cgroups;
4789 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004790 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004791 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004792}
4793
4794/**
Li Zefana043e3b2008-02-23 15:24:09 -08004795 * cgroup_post_fork - called on a new task after adding it to the task list
4796 * @child: the task in question
4797 *
Tejun Heo5edee612012-10-16 15:03:14 -07004798 * Adds the task to the list running through its css_set if necessary and
4799 * call the subsystem fork() callbacks. Has to be after the task is
4800 * visible on the task list in case we race with the first call to
4801 * cgroup_iter_start() - to guarantee that the new task ends up on its
4802 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004803 */
Paul Menage817929e2007-10-18 23:39:36 -07004804void cgroup_post_fork(struct task_struct *child)
4805{
Tejun Heo5edee612012-10-16 15:03:14 -07004806 int i;
4807
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004808 /*
4809 * use_task_css_set_links is set to 1 before we walk the tasklist
4810 * under the tasklist_lock and we read it here after we added the child
4811 * to the tasklist under the tasklist_lock as well. If the child wasn't
4812 * yet in the tasklist when we walked through it from
4813 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4814 * should be visible now due to the paired locking and barriers implied
4815 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4816 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4817 * lock on fork.
4818 */
Paul Menage817929e2007-10-18 23:39:36 -07004819 if (use_task_css_set_links) {
4820 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004821 task_lock(child);
4822 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004823 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004824 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004825 write_unlock(&css_set_lock);
4826 }
Tejun Heo5edee612012-10-16 15:03:14 -07004827
4828 /*
4829 * Call ss->fork(). This must happen after @child is linked on
4830 * css_set; otherwise, @child might change state between ->fork()
4831 * and addition to css_set.
4832 */
4833 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004834 /*
4835 * fork/exit callbacks are supported only for builtin
4836 * subsystems, and the builtin section of the subsys
4837 * array is immutable, so we don't need to lock the
4838 * subsys array here. On the other hand, modular section
4839 * of the array can be freed at module unload, so we
4840 * can't touch that.
4841 */
4842 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004843 struct cgroup_subsys *ss = subsys[i];
4844
Tejun Heo5edee612012-10-16 15:03:14 -07004845 if (ss->fork)
4846 ss->fork(child);
4847 }
4848 }
Paul Menage817929e2007-10-18 23:39:36 -07004849}
Tejun Heo5edee612012-10-16 15:03:14 -07004850
Paul Menage817929e2007-10-18 23:39:36 -07004851/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004852 * cgroup_exit - detach cgroup from exiting task
4853 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004854 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004855 *
4856 * Description: Detach cgroup from @tsk and release it.
4857 *
4858 * Note that cgroups marked notify_on_release force every task in
4859 * them to take the global cgroup_mutex mutex when exiting.
4860 * This could impact scaling on very large systems. Be reluctant to
4861 * use notify_on_release cgroups where very high task exit scaling
4862 * is required on large systems.
4863 *
4864 * the_top_cgroup_hack:
4865 *
4866 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4867 *
4868 * We call cgroup_exit() while the task is still competent to
4869 * handle notify_on_release(), then leave the task attached to the
4870 * root cgroup in each hierarchy for the remainder of its exit.
4871 *
4872 * To do this properly, we would increment the reference count on
4873 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4874 * code we would add a second cgroup function call, to drop that
4875 * reference. This would just create an unnecessary hot spot on
4876 * the top_cgroup reference count, to no avail.
4877 *
4878 * Normally, holding a reference to a cgroup without bumping its
4879 * count is unsafe. The cgroup could go away, or someone could
4880 * attach us to a different cgroup, decrementing the count on
4881 * the first cgroup that we never incremented. But in this case,
4882 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004883 * which wards off any cgroup_attach_task() attempts, or task is a failed
4884 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004885 */
4886void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4887{
Paul Menage817929e2007-10-18 23:39:36 -07004888 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004889 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004890
4891 /*
4892 * Unlink from the css_set task list if necessary.
4893 * Optimistically check cg_list before taking
4894 * css_set_lock
4895 */
4896 if (!list_empty(&tsk->cg_list)) {
4897 write_lock(&css_set_lock);
4898 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004899 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004900 write_unlock(&css_set_lock);
4901 }
4902
Paul Menageb4f48b62007-10-18 23:39:33 -07004903 /* Reassign the task to the init_css_set. */
4904 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004905 cg = tsk->cgroups;
4906 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004907
4908 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004909 /*
4910 * fork/exit callbacks are supported only for builtin
4911 * subsystems, see cgroup_post_fork() for details.
4912 */
4913 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004914 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004915
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004916 if (ss->exit) {
4917 struct cgroup *old_cgrp =
4918 rcu_dereference_raw(cg->subsys[i])->cgroup;
4919 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004920 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004921 }
4922 }
4923 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004924 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004925
Li Zefanb5d646f2013-01-24 14:43:51 +08004926 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004927}
Paul Menage697f4162007-10-18 23:39:34 -07004928
Paul Menagebd89aab2007-10-18 23:40:44 -07004929static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004930{
4931 /* All of these checks rely on RCU to keep the cgroup
4932 * structure alive */
Li Zefanf50daa72013-03-01 15:06:07 +08004933 if (cgroup_is_releasable(cgrp) &&
4934 !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) {
4935 /*
4936 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004937 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004938 * it now
4939 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004940 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004941
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004942 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004943 if (!cgroup_is_removed(cgrp) &&
4944 list_empty(&cgrp->release_list)) {
4945 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004946 need_schedule_work = 1;
4947 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004948 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004949 if (need_schedule_work)
4950 schedule_work(&release_agent_work);
4951 }
4952}
4953
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004954/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07004955bool __css_tryget(struct cgroup_subsys_state *css)
4956{
Tejun Heoe9316082012-11-05 09:16:58 -08004957 while (true) {
4958 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004959
Tejun Heoe9316082012-11-05 09:16:58 -08004960 v = css_refcnt(css);
4961 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
4962 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07004963 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08004964 else if (t < 0)
4965 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07004966 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08004967 }
Tejun Heo28b4c272012-04-01 12:09:56 -07004968}
4969EXPORT_SYMBOL_GPL(__css_tryget);
4970
4971/* Caller must verify that the css is not for root cgroup */
4972void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004973{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004974 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004975
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004976 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08004977 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08004978 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004979}
Ben Blum67523c42010-03-10 15:22:11 -08004980EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004981
4982/*
4983 * Notify userspace when a cgroup is released, by running the
4984 * configured release agent with the name of the cgroup (path
4985 * relative to the root of cgroup file system) as the argument.
4986 *
4987 * Most likely, this user command will try to rmdir this cgroup.
4988 *
4989 * This races with the possibility that some other task will be
4990 * attached to this cgroup before it is removed, or that some other
4991 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4992 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4993 * unused, and this cgroup will be reprieved from its death sentence,
4994 * to continue to serve a useful existence. Next time it's released,
4995 * we will get notified again, if it still has 'notify_on_release' set.
4996 *
4997 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4998 * means only wait until the task is successfully execve()'d. The
4999 * separate release agent task is forked by call_usermodehelper(),
5000 * then control in this thread returns here, without waiting for the
5001 * release agent task. We don't bother to wait because the caller of
5002 * this routine has no use for the exit status of the release agent
5003 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005004 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005005static void cgroup_release_agent(struct work_struct *work)
5006{
5007 BUG_ON(work != &release_agent_work);
5008 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005009 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005010 while (!list_empty(&release_list)) {
5011 char *argv[3], *envp[3];
5012 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005013 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005014 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005015 struct cgroup,
5016 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005017 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005018 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005019 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005020 if (!pathbuf)
5021 goto continue_free;
5022 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5023 goto continue_free;
5024 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5025 if (!agentbuf)
5026 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005027
5028 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005029 argv[i++] = agentbuf;
5030 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005031 argv[i] = NULL;
5032
5033 i = 0;
5034 /* minimal command environment */
5035 envp[i++] = "HOME=/";
5036 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5037 envp[i] = NULL;
5038
5039 /* Drop the lock while we invoke the usermode helper,
5040 * since the exec could involve hitting disk and hence
5041 * be a slow process */
5042 mutex_unlock(&cgroup_mutex);
5043 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005044 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005045 continue_free:
5046 kfree(pathbuf);
5047 kfree(agentbuf);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005048 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005049 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005050 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005051 mutex_unlock(&cgroup_mutex);
5052}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005053
5054static int __init cgroup_disable(char *str)
5055{
5056 int i;
5057 char *token;
5058
5059 while ((token = strsep(&str, ",")) != NULL) {
5060 if (!*token)
5061 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005062 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005063 struct cgroup_subsys *ss = subsys[i];
5064
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005065 /*
5066 * cgroup_disable, being at boot time, can't
5067 * know about module subsystems, so we don't
5068 * worry about them.
5069 */
5070 if (!ss || ss->module)
5071 continue;
5072
Paul Menage8bab8dd2008-04-04 14:29:57 -07005073 if (!strcmp(token, ss->name)) {
5074 ss->disabled = 1;
5075 printk(KERN_INFO "Disabling %s control group"
5076 " subsystem\n", ss->name);
5077 break;
5078 }
5079 }
5080 }
5081 return 1;
5082}
5083__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005084
5085/*
5086 * Functons for CSS ID.
5087 */
5088
5089/*
5090 *To get ID other than 0, this should be called when !cgroup_is_removed().
5091 */
5092unsigned short css_id(struct cgroup_subsys_state *css)
5093{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005094 struct css_id *cssid;
5095
5096 /*
5097 * This css_id() can return correct value when somone has refcnt
5098 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5099 * it's unchanged until freed.
5100 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005101 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005102
5103 if (cssid)
5104 return cssid->id;
5105 return 0;
5106}
Ben Blum67523c42010-03-10 15:22:11 -08005107EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005108
5109unsigned short css_depth(struct cgroup_subsys_state *css)
5110{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005111 struct css_id *cssid;
5112
Tejun Heo28b4c272012-04-01 12:09:56 -07005113 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005114
5115 if (cssid)
5116 return cssid->depth;
5117 return 0;
5118}
Ben Blum67523c42010-03-10 15:22:11 -08005119EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005120
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005121/**
5122 * css_is_ancestor - test "root" css is an ancestor of "child"
5123 * @child: the css to be tested.
5124 * @root: the css supporsed to be an ancestor of the child.
5125 *
5126 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005127 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005128 * But, considering usual usage, the csses should be valid objects after test.
5129 * Assuming that the caller will do some action to the child if this returns
5130 * returns true, the caller must take "child";s reference count.
5131 * If "child" is valid object and this returns true, "root" is valid, too.
5132 */
5133
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005134bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005135 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005136{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005137 struct css_id *child_id;
5138 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005139
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005140 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005141 if (!child_id)
5142 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005143 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005144 if (!root_id)
5145 return false;
5146 if (child_id->depth < root_id->depth)
5147 return false;
5148 if (child_id->stack[root_id->depth] != root_id->id)
5149 return false;
5150 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005151}
5152
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005153void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5154{
5155 struct css_id *id = css->id;
5156 /* When this is called before css_id initialization, id can be NULL */
5157 if (!id)
5158 return;
5159
5160 BUG_ON(!ss->use_id);
5161
5162 rcu_assign_pointer(id->css, NULL);
5163 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005164 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005165 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005166 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005167 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005168}
Ben Blum67523c42010-03-10 15:22:11 -08005169EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005170
5171/*
5172 * This is called by init or create(). Then, calls to this function are
5173 * always serialized (By cgroup_mutex() at create()).
5174 */
5175
5176static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5177{
5178 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005179 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005180
5181 BUG_ON(!ss->use_id);
5182
5183 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5184 newid = kzalloc(size, GFP_KERNEL);
5185 if (!newid)
5186 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005187
5188 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005189 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005190 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005191 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005192 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005193 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005194
5195 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005196 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005197 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005198
Tejun Heod228d9e2013-02-27 17:04:54 -08005199 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005200 newid->depth = depth;
5201 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005202err_out:
5203 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005204 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005205
5206}
5207
Ben Blume6a11052010-03-10 15:22:09 -08005208static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5209 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005210{
5211 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005212
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005213 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005214 idr_init(&ss->idr);
5215
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005216 newid = get_new_cssid(ss, 0);
5217 if (IS_ERR(newid))
5218 return PTR_ERR(newid);
5219
5220 newid->stack[0] = newid->id;
5221 newid->css = rootcss;
5222 rootcss->id = newid;
5223 return 0;
5224}
5225
5226static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5227 struct cgroup *child)
5228{
5229 int subsys_id, i, depth = 0;
5230 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005231 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005232
5233 subsys_id = ss->subsys_id;
5234 parent_css = parent->subsys[subsys_id];
5235 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005236 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005237 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005238
5239 child_id = get_new_cssid(ss, depth);
5240 if (IS_ERR(child_id))
5241 return PTR_ERR(child_id);
5242
5243 for (i = 0; i < depth; i++)
5244 child_id->stack[i] = parent_id->stack[i];
5245 child_id->stack[depth] = child_id->id;
5246 /*
5247 * child_id->css pointer will be set after this cgroup is available
5248 * see cgroup_populate_dir()
5249 */
5250 rcu_assign_pointer(child_css->id, child_id);
5251
5252 return 0;
5253}
5254
5255/**
5256 * css_lookup - lookup css by id
5257 * @ss: cgroup subsys to be looked into.
5258 * @id: the id
5259 *
5260 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5261 * NULL if not. Should be called under rcu_read_lock()
5262 */
5263struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5264{
5265 struct css_id *cssid = NULL;
5266
5267 BUG_ON(!ss->use_id);
5268 cssid = idr_find(&ss->idr, id);
5269
5270 if (unlikely(!cssid))
5271 return NULL;
5272
5273 return rcu_dereference(cssid->css);
5274}
Ben Blum67523c42010-03-10 15:22:11 -08005275EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005276
Stephane Eraniane5d13672011-02-14 11:20:01 +02005277/*
5278 * get corresponding css from file open on cgroupfs directory
5279 */
5280struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5281{
5282 struct cgroup *cgrp;
5283 struct inode *inode;
5284 struct cgroup_subsys_state *css;
5285
Al Viro496ad9a2013-01-23 17:07:38 -05005286 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005287 /* check in cgroup filesystem dir */
5288 if (inode->i_op != &cgroup_dir_inode_operations)
5289 return ERR_PTR(-EBADF);
5290
5291 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5292 return ERR_PTR(-EINVAL);
5293
5294 /* get cgroup */
5295 cgrp = __d_cgrp(f->f_dentry);
5296 css = cgrp->subsys[id];
5297 return css ? css : ERR_PTR(-ENOENT);
5298}
5299
Paul Menagefe693432009-09-23 15:56:20 -07005300#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005301static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005302{
5303 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5304
5305 if (!css)
5306 return ERR_PTR(-ENOMEM);
5307
5308 return css;
5309}
5310
Tejun Heo92fb9742012-11-19 08:13:38 -08005311static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005312{
5313 kfree(cont->subsys[debug_subsys_id]);
5314}
5315
5316static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5317{
5318 return atomic_read(&cont->count);
5319}
5320
5321static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5322{
5323 return cgroup_task_count(cont);
5324}
5325
5326static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5327{
5328 return (u64)(unsigned long)current->cgroups;
5329}
5330
5331static u64 current_css_set_refcount_read(struct cgroup *cont,
5332 struct cftype *cft)
5333{
5334 u64 count;
5335
5336 rcu_read_lock();
5337 count = atomic_read(&current->cgroups->refcount);
5338 rcu_read_unlock();
5339 return count;
5340}
5341
Paul Menage7717f7b2009-09-23 15:56:22 -07005342static int current_css_set_cg_links_read(struct cgroup *cont,
5343 struct cftype *cft,
5344 struct seq_file *seq)
5345{
5346 struct cg_cgroup_link *link;
5347 struct css_set *cg;
5348
5349 read_lock(&css_set_lock);
5350 rcu_read_lock();
5351 cg = rcu_dereference(current->cgroups);
5352 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5353 struct cgroup *c = link->cgrp;
5354 const char *name;
5355
5356 if (c->dentry)
5357 name = c->dentry->d_name.name;
5358 else
5359 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005360 seq_printf(seq, "Root %d group %s\n",
5361 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005362 }
5363 rcu_read_unlock();
5364 read_unlock(&css_set_lock);
5365 return 0;
5366}
5367
5368#define MAX_TASKS_SHOWN_PER_CSS 25
5369static int cgroup_css_links_read(struct cgroup *cont,
5370 struct cftype *cft,
5371 struct seq_file *seq)
5372{
5373 struct cg_cgroup_link *link;
5374
5375 read_lock(&css_set_lock);
5376 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5377 struct css_set *cg = link->cg;
5378 struct task_struct *task;
5379 int count = 0;
5380 seq_printf(seq, "css_set %p\n", cg);
5381 list_for_each_entry(task, &cg->tasks, cg_list) {
5382 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5383 seq_puts(seq, " ...\n");
5384 break;
5385 } else {
5386 seq_printf(seq, " task %d\n",
5387 task_pid_vnr(task));
5388 }
5389 }
5390 }
5391 read_unlock(&css_set_lock);
5392 return 0;
5393}
5394
Paul Menagefe693432009-09-23 15:56:20 -07005395static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5396{
5397 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5398}
5399
5400static struct cftype debug_files[] = {
5401 {
5402 .name = "cgroup_refcount",
5403 .read_u64 = cgroup_refcount_read,
5404 },
5405 {
5406 .name = "taskcount",
5407 .read_u64 = debug_taskcount_read,
5408 },
5409
5410 {
5411 .name = "current_css_set",
5412 .read_u64 = current_css_set_read,
5413 },
5414
5415 {
5416 .name = "current_css_set_refcount",
5417 .read_u64 = current_css_set_refcount_read,
5418 },
5419
5420 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005421 .name = "current_css_set_cg_links",
5422 .read_seq_string = current_css_set_cg_links_read,
5423 },
5424
5425 {
5426 .name = "cgroup_css_links",
5427 .read_seq_string = cgroup_css_links_read,
5428 },
5429
5430 {
Paul Menagefe693432009-09-23 15:56:20 -07005431 .name = "releasable",
5432 .read_u64 = releasable_read,
5433 },
Paul Menagefe693432009-09-23 15:56:20 -07005434
Tejun Heo4baf6e32012-04-01 12:09:55 -07005435 { } /* terminate */
5436};
Paul Menagefe693432009-09-23 15:56:20 -07005437
5438struct cgroup_subsys debug_subsys = {
5439 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005440 .css_alloc = debug_css_alloc,
5441 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005442 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005443 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005444};
5445#endif /* CONFIG_CGROUP_DEBUG */