blob: d0803f097f87ead7348445db6ce789c0effff1c5 [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>
33#include <linux/fs.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100034#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/kernel.h>
36#include <linux/list.h>
37#include <linux/mm.h>
38#include <linux/mutex.h>
39#include <linux/mount.h>
40#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070041#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070042#include <linux/rcupdate.h>
43#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070044#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/seq_file.h>
46#include <linux/slab.h>
47#include <linux/magic.h>
48#include <linux/spinlock.h>
49#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080052#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070053#include <linux/delayacct.h>
54#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070055#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040056#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070057#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070058#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070059#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080060#include <linux/eventfd.h>
61#include <linux/poll.h>
Ben Blumd8466872011-05-26 16:25:21 -070062#include <linux/flex_array.h> /* used in cgroup_attach_proc */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020063#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070064
Arun Sharma600634972011-07-26 16:09:06 -070065#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070066
Tejun Heo28b4c272012-04-01 12:09:56 -070067/* css deactivation bias, makes css->refcnt negative to deny new trygets */
68#define CSS_DEACT_BIAS INT_MIN
69
Tejun Heoe25e2cb2011-12-12 18:12:21 -080070/*
71 * cgroup_mutex is the master lock. Any modification to cgroup or its
72 * hierarchy must be performed while holding it.
73 *
74 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
75 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
76 * release_agent_path and so on. Modifying requires both cgroup_mutex and
77 * cgroup_root_mutex. Readers can acquire either of the two. This is to
78 * break the following locking order cycle.
79 *
80 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
81 * B. namespace_sem -> cgroup_mutex
82 *
83 * B happens only through cgroup_show_options() and using cgroup_root_mutex
84 * breaks it.
85 */
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -080087static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070088
Ben Blumaae8aab2010-03-10 15:22:07 -080089/*
90 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020091 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080092 * registered after that. The mutable section of this array is protected by
93 * cgroup_mutex.
94 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020095#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020096#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -080097static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070098#include <linux/cgroup_subsys.h>
99};
100
Paul Menagec6d57f32009-09-23 15:56:19 -0700101#define MAX_CGROUP_ROOT_NAMELEN 64
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
104 * A cgroupfs_root represents the root of a cgroup hierarchy,
105 * and may be associated with a superblock to form an active
106 * hierarchy
107 */
108struct cgroupfs_root {
109 struct super_block *sb;
110
111 /*
112 * The bitmask of subsystems intended to be attached to this
113 * hierarchy
114 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400115 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700116
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700117 /* Unique id for this hierarchy. */
118 int hierarchy_id;
119
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120 /* The bitmask of subsystems currently attached to this hierarchy */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400121 unsigned long actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122
123 /* A list running through the attached subsystems */
124 struct list_head subsys_list;
125
126 /* The root cgroup for this hierarchy */
127 struct cgroup top_cgroup;
128
129 /* Tracks how many cgroups are currently defined in hierarchy.*/
130 int number_of_cgroups;
131
Li Zefane5f6a862009-01-07 18:07:41 -0800132 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700133 struct list_head root_list;
134
Tejun Heob0ca5a82012-04-01 12:09:54 -0700135 /* All cgroups on this root, cgroup_mutex protected */
136 struct list_head allcg_list;
137
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138 /* Hierarchy-specific flags */
139 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700140
Paul Menagee788e062008-07-25 01:46:59 -0700141 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700142 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700143
144 /* The name for this hierarchy - may be empty */
145 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700146};
147
Paul Menageddbcc7e2007-10-18 23:39:30 -0700148/*
149 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
150 * subsystems that are otherwise unattached - it never has more than a
151 * single cgroup, and all tasks are part of that cgroup.
152 */
153static struct cgroupfs_root rootnode;
154
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700155/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700156 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
157 */
158struct cfent {
159 struct list_head node;
160 struct dentry *dentry;
161 struct cftype *type;
162};
163
164/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700165 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
166 * cgroup_subsys->use_id != 0.
167 */
168#define CSS_ID_MAX (65535)
169struct css_id {
170 /*
171 * The css to which this ID points. This pointer is set to valid value
172 * after cgroup is populated. If cgroup is removed, this will be NULL.
173 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800174 * is called after synchronize_rcu(). But for safe use, css_tryget()
175 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700176 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100177 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700178 /*
179 * ID of this css.
180 */
181 unsigned short id;
182 /*
183 * Depth in hierarchy which this ID belongs to.
184 */
185 unsigned short depth;
186 /*
187 * ID is freed by RCU. (and lookup routine is RCU safe.)
188 */
189 struct rcu_head rcu_head;
190 /*
191 * Hierarchy of CSS ID belongs to.
192 */
193 unsigned short stack[0]; /* Array of Length (depth+1) */
194};
195
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800196/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300197 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800198 */
199struct cgroup_event {
200 /*
201 * Cgroup which the event belongs to.
202 */
203 struct cgroup *cgrp;
204 /*
205 * Control file which the event associated.
206 */
207 struct cftype *cft;
208 /*
209 * eventfd to signal userspace about the event.
210 */
211 struct eventfd_ctx *eventfd;
212 /*
213 * Each of these stored in a list by the cgroup.
214 */
215 struct list_head list;
216 /*
217 * All fields below needed to unregister event when
218 * userspace closes eventfd.
219 */
220 poll_table pt;
221 wait_queue_head_t *wqh;
222 wait_queue_t wait;
223 struct work_struct remove;
224};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700225
Paul Menageddbcc7e2007-10-18 23:39:30 -0700226/* The list of hierarchy roots */
227
228static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700229static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700230
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700231static DEFINE_IDA(hierarchy_ida);
232static int next_hierarchy_id;
233static DEFINE_SPINLOCK(hierarchy_id_lock);
234
Paul Menageddbcc7e2007-10-18 23:39:30 -0700235/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
236#define dummytop (&rootnode.top_cgroup)
237
238/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800239 * check for fork/exit handlers to call. This avoids us having to do
240 * extra work in the fork/exit path if none of the subsystems need to
241 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700242 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700243static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700244
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800245#ifdef CONFIG_PROVE_LOCKING
246int cgroup_lock_is_held(void)
247{
248 return lockdep_is_held(&cgroup_mutex);
249}
250#else /* #ifdef CONFIG_PROVE_LOCKING */
251int cgroup_lock_is_held(void)
252{
253 return mutex_is_locked(&cgroup_mutex);
254}
255#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
256
257EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
258
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700259static int css_unbias_refcnt(int refcnt)
260{
261 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
262}
263
Tejun Heo28b4c272012-04-01 12:09:56 -0700264/* the current nr of refs, always >= 0 whether @css is deactivated or not */
265static int css_refcnt(struct cgroup_subsys_state *css)
266{
267 int v = atomic_read(&css->refcnt);
268
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700269 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700270}
271
Paul Menageddbcc7e2007-10-18 23:39:30 -0700272/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700273inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700274{
Paul Menagebd89aab2007-10-18 23:40:44 -0700275 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700276}
277
278/* bits in struct cgroupfs_root flags field */
279enum {
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400280 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
281 ROOT_XATTR, /* supports extended attributes */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700282};
283
Adrian Bunke9685a02008-02-07 00:13:46 -0800284static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700285{
286 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700287 (1 << CGRP_RELEASABLE) |
288 (1 << CGRP_NOTIFY_ON_RELEASE);
289 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700290}
291
Adrian Bunke9685a02008-02-07 00:13:46 -0800292static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700293{
Paul Menagebd89aab2007-10-18 23:40:44 -0700294 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700295}
296
Daniel Lezcano97978e62010-10-27 15:33:35 -0700297static int clone_children(const struct cgroup *cgrp)
298{
299 return test_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
300}
301
Paul Menageddbcc7e2007-10-18 23:39:30 -0700302/*
303 * for_each_subsys() allows you to iterate on each subsystem attached to
304 * an active hierarchy
305 */
306#define for_each_subsys(_root, _ss) \
307list_for_each_entry(_ss, &_root->subsys_list, sibling)
308
Li Zefane5f6a862009-01-07 18:07:41 -0800309/* for_each_active_root() allows you to iterate across the active hierarchies */
310#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700311list_for_each_entry(_root, &roots, root_list)
312
Tejun Heof6ea9372012-04-01 12:09:55 -0700313static inline struct cgroup *__d_cgrp(struct dentry *dentry)
314{
315 return dentry->d_fsdata;
316}
317
Tejun Heo05ef1d72012-04-01 12:09:56 -0700318static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700319{
320 return dentry->d_fsdata;
321}
322
Tejun Heo05ef1d72012-04-01 12:09:56 -0700323static inline struct cftype *__d_cft(struct dentry *dentry)
324{
325 return __d_cfe(dentry)->type;
326}
327
Paul Menage81a6a5c2007-10-18 23:39:38 -0700328/* the list of cgroups eligible for automatic release. Protected by
329 * release_list_lock */
330static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200331static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700332static void cgroup_release_agent(struct work_struct *work);
333static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700334static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700335
Paul Menage817929e2007-10-18 23:39:36 -0700336/* Link structure for associating css_set objects with cgroups */
337struct cg_cgroup_link {
338 /*
339 * List running through cg_cgroup_links associated with a
340 * cgroup, anchored on cgroup->css_sets
341 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700342 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700343 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700344 /*
345 * List running through cg_cgroup_links pointing at a
346 * single css_set object, anchored on css_set->cg_links
347 */
348 struct list_head cg_link_list;
349 struct css_set *cg;
350};
351
352/* The default css_set - used by init and its children prior to any
353 * hierarchies being mounted. It contains a pointer to the root state
354 * for each subsystem. Also used to anchor the list of css_sets. Not
355 * reference-counted, to improve performance when child cgroups
356 * haven't been created.
357 */
358
359static struct css_set init_css_set;
360static struct cg_cgroup_link init_css_set_link;
361
Ben Blume6a11052010-03-10 15:22:09 -0800362static int cgroup_init_idr(struct cgroup_subsys *ss,
363 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700364
Paul Menage817929e2007-10-18 23:39:36 -0700365/* css_set_lock protects the list of css_set objects, and the
366 * chain of tasks off each css_set. Nests outside task->alloc_lock
367 * due to cgroup_iter_start() */
368static DEFINE_RWLOCK(css_set_lock);
369static int css_set_count;
370
Paul Menage7717f7b2009-09-23 15:56:22 -0700371/*
372 * hash table for cgroup groups. This improves the performance to find
373 * an existing css_set. This hash doesn't (currently) take into
374 * account cgroups in empty hierarchies.
375 */
Li Zefan472b1052008-04-29 01:00:11 -0700376#define CSS_SET_HASH_BITS 7
377#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
378static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
379
380static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
381{
382 int i;
383 int index;
384 unsigned long tmp = 0UL;
385
386 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
387 tmp += (unsigned long)css[i];
388 tmp = (tmp >> 16) ^ tmp;
389
390 index = hash_long(tmp, CSS_SET_HASH_BITS);
391
392 return &css_set_table[index];
393}
394
Paul Menage817929e2007-10-18 23:39:36 -0700395/* We don't maintain the lists running through each css_set to its
396 * task until after the first call to cgroup_iter_start(). This
397 * reduces the fork()/exit() overhead for people who have cgroups
398 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700399static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700400
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700401static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700402{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700403 struct cg_cgroup_link *link;
404 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700405 /*
406 * Ensure that the refcount doesn't hit zero while any readers
407 * can see it. Similar to atomic_dec_and_lock(), but for an
408 * rwlock
409 */
410 if (atomic_add_unless(&cg->refcount, -1, 1))
411 return;
412 write_lock(&css_set_lock);
413 if (!atomic_dec_and_test(&cg->refcount)) {
414 write_unlock(&css_set_lock);
415 return;
416 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700417
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700418 /* This css_set is dead. unlink it and release cgroup refcounts */
419 hlist_del(&cg->hlist);
420 css_set_count--;
421
422 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
423 cg_link_list) {
424 struct cgroup *cgrp = link->cgrp;
425 list_del(&link->cg_link_list);
426 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700427 if (atomic_dec_and_test(&cgrp->count) &&
428 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700429 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700430 set_bit(CGRP_RELEASABLE, &cgrp->flags);
431 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700433
434 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700435 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700436
437 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800438 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700439}
440
441/*
442 * refcounted get/put for css_set objects
443 */
444static inline void get_css_set(struct css_set *cg)
445{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700446 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700447}
448
449static inline void put_css_set(struct css_set *cg)
450{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700451 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700452}
453
Paul Menage81a6a5c2007-10-18 23:39:38 -0700454static inline void put_css_set_taskexit(struct css_set *cg)
455{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700456 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700457}
458
Paul Menage817929e2007-10-18 23:39:36 -0700459/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700460 * compare_css_sets - helper function for find_existing_css_set().
461 * @cg: candidate css_set being tested
462 * @old_cg: existing css_set for a task
463 * @new_cgrp: cgroup that's being entered by the task
464 * @template: desired set of css pointers in css_set (pre-calculated)
465 *
466 * Returns true if "cg" matches "old_cg" except for the hierarchy
467 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
468 */
469static bool compare_css_sets(struct css_set *cg,
470 struct css_set *old_cg,
471 struct cgroup *new_cgrp,
472 struct cgroup_subsys_state *template[])
473{
474 struct list_head *l1, *l2;
475
476 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
477 /* Not all subsystems matched */
478 return false;
479 }
480
481 /*
482 * Compare cgroup pointers in order to distinguish between
483 * different cgroups in heirarchies with no subsystems. We
484 * could get by with just this check alone (and skip the
485 * memcmp above) but on most setups the memcmp check will
486 * avoid the need for this more expensive check on almost all
487 * candidates.
488 */
489
490 l1 = &cg->cg_links;
491 l2 = &old_cg->cg_links;
492 while (1) {
493 struct cg_cgroup_link *cgl1, *cgl2;
494 struct cgroup *cg1, *cg2;
495
496 l1 = l1->next;
497 l2 = l2->next;
498 /* See if we reached the end - both lists are equal length. */
499 if (l1 == &cg->cg_links) {
500 BUG_ON(l2 != &old_cg->cg_links);
501 break;
502 } else {
503 BUG_ON(l2 == &old_cg->cg_links);
504 }
505 /* Locate the cgroups associated with these links. */
506 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
507 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
508 cg1 = cgl1->cgrp;
509 cg2 = cgl2->cgrp;
510 /* Hierarchies should be linked in the same order. */
511 BUG_ON(cg1->root != cg2->root);
512
513 /*
514 * If this hierarchy is the hierarchy of the cgroup
515 * that's changing, then we need to check that this
516 * css_set points to the new cgroup; if it's any other
517 * hierarchy, then this css_set should point to the
518 * same cgroup as the old css_set.
519 */
520 if (cg1->root == new_cgrp->root) {
521 if (cg1 != new_cgrp)
522 return false;
523 } else {
524 if (cg1 != cg2)
525 return false;
526 }
527 }
528 return true;
529}
530
531/*
Paul Menage817929e2007-10-18 23:39:36 -0700532 * find_existing_css_set() is a helper for
533 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700534 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700535 *
536 * oldcg: the cgroup group that we're using before the cgroup
537 * transition
538 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700539 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700540 *
541 * template: location in which to build the desired set of subsystem
542 * state objects for the new cgroup group
543 */
Paul Menage817929e2007-10-18 23:39:36 -0700544static struct css_set *find_existing_css_set(
545 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700546 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700547 struct cgroup_subsys_state *template[])
548{
549 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700550 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700551 struct hlist_head *hhead;
552 struct hlist_node *node;
553 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700554
Ben Blumaae8aab2010-03-10 15:22:07 -0800555 /*
556 * Build the set of subsystem state objects that we want to see in the
557 * new css_set. while subsystems can change globally, the entries here
558 * won't change, so no need for locking.
559 */
Paul Menage817929e2007-10-18 23:39:36 -0700560 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400561 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700562 /* Subsystem is in this hierarchy. So we want
563 * the subsystem state from the new
564 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700565 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700566 } else {
567 /* Subsystem is not in this hierarchy, so we
568 * don't want to change the subsystem state */
569 template[i] = oldcg->subsys[i];
570 }
571 }
572
Li Zefan472b1052008-04-29 01:00:11 -0700573 hhead = css_set_hash(template);
574 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700575 if (!compare_css_sets(cg, oldcg, cgrp, template))
576 continue;
577
578 /* This css_set matches what we need */
579 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700580 }
Paul Menage817929e2007-10-18 23:39:36 -0700581
582 /* No existing cgroup group matched */
583 return NULL;
584}
585
Paul Menage817929e2007-10-18 23:39:36 -0700586static void free_cg_links(struct list_head *tmp)
587{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700588 struct cg_cgroup_link *link;
589 struct cg_cgroup_link *saved_link;
590
591 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700592 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700593 kfree(link);
594 }
595}
596
597/*
Li Zefan36553432008-07-29 22:33:19 -0700598 * allocate_cg_links() allocates "count" cg_cgroup_link structures
599 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
600 * success or a negative error
601 */
602static int allocate_cg_links(int count, struct list_head *tmp)
603{
604 struct cg_cgroup_link *link;
605 int i;
606 INIT_LIST_HEAD(tmp);
607 for (i = 0; i < count; i++) {
608 link = kmalloc(sizeof(*link), GFP_KERNEL);
609 if (!link) {
610 free_cg_links(tmp);
611 return -ENOMEM;
612 }
613 list_add(&link->cgrp_link_list, tmp);
614 }
615 return 0;
616}
617
Li Zefanc12f65d2009-01-07 18:07:42 -0800618/**
619 * link_css_set - a helper function to link a css_set to a cgroup
620 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
621 * @cg: the css_set to be linked
622 * @cgrp: the destination cgroup
623 */
624static void link_css_set(struct list_head *tmp_cg_links,
625 struct css_set *cg, struct cgroup *cgrp)
626{
627 struct cg_cgroup_link *link;
628
629 BUG_ON(list_empty(tmp_cg_links));
630 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
631 cgrp_link_list);
632 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700633 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700634 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800635 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700636 /*
637 * Always add links to the tail of the list so that the list
638 * is sorted by order of hierarchy creation
639 */
640 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800641}
642
Li Zefan36553432008-07-29 22:33:19 -0700643/*
Paul Menage817929e2007-10-18 23:39:36 -0700644 * find_css_set() takes an existing cgroup group and a
645 * cgroup object, and returns a css_set object that's
646 * equivalent to the old group, but with the given cgroup
647 * substituted into the appropriate hierarchy. Must be called with
648 * cgroup_mutex held
649 */
Paul Menage817929e2007-10-18 23:39:36 -0700650static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700651 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700652{
653 struct css_set *res;
654 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700655
656 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700657
Li Zefan472b1052008-04-29 01:00:11 -0700658 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700659 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700660
Paul Menage817929e2007-10-18 23:39:36 -0700661 /* First see if we already have a cgroup group that matches
662 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700663 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700664 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700665 if (res)
666 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700667 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700668
669 if (res)
670 return res;
671
672 res = kmalloc(sizeof(*res), GFP_KERNEL);
673 if (!res)
674 return NULL;
675
676 /* Allocate all the cg_cgroup_link objects that we'll need */
677 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
678 kfree(res);
679 return NULL;
680 }
681
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700682 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700683 INIT_LIST_HEAD(&res->cg_links);
684 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700685 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700686
687 /* Copy the set of subsystem state objects generated in
688 * find_existing_css_set() */
689 memcpy(res->subsys, template, sizeof(res->subsys));
690
691 write_lock(&css_set_lock);
692 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700693 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
694 struct cgroup *c = link->cgrp;
695 if (c->root == cgrp->root)
696 c = cgrp;
697 link_css_set(&tmp_cg_links, res, c);
698 }
Paul Menage817929e2007-10-18 23:39:36 -0700699
700 BUG_ON(!list_empty(&tmp_cg_links));
701
Paul Menage817929e2007-10-18 23:39:36 -0700702 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700703
704 /* Add this cgroup group to the hash table */
705 hhead = css_set_hash(res->subsys);
706 hlist_add_head(&res->hlist, hhead);
707
Paul Menage817929e2007-10-18 23:39:36 -0700708 write_unlock(&css_set_lock);
709
710 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700711}
712
Paul Menageddbcc7e2007-10-18 23:39:30 -0700713/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700714 * Return the cgroup for "task" from the given hierarchy. Must be
715 * called with cgroup_mutex held.
716 */
717static struct cgroup *task_cgroup_from_root(struct task_struct *task,
718 struct cgroupfs_root *root)
719{
720 struct css_set *css;
721 struct cgroup *res = NULL;
722
723 BUG_ON(!mutex_is_locked(&cgroup_mutex));
724 read_lock(&css_set_lock);
725 /*
726 * No need to lock the task - since we hold cgroup_mutex the
727 * task can't change groups, so the only thing that can happen
728 * is that it exits and its css is set back to init_css_set.
729 */
730 css = task->cgroups;
731 if (css == &init_css_set) {
732 res = &root->top_cgroup;
733 } else {
734 struct cg_cgroup_link *link;
735 list_for_each_entry(link, &css->cg_links, cg_link_list) {
736 struct cgroup *c = link->cgrp;
737 if (c->root == root) {
738 res = c;
739 break;
740 }
741 }
742 }
743 read_unlock(&css_set_lock);
744 BUG_ON(!res);
745 return res;
746}
747
748/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700749 * There is one global cgroup mutex. We also require taking
750 * task_lock() when dereferencing a task's cgroup subsys pointers.
751 * See "The task_lock() exception", at the end of this comment.
752 *
753 * A task must hold cgroup_mutex to modify cgroups.
754 *
755 * Any task can increment and decrement the count field without lock.
756 * So in general, code holding cgroup_mutex can't rely on the count
757 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800758 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700759 * means that no tasks are currently attached, therefore there is no
760 * way a task attached to that cgroup can fork (the other way to
761 * increment the count). So code holding cgroup_mutex can safely
762 * assume that if the count is zero, it will stay zero. Similarly, if
763 * a task holds cgroup_mutex on a cgroup with zero count, it
764 * knows that the cgroup won't be removed, as cgroup_rmdir()
765 * needs that mutex.
766 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700767 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
768 * (usually) take cgroup_mutex. These are the two most performance
769 * critical pieces of code here. The exception occurs on cgroup_exit(),
770 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
771 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800772 * to the release agent with the name of the cgroup (path relative to
773 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700774 *
775 * A cgroup can only be deleted if both its 'count' of using tasks
776 * is zero, and its list of 'children' cgroups is empty. Since all
777 * tasks in the system use _some_ cgroup, and since there is always at
778 * least one task in the system (init, pid == 1), therefore, top_cgroup
779 * always has either children cgroups and/or using tasks. So we don't
780 * need a special hack to ensure that top_cgroup cannot be deleted.
781 *
782 * The task_lock() exception
783 *
784 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800785 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800786 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700787 * several performance critical places that need to reference
788 * task->cgroup without the expense of grabbing a system global
789 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800790 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
792 * the task_struct routinely used for such matters.
793 *
794 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800795 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796 */
797
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798/**
799 * cgroup_lock - lock out any changes to cgroup structures
800 *
801 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700802void cgroup_lock(void)
803{
804 mutex_lock(&cgroup_mutex);
805}
Ben Blum67523c42010-03-10 15:22:11 -0800806EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700807
808/**
809 * cgroup_unlock - release lock on cgroup changes
810 *
811 * Undo the lock taken in a previous cgroup_lock() call.
812 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700813void cgroup_unlock(void)
814{
815 mutex_unlock(&cgroup_mutex);
816}
Ben Blum67523c42010-03-10 15:22:11 -0800817EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700818
819/*
820 * A couple of forward declarations required, due to cyclic reference loop:
821 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
822 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
823 * -> cgroup_mkdir.
824 */
825
Al Viro18bb1db2011-07-26 01:41:39 -0400826static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400827static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700828static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400829static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
830 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700831static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700832static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700833
834static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200835 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700836 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700837};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700838
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700839static int alloc_css_id(struct cgroup_subsys *ss,
840 struct cgroup *parent, struct cgroup *child);
841
Al Viroa5e7ed32011-07-26 01:55:55 -0400842static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700843{
844 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700845
846 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400847 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700848 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100849 inode->i_uid = current_fsuid();
850 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700851 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
852 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
853 }
854 return inode;
855}
856
857static void cgroup_diput(struct dentry *dentry, struct inode *inode)
858{
859 /* is dentry a directory ? if so, kfree() associated cgroup */
860 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700861 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800862 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700863 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700864 /* It's possible for external users to be holding css
865 * reference counts on a cgroup; css_put() needs to
866 * be able to access the cgroup after decrementing
867 * the reference count in order to know if it needs to
868 * queue the cgroup to be handled by the release
869 * agent */
870 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800871
872 mutex_lock(&cgroup_mutex);
873 /*
874 * Release the subsystem state objects.
875 */
Li Zefan75139b82009-01-07 18:07:33 -0800876 for_each_subsys(cgrp->root, ss)
Li Zefan761b3ef2012-01-31 13:47:36 +0800877 ss->destroy(cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800878
879 cgrp->root->number_of_cgroups--;
880 mutex_unlock(&cgroup_mutex);
881
Paul Menagea47295e2009-01-07 18:07:44 -0800882 /*
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700883 * Drop the active superblock reference that we took when we
884 * created the cgroup
Paul Menagea47295e2009-01-07 18:07:44 -0800885 */
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700886 deactivate_super(cgrp->root->sb);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800887
Ben Blum72a8cb32009-09-23 15:56:27 -0700888 /*
889 * if we're getting rid of the cgroup, refcount should ensure
890 * that there are no pidlists left.
891 */
892 BUG_ON(!list_empty(&cgrp->pidlists));
893
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400894 simple_xattrs_free(&cgrp->xattrs);
895
Lai Jiangshanf2da1c42011-03-15 17:55:16 +0800896 kfree_rcu(cgrp, rcu_head);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700897 } else {
898 struct cfent *cfe = __d_cfe(dentry);
899 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400900 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700901
902 WARN_ONCE(!list_empty(&cfe->node) &&
903 cgrp != &cgrp->root->top_cgroup,
904 "cfe still linked for %s\n", cfe->type->name);
905 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400906 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700907 }
908 iput(inode);
909}
910
Al Viroc72a04e2011-01-14 05:31:45 +0000911static int cgroup_delete(const struct dentry *d)
912{
913 return 1;
914}
915
Paul Menageddbcc7e2007-10-18 23:39:30 -0700916static void remove_dir(struct dentry *d)
917{
918 struct dentry *parent = dget(d->d_parent);
919
920 d_delete(d);
921 simple_rmdir(parent->d_inode, d);
922 dput(parent);
923}
924
Tejun Heo05ef1d72012-04-01 12:09:56 -0700925static int cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700926{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700927 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700928
Tejun Heo05ef1d72012-04-01 12:09:56 -0700929 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
930 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100931
Tejun Heo05ef1d72012-04-01 12:09:56 -0700932 list_for_each_entry(cfe, &cgrp->files, node) {
933 struct dentry *d = cfe->dentry;
934
935 if (cft && cfe->type != cft)
936 continue;
937
938 dget(d);
939 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700940 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700941 list_del_init(&cfe->node);
942 dput(d);
943
944 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700945 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700946 return -ENOENT;
947}
948
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400949/**
950 * cgroup_clear_directory - selective removal of base and subsystem files
951 * @dir: directory containing the files
952 * @base_files: true if the base files should be removed
953 * @subsys_mask: mask of the subsystem ids whose files should be removed
954 */
955static void cgroup_clear_directory(struct dentry *dir, bool base_files,
956 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700957{
958 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400959 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700960
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400961 for_each_subsys(cgrp->root, ss) {
962 struct cftype_set *set;
963 if (!test_bit(ss->subsys_id, &subsys_mask))
964 continue;
965 list_for_each_entry(set, &ss->cftsets, node)
966 cgroup_rm_file(cgrp, set->cfts);
967 }
968 if (base_files) {
969 while (!list_empty(&cgrp->files))
970 cgroup_rm_file(cgrp, NULL);
971 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972}
973
974/*
975 * NOTE : the dentry must have been dget()'ed
976 */
977static void cgroup_d_remove_dir(struct dentry *dentry)
978{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100979 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400980 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100981
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400982 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100984 parent = dentry->d_parent;
985 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800986 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100988 spin_unlock(&dentry->d_lock);
989 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990 remove_dir(dentry);
991}
992
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700993/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800994 * Call with cgroup_mutex held. Drops reference counts on modules, including
995 * any duplicate ones that parse_cgroupfs_options took. If this function
996 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800997 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400999 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000{
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001001 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001002 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 int i;
1004
Ben Blumaae8aab2010-03-10 15:22:07 -08001005 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001006 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001007
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001008 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1009 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010 /* Check that any added subsystems are currently free */
1011 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001012 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001014 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001016 /*
1017 * Nobody should tell us to do a subsys that doesn't exist:
1018 * parse_cgroupfs_options should catch that case and refcounts
1019 * ensure that subsystems won't disappear once selected.
1020 */
1021 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001022 if (ss->root != &rootnode) {
1023 /* Subsystem isn't free */
1024 return -EBUSY;
1025 }
1026 }
1027
1028 /* Currently we don't handle adding/removing subsystems when
1029 * any child cgroups exist. This is theoretically supportable
1030 * but involves complex error handling, so it's being left until
1031 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001032 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 return -EBUSY;
1034
1035 /* Process each subsystem */
1036 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1037 struct cgroup_subsys *ss = subsys[i];
1038 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001039 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001040 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001041 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001042 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 BUG_ON(!dummytop->subsys[i]);
1044 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001045 cgrp->subsys[i] = dummytop->subsys[i];
1046 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001047 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001048 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001050 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001051 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001052 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001054 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001055 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1056 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001058 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001060 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001061 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001062 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001063 /* subsystem is now free - drop reference on module */
1064 module_put(ss->module);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001065 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001067 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001068 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001069 /*
1070 * a refcount was taken, but we already had one, so
1071 * drop the extra reference.
1072 */
1073 module_put(ss->module);
1074#ifdef CONFIG_MODULE_UNLOAD
1075 BUG_ON(ss->module && !module_refcount(ss->module));
1076#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077 } else {
1078 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001079 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080 }
1081 }
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001082 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 synchronize_rcu();
1084
1085 return 0;
1086}
1087
Al Viro34c80b12011-12-08 21:32:45 -05001088static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089{
Al Viro34c80b12011-12-08 21:32:45 -05001090 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091 struct cgroup_subsys *ss;
1092
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001093 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 for_each_subsys(root, ss)
1095 seq_printf(seq, ",%s", ss->name);
1096 if (test_bit(ROOT_NOPREFIX, &root->flags))
1097 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001098 if (test_bit(ROOT_XATTR, &root->flags))
1099 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001100 if (strlen(root->release_agent_path))
1101 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001102 if (clone_children(&root->top_cgroup))
1103 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001104 if (strlen(root->name))
1105 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001106 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 return 0;
1108}
1109
1110struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001111 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001112 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001113 char *release_agent;
Daniel Lezcano97978e62010-10-27 15:33:35 -07001114 bool clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001115 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001116 /* User explicitly requested empty subsystem */
1117 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001118
1119 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001120
Paul Menageddbcc7e2007-10-18 23:39:30 -07001121};
1122
Ben Blumaae8aab2010-03-10 15:22:07 -08001123/*
1124 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001125 * with cgroup_mutex held to protect the subsys[] array. This function takes
1126 * refcounts on subsystems to be used, unless it returns error, in which case
1127 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001128 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001129static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001130{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001131 char *token, *o = data;
1132 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001133 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001134 int i;
1135 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001136
Ben Blumaae8aab2010-03-10 15:22:07 -08001137 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1138
Li Zefanf9ab5b52009-06-17 16:26:33 -07001139#ifdef CONFIG_CPUSETS
1140 mask = ~(1UL << cpuset_subsys_id);
1141#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001142
Paul Menagec6d57f32009-09-23 15:56:19 -07001143 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001144
1145 while ((token = strsep(&o, ",")) != NULL) {
1146 if (!*token)
1147 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001148 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001149 /* Explicitly have no subsystems */
1150 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001151 continue;
1152 }
1153 if (!strcmp(token, "all")) {
1154 /* Mutually exclusive option 'all' + subsystem name */
1155 if (one_ss)
1156 return -EINVAL;
1157 all_ss = true;
1158 continue;
1159 }
1160 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001161 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001162 continue;
1163 }
1164 if (!strcmp(token, "clone_children")) {
Daniel Lezcano97978e62010-10-27 15:33:35 -07001165 opts->clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001166 continue;
1167 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001168 if (!strcmp(token, "xattr")) {
1169 set_bit(ROOT_XATTR, &opts->flags);
1170 continue;
1171 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001172 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001173 /* Specifying two release agents is forbidden */
1174 if (opts->release_agent)
1175 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001176 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001177 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001178 if (!opts->release_agent)
1179 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001180 continue;
1181 }
1182 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001183 const char *name = token + 5;
1184 /* Can't specify an empty name */
1185 if (!strlen(name))
1186 return -EINVAL;
1187 /* Must match [\w.-]+ */
1188 for (i = 0; i < strlen(name); i++) {
1189 char c = name[i];
1190 if (isalnum(c))
1191 continue;
1192 if ((c == '.') || (c == '-') || (c == '_'))
1193 continue;
1194 return -EINVAL;
1195 }
1196 /* Specifying two names is forbidden */
1197 if (opts->name)
1198 return -EINVAL;
1199 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001200 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001201 GFP_KERNEL);
1202 if (!opts->name)
1203 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001204
1205 continue;
1206 }
1207
1208 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1209 struct cgroup_subsys *ss = subsys[i];
1210 if (ss == NULL)
1211 continue;
1212 if (strcmp(token, ss->name))
1213 continue;
1214 if (ss->disabled)
1215 continue;
1216
1217 /* Mutually exclusive option 'all' + subsystem name */
1218 if (all_ss)
1219 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001220 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001221 one_ss = true;
1222
1223 break;
1224 }
1225 if (i == CGROUP_SUBSYS_COUNT)
1226 return -ENOENT;
1227 }
1228
1229 /*
1230 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001231 * otherwise if 'none', 'name=' and a subsystem name options
1232 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001233 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001234 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001235 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1236 struct cgroup_subsys *ss = subsys[i];
1237 if (ss == NULL)
1238 continue;
1239 if (ss->disabled)
1240 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001241 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001242 }
1243 }
1244
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001245 /* Consistency checks */
1246
Li Zefanf9ab5b52009-06-17 16:26:33 -07001247 /*
1248 * Option noprefix was introduced just for backward compatibility
1249 * with the old cpuset, so we allow noprefix only if mounting just
1250 * the cpuset subsystem.
1251 */
1252 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001253 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001254 return -EINVAL;
1255
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001256
1257 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001258 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001259 return -EINVAL;
1260
1261 /*
1262 * We either have to specify by name or by subsystems. (So all
1263 * empty hierarchies must have a name).
1264 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001265 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001266 return -EINVAL;
1267
Ben Blumcf5d5942010-03-10 15:22:09 -08001268 /*
1269 * Grab references on all the modules we'll need, so the subsystems
1270 * don't dance around before rebind_subsystems attaches them. This may
1271 * take duplicate reference counts on a subsystem that's already used,
1272 * but rebind_subsystems handles this case.
1273 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001274 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001275 unsigned long bit = 1UL << i;
1276
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001277 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001278 continue;
1279 if (!try_module_get(subsys[i]->module)) {
1280 module_pin_failed = true;
1281 break;
1282 }
1283 }
1284 if (module_pin_failed) {
1285 /*
1286 * oops, one of the modules was going away. this means that we
1287 * raced with a module_delete call, and to the user this is
1288 * essentially a "subsystem doesn't exist" case.
1289 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001290 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001291 /* drop refcounts only on the ones we took */
1292 unsigned long bit = 1UL << i;
1293
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001294 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001295 continue;
1296 module_put(subsys[i]->module);
1297 }
1298 return -ENOENT;
1299 }
1300
Paul Menageddbcc7e2007-10-18 23:39:30 -07001301 return 0;
1302}
1303
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001304static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001305{
1306 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001307 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001308 unsigned long bit = 1UL << i;
1309
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001310 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001311 continue;
1312 module_put(subsys[i]->module);
1313 }
1314}
1315
Paul Menageddbcc7e2007-10-18 23:39:30 -07001316static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1317{
1318 int ret = 0;
1319 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001320 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001321 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001322 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323
Paul Menagebd89aab2007-10-18 23:40:44 -07001324 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001326 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001327
1328 /* See what subsystems are wanted */
1329 ret = parse_cgroupfs_options(data, &opts);
1330 if (ret)
1331 goto out_unlock;
1332
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001333 /* See feature-removal-schedule.txt */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001334 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001335 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1336 task_tgid_nr(current), current->comm);
1337
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001338 added_mask = opts.subsys_mask & ~root->subsys_mask;
1339 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001340
Ben Blumcf5d5942010-03-10 15:22:09 -08001341 /* Don't allow flags or name to change at remount */
1342 if (opts.flags != root->flags ||
1343 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001344 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001345 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001346 goto out_unlock;
1347 }
1348
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001349 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001350 if (ret) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001351 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001352 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001353 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001354
Tejun Heoff4c8d52012-04-01 12:09:54 -07001355 /* clear out any existing files and repopulate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001356 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001357 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001358 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001359
Paul Menage81a6a5c2007-10-18 23:39:38 -07001360 if (opts.release_agent)
1361 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001362 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001363 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001364 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001365 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001366 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001367 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368 return ret;
1369}
1370
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001371static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001372 .statfs = simple_statfs,
1373 .drop_inode = generic_delete_inode,
1374 .show_options = cgroup_show_options,
1375 .remount_fs = cgroup_remount,
1376};
1377
Paul Menagecc31edc2008-10-18 20:28:04 -07001378static void init_cgroup_housekeeping(struct cgroup *cgrp)
1379{
1380 INIT_LIST_HEAD(&cgrp->sibling);
1381 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001382 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001383 INIT_LIST_HEAD(&cgrp->css_sets);
1384 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001385 INIT_LIST_HEAD(&cgrp->pidlists);
1386 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001387 INIT_LIST_HEAD(&cgrp->event_list);
1388 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001389 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001390}
Paul Menagec6d57f32009-09-23 15:56:19 -07001391
Paul Menageddbcc7e2007-10-18 23:39:30 -07001392static void init_cgroup_root(struct cgroupfs_root *root)
1393{
Paul Menagebd89aab2007-10-18 23:40:44 -07001394 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001395
Paul Menageddbcc7e2007-10-18 23:39:30 -07001396 INIT_LIST_HEAD(&root->subsys_list);
1397 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001398 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001399 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001400 cgrp->root = root;
1401 cgrp->top_cgroup = cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001402 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menagecc31edc2008-10-18 20:28:04 -07001403 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001404}
1405
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001406static bool init_root_id(struct cgroupfs_root *root)
1407{
1408 int ret = 0;
1409
1410 do {
1411 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1412 return false;
1413 spin_lock(&hierarchy_id_lock);
1414 /* Try to allocate the next unused ID */
1415 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1416 &root->hierarchy_id);
1417 if (ret == -ENOSPC)
1418 /* Try again starting from 0 */
1419 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1420 if (!ret) {
1421 next_hierarchy_id = root->hierarchy_id + 1;
1422 } else if (ret != -EAGAIN) {
1423 /* Can only get here if the 31-bit IDR is full ... */
1424 BUG_ON(ret);
1425 }
1426 spin_unlock(&hierarchy_id_lock);
1427 } while (ret);
1428 return true;
1429}
1430
Paul Menageddbcc7e2007-10-18 23:39:30 -07001431static int cgroup_test_super(struct super_block *sb, void *data)
1432{
Paul Menagec6d57f32009-09-23 15:56:19 -07001433 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001434 struct cgroupfs_root *root = sb->s_fs_info;
1435
Paul Menagec6d57f32009-09-23 15:56:19 -07001436 /* If we asked for a name then it must match */
1437 if (opts->name && strcmp(opts->name, root->name))
1438 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001439
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001440 /*
1441 * If we asked for subsystems (or explicitly for no
1442 * subsystems) then they must match
1443 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001444 if ((opts->subsys_mask || opts->none)
1445 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001446 return 0;
1447
1448 return 1;
1449}
1450
Paul Menagec6d57f32009-09-23 15:56:19 -07001451static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1452{
1453 struct cgroupfs_root *root;
1454
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001455 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001456 return NULL;
1457
1458 root = kzalloc(sizeof(*root), GFP_KERNEL);
1459 if (!root)
1460 return ERR_PTR(-ENOMEM);
1461
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001462 if (!init_root_id(root)) {
1463 kfree(root);
1464 return ERR_PTR(-ENOMEM);
1465 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001466 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001467
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001468 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001469 root->flags = opts->flags;
1470 if (opts->release_agent)
1471 strcpy(root->release_agent_path, opts->release_agent);
1472 if (opts->name)
1473 strcpy(root->name, opts->name);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001474 if (opts->clone_children)
1475 set_bit(CGRP_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001476 return root;
1477}
1478
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001479static void cgroup_drop_root(struct cgroupfs_root *root)
1480{
1481 if (!root)
1482 return;
1483
1484 BUG_ON(!root->hierarchy_id);
1485 spin_lock(&hierarchy_id_lock);
1486 ida_remove(&hierarchy_ida, root->hierarchy_id);
1487 spin_unlock(&hierarchy_id_lock);
1488 kfree(root);
1489}
1490
Paul Menageddbcc7e2007-10-18 23:39:30 -07001491static int cgroup_set_super(struct super_block *sb, void *data)
1492{
1493 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001494 struct cgroup_sb_opts *opts = data;
1495
1496 /* If we don't have a new root, we can't set up a new sb */
1497 if (!opts->new_root)
1498 return -EINVAL;
1499
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001500 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001501
1502 ret = set_anon_super(sb, NULL);
1503 if (ret)
1504 return ret;
1505
Paul Menagec6d57f32009-09-23 15:56:19 -07001506 sb->s_fs_info = opts->new_root;
1507 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508
1509 sb->s_blocksize = PAGE_CACHE_SIZE;
1510 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1511 sb->s_magic = CGROUP_SUPER_MAGIC;
1512 sb->s_op = &cgroup_ops;
1513
1514 return 0;
1515}
1516
1517static int cgroup_get_rootdir(struct super_block *sb)
1518{
Al Viro0df6a632010-12-21 13:29:29 -05001519 static const struct dentry_operations cgroup_dops = {
1520 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001521 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001522 };
1523
Paul Menageddbcc7e2007-10-18 23:39:30 -07001524 struct inode *inode =
1525 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526
1527 if (!inode)
1528 return -ENOMEM;
1529
Paul Menageddbcc7e2007-10-18 23:39:30 -07001530 inode->i_fop = &simple_dir_operations;
1531 inode->i_op = &cgroup_dir_inode_operations;
1532 /* directories start off with i_nlink == 2 (for "." entry) */
1533 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001534 sb->s_root = d_make_root(inode);
1535 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001536 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001537 /* for everything else we want ->d_op set */
1538 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539 return 0;
1540}
1541
Al Virof7e83572010-07-26 13:23:11 +04001542static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001544 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001545{
1546 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001547 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548 int ret = 0;
1549 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001550 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001551 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552
1553 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001554 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001556 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001557 if (ret)
1558 goto out_err;
1559
1560 /*
1561 * Allocate a new cgroup root. We may not need it if we're
1562 * reusing an existing hierarchy.
1563 */
1564 new_root = cgroup_root_from_opts(&opts);
1565 if (IS_ERR(new_root)) {
1566 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001567 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001568 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001569 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001570
Paul Menagec6d57f32009-09-23 15:56:19 -07001571 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001572 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001574 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001575 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001576 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577 }
1578
Paul Menagec6d57f32009-09-23 15:56:19 -07001579 root = sb->s_fs_info;
1580 BUG_ON(!root);
1581 if (root == opts.new_root) {
1582 /* We used the new root structure, so this is a new hierarchy */
1583 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001584 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001585 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001586 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001587 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588
1589 BUG_ON(sb->s_root != NULL);
1590
1591 ret = cgroup_get_rootdir(sb);
1592 if (ret)
1593 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001594 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595
Paul Menage817929e2007-10-18 23:39:36 -07001596 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001597 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001598 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001599
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001600 /* Check for name clashes with existing mounts */
1601 ret = -EBUSY;
1602 if (strlen(root->name))
1603 for_each_active_root(existing_root)
1604 if (!strcmp(existing_root->name, root->name))
1605 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001606
Paul Menage817929e2007-10-18 23:39:36 -07001607 /*
1608 * We're accessing css_set_count without locking
1609 * css_set_lock here, but that's OK - it can only be
1610 * increased by someone holding cgroup_lock, and
1611 * that's us. The worst that can happen is that we
1612 * have some link structures left over
1613 */
1614 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001615 if (ret)
1616 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001617
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001618 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001619 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001620 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001621 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001622 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001623 /*
1624 * There must be no failure case after here, since rebinding
1625 * takes care of subsystems' refcounts, which are explicitly
1626 * dropped in the failure exit path.
1627 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628
1629 /* EBUSY should be the only error here */
1630 BUG_ON(ret);
1631
1632 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001633 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001634
Li Zefanc12f65d2009-01-07 18:07:42 -08001635 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636 root->top_cgroup.dentry = sb->s_root;
1637
Paul Menage817929e2007-10-18 23:39:36 -07001638 /* Link the top cgroup in this hierarchy into all
1639 * the css_set objects */
1640 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001641 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1642 struct hlist_head *hhead = &css_set_table[i];
1643 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001644 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001645
Li Zefanc12f65d2009-01-07 18:07:42 -08001646 hlist_for_each_entry(cg, node, hhead, hlist)
1647 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001648 }
Paul Menage817929e2007-10-18 23:39:36 -07001649 write_unlock(&css_set_lock);
1650
1651 free_cg_links(&tmp_cg_links);
1652
Li Zefanc12f65d2009-01-07 18:07:42 -08001653 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654 BUG_ON(root->number_of_cgroups != 1);
1655
eparis@redhat2ce97382011-06-02 21:20:51 +10001656 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001657 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001658 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001659 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001661 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001662 } else {
1663 /*
1664 * We re-used an existing hierarchy - the new root (if
1665 * any) is not needed
1666 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001667 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001668 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001669 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001670 }
1671
Paul Menagec6d57f32009-09-23 15:56:19 -07001672 kfree(opts.release_agent);
1673 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001674 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001675
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001676 unlock_drop:
1677 mutex_unlock(&cgroup_root_mutex);
1678 mutex_unlock(&cgroup_mutex);
1679 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001680 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001681 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001682 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001683 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001684 out_err:
1685 kfree(opts.release_agent);
1686 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001687 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001688}
1689
1690static void cgroup_kill_sb(struct super_block *sb) {
1691 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001692 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001694 struct cg_cgroup_link *link;
1695 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696
1697 BUG_ON(!root);
1698
1699 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001700 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701
1702 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001703 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001704
1705 /* Rebind all subsystems back to the default hierarchy */
1706 ret = rebind_subsystems(root, 0);
1707 /* Shouldn't be able to fail ... */
1708 BUG_ON(ret);
1709
Paul Menage817929e2007-10-18 23:39:36 -07001710 /*
1711 * Release all the links from css_sets to this hierarchy's
1712 * root cgroup
1713 */
1714 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001715
1716 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1717 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001718 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001719 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001720 kfree(link);
1721 }
1722 write_unlock(&css_set_lock);
1723
Paul Menage839ec542009-01-29 14:25:22 -08001724 if (!list_empty(&root->root_list)) {
1725 list_del(&root->root_list);
1726 root_count--;
1727 }
Li Zefane5f6a862009-01-07 18:07:41 -08001728
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001729 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730 mutex_unlock(&cgroup_mutex);
1731
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001732 simple_xattrs_free(&cgrp->xattrs);
1733
Paul Menageddbcc7e2007-10-18 23:39:30 -07001734 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001735 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736}
1737
1738static struct file_system_type cgroup_fs_type = {
1739 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001740 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001741 .kill_sb = cgroup_kill_sb,
1742};
1743
Greg KH676db4a2010-08-05 13:53:35 -07001744static struct kobject *cgroup_kobj;
1745
Li Zefana043e3b2008-02-23 15:24:09 -08001746/**
1747 * cgroup_path - generate the path of a cgroup
1748 * @cgrp: the cgroup in question
1749 * @buf: the buffer to write the path into
1750 * @buflen: the length of the buffer
1751 *
Paul Menagea47295e2009-01-07 18:07:44 -08001752 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1753 * reference. Writes path of cgroup into buf. Returns 0 on success,
1754 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001755 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001756int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757{
1758 char *start;
Li Zefan9a9686b2010-04-22 17:29:24 +08001759 struct dentry *dentry = rcu_dereference_check(cgrp->dentry,
Li Zefan9a9686b2010-04-22 17:29:24 +08001760 cgroup_lock_is_held());
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761
Paul Menagea47295e2009-01-07 18:07:44 -08001762 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001763 /*
1764 * Inactive subsystems have no dentry for their root
1765 * cgroup
1766 */
1767 strcpy(buf, "/");
1768 return 0;
1769 }
1770
Tao Ma316eb662012-11-08 21:36:38 +08001771 start = buf + buflen - 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772
Tao Ma316eb662012-11-08 21:36:38 +08001773 *start = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001775 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001776
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777 if ((start -= len) < buf)
1778 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001779 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001780 cgrp = cgrp->parent;
1781 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001783
1784 dentry = rcu_dereference_check(cgrp->dentry,
Li Zefan9a9686b2010-04-22 17:29:24 +08001785 cgroup_lock_is_held());
Paul Menagebd89aab2007-10-18 23:40:44 -07001786 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787 continue;
1788 if (--start < buf)
1789 return -ENAMETOOLONG;
1790 *start = '/';
1791 }
1792 memmove(buf, start, buf + buflen - start);
1793 return 0;
1794}
Ben Blum67523c42010-03-10 15:22:11 -08001795EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001796
Ben Blum74a11662011-05-26 16:25:20 -07001797/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001798 * Control Group taskset
1799 */
Tejun Heo134d3372011-12-12 18:12:21 -08001800struct task_and_cgroup {
1801 struct task_struct *task;
1802 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001803 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001804};
1805
Tejun Heo2f7ee562011-12-12 18:12:21 -08001806struct cgroup_taskset {
1807 struct task_and_cgroup single;
1808 struct flex_array *tc_array;
1809 int tc_array_len;
1810 int idx;
1811 struct cgroup *cur_cgrp;
1812};
1813
1814/**
1815 * cgroup_taskset_first - reset taskset and return the first task
1816 * @tset: taskset of interest
1817 *
1818 * @tset iteration is initialized and the first task is returned.
1819 */
1820struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1821{
1822 if (tset->tc_array) {
1823 tset->idx = 0;
1824 return cgroup_taskset_next(tset);
1825 } else {
1826 tset->cur_cgrp = tset->single.cgrp;
1827 return tset->single.task;
1828 }
1829}
1830EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1831
1832/**
1833 * cgroup_taskset_next - iterate to the next task in taskset
1834 * @tset: taskset of interest
1835 *
1836 * Return the next task in @tset. Iteration must have been initialized
1837 * with cgroup_taskset_first().
1838 */
1839struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1840{
1841 struct task_and_cgroup *tc;
1842
1843 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1844 return NULL;
1845
1846 tc = flex_array_get(tset->tc_array, tset->idx++);
1847 tset->cur_cgrp = tc->cgrp;
1848 return tc->task;
1849}
1850EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1851
1852/**
1853 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1854 * @tset: taskset of interest
1855 *
1856 * Return the cgroup for the current (last returned) task of @tset. This
1857 * function must be preceded by either cgroup_taskset_first() or
1858 * cgroup_taskset_next().
1859 */
1860struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1861{
1862 return tset->cur_cgrp;
1863}
1864EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1865
1866/**
1867 * cgroup_taskset_size - return the number of tasks in taskset
1868 * @tset: taskset of interest
1869 */
1870int cgroup_taskset_size(struct cgroup_taskset *tset)
1871{
1872 return tset->tc_array ? tset->tc_array_len : 1;
1873}
1874EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1875
1876
Ben Blum74a11662011-05-26 16:25:20 -07001877/*
1878 * cgroup_task_migrate - move a task from one cgroup to another.
1879 *
1880 * 'guarantee' is set if the caller promises that a new css_set for the task
1881 * will already exist. If not set, this function might sleep, and can fail with
Tejun Heocd3d0952011-12-12 18:12:21 -08001882 * -ENOMEM. Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001883 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001884static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1885 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001886{
1887 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001888
1889 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001890 * We are synchronized through threadgroup_lock() against PF_EXITING
1891 * setting such that we can't race against cgroup_exit() changing the
1892 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001893 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001894 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001895 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001896
Ben Blum74a11662011-05-26 16:25:20 -07001897 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001898 rcu_assign_pointer(tsk->cgroups, newcg);
1899 task_unlock(tsk);
1900
1901 /* Update the css_set linked lists if we're using them */
1902 write_lock(&css_set_lock);
1903 if (!list_empty(&tsk->cg_list))
1904 list_move(&tsk->cg_list, &newcg->tasks);
1905 write_unlock(&css_set_lock);
1906
1907 /*
1908 * We just gained a reference on oldcg by taking it from the task. As
1909 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1910 * it here; it will be freed under RCU.
1911 */
Ben Blum74a11662011-05-26 16:25:20 -07001912 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001913 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001914}
1915
Li Zefana043e3b2008-02-23 15:24:09 -08001916/**
1917 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1918 * @cgrp: the cgroup the task is attaching to
1919 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001920 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001921 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1922 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001923 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001924int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001925{
Tejun Heo8f121912012-03-29 22:03:33 -07001926 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001927 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001928 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001929 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001930 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001931 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001932
Tejun Heocd3d0952011-12-12 18:12:21 -08001933 /* @tsk either already exited or can't exit until the end */
1934 if (tsk->flags & PF_EXITING)
1935 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001936
1937 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001938 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001939 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001940 return 0;
1941
Tejun Heo2f7ee562011-12-12 18:12:21 -08001942 tset.single.task = tsk;
1943 tset.single.cgrp = oldcgrp;
1944
Paul Menagebbcb81d2007-10-18 23:39:32 -07001945 for_each_subsys(root, ss) {
1946 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08001947 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001948 if (retval) {
1949 /*
1950 * Remember on which subsystem the can_attach()
1951 * failed, so that we only call cancel_attach()
1952 * against the subsystems whose can_attach()
1953 * succeeded. (See below)
1954 */
1955 failed_ss = ss;
1956 goto out;
1957 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001958 }
1959 }
1960
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001961 newcg = find_css_set(tsk->cgroups, cgrp);
1962 if (!newcg) {
1963 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001964 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001965 }
1966
1967 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001968
Paul Menagebbcb81d2007-10-18 23:39:32 -07001969 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001970 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001971 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001972 }
Ben Blum74a11662011-05-26 16:25:20 -07001973
Paul Menagebbcb81d2007-10-18 23:39:32 -07001974 synchronize_rcu();
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001975out:
1976 if (retval) {
1977 for_each_subsys(root, ss) {
1978 if (ss == failed_ss)
1979 /*
1980 * This subsystem was the one that failed the
1981 * can_attach() check earlier, so we don't need
1982 * to call cancel_attach() against it or any
1983 * remaining subsystems.
1984 */
1985 break;
1986 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001987 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001988 }
1989 }
1990 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001991}
1992
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001993/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001994 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1995 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001996 * @tsk: the task to be attached
1997 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001998int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001999{
2000 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002001 int retval = 0;
2002
2003 cgroup_lock();
2004 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002005 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2006
2007 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002008 if (retval)
2009 break;
2010 }
2011 cgroup_unlock();
2012
2013 return retval;
2014}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002015EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002016
Ben Blum74a11662011-05-26 16:25:20 -07002017/**
2018 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2019 * @cgrp: the cgroup to attach to
2020 * @leader: the threadgroup leader task_struct of the group to be attached
2021 *
Tejun Heo257058a2011-12-12 18:12:21 -08002022 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2023 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002024 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002025static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002026{
2027 int retval, i, group_size;
2028 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002029 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002030 struct cgroupfs_root *root = cgrp->root;
2031 /* threadgroup list cursor and array */
2032 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002033 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002034 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002035 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002036
2037 /*
2038 * step 0: in order to do expensive, possibly blocking operations for
2039 * every thread, we cannot iterate the thread group list, since it needs
2040 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002041 * group - group_rwsem prevents new threads from appearing, and if
2042 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002043 */
2044 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002045 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002046 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002047 if (!group)
2048 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002049 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2050 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2051 if (retval)
2052 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002053
Ben Blum74a11662011-05-26 16:25:20 -07002054 tsk = leader;
2055 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002056 /*
2057 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2058 * already PF_EXITING could be freed from underneath us unless we
2059 * take an rcu_read_lock.
2060 */
2061 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002062 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002063 struct task_and_cgroup ent;
2064
Tejun Heocd3d0952011-12-12 18:12:21 -08002065 /* @tsk either already exited or can't exit until the end */
2066 if (tsk->flags & PF_EXITING)
2067 continue;
2068
Ben Blum74a11662011-05-26 16:25:20 -07002069 /* as per above, nr_threads may decrease, but not increase. */
2070 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002071 ent.task = tsk;
2072 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002073 /* nothing to do if this task is already in the cgroup */
2074 if (ent.cgrp == cgrp)
2075 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002076 /*
2077 * saying GFP_ATOMIC has no effect here because we did prealloc
2078 * earlier, but it's good form to communicate our expectations.
2079 */
Tejun Heo134d3372011-12-12 18:12:21 -08002080 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002081 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002082 i++;
2083 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002084 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002085 /* remember the number of threads in the array for later. */
2086 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002087 tset.tc_array = group;
2088 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002089
Tejun Heo134d3372011-12-12 18:12:21 -08002090 /* methods shouldn't be called if no task is actually migrating */
2091 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002092 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002093 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002094
Ben Blum74a11662011-05-26 16:25:20 -07002095 /*
2096 * step 1: check that we can legitimately attach to the cgroup.
2097 */
2098 for_each_subsys(root, ss) {
2099 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002100 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002101 if (retval) {
2102 failed_ss = ss;
2103 goto out_cancel_attach;
2104 }
2105 }
Ben Blum74a11662011-05-26 16:25:20 -07002106 }
2107
2108 /*
2109 * step 2: make sure css_sets exist for all threads to be migrated.
2110 * we use find_css_set, which allocates a new one if necessary.
2111 */
Ben Blum74a11662011-05-26 16:25:20 -07002112 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002113 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002114 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2115 if (!tc->cg) {
2116 retval = -ENOMEM;
2117 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002118 }
2119 }
2120
2121 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002122 * step 3: now that we're guaranteed success wrt the css_sets,
2123 * proceed to move all tasks to the new cgroup. There are no
2124 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002125 */
Ben Blum74a11662011-05-26 16:25:20 -07002126 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002127 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002128 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002129 }
2130 /* nothing is sensitive to fork() after this point. */
2131
2132 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002133 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002134 */
2135 for_each_subsys(root, ss) {
2136 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002137 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002138 }
2139
2140 /*
2141 * step 5: success! and cleanup
2142 */
2143 synchronize_rcu();
Ben Blum74a11662011-05-26 16:25:20 -07002144 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002145out_put_css_set_refs:
2146 if (retval) {
2147 for (i = 0; i < group_size; i++) {
2148 tc = flex_array_get(group, i);
2149 if (!tc->cg)
2150 break;
2151 put_css_set(tc->cg);
2152 }
Ben Blum74a11662011-05-26 16:25:20 -07002153 }
2154out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002155 if (retval) {
2156 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002157 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002158 break;
Ben Blum74a11662011-05-26 16:25:20 -07002159 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002160 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002161 }
2162 }
Ben Blum74a11662011-05-26 16:25:20 -07002163out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002164 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002165 return retval;
2166}
2167
2168/*
2169 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002170 * function to attach either it or all tasks in its threadgroup. Will lock
2171 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002172 */
2173static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002174{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002175 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002176 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002177 int ret;
2178
Ben Blum74a11662011-05-26 16:25:20 -07002179 if (!cgroup_lock_live_group(cgrp))
2180 return -ENODEV;
2181
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002182retry_find_task:
2183 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002184 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002185 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002186 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002187 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002188 ret= -ESRCH;
2189 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002190 }
Ben Blum74a11662011-05-26 16:25:20 -07002191 /*
2192 * even if we're attaching all tasks in the thread group, we
2193 * only need to check permissions on one of them.
2194 */
David Howellsc69e8d92008-11-14 10:39:19 +11002195 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002196 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2197 !uid_eq(cred->euid, tcred->uid) &&
2198 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002199 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002200 ret = -EACCES;
2201 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002202 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002203 } else
2204 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002205
2206 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002207 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002208
2209 /*
2210 * Workqueue threads may acquire PF_THREAD_BOUND and become
2211 * trapped in a cpuset, or RT worker may be born in a cgroup
2212 * with no rt_runtime allocated. Just say no.
2213 */
2214 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2215 ret = -EINVAL;
2216 rcu_read_unlock();
2217 goto out_unlock_cgroup;
2218 }
2219
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002220 get_task_struct(tsk);
2221 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002222
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002223 threadgroup_lock(tsk);
2224 if (threadgroup) {
2225 if (!thread_group_leader(tsk)) {
2226 /*
2227 * a race with de_thread from another thread's exec()
2228 * may strip us of our leadership, if this happens,
2229 * there is no choice but to throw this task away and
2230 * try again; this is
2231 * "double-double-toil-and-trouble-check locking".
2232 */
2233 threadgroup_unlock(tsk);
2234 put_task_struct(tsk);
2235 goto retry_find_task;
2236 }
2237 ret = cgroup_attach_proc(cgrp, tsk);
2238 } else
2239 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002240 threadgroup_unlock(tsk);
2241
Paul Menagebbcb81d2007-10-18 23:39:32 -07002242 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002243out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002244 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002245 return ret;
2246}
2247
Paul Menageaf351022008-07-25 01:47:01 -07002248static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2249{
Ben Blum74a11662011-05-26 16:25:20 -07002250 return attach_task_by_pid(cgrp, pid, false);
2251}
2252
2253static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2254{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002255 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002256}
2257
Paul Menagee788e062008-07-25 01:46:59 -07002258/**
2259 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2260 * @cgrp: the cgroup to be checked for liveness
2261 *
Paul Menage84eea842008-07-25 01:47:00 -07002262 * On success, returns true; the lock should be later released with
2263 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002264 */
Paul Menage84eea842008-07-25 01:47:00 -07002265bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002266{
2267 mutex_lock(&cgroup_mutex);
2268 if (cgroup_is_removed(cgrp)) {
2269 mutex_unlock(&cgroup_mutex);
2270 return false;
2271 }
2272 return true;
2273}
Ben Blum67523c42010-03-10 15:22:11 -08002274EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002275
2276static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2277 const char *buffer)
2278{
2279 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002280 if (strlen(buffer) >= PATH_MAX)
2281 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002282 if (!cgroup_lock_live_group(cgrp))
2283 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002284 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002285 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002286 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002287 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002288 return 0;
2289}
2290
2291static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2292 struct seq_file *seq)
2293{
2294 if (!cgroup_lock_live_group(cgrp))
2295 return -ENODEV;
2296 seq_puts(seq, cgrp->root->release_agent_path);
2297 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002298 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002299 return 0;
2300}
2301
Paul Menage84eea842008-07-25 01:47:00 -07002302/* A buffer size big enough for numbers or short strings */
2303#define CGROUP_LOCAL_BUFFER_SIZE 64
2304
Paul Menagee73d2c62008-04-29 01:00:06 -07002305static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002306 struct file *file,
2307 const char __user *userbuf,
2308 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002309{
Paul Menage84eea842008-07-25 01:47:00 -07002310 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002311 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002312 char *end;
2313
2314 if (!nbytes)
2315 return -EINVAL;
2316 if (nbytes >= sizeof(buffer))
2317 return -E2BIG;
2318 if (copy_from_user(buffer, userbuf, nbytes))
2319 return -EFAULT;
2320
2321 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002322 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002323 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002324 if (*end)
2325 return -EINVAL;
2326 retval = cft->write_u64(cgrp, cft, val);
2327 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002328 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002329 if (*end)
2330 return -EINVAL;
2331 retval = cft->write_s64(cgrp, cft, val);
2332 }
Paul Menage355e0c42007-10-18 23:39:33 -07002333 if (!retval)
2334 retval = nbytes;
2335 return retval;
2336}
2337
Paul Menagedb3b1492008-07-25 01:46:58 -07002338static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2339 struct file *file,
2340 const char __user *userbuf,
2341 size_t nbytes, loff_t *unused_ppos)
2342{
Paul Menage84eea842008-07-25 01:47:00 -07002343 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002344 int retval = 0;
2345 size_t max_bytes = cft->max_write_len;
2346 char *buffer = local_buffer;
2347
2348 if (!max_bytes)
2349 max_bytes = sizeof(local_buffer) - 1;
2350 if (nbytes >= max_bytes)
2351 return -E2BIG;
2352 /* Allocate a dynamic buffer if we need one */
2353 if (nbytes >= sizeof(local_buffer)) {
2354 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2355 if (buffer == NULL)
2356 return -ENOMEM;
2357 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002358 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2359 retval = -EFAULT;
2360 goto out;
2361 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002362
2363 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002364 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002365 if (!retval)
2366 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002367out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002368 if (buffer != local_buffer)
2369 kfree(buffer);
2370 return retval;
2371}
2372
Paul Menageddbcc7e2007-10-18 23:39:30 -07002373static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2374 size_t nbytes, loff_t *ppos)
2375{
2376 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002377 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002378
Li Zefan75139b82009-01-07 18:07:33 -08002379 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002380 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002381 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002382 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002383 if (cft->write_u64 || cft->write_s64)
2384 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002385 if (cft->write_string)
2386 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002387 if (cft->trigger) {
2388 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2389 return ret ? ret : nbytes;
2390 }
Paul Menage355e0c42007-10-18 23:39:33 -07002391 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392}
2393
Paul Menagef4c753b2008-04-29 00:59:56 -07002394static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2395 struct file *file,
2396 char __user *buf, size_t nbytes,
2397 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002398{
Paul Menage84eea842008-07-25 01:47:00 -07002399 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002400 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002401 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2402
2403 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2404}
2405
Paul Menagee73d2c62008-04-29 01:00:06 -07002406static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2407 struct file *file,
2408 char __user *buf, size_t nbytes,
2409 loff_t *ppos)
2410{
Paul Menage84eea842008-07-25 01:47:00 -07002411 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002412 s64 val = cft->read_s64(cgrp, cft);
2413 int len = sprintf(tmp, "%lld\n", (long long) val);
2414
2415 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2416}
2417
Paul Menageddbcc7e2007-10-18 23:39:30 -07002418static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2419 size_t nbytes, loff_t *ppos)
2420{
2421 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002422 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002423
Li Zefan75139b82009-01-07 18:07:33 -08002424 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002425 return -ENODEV;
2426
2427 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002428 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002429 if (cft->read_u64)
2430 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002431 if (cft->read_s64)
2432 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002433 return -EINVAL;
2434}
2435
Paul Menage91796562008-04-29 01:00:01 -07002436/*
2437 * seqfile ops/methods for returning structured data. Currently just
2438 * supports string->u64 maps, but can be extended in future.
2439 */
2440
2441struct cgroup_seqfile_state {
2442 struct cftype *cft;
2443 struct cgroup *cgroup;
2444};
2445
2446static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2447{
2448 struct seq_file *sf = cb->state;
2449 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2450}
2451
2452static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2453{
2454 struct cgroup_seqfile_state *state = m->private;
2455 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002456 if (cft->read_map) {
2457 struct cgroup_map_cb cb = {
2458 .fill = cgroup_map_add,
2459 .state = m,
2460 };
2461 return cft->read_map(state->cgroup, cft, &cb);
2462 }
2463 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002464}
2465
Adrian Bunk96930a62008-07-25 19:46:21 -07002466static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002467{
2468 struct seq_file *seq = file->private_data;
2469 kfree(seq->private);
2470 return single_release(inode, file);
2471}
2472
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002473static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002474 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002475 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002476 .llseek = seq_lseek,
2477 .release = cgroup_seqfile_release,
2478};
2479
Paul Menageddbcc7e2007-10-18 23:39:30 -07002480static int cgroup_file_open(struct inode *inode, struct file *file)
2481{
2482 int err;
2483 struct cftype *cft;
2484
2485 err = generic_file_open(inode, file);
2486 if (err)
2487 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002488 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002489
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002490 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002491 struct cgroup_seqfile_state *state =
2492 kzalloc(sizeof(*state), GFP_USER);
2493 if (!state)
2494 return -ENOMEM;
2495 state->cft = cft;
2496 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2497 file->f_op = &cgroup_seqfile_operations;
2498 err = single_open(file, cgroup_seqfile_show, state);
2499 if (err < 0)
2500 kfree(state);
2501 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002502 err = cft->open(inode, file);
2503 else
2504 err = 0;
2505
2506 return err;
2507}
2508
2509static int cgroup_file_release(struct inode *inode, struct file *file)
2510{
2511 struct cftype *cft = __d_cft(file->f_dentry);
2512 if (cft->release)
2513 return cft->release(inode, file);
2514 return 0;
2515}
2516
2517/*
2518 * cgroup_rename - Only allow simple rename of directories in place.
2519 */
2520static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2521 struct inode *new_dir, struct dentry *new_dentry)
2522{
2523 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2524 return -ENOTDIR;
2525 if (new_dentry->d_inode)
2526 return -EEXIST;
2527 if (old_dir != new_dir)
2528 return -EIO;
2529 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2530}
2531
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002532static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2533{
2534 if (S_ISDIR(dentry->d_inode->i_mode))
2535 return &__d_cgrp(dentry)->xattrs;
2536 else
2537 return &__d_cft(dentry)->xattrs;
2538}
2539
2540static inline int xattr_enabled(struct dentry *dentry)
2541{
2542 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2543 return test_bit(ROOT_XATTR, &root->flags);
2544}
2545
2546static bool is_valid_xattr(const char *name)
2547{
2548 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2549 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2550 return true;
2551 return false;
2552}
2553
2554static int cgroup_setxattr(struct dentry *dentry, const char *name,
2555 const void *val, size_t size, int flags)
2556{
2557 if (!xattr_enabled(dentry))
2558 return -EOPNOTSUPP;
2559 if (!is_valid_xattr(name))
2560 return -EINVAL;
2561 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2562}
2563
2564static int cgroup_removexattr(struct dentry *dentry, const char *name)
2565{
2566 if (!xattr_enabled(dentry))
2567 return -EOPNOTSUPP;
2568 if (!is_valid_xattr(name))
2569 return -EINVAL;
2570 return simple_xattr_remove(__d_xattrs(dentry), name);
2571}
2572
2573static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2574 void *buf, size_t size)
2575{
2576 if (!xattr_enabled(dentry))
2577 return -EOPNOTSUPP;
2578 if (!is_valid_xattr(name))
2579 return -EINVAL;
2580 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2581}
2582
2583static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2584{
2585 if (!xattr_enabled(dentry))
2586 return -EOPNOTSUPP;
2587 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2588}
2589
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002590static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002591 .read = cgroup_file_read,
2592 .write = cgroup_file_write,
2593 .llseek = generic_file_llseek,
2594 .open = cgroup_file_open,
2595 .release = cgroup_file_release,
2596};
2597
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002598static const struct inode_operations cgroup_file_inode_operations = {
2599 .setxattr = cgroup_setxattr,
2600 .getxattr = cgroup_getxattr,
2601 .listxattr = cgroup_listxattr,
2602 .removexattr = cgroup_removexattr,
2603};
2604
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002605static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002606 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002607 .mkdir = cgroup_mkdir,
2608 .rmdir = cgroup_rmdir,
2609 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002610 .setxattr = cgroup_setxattr,
2611 .getxattr = cgroup_getxattr,
2612 .listxattr = cgroup_listxattr,
2613 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002614};
2615
Al Viro00cd8dd2012-06-10 17:13:09 -04002616static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002617{
2618 if (dentry->d_name.len > NAME_MAX)
2619 return ERR_PTR(-ENAMETOOLONG);
2620 d_add(dentry, NULL);
2621 return NULL;
2622}
2623
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002624/*
2625 * Check if a file is a control file
2626 */
2627static inline struct cftype *__file_cft(struct file *file)
2628{
2629 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2630 return ERR_PTR(-EINVAL);
2631 return __d_cft(file->f_dentry);
2632}
2633
Al Viroa5e7ed32011-07-26 01:55:55 -04002634static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002635 struct super_block *sb)
2636{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002637 struct inode *inode;
2638
2639 if (!dentry)
2640 return -ENOENT;
2641 if (dentry->d_inode)
2642 return -EEXIST;
2643
2644 inode = cgroup_new_inode(mode, sb);
2645 if (!inode)
2646 return -ENOMEM;
2647
2648 if (S_ISDIR(mode)) {
2649 inode->i_op = &cgroup_dir_inode_operations;
2650 inode->i_fop = &simple_dir_operations;
2651
2652 /* start off with i_nlink == 2 (for "." entry) */
2653 inc_nlink(inode);
2654
2655 /* start with the directory inode held, so that we can
2656 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07002657 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002658 } else if (S_ISREG(mode)) {
2659 inode->i_size = 0;
2660 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002661 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002662 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002663 d_instantiate(dentry, inode);
2664 dget(dentry); /* Extra count - pin the dentry in core */
2665 return 0;
2666}
2667
2668/*
Li Zefana043e3b2008-02-23 15:24:09 -08002669 * cgroup_create_dir - create a directory for an object.
2670 * @cgrp: the cgroup we create the directory for. It must have a valid
2671 * ->parent field. And we are going to fill its ->dentry field.
2672 * @dentry: dentry of the new cgroup
2673 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002675static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04002676 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002677{
2678 struct dentry *parent;
2679 int error = 0;
2680
Paul Menagebd89aab2007-10-18 23:40:44 -07002681 parent = cgrp->parent->dentry;
2682 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002683 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002684 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002685 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08002686 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002687 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002688
2689 return error;
2690}
2691
Li Zefan099fca32009-04-02 16:57:29 -07002692/**
2693 * cgroup_file_mode - deduce file mode of a control file
2694 * @cft: the control file in question
2695 *
2696 * returns cft->mode if ->mode is not 0
2697 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2698 * returns S_IRUGO if it has only a read handler
2699 * returns S_IWUSR if it has only a write hander
2700 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002701static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002702{
Al Viroa5e7ed32011-07-26 01:55:55 -04002703 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002704
2705 if (cft->mode)
2706 return cft->mode;
2707
2708 if (cft->read || cft->read_u64 || cft->read_s64 ||
2709 cft->read_map || cft->read_seq_string)
2710 mode |= S_IRUGO;
2711
2712 if (cft->write || cft->write_u64 || cft->write_s64 ||
2713 cft->write_string || cft->trigger)
2714 mode |= S_IWUSR;
2715
2716 return mode;
2717}
2718
Tejun Heodb0416b2012-04-01 12:09:55 -07002719static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002720 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721{
Paul Menagebd89aab2007-10-18 23:40:44 -07002722 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002723 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002725 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002726 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002727 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002728 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002729
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002730 simple_xattrs_init(&cft->xattrs);
2731
Tejun Heo8e3f6542012-04-01 12:09:55 -07002732 /* does @cft->flags tell us to skip creation on @cgrp? */
2733 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2734 return 0;
2735 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2736 return 0;
2737
Paul Menagebd89aab2007-10-18 23:40:44 -07002738 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002739 strcpy(name, subsys->name);
2740 strcat(name, ".");
2741 }
2742 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002743
Paul Menageddbcc7e2007-10-18 23:39:30 -07002744 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002745
2746 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2747 if (!cfe)
2748 return -ENOMEM;
2749
Paul Menageddbcc7e2007-10-18 23:39:30 -07002750 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002751 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002752 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002753 goto out;
2754 }
2755
2756 mode = cgroup_file_mode(cft);
2757 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2758 if (!error) {
2759 cfe->type = (void *)cft;
2760 cfe->dentry = dentry;
2761 dentry->d_fsdata = cfe;
2762 list_add_tail(&cfe->node, &parent->files);
2763 cfe = NULL;
2764 }
2765 dput(dentry);
2766out:
2767 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002768 return error;
2769}
2770
Tejun Heo79578622012-04-01 12:09:56 -07002771static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002772 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002773{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002774 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002775 int err, ret = 0;
2776
2777 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Tejun Heo79578622012-04-01 12:09:56 -07002778 if (is_add)
2779 err = cgroup_add_file(cgrp, subsys, cft);
2780 else
2781 err = cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002782 if (err) {
Tejun Heo79578622012-04-01 12:09:56 -07002783 pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n",
2784 is_add ? "add" : "remove", cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002785 ret = err;
2786 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002787 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002788 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002789}
2790
Tejun Heo8e3f6542012-04-01 12:09:55 -07002791static DEFINE_MUTEX(cgroup_cft_mutex);
2792
2793static void cgroup_cfts_prepare(void)
2794 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2795{
2796 /*
2797 * Thanks to the entanglement with vfs inode locking, we can't walk
2798 * the existing cgroups under cgroup_mutex and create files.
2799 * Instead, we increment reference on all cgroups and build list of
2800 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2801 * exclusive access to the field.
2802 */
2803 mutex_lock(&cgroup_cft_mutex);
2804 mutex_lock(&cgroup_mutex);
2805}
2806
2807static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002808 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002809 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2810{
2811 LIST_HEAD(pending);
2812 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002813
2814 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2815 if (cfts && ss->root != &rootnode) {
2816 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2817 dget(cgrp->dentry);
2818 list_add_tail(&cgrp->cft_q_node, &pending);
2819 }
2820 }
2821
2822 mutex_unlock(&cgroup_mutex);
2823
2824 /*
2825 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2826 * files for all cgroups which were created before.
2827 */
2828 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2829 struct inode *inode = cgrp->dentry->d_inode;
2830
2831 mutex_lock(&inode->i_mutex);
2832 mutex_lock(&cgroup_mutex);
2833 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002834 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002835 mutex_unlock(&cgroup_mutex);
2836 mutex_unlock(&inode->i_mutex);
2837
2838 list_del_init(&cgrp->cft_q_node);
2839 dput(cgrp->dentry);
2840 }
2841
2842 mutex_unlock(&cgroup_cft_mutex);
2843}
2844
2845/**
2846 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2847 * @ss: target cgroup subsystem
2848 * @cfts: zero-length name terminated array of cftypes
2849 *
2850 * Register @cfts to @ss. Files described by @cfts are created for all
2851 * existing cgroups to which @ss is attached and all future cgroups will
2852 * have them too. This function can be called anytime whether @ss is
2853 * attached or not.
2854 *
2855 * Returns 0 on successful registration, -errno on failure. Note that this
2856 * function currently returns 0 as long as @cfts registration is successful
2857 * even if some file creation attempts on existing cgroups fail.
2858 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002859int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002860{
2861 struct cftype_set *set;
2862
2863 set = kzalloc(sizeof(*set), GFP_KERNEL);
2864 if (!set)
2865 return -ENOMEM;
2866
2867 cgroup_cfts_prepare();
2868 set->cfts = cfts;
2869 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002870 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002871
2872 return 0;
2873}
2874EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2875
Li Zefana043e3b2008-02-23 15:24:09 -08002876/**
Tejun Heo79578622012-04-01 12:09:56 -07002877 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2878 * @ss: target cgroup subsystem
2879 * @cfts: zero-length name terminated array of cftypes
2880 *
2881 * Unregister @cfts from @ss. Files described by @cfts are removed from
2882 * all existing cgroups to which @ss is attached and all future cgroups
2883 * won't have them either. This function can be called anytime whether @ss
2884 * is attached or not.
2885 *
2886 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2887 * registered with @ss.
2888 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002889int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002890{
2891 struct cftype_set *set;
2892
2893 cgroup_cfts_prepare();
2894
2895 list_for_each_entry(set, &ss->cftsets, node) {
2896 if (set->cfts == cfts) {
2897 list_del_init(&set->node);
2898 cgroup_cfts_commit(ss, cfts, false);
2899 return 0;
2900 }
2901 }
2902
2903 cgroup_cfts_commit(ss, NULL, false);
2904 return -ENOENT;
2905}
2906
2907/**
Li Zefana043e3b2008-02-23 15:24:09 -08002908 * cgroup_task_count - count the number of tasks in a cgroup.
2909 * @cgrp: the cgroup in question
2910 *
2911 * Return the number of tasks in the cgroup.
2912 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002913int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002914{
2915 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002916 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002917
Paul Menage817929e2007-10-18 23:39:36 -07002918 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002919 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002920 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002921 }
2922 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002923 return count;
2924}
2925
2926/*
Paul Menage817929e2007-10-18 23:39:36 -07002927 * Advance a list_head iterator. The iterator should be positioned at
2928 * the start of a css_set
2929 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002930static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002931 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002932{
2933 struct list_head *l = it->cg_link;
2934 struct cg_cgroup_link *link;
2935 struct css_set *cg;
2936
2937 /* Advance to the next non-empty css_set */
2938 do {
2939 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002940 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002941 it->cg_link = NULL;
2942 return;
2943 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002944 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002945 cg = link->cg;
2946 } while (list_empty(&cg->tasks));
2947 it->cg_link = l;
2948 it->task = cg->tasks.next;
2949}
2950
Cliff Wickman31a7df02008-02-07 00:14:42 -08002951/*
2952 * To reduce the fork() overhead for systems that are not actually
2953 * using their cgroups capability, we don't maintain the lists running
2954 * through each css_set to its tasks until we see the list actually
2955 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002956 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002957static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002958{
2959 struct task_struct *p, *g;
2960 write_lock(&css_set_lock);
2961 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002962 /*
2963 * We need tasklist_lock because RCU is not safe against
2964 * while_each_thread(). Besides, a forking task that has passed
2965 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2966 * is not guaranteed to have its child immediately visible in the
2967 * tasklist if we walk through it with RCU.
2968 */
2969 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002970 do_each_thread(g, p) {
2971 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002972 /*
2973 * We should check if the process is exiting, otherwise
2974 * it will race with cgroup_exit() in that the list
2975 * entry won't be deleted though the process has exited.
2976 */
2977 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002978 list_add(&p->cg_list, &p->cgroups->tasks);
2979 task_unlock(p);
2980 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002981 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002982 write_unlock(&css_set_lock);
2983}
2984
Tejun Heo574bd9f2012-11-09 09:12:29 -08002985/**
2986 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2987 * @pos: the current position (%NULL to initiate traversal)
2988 * @cgroup: cgroup whose descendants to walk
2989 *
2990 * To be used by cgroup_for_each_descendant_pre(). Find the next
2991 * descendant to visit for pre-order traversal of @cgroup's descendants.
2992 */
2993struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2994 struct cgroup *cgroup)
2995{
2996 struct cgroup *next;
2997
2998 WARN_ON_ONCE(!rcu_read_lock_held());
2999
3000 /* if first iteration, pretend we just visited @cgroup */
3001 if (!pos) {
3002 if (list_empty(&cgroup->children))
3003 return NULL;
3004 pos = cgroup;
3005 }
3006
3007 /* visit the first child if exists */
3008 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3009 if (next)
3010 return next;
3011
3012 /* no child, visit my or the closest ancestor's next sibling */
3013 do {
3014 next = list_entry_rcu(pos->sibling.next, struct cgroup,
3015 sibling);
3016 if (&next->sibling != &pos->parent->children)
3017 return next;
3018
3019 pos = pos->parent;
3020 } while (pos != cgroup);
3021
3022 return NULL;
3023}
3024EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3025
3026static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3027{
3028 struct cgroup *last;
3029
3030 do {
3031 last = pos;
3032 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3033 sibling);
3034 } while (pos);
3035
3036 return last;
3037}
3038
3039/**
3040 * cgroup_next_descendant_post - find the next descendant for post-order walk
3041 * @pos: the current position (%NULL to initiate traversal)
3042 * @cgroup: cgroup whose descendants to walk
3043 *
3044 * To be used by cgroup_for_each_descendant_post(). Find the next
3045 * descendant to visit for post-order traversal of @cgroup's descendants.
3046 */
3047struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3048 struct cgroup *cgroup)
3049{
3050 struct cgroup *next;
3051
3052 WARN_ON_ONCE(!rcu_read_lock_held());
3053
3054 /* if first iteration, visit the leftmost descendant */
3055 if (!pos) {
3056 next = cgroup_leftmost_descendant(cgroup);
3057 return next != cgroup ? next : NULL;
3058 }
3059
3060 /* if there's an unvisited sibling, visit its leftmost descendant */
3061 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3062 if (&next->sibling != &pos->parent->children)
3063 return cgroup_leftmost_descendant(next);
3064
3065 /* no sibling left, visit parent */
3066 next = pos->parent;
3067 return next != cgroup ? next : NULL;
3068}
3069EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3070
Paul Menagebd89aab2007-10-18 23:40:44 -07003071void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003072 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003073{
3074 /*
3075 * The first time anyone tries to iterate across a cgroup,
3076 * we need to enable the list linking each css_set to its
3077 * tasks, and fix up all existing tasks.
3078 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003079 if (!use_task_css_set_links)
3080 cgroup_enable_task_cg_lists();
3081
Paul Menage817929e2007-10-18 23:39:36 -07003082 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003083 it->cg_link = &cgrp->css_sets;
3084 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003085}
3086
Paul Menagebd89aab2007-10-18 23:40:44 -07003087struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003088 struct cgroup_iter *it)
3089{
3090 struct task_struct *res;
3091 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003092 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003093
3094 /* If the iterator cg is NULL, we have no tasks */
3095 if (!it->cg_link)
3096 return NULL;
3097 res = list_entry(l, struct task_struct, cg_list);
3098 /* Advance iterator to find next entry */
3099 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003100 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3101 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003102 /* We reached the end of this task list - move on to
3103 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003104 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003105 } else {
3106 it->task = l;
3107 }
3108 return res;
3109}
3110
Paul Menagebd89aab2007-10-18 23:40:44 -07003111void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003112 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003113{
3114 read_unlock(&css_set_lock);
3115}
3116
Cliff Wickman31a7df02008-02-07 00:14:42 -08003117static inline int started_after_time(struct task_struct *t1,
3118 struct timespec *time,
3119 struct task_struct *t2)
3120{
3121 int start_diff = timespec_compare(&t1->start_time, time);
3122 if (start_diff > 0) {
3123 return 1;
3124 } else if (start_diff < 0) {
3125 return 0;
3126 } else {
3127 /*
3128 * Arbitrarily, if two processes started at the same
3129 * time, we'll say that the lower pointer value
3130 * started first. Note that t2 may have exited by now
3131 * so this may not be a valid pointer any longer, but
3132 * that's fine - it still serves to distinguish
3133 * between two tasks started (effectively) simultaneously.
3134 */
3135 return t1 > t2;
3136 }
3137}
3138
3139/*
3140 * This function is a callback from heap_insert() and is used to order
3141 * the heap.
3142 * In this case we order the heap in descending task start time.
3143 */
3144static inline int started_after(void *p1, void *p2)
3145{
3146 struct task_struct *t1 = p1;
3147 struct task_struct *t2 = p2;
3148 return started_after_time(t1, &t2->start_time, t2);
3149}
3150
3151/**
3152 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3153 * @scan: struct cgroup_scanner containing arguments for the scan
3154 *
3155 * Arguments include pointers to callback functions test_task() and
3156 * process_task().
3157 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3158 * and if it returns true, call process_task() for it also.
3159 * The test_task pointer may be NULL, meaning always true (select all tasks).
3160 * Effectively duplicates cgroup_iter_{start,next,end}()
3161 * but does not lock css_set_lock for the call to process_task().
3162 * The struct cgroup_scanner may be embedded in any structure of the caller's
3163 * creation.
3164 * It is guaranteed that process_task() will act on every task that
3165 * is a member of the cgroup for the duration of this call. This
3166 * function may or may not call process_task() for tasks that exit
3167 * or move to a different cgroup during the call, or are forked or
3168 * move into the cgroup during the call.
3169 *
3170 * Note that test_task() may be called with locks held, and may in some
3171 * situations be called multiple times for the same task, so it should
3172 * be cheap.
3173 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3174 * pre-allocated and will be used for heap operations (and its "gt" member will
3175 * be overwritten), else a temporary heap will be used (allocation of which
3176 * may cause this function to fail).
3177 */
3178int cgroup_scan_tasks(struct cgroup_scanner *scan)
3179{
3180 int retval, i;
3181 struct cgroup_iter it;
3182 struct task_struct *p, *dropped;
3183 /* Never dereference latest_task, since it's not refcounted */
3184 struct task_struct *latest_task = NULL;
3185 struct ptr_heap tmp_heap;
3186 struct ptr_heap *heap;
3187 struct timespec latest_time = { 0, 0 };
3188
3189 if (scan->heap) {
3190 /* The caller supplied our heap and pre-allocated its memory */
3191 heap = scan->heap;
3192 heap->gt = &started_after;
3193 } else {
3194 /* We need to allocate our own heap memory */
3195 heap = &tmp_heap;
3196 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3197 if (retval)
3198 /* cannot allocate the heap */
3199 return retval;
3200 }
3201
3202 again:
3203 /*
3204 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3205 * to determine which are of interest, and using the scanner's
3206 * "process_task" callback to process any of them that need an update.
3207 * Since we don't want to hold any locks during the task updates,
3208 * gather tasks to be processed in a heap structure.
3209 * The heap is sorted by descending task start time.
3210 * If the statically-sized heap fills up, we overflow tasks that
3211 * started later, and in future iterations only consider tasks that
3212 * started after the latest task in the previous pass. This
3213 * guarantees forward progress and that we don't miss any tasks.
3214 */
3215 heap->size = 0;
3216 cgroup_iter_start(scan->cg, &it);
3217 while ((p = cgroup_iter_next(scan->cg, &it))) {
3218 /*
3219 * Only affect tasks that qualify per the caller's callback,
3220 * if he provided one
3221 */
3222 if (scan->test_task && !scan->test_task(p, scan))
3223 continue;
3224 /*
3225 * Only process tasks that started after the last task
3226 * we processed
3227 */
3228 if (!started_after_time(p, &latest_time, latest_task))
3229 continue;
3230 dropped = heap_insert(heap, p);
3231 if (dropped == NULL) {
3232 /*
3233 * The new task was inserted; the heap wasn't
3234 * previously full
3235 */
3236 get_task_struct(p);
3237 } else if (dropped != p) {
3238 /*
3239 * The new task was inserted, and pushed out a
3240 * different task
3241 */
3242 get_task_struct(p);
3243 put_task_struct(dropped);
3244 }
3245 /*
3246 * Else the new task was newer than anything already in
3247 * the heap and wasn't inserted
3248 */
3249 }
3250 cgroup_iter_end(scan->cg, &it);
3251
3252 if (heap->size) {
3253 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003254 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003255 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003256 latest_time = q->start_time;
3257 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003258 }
3259 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003260 scan->process_task(q, scan);
3261 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003262 }
3263 /*
3264 * If we had to process any tasks at all, scan again
3265 * in case some of them were in the middle of forking
3266 * children that didn't get processed.
3267 * Not the most efficient way to do it, but it avoids
3268 * having to take callback_mutex in the fork path
3269 */
3270 goto again;
3271 }
3272 if (heap == &tmp_heap)
3273 heap_free(&tmp_heap);
3274 return 0;
3275}
3276
Paul Menage817929e2007-10-18 23:39:36 -07003277/*
Ben Blum102a7752009-09-23 15:56:26 -07003278 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003279 *
3280 * Reading this file can return large amounts of data if a cgroup has
3281 * *lots* of attached tasks. So it may need several calls to read(),
3282 * but we cannot guarantee that the information we produce is correct
3283 * unless we produce it entirely atomically.
3284 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003285 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003286
Li Zefan24528252012-01-20 11:58:43 +08003287/* which pidlist file are we talking about? */
3288enum cgroup_filetype {
3289 CGROUP_FILE_PROCS,
3290 CGROUP_FILE_TASKS,
3291};
3292
3293/*
3294 * A pidlist is a list of pids that virtually represents the contents of one
3295 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3296 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3297 * to the cgroup.
3298 */
3299struct cgroup_pidlist {
3300 /*
3301 * used to find which pidlist is wanted. doesn't change as long as
3302 * this particular list stays in the list.
3303 */
3304 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3305 /* array of xids */
3306 pid_t *list;
3307 /* how many elements the above list has */
3308 int length;
3309 /* how many files are using the current array */
3310 int use_count;
3311 /* each of these stored in a list by its cgroup */
3312 struct list_head links;
3313 /* pointer to the cgroup we belong to, for list removal purposes */
3314 struct cgroup *owner;
3315 /* protects the other fields */
3316 struct rw_semaphore mutex;
3317};
3318
Paul Menagebbcb81d2007-10-18 23:39:32 -07003319/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003320 * The following two functions "fix" the issue where there are more pids
3321 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3322 * TODO: replace with a kernel-wide solution to this problem
3323 */
3324#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3325static void *pidlist_allocate(int count)
3326{
3327 if (PIDLIST_TOO_LARGE(count))
3328 return vmalloc(count * sizeof(pid_t));
3329 else
3330 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3331}
3332static void pidlist_free(void *p)
3333{
3334 if (is_vmalloc_addr(p))
3335 vfree(p);
3336 else
3337 kfree(p);
3338}
3339static void *pidlist_resize(void *p, int newcount)
3340{
3341 void *newlist;
3342 /* note: if new alloc fails, old p will still be valid either way */
3343 if (is_vmalloc_addr(p)) {
3344 newlist = vmalloc(newcount * sizeof(pid_t));
3345 if (!newlist)
3346 return NULL;
3347 memcpy(newlist, p, newcount * sizeof(pid_t));
3348 vfree(p);
3349 } else {
3350 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3351 }
3352 return newlist;
3353}
3354
3355/*
Ben Blum102a7752009-09-23 15:56:26 -07003356 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3357 * If the new stripped list is sufficiently smaller and there's enough memory
3358 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3359 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003360 */
Ben Blum102a7752009-09-23 15:56:26 -07003361/* is the size difference enough that we should re-allocate the array? */
3362#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3363static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003364{
Ben Blum102a7752009-09-23 15:56:26 -07003365 int src, dest = 1;
3366 pid_t *list = *p;
3367 pid_t *newlist;
3368
3369 /*
3370 * we presume the 0th element is unique, so i starts at 1. trivial
3371 * edge cases first; no work needs to be done for either
3372 */
3373 if (length == 0 || length == 1)
3374 return length;
3375 /* src and dest walk down the list; dest counts unique elements */
3376 for (src = 1; src < length; src++) {
3377 /* find next unique element */
3378 while (list[src] == list[src-1]) {
3379 src++;
3380 if (src == length)
3381 goto after;
3382 }
3383 /* dest always points to where the next unique element goes */
3384 list[dest] = list[src];
3385 dest++;
3386 }
3387after:
3388 /*
3389 * if the length difference is large enough, we want to allocate a
3390 * smaller buffer to save memory. if this fails due to out of memory,
3391 * we'll just stay with what we've got.
3392 */
3393 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003394 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003395 if (newlist)
3396 *p = newlist;
3397 }
3398 return dest;
3399}
3400
3401static int cmppid(const void *a, const void *b)
3402{
3403 return *(pid_t *)a - *(pid_t *)b;
3404}
3405
3406/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003407 * find the appropriate pidlist for our purpose (given procs vs tasks)
3408 * returns with the lock on that pidlist already held, and takes care
3409 * of the use count, or returns NULL with no locks held if we're out of
3410 * memory.
3411 */
3412static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3413 enum cgroup_filetype type)
3414{
3415 struct cgroup_pidlist *l;
3416 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08003417 struct pid_namespace *ns = current->nsproxy->pid_ns;
3418
Ben Blum72a8cb32009-09-23 15:56:27 -07003419 /*
3420 * We can't drop the pidlist_mutex before taking the l->mutex in case
3421 * the last ref-holder is trying to remove l from the list at the same
3422 * time. Holding the pidlist_mutex precludes somebody taking whichever
3423 * list we find out from under us - compare release_pid_array().
3424 */
3425 mutex_lock(&cgrp->pidlist_mutex);
3426 list_for_each_entry(l, &cgrp->pidlists, links) {
3427 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003428 /* make sure l doesn't vanish out from under us */
3429 down_write(&l->mutex);
3430 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003431 return l;
3432 }
3433 }
3434 /* entry not found; create a new one */
3435 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3436 if (!l) {
3437 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003438 return l;
3439 }
3440 init_rwsem(&l->mutex);
3441 down_write(&l->mutex);
3442 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003443 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003444 l->use_count = 0; /* don't increment here */
3445 l->list = NULL;
3446 l->owner = cgrp;
3447 list_add(&l->links, &cgrp->pidlists);
3448 mutex_unlock(&cgrp->pidlist_mutex);
3449 return l;
3450}
3451
3452/*
Ben Blum102a7752009-09-23 15:56:26 -07003453 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3454 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003455static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3456 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003457{
3458 pid_t *array;
3459 int length;
3460 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003461 struct cgroup_iter it;
3462 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003463 struct cgroup_pidlist *l;
3464
3465 /*
3466 * If cgroup gets more users after we read count, we won't have
3467 * enough space - tough. This race is indistinguishable to the
3468 * caller from the case that the additional cgroup users didn't
3469 * show up until sometime later on.
3470 */
3471 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003472 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003473 if (!array)
3474 return -ENOMEM;
3475 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003476 cgroup_iter_start(cgrp, &it);
3477 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003478 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003479 break;
Ben Blum102a7752009-09-23 15:56:26 -07003480 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003481 if (type == CGROUP_FILE_PROCS)
3482 pid = task_tgid_vnr(tsk);
3483 else
3484 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003485 if (pid > 0) /* make sure to only use valid results */
3486 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003487 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003488 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003489 length = n;
3490 /* now sort & (if procs) strip out duplicates */
3491 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003492 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003493 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003494 l = cgroup_pidlist_find(cgrp, type);
3495 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003496 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003497 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003498 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003499 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003500 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003501 l->list = array;
3502 l->length = length;
3503 l->use_count++;
3504 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003505 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003506 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003507}
3508
Balbir Singh846c7bb2007-10-18 23:39:44 -07003509/**
Li Zefana043e3b2008-02-23 15:24:09 -08003510 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003511 * @stats: cgroupstats to fill information into
3512 * @dentry: A dentry entry belonging to the cgroup for which stats have
3513 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003514 *
3515 * Build and fill cgroupstats so that taskstats can export it to user
3516 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003517 */
3518int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3519{
3520 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003521 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003522 struct cgroup_iter it;
3523 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003524
Balbir Singh846c7bb2007-10-18 23:39:44 -07003525 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003526 * Validate dentry by checking the superblock operations,
3527 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003528 */
Li Zefan33d283b2008-11-19 15:36:48 -08003529 if (dentry->d_sb->s_op != &cgroup_ops ||
3530 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003531 goto err;
3532
3533 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003534 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003535
Paul Menagebd89aab2007-10-18 23:40:44 -07003536 cgroup_iter_start(cgrp, &it);
3537 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003538 switch (tsk->state) {
3539 case TASK_RUNNING:
3540 stats->nr_running++;
3541 break;
3542 case TASK_INTERRUPTIBLE:
3543 stats->nr_sleeping++;
3544 break;
3545 case TASK_UNINTERRUPTIBLE:
3546 stats->nr_uninterruptible++;
3547 break;
3548 case TASK_STOPPED:
3549 stats->nr_stopped++;
3550 break;
3551 default:
3552 if (delayacct_is_task_waiting_on_io(tsk))
3553 stats->nr_io_wait++;
3554 break;
3555 }
3556 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003557 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003558
Balbir Singh846c7bb2007-10-18 23:39:44 -07003559err:
3560 return ret;
3561}
3562
Paul Menage8f3ff202009-09-23 15:56:25 -07003563
Paul Menagecc31edc2008-10-18 20:28:04 -07003564/*
Ben Blum102a7752009-09-23 15:56:26 -07003565 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003566 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003567 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003568 */
3569
Ben Blum102a7752009-09-23 15:56:26 -07003570static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003571{
3572 /*
3573 * Initially we receive a position value that corresponds to
3574 * one more than the last pid shown (or 0 on the first call or
3575 * after a seek to the start). Use a binary-search to find the
3576 * next pid to display, if any
3577 */
Ben Blum102a7752009-09-23 15:56:26 -07003578 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003579 int index = 0, pid = *pos;
3580 int *iter;
3581
Ben Blum102a7752009-09-23 15:56:26 -07003582 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003583 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003584 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003585
Paul Menagecc31edc2008-10-18 20:28:04 -07003586 while (index < end) {
3587 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003588 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003589 index = mid;
3590 break;
Ben Blum102a7752009-09-23 15:56:26 -07003591 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003592 index = mid + 1;
3593 else
3594 end = mid;
3595 }
3596 }
3597 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003598 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003599 return NULL;
3600 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003601 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003602 *pos = *iter;
3603 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003604}
3605
Ben Blum102a7752009-09-23 15:56:26 -07003606static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003607{
Ben Blum102a7752009-09-23 15:56:26 -07003608 struct cgroup_pidlist *l = s->private;
3609 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003610}
3611
Ben Blum102a7752009-09-23 15:56:26 -07003612static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003613{
Ben Blum102a7752009-09-23 15:56:26 -07003614 struct cgroup_pidlist *l = s->private;
3615 pid_t *p = v;
3616 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003617 /*
3618 * Advance to the next pid in the array. If this goes off the
3619 * end, we're done
3620 */
3621 p++;
3622 if (p >= end) {
3623 return NULL;
3624 } else {
3625 *pos = *p;
3626 return p;
3627 }
3628}
3629
Ben Blum102a7752009-09-23 15:56:26 -07003630static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003631{
3632 return seq_printf(s, "%d\n", *(int *)v);
3633}
3634
Ben Blum102a7752009-09-23 15:56:26 -07003635/*
3636 * seq_operations functions for iterating on pidlists through seq_file -
3637 * independent of whether it's tasks or procs
3638 */
3639static const struct seq_operations cgroup_pidlist_seq_operations = {
3640 .start = cgroup_pidlist_start,
3641 .stop = cgroup_pidlist_stop,
3642 .next = cgroup_pidlist_next,
3643 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003644};
3645
Ben Blum102a7752009-09-23 15:56:26 -07003646static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003647{
Ben Blum72a8cb32009-09-23 15:56:27 -07003648 /*
3649 * the case where we're the last user of this particular pidlist will
3650 * have us remove it from the cgroup's list, which entails taking the
3651 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3652 * pidlist_mutex, we have to take pidlist_mutex first.
3653 */
3654 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003655 down_write(&l->mutex);
3656 BUG_ON(!l->use_count);
3657 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003658 /* we're the last user if refcount is 0; remove and free */
3659 list_del(&l->links);
3660 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003661 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003662 put_pid_ns(l->key.ns);
3663 up_write(&l->mutex);
3664 kfree(l);
3665 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003666 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003667 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003668 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003669}
3670
Ben Blum102a7752009-09-23 15:56:26 -07003671static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003672{
Ben Blum102a7752009-09-23 15:56:26 -07003673 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003674 if (!(file->f_mode & FMODE_READ))
3675 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003676 /*
3677 * the seq_file will only be initialized if the file was opened for
3678 * reading; hence we check if it's not null only in that case.
3679 */
3680 l = ((struct seq_file *)file->private_data)->private;
3681 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003682 return seq_release(inode, file);
3683}
3684
Ben Blum102a7752009-09-23 15:56:26 -07003685static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003686 .read = seq_read,
3687 .llseek = seq_lseek,
3688 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003689 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003690};
3691
3692/*
Ben Blum102a7752009-09-23 15:56:26 -07003693 * The following functions handle opens on a file that displays a pidlist
3694 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3695 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003696 */
Ben Blum102a7752009-09-23 15:56:26 -07003697/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003698static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003699{
3700 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003701 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003702 int retval;
3703
3704 /* Nothing to do for write-only files */
3705 if (!(file->f_mode & FMODE_READ))
3706 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003707
Ben Blum102a7752009-09-23 15:56:26 -07003708 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003709 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003710 if (retval)
3711 return retval;
3712 /* configure file information */
3713 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003714
Ben Blum102a7752009-09-23 15:56:26 -07003715 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003716 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003717 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003718 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003719 }
Ben Blum102a7752009-09-23 15:56:26 -07003720 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003721 return 0;
3722}
Ben Blum102a7752009-09-23 15:56:26 -07003723static int cgroup_tasks_open(struct inode *unused, struct file *file)
3724{
Ben Blum72a8cb32009-09-23 15:56:27 -07003725 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003726}
3727static int cgroup_procs_open(struct inode *unused, struct file *file)
3728{
Ben Blum72a8cb32009-09-23 15:56:27 -07003729 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003730}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003731
Paul Menagebd89aab2007-10-18 23:40:44 -07003732static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003733 struct cftype *cft)
3734{
Paul Menagebd89aab2007-10-18 23:40:44 -07003735 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003736}
3737
Paul Menage6379c102008-07-25 01:47:01 -07003738static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3739 struct cftype *cft,
3740 u64 val)
3741{
3742 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3743 if (val)
3744 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3745 else
3746 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3747 return 0;
3748}
3749
Paul Menagebbcb81d2007-10-18 23:39:32 -07003750/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003751 * Unregister event and free resources.
3752 *
3753 * Gets called from workqueue.
3754 */
3755static void cgroup_event_remove(struct work_struct *work)
3756{
3757 struct cgroup_event *event = container_of(work, struct cgroup_event,
3758 remove);
3759 struct cgroup *cgrp = event->cgrp;
3760
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003761 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3762
3763 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003764 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003765 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003766}
3767
3768/*
3769 * Gets called on POLLHUP on eventfd when user closes it.
3770 *
3771 * Called with wqh->lock held and interrupts disabled.
3772 */
3773static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3774 int sync, void *key)
3775{
3776 struct cgroup_event *event = container_of(wait,
3777 struct cgroup_event, wait);
3778 struct cgroup *cgrp = event->cgrp;
3779 unsigned long flags = (unsigned long)key;
3780
3781 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003782 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003783 spin_lock(&cgrp->event_list_lock);
3784 list_del(&event->list);
3785 spin_unlock(&cgrp->event_list_lock);
3786 /*
3787 * We are in atomic context, but cgroup_event_remove() may
3788 * sleep, so we have to call it in workqueue.
3789 */
3790 schedule_work(&event->remove);
3791 }
3792
3793 return 0;
3794}
3795
3796static void cgroup_event_ptable_queue_proc(struct file *file,
3797 wait_queue_head_t *wqh, poll_table *pt)
3798{
3799 struct cgroup_event *event = container_of(pt,
3800 struct cgroup_event, pt);
3801
3802 event->wqh = wqh;
3803 add_wait_queue(wqh, &event->wait);
3804}
3805
3806/*
3807 * Parse input and register new cgroup event handler.
3808 *
3809 * Input must be in format '<event_fd> <control_fd> <args>'.
3810 * Interpretation of args is defined by control file implementation.
3811 */
3812static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3813 const char *buffer)
3814{
3815 struct cgroup_event *event = NULL;
3816 unsigned int efd, cfd;
3817 struct file *efile = NULL;
3818 struct file *cfile = NULL;
3819 char *endp;
3820 int ret;
3821
3822 efd = simple_strtoul(buffer, &endp, 10);
3823 if (*endp != ' ')
3824 return -EINVAL;
3825 buffer = endp + 1;
3826
3827 cfd = simple_strtoul(buffer, &endp, 10);
3828 if ((*endp != ' ') && (*endp != '\0'))
3829 return -EINVAL;
3830 buffer = endp + 1;
3831
3832 event = kzalloc(sizeof(*event), GFP_KERNEL);
3833 if (!event)
3834 return -ENOMEM;
3835 event->cgrp = cgrp;
3836 INIT_LIST_HEAD(&event->list);
3837 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3838 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3839 INIT_WORK(&event->remove, cgroup_event_remove);
3840
3841 efile = eventfd_fget(efd);
3842 if (IS_ERR(efile)) {
3843 ret = PTR_ERR(efile);
3844 goto fail;
3845 }
3846
3847 event->eventfd = eventfd_ctx_fileget(efile);
3848 if (IS_ERR(event->eventfd)) {
3849 ret = PTR_ERR(event->eventfd);
3850 goto fail;
3851 }
3852
3853 cfile = fget(cfd);
3854 if (!cfile) {
3855 ret = -EBADF;
3856 goto fail;
3857 }
3858
3859 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003860 /* AV: shouldn't we check that it's been opened for read instead? */
3861 ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003862 if (ret < 0)
3863 goto fail;
3864
3865 event->cft = __file_cft(cfile);
3866 if (IS_ERR(event->cft)) {
3867 ret = PTR_ERR(event->cft);
3868 goto fail;
3869 }
3870
3871 if (!event->cft->register_event || !event->cft->unregister_event) {
3872 ret = -EINVAL;
3873 goto fail;
3874 }
3875
3876 ret = event->cft->register_event(cgrp, event->cft,
3877 event->eventfd, buffer);
3878 if (ret)
3879 goto fail;
3880
3881 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3882 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3883 ret = 0;
3884 goto fail;
3885 }
3886
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{
3921 return clone_children(cgrp);
3922}
3923
3924static int cgroup_clone_children_write(struct cgroup *cgrp,
3925 struct cftype *cft,
3926 u64 val)
3927{
3928 if (val)
3929 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3930 else
3931 clear_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3932 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",
3967 .read_u64 = cgroup_clone_children_read,
3968 .write_u64 = cgroup_clone_children_write,
3969 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003970 {
3971 .name = "release_agent",
3972 .flags = CFTYPE_ONLY_ON_ROOT,
3973 .read_seq_string = cgroup_release_agent_show,
3974 .write_string = cgroup_release_agent_write,
3975 .max_write_len = PATH_MAX,
3976 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003977 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003978};
3979
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003980/**
3981 * cgroup_populate_dir - selectively creation of files in a directory
3982 * @cgrp: target cgroup
3983 * @base_files: true if the base files should be added
3984 * @subsys_mask: mask of the subsystem ids whose files should be added
3985 */
3986static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3987 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003988{
3989 int err;
3990 struct cgroup_subsys *ss;
3991
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003992 if (base_files) {
3993 err = cgroup_addrm_files(cgrp, NULL, files, true);
3994 if (err < 0)
3995 return err;
3996 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07003997
Tejun Heo8e3f6542012-04-01 12:09:55 -07003998 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07003999 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004000 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004001 if (!test_bit(ss->subsys_id, &subsys_mask))
4002 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004003
Tejun Heodb0416b2012-04-01 12:09:55 -07004004 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004005 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004006 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004007
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004008 /* This cgroup is ready now */
4009 for_each_subsys(cgrp->root, ss) {
4010 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4011 /*
4012 * Update id->css pointer and make this css visible from
4013 * CSS ID functions. This pointer will be dereferened
4014 * from RCU-read-side without locks.
4015 */
4016 if (css->id)
4017 rcu_assign_pointer(css->id->css, css);
4018 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004019
4020 return 0;
4021}
4022
Tejun Heo48ddbe12012-04-01 12:09:56 -07004023static void css_dput_fn(struct work_struct *work)
4024{
4025 struct cgroup_subsys_state *css =
4026 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004027 struct dentry *dentry = css->cgroup->dentry;
4028 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004029
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004030 atomic_inc(&sb->s_active);
4031 dput(dentry);
4032 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004033}
4034
Paul Menageddbcc7e2007-10-18 23:39:30 -07004035static void init_cgroup_css(struct cgroup_subsys_state *css,
4036 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004037 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004038{
Paul Menagebd89aab2007-10-18 23:40:44 -07004039 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004040 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004041 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004042 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004043 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004044 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07004045 BUG_ON(cgrp->subsys[ss->subsys_id]);
4046 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004047
4048 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004049 * css holds an extra ref to @cgrp->dentry which is put on the last
4050 * css_put(). dput() requires process context, which css_put() may
4051 * be called without. @css->dput_work will be used to invoke
4052 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004053 */
4054 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004055}
4056
4057/*
Li Zefana043e3b2008-02-23 15:24:09 -08004058 * cgroup_create - create a cgroup
4059 * @parent: cgroup that will be parent of the new cgroup
4060 * @dentry: dentry of the new cgroup
4061 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004062 *
Li Zefana043e3b2008-02-23 15:24:09 -08004063 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004064 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004065static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004066 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004067{
Paul Menagebd89aab2007-10-18 23:40:44 -07004068 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004069 struct cgroupfs_root *root = parent->root;
4070 int err = 0;
4071 struct cgroup_subsys *ss;
4072 struct super_block *sb = root->sb;
4073
Paul Menagebd89aab2007-10-18 23:40:44 -07004074 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4075 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004076 return -ENOMEM;
4077
Tejun Heo976c06b2012-11-05 09:16:59 -08004078 /*
4079 * Only live parents can have children. Note that the liveliness
4080 * check isn't strictly necessary because cgroup_mkdir() and
4081 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4082 * anyway so that locking is contained inside cgroup proper and we
4083 * don't get nasty surprises if we ever grow another caller.
4084 */
4085 if (!cgroup_lock_live_group(parent)) {
4086 err = -ENODEV;
4087 goto err_free;
4088 }
4089
Paul Menageddbcc7e2007-10-18 23:39:30 -07004090 /* Grab a reference on the superblock so the hierarchy doesn't
4091 * get deleted on unmount if there are child cgroups. This
4092 * can be done outside cgroup_mutex, since the sb can't
4093 * disappear while someone has an open control file on the
4094 * fs */
4095 atomic_inc(&sb->s_active);
4096
Paul Menagecc31edc2008-10-18 20:28:04 -07004097 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004098
Paul Menagebd89aab2007-10-18 23:40:44 -07004099 cgrp->parent = parent;
4100 cgrp->root = parent->root;
4101 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004102
Li Zefanb6abdb02008-03-04 14:28:19 -08004103 if (notify_on_release(parent))
4104 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4105
Daniel Lezcano97978e62010-10-27 15:33:35 -07004106 if (clone_children(parent))
4107 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
4108
Paul Menageddbcc7e2007-10-18 23:39:30 -07004109 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004110 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004111
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004112 css = ss->create(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004113 if (IS_ERR(css)) {
4114 err = PTR_ERR(css);
4115 goto err_destroy;
4116 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004117 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004118 if (ss->use_id) {
4119 err = alloc_css_id(ss, parent, cgrp);
4120 if (err)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004121 goto err_destroy;
Li Zefan4528fd02010-02-02 13:44:10 -08004122 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004123 /* At error, ->destroy() callback has to free assigned ID. */
Daniel Lezcano97978e62010-10-27 15:33:35 -07004124 if (clone_children(parent) && ss->post_clone)
Li Zefan761b3ef2012-01-31 13:47:36 +08004125 ss->post_clone(cgrp);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004126
4127 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4128 parent->parent) {
4129 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",
4130 current->comm, current->pid, ss->name);
4131 if (!strcmp(ss->name, "memory"))
4132 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4133 ss->warned_broken_hierarchy = true;
4134 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004135 }
4136
Tejun Heoeb6fd502012-11-09 09:12:29 -08004137 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004138 root->number_of_cgroups++;
4139
Paul Menagebd89aab2007-10-18 23:40:44 -07004140 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004141 if (err < 0)
4142 goto err_remove;
4143
Tejun Heoa8638032012-11-09 09:12:29 -08004144 for_each_subsys(root, ss) {
4145 /* each css holds a ref to the cgroup's dentry */
Tejun Heoed9577932012-11-05 09:16:58 -08004146 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004147
Tejun Heoa8638032012-11-09 09:12:29 -08004148 /* creation succeeded, notify subsystems */
4149 if (ss->post_create)
4150 ss->post_create(cgrp);
4151 }
4152
Paul Menageddbcc7e2007-10-18 23:39:30 -07004153 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07004154 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004155
Tejun Heob0ca5a82012-04-01 12:09:54 -07004156 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4157
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004158 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004159 /* If err < 0, we have a half-filled directory - oh well ;) */
4160
4161 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004162 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004163
4164 return 0;
4165
4166 err_remove:
4167
Tejun Heoeb6fd502012-11-09 09:12:29 -08004168 list_del_rcu(&cgrp->sibling);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004169 root->number_of_cgroups--;
4170
4171 err_destroy:
4172
4173 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004174 if (cgrp->subsys[ss->subsys_id])
Li Zefan761b3ef2012-01-31 13:47:36 +08004175 ss->destroy(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004176 }
4177
4178 mutex_unlock(&cgroup_mutex);
4179
4180 /* Release the reference count that we took on the superblock */
4181 deactivate_super(sb);
Tejun Heo976c06b2012-11-05 09:16:59 -08004182err_free:
Paul Menagebd89aab2007-10-18 23:40:44 -07004183 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004184 return err;
4185}
4186
Al Viro18bb1db2011-07-26 01:41:39 -04004187static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004188{
4189 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4190
4191 /* the vfs holds inode->i_mutex already */
4192 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4193}
4194
Tejun Heo28b4c272012-04-01 12:09:56 -07004195/*
4196 * Check the reference count on each subsystem. Since we already
4197 * established that there are no tasks in the cgroup, if the css refcount
4198 * is also 1, then there should be no outstanding references, so the
4199 * subsystem is safe to destroy. We scan across all subsystems rather than
4200 * using the per-hierarchy linked list of mounted subsystems since we can
4201 * be called via check_for_release() with no synchronization other than
4202 * RCU, and the subsystem linked list isn't RCU-safe.
4203 */
Li Zefan55b6fd02008-07-29 22:33:20 -07004204static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004205{
Paul Menage81a6a5c2007-10-18 23:39:38 -07004206 int i;
Tejun Heo28b4c272012-04-01 12:09:56 -07004207
Ben Blumaae8aab2010-03-10 15:22:07 -08004208 /*
4209 * We won't need to lock the subsys array, because the subsystems
4210 * we're concerned about aren't going anywhere since our cgroup root
4211 * has a reference on them.
4212 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004213 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4214 struct cgroup_subsys *ss = subsys[i];
4215 struct cgroup_subsys_state *css;
Tejun Heo28b4c272012-04-01 12:09:56 -07004216
Ben Blumaae8aab2010-03-10 15:22:07 -08004217 /* Skip subsystems not present or not in this hierarchy */
4218 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004219 continue;
Tejun Heo28b4c272012-04-01 12:09:56 -07004220
Paul Menagebd89aab2007-10-18 23:40:44 -07004221 css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004222 /*
4223 * When called from check_for_release() it's possible
Paul Menage81a6a5c2007-10-18 23:39:38 -07004224 * that by this point the cgroup has been removed
4225 * and the css deleted. But a false-positive doesn't
4226 * matter, since it can only happen if the cgroup
4227 * has been deleted and hence no longer needs the
Tejun Heo28b4c272012-04-01 12:09:56 -07004228 * release agent to be called anyway.
4229 */
4230 if (css && css_refcnt(css) > 1)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004231 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004232 }
4233 return 0;
4234}
4235
Paul Menageddbcc7e2007-10-18 23:39:30 -07004236static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4237{
Paul Menagebd89aab2007-10-18 23:40:44 -07004238 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004239 struct dentry *d;
4240 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004241 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004242 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004243 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004244
4245 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08004246 mutex_lock(&cgroup_mutex);
4247 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004248 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249 mutex_unlock(&cgroup_mutex);
4250 return -EBUSY;
4251 }
Tejun Heoed9577932012-11-05 09:16:58 -08004252
Tejun Heo1a90dd52012-11-05 09:16:59 -08004253 /*
4254 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4255 * removed. This makes future css_tryget() and child creation
4256 * attempts fail thus maintaining the removal conditions verified
4257 * above.
4258 */
Tejun Heoed9577932012-11-05 09:16:58 -08004259 for_each_subsys(cgrp->root, ss) {
4260 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4261
4262 WARN_ON(atomic_read(&css->refcnt) < 0);
4263 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004264 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004265 set_bit(CGRP_REMOVED, &cgrp->flags);
4266
4267 /*
4268 * Tell subsystems to initate destruction. pre_destroy() should be
4269 * called with cgroup_mutex unlocked. See 3fa59dfbc3 ("cgroup: fix
4270 * potential deadlock in pre_destroy") for details.
4271 */
4272 mutex_unlock(&cgroup_mutex);
4273 for_each_subsys(cgrp->root, ss)
4274 if (ss->pre_destroy)
Tejun Heobcf6de12012-11-05 09:16:59 -08004275 ss->pre_destroy(cgrp);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004276 mutex_lock(&cgroup_mutex);
Tejun Heoed9577932012-11-05 09:16:58 -08004277
4278 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004279 * Put all the base refs. Each css holds an extra reference to the
4280 * cgroup's dentry and cgroup removal proceeds regardless of css
4281 * refs. On the last put of each css, whenever that may be, the
4282 * extra dentry ref is put so that dentry destruction happens only
4283 * after all css's are released.
4284 */
Tejun Heoe9316082012-11-05 09:16:58 -08004285 for_each_subsys(cgrp->root, ss)
4286 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004287
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004288 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004289 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004290 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004291 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004292
Paul Menage999cd8a2009-01-07 18:08:36 -08004293 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004294 list_del_rcu(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08004295
Tejun Heob0ca5a82012-04-01 12:09:54 -07004296 list_del_init(&cgrp->allcg_node);
4297
Paul Menagebd89aab2007-10-18 23:40:44 -07004298 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004299
4300 cgroup_d_remove_dir(d);
4301 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004302
Paul Menagebd89aab2007-10-18 23:40:44 -07004303 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004304 check_for_release(parent);
4305
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004306 /*
4307 * Unregister events and notify userspace.
4308 * Notify userspace about cgroup removing only after rmdir of cgroup
4309 * directory to avoid race between userspace and kernelspace
4310 */
4311 spin_lock(&cgrp->event_list_lock);
4312 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4313 list_del(&event->list);
4314 remove_wait_queue(event->wqh, &event->wait);
4315 eventfd_signal(event->eventfd, 1);
4316 schedule_work(&event->remove);
4317 }
4318 spin_unlock(&cgrp->event_list_lock);
4319
Paul Menageddbcc7e2007-10-18 23:39:30 -07004320 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004321 return 0;
4322}
4323
Tejun Heo8e3f6542012-04-01 12:09:55 -07004324static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4325{
4326 INIT_LIST_HEAD(&ss->cftsets);
4327
4328 /*
4329 * base_cftset is embedded in subsys itself, no need to worry about
4330 * deregistration.
4331 */
4332 if (ss->base_cftypes) {
4333 ss->base_cftset.cfts = ss->base_cftypes;
4334 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4335 }
4336}
4337
Li Zefan06a11922008-04-29 01:00:07 -07004338static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004339{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004340 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004341
4342 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004343
Tejun Heo8e3f6542012-04-01 12:09:55 -07004344 /* init base cftset */
4345 cgroup_init_cftsets(ss);
4346
Paul Menageddbcc7e2007-10-18 23:39:30 -07004347 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004348 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004349 ss->root = &rootnode;
Li Zefan761b3ef2012-01-31 13:47:36 +08004350 css = ss->create(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004351 /* We don't handle early failures gracefully */
4352 BUG_ON(IS_ERR(css));
4353 init_cgroup_css(css, ss, dummytop);
4354
Li Zefane8d55fd2008-04-29 01:00:13 -07004355 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004356 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004357 * newly registered, all tasks and hence the
4358 * init_css_set is in the subsystem's top cgroup. */
4359 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004360
4361 need_forkexit_callback |= ss->fork || ss->exit;
4362
Li Zefane8d55fd2008-04-29 01:00:13 -07004363 /* At system boot, before all subsystems have been
4364 * registered, no tasks have been forked, so we don't
4365 * need to invoke fork callbacks here. */
4366 BUG_ON(!list_empty(&init_task.tasks));
4367
Paul Menageddbcc7e2007-10-18 23:39:30 -07004368 ss->active = 1;
Ben Blume6a11052010-03-10 15:22:09 -08004369
Tejun Heoa8638032012-11-09 09:12:29 -08004370 if (ss->post_create)
4371 ss->post_create(&ss->root->top_cgroup);
4372
Ben Blume6a11052010-03-10 15:22:09 -08004373 /* this function shouldn't be used with modular subsystems, since they
4374 * need to register a subsys_id, among other things */
4375 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004376}
4377
4378/**
Ben Blume6a11052010-03-10 15:22:09 -08004379 * cgroup_load_subsys: load and register a modular subsystem at runtime
4380 * @ss: the subsystem to load
4381 *
4382 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004383 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004384 * up for use. If the subsystem is built-in anyway, work is delegated to the
4385 * simpler cgroup_init_subsys.
4386 */
4387int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4388{
4389 int i;
4390 struct cgroup_subsys_state *css;
4391
4392 /* check name and function validity */
4393 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4394 ss->create == NULL || ss->destroy == NULL)
4395 return -EINVAL;
4396
4397 /*
4398 * we don't support callbacks in modular subsystems. this check is
4399 * before the ss->module check for consistency; a subsystem that could
4400 * be a module should still have no callbacks even if the user isn't
4401 * compiling it as one.
4402 */
4403 if (ss->fork || ss->exit)
4404 return -EINVAL;
4405
4406 /*
4407 * an optionally modular subsystem is built-in: we want to do nothing,
4408 * since cgroup_init_subsys will have already taken care of it.
4409 */
4410 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004411 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004412 BUG_ON(subsys[ss->subsys_id] != ss);
4413 return 0;
4414 }
4415
Tejun Heo8e3f6542012-04-01 12:09:55 -07004416 /* init base cftset */
4417 cgroup_init_cftsets(ss);
4418
Ben Blume6a11052010-03-10 15:22:09 -08004419 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004420 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004421
4422 /*
4423 * no ss->create seems to need anything important in the ss struct, so
4424 * this can happen first (i.e. before the rootnode attachment).
4425 */
Li Zefan761b3ef2012-01-31 13:47:36 +08004426 css = ss->create(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004427 if (IS_ERR(css)) {
4428 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004429 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004430 mutex_unlock(&cgroup_mutex);
4431 return PTR_ERR(css);
4432 }
4433
4434 list_add(&ss->sibling, &rootnode.subsys_list);
4435 ss->root = &rootnode;
4436
4437 /* our new subsystem will be attached to the dummy hierarchy. */
4438 init_cgroup_css(css, ss, dummytop);
4439 /* init_idr must be after init_cgroup_css because it sets css->id. */
4440 if (ss->use_id) {
4441 int ret = cgroup_init_idr(ss, css);
4442 if (ret) {
4443 dummytop->subsys[ss->subsys_id] = NULL;
Li Zefan761b3ef2012-01-31 13:47:36 +08004444 ss->destroy(dummytop);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004445 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004446 mutex_unlock(&cgroup_mutex);
4447 return ret;
4448 }
4449 }
4450
4451 /*
4452 * Now we need to entangle the css into the existing css_sets. unlike
4453 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4454 * will need a new pointer to it; done by iterating the css_set_table.
4455 * furthermore, modifying the existing css_sets will corrupt the hash
4456 * table state, so each changed css_set will need its hash recomputed.
4457 * this is all done under the css_set_lock.
4458 */
4459 write_lock(&css_set_lock);
4460 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4461 struct css_set *cg;
4462 struct hlist_node *node, *tmp;
4463 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4464
4465 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4466 /* skip entries that we already rehashed */
4467 if (cg->subsys[ss->subsys_id])
4468 continue;
4469 /* remove existing entry */
4470 hlist_del(&cg->hlist);
4471 /* set new value */
4472 cg->subsys[ss->subsys_id] = css;
4473 /* recompute hash and restore entry */
4474 new_bucket = css_set_hash(cg->subsys);
4475 hlist_add_head(&cg->hlist, new_bucket);
4476 }
4477 }
4478 write_unlock(&css_set_lock);
4479
Ben Blume6a11052010-03-10 15:22:09 -08004480 ss->active = 1;
4481
Tejun Heoa8638032012-11-09 09:12:29 -08004482 if (ss->post_create)
4483 ss->post_create(&ss->root->top_cgroup);
4484
Ben Blume6a11052010-03-10 15:22:09 -08004485 /* success! */
4486 mutex_unlock(&cgroup_mutex);
4487 return 0;
4488}
4489EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4490
4491/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004492 * cgroup_unload_subsys: unload a modular subsystem
4493 * @ss: the subsystem to unload
4494 *
4495 * This function should be called in a modular subsystem's exitcall. When this
4496 * function is invoked, the refcount on the subsystem's module will be 0, so
4497 * the subsystem will not be attached to any hierarchy.
4498 */
4499void cgroup_unload_subsys(struct cgroup_subsys *ss)
4500{
4501 struct cg_cgroup_link *link;
4502 struct hlist_head *hhead;
4503
4504 BUG_ON(ss->module == NULL);
4505
4506 /*
4507 * we shouldn't be called if the subsystem is in use, and the use of
4508 * try_module_get in parse_cgroupfs_options should ensure that it
4509 * doesn't start being used while we're killing it off.
4510 */
4511 BUG_ON(ss->root != &rootnode);
4512
4513 mutex_lock(&cgroup_mutex);
4514 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004515 subsys[ss->subsys_id] = NULL;
4516
4517 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004518 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004519
4520 /*
4521 * disentangle the css from all css_sets attached to the dummytop. as
4522 * in loading, we need to pay our respects to the hashtable gods.
4523 */
4524 write_lock(&css_set_lock);
4525 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4526 struct css_set *cg = link->cg;
4527
4528 hlist_del(&cg->hlist);
4529 BUG_ON(!cg->subsys[ss->subsys_id]);
4530 cg->subsys[ss->subsys_id] = NULL;
4531 hhead = css_set_hash(cg->subsys);
4532 hlist_add_head(&cg->hlist, hhead);
4533 }
4534 write_unlock(&css_set_lock);
4535
4536 /*
4537 * remove subsystem's css from the dummytop and free it - need to free
4538 * before marking as null because ss->destroy needs the cgrp->subsys
4539 * pointer to find their state. note that this also takes care of
4540 * freeing the css_id.
4541 */
Li Zefan761b3ef2012-01-31 13:47:36 +08004542 ss->destroy(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004543 dummytop->subsys[ss->subsys_id] = NULL;
4544
4545 mutex_unlock(&cgroup_mutex);
4546}
4547EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4548
4549/**
Li Zefana043e3b2008-02-23 15:24:09 -08004550 * cgroup_init_early - cgroup initialization at system boot
4551 *
4552 * Initialize cgroups at system boot, and initialize any
4553 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004554 */
4555int __init cgroup_init_early(void)
4556{
4557 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004558 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004559 INIT_LIST_HEAD(&init_css_set.cg_links);
4560 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004561 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004562 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004563 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004564 root_count = 1;
4565 init_task.cgroups = &init_css_set;
4566
4567 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004568 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004569 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004570 &rootnode.top_cgroup.css_sets);
4571 list_add(&init_css_set_link.cg_link_list,
4572 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004573
Li Zefan472b1052008-04-29 01:00:11 -07004574 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4575 INIT_HLIST_HEAD(&css_set_table[i]);
4576
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004577 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004578 struct cgroup_subsys *ss = subsys[i];
4579
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004580 /* at bootup time, we don't worry about modular subsystems */
4581 if (!ss || ss->module)
4582 continue;
4583
Paul Menageddbcc7e2007-10-18 23:39:30 -07004584 BUG_ON(!ss->name);
4585 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
4586 BUG_ON(!ss->create);
4587 BUG_ON(!ss->destroy);
4588 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004589 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004590 ss->name, ss->subsys_id);
4591 BUG();
4592 }
4593
4594 if (ss->early_init)
4595 cgroup_init_subsys(ss);
4596 }
4597 return 0;
4598}
4599
4600/**
Li Zefana043e3b2008-02-23 15:24:09 -08004601 * cgroup_init - cgroup initialization
4602 *
4603 * Register cgroup filesystem and /proc file, and initialize
4604 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004605 */
4606int __init cgroup_init(void)
4607{
4608 int err;
4609 int i;
Li Zefan472b1052008-04-29 01:00:11 -07004610 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07004611
4612 err = bdi_init(&cgroup_backing_dev_info);
4613 if (err)
4614 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004615
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004616 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004617 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004618
4619 /* at bootup time, we don't worry about modular subsystems */
4620 if (!ss || ss->module)
4621 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004622 if (!ss->early_init)
4623 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004624 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004625 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004626 }
4627
Li Zefan472b1052008-04-29 01:00:11 -07004628 /* Add init_css_set to the hash table */
4629 hhead = css_set_hash(init_css_set.subsys);
4630 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004631 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004632
4633 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4634 if (!cgroup_kobj) {
4635 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004636 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004637 }
4638
4639 err = register_filesystem(&cgroup_fs_type);
4640 if (err < 0) {
4641 kobject_put(cgroup_kobj);
4642 goto out;
4643 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004644
Li Zefan46ae2202008-04-29 01:00:08 -07004645 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004646
Paul Menageddbcc7e2007-10-18 23:39:30 -07004647out:
Paul Menagea4243162007-10-18 23:39:35 -07004648 if (err)
4649 bdi_destroy(&cgroup_backing_dev_info);
4650
Paul Menageddbcc7e2007-10-18 23:39:30 -07004651 return err;
4652}
Paul Menageb4f48b62007-10-18 23:39:33 -07004653
Paul Menagea4243162007-10-18 23:39:35 -07004654/*
4655 * proc_cgroup_show()
4656 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4657 * - Used for /proc/<pid>/cgroup.
4658 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4659 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004660 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004661 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4662 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4663 * cgroup to top_cgroup.
4664 */
4665
4666/* TODO: Use a proper seq_file iterator */
4667static int proc_cgroup_show(struct seq_file *m, void *v)
4668{
4669 struct pid *pid;
4670 struct task_struct *tsk;
4671 char *buf;
4672 int retval;
4673 struct cgroupfs_root *root;
4674
4675 retval = -ENOMEM;
4676 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4677 if (!buf)
4678 goto out;
4679
4680 retval = -ESRCH;
4681 pid = m->private;
4682 tsk = get_pid_task(pid, PIDTYPE_PID);
4683 if (!tsk)
4684 goto out_free;
4685
4686 retval = 0;
4687
4688 mutex_lock(&cgroup_mutex);
4689
Li Zefane5f6a862009-01-07 18:07:41 -08004690 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004691 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004692 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004693 int count = 0;
4694
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004695 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004696 for_each_subsys(root, ss)
4697 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004698 if (strlen(root->name))
4699 seq_printf(m, "%sname=%s", count ? "," : "",
4700 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004701 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004702 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004703 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004704 if (retval < 0)
4705 goto out_unlock;
4706 seq_puts(m, buf);
4707 seq_putc(m, '\n');
4708 }
4709
4710out_unlock:
4711 mutex_unlock(&cgroup_mutex);
4712 put_task_struct(tsk);
4713out_free:
4714 kfree(buf);
4715out:
4716 return retval;
4717}
4718
4719static int cgroup_open(struct inode *inode, struct file *file)
4720{
4721 struct pid *pid = PROC_I(inode)->pid;
4722 return single_open(file, proc_cgroup_show, pid);
4723}
4724
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004725const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004726 .open = cgroup_open,
4727 .read = seq_read,
4728 .llseek = seq_lseek,
4729 .release = single_release,
4730};
4731
4732/* Display information about each subsystem and each hierarchy */
4733static int proc_cgroupstats_show(struct seq_file *m, void *v)
4734{
4735 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004736
Paul Menage8bab8dd2008-04-04 14:29:57 -07004737 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004738 /*
4739 * ideally we don't want subsystems moving around while we do this.
4740 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4741 * subsys/hierarchy state.
4742 */
Paul Menagea4243162007-10-18 23:39:35 -07004743 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004744 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4745 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004746 if (ss == NULL)
4747 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004748 seq_printf(m, "%s\t%d\t%d\t%d\n",
4749 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004750 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004751 }
4752 mutex_unlock(&cgroup_mutex);
4753 return 0;
4754}
4755
4756static int cgroupstats_open(struct inode *inode, struct file *file)
4757{
Al Viro9dce07f2008-03-29 03:07:28 +00004758 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004759}
4760
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004761static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004762 .open = cgroupstats_open,
4763 .read = seq_read,
4764 .llseek = seq_lseek,
4765 .release = single_release,
4766};
4767
Paul Menageb4f48b62007-10-18 23:39:33 -07004768/**
4769 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004770 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004771 *
4772 * Description: A task inherits its parent's cgroup at fork().
4773 *
4774 * A pointer to the shared css_set was automatically copied in
4775 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004776 * it was not made under the protection of RCU or cgroup_mutex, so
4777 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4778 * have already changed current->cgroups, allowing the previously
4779 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004780 *
4781 * At the point that cgroup_fork() is called, 'current' is the parent
4782 * task, and the passed argument 'child' points to the child task.
4783 */
4784void cgroup_fork(struct task_struct *child)
4785{
Tejun Heo9bb71302012-10-18 17:52:07 -07004786 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004787 child->cgroups = current->cgroups;
4788 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004789 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004790 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004791}
4792
4793/**
Li Zefana043e3b2008-02-23 15:24:09 -08004794 * cgroup_post_fork - called on a new task after adding it to the task list
4795 * @child: the task in question
4796 *
Tejun Heo5edee612012-10-16 15:03:14 -07004797 * Adds the task to the list running through its css_set if necessary and
4798 * call the subsystem fork() callbacks. Has to be after the task is
4799 * visible on the task list in case we race with the first call to
4800 * cgroup_iter_start() - to guarantee that the new task ends up on its
4801 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004802 */
Paul Menage817929e2007-10-18 23:39:36 -07004803void cgroup_post_fork(struct task_struct *child)
4804{
Tejun Heo5edee612012-10-16 15:03:14 -07004805 int i;
4806
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004807 /*
4808 * use_task_css_set_links is set to 1 before we walk the tasklist
4809 * under the tasklist_lock and we read it here after we added the child
4810 * to the tasklist under the tasklist_lock as well. If the child wasn't
4811 * yet in the tasklist when we walked through it from
4812 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4813 * should be visible now due to the paired locking and barriers implied
4814 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4815 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4816 * lock on fork.
4817 */
Paul Menage817929e2007-10-18 23:39:36 -07004818 if (use_task_css_set_links) {
4819 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004820 task_lock(child);
4821 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004822 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004823 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004824 write_unlock(&css_set_lock);
4825 }
Tejun Heo5edee612012-10-16 15:03:14 -07004826
4827 /*
4828 * Call ss->fork(). This must happen after @child is linked on
4829 * css_set; otherwise, @child might change state between ->fork()
4830 * and addition to css_set.
4831 */
4832 if (need_forkexit_callback) {
4833 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4834 struct cgroup_subsys *ss = subsys[i];
4835
4836 /*
4837 * fork/exit callbacks are supported only for
4838 * builtin subsystems and we don't need further
4839 * synchronization as they never go away.
4840 */
4841 if (!ss || ss->module)
4842 continue;
4843
4844 if (ss->fork)
4845 ss->fork(child);
4846 }
4847 }
Paul Menage817929e2007-10-18 23:39:36 -07004848}
Tejun Heo5edee612012-10-16 15:03:14 -07004849
Paul Menage817929e2007-10-18 23:39:36 -07004850/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004851 * cgroup_exit - detach cgroup from exiting task
4852 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004853 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004854 *
4855 * Description: Detach cgroup from @tsk and release it.
4856 *
4857 * Note that cgroups marked notify_on_release force every task in
4858 * them to take the global cgroup_mutex mutex when exiting.
4859 * This could impact scaling on very large systems. Be reluctant to
4860 * use notify_on_release cgroups where very high task exit scaling
4861 * is required on large systems.
4862 *
4863 * the_top_cgroup_hack:
4864 *
4865 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4866 *
4867 * We call cgroup_exit() while the task is still competent to
4868 * handle notify_on_release(), then leave the task attached to the
4869 * root cgroup in each hierarchy for the remainder of its exit.
4870 *
4871 * To do this properly, we would increment the reference count on
4872 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4873 * code we would add a second cgroup function call, to drop that
4874 * reference. This would just create an unnecessary hot spot on
4875 * the top_cgroup reference count, to no avail.
4876 *
4877 * Normally, holding a reference to a cgroup without bumping its
4878 * count is unsafe. The cgroup could go away, or someone could
4879 * attach us to a different cgroup, decrementing the count on
4880 * the first cgroup that we never incremented. But in this case,
4881 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004882 * which wards off any cgroup_attach_task() attempts, or task is a failed
4883 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004884 */
4885void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4886{
Paul Menage817929e2007-10-18 23:39:36 -07004887 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004888 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004889
4890 /*
4891 * Unlink from the css_set task list if necessary.
4892 * Optimistically check cg_list before taking
4893 * css_set_lock
4894 */
4895 if (!list_empty(&tsk->cg_list)) {
4896 write_lock(&css_set_lock);
4897 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004898 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004899 write_unlock(&css_set_lock);
4900 }
4901
Paul Menageb4f48b62007-10-18 23:39:33 -07004902 /* Reassign the task to the init_css_set. */
4903 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004904 cg = tsk->cgroups;
4905 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004906
4907 if (run_callbacks && need_forkexit_callback) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004908 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004909 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004910
4911 /* modular subsystems can't use callbacks */
4912 if (!ss || ss->module)
4913 continue;
4914
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004915 if (ss->exit) {
4916 struct cgroup *old_cgrp =
4917 rcu_dereference_raw(cg->subsys[i])->cgroup;
4918 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004919 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004920 }
4921 }
4922 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004923 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004924
Paul Menage817929e2007-10-18 23:39:36 -07004925 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004926 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004927}
Paul Menage697f4162007-10-18 23:39:34 -07004928
4929/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004930 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08004931 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004932 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08004933 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004934 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4935 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07004936 *
4937 * If we are sending in dummytop, then presumably we are creating
4938 * the top cgroup in the subsystem.
4939 *
4940 * Called only by the ns (nsproxy) cgroup.
4941 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004942int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07004943{
4944 int ret;
4945 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07004946
Paul Menagebd89aab2007-10-18 23:40:44 -07004947 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07004948 return 1;
4949
Paul Menage7717f7b2009-09-23 15:56:22 -07004950 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004951 while (cgrp != target && cgrp!= cgrp->top_cgroup)
4952 cgrp = cgrp->parent;
4953 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07004954 return ret;
4955}
Paul Menage81a6a5c2007-10-18 23:39:38 -07004956
Paul Menagebd89aab2007-10-18 23:40:44 -07004957static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004958{
4959 /* All of these checks rely on RCU to keep the cgroup
4960 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07004961 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
4962 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004963 /* Control Group is currently removeable. If it's not
4964 * already queued for a userspace notification, queue
4965 * it now */
4966 int need_schedule_work = 0;
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004967 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004968 if (!cgroup_is_removed(cgrp) &&
4969 list_empty(&cgrp->release_list)) {
4970 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004971 need_schedule_work = 1;
4972 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004973 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004974 if (need_schedule_work)
4975 schedule_work(&release_agent_work);
4976 }
4977}
4978
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004979/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07004980bool __css_tryget(struct cgroup_subsys_state *css)
4981{
Tejun Heoe9316082012-11-05 09:16:58 -08004982 while (true) {
4983 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004984
Tejun Heoe9316082012-11-05 09:16:58 -08004985 v = css_refcnt(css);
4986 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
4987 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07004988 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08004989 else if (t < 0)
4990 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07004991 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08004992 }
Tejun Heo28b4c272012-04-01 12:09:56 -07004993}
4994EXPORT_SYMBOL_GPL(__css_tryget);
4995
4996/* Caller must verify that the css is not for root cgroup */
4997void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004998{
Paul Menagebd89aab2007-10-18 23:40:44 -07004999 struct cgroup *cgrp = css->cgroup;
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005000 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005001
Paul Menage81a6a5c2007-10-18 23:39:38 -07005002 rcu_read_lock();
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005003 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
5004
5005 switch (v) {
Tejun Heo48ddbe12012-04-01 12:09:56 -07005006 case 1:
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005007 if (notify_on_release(cgrp)) {
5008 set_bit(CGRP_RELEASABLE, &cgrp->flags);
5009 check_for_release(cgrp);
5010 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07005011 break;
5012 case 0:
Tejun Heoed9577932012-11-05 09:16:58 -08005013 schedule_work(&css->dput_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005014 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005015 }
5016 rcu_read_unlock();
5017}
Ben Blum67523c42010-03-10 15:22:11 -08005018EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005019
5020/*
5021 * Notify userspace when a cgroup is released, by running the
5022 * configured release agent with the name of the cgroup (path
5023 * relative to the root of cgroup file system) as the argument.
5024 *
5025 * Most likely, this user command will try to rmdir this cgroup.
5026 *
5027 * This races with the possibility that some other task will be
5028 * attached to this cgroup before it is removed, or that some other
5029 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5030 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5031 * unused, and this cgroup will be reprieved from its death sentence,
5032 * to continue to serve a useful existence. Next time it's released,
5033 * we will get notified again, if it still has 'notify_on_release' set.
5034 *
5035 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5036 * means only wait until the task is successfully execve()'d. The
5037 * separate release agent task is forked by call_usermodehelper(),
5038 * then control in this thread returns here, without waiting for the
5039 * release agent task. We don't bother to wait because the caller of
5040 * this routine has no use for the exit status of the release agent
5041 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005042 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005043static void cgroup_release_agent(struct work_struct *work)
5044{
5045 BUG_ON(work != &release_agent_work);
5046 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005047 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005048 while (!list_empty(&release_list)) {
5049 char *argv[3], *envp[3];
5050 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005051 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005052 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005053 struct cgroup,
5054 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005055 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005056 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005057 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005058 if (!pathbuf)
5059 goto continue_free;
5060 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5061 goto continue_free;
5062 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5063 if (!agentbuf)
5064 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005065
5066 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005067 argv[i++] = agentbuf;
5068 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005069 argv[i] = NULL;
5070
5071 i = 0;
5072 /* minimal command environment */
5073 envp[i++] = "HOME=/";
5074 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5075 envp[i] = NULL;
5076
5077 /* Drop the lock while we invoke the usermode helper,
5078 * since the exec could involve hitting disk and hence
5079 * be a slow process */
5080 mutex_unlock(&cgroup_mutex);
5081 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005082 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005083 continue_free:
5084 kfree(pathbuf);
5085 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005086 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005087 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005088 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005089 mutex_unlock(&cgroup_mutex);
5090}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005091
5092static int __init cgroup_disable(char *str)
5093{
5094 int i;
5095 char *token;
5096
5097 while ((token = strsep(&str, ",")) != NULL) {
5098 if (!*token)
5099 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005100 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005101 struct cgroup_subsys *ss = subsys[i];
5102
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005103 /*
5104 * cgroup_disable, being at boot time, can't
5105 * know about module subsystems, so we don't
5106 * worry about them.
5107 */
5108 if (!ss || ss->module)
5109 continue;
5110
Paul Menage8bab8dd2008-04-04 14:29:57 -07005111 if (!strcmp(token, ss->name)) {
5112 ss->disabled = 1;
5113 printk(KERN_INFO "Disabling %s control group"
5114 " subsystem\n", ss->name);
5115 break;
5116 }
5117 }
5118 }
5119 return 1;
5120}
5121__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005122
5123/*
5124 * Functons for CSS ID.
5125 */
5126
5127/*
5128 *To get ID other than 0, this should be called when !cgroup_is_removed().
5129 */
5130unsigned short css_id(struct cgroup_subsys_state *css)
5131{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005132 struct css_id *cssid;
5133
5134 /*
5135 * This css_id() can return correct value when somone has refcnt
5136 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5137 * it's unchanged until freed.
5138 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005139 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005140
5141 if (cssid)
5142 return cssid->id;
5143 return 0;
5144}
Ben Blum67523c42010-03-10 15:22:11 -08005145EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005146
5147unsigned short css_depth(struct cgroup_subsys_state *css)
5148{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005149 struct css_id *cssid;
5150
Tejun Heo28b4c272012-04-01 12:09:56 -07005151 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005152
5153 if (cssid)
5154 return cssid->depth;
5155 return 0;
5156}
Ben Blum67523c42010-03-10 15:22:11 -08005157EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005158
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005159/**
5160 * css_is_ancestor - test "root" css is an ancestor of "child"
5161 * @child: the css to be tested.
5162 * @root: the css supporsed to be an ancestor of the child.
5163 *
5164 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005165 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005166 * But, considering usual usage, the csses should be valid objects after test.
5167 * Assuming that the caller will do some action to the child if this returns
5168 * returns true, the caller must take "child";s reference count.
5169 * If "child" is valid object and this returns true, "root" is valid, too.
5170 */
5171
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005172bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005173 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005174{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005175 struct css_id *child_id;
5176 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005177
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005178 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005179 if (!child_id)
5180 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005181 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005182 if (!root_id)
5183 return false;
5184 if (child_id->depth < root_id->depth)
5185 return false;
5186 if (child_id->stack[root_id->depth] != root_id->id)
5187 return false;
5188 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005189}
5190
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005191void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5192{
5193 struct css_id *id = css->id;
5194 /* When this is called before css_id initialization, id can be NULL */
5195 if (!id)
5196 return;
5197
5198 BUG_ON(!ss->use_id);
5199
5200 rcu_assign_pointer(id->css, NULL);
5201 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005202 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005203 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005204 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005205 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005206}
Ben Blum67523c42010-03-10 15:22:11 -08005207EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005208
5209/*
5210 * This is called by init or create(). Then, calls to this function are
5211 * always serialized (By cgroup_mutex() at create()).
5212 */
5213
5214static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5215{
5216 struct css_id *newid;
5217 int myid, error, size;
5218
5219 BUG_ON(!ss->use_id);
5220
5221 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5222 newid = kzalloc(size, GFP_KERNEL);
5223 if (!newid)
5224 return ERR_PTR(-ENOMEM);
5225 /* get id */
5226 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
5227 error = -ENOMEM;
5228 goto err_out;
5229 }
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005230 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005231 /* Don't use 0. allocates an ID of 1-65535 */
5232 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005233 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005234
5235 /* Returns error when there are no free spaces for new ID.*/
5236 if (error) {
5237 error = -ENOSPC;
5238 goto err_out;
5239 }
5240 if (myid > CSS_ID_MAX)
5241 goto remove_idr;
5242
5243 newid->id = myid;
5244 newid->depth = depth;
5245 return newid;
5246remove_idr:
5247 error = -ENOSPC;
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005248 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005249 idr_remove(&ss->idr, myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005250 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005251err_out:
5252 kfree(newid);
5253 return ERR_PTR(error);
5254
5255}
5256
Ben Blume6a11052010-03-10 15:22:09 -08005257static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5258 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005259{
5260 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005261
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005262 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005263 idr_init(&ss->idr);
5264
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005265 newid = get_new_cssid(ss, 0);
5266 if (IS_ERR(newid))
5267 return PTR_ERR(newid);
5268
5269 newid->stack[0] = newid->id;
5270 newid->css = rootcss;
5271 rootcss->id = newid;
5272 return 0;
5273}
5274
5275static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5276 struct cgroup *child)
5277{
5278 int subsys_id, i, depth = 0;
5279 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005280 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005281
5282 subsys_id = ss->subsys_id;
5283 parent_css = parent->subsys[subsys_id];
5284 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005285 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005286 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005287
5288 child_id = get_new_cssid(ss, depth);
5289 if (IS_ERR(child_id))
5290 return PTR_ERR(child_id);
5291
5292 for (i = 0; i < depth; i++)
5293 child_id->stack[i] = parent_id->stack[i];
5294 child_id->stack[depth] = child_id->id;
5295 /*
5296 * child_id->css pointer will be set after this cgroup is available
5297 * see cgroup_populate_dir()
5298 */
5299 rcu_assign_pointer(child_css->id, child_id);
5300
5301 return 0;
5302}
5303
5304/**
5305 * css_lookup - lookup css by id
5306 * @ss: cgroup subsys to be looked into.
5307 * @id: the id
5308 *
5309 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5310 * NULL if not. Should be called under rcu_read_lock()
5311 */
5312struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5313{
5314 struct css_id *cssid = NULL;
5315
5316 BUG_ON(!ss->use_id);
5317 cssid = idr_find(&ss->idr, id);
5318
5319 if (unlikely(!cssid))
5320 return NULL;
5321
5322 return rcu_dereference(cssid->css);
5323}
Ben Blum67523c42010-03-10 15:22:11 -08005324EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005325
5326/**
5327 * css_get_next - lookup next cgroup under specified hierarchy.
5328 * @ss: pointer to subsystem
5329 * @id: current position of iteration.
5330 * @root: pointer to css. search tree under this.
5331 * @foundid: position of found object.
5332 *
5333 * Search next css under the specified hierarchy of rootid. Calling under
5334 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5335 */
5336struct cgroup_subsys_state *
5337css_get_next(struct cgroup_subsys *ss, int id,
5338 struct cgroup_subsys_state *root, int *foundid)
5339{
5340 struct cgroup_subsys_state *ret = NULL;
5341 struct css_id *tmp;
5342 int tmpid;
5343 int rootid = css_id(root);
5344 int depth = css_depth(root);
5345
5346 if (!rootid)
5347 return NULL;
5348
5349 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005350 WARN_ON_ONCE(!rcu_read_lock_held());
5351
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005352 /* fill start point for scan */
5353 tmpid = id;
5354 while (1) {
5355 /*
5356 * scan next entry from bitmap(tree), tmpid is updated after
5357 * idr_get_next().
5358 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005359 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005360 if (!tmp)
5361 break;
5362 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5363 ret = rcu_dereference(tmp->css);
5364 if (ret) {
5365 *foundid = tmpid;
5366 break;
5367 }
5368 }
5369 /* continue to scan from next id */
5370 tmpid = tmpid + 1;
5371 }
5372 return ret;
5373}
5374
Stephane Eraniane5d13672011-02-14 11:20:01 +02005375/*
5376 * get corresponding css from file open on cgroupfs directory
5377 */
5378struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5379{
5380 struct cgroup *cgrp;
5381 struct inode *inode;
5382 struct cgroup_subsys_state *css;
5383
5384 inode = f->f_dentry->d_inode;
5385 /* check in cgroup filesystem dir */
5386 if (inode->i_op != &cgroup_dir_inode_operations)
5387 return ERR_PTR(-EBADF);
5388
5389 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5390 return ERR_PTR(-EINVAL);
5391
5392 /* get cgroup */
5393 cgrp = __d_cgrp(f->f_dentry);
5394 css = cgrp->subsys[id];
5395 return css ? css : ERR_PTR(-ENOENT);
5396}
5397
Paul Menagefe693432009-09-23 15:56:20 -07005398#ifdef CONFIG_CGROUP_DEBUG
Li Zefan761b3ef2012-01-31 13:47:36 +08005399static struct cgroup_subsys_state *debug_create(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005400{
5401 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5402
5403 if (!css)
5404 return ERR_PTR(-ENOMEM);
5405
5406 return css;
5407}
5408
Li Zefan761b3ef2012-01-31 13:47:36 +08005409static void debug_destroy(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005410{
5411 kfree(cont->subsys[debug_subsys_id]);
5412}
5413
5414static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5415{
5416 return atomic_read(&cont->count);
5417}
5418
5419static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5420{
5421 return cgroup_task_count(cont);
5422}
5423
5424static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5425{
5426 return (u64)(unsigned long)current->cgroups;
5427}
5428
5429static u64 current_css_set_refcount_read(struct cgroup *cont,
5430 struct cftype *cft)
5431{
5432 u64 count;
5433
5434 rcu_read_lock();
5435 count = atomic_read(&current->cgroups->refcount);
5436 rcu_read_unlock();
5437 return count;
5438}
5439
Paul Menage7717f7b2009-09-23 15:56:22 -07005440static int current_css_set_cg_links_read(struct cgroup *cont,
5441 struct cftype *cft,
5442 struct seq_file *seq)
5443{
5444 struct cg_cgroup_link *link;
5445 struct css_set *cg;
5446
5447 read_lock(&css_set_lock);
5448 rcu_read_lock();
5449 cg = rcu_dereference(current->cgroups);
5450 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5451 struct cgroup *c = link->cgrp;
5452 const char *name;
5453
5454 if (c->dentry)
5455 name = c->dentry->d_name.name;
5456 else
5457 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005458 seq_printf(seq, "Root %d group %s\n",
5459 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005460 }
5461 rcu_read_unlock();
5462 read_unlock(&css_set_lock);
5463 return 0;
5464}
5465
5466#define MAX_TASKS_SHOWN_PER_CSS 25
5467static int cgroup_css_links_read(struct cgroup *cont,
5468 struct cftype *cft,
5469 struct seq_file *seq)
5470{
5471 struct cg_cgroup_link *link;
5472
5473 read_lock(&css_set_lock);
5474 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5475 struct css_set *cg = link->cg;
5476 struct task_struct *task;
5477 int count = 0;
5478 seq_printf(seq, "css_set %p\n", cg);
5479 list_for_each_entry(task, &cg->tasks, cg_list) {
5480 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5481 seq_puts(seq, " ...\n");
5482 break;
5483 } else {
5484 seq_printf(seq, " task %d\n",
5485 task_pid_vnr(task));
5486 }
5487 }
5488 }
5489 read_unlock(&css_set_lock);
5490 return 0;
5491}
5492
Paul Menagefe693432009-09-23 15:56:20 -07005493static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5494{
5495 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5496}
5497
5498static struct cftype debug_files[] = {
5499 {
5500 .name = "cgroup_refcount",
5501 .read_u64 = cgroup_refcount_read,
5502 },
5503 {
5504 .name = "taskcount",
5505 .read_u64 = debug_taskcount_read,
5506 },
5507
5508 {
5509 .name = "current_css_set",
5510 .read_u64 = current_css_set_read,
5511 },
5512
5513 {
5514 .name = "current_css_set_refcount",
5515 .read_u64 = current_css_set_refcount_read,
5516 },
5517
5518 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005519 .name = "current_css_set_cg_links",
5520 .read_seq_string = current_css_set_cg_links_read,
5521 },
5522
5523 {
5524 .name = "cgroup_css_links",
5525 .read_seq_string = cgroup_css_links_read,
5526 },
5527
5528 {
Paul Menagefe693432009-09-23 15:56:20 -07005529 .name = "releasable",
5530 .read_u64 = releasable_read,
5531 },
Paul Menagefe693432009-09-23 15:56:20 -07005532
Tejun Heo4baf6e32012-04-01 12:09:55 -07005533 { } /* terminate */
5534};
Paul Menagefe693432009-09-23 15:56:20 -07005535
5536struct cgroup_subsys debug_subsys = {
5537 .name = "debug",
5538 .create = debug_create,
5539 .destroy = debug_destroy,
Paul Menagefe693432009-09-23 15:56:20 -07005540 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005541 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005542};
5543#endif /* CONFIG_CGROUP_DEBUG */