blob: c53f42e317043b5f57215301b771ae324f954c2a [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
Tejun Heo42809dd2012-11-19 08:13:37 -0800245static int cgroup_destroy_locked(struct cgroup *cgrp);
246
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800247#ifdef CONFIG_PROVE_LOCKING
248int cgroup_lock_is_held(void)
249{
250 return lockdep_is_held(&cgroup_mutex);
251}
252#else /* #ifdef CONFIG_PROVE_LOCKING */
253int cgroup_lock_is_held(void)
254{
255 return mutex_is_locked(&cgroup_mutex);
256}
257#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
258
259EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
260
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700261static int css_unbias_refcnt(int refcnt)
262{
263 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
264}
265
Tejun Heo28b4c272012-04-01 12:09:56 -0700266/* the current nr of refs, always >= 0 whether @css is deactivated or not */
267static int css_refcnt(struct cgroup_subsys_state *css)
268{
269 int v = atomic_read(&css->refcnt);
270
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700271 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700272}
273
Paul Menageddbcc7e2007-10-18 23:39:30 -0700274/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700275inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700276{
Paul Menagebd89aab2007-10-18 23:40:44 -0700277 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700278}
279
280/* bits in struct cgroupfs_root flags field */
281enum {
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400282 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
283 ROOT_XATTR, /* supports extended attributes */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700284};
285
Adrian Bunke9685a02008-02-07 00:13:46 -0800286static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700287{
288 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700289 (1 << CGRP_RELEASABLE) |
290 (1 << CGRP_NOTIFY_ON_RELEASE);
291 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700292}
293
Adrian Bunke9685a02008-02-07 00:13:46 -0800294static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700295{
Paul Menagebd89aab2007-10-18 23:40:44 -0700296 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700297}
298
Daniel Lezcano97978e62010-10-27 15:33:35 -0700299static int clone_children(const struct cgroup *cgrp)
300{
301 return test_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
302}
303
Paul Menageddbcc7e2007-10-18 23:39:30 -0700304/*
305 * for_each_subsys() allows you to iterate on each subsystem attached to
306 * an active hierarchy
307 */
308#define for_each_subsys(_root, _ss) \
309list_for_each_entry(_ss, &_root->subsys_list, sibling)
310
Li Zefane5f6a862009-01-07 18:07:41 -0800311/* for_each_active_root() allows you to iterate across the active hierarchies */
312#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700313list_for_each_entry(_root, &roots, root_list)
314
Tejun Heof6ea9372012-04-01 12:09:55 -0700315static inline struct cgroup *__d_cgrp(struct dentry *dentry)
316{
317 return dentry->d_fsdata;
318}
319
Tejun Heo05ef1d72012-04-01 12:09:56 -0700320static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700321{
322 return dentry->d_fsdata;
323}
324
Tejun Heo05ef1d72012-04-01 12:09:56 -0700325static inline struct cftype *__d_cft(struct dentry *dentry)
326{
327 return __d_cfe(dentry)->type;
328}
329
Paul Menage81a6a5c2007-10-18 23:39:38 -0700330/* the list of cgroups eligible for automatic release. Protected by
331 * release_list_lock */
332static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200333static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700334static void cgroup_release_agent(struct work_struct *work);
335static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700336static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700337
Paul Menage817929e2007-10-18 23:39:36 -0700338/* Link structure for associating css_set objects with cgroups */
339struct cg_cgroup_link {
340 /*
341 * List running through cg_cgroup_links associated with a
342 * cgroup, anchored on cgroup->css_sets
343 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700344 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700345 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700346 /*
347 * List running through cg_cgroup_links pointing at a
348 * single css_set object, anchored on css_set->cg_links
349 */
350 struct list_head cg_link_list;
351 struct css_set *cg;
352};
353
354/* The default css_set - used by init and its children prior to any
355 * hierarchies being mounted. It contains a pointer to the root state
356 * for each subsystem. Also used to anchor the list of css_sets. Not
357 * reference-counted, to improve performance when child cgroups
358 * haven't been created.
359 */
360
361static struct css_set init_css_set;
362static struct cg_cgroup_link init_css_set_link;
363
Ben Blume6a11052010-03-10 15:22:09 -0800364static int cgroup_init_idr(struct cgroup_subsys *ss,
365 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700366
Paul Menage817929e2007-10-18 23:39:36 -0700367/* css_set_lock protects the list of css_set objects, and the
368 * chain of tasks off each css_set. Nests outside task->alloc_lock
369 * due to cgroup_iter_start() */
370static DEFINE_RWLOCK(css_set_lock);
371static int css_set_count;
372
Paul Menage7717f7b2009-09-23 15:56:22 -0700373/*
374 * hash table for cgroup groups. This improves the performance to find
375 * an existing css_set. This hash doesn't (currently) take into
376 * account cgroups in empty hierarchies.
377 */
Li Zefan472b1052008-04-29 01:00:11 -0700378#define CSS_SET_HASH_BITS 7
379#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
380static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
381
382static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
383{
384 int i;
385 int index;
386 unsigned long tmp = 0UL;
387
388 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
389 tmp += (unsigned long)css[i];
390 tmp = (tmp >> 16) ^ tmp;
391
392 index = hash_long(tmp, CSS_SET_HASH_BITS);
393
394 return &css_set_table[index];
395}
396
Paul Menage817929e2007-10-18 23:39:36 -0700397/* We don't maintain the lists running through each css_set to its
398 * task until after the first call to cgroup_iter_start(). This
399 * reduces the fork()/exit() overhead for people who have cgroups
400 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700401static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700402
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700403static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700404{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700405 struct cg_cgroup_link *link;
406 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700407 /*
408 * Ensure that the refcount doesn't hit zero while any readers
409 * can see it. Similar to atomic_dec_and_lock(), but for an
410 * rwlock
411 */
412 if (atomic_add_unless(&cg->refcount, -1, 1))
413 return;
414 write_lock(&css_set_lock);
415 if (!atomic_dec_and_test(&cg->refcount)) {
416 write_unlock(&css_set_lock);
417 return;
418 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700419
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420 /* This css_set is dead. unlink it and release cgroup refcounts */
421 hlist_del(&cg->hlist);
422 css_set_count--;
423
424 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
425 cg_link_list) {
426 struct cgroup *cgrp = link->cgrp;
427 list_del(&link->cg_link_list);
428 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700429 if (atomic_dec_and_test(&cgrp->count) &&
430 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700431 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700432 set_bit(CGRP_RELEASABLE, &cgrp->flags);
433 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700434 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700435
436 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700437 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700438
439 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800440 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700441}
442
443/*
444 * refcounted get/put for css_set objects
445 */
446static inline void get_css_set(struct css_set *cg)
447{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700448 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700449}
450
451static inline void put_css_set(struct css_set *cg)
452{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700453 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700454}
455
Paul Menage81a6a5c2007-10-18 23:39:38 -0700456static inline void put_css_set_taskexit(struct css_set *cg)
457{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700458 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700459}
460
Paul Menage817929e2007-10-18 23:39:36 -0700461/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700462 * compare_css_sets - helper function for find_existing_css_set().
463 * @cg: candidate css_set being tested
464 * @old_cg: existing css_set for a task
465 * @new_cgrp: cgroup that's being entered by the task
466 * @template: desired set of css pointers in css_set (pre-calculated)
467 *
468 * Returns true if "cg" matches "old_cg" except for the hierarchy
469 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
470 */
471static bool compare_css_sets(struct css_set *cg,
472 struct css_set *old_cg,
473 struct cgroup *new_cgrp,
474 struct cgroup_subsys_state *template[])
475{
476 struct list_head *l1, *l2;
477
478 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
479 /* Not all subsystems matched */
480 return false;
481 }
482
483 /*
484 * Compare cgroup pointers in order to distinguish between
485 * different cgroups in heirarchies with no subsystems. We
486 * could get by with just this check alone (and skip the
487 * memcmp above) but on most setups the memcmp check will
488 * avoid the need for this more expensive check on almost all
489 * candidates.
490 */
491
492 l1 = &cg->cg_links;
493 l2 = &old_cg->cg_links;
494 while (1) {
495 struct cg_cgroup_link *cgl1, *cgl2;
496 struct cgroup *cg1, *cg2;
497
498 l1 = l1->next;
499 l2 = l2->next;
500 /* See if we reached the end - both lists are equal length. */
501 if (l1 == &cg->cg_links) {
502 BUG_ON(l2 != &old_cg->cg_links);
503 break;
504 } else {
505 BUG_ON(l2 == &old_cg->cg_links);
506 }
507 /* Locate the cgroups associated with these links. */
508 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
509 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
510 cg1 = cgl1->cgrp;
511 cg2 = cgl2->cgrp;
512 /* Hierarchies should be linked in the same order. */
513 BUG_ON(cg1->root != cg2->root);
514
515 /*
516 * If this hierarchy is the hierarchy of the cgroup
517 * that's changing, then we need to check that this
518 * css_set points to the new cgroup; if it's any other
519 * hierarchy, then this css_set should point to the
520 * same cgroup as the old css_set.
521 */
522 if (cg1->root == new_cgrp->root) {
523 if (cg1 != new_cgrp)
524 return false;
525 } else {
526 if (cg1 != cg2)
527 return false;
528 }
529 }
530 return true;
531}
532
533/*
Paul Menage817929e2007-10-18 23:39:36 -0700534 * find_existing_css_set() is a helper for
535 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700536 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700537 *
538 * oldcg: the cgroup group that we're using before the cgroup
539 * transition
540 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700541 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700542 *
543 * template: location in which to build the desired set of subsystem
544 * state objects for the new cgroup group
545 */
Paul Menage817929e2007-10-18 23:39:36 -0700546static struct css_set *find_existing_css_set(
547 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700548 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700549 struct cgroup_subsys_state *template[])
550{
551 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700552 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700553 struct hlist_head *hhead;
554 struct hlist_node *node;
555 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700556
Ben Blumaae8aab2010-03-10 15:22:07 -0800557 /*
558 * Build the set of subsystem state objects that we want to see in the
559 * new css_set. while subsystems can change globally, the entries here
560 * won't change, so no need for locking.
561 */
Paul Menage817929e2007-10-18 23:39:36 -0700562 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400563 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700564 /* Subsystem is in this hierarchy. So we want
565 * the subsystem state from the new
566 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700567 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700568 } else {
569 /* Subsystem is not in this hierarchy, so we
570 * don't want to change the subsystem state */
571 template[i] = oldcg->subsys[i];
572 }
573 }
574
Li Zefan472b1052008-04-29 01:00:11 -0700575 hhead = css_set_hash(template);
576 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700577 if (!compare_css_sets(cg, oldcg, cgrp, template))
578 continue;
579
580 /* This css_set matches what we need */
581 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700582 }
Paul Menage817929e2007-10-18 23:39:36 -0700583
584 /* No existing cgroup group matched */
585 return NULL;
586}
587
Paul Menage817929e2007-10-18 23:39:36 -0700588static void free_cg_links(struct list_head *tmp)
589{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700590 struct cg_cgroup_link *link;
591 struct cg_cgroup_link *saved_link;
592
593 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700594 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700595 kfree(link);
596 }
597}
598
599/*
Li Zefan36553432008-07-29 22:33:19 -0700600 * allocate_cg_links() allocates "count" cg_cgroup_link structures
601 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
602 * success or a negative error
603 */
604static int allocate_cg_links(int count, struct list_head *tmp)
605{
606 struct cg_cgroup_link *link;
607 int i;
608 INIT_LIST_HEAD(tmp);
609 for (i = 0; i < count; i++) {
610 link = kmalloc(sizeof(*link), GFP_KERNEL);
611 if (!link) {
612 free_cg_links(tmp);
613 return -ENOMEM;
614 }
615 list_add(&link->cgrp_link_list, tmp);
616 }
617 return 0;
618}
619
Li Zefanc12f65d2009-01-07 18:07:42 -0800620/**
621 * link_css_set - a helper function to link a css_set to a cgroup
622 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
623 * @cg: the css_set to be linked
624 * @cgrp: the destination cgroup
625 */
626static void link_css_set(struct list_head *tmp_cg_links,
627 struct css_set *cg, struct cgroup *cgrp)
628{
629 struct cg_cgroup_link *link;
630
631 BUG_ON(list_empty(tmp_cg_links));
632 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
633 cgrp_link_list);
634 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700635 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700636 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800637 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700638 /*
639 * Always add links to the tail of the list so that the list
640 * is sorted by order of hierarchy creation
641 */
642 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800643}
644
Li Zefan36553432008-07-29 22:33:19 -0700645/*
Paul Menage817929e2007-10-18 23:39:36 -0700646 * find_css_set() takes an existing cgroup group and a
647 * cgroup object, and returns a css_set object that's
648 * equivalent to the old group, but with the given cgroup
649 * substituted into the appropriate hierarchy. Must be called with
650 * cgroup_mutex held
651 */
Paul Menage817929e2007-10-18 23:39:36 -0700652static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700653 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700654{
655 struct css_set *res;
656 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700657
658 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700659
Li Zefan472b1052008-04-29 01:00:11 -0700660 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700661 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700662
Paul Menage817929e2007-10-18 23:39:36 -0700663 /* First see if we already have a cgroup group that matches
664 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700665 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700666 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700667 if (res)
668 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700669 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700670
671 if (res)
672 return res;
673
674 res = kmalloc(sizeof(*res), GFP_KERNEL);
675 if (!res)
676 return NULL;
677
678 /* Allocate all the cg_cgroup_link objects that we'll need */
679 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
680 kfree(res);
681 return NULL;
682 }
683
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700684 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700685 INIT_LIST_HEAD(&res->cg_links);
686 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700687 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700688
689 /* Copy the set of subsystem state objects generated in
690 * find_existing_css_set() */
691 memcpy(res->subsys, template, sizeof(res->subsys));
692
693 write_lock(&css_set_lock);
694 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700695 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
696 struct cgroup *c = link->cgrp;
697 if (c->root == cgrp->root)
698 c = cgrp;
699 link_css_set(&tmp_cg_links, res, c);
700 }
Paul Menage817929e2007-10-18 23:39:36 -0700701
702 BUG_ON(!list_empty(&tmp_cg_links));
703
Paul Menage817929e2007-10-18 23:39:36 -0700704 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700705
706 /* Add this cgroup group to the hash table */
707 hhead = css_set_hash(res->subsys);
708 hlist_add_head(&res->hlist, hhead);
709
Paul Menage817929e2007-10-18 23:39:36 -0700710 write_unlock(&css_set_lock);
711
712 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700713}
714
Paul Menageddbcc7e2007-10-18 23:39:30 -0700715/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700716 * Return the cgroup for "task" from the given hierarchy. Must be
717 * called with cgroup_mutex held.
718 */
719static struct cgroup *task_cgroup_from_root(struct task_struct *task,
720 struct cgroupfs_root *root)
721{
722 struct css_set *css;
723 struct cgroup *res = NULL;
724
725 BUG_ON(!mutex_is_locked(&cgroup_mutex));
726 read_lock(&css_set_lock);
727 /*
728 * No need to lock the task - since we hold cgroup_mutex the
729 * task can't change groups, so the only thing that can happen
730 * is that it exits and its css is set back to init_css_set.
731 */
732 css = task->cgroups;
733 if (css == &init_css_set) {
734 res = &root->top_cgroup;
735 } else {
736 struct cg_cgroup_link *link;
737 list_for_each_entry(link, &css->cg_links, cg_link_list) {
738 struct cgroup *c = link->cgrp;
739 if (c->root == root) {
740 res = c;
741 break;
742 }
743 }
744 }
745 read_unlock(&css_set_lock);
746 BUG_ON(!res);
747 return res;
748}
749
750/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700751 * There is one global cgroup mutex. We also require taking
752 * task_lock() when dereferencing a task's cgroup subsys pointers.
753 * See "The task_lock() exception", at the end of this comment.
754 *
755 * A task must hold cgroup_mutex to modify cgroups.
756 *
757 * Any task can increment and decrement the count field without lock.
758 * So in general, code holding cgroup_mutex can't rely on the count
759 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800760 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700761 * means that no tasks are currently attached, therefore there is no
762 * way a task attached to that cgroup can fork (the other way to
763 * increment the count). So code holding cgroup_mutex can safely
764 * assume that if the count is zero, it will stay zero. Similarly, if
765 * a task holds cgroup_mutex on a cgroup with zero count, it
766 * knows that the cgroup won't be removed, as cgroup_rmdir()
767 * needs that mutex.
768 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700769 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
770 * (usually) take cgroup_mutex. These are the two most performance
771 * critical pieces of code here. The exception occurs on cgroup_exit(),
772 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
773 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800774 * to the release agent with the name of the cgroup (path relative to
775 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700776 *
777 * A cgroup can only be deleted if both its 'count' of using tasks
778 * is zero, and its list of 'children' cgroups is empty. Since all
779 * tasks in the system use _some_ cgroup, and since there is always at
780 * least one task in the system (init, pid == 1), therefore, top_cgroup
781 * always has either children cgroups and/or using tasks. So we don't
782 * need a special hack to ensure that top_cgroup cannot be deleted.
783 *
784 * The task_lock() exception
785 *
786 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800787 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800788 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700789 * several performance critical places that need to reference
790 * task->cgroup without the expense of grabbing a system global
791 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800792 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
794 * the task_struct routinely used for such matters.
795 *
796 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800797 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798 */
799
Paul Menageddbcc7e2007-10-18 23:39:30 -0700800/**
801 * cgroup_lock - lock out any changes to cgroup structures
802 *
803 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700804void cgroup_lock(void)
805{
806 mutex_lock(&cgroup_mutex);
807}
Ben Blum67523c42010-03-10 15:22:11 -0800808EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809
810/**
811 * cgroup_unlock - release lock on cgroup changes
812 *
813 * Undo the lock taken in a previous cgroup_lock() call.
814 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815void cgroup_unlock(void)
816{
817 mutex_unlock(&cgroup_mutex);
818}
Ben Blum67523c42010-03-10 15:22:11 -0800819EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700820
821/*
822 * A couple of forward declarations required, due to cyclic reference loop:
823 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
824 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
825 * -> cgroup_mkdir.
826 */
827
Al Viro18bb1db2011-07-26 01:41:39 -0400828static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400829static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700830static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400831static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
832 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700833static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700834static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700835
836static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200837 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700838 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700839};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700840
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700841static int alloc_css_id(struct cgroup_subsys *ss,
842 struct cgroup *parent, struct cgroup *child);
843
Al Viroa5e7ed32011-07-26 01:55:55 -0400844static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700845{
846 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700847
848 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400849 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700850 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100851 inode->i_uid = current_fsuid();
852 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700853 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
854 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
855 }
856 return inode;
857}
858
859static void cgroup_diput(struct dentry *dentry, struct inode *inode)
860{
861 /* is dentry a directory ? if so, kfree() associated cgroup */
862 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700863 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800864 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700865 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700866 /* It's possible for external users to be holding css
867 * reference counts on a cgroup; css_put() needs to
868 * be able to access the cgroup after decrementing
869 * the reference count in order to know if it needs to
870 * queue the cgroup to be handled by the release
871 * agent */
872 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800873
874 mutex_lock(&cgroup_mutex);
875 /*
876 * Release the subsystem state objects.
877 */
Li Zefan75139b82009-01-07 18:07:33 -0800878 for_each_subsys(cgrp->root, ss)
Li Zefan761b3ef2012-01-31 13:47:36 +0800879 ss->destroy(cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800880
881 cgrp->root->number_of_cgroups--;
882 mutex_unlock(&cgroup_mutex);
883
Paul Menagea47295e2009-01-07 18:07:44 -0800884 /*
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700885 * Drop the active superblock reference that we took when we
886 * created the cgroup
Paul Menagea47295e2009-01-07 18:07:44 -0800887 */
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700888 deactivate_super(cgrp->root->sb);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800889
Ben Blum72a8cb32009-09-23 15:56:27 -0700890 /*
891 * if we're getting rid of the cgroup, refcount should ensure
892 * that there are no pidlists left.
893 */
894 BUG_ON(!list_empty(&cgrp->pidlists));
895
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400896 simple_xattrs_free(&cgrp->xattrs);
897
Lai Jiangshanf2da1c42011-03-15 17:55:16 +0800898 kfree_rcu(cgrp, rcu_head);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700899 } else {
900 struct cfent *cfe = __d_cfe(dentry);
901 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400902 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700903
904 WARN_ONCE(!list_empty(&cfe->node) &&
905 cgrp != &cgrp->root->top_cgroup,
906 "cfe still linked for %s\n", cfe->type->name);
907 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400908 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700909 }
910 iput(inode);
911}
912
Al Viroc72a04e2011-01-14 05:31:45 +0000913static int cgroup_delete(const struct dentry *d)
914{
915 return 1;
916}
917
Paul Menageddbcc7e2007-10-18 23:39:30 -0700918static void remove_dir(struct dentry *d)
919{
920 struct dentry *parent = dget(d->d_parent);
921
922 d_delete(d);
923 simple_rmdir(parent->d_inode, d);
924 dput(parent);
925}
926
Tejun Heo05ef1d72012-04-01 12:09:56 -0700927static int cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700928{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700929 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700930
Tejun Heo05ef1d72012-04-01 12:09:56 -0700931 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
932 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100933
Tejun Heo05ef1d72012-04-01 12:09:56 -0700934 list_for_each_entry(cfe, &cgrp->files, node) {
935 struct dentry *d = cfe->dentry;
936
937 if (cft && cfe->type != cft)
938 continue;
939
940 dget(d);
941 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700942 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700943 list_del_init(&cfe->node);
944 dput(d);
945
946 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700947 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700948 return -ENOENT;
949}
950
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400951/**
952 * cgroup_clear_directory - selective removal of base and subsystem files
953 * @dir: directory containing the files
954 * @base_files: true if the base files should be removed
955 * @subsys_mask: mask of the subsystem ids whose files should be removed
956 */
957static void cgroup_clear_directory(struct dentry *dir, bool base_files,
958 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700959{
960 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400961 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700962
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400963 for_each_subsys(cgrp->root, ss) {
964 struct cftype_set *set;
965 if (!test_bit(ss->subsys_id, &subsys_mask))
966 continue;
967 list_for_each_entry(set, &ss->cftsets, node)
968 cgroup_rm_file(cgrp, set->cfts);
969 }
970 if (base_files) {
971 while (!list_empty(&cgrp->files))
972 cgroup_rm_file(cgrp, NULL);
973 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700974}
975
976/*
977 * NOTE : the dentry must have been dget()'ed
978 */
979static void cgroup_d_remove_dir(struct dentry *dentry)
980{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100981 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400982 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100983
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400984 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700985
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100986 parent = dentry->d_parent;
987 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800988 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700989 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100990 spin_unlock(&dentry->d_lock);
991 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700992 remove_dir(dentry);
993}
994
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700995/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800996 * Call with cgroup_mutex held. Drops reference counts on modules, including
997 * any duplicate ones that parse_cgroupfs_options took. If this function
998 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800999 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001001 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002{
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001003 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001004 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001005 int i;
1006
Ben Blumaae8aab2010-03-10 15:22:07 -08001007 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001008 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001009
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001010 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1011 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 /* Check that any added subsystems are currently free */
1013 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001014 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001016 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001018 /*
1019 * Nobody should tell us to do a subsys that doesn't exist:
1020 * parse_cgroupfs_options should catch that case and refcounts
1021 * ensure that subsystems won't disappear once selected.
1022 */
1023 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024 if (ss->root != &rootnode) {
1025 /* Subsystem isn't free */
1026 return -EBUSY;
1027 }
1028 }
1029
1030 /* Currently we don't handle adding/removing subsystems when
1031 * any child cgroups exist. This is theoretically supportable
1032 * but involves complex error handling, so it's being left until
1033 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001034 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 return -EBUSY;
1036
1037 /* Process each subsystem */
1038 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1039 struct cgroup_subsys *ss = subsys[i];
1040 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001041 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001042 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001043 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001044 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 BUG_ON(!dummytop->subsys[i]);
1046 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001047 cgrp->subsys[i] = dummytop->subsys[i];
1048 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001049 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001050 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001052 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001053 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001054 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001056 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001057 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1058 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001060 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001062 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001063 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001064 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001065 /* subsystem is now free - drop reference on module */
1066 module_put(ss->module);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001067 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001068 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001069 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001070 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001071 /*
1072 * a refcount was taken, but we already had one, so
1073 * drop the extra reference.
1074 */
1075 module_put(ss->module);
1076#ifdef CONFIG_MODULE_UNLOAD
1077 BUG_ON(ss->module && !module_refcount(ss->module));
1078#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079 } else {
1080 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001081 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082 }
1083 }
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001084 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085 synchronize_rcu();
1086
1087 return 0;
1088}
1089
Al Viro34c80b12011-12-08 21:32:45 -05001090static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091{
Al Viro34c80b12011-12-08 21:32:45 -05001092 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093 struct cgroup_subsys *ss;
1094
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001095 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096 for_each_subsys(root, ss)
1097 seq_printf(seq, ",%s", ss->name);
1098 if (test_bit(ROOT_NOPREFIX, &root->flags))
1099 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001100 if (test_bit(ROOT_XATTR, &root->flags))
1101 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001102 if (strlen(root->release_agent_path))
1103 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001104 if (clone_children(&root->top_cgroup))
1105 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001106 if (strlen(root->name))
1107 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001108 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109 return 0;
1110}
1111
1112struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001113 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001115 char *release_agent;
Daniel Lezcano97978e62010-10-27 15:33:35 -07001116 bool clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001117 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001118 /* User explicitly requested empty subsystem */
1119 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001120
1121 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001122
Paul Menageddbcc7e2007-10-18 23:39:30 -07001123};
1124
Ben Blumaae8aab2010-03-10 15:22:07 -08001125/*
1126 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001127 * with cgroup_mutex held to protect the subsys[] array. This function takes
1128 * refcounts on subsystems to be used, unless it returns error, in which case
1129 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001130 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001131static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001132{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001133 char *token, *o = data;
1134 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001135 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001136 int i;
1137 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001138
Ben Blumaae8aab2010-03-10 15:22:07 -08001139 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1140
Li Zefanf9ab5b52009-06-17 16:26:33 -07001141#ifdef CONFIG_CPUSETS
1142 mask = ~(1UL << cpuset_subsys_id);
1143#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001144
Paul Menagec6d57f32009-09-23 15:56:19 -07001145 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001146
1147 while ((token = strsep(&o, ",")) != NULL) {
1148 if (!*token)
1149 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001150 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001151 /* Explicitly have no subsystems */
1152 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001153 continue;
1154 }
1155 if (!strcmp(token, "all")) {
1156 /* Mutually exclusive option 'all' + subsystem name */
1157 if (one_ss)
1158 return -EINVAL;
1159 all_ss = true;
1160 continue;
1161 }
1162 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001163 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001164 continue;
1165 }
1166 if (!strcmp(token, "clone_children")) {
Daniel Lezcano97978e62010-10-27 15:33:35 -07001167 opts->clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001168 continue;
1169 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001170 if (!strcmp(token, "xattr")) {
1171 set_bit(ROOT_XATTR, &opts->flags);
1172 continue;
1173 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001174 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001175 /* Specifying two release agents is forbidden */
1176 if (opts->release_agent)
1177 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001178 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001179 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001180 if (!opts->release_agent)
1181 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001182 continue;
1183 }
1184 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001185 const char *name = token + 5;
1186 /* Can't specify an empty name */
1187 if (!strlen(name))
1188 return -EINVAL;
1189 /* Must match [\w.-]+ */
1190 for (i = 0; i < strlen(name); i++) {
1191 char c = name[i];
1192 if (isalnum(c))
1193 continue;
1194 if ((c == '.') || (c == '-') || (c == '_'))
1195 continue;
1196 return -EINVAL;
1197 }
1198 /* Specifying two names is forbidden */
1199 if (opts->name)
1200 return -EINVAL;
1201 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001202 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001203 GFP_KERNEL);
1204 if (!opts->name)
1205 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001206
1207 continue;
1208 }
1209
1210 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1211 struct cgroup_subsys *ss = subsys[i];
1212 if (ss == NULL)
1213 continue;
1214 if (strcmp(token, ss->name))
1215 continue;
1216 if (ss->disabled)
1217 continue;
1218
1219 /* Mutually exclusive option 'all' + subsystem name */
1220 if (all_ss)
1221 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001222 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001223 one_ss = true;
1224
1225 break;
1226 }
1227 if (i == CGROUP_SUBSYS_COUNT)
1228 return -ENOENT;
1229 }
1230
1231 /*
1232 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001233 * otherwise if 'none', 'name=' and a subsystem name options
1234 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001235 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001236 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001237 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1238 struct cgroup_subsys *ss = subsys[i];
1239 if (ss == NULL)
1240 continue;
1241 if (ss->disabled)
1242 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001243 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001244 }
1245 }
1246
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001247 /* Consistency checks */
1248
Li Zefanf9ab5b52009-06-17 16:26:33 -07001249 /*
1250 * Option noprefix was introduced just for backward compatibility
1251 * with the old cpuset, so we allow noprefix only if mounting just
1252 * the cpuset subsystem.
1253 */
1254 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001255 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001256 return -EINVAL;
1257
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001258
1259 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001260 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001261 return -EINVAL;
1262
1263 /*
1264 * We either have to specify by name or by subsystems. (So all
1265 * empty hierarchies must have a name).
1266 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001267 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001268 return -EINVAL;
1269
Ben Blumcf5d5942010-03-10 15:22:09 -08001270 /*
1271 * Grab references on all the modules we'll need, so the subsystems
1272 * don't dance around before rebind_subsystems attaches them. This may
1273 * take duplicate reference counts on a subsystem that's already used,
1274 * but rebind_subsystems handles this case.
1275 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001276 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001277 unsigned long bit = 1UL << i;
1278
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001279 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001280 continue;
1281 if (!try_module_get(subsys[i]->module)) {
1282 module_pin_failed = true;
1283 break;
1284 }
1285 }
1286 if (module_pin_failed) {
1287 /*
1288 * oops, one of the modules was going away. this means that we
1289 * raced with a module_delete call, and to the user this is
1290 * essentially a "subsystem doesn't exist" case.
1291 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001292 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001293 /* drop refcounts only on the ones we took */
1294 unsigned long bit = 1UL << i;
1295
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001296 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001297 continue;
1298 module_put(subsys[i]->module);
1299 }
1300 return -ENOENT;
1301 }
1302
Paul Menageddbcc7e2007-10-18 23:39:30 -07001303 return 0;
1304}
1305
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001306static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001307{
1308 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001309 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001310 unsigned long bit = 1UL << i;
1311
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001312 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001313 continue;
1314 module_put(subsys[i]->module);
1315 }
1316}
1317
Paul Menageddbcc7e2007-10-18 23:39:30 -07001318static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1319{
1320 int ret = 0;
1321 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001322 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001324 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325
Paul Menagebd89aab2007-10-18 23:40:44 -07001326 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001327 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001328 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001329
1330 /* See what subsystems are wanted */
1331 ret = parse_cgroupfs_options(data, &opts);
1332 if (ret)
1333 goto out_unlock;
1334
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001335 /* See feature-removal-schedule.txt */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001336 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001337 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1338 task_tgid_nr(current), current->comm);
1339
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001340 added_mask = opts.subsys_mask & ~root->subsys_mask;
1341 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001342
Ben Blumcf5d5942010-03-10 15:22:09 -08001343 /* Don't allow flags or name to change at remount */
1344 if (opts.flags != root->flags ||
1345 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001347 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001348 goto out_unlock;
1349 }
1350
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001351 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001352 if (ret) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001353 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001354 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001355 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001356
Tejun Heoff4c8d52012-04-01 12:09:54 -07001357 /* clear out any existing files and repopulate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001358 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001359 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001360 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001361
Paul Menage81a6a5c2007-10-18 23:39:38 -07001362 if (opts.release_agent)
1363 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001364 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001365 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001366 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001367 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001369 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001370 return ret;
1371}
1372
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001373static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374 .statfs = simple_statfs,
1375 .drop_inode = generic_delete_inode,
1376 .show_options = cgroup_show_options,
1377 .remount_fs = cgroup_remount,
1378};
1379
Paul Menagecc31edc2008-10-18 20:28:04 -07001380static void init_cgroup_housekeeping(struct cgroup *cgrp)
1381{
1382 INIT_LIST_HEAD(&cgrp->sibling);
1383 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001384 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001385 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001386 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001387 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001388 INIT_LIST_HEAD(&cgrp->pidlists);
1389 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001390 INIT_LIST_HEAD(&cgrp->event_list);
1391 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001392 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001393}
Paul Menagec6d57f32009-09-23 15:56:19 -07001394
Paul Menageddbcc7e2007-10-18 23:39:30 -07001395static void init_cgroup_root(struct cgroupfs_root *root)
1396{
Paul Menagebd89aab2007-10-18 23:40:44 -07001397 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001398
Paul Menageddbcc7e2007-10-18 23:39:30 -07001399 INIT_LIST_HEAD(&root->subsys_list);
1400 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001401 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001402 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001403 cgrp->root = root;
1404 cgrp->top_cgroup = cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001405 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menagecc31edc2008-10-18 20:28:04 -07001406 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001407}
1408
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001409static bool init_root_id(struct cgroupfs_root *root)
1410{
1411 int ret = 0;
1412
1413 do {
1414 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1415 return false;
1416 spin_lock(&hierarchy_id_lock);
1417 /* Try to allocate the next unused ID */
1418 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1419 &root->hierarchy_id);
1420 if (ret == -ENOSPC)
1421 /* Try again starting from 0 */
1422 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1423 if (!ret) {
1424 next_hierarchy_id = root->hierarchy_id + 1;
1425 } else if (ret != -EAGAIN) {
1426 /* Can only get here if the 31-bit IDR is full ... */
1427 BUG_ON(ret);
1428 }
1429 spin_unlock(&hierarchy_id_lock);
1430 } while (ret);
1431 return true;
1432}
1433
Paul Menageddbcc7e2007-10-18 23:39:30 -07001434static int cgroup_test_super(struct super_block *sb, void *data)
1435{
Paul Menagec6d57f32009-09-23 15:56:19 -07001436 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001437 struct cgroupfs_root *root = sb->s_fs_info;
1438
Paul Menagec6d57f32009-09-23 15:56:19 -07001439 /* If we asked for a name then it must match */
1440 if (opts->name && strcmp(opts->name, root->name))
1441 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001443 /*
1444 * If we asked for subsystems (or explicitly for no
1445 * subsystems) then they must match
1446 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001447 if ((opts->subsys_mask || opts->none)
1448 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001449 return 0;
1450
1451 return 1;
1452}
1453
Paul Menagec6d57f32009-09-23 15:56:19 -07001454static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1455{
1456 struct cgroupfs_root *root;
1457
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001458 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001459 return NULL;
1460
1461 root = kzalloc(sizeof(*root), GFP_KERNEL);
1462 if (!root)
1463 return ERR_PTR(-ENOMEM);
1464
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001465 if (!init_root_id(root)) {
1466 kfree(root);
1467 return ERR_PTR(-ENOMEM);
1468 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001469 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001470
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001471 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001472 root->flags = opts->flags;
1473 if (opts->release_agent)
1474 strcpy(root->release_agent_path, opts->release_agent);
1475 if (opts->name)
1476 strcpy(root->name, opts->name);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001477 if (opts->clone_children)
1478 set_bit(CGRP_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001479 return root;
1480}
1481
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001482static void cgroup_drop_root(struct cgroupfs_root *root)
1483{
1484 if (!root)
1485 return;
1486
1487 BUG_ON(!root->hierarchy_id);
1488 spin_lock(&hierarchy_id_lock);
1489 ida_remove(&hierarchy_ida, root->hierarchy_id);
1490 spin_unlock(&hierarchy_id_lock);
1491 kfree(root);
1492}
1493
Paul Menageddbcc7e2007-10-18 23:39:30 -07001494static int cgroup_set_super(struct super_block *sb, void *data)
1495{
1496 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001497 struct cgroup_sb_opts *opts = data;
1498
1499 /* If we don't have a new root, we can't set up a new sb */
1500 if (!opts->new_root)
1501 return -EINVAL;
1502
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001503 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001504
1505 ret = set_anon_super(sb, NULL);
1506 if (ret)
1507 return ret;
1508
Paul Menagec6d57f32009-09-23 15:56:19 -07001509 sb->s_fs_info = opts->new_root;
1510 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001511
1512 sb->s_blocksize = PAGE_CACHE_SIZE;
1513 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1514 sb->s_magic = CGROUP_SUPER_MAGIC;
1515 sb->s_op = &cgroup_ops;
1516
1517 return 0;
1518}
1519
1520static int cgroup_get_rootdir(struct super_block *sb)
1521{
Al Viro0df6a632010-12-21 13:29:29 -05001522 static const struct dentry_operations cgroup_dops = {
1523 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001524 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001525 };
1526
Paul Menageddbcc7e2007-10-18 23:39:30 -07001527 struct inode *inode =
1528 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001529
1530 if (!inode)
1531 return -ENOMEM;
1532
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533 inode->i_fop = &simple_dir_operations;
1534 inode->i_op = &cgroup_dir_inode_operations;
1535 /* directories start off with i_nlink == 2 (for "." entry) */
1536 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001537 sb->s_root = d_make_root(inode);
1538 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001540 /* for everything else we want ->d_op set */
1541 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001542 return 0;
1543}
1544
Al Virof7e83572010-07-26 13:23:11 +04001545static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001547 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548{
1549 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001550 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551 int ret = 0;
1552 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001553 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001554 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555
1556 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001557 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001559 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001560 if (ret)
1561 goto out_err;
1562
1563 /*
1564 * Allocate a new cgroup root. We may not need it if we're
1565 * reusing an existing hierarchy.
1566 */
1567 new_root = cgroup_root_from_opts(&opts);
1568 if (IS_ERR(new_root)) {
1569 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001570 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001571 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001572 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573
Paul Menagec6d57f32009-09-23 15:56:19 -07001574 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001575 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001577 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001578 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001579 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580 }
1581
Paul Menagec6d57f32009-09-23 15:56:19 -07001582 root = sb->s_fs_info;
1583 BUG_ON(!root);
1584 if (root == opts.new_root) {
1585 /* We used the new root structure, so this is a new hierarchy */
1586 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001587 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001588 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001589 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001590 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001591
1592 BUG_ON(sb->s_root != NULL);
1593
1594 ret = cgroup_get_rootdir(sb);
1595 if (ret)
1596 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001597 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001598
Paul Menage817929e2007-10-18 23:39:36 -07001599 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001601 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001602
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001603 /* Check for name clashes with existing mounts */
1604 ret = -EBUSY;
1605 if (strlen(root->name))
1606 for_each_active_root(existing_root)
1607 if (!strcmp(existing_root->name, root->name))
1608 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001609
Paul Menage817929e2007-10-18 23:39:36 -07001610 /*
1611 * We're accessing css_set_count without locking
1612 * css_set_lock here, but that's OK - it can only be
1613 * increased by someone holding cgroup_lock, and
1614 * that's us. The worst that can happen is that we
1615 * have some link structures left over
1616 */
1617 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001618 if (ret)
1619 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001620
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001621 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001622 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001623 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001624 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001625 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001626 /*
1627 * There must be no failure case after here, since rebinding
1628 * takes care of subsystems' refcounts, which are explicitly
1629 * dropped in the failure exit path.
1630 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631
1632 /* EBUSY should be the only error here */
1633 BUG_ON(ret);
1634
1635 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001636 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001637
Li Zefanc12f65d2009-01-07 18:07:42 -08001638 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001639 root->top_cgroup.dentry = sb->s_root;
1640
Paul Menage817929e2007-10-18 23:39:36 -07001641 /* Link the top cgroup in this hierarchy into all
1642 * the css_set objects */
1643 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001644 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1645 struct hlist_head *hhead = &css_set_table[i];
1646 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001647 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001648
Li Zefanc12f65d2009-01-07 18:07:42 -08001649 hlist_for_each_entry(cg, node, hhead, hlist)
1650 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001651 }
Paul Menage817929e2007-10-18 23:39:36 -07001652 write_unlock(&css_set_lock);
1653
1654 free_cg_links(&tmp_cg_links);
1655
Li Zefanc12f65d2009-01-07 18:07:42 -08001656 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657 BUG_ON(root->number_of_cgroups != 1);
1658
eparis@redhat2ce97382011-06-02 21:20:51 +10001659 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001660 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001661 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001662 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001664 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001665 } else {
1666 /*
1667 * We re-used an existing hierarchy - the new root (if
1668 * any) is not needed
1669 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001670 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001671 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001672 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673 }
1674
Paul Menagec6d57f32009-09-23 15:56:19 -07001675 kfree(opts.release_agent);
1676 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001677 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001678
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001679 unlock_drop:
1680 mutex_unlock(&cgroup_root_mutex);
1681 mutex_unlock(&cgroup_mutex);
1682 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001684 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001685 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001686 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001687 out_err:
1688 kfree(opts.release_agent);
1689 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001690 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001691}
1692
1693static void cgroup_kill_sb(struct super_block *sb) {
1694 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001695 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001697 struct cg_cgroup_link *link;
1698 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001699
1700 BUG_ON(!root);
1701
1702 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001703 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001704
1705 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001706 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001707
1708 /* Rebind all subsystems back to the default hierarchy */
1709 ret = rebind_subsystems(root, 0);
1710 /* Shouldn't be able to fail ... */
1711 BUG_ON(ret);
1712
Paul Menage817929e2007-10-18 23:39:36 -07001713 /*
1714 * Release all the links from css_sets to this hierarchy's
1715 * root cgroup
1716 */
1717 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001718
1719 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1720 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001721 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001722 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001723 kfree(link);
1724 }
1725 write_unlock(&css_set_lock);
1726
Paul Menage839ec542009-01-29 14:25:22 -08001727 if (!list_empty(&root->root_list)) {
1728 list_del(&root->root_list);
1729 root_count--;
1730 }
Li Zefane5f6a862009-01-07 18:07:41 -08001731
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001732 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733 mutex_unlock(&cgroup_mutex);
1734
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001735 simple_xattrs_free(&cgrp->xattrs);
1736
Paul Menageddbcc7e2007-10-18 23:39:30 -07001737 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001738 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739}
1740
1741static struct file_system_type cgroup_fs_type = {
1742 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001743 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744 .kill_sb = cgroup_kill_sb,
1745};
1746
Greg KH676db4a2010-08-05 13:53:35 -07001747static struct kobject *cgroup_kobj;
1748
Li Zefana043e3b2008-02-23 15:24:09 -08001749/**
1750 * cgroup_path - generate the path of a cgroup
1751 * @cgrp: the cgroup in question
1752 * @buf: the buffer to write the path into
1753 * @buflen: the length of the buffer
1754 *
Paul Menagea47295e2009-01-07 18:07:44 -08001755 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1756 * reference. Writes path of cgroup into buf. Returns 0 on success,
1757 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001759int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760{
Tejun Heofebfcef2012-11-19 08:13:36 -08001761 struct dentry *dentry = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001762 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001763
1764 rcu_lockdep_assert(rcu_read_lock_held() || cgroup_lock_is_held(),
1765 "cgroup_path() called without proper locking");
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766
Paul Menagea47295e2009-01-07 18:07:44 -08001767 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001768 /*
1769 * Inactive subsystems have no dentry for their root
1770 * cgroup
1771 */
1772 strcpy(buf, "/");
1773 return 0;
1774 }
1775
Tao Ma316eb662012-11-08 21:36:38 +08001776 start = buf + buflen - 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777
Tao Ma316eb662012-11-08 21:36:38 +08001778 *start = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001780 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001781
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 if ((start -= len) < buf)
1783 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001784 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001785 cgrp = cgrp->parent;
1786 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001788
Tejun Heofebfcef2012-11-19 08:13:36 -08001789 dentry = cgrp->dentry;
Paul Menagebd89aab2007-10-18 23:40:44 -07001790 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791 continue;
1792 if (--start < buf)
1793 return -ENAMETOOLONG;
1794 *start = '/';
1795 }
1796 memmove(buf, start, buf + buflen - start);
1797 return 0;
1798}
Ben Blum67523c42010-03-10 15:22:11 -08001799EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001800
Ben Blum74a11662011-05-26 16:25:20 -07001801/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001802 * Control Group taskset
1803 */
Tejun Heo134d3372011-12-12 18:12:21 -08001804struct task_and_cgroup {
1805 struct task_struct *task;
1806 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001807 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001808};
1809
Tejun Heo2f7ee562011-12-12 18:12:21 -08001810struct cgroup_taskset {
1811 struct task_and_cgroup single;
1812 struct flex_array *tc_array;
1813 int tc_array_len;
1814 int idx;
1815 struct cgroup *cur_cgrp;
1816};
1817
1818/**
1819 * cgroup_taskset_first - reset taskset and return the first task
1820 * @tset: taskset of interest
1821 *
1822 * @tset iteration is initialized and the first task is returned.
1823 */
1824struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1825{
1826 if (tset->tc_array) {
1827 tset->idx = 0;
1828 return cgroup_taskset_next(tset);
1829 } else {
1830 tset->cur_cgrp = tset->single.cgrp;
1831 return tset->single.task;
1832 }
1833}
1834EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1835
1836/**
1837 * cgroup_taskset_next - iterate to the next task in taskset
1838 * @tset: taskset of interest
1839 *
1840 * Return the next task in @tset. Iteration must have been initialized
1841 * with cgroup_taskset_first().
1842 */
1843struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1844{
1845 struct task_and_cgroup *tc;
1846
1847 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1848 return NULL;
1849
1850 tc = flex_array_get(tset->tc_array, tset->idx++);
1851 tset->cur_cgrp = tc->cgrp;
1852 return tc->task;
1853}
1854EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1855
1856/**
1857 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1858 * @tset: taskset of interest
1859 *
1860 * Return the cgroup for the current (last returned) task of @tset. This
1861 * function must be preceded by either cgroup_taskset_first() or
1862 * cgroup_taskset_next().
1863 */
1864struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1865{
1866 return tset->cur_cgrp;
1867}
1868EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1869
1870/**
1871 * cgroup_taskset_size - return the number of tasks in taskset
1872 * @tset: taskset of interest
1873 */
1874int cgroup_taskset_size(struct cgroup_taskset *tset)
1875{
1876 return tset->tc_array ? tset->tc_array_len : 1;
1877}
1878EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1879
1880
Ben Blum74a11662011-05-26 16:25:20 -07001881/*
1882 * cgroup_task_migrate - move a task from one cgroup to another.
1883 *
1884 * 'guarantee' is set if the caller promises that a new css_set for the task
1885 * will already exist. If not set, this function might sleep, and can fail with
Tejun Heocd3d0952011-12-12 18:12:21 -08001886 * -ENOMEM. Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001887 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001888static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1889 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001890{
1891 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001892
1893 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001894 * We are synchronized through threadgroup_lock() against PF_EXITING
1895 * setting such that we can't race against cgroup_exit() changing the
1896 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001897 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001898 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001899 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001900
Ben Blum74a11662011-05-26 16:25:20 -07001901 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001902 rcu_assign_pointer(tsk->cgroups, newcg);
1903 task_unlock(tsk);
1904
1905 /* Update the css_set linked lists if we're using them */
1906 write_lock(&css_set_lock);
1907 if (!list_empty(&tsk->cg_list))
1908 list_move(&tsk->cg_list, &newcg->tasks);
1909 write_unlock(&css_set_lock);
1910
1911 /*
1912 * We just gained a reference on oldcg by taking it from the task. As
1913 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1914 * it here; it will be freed under RCU.
1915 */
Ben Blum74a11662011-05-26 16:25:20 -07001916 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001917 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001918}
1919
Li Zefana043e3b2008-02-23 15:24:09 -08001920/**
1921 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1922 * @cgrp: the cgroup the task is attaching to
1923 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001924 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001925 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1926 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001927 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001928int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001929{
Tejun Heo8f121912012-03-29 22:03:33 -07001930 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001931 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001932 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001933 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001934 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001935 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001936
Tejun Heocd3d0952011-12-12 18:12:21 -08001937 /* @tsk either already exited or can't exit until the end */
1938 if (tsk->flags & PF_EXITING)
1939 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001940
1941 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001942 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001943 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001944 return 0;
1945
Tejun Heo2f7ee562011-12-12 18:12:21 -08001946 tset.single.task = tsk;
1947 tset.single.cgrp = oldcgrp;
1948
Paul Menagebbcb81d2007-10-18 23:39:32 -07001949 for_each_subsys(root, ss) {
1950 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08001951 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001952 if (retval) {
1953 /*
1954 * Remember on which subsystem the can_attach()
1955 * failed, so that we only call cancel_attach()
1956 * against the subsystems whose can_attach()
1957 * succeeded. (See below)
1958 */
1959 failed_ss = ss;
1960 goto out;
1961 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001962 }
1963 }
1964
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001965 newcg = find_css_set(tsk->cgroups, cgrp);
1966 if (!newcg) {
1967 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001968 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001969 }
1970
1971 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001972
Paul Menagebbcb81d2007-10-18 23:39:32 -07001973 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001974 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001975 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001976 }
Ben Blum74a11662011-05-26 16:25:20 -07001977
Paul Menagebbcb81d2007-10-18 23:39:32 -07001978 synchronize_rcu();
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001979out:
1980 if (retval) {
1981 for_each_subsys(root, ss) {
1982 if (ss == failed_ss)
1983 /*
1984 * This subsystem was the one that failed the
1985 * can_attach() check earlier, so we don't need
1986 * to call cancel_attach() against it or any
1987 * remaining subsystems.
1988 */
1989 break;
1990 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001991 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001992 }
1993 }
1994 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001995}
1996
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001997/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001998 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1999 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002000 * @tsk: the task to be attached
2001 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002002int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002003{
2004 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002005 int retval = 0;
2006
2007 cgroup_lock();
2008 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002009 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2010
2011 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002012 if (retval)
2013 break;
2014 }
2015 cgroup_unlock();
2016
2017 return retval;
2018}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002019EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002020
Ben Blum74a11662011-05-26 16:25:20 -07002021/**
2022 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2023 * @cgrp: the cgroup to attach to
2024 * @leader: the threadgroup leader task_struct of the group to be attached
2025 *
Tejun Heo257058a2011-12-12 18:12:21 -08002026 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2027 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002028 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002029static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002030{
2031 int retval, i, group_size;
2032 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002033 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002034 struct cgroupfs_root *root = cgrp->root;
2035 /* threadgroup list cursor and array */
2036 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002037 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002038 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002039 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002040
2041 /*
2042 * step 0: in order to do expensive, possibly blocking operations for
2043 * every thread, we cannot iterate the thread group list, since it needs
2044 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002045 * group - group_rwsem prevents new threads from appearing, and if
2046 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002047 */
2048 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002049 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002050 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002051 if (!group)
2052 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002053 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2054 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2055 if (retval)
2056 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002057
Ben Blum74a11662011-05-26 16:25:20 -07002058 tsk = leader;
2059 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002060 /*
2061 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2062 * already PF_EXITING could be freed from underneath us unless we
2063 * take an rcu_read_lock.
2064 */
2065 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002066 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002067 struct task_and_cgroup ent;
2068
Tejun Heocd3d0952011-12-12 18:12:21 -08002069 /* @tsk either already exited or can't exit until the end */
2070 if (tsk->flags & PF_EXITING)
2071 continue;
2072
Ben Blum74a11662011-05-26 16:25:20 -07002073 /* as per above, nr_threads may decrease, but not increase. */
2074 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002075 ent.task = tsk;
2076 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002077 /* nothing to do if this task is already in the cgroup */
2078 if (ent.cgrp == cgrp)
2079 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002080 /*
2081 * saying GFP_ATOMIC has no effect here because we did prealloc
2082 * earlier, but it's good form to communicate our expectations.
2083 */
Tejun Heo134d3372011-12-12 18:12:21 -08002084 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002085 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002086 i++;
2087 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002088 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002089 /* remember the number of threads in the array for later. */
2090 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002091 tset.tc_array = group;
2092 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002093
Tejun Heo134d3372011-12-12 18:12:21 -08002094 /* methods shouldn't be called if no task is actually migrating */
2095 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002096 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002097 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002098
Ben Blum74a11662011-05-26 16:25:20 -07002099 /*
2100 * step 1: check that we can legitimately attach to the cgroup.
2101 */
2102 for_each_subsys(root, ss) {
2103 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002104 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002105 if (retval) {
2106 failed_ss = ss;
2107 goto out_cancel_attach;
2108 }
2109 }
Ben Blum74a11662011-05-26 16:25:20 -07002110 }
2111
2112 /*
2113 * step 2: make sure css_sets exist for all threads to be migrated.
2114 * we use find_css_set, which allocates a new one if necessary.
2115 */
Ben Blum74a11662011-05-26 16:25:20 -07002116 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002117 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002118 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2119 if (!tc->cg) {
2120 retval = -ENOMEM;
2121 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002122 }
2123 }
2124
2125 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002126 * step 3: now that we're guaranteed success wrt the css_sets,
2127 * proceed to move all tasks to the new cgroup. There are no
2128 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002129 */
Ben Blum74a11662011-05-26 16:25:20 -07002130 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002131 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002132 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002133 }
2134 /* nothing is sensitive to fork() after this point. */
2135
2136 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002137 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002138 */
2139 for_each_subsys(root, ss) {
2140 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002141 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002142 }
2143
2144 /*
2145 * step 5: success! and cleanup
2146 */
2147 synchronize_rcu();
Ben Blum74a11662011-05-26 16:25:20 -07002148 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002149out_put_css_set_refs:
2150 if (retval) {
2151 for (i = 0; i < group_size; i++) {
2152 tc = flex_array_get(group, i);
2153 if (!tc->cg)
2154 break;
2155 put_css_set(tc->cg);
2156 }
Ben Blum74a11662011-05-26 16:25:20 -07002157 }
2158out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002159 if (retval) {
2160 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002161 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002162 break;
Ben Blum74a11662011-05-26 16:25:20 -07002163 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002164 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002165 }
2166 }
Ben Blum74a11662011-05-26 16:25:20 -07002167out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002168 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002169 return retval;
2170}
2171
2172/*
2173 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002174 * function to attach either it or all tasks in its threadgroup. Will lock
2175 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002176 */
2177static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002178{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002179 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002180 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002181 int ret;
2182
Ben Blum74a11662011-05-26 16:25:20 -07002183 if (!cgroup_lock_live_group(cgrp))
2184 return -ENODEV;
2185
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002186retry_find_task:
2187 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002188 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002189 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002190 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002191 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002192 ret= -ESRCH;
2193 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002194 }
Ben Blum74a11662011-05-26 16:25:20 -07002195 /*
2196 * even if we're attaching all tasks in the thread group, we
2197 * only need to check permissions on one of them.
2198 */
David Howellsc69e8d92008-11-14 10:39:19 +11002199 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002200 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2201 !uid_eq(cred->euid, tcred->uid) &&
2202 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002203 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002204 ret = -EACCES;
2205 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002206 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002207 } else
2208 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002209
2210 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002211 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002212
2213 /*
2214 * Workqueue threads may acquire PF_THREAD_BOUND and become
2215 * trapped in a cpuset, or RT worker may be born in a cgroup
2216 * with no rt_runtime allocated. Just say no.
2217 */
2218 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2219 ret = -EINVAL;
2220 rcu_read_unlock();
2221 goto out_unlock_cgroup;
2222 }
2223
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002224 get_task_struct(tsk);
2225 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002226
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002227 threadgroup_lock(tsk);
2228 if (threadgroup) {
2229 if (!thread_group_leader(tsk)) {
2230 /*
2231 * a race with de_thread from another thread's exec()
2232 * may strip us of our leadership, if this happens,
2233 * there is no choice but to throw this task away and
2234 * try again; this is
2235 * "double-double-toil-and-trouble-check locking".
2236 */
2237 threadgroup_unlock(tsk);
2238 put_task_struct(tsk);
2239 goto retry_find_task;
2240 }
2241 ret = cgroup_attach_proc(cgrp, tsk);
2242 } else
2243 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002244 threadgroup_unlock(tsk);
2245
Paul Menagebbcb81d2007-10-18 23:39:32 -07002246 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002247out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002248 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002249 return ret;
2250}
2251
Paul Menageaf351022008-07-25 01:47:01 -07002252static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2253{
Ben Blum74a11662011-05-26 16:25:20 -07002254 return attach_task_by_pid(cgrp, pid, false);
2255}
2256
2257static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2258{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002259 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002260}
2261
Paul Menagee788e062008-07-25 01:46:59 -07002262/**
2263 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2264 * @cgrp: the cgroup to be checked for liveness
2265 *
Paul Menage84eea842008-07-25 01:47:00 -07002266 * On success, returns true; the lock should be later released with
2267 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002268 */
Paul Menage84eea842008-07-25 01:47:00 -07002269bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002270{
2271 mutex_lock(&cgroup_mutex);
2272 if (cgroup_is_removed(cgrp)) {
2273 mutex_unlock(&cgroup_mutex);
2274 return false;
2275 }
2276 return true;
2277}
Ben Blum67523c42010-03-10 15:22:11 -08002278EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002279
2280static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2281 const char *buffer)
2282{
2283 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002284 if (strlen(buffer) >= PATH_MAX)
2285 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002286 if (!cgroup_lock_live_group(cgrp))
2287 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002288 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002289 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002290 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002291 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002292 return 0;
2293}
2294
2295static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2296 struct seq_file *seq)
2297{
2298 if (!cgroup_lock_live_group(cgrp))
2299 return -ENODEV;
2300 seq_puts(seq, cgrp->root->release_agent_path);
2301 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002302 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002303 return 0;
2304}
2305
Paul Menage84eea842008-07-25 01:47:00 -07002306/* A buffer size big enough for numbers or short strings */
2307#define CGROUP_LOCAL_BUFFER_SIZE 64
2308
Paul Menagee73d2c62008-04-29 01:00:06 -07002309static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002310 struct file *file,
2311 const char __user *userbuf,
2312 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002313{
Paul Menage84eea842008-07-25 01:47:00 -07002314 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002315 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002316 char *end;
2317
2318 if (!nbytes)
2319 return -EINVAL;
2320 if (nbytes >= sizeof(buffer))
2321 return -E2BIG;
2322 if (copy_from_user(buffer, userbuf, nbytes))
2323 return -EFAULT;
2324
2325 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002326 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002327 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002328 if (*end)
2329 return -EINVAL;
2330 retval = cft->write_u64(cgrp, cft, val);
2331 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002332 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002333 if (*end)
2334 return -EINVAL;
2335 retval = cft->write_s64(cgrp, cft, val);
2336 }
Paul Menage355e0c42007-10-18 23:39:33 -07002337 if (!retval)
2338 retval = nbytes;
2339 return retval;
2340}
2341
Paul Menagedb3b1492008-07-25 01:46:58 -07002342static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2343 struct file *file,
2344 const char __user *userbuf,
2345 size_t nbytes, loff_t *unused_ppos)
2346{
Paul Menage84eea842008-07-25 01:47:00 -07002347 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002348 int retval = 0;
2349 size_t max_bytes = cft->max_write_len;
2350 char *buffer = local_buffer;
2351
2352 if (!max_bytes)
2353 max_bytes = sizeof(local_buffer) - 1;
2354 if (nbytes >= max_bytes)
2355 return -E2BIG;
2356 /* Allocate a dynamic buffer if we need one */
2357 if (nbytes >= sizeof(local_buffer)) {
2358 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2359 if (buffer == NULL)
2360 return -ENOMEM;
2361 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002362 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2363 retval = -EFAULT;
2364 goto out;
2365 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002366
2367 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002368 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002369 if (!retval)
2370 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002371out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002372 if (buffer != local_buffer)
2373 kfree(buffer);
2374 return retval;
2375}
2376
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2378 size_t nbytes, loff_t *ppos)
2379{
2380 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002381 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002382
Li Zefan75139b82009-01-07 18:07:33 -08002383 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002384 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002385 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002386 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002387 if (cft->write_u64 || cft->write_s64)
2388 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002389 if (cft->write_string)
2390 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002391 if (cft->trigger) {
2392 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2393 return ret ? ret : nbytes;
2394 }
Paul Menage355e0c42007-10-18 23:39:33 -07002395 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002396}
2397
Paul Menagef4c753b2008-04-29 00:59:56 -07002398static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2399 struct file *file,
2400 char __user *buf, size_t nbytes,
2401 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002402{
Paul Menage84eea842008-07-25 01:47:00 -07002403 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002404 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002405 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2406
2407 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2408}
2409
Paul Menagee73d2c62008-04-29 01:00:06 -07002410static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2411 struct file *file,
2412 char __user *buf, size_t nbytes,
2413 loff_t *ppos)
2414{
Paul Menage84eea842008-07-25 01:47:00 -07002415 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002416 s64 val = cft->read_s64(cgrp, cft);
2417 int len = sprintf(tmp, "%lld\n", (long long) val);
2418
2419 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2420}
2421
Paul Menageddbcc7e2007-10-18 23:39:30 -07002422static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2423 size_t nbytes, loff_t *ppos)
2424{
2425 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002426 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002427
Li Zefan75139b82009-01-07 18:07:33 -08002428 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002429 return -ENODEV;
2430
2431 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002432 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002433 if (cft->read_u64)
2434 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002435 if (cft->read_s64)
2436 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002437 return -EINVAL;
2438}
2439
Paul Menage91796562008-04-29 01:00:01 -07002440/*
2441 * seqfile ops/methods for returning structured data. Currently just
2442 * supports string->u64 maps, but can be extended in future.
2443 */
2444
2445struct cgroup_seqfile_state {
2446 struct cftype *cft;
2447 struct cgroup *cgroup;
2448};
2449
2450static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2451{
2452 struct seq_file *sf = cb->state;
2453 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2454}
2455
2456static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2457{
2458 struct cgroup_seqfile_state *state = m->private;
2459 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002460 if (cft->read_map) {
2461 struct cgroup_map_cb cb = {
2462 .fill = cgroup_map_add,
2463 .state = m,
2464 };
2465 return cft->read_map(state->cgroup, cft, &cb);
2466 }
2467 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002468}
2469
Adrian Bunk96930a62008-07-25 19:46:21 -07002470static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002471{
2472 struct seq_file *seq = file->private_data;
2473 kfree(seq->private);
2474 return single_release(inode, file);
2475}
2476
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002477static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002478 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002479 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002480 .llseek = seq_lseek,
2481 .release = cgroup_seqfile_release,
2482};
2483
Paul Menageddbcc7e2007-10-18 23:39:30 -07002484static int cgroup_file_open(struct inode *inode, struct file *file)
2485{
2486 int err;
2487 struct cftype *cft;
2488
2489 err = generic_file_open(inode, file);
2490 if (err)
2491 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002492 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002493
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002494 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002495 struct cgroup_seqfile_state *state =
2496 kzalloc(sizeof(*state), GFP_USER);
2497 if (!state)
2498 return -ENOMEM;
2499 state->cft = cft;
2500 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2501 file->f_op = &cgroup_seqfile_operations;
2502 err = single_open(file, cgroup_seqfile_show, state);
2503 if (err < 0)
2504 kfree(state);
2505 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002506 err = cft->open(inode, file);
2507 else
2508 err = 0;
2509
2510 return err;
2511}
2512
2513static int cgroup_file_release(struct inode *inode, struct file *file)
2514{
2515 struct cftype *cft = __d_cft(file->f_dentry);
2516 if (cft->release)
2517 return cft->release(inode, file);
2518 return 0;
2519}
2520
2521/*
2522 * cgroup_rename - Only allow simple rename of directories in place.
2523 */
2524static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2525 struct inode *new_dir, struct dentry *new_dentry)
2526{
2527 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2528 return -ENOTDIR;
2529 if (new_dentry->d_inode)
2530 return -EEXIST;
2531 if (old_dir != new_dir)
2532 return -EIO;
2533 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2534}
2535
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002536static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2537{
2538 if (S_ISDIR(dentry->d_inode->i_mode))
2539 return &__d_cgrp(dentry)->xattrs;
2540 else
2541 return &__d_cft(dentry)->xattrs;
2542}
2543
2544static inline int xattr_enabled(struct dentry *dentry)
2545{
2546 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2547 return test_bit(ROOT_XATTR, &root->flags);
2548}
2549
2550static bool is_valid_xattr(const char *name)
2551{
2552 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2553 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2554 return true;
2555 return false;
2556}
2557
2558static int cgroup_setxattr(struct dentry *dentry, const char *name,
2559 const void *val, size_t size, int flags)
2560{
2561 if (!xattr_enabled(dentry))
2562 return -EOPNOTSUPP;
2563 if (!is_valid_xattr(name))
2564 return -EINVAL;
2565 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2566}
2567
2568static int cgroup_removexattr(struct dentry *dentry, const char *name)
2569{
2570 if (!xattr_enabled(dentry))
2571 return -EOPNOTSUPP;
2572 if (!is_valid_xattr(name))
2573 return -EINVAL;
2574 return simple_xattr_remove(__d_xattrs(dentry), name);
2575}
2576
2577static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2578 void *buf, size_t size)
2579{
2580 if (!xattr_enabled(dentry))
2581 return -EOPNOTSUPP;
2582 if (!is_valid_xattr(name))
2583 return -EINVAL;
2584 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2585}
2586
2587static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2588{
2589 if (!xattr_enabled(dentry))
2590 return -EOPNOTSUPP;
2591 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2592}
2593
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002594static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002595 .read = cgroup_file_read,
2596 .write = cgroup_file_write,
2597 .llseek = generic_file_llseek,
2598 .open = cgroup_file_open,
2599 .release = cgroup_file_release,
2600};
2601
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002602static const struct inode_operations cgroup_file_inode_operations = {
2603 .setxattr = cgroup_setxattr,
2604 .getxattr = cgroup_getxattr,
2605 .listxattr = cgroup_listxattr,
2606 .removexattr = cgroup_removexattr,
2607};
2608
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002609static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002610 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002611 .mkdir = cgroup_mkdir,
2612 .rmdir = cgroup_rmdir,
2613 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002614 .setxattr = cgroup_setxattr,
2615 .getxattr = cgroup_getxattr,
2616 .listxattr = cgroup_listxattr,
2617 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002618};
2619
Al Viro00cd8dd2012-06-10 17:13:09 -04002620static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002621{
2622 if (dentry->d_name.len > NAME_MAX)
2623 return ERR_PTR(-ENAMETOOLONG);
2624 d_add(dentry, NULL);
2625 return NULL;
2626}
2627
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002628/*
2629 * Check if a file is a control file
2630 */
2631static inline struct cftype *__file_cft(struct file *file)
2632{
2633 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2634 return ERR_PTR(-EINVAL);
2635 return __d_cft(file->f_dentry);
2636}
2637
Al Viroa5e7ed32011-07-26 01:55:55 -04002638static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002639 struct super_block *sb)
2640{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002641 struct inode *inode;
2642
2643 if (!dentry)
2644 return -ENOENT;
2645 if (dentry->d_inode)
2646 return -EEXIST;
2647
2648 inode = cgroup_new_inode(mode, sb);
2649 if (!inode)
2650 return -ENOMEM;
2651
2652 if (S_ISDIR(mode)) {
2653 inode->i_op = &cgroup_dir_inode_operations;
2654 inode->i_fop = &simple_dir_operations;
2655
2656 /* start off with i_nlink == 2 (for "." entry) */
2657 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002658 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002659
Tejun Heob8a2df62012-11-19 08:13:37 -08002660 /*
2661 * Control reaches here with cgroup_mutex held.
2662 * @inode->i_mutex should nest outside cgroup_mutex but we
2663 * want to populate it immediately without releasing
2664 * cgroup_mutex. As @inode isn't visible to anyone else
2665 * yet, trylock will always succeed without affecting
2666 * lockdep checks.
2667 */
2668 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002669 } else if (S_ISREG(mode)) {
2670 inode->i_size = 0;
2671 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002672 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002673 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674 d_instantiate(dentry, inode);
2675 dget(dentry); /* Extra count - pin the dentry in core */
2676 return 0;
2677}
2678
Li Zefan099fca32009-04-02 16:57:29 -07002679/**
2680 * cgroup_file_mode - deduce file mode of a control file
2681 * @cft: the control file in question
2682 *
2683 * returns cft->mode if ->mode is not 0
2684 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2685 * returns S_IRUGO if it has only a read handler
2686 * returns S_IWUSR if it has only a write hander
2687 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002688static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002689{
Al Viroa5e7ed32011-07-26 01:55:55 -04002690 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002691
2692 if (cft->mode)
2693 return cft->mode;
2694
2695 if (cft->read || cft->read_u64 || cft->read_s64 ||
2696 cft->read_map || cft->read_seq_string)
2697 mode |= S_IRUGO;
2698
2699 if (cft->write || cft->write_u64 || cft->write_s64 ||
2700 cft->write_string || cft->trigger)
2701 mode |= S_IWUSR;
2702
2703 return mode;
2704}
2705
Tejun Heodb0416b2012-04-01 12:09:55 -07002706static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002707 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002708{
Paul Menagebd89aab2007-10-18 23:40:44 -07002709 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002710 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002711 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002712 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002713 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002714 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002715 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002716
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002717 simple_xattrs_init(&cft->xattrs);
2718
Tejun Heo8e3f6542012-04-01 12:09:55 -07002719 /* does @cft->flags tell us to skip creation on @cgrp? */
2720 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2721 return 0;
2722 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2723 return 0;
2724
Paul Menagebd89aab2007-10-18 23:40:44 -07002725 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002726 strcpy(name, subsys->name);
2727 strcat(name, ".");
2728 }
2729 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002730
Paul Menageddbcc7e2007-10-18 23:39:30 -07002731 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002732
2733 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2734 if (!cfe)
2735 return -ENOMEM;
2736
Paul Menageddbcc7e2007-10-18 23:39:30 -07002737 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002738 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002739 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002740 goto out;
2741 }
2742
2743 mode = cgroup_file_mode(cft);
2744 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2745 if (!error) {
2746 cfe->type = (void *)cft;
2747 cfe->dentry = dentry;
2748 dentry->d_fsdata = cfe;
2749 list_add_tail(&cfe->node, &parent->files);
2750 cfe = NULL;
2751 }
2752 dput(dentry);
2753out:
2754 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002755 return error;
2756}
2757
Tejun Heo79578622012-04-01 12:09:56 -07002758static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002759 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002760{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002761 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002762 int err, ret = 0;
2763
2764 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Tejun Heo79578622012-04-01 12:09:56 -07002765 if (is_add)
2766 err = cgroup_add_file(cgrp, subsys, cft);
2767 else
2768 err = cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002769 if (err) {
Tejun Heo79578622012-04-01 12:09:56 -07002770 pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n",
2771 is_add ? "add" : "remove", cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002772 ret = err;
2773 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002774 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002775 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002776}
2777
Tejun Heo8e3f6542012-04-01 12:09:55 -07002778static DEFINE_MUTEX(cgroup_cft_mutex);
2779
2780static void cgroup_cfts_prepare(void)
2781 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2782{
2783 /*
2784 * Thanks to the entanglement with vfs inode locking, we can't walk
2785 * the existing cgroups under cgroup_mutex and create files.
2786 * Instead, we increment reference on all cgroups and build list of
2787 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2788 * exclusive access to the field.
2789 */
2790 mutex_lock(&cgroup_cft_mutex);
2791 mutex_lock(&cgroup_mutex);
2792}
2793
2794static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002795 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002796 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2797{
2798 LIST_HEAD(pending);
2799 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002800
2801 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2802 if (cfts && ss->root != &rootnode) {
2803 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2804 dget(cgrp->dentry);
2805 list_add_tail(&cgrp->cft_q_node, &pending);
2806 }
2807 }
2808
2809 mutex_unlock(&cgroup_mutex);
2810
2811 /*
2812 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2813 * files for all cgroups which were created before.
2814 */
2815 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2816 struct inode *inode = cgrp->dentry->d_inode;
2817
2818 mutex_lock(&inode->i_mutex);
2819 mutex_lock(&cgroup_mutex);
2820 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002821 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002822 mutex_unlock(&cgroup_mutex);
2823 mutex_unlock(&inode->i_mutex);
2824
2825 list_del_init(&cgrp->cft_q_node);
2826 dput(cgrp->dentry);
2827 }
2828
2829 mutex_unlock(&cgroup_cft_mutex);
2830}
2831
2832/**
2833 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2834 * @ss: target cgroup subsystem
2835 * @cfts: zero-length name terminated array of cftypes
2836 *
2837 * Register @cfts to @ss. Files described by @cfts are created for all
2838 * existing cgroups to which @ss is attached and all future cgroups will
2839 * have them too. This function can be called anytime whether @ss is
2840 * attached or not.
2841 *
2842 * Returns 0 on successful registration, -errno on failure. Note that this
2843 * function currently returns 0 as long as @cfts registration is successful
2844 * even if some file creation attempts on existing cgroups fail.
2845 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002846int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002847{
2848 struct cftype_set *set;
2849
2850 set = kzalloc(sizeof(*set), GFP_KERNEL);
2851 if (!set)
2852 return -ENOMEM;
2853
2854 cgroup_cfts_prepare();
2855 set->cfts = cfts;
2856 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002857 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002858
2859 return 0;
2860}
2861EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2862
Li Zefana043e3b2008-02-23 15:24:09 -08002863/**
Tejun Heo79578622012-04-01 12:09:56 -07002864 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2865 * @ss: target cgroup subsystem
2866 * @cfts: zero-length name terminated array of cftypes
2867 *
2868 * Unregister @cfts from @ss. Files described by @cfts are removed from
2869 * all existing cgroups to which @ss is attached and all future cgroups
2870 * won't have them either. This function can be called anytime whether @ss
2871 * is attached or not.
2872 *
2873 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2874 * registered with @ss.
2875 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002876int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002877{
2878 struct cftype_set *set;
2879
2880 cgroup_cfts_prepare();
2881
2882 list_for_each_entry(set, &ss->cftsets, node) {
2883 if (set->cfts == cfts) {
2884 list_del_init(&set->node);
2885 cgroup_cfts_commit(ss, cfts, false);
2886 return 0;
2887 }
2888 }
2889
2890 cgroup_cfts_commit(ss, NULL, false);
2891 return -ENOENT;
2892}
2893
2894/**
Li Zefana043e3b2008-02-23 15:24:09 -08002895 * cgroup_task_count - count the number of tasks in a cgroup.
2896 * @cgrp: the cgroup in question
2897 *
2898 * Return the number of tasks in the cgroup.
2899 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002900int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002901{
2902 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002903 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002904
Paul Menage817929e2007-10-18 23:39:36 -07002905 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002906 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002907 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002908 }
2909 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002910 return count;
2911}
2912
2913/*
Paul Menage817929e2007-10-18 23:39:36 -07002914 * Advance a list_head iterator. The iterator should be positioned at
2915 * the start of a css_set
2916 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002917static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002918 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002919{
2920 struct list_head *l = it->cg_link;
2921 struct cg_cgroup_link *link;
2922 struct css_set *cg;
2923
2924 /* Advance to the next non-empty css_set */
2925 do {
2926 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002927 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002928 it->cg_link = NULL;
2929 return;
2930 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002931 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002932 cg = link->cg;
2933 } while (list_empty(&cg->tasks));
2934 it->cg_link = l;
2935 it->task = cg->tasks.next;
2936}
2937
Cliff Wickman31a7df02008-02-07 00:14:42 -08002938/*
2939 * To reduce the fork() overhead for systems that are not actually
2940 * using their cgroups capability, we don't maintain the lists running
2941 * through each css_set to its tasks until we see the list actually
2942 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002943 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002944static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002945{
2946 struct task_struct *p, *g;
2947 write_lock(&css_set_lock);
2948 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002949 /*
2950 * We need tasklist_lock because RCU is not safe against
2951 * while_each_thread(). Besides, a forking task that has passed
2952 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2953 * is not guaranteed to have its child immediately visible in the
2954 * tasklist if we walk through it with RCU.
2955 */
2956 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002957 do_each_thread(g, p) {
2958 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002959 /*
2960 * We should check if the process is exiting, otherwise
2961 * it will race with cgroup_exit() in that the list
2962 * entry won't be deleted though the process has exited.
2963 */
2964 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002965 list_add(&p->cg_list, &p->cgroups->tasks);
2966 task_unlock(p);
2967 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002968 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002969 write_unlock(&css_set_lock);
2970}
2971
Tejun Heo574bd9f2012-11-09 09:12:29 -08002972/**
2973 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2974 * @pos: the current position (%NULL to initiate traversal)
2975 * @cgroup: cgroup whose descendants to walk
2976 *
2977 * To be used by cgroup_for_each_descendant_pre(). Find the next
2978 * descendant to visit for pre-order traversal of @cgroup's descendants.
2979 */
2980struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2981 struct cgroup *cgroup)
2982{
2983 struct cgroup *next;
2984
2985 WARN_ON_ONCE(!rcu_read_lock_held());
2986
2987 /* if first iteration, pretend we just visited @cgroup */
2988 if (!pos) {
2989 if (list_empty(&cgroup->children))
2990 return NULL;
2991 pos = cgroup;
2992 }
2993
2994 /* visit the first child if exists */
2995 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2996 if (next)
2997 return next;
2998
2999 /* no child, visit my or the closest ancestor's next sibling */
3000 do {
3001 next = list_entry_rcu(pos->sibling.next, struct cgroup,
3002 sibling);
3003 if (&next->sibling != &pos->parent->children)
3004 return next;
3005
3006 pos = pos->parent;
3007 } while (pos != cgroup);
3008
3009 return NULL;
3010}
3011EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3012
3013static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3014{
3015 struct cgroup *last;
3016
3017 do {
3018 last = pos;
3019 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3020 sibling);
3021 } while (pos);
3022
3023 return last;
3024}
3025
3026/**
3027 * cgroup_next_descendant_post - find the next descendant for post-order walk
3028 * @pos: the current position (%NULL to initiate traversal)
3029 * @cgroup: cgroup whose descendants to walk
3030 *
3031 * To be used by cgroup_for_each_descendant_post(). Find the next
3032 * descendant to visit for post-order traversal of @cgroup's descendants.
3033 */
3034struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3035 struct cgroup *cgroup)
3036{
3037 struct cgroup *next;
3038
3039 WARN_ON_ONCE(!rcu_read_lock_held());
3040
3041 /* if first iteration, visit the leftmost descendant */
3042 if (!pos) {
3043 next = cgroup_leftmost_descendant(cgroup);
3044 return next != cgroup ? next : NULL;
3045 }
3046
3047 /* if there's an unvisited sibling, visit its leftmost descendant */
3048 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3049 if (&next->sibling != &pos->parent->children)
3050 return cgroup_leftmost_descendant(next);
3051
3052 /* no sibling left, visit parent */
3053 next = pos->parent;
3054 return next != cgroup ? next : NULL;
3055}
3056EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3057
Paul Menagebd89aab2007-10-18 23:40:44 -07003058void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003059 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003060{
3061 /*
3062 * The first time anyone tries to iterate across a cgroup,
3063 * we need to enable the list linking each css_set to its
3064 * tasks, and fix up all existing tasks.
3065 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003066 if (!use_task_css_set_links)
3067 cgroup_enable_task_cg_lists();
3068
Paul Menage817929e2007-10-18 23:39:36 -07003069 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003070 it->cg_link = &cgrp->css_sets;
3071 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003072}
3073
Paul Menagebd89aab2007-10-18 23:40:44 -07003074struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003075 struct cgroup_iter *it)
3076{
3077 struct task_struct *res;
3078 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003079 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003080
3081 /* If the iterator cg is NULL, we have no tasks */
3082 if (!it->cg_link)
3083 return NULL;
3084 res = list_entry(l, struct task_struct, cg_list);
3085 /* Advance iterator to find next entry */
3086 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003087 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3088 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003089 /* We reached the end of this task list - move on to
3090 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003091 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003092 } else {
3093 it->task = l;
3094 }
3095 return res;
3096}
3097
Paul Menagebd89aab2007-10-18 23:40:44 -07003098void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003099 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003100{
3101 read_unlock(&css_set_lock);
3102}
3103
Cliff Wickman31a7df02008-02-07 00:14:42 -08003104static inline int started_after_time(struct task_struct *t1,
3105 struct timespec *time,
3106 struct task_struct *t2)
3107{
3108 int start_diff = timespec_compare(&t1->start_time, time);
3109 if (start_diff > 0) {
3110 return 1;
3111 } else if (start_diff < 0) {
3112 return 0;
3113 } else {
3114 /*
3115 * Arbitrarily, if two processes started at the same
3116 * time, we'll say that the lower pointer value
3117 * started first. Note that t2 may have exited by now
3118 * so this may not be a valid pointer any longer, but
3119 * that's fine - it still serves to distinguish
3120 * between two tasks started (effectively) simultaneously.
3121 */
3122 return t1 > t2;
3123 }
3124}
3125
3126/*
3127 * This function is a callback from heap_insert() and is used to order
3128 * the heap.
3129 * In this case we order the heap in descending task start time.
3130 */
3131static inline int started_after(void *p1, void *p2)
3132{
3133 struct task_struct *t1 = p1;
3134 struct task_struct *t2 = p2;
3135 return started_after_time(t1, &t2->start_time, t2);
3136}
3137
3138/**
3139 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3140 * @scan: struct cgroup_scanner containing arguments for the scan
3141 *
3142 * Arguments include pointers to callback functions test_task() and
3143 * process_task().
3144 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3145 * and if it returns true, call process_task() for it also.
3146 * The test_task pointer may be NULL, meaning always true (select all tasks).
3147 * Effectively duplicates cgroup_iter_{start,next,end}()
3148 * but does not lock css_set_lock for the call to process_task().
3149 * The struct cgroup_scanner may be embedded in any structure of the caller's
3150 * creation.
3151 * It is guaranteed that process_task() will act on every task that
3152 * is a member of the cgroup for the duration of this call. This
3153 * function may or may not call process_task() for tasks that exit
3154 * or move to a different cgroup during the call, or are forked or
3155 * move into the cgroup during the call.
3156 *
3157 * Note that test_task() may be called with locks held, and may in some
3158 * situations be called multiple times for the same task, so it should
3159 * be cheap.
3160 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3161 * pre-allocated and will be used for heap operations (and its "gt" member will
3162 * be overwritten), else a temporary heap will be used (allocation of which
3163 * may cause this function to fail).
3164 */
3165int cgroup_scan_tasks(struct cgroup_scanner *scan)
3166{
3167 int retval, i;
3168 struct cgroup_iter it;
3169 struct task_struct *p, *dropped;
3170 /* Never dereference latest_task, since it's not refcounted */
3171 struct task_struct *latest_task = NULL;
3172 struct ptr_heap tmp_heap;
3173 struct ptr_heap *heap;
3174 struct timespec latest_time = { 0, 0 };
3175
3176 if (scan->heap) {
3177 /* The caller supplied our heap and pre-allocated its memory */
3178 heap = scan->heap;
3179 heap->gt = &started_after;
3180 } else {
3181 /* We need to allocate our own heap memory */
3182 heap = &tmp_heap;
3183 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3184 if (retval)
3185 /* cannot allocate the heap */
3186 return retval;
3187 }
3188
3189 again:
3190 /*
3191 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3192 * to determine which are of interest, and using the scanner's
3193 * "process_task" callback to process any of them that need an update.
3194 * Since we don't want to hold any locks during the task updates,
3195 * gather tasks to be processed in a heap structure.
3196 * The heap is sorted by descending task start time.
3197 * If the statically-sized heap fills up, we overflow tasks that
3198 * started later, and in future iterations only consider tasks that
3199 * started after the latest task in the previous pass. This
3200 * guarantees forward progress and that we don't miss any tasks.
3201 */
3202 heap->size = 0;
3203 cgroup_iter_start(scan->cg, &it);
3204 while ((p = cgroup_iter_next(scan->cg, &it))) {
3205 /*
3206 * Only affect tasks that qualify per the caller's callback,
3207 * if he provided one
3208 */
3209 if (scan->test_task && !scan->test_task(p, scan))
3210 continue;
3211 /*
3212 * Only process tasks that started after the last task
3213 * we processed
3214 */
3215 if (!started_after_time(p, &latest_time, latest_task))
3216 continue;
3217 dropped = heap_insert(heap, p);
3218 if (dropped == NULL) {
3219 /*
3220 * The new task was inserted; the heap wasn't
3221 * previously full
3222 */
3223 get_task_struct(p);
3224 } else if (dropped != p) {
3225 /*
3226 * The new task was inserted, and pushed out a
3227 * different task
3228 */
3229 get_task_struct(p);
3230 put_task_struct(dropped);
3231 }
3232 /*
3233 * Else the new task was newer than anything already in
3234 * the heap and wasn't inserted
3235 */
3236 }
3237 cgroup_iter_end(scan->cg, &it);
3238
3239 if (heap->size) {
3240 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003241 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003242 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003243 latest_time = q->start_time;
3244 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003245 }
3246 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003247 scan->process_task(q, scan);
3248 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003249 }
3250 /*
3251 * If we had to process any tasks at all, scan again
3252 * in case some of them were in the middle of forking
3253 * children that didn't get processed.
3254 * Not the most efficient way to do it, but it avoids
3255 * having to take callback_mutex in the fork path
3256 */
3257 goto again;
3258 }
3259 if (heap == &tmp_heap)
3260 heap_free(&tmp_heap);
3261 return 0;
3262}
3263
Paul Menage817929e2007-10-18 23:39:36 -07003264/*
Ben Blum102a7752009-09-23 15:56:26 -07003265 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003266 *
3267 * Reading this file can return large amounts of data if a cgroup has
3268 * *lots* of attached tasks. So it may need several calls to read(),
3269 * but we cannot guarantee that the information we produce is correct
3270 * unless we produce it entirely atomically.
3271 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003272 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003273
Li Zefan24528252012-01-20 11:58:43 +08003274/* which pidlist file are we talking about? */
3275enum cgroup_filetype {
3276 CGROUP_FILE_PROCS,
3277 CGROUP_FILE_TASKS,
3278};
3279
3280/*
3281 * A pidlist is a list of pids that virtually represents the contents of one
3282 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3283 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3284 * to the cgroup.
3285 */
3286struct cgroup_pidlist {
3287 /*
3288 * used to find which pidlist is wanted. doesn't change as long as
3289 * this particular list stays in the list.
3290 */
3291 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3292 /* array of xids */
3293 pid_t *list;
3294 /* how many elements the above list has */
3295 int length;
3296 /* how many files are using the current array */
3297 int use_count;
3298 /* each of these stored in a list by its cgroup */
3299 struct list_head links;
3300 /* pointer to the cgroup we belong to, for list removal purposes */
3301 struct cgroup *owner;
3302 /* protects the other fields */
3303 struct rw_semaphore mutex;
3304};
3305
Paul Menagebbcb81d2007-10-18 23:39:32 -07003306/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003307 * The following two functions "fix" the issue where there are more pids
3308 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3309 * TODO: replace with a kernel-wide solution to this problem
3310 */
3311#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3312static void *pidlist_allocate(int count)
3313{
3314 if (PIDLIST_TOO_LARGE(count))
3315 return vmalloc(count * sizeof(pid_t));
3316 else
3317 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3318}
3319static void pidlist_free(void *p)
3320{
3321 if (is_vmalloc_addr(p))
3322 vfree(p);
3323 else
3324 kfree(p);
3325}
3326static void *pidlist_resize(void *p, int newcount)
3327{
3328 void *newlist;
3329 /* note: if new alloc fails, old p will still be valid either way */
3330 if (is_vmalloc_addr(p)) {
3331 newlist = vmalloc(newcount * sizeof(pid_t));
3332 if (!newlist)
3333 return NULL;
3334 memcpy(newlist, p, newcount * sizeof(pid_t));
3335 vfree(p);
3336 } else {
3337 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3338 }
3339 return newlist;
3340}
3341
3342/*
Ben Blum102a7752009-09-23 15:56:26 -07003343 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3344 * If the new stripped list is sufficiently smaller and there's enough memory
3345 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3346 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003347 */
Ben Blum102a7752009-09-23 15:56:26 -07003348/* is the size difference enough that we should re-allocate the array? */
3349#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3350static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003351{
Ben Blum102a7752009-09-23 15:56:26 -07003352 int src, dest = 1;
3353 pid_t *list = *p;
3354 pid_t *newlist;
3355
3356 /*
3357 * we presume the 0th element is unique, so i starts at 1. trivial
3358 * edge cases first; no work needs to be done for either
3359 */
3360 if (length == 0 || length == 1)
3361 return length;
3362 /* src and dest walk down the list; dest counts unique elements */
3363 for (src = 1; src < length; src++) {
3364 /* find next unique element */
3365 while (list[src] == list[src-1]) {
3366 src++;
3367 if (src == length)
3368 goto after;
3369 }
3370 /* dest always points to where the next unique element goes */
3371 list[dest] = list[src];
3372 dest++;
3373 }
3374after:
3375 /*
3376 * if the length difference is large enough, we want to allocate a
3377 * smaller buffer to save memory. if this fails due to out of memory,
3378 * we'll just stay with what we've got.
3379 */
3380 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003381 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003382 if (newlist)
3383 *p = newlist;
3384 }
3385 return dest;
3386}
3387
3388static int cmppid(const void *a, const void *b)
3389{
3390 return *(pid_t *)a - *(pid_t *)b;
3391}
3392
3393/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003394 * find the appropriate pidlist for our purpose (given procs vs tasks)
3395 * returns with the lock on that pidlist already held, and takes care
3396 * of the use count, or returns NULL with no locks held if we're out of
3397 * memory.
3398 */
3399static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3400 enum cgroup_filetype type)
3401{
3402 struct cgroup_pidlist *l;
3403 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08003404 struct pid_namespace *ns = current->nsproxy->pid_ns;
3405
Ben Blum72a8cb32009-09-23 15:56:27 -07003406 /*
3407 * We can't drop the pidlist_mutex before taking the l->mutex in case
3408 * the last ref-holder is trying to remove l from the list at the same
3409 * time. Holding the pidlist_mutex precludes somebody taking whichever
3410 * list we find out from under us - compare release_pid_array().
3411 */
3412 mutex_lock(&cgrp->pidlist_mutex);
3413 list_for_each_entry(l, &cgrp->pidlists, links) {
3414 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003415 /* make sure l doesn't vanish out from under us */
3416 down_write(&l->mutex);
3417 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003418 return l;
3419 }
3420 }
3421 /* entry not found; create a new one */
3422 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3423 if (!l) {
3424 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003425 return l;
3426 }
3427 init_rwsem(&l->mutex);
3428 down_write(&l->mutex);
3429 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003430 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003431 l->use_count = 0; /* don't increment here */
3432 l->list = NULL;
3433 l->owner = cgrp;
3434 list_add(&l->links, &cgrp->pidlists);
3435 mutex_unlock(&cgrp->pidlist_mutex);
3436 return l;
3437}
3438
3439/*
Ben Blum102a7752009-09-23 15:56:26 -07003440 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3441 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003442static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3443 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003444{
3445 pid_t *array;
3446 int length;
3447 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003448 struct cgroup_iter it;
3449 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003450 struct cgroup_pidlist *l;
3451
3452 /*
3453 * If cgroup gets more users after we read count, we won't have
3454 * enough space - tough. This race is indistinguishable to the
3455 * caller from the case that the additional cgroup users didn't
3456 * show up until sometime later on.
3457 */
3458 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003459 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003460 if (!array)
3461 return -ENOMEM;
3462 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003463 cgroup_iter_start(cgrp, &it);
3464 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003465 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003466 break;
Ben Blum102a7752009-09-23 15:56:26 -07003467 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003468 if (type == CGROUP_FILE_PROCS)
3469 pid = task_tgid_vnr(tsk);
3470 else
3471 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003472 if (pid > 0) /* make sure to only use valid results */
3473 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003474 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003475 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003476 length = n;
3477 /* now sort & (if procs) strip out duplicates */
3478 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003479 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003480 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003481 l = cgroup_pidlist_find(cgrp, type);
3482 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003483 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003484 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003485 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003486 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003487 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003488 l->list = array;
3489 l->length = length;
3490 l->use_count++;
3491 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003492 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003493 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003494}
3495
Balbir Singh846c7bb2007-10-18 23:39:44 -07003496/**
Li Zefana043e3b2008-02-23 15:24:09 -08003497 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003498 * @stats: cgroupstats to fill information into
3499 * @dentry: A dentry entry belonging to the cgroup for which stats have
3500 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003501 *
3502 * Build and fill cgroupstats so that taskstats can export it to user
3503 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003504 */
3505int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3506{
3507 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003508 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003509 struct cgroup_iter it;
3510 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003511
Balbir Singh846c7bb2007-10-18 23:39:44 -07003512 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003513 * Validate dentry by checking the superblock operations,
3514 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003515 */
Li Zefan33d283b2008-11-19 15:36:48 -08003516 if (dentry->d_sb->s_op != &cgroup_ops ||
3517 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003518 goto err;
3519
3520 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003521 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003522
Paul Menagebd89aab2007-10-18 23:40:44 -07003523 cgroup_iter_start(cgrp, &it);
3524 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003525 switch (tsk->state) {
3526 case TASK_RUNNING:
3527 stats->nr_running++;
3528 break;
3529 case TASK_INTERRUPTIBLE:
3530 stats->nr_sleeping++;
3531 break;
3532 case TASK_UNINTERRUPTIBLE:
3533 stats->nr_uninterruptible++;
3534 break;
3535 case TASK_STOPPED:
3536 stats->nr_stopped++;
3537 break;
3538 default:
3539 if (delayacct_is_task_waiting_on_io(tsk))
3540 stats->nr_io_wait++;
3541 break;
3542 }
3543 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003544 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003545
Balbir Singh846c7bb2007-10-18 23:39:44 -07003546err:
3547 return ret;
3548}
3549
Paul Menage8f3ff202009-09-23 15:56:25 -07003550
Paul Menagecc31edc2008-10-18 20:28:04 -07003551/*
Ben Blum102a7752009-09-23 15:56:26 -07003552 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003553 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003554 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003555 */
3556
Ben Blum102a7752009-09-23 15:56:26 -07003557static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003558{
3559 /*
3560 * Initially we receive a position value that corresponds to
3561 * one more than the last pid shown (or 0 on the first call or
3562 * after a seek to the start). Use a binary-search to find the
3563 * next pid to display, if any
3564 */
Ben Blum102a7752009-09-23 15:56:26 -07003565 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003566 int index = 0, pid = *pos;
3567 int *iter;
3568
Ben Blum102a7752009-09-23 15:56:26 -07003569 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003570 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003571 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003572
Paul Menagecc31edc2008-10-18 20:28:04 -07003573 while (index < end) {
3574 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003575 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003576 index = mid;
3577 break;
Ben Blum102a7752009-09-23 15:56:26 -07003578 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003579 index = mid + 1;
3580 else
3581 end = mid;
3582 }
3583 }
3584 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003585 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003586 return NULL;
3587 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003588 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003589 *pos = *iter;
3590 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003591}
3592
Ben Blum102a7752009-09-23 15:56:26 -07003593static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003594{
Ben Blum102a7752009-09-23 15:56:26 -07003595 struct cgroup_pidlist *l = s->private;
3596 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003597}
3598
Ben Blum102a7752009-09-23 15:56:26 -07003599static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003600{
Ben Blum102a7752009-09-23 15:56:26 -07003601 struct cgroup_pidlist *l = s->private;
3602 pid_t *p = v;
3603 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003604 /*
3605 * Advance to the next pid in the array. If this goes off the
3606 * end, we're done
3607 */
3608 p++;
3609 if (p >= end) {
3610 return NULL;
3611 } else {
3612 *pos = *p;
3613 return p;
3614 }
3615}
3616
Ben Blum102a7752009-09-23 15:56:26 -07003617static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003618{
3619 return seq_printf(s, "%d\n", *(int *)v);
3620}
3621
Ben Blum102a7752009-09-23 15:56:26 -07003622/*
3623 * seq_operations functions for iterating on pidlists through seq_file -
3624 * independent of whether it's tasks or procs
3625 */
3626static const struct seq_operations cgroup_pidlist_seq_operations = {
3627 .start = cgroup_pidlist_start,
3628 .stop = cgroup_pidlist_stop,
3629 .next = cgroup_pidlist_next,
3630 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003631};
3632
Ben Blum102a7752009-09-23 15:56:26 -07003633static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003634{
Ben Blum72a8cb32009-09-23 15:56:27 -07003635 /*
3636 * the case where we're the last user of this particular pidlist will
3637 * have us remove it from the cgroup's list, which entails taking the
3638 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3639 * pidlist_mutex, we have to take pidlist_mutex first.
3640 */
3641 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003642 down_write(&l->mutex);
3643 BUG_ON(!l->use_count);
3644 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003645 /* we're the last user if refcount is 0; remove and free */
3646 list_del(&l->links);
3647 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003648 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003649 put_pid_ns(l->key.ns);
3650 up_write(&l->mutex);
3651 kfree(l);
3652 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003653 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003654 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003655 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003656}
3657
Ben Blum102a7752009-09-23 15:56:26 -07003658static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003659{
Ben Blum102a7752009-09-23 15:56:26 -07003660 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003661 if (!(file->f_mode & FMODE_READ))
3662 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003663 /*
3664 * the seq_file will only be initialized if the file was opened for
3665 * reading; hence we check if it's not null only in that case.
3666 */
3667 l = ((struct seq_file *)file->private_data)->private;
3668 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003669 return seq_release(inode, file);
3670}
3671
Ben Blum102a7752009-09-23 15:56:26 -07003672static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003673 .read = seq_read,
3674 .llseek = seq_lseek,
3675 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003676 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003677};
3678
3679/*
Ben Blum102a7752009-09-23 15:56:26 -07003680 * The following functions handle opens on a file that displays a pidlist
3681 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3682 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003683 */
Ben Blum102a7752009-09-23 15:56:26 -07003684/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003685static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003686{
3687 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003688 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003689 int retval;
3690
3691 /* Nothing to do for write-only files */
3692 if (!(file->f_mode & FMODE_READ))
3693 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003694
Ben Blum102a7752009-09-23 15:56:26 -07003695 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003696 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003697 if (retval)
3698 return retval;
3699 /* configure file information */
3700 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003701
Ben Blum102a7752009-09-23 15:56:26 -07003702 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003703 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003704 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003705 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003706 }
Ben Blum102a7752009-09-23 15:56:26 -07003707 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003708 return 0;
3709}
Ben Blum102a7752009-09-23 15:56:26 -07003710static int cgroup_tasks_open(struct inode *unused, struct file *file)
3711{
Ben Blum72a8cb32009-09-23 15:56:27 -07003712 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003713}
3714static int cgroup_procs_open(struct inode *unused, struct file *file)
3715{
Ben Blum72a8cb32009-09-23 15:56:27 -07003716 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003717}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003718
Paul Menagebd89aab2007-10-18 23:40:44 -07003719static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003720 struct cftype *cft)
3721{
Paul Menagebd89aab2007-10-18 23:40:44 -07003722 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003723}
3724
Paul Menage6379c102008-07-25 01:47:01 -07003725static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3726 struct cftype *cft,
3727 u64 val)
3728{
3729 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3730 if (val)
3731 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3732 else
3733 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3734 return 0;
3735}
3736
Paul Menagebbcb81d2007-10-18 23:39:32 -07003737/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003738 * Unregister event and free resources.
3739 *
3740 * Gets called from workqueue.
3741 */
3742static void cgroup_event_remove(struct work_struct *work)
3743{
3744 struct cgroup_event *event = container_of(work, struct cgroup_event,
3745 remove);
3746 struct cgroup *cgrp = event->cgrp;
3747
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003748 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3749
3750 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003751 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003752 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003753}
3754
3755/*
3756 * Gets called on POLLHUP on eventfd when user closes it.
3757 *
3758 * Called with wqh->lock held and interrupts disabled.
3759 */
3760static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3761 int sync, void *key)
3762{
3763 struct cgroup_event *event = container_of(wait,
3764 struct cgroup_event, wait);
3765 struct cgroup *cgrp = event->cgrp;
3766 unsigned long flags = (unsigned long)key;
3767
3768 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003769 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003770 spin_lock(&cgrp->event_list_lock);
3771 list_del(&event->list);
3772 spin_unlock(&cgrp->event_list_lock);
3773 /*
3774 * We are in atomic context, but cgroup_event_remove() may
3775 * sleep, so we have to call it in workqueue.
3776 */
3777 schedule_work(&event->remove);
3778 }
3779
3780 return 0;
3781}
3782
3783static void cgroup_event_ptable_queue_proc(struct file *file,
3784 wait_queue_head_t *wqh, poll_table *pt)
3785{
3786 struct cgroup_event *event = container_of(pt,
3787 struct cgroup_event, pt);
3788
3789 event->wqh = wqh;
3790 add_wait_queue(wqh, &event->wait);
3791}
3792
3793/*
3794 * Parse input and register new cgroup event handler.
3795 *
3796 * Input must be in format '<event_fd> <control_fd> <args>'.
3797 * Interpretation of args is defined by control file implementation.
3798 */
3799static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3800 const char *buffer)
3801{
3802 struct cgroup_event *event = NULL;
3803 unsigned int efd, cfd;
3804 struct file *efile = NULL;
3805 struct file *cfile = NULL;
3806 char *endp;
3807 int ret;
3808
3809 efd = simple_strtoul(buffer, &endp, 10);
3810 if (*endp != ' ')
3811 return -EINVAL;
3812 buffer = endp + 1;
3813
3814 cfd = simple_strtoul(buffer, &endp, 10);
3815 if ((*endp != ' ') && (*endp != '\0'))
3816 return -EINVAL;
3817 buffer = endp + 1;
3818
3819 event = kzalloc(sizeof(*event), GFP_KERNEL);
3820 if (!event)
3821 return -ENOMEM;
3822 event->cgrp = cgrp;
3823 INIT_LIST_HEAD(&event->list);
3824 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3825 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3826 INIT_WORK(&event->remove, cgroup_event_remove);
3827
3828 efile = eventfd_fget(efd);
3829 if (IS_ERR(efile)) {
3830 ret = PTR_ERR(efile);
3831 goto fail;
3832 }
3833
3834 event->eventfd = eventfd_ctx_fileget(efile);
3835 if (IS_ERR(event->eventfd)) {
3836 ret = PTR_ERR(event->eventfd);
3837 goto fail;
3838 }
3839
3840 cfile = fget(cfd);
3841 if (!cfile) {
3842 ret = -EBADF;
3843 goto fail;
3844 }
3845
3846 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003847 /* AV: shouldn't we check that it's been opened for read instead? */
3848 ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003849 if (ret < 0)
3850 goto fail;
3851
3852 event->cft = __file_cft(cfile);
3853 if (IS_ERR(event->cft)) {
3854 ret = PTR_ERR(event->cft);
3855 goto fail;
3856 }
3857
3858 if (!event->cft->register_event || !event->cft->unregister_event) {
3859 ret = -EINVAL;
3860 goto fail;
3861 }
3862
3863 ret = event->cft->register_event(cgrp, event->cft,
3864 event->eventfd, buffer);
3865 if (ret)
3866 goto fail;
3867
3868 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3869 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3870 ret = 0;
3871 goto fail;
3872 }
3873
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003874 /*
3875 * Events should be removed after rmdir of cgroup directory, but before
3876 * destroying subsystem state objects. Let's take reference to cgroup
3877 * directory dentry to do that.
3878 */
3879 dget(cgrp->dentry);
3880
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003881 spin_lock(&cgrp->event_list_lock);
3882 list_add(&event->list, &cgrp->event_list);
3883 spin_unlock(&cgrp->event_list_lock);
3884
3885 fput(cfile);
3886 fput(efile);
3887
3888 return 0;
3889
3890fail:
3891 if (cfile)
3892 fput(cfile);
3893
3894 if (event && event->eventfd && !IS_ERR(event->eventfd))
3895 eventfd_ctx_put(event->eventfd);
3896
3897 if (!IS_ERR_OR_NULL(efile))
3898 fput(efile);
3899
3900 kfree(event);
3901
3902 return ret;
3903}
3904
Daniel Lezcano97978e62010-10-27 15:33:35 -07003905static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3906 struct cftype *cft)
3907{
3908 return clone_children(cgrp);
3909}
3910
3911static int cgroup_clone_children_write(struct cgroup *cgrp,
3912 struct cftype *cft,
3913 u64 val)
3914{
3915 if (val)
3916 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3917 else
3918 clear_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3919 return 0;
3920}
3921
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003922/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003923 * for the common functions, 'private' gives the type of file
3924 */
Ben Blum102a7752009-09-23 15:56:26 -07003925/* for hysterical raisins, we can't put this on the older files */
3926#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003927static struct cftype files[] = {
3928 {
3929 .name = "tasks",
3930 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003931 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003932 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003933 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003934 },
Ben Blum102a7752009-09-23 15:56:26 -07003935 {
3936 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3937 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003938 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003939 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003940 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003941 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003942 {
3943 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003944 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003945 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003946 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003947 {
3948 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3949 .write_string = cgroup_write_event_control,
3950 .mode = S_IWUGO,
3951 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003952 {
3953 .name = "cgroup.clone_children",
3954 .read_u64 = cgroup_clone_children_read,
3955 .write_u64 = cgroup_clone_children_write,
3956 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003957 {
3958 .name = "release_agent",
3959 .flags = CFTYPE_ONLY_ON_ROOT,
3960 .read_seq_string = cgroup_release_agent_show,
3961 .write_string = cgroup_release_agent_write,
3962 .max_write_len = PATH_MAX,
3963 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003964 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003965};
3966
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003967/**
3968 * cgroup_populate_dir - selectively creation of files in a directory
3969 * @cgrp: target cgroup
3970 * @base_files: true if the base files should be added
3971 * @subsys_mask: mask of the subsystem ids whose files should be added
3972 */
3973static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3974 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003975{
3976 int err;
3977 struct cgroup_subsys *ss;
3978
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003979 if (base_files) {
3980 err = cgroup_addrm_files(cgrp, NULL, files, true);
3981 if (err < 0)
3982 return err;
3983 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07003984
Tejun Heo8e3f6542012-04-01 12:09:55 -07003985 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07003986 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003987 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003988 if (!test_bit(ss->subsys_id, &subsys_mask))
3989 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003990
Tejun Heodb0416b2012-04-01 12:09:55 -07003991 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07003992 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003993 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07003994
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003995 /* This cgroup is ready now */
3996 for_each_subsys(cgrp->root, ss) {
3997 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3998 /*
3999 * Update id->css pointer and make this css visible from
4000 * CSS ID functions. This pointer will be dereferened
4001 * from RCU-read-side without locks.
4002 */
4003 if (css->id)
4004 rcu_assign_pointer(css->id->css, css);
4005 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004006
4007 return 0;
4008}
4009
Tejun Heo48ddbe12012-04-01 12:09:56 -07004010static void css_dput_fn(struct work_struct *work)
4011{
4012 struct cgroup_subsys_state *css =
4013 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004014 struct dentry *dentry = css->cgroup->dentry;
4015 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004016
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004017 atomic_inc(&sb->s_active);
4018 dput(dentry);
4019 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004020}
4021
Paul Menageddbcc7e2007-10-18 23:39:30 -07004022static void init_cgroup_css(struct cgroup_subsys_state *css,
4023 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004024 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004025{
Paul Menagebd89aab2007-10-18 23:40:44 -07004026 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004027 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004028 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004029 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004030 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004031 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004032 BUG_ON(cgrp->subsys[ss->subsys_id]);
4033 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004034
4035 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004036 * css holds an extra ref to @cgrp->dentry which is put on the last
4037 * css_put(). dput() requires process context, which css_put() may
4038 * be called without. @css->dput_work will be used to invoke
4039 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004040 */
4041 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004042}
4043
Tejun Heoa31f2d32012-11-19 08:13:37 -08004044/* invoke ->post_create() on a new CSS and mark it online */
4045static void online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4046{
4047 lockdep_assert_held(&cgroup_mutex);
4048
4049 if (ss->post_create)
4050 ss->post_create(cgrp);
4051 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4052}
4053
4054/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4055static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4056 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4057{
4058 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4059
4060 lockdep_assert_held(&cgroup_mutex);
4061
4062 if (!(css->flags & CSS_ONLINE))
4063 return;
4064
4065 /*
4066 * pre_destroy() should be called with cgroup_mutex unlocked. See
4067 * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for
4068 * details. This temporary unlocking should go away once
4069 * cgroup_mutex is unexported from controllers.
4070 */
4071 if (ss->pre_destroy) {
4072 mutex_unlock(&cgroup_mutex);
4073 ss->pre_destroy(cgrp);
4074 mutex_lock(&cgroup_mutex);
4075 }
4076
4077 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4078}
4079
Paul Menageddbcc7e2007-10-18 23:39:30 -07004080/*
Li Zefana043e3b2008-02-23 15:24:09 -08004081 * cgroup_create - create a cgroup
4082 * @parent: cgroup that will be parent of the new cgroup
4083 * @dentry: dentry of the new cgroup
4084 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004085 *
Li Zefana043e3b2008-02-23 15:24:09 -08004086 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004087 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004088static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004089 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004090{
Paul Menagebd89aab2007-10-18 23:40:44 -07004091 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004092 struct cgroupfs_root *root = parent->root;
4093 int err = 0;
4094 struct cgroup_subsys *ss;
4095 struct super_block *sb = root->sb;
4096
Paul Menagebd89aab2007-10-18 23:40:44 -07004097 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4098 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004099 return -ENOMEM;
4100
Tejun Heo976c06b2012-11-05 09:16:59 -08004101 /*
4102 * Only live parents can have children. Note that the liveliness
4103 * check isn't strictly necessary because cgroup_mkdir() and
4104 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4105 * anyway so that locking is contained inside cgroup proper and we
4106 * don't get nasty surprises if we ever grow another caller.
4107 */
4108 if (!cgroup_lock_live_group(parent)) {
4109 err = -ENODEV;
4110 goto err_free;
4111 }
4112
Paul Menageddbcc7e2007-10-18 23:39:30 -07004113 /* Grab a reference on the superblock so the hierarchy doesn't
4114 * get deleted on unmount if there are child cgroups. This
4115 * can be done outside cgroup_mutex, since the sb can't
4116 * disappear while someone has an open control file on the
4117 * fs */
4118 atomic_inc(&sb->s_active);
4119
Paul Menagecc31edc2008-10-18 20:28:04 -07004120 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004121
Paul Menagebd89aab2007-10-18 23:40:44 -07004122 cgrp->parent = parent;
4123 cgrp->root = parent->root;
4124 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004125
Li Zefanb6abdb02008-03-04 14:28:19 -08004126 if (notify_on_release(parent))
4127 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4128
Daniel Lezcano97978e62010-10-27 15:33:35 -07004129 if (clone_children(parent))
4130 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
4131
Paul Menageddbcc7e2007-10-18 23:39:30 -07004132 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004133 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004134
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004135 css = ss->create(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004136 if (IS_ERR(css)) {
4137 err = PTR_ERR(css);
4138 goto err_destroy;
4139 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004140 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004141 if (ss->use_id) {
4142 err = alloc_css_id(ss, parent, cgrp);
4143 if (err)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004144 goto err_destroy;
Li Zefan4528fd02010-02-02 13:44:10 -08004145 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004146 /* At error, ->destroy() callback has to free assigned ID. */
Daniel Lezcano97978e62010-10-27 15:33:35 -07004147 if (clone_children(parent) && ss->post_clone)
Li Zefan761b3ef2012-01-31 13:47:36 +08004148 ss->post_clone(cgrp);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004149
4150 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4151 parent->parent) {
4152 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",
4153 current->comm, current->pid, ss->name);
4154 if (!strcmp(ss->name, "memory"))
4155 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4156 ss->warned_broken_hierarchy = true;
4157 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004158 }
4159
Tejun Heo4e139af2012-11-19 08:13:36 -08004160 /*
4161 * Create directory. cgroup_create_file() returns with the new
4162 * directory locked on success so that it can be populated without
4163 * dropping cgroup_mutex.
4164 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004165 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004166 if (err < 0)
Tejun Heo4e139af2012-11-19 08:13:36 -08004167 goto err_destroy;
4168 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004169
Tejun Heo4e139af2012-11-19 08:13:36 -08004170 /* allocation complete, commit to creation */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004171 dentry->d_fsdata = cgrp;
Tejun Heofebfcef2012-11-19 08:13:36 -08004172 cgrp->dentry = dentry;
Tejun Heo4e139af2012-11-19 08:13:36 -08004173 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4174 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4175 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004176
Tejun Heoa8638032012-11-09 09:12:29 -08004177 for_each_subsys(root, ss) {
4178 /* each css holds a ref to the cgroup's dentry */
Tejun Heoed9577932012-11-05 09:16:58 -08004179 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004180
Tejun Heoa8638032012-11-09 09:12:29 -08004181 /* creation succeeded, notify subsystems */
Tejun Heoa31f2d32012-11-19 08:13:37 -08004182 online_css(ss, cgrp);
Tejun Heoa8638032012-11-09 09:12:29 -08004183 }
4184
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004185 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004186 /* If err < 0, we have a half-filled directory - oh well ;) */
4187
4188 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004189 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004190
4191 return 0;
4192
Tejun Heo4e139af2012-11-19 08:13:36 -08004193err_destroy:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004194 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004195 if (cgrp->subsys[ss->subsys_id])
Li Zefan761b3ef2012-01-31 13:47:36 +08004196 ss->destroy(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004197 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004198 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004199 /* Release the reference count that we took on the superblock */
4200 deactivate_super(sb);
Tejun Heo976c06b2012-11-05 09:16:59 -08004201err_free:
Paul Menagebd89aab2007-10-18 23:40:44 -07004202 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004203 return err;
4204}
4205
Al Viro18bb1db2011-07-26 01:41:39 -04004206static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004207{
4208 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4209
4210 /* the vfs holds inode->i_mutex already */
4211 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4212}
4213
Tejun Heo28b4c272012-04-01 12:09:56 -07004214/*
4215 * Check the reference count on each subsystem. Since we already
4216 * established that there are no tasks in the cgroup, if the css refcount
4217 * is also 1, then there should be no outstanding references, so the
4218 * subsystem is safe to destroy. We scan across all subsystems rather than
4219 * using the per-hierarchy linked list of mounted subsystems since we can
4220 * be called via check_for_release() with no synchronization other than
4221 * RCU, and the subsystem linked list isn't RCU-safe.
4222 */
Li Zefan55b6fd02008-07-29 22:33:20 -07004223static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004224{
Paul Menage81a6a5c2007-10-18 23:39:38 -07004225 int i;
Tejun Heo28b4c272012-04-01 12:09:56 -07004226
Ben Blumaae8aab2010-03-10 15:22:07 -08004227 /*
4228 * We won't need to lock the subsys array, because the subsystems
4229 * we're concerned about aren't going anywhere since our cgroup root
4230 * has a reference on them.
4231 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004232 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4233 struct cgroup_subsys *ss = subsys[i];
4234 struct cgroup_subsys_state *css;
Tejun Heo28b4c272012-04-01 12:09:56 -07004235
Ben Blumaae8aab2010-03-10 15:22:07 -08004236 /* Skip subsystems not present or not in this hierarchy */
4237 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004238 continue;
Tejun Heo28b4c272012-04-01 12:09:56 -07004239
Paul Menagebd89aab2007-10-18 23:40:44 -07004240 css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004241 /*
4242 * When called from check_for_release() it's possible
Paul Menage81a6a5c2007-10-18 23:39:38 -07004243 * that by this point the cgroup has been removed
4244 * and the css deleted. But a false-positive doesn't
4245 * matter, since it can only happen if the cgroup
4246 * has been deleted and hence no longer needs the
Tejun Heo28b4c272012-04-01 12:09:56 -07004247 * release agent to be called anyway.
4248 */
4249 if (css && css_refcnt(css) > 1)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004250 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004251 }
4252 return 0;
4253}
4254
Tejun Heo42809dd2012-11-19 08:13:37 -08004255static int cgroup_destroy_locked(struct cgroup *cgrp)
4256 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004257{
Tejun Heo42809dd2012-11-19 08:13:37 -08004258 struct dentry *d = cgrp->dentry;
4259 struct cgroup *parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004260 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004261 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004262 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004263
Tejun Heo42809dd2012-11-19 08:13:37 -08004264 lockdep_assert_held(&d->d_inode->i_mutex);
4265 lockdep_assert_held(&cgroup_mutex);
4266
4267 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004268 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004269
Tejun Heo1a90dd52012-11-05 09:16:59 -08004270 /*
4271 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4272 * removed. This makes future css_tryget() and child creation
4273 * attempts fail thus maintaining the removal conditions verified
4274 * above.
4275 */
Tejun Heoed9577932012-11-05 09:16:58 -08004276 for_each_subsys(cgrp->root, ss) {
4277 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4278
4279 WARN_ON(atomic_read(&css->refcnt) < 0);
4280 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004281 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004282 set_bit(CGRP_REMOVED, &cgrp->flags);
4283
Tejun Heoa31f2d32012-11-19 08:13:37 -08004284 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004285 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004286 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004287
4288 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004289 * Put all the base refs. Each css holds an extra reference to the
4290 * cgroup's dentry and cgroup removal proceeds regardless of css
4291 * refs. On the last put of each css, whenever that may be, the
4292 * extra dentry ref is put so that dentry destruction happens only
4293 * after all css's are released.
4294 */
Tejun Heoe9316082012-11-05 09:16:58 -08004295 for_each_subsys(cgrp->root, ss)
4296 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004297
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004298 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004299 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004300 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004301 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004302
Paul Menage999cd8a2009-01-07 18:08:36 -08004303 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004304 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004305 list_del_init(&cgrp->allcg_node);
4306
Tejun Heo42809dd2012-11-19 08:13:37 -08004307 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004308 cgroup_d_remove_dir(d);
4309 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004310
Paul Menagebd89aab2007-10-18 23:40:44 -07004311 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004312 check_for_release(parent);
4313
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004314 /*
4315 * Unregister events and notify userspace.
4316 * Notify userspace about cgroup removing only after rmdir of cgroup
4317 * directory to avoid race between userspace and kernelspace
4318 */
4319 spin_lock(&cgrp->event_list_lock);
4320 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4321 list_del(&event->list);
4322 remove_wait_queue(event->wqh, &event->wait);
4323 eventfd_signal(event->eventfd, 1);
4324 schedule_work(&event->remove);
4325 }
4326 spin_unlock(&cgrp->event_list_lock);
4327
Paul Menageddbcc7e2007-10-18 23:39:30 -07004328 return 0;
4329}
4330
Tejun Heo42809dd2012-11-19 08:13:37 -08004331static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4332{
4333 int ret;
4334
4335 mutex_lock(&cgroup_mutex);
4336 ret = cgroup_destroy_locked(dentry->d_fsdata);
4337 mutex_unlock(&cgroup_mutex);
4338
4339 return ret;
4340}
4341
Tejun Heo8e3f6542012-04-01 12:09:55 -07004342static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4343{
4344 INIT_LIST_HEAD(&ss->cftsets);
4345
4346 /*
4347 * base_cftset is embedded in subsys itself, no need to worry about
4348 * deregistration.
4349 */
4350 if (ss->base_cftypes) {
4351 ss->base_cftset.cfts = ss->base_cftypes;
4352 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4353 }
4354}
4355
Li Zefan06a11922008-04-29 01:00:07 -07004356static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004357{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004358 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004359
4360 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004361
Tejun Heo648bb562012-11-19 08:13:36 -08004362 mutex_lock(&cgroup_mutex);
4363
Tejun Heo8e3f6542012-04-01 12:09:55 -07004364 /* init base cftset */
4365 cgroup_init_cftsets(ss);
4366
Paul Menageddbcc7e2007-10-18 23:39:30 -07004367 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004368 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004369 ss->root = &rootnode;
Li Zefan761b3ef2012-01-31 13:47:36 +08004370 css = ss->create(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004371 /* We don't handle early failures gracefully */
4372 BUG_ON(IS_ERR(css));
4373 init_cgroup_css(css, ss, dummytop);
4374
Li Zefane8d55fd2008-04-29 01:00:13 -07004375 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004376 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004377 * newly registered, all tasks and hence the
4378 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004379 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004380
4381 need_forkexit_callback |= ss->fork || ss->exit;
4382
Li Zefane8d55fd2008-04-29 01:00:13 -07004383 /* At system boot, before all subsystems have been
4384 * registered, no tasks have been forked, so we don't
4385 * need to invoke fork callbacks here. */
4386 BUG_ON(!list_empty(&init_task.tasks));
4387
Paul Menageddbcc7e2007-10-18 23:39:30 -07004388 ss->active = 1;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004389 online_css(ss, dummytop);
Tejun Heoa8638032012-11-09 09:12:29 -08004390
Tejun Heo648bb562012-11-19 08:13:36 -08004391 mutex_unlock(&cgroup_mutex);
4392
Ben Blume6a11052010-03-10 15:22:09 -08004393 /* this function shouldn't be used with modular subsystems, since they
4394 * need to register a subsys_id, among other things */
4395 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004396}
4397
4398/**
Ben Blume6a11052010-03-10 15:22:09 -08004399 * cgroup_load_subsys: load and register a modular subsystem at runtime
4400 * @ss: the subsystem to load
4401 *
4402 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004403 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004404 * up for use. If the subsystem is built-in anyway, work is delegated to the
4405 * simpler cgroup_init_subsys.
4406 */
4407int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4408{
Ben Blume6a11052010-03-10 15:22:09 -08004409 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004410 int i, ret;
Ben Blume6a11052010-03-10 15:22:09 -08004411
4412 /* check name and function validity */
4413 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4414 ss->create == NULL || ss->destroy == NULL)
4415 return -EINVAL;
4416
4417 /*
4418 * we don't support callbacks in modular subsystems. this check is
4419 * before the ss->module check for consistency; a subsystem that could
4420 * be a module should still have no callbacks even if the user isn't
4421 * compiling it as one.
4422 */
4423 if (ss->fork || ss->exit)
4424 return -EINVAL;
4425
4426 /*
4427 * an optionally modular subsystem is built-in: we want to do nothing,
4428 * since cgroup_init_subsys will have already taken care of it.
4429 */
4430 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004431 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004432 BUG_ON(subsys[ss->subsys_id] != ss);
4433 return 0;
4434 }
4435
Tejun Heo8e3f6542012-04-01 12:09:55 -07004436 /* init base cftset */
4437 cgroup_init_cftsets(ss);
4438
Ben Blume6a11052010-03-10 15:22:09 -08004439 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004440 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004441
4442 /*
4443 * no ss->create seems to need anything important in the ss struct, so
4444 * this can happen first (i.e. before the rootnode attachment).
4445 */
Li Zefan761b3ef2012-01-31 13:47:36 +08004446 css = ss->create(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004447 if (IS_ERR(css)) {
4448 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004449 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004450 mutex_unlock(&cgroup_mutex);
4451 return PTR_ERR(css);
4452 }
4453
4454 list_add(&ss->sibling, &rootnode.subsys_list);
4455 ss->root = &rootnode;
4456
4457 /* our new subsystem will be attached to the dummy hierarchy. */
4458 init_cgroup_css(css, ss, dummytop);
4459 /* init_idr must be after init_cgroup_css because it sets css->id. */
4460 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004461 ret = cgroup_init_idr(ss, css);
4462 if (ret)
4463 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004464 }
4465
4466 /*
4467 * Now we need to entangle the css into the existing css_sets. unlike
4468 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4469 * will need a new pointer to it; done by iterating the css_set_table.
4470 * furthermore, modifying the existing css_sets will corrupt the hash
4471 * table state, so each changed css_set will need its hash recomputed.
4472 * this is all done under the css_set_lock.
4473 */
4474 write_lock(&css_set_lock);
4475 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4476 struct css_set *cg;
4477 struct hlist_node *node, *tmp;
4478 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4479
4480 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4481 /* skip entries that we already rehashed */
4482 if (cg->subsys[ss->subsys_id])
4483 continue;
4484 /* remove existing entry */
4485 hlist_del(&cg->hlist);
4486 /* set new value */
4487 cg->subsys[ss->subsys_id] = css;
4488 /* recompute hash and restore entry */
4489 new_bucket = css_set_hash(cg->subsys);
4490 hlist_add_head(&cg->hlist, new_bucket);
4491 }
4492 }
4493 write_unlock(&css_set_lock);
4494
Ben Blume6a11052010-03-10 15:22:09 -08004495 ss->active = 1;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004496 online_css(ss, dummytop);
Tejun Heoa8638032012-11-09 09:12:29 -08004497
Ben Blume6a11052010-03-10 15:22:09 -08004498 /* success! */
4499 mutex_unlock(&cgroup_mutex);
4500 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004501
4502err_unload:
4503 mutex_unlock(&cgroup_mutex);
4504 /* @ss can't be mounted here as try_module_get() would fail */
4505 cgroup_unload_subsys(ss);
4506 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004507}
4508EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4509
4510/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004511 * cgroup_unload_subsys: unload a modular subsystem
4512 * @ss: the subsystem to unload
4513 *
4514 * This function should be called in a modular subsystem's exitcall. When this
4515 * function is invoked, the refcount on the subsystem's module will be 0, so
4516 * the subsystem will not be attached to any hierarchy.
4517 */
4518void cgroup_unload_subsys(struct cgroup_subsys *ss)
4519{
4520 struct cg_cgroup_link *link;
4521 struct hlist_head *hhead;
4522
4523 BUG_ON(ss->module == NULL);
4524
4525 /*
4526 * we shouldn't be called if the subsystem is in use, and the use of
4527 * try_module_get in parse_cgroupfs_options should ensure that it
4528 * doesn't start being used while we're killing it off.
4529 */
4530 BUG_ON(ss->root != &rootnode);
4531
4532 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004533
Tejun Heoa31f2d32012-11-19 08:13:37 -08004534 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004535 ss->active = 0;
4536
4537 if (ss->use_id) {
4538 idr_remove_all(&ss->idr);
4539 idr_destroy(&ss->idr);
4540 }
4541
Ben Blumcf5d5942010-03-10 15:22:09 -08004542 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004543 subsys[ss->subsys_id] = NULL;
4544
4545 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004546 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004547
4548 /*
4549 * disentangle the css from all css_sets attached to the dummytop. as
4550 * in loading, we need to pay our respects to the hashtable gods.
4551 */
4552 write_lock(&css_set_lock);
4553 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4554 struct css_set *cg = link->cg;
4555
4556 hlist_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004557 cg->subsys[ss->subsys_id] = NULL;
4558 hhead = css_set_hash(cg->subsys);
4559 hlist_add_head(&cg->hlist, hhead);
4560 }
4561 write_unlock(&css_set_lock);
4562
4563 /*
4564 * remove subsystem's css from the dummytop and free it - need to free
4565 * before marking as null because ss->destroy needs the cgrp->subsys
4566 * pointer to find their state. note that this also takes care of
4567 * freeing the css_id.
4568 */
Li Zefan761b3ef2012-01-31 13:47:36 +08004569 ss->destroy(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004570 dummytop->subsys[ss->subsys_id] = NULL;
4571
4572 mutex_unlock(&cgroup_mutex);
4573}
4574EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4575
4576/**
Li Zefana043e3b2008-02-23 15:24:09 -08004577 * cgroup_init_early - cgroup initialization at system boot
4578 *
4579 * Initialize cgroups at system boot, and initialize any
4580 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004581 */
4582int __init cgroup_init_early(void)
4583{
4584 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004585 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004586 INIT_LIST_HEAD(&init_css_set.cg_links);
4587 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004588 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004589 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004590 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004591 root_count = 1;
4592 init_task.cgroups = &init_css_set;
4593
4594 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004595 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004596 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004597 &rootnode.top_cgroup.css_sets);
4598 list_add(&init_css_set_link.cg_link_list,
4599 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004600
Li Zefan472b1052008-04-29 01:00:11 -07004601 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4602 INIT_HLIST_HEAD(&css_set_table[i]);
4603
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004604 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004605 struct cgroup_subsys *ss = subsys[i];
4606
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004607 /* at bootup time, we don't worry about modular subsystems */
4608 if (!ss || ss->module)
4609 continue;
4610
Paul Menageddbcc7e2007-10-18 23:39:30 -07004611 BUG_ON(!ss->name);
4612 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
4613 BUG_ON(!ss->create);
4614 BUG_ON(!ss->destroy);
4615 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004616 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004617 ss->name, ss->subsys_id);
4618 BUG();
4619 }
4620
4621 if (ss->early_init)
4622 cgroup_init_subsys(ss);
4623 }
4624 return 0;
4625}
4626
4627/**
Li Zefana043e3b2008-02-23 15:24:09 -08004628 * cgroup_init - cgroup initialization
4629 *
4630 * Register cgroup filesystem and /proc file, and initialize
4631 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004632 */
4633int __init cgroup_init(void)
4634{
4635 int err;
4636 int i;
Li Zefan472b1052008-04-29 01:00:11 -07004637 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07004638
4639 err = bdi_init(&cgroup_backing_dev_info);
4640 if (err)
4641 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004642
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004643 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004644 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004645
4646 /* at bootup time, we don't worry about modular subsystems */
4647 if (!ss || ss->module)
4648 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004649 if (!ss->early_init)
4650 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004651 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004652 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004653 }
4654
Li Zefan472b1052008-04-29 01:00:11 -07004655 /* Add init_css_set to the hash table */
4656 hhead = css_set_hash(init_css_set.subsys);
4657 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004658 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004659
4660 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4661 if (!cgroup_kobj) {
4662 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004663 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004664 }
4665
4666 err = register_filesystem(&cgroup_fs_type);
4667 if (err < 0) {
4668 kobject_put(cgroup_kobj);
4669 goto out;
4670 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004671
Li Zefan46ae2202008-04-29 01:00:08 -07004672 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004673
Paul Menageddbcc7e2007-10-18 23:39:30 -07004674out:
Paul Menagea4243162007-10-18 23:39:35 -07004675 if (err)
4676 bdi_destroy(&cgroup_backing_dev_info);
4677
Paul Menageddbcc7e2007-10-18 23:39:30 -07004678 return err;
4679}
Paul Menageb4f48b62007-10-18 23:39:33 -07004680
Paul Menagea4243162007-10-18 23:39:35 -07004681/*
4682 * proc_cgroup_show()
4683 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4684 * - Used for /proc/<pid>/cgroup.
4685 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4686 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004687 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004688 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4689 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4690 * cgroup to top_cgroup.
4691 */
4692
4693/* TODO: Use a proper seq_file iterator */
4694static int proc_cgroup_show(struct seq_file *m, void *v)
4695{
4696 struct pid *pid;
4697 struct task_struct *tsk;
4698 char *buf;
4699 int retval;
4700 struct cgroupfs_root *root;
4701
4702 retval = -ENOMEM;
4703 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4704 if (!buf)
4705 goto out;
4706
4707 retval = -ESRCH;
4708 pid = m->private;
4709 tsk = get_pid_task(pid, PIDTYPE_PID);
4710 if (!tsk)
4711 goto out_free;
4712
4713 retval = 0;
4714
4715 mutex_lock(&cgroup_mutex);
4716
Li Zefane5f6a862009-01-07 18:07:41 -08004717 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004718 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004719 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004720 int count = 0;
4721
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004722 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004723 for_each_subsys(root, ss)
4724 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004725 if (strlen(root->name))
4726 seq_printf(m, "%sname=%s", count ? "," : "",
4727 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004728 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004729 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004730 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004731 if (retval < 0)
4732 goto out_unlock;
4733 seq_puts(m, buf);
4734 seq_putc(m, '\n');
4735 }
4736
4737out_unlock:
4738 mutex_unlock(&cgroup_mutex);
4739 put_task_struct(tsk);
4740out_free:
4741 kfree(buf);
4742out:
4743 return retval;
4744}
4745
4746static int cgroup_open(struct inode *inode, struct file *file)
4747{
4748 struct pid *pid = PROC_I(inode)->pid;
4749 return single_open(file, proc_cgroup_show, pid);
4750}
4751
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004752const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004753 .open = cgroup_open,
4754 .read = seq_read,
4755 .llseek = seq_lseek,
4756 .release = single_release,
4757};
4758
4759/* Display information about each subsystem and each hierarchy */
4760static int proc_cgroupstats_show(struct seq_file *m, void *v)
4761{
4762 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004763
Paul Menage8bab8dd2008-04-04 14:29:57 -07004764 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004765 /*
4766 * ideally we don't want subsystems moving around while we do this.
4767 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4768 * subsys/hierarchy state.
4769 */
Paul Menagea4243162007-10-18 23:39:35 -07004770 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004771 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4772 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004773 if (ss == NULL)
4774 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004775 seq_printf(m, "%s\t%d\t%d\t%d\n",
4776 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004777 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004778 }
4779 mutex_unlock(&cgroup_mutex);
4780 return 0;
4781}
4782
4783static int cgroupstats_open(struct inode *inode, struct file *file)
4784{
Al Viro9dce07f2008-03-29 03:07:28 +00004785 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004786}
4787
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004788static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004789 .open = cgroupstats_open,
4790 .read = seq_read,
4791 .llseek = seq_lseek,
4792 .release = single_release,
4793};
4794
Paul Menageb4f48b62007-10-18 23:39:33 -07004795/**
4796 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004797 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004798 *
4799 * Description: A task inherits its parent's cgroup at fork().
4800 *
4801 * A pointer to the shared css_set was automatically copied in
4802 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004803 * it was not made under the protection of RCU or cgroup_mutex, so
4804 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4805 * have already changed current->cgroups, allowing the previously
4806 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004807 *
4808 * At the point that cgroup_fork() is called, 'current' is the parent
4809 * task, and the passed argument 'child' points to the child task.
4810 */
4811void cgroup_fork(struct task_struct *child)
4812{
Tejun Heo9bb71302012-10-18 17:52:07 -07004813 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004814 child->cgroups = current->cgroups;
4815 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004816 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004817 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004818}
4819
4820/**
Li Zefana043e3b2008-02-23 15:24:09 -08004821 * cgroup_post_fork - called on a new task after adding it to the task list
4822 * @child: the task in question
4823 *
Tejun Heo5edee612012-10-16 15:03:14 -07004824 * Adds the task to the list running through its css_set if necessary and
4825 * call the subsystem fork() callbacks. Has to be after the task is
4826 * visible on the task list in case we race with the first call to
4827 * cgroup_iter_start() - to guarantee that the new task ends up on its
4828 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004829 */
Paul Menage817929e2007-10-18 23:39:36 -07004830void cgroup_post_fork(struct task_struct *child)
4831{
Tejun Heo5edee612012-10-16 15:03:14 -07004832 int i;
4833
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004834 /*
4835 * use_task_css_set_links is set to 1 before we walk the tasklist
4836 * under the tasklist_lock and we read it here after we added the child
4837 * to the tasklist under the tasklist_lock as well. If the child wasn't
4838 * yet in the tasklist when we walked through it from
4839 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4840 * should be visible now due to the paired locking and barriers implied
4841 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4842 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4843 * lock on fork.
4844 */
Paul Menage817929e2007-10-18 23:39:36 -07004845 if (use_task_css_set_links) {
4846 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004847 task_lock(child);
4848 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004849 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004850 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004851 write_unlock(&css_set_lock);
4852 }
Tejun Heo5edee612012-10-16 15:03:14 -07004853
4854 /*
4855 * Call ss->fork(). This must happen after @child is linked on
4856 * css_set; otherwise, @child might change state between ->fork()
4857 * and addition to css_set.
4858 */
4859 if (need_forkexit_callback) {
4860 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4861 struct cgroup_subsys *ss = subsys[i];
4862
4863 /*
4864 * fork/exit callbacks are supported only for
4865 * builtin subsystems and we don't need further
4866 * synchronization as they never go away.
4867 */
4868 if (!ss || ss->module)
4869 continue;
4870
4871 if (ss->fork)
4872 ss->fork(child);
4873 }
4874 }
Paul Menage817929e2007-10-18 23:39:36 -07004875}
Tejun Heo5edee612012-10-16 15:03:14 -07004876
Paul Menage817929e2007-10-18 23:39:36 -07004877/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004878 * cgroup_exit - detach cgroup from exiting task
4879 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004880 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004881 *
4882 * Description: Detach cgroup from @tsk and release it.
4883 *
4884 * Note that cgroups marked notify_on_release force every task in
4885 * them to take the global cgroup_mutex mutex when exiting.
4886 * This could impact scaling on very large systems. Be reluctant to
4887 * use notify_on_release cgroups where very high task exit scaling
4888 * is required on large systems.
4889 *
4890 * the_top_cgroup_hack:
4891 *
4892 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4893 *
4894 * We call cgroup_exit() while the task is still competent to
4895 * handle notify_on_release(), then leave the task attached to the
4896 * root cgroup in each hierarchy for the remainder of its exit.
4897 *
4898 * To do this properly, we would increment the reference count on
4899 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4900 * code we would add a second cgroup function call, to drop that
4901 * reference. This would just create an unnecessary hot spot on
4902 * the top_cgroup reference count, to no avail.
4903 *
4904 * Normally, holding a reference to a cgroup without bumping its
4905 * count is unsafe. The cgroup could go away, or someone could
4906 * attach us to a different cgroup, decrementing the count on
4907 * the first cgroup that we never incremented. But in this case,
4908 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004909 * which wards off any cgroup_attach_task() attempts, or task is a failed
4910 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004911 */
4912void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4913{
Paul Menage817929e2007-10-18 23:39:36 -07004914 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004915 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004916
4917 /*
4918 * Unlink from the css_set task list if necessary.
4919 * Optimistically check cg_list before taking
4920 * css_set_lock
4921 */
4922 if (!list_empty(&tsk->cg_list)) {
4923 write_lock(&css_set_lock);
4924 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004925 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004926 write_unlock(&css_set_lock);
4927 }
4928
Paul Menageb4f48b62007-10-18 23:39:33 -07004929 /* Reassign the task to the init_css_set. */
4930 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004931 cg = tsk->cgroups;
4932 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004933
4934 if (run_callbacks && need_forkexit_callback) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004935 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004936 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004937
4938 /* modular subsystems can't use callbacks */
4939 if (!ss || ss->module)
4940 continue;
4941
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004942 if (ss->exit) {
4943 struct cgroup *old_cgrp =
4944 rcu_dereference_raw(cg->subsys[i])->cgroup;
4945 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004946 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004947 }
4948 }
4949 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004950 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004951
Paul Menage817929e2007-10-18 23:39:36 -07004952 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004953 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004954}
Paul Menage697f4162007-10-18 23:39:34 -07004955
4956/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004957 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08004958 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004959 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08004960 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004961 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4962 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07004963 *
4964 * If we are sending in dummytop, then presumably we are creating
4965 * the top cgroup in the subsystem.
4966 *
4967 * Called only by the ns (nsproxy) cgroup.
4968 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004969int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07004970{
4971 int ret;
4972 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07004973
Paul Menagebd89aab2007-10-18 23:40:44 -07004974 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07004975 return 1;
4976
Paul Menage7717f7b2009-09-23 15:56:22 -07004977 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004978 while (cgrp != target && cgrp!= cgrp->top_cgroup)
4979 cgrp = cgrp->parent;
4980 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07004981 return ret;
4982}
Paul Menage81a6a5c2007-10-18 23:39:38 -07004983
Paul Menagebd89aab2007-10-18 23:40:44 -07004984static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004985{
4986 /* All of these checks rely on RCU to keep the cgroup
4987 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07004988 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
4989 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004990 /* Control Group is currently removeable. If it's not
4991 * already queued for a userspace notification, queue
4992 * it now */
4993 int need_schedule_work = 0;
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004994 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004995 if (!cgroup_is_removed(cgrp) &&
4996 list_empty(&cgrp->release_list)) {
4997 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004998 need_schedule_work = 1;
4999 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005000 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005001 if (need_schedule_work)
5002 schedule_work(&release_agent_work);
5003 }
5004}
5005
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005006/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005007bool __css_tryget(struct cgroup_subsys_state *css)
5008{
Tejun Heoe9316082012-11-05 09:16:58 -08005009 while (true) {
5010 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005011
Tejun Heoe9316082012-11-05 09:16:58 -08005012 v = css_refcnt(css);
5013 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5014 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005015 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005016 else if (t < 0)
5017 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005018 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005019 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005020}
5021EXPORT_SYMBOL_GPL(__css_tryget);
5022
5023/* Caller must verify that the css is not for root cgroup */
5024void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005025{
Paul Menagebd89aab2007-10-18 23:40:44 -07005026 struct cgroup *cgrp = css->cgroup;
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005027 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005028
Paul Menage81a6a5c2007-10-18 23:39:38 -07005029 rcu_read_lock();
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005030 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
5031
5032 switch (v) {
Tejun Heo48ddbe12012-04-01 12:09:56 -07005033 case 1:
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005034 if (notify_on_release(cgrp)) {
5035 set_bit(CGRP_RELEASABLE, &cgrp->flags);
5036 check_for_release(cgrp);
5037 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07005038 break;
5039 case 0:
Tejun Heoed9577932012-11-05 09:16:58 -08005040 schedule_work(&css->dput_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005041 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005042 }
5043 rcu_read_unlock();
5044}
Ben Blum67523c42010-03-10 15:22:11 -08005045EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005046
5047/*
5048 * Notify userspace when a cgroup is released, by running the
5049 * configured release agent with the name of the cgroup (path
5050 * relative to the root of cgroup file system) as the argument.
5051 *
5052 * Most likely, this user command will try to rmdir this cgroup.
5053 *
5054 * This races with the possibility that some other task will be
5055 * attached to this cgroup before it is removed, or that some other
5056 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5057 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5058 * unused, and this cgroup will be reprieved from its death sentence,
5059 * to continue to serve a useful existence. Next time it's released,
5060 * we will get notified again, if it still has 'notify_on_release' set.
5061 *
5062 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5063 * means only wait until the task is successfully execve()'d. The
5064 * separate release agent task is forked by call_usermodehelper(),
5065 * then control in this thread returns here, without waiting for the
5066 * release agent task. We don't bother to wait because the caller of
5067 * this routine has no use for the exit status of the release agent
5068 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005069 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005070static void cgroup_release_agent(struct work_struct *work)
5071{
5072 BUG_ON(work != &release_agent_work);
5073 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005074 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005075 while (!list_empty(&release_list)) {
5076 char *argv[3], *envp[3];
5077 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005078 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005079 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005080 struct cgroup,
5081 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005082 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005083 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005084 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005085 if (!pathbuf)
5086 goto continue_free;
5087 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5088 goto continue_free;
5089 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5090 if (!agentbuf)
5091 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005092
5093 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005094 argv[i++] = agentbuf;
5095 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005096 argv[i] = NULL;
5097
5098 i = 0;
5099 /* minimal command environment */
5100 envp[i++] = "HOME=/";
5101 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5102 envp[i] = NULL;
5103
5104 /* Drop the lock while we invoke the usermode helper,
5105 * since the exec could involve hitting disk and hence
5106 * be a slow process */
5107 mutex_unlock(&cgroup_mutex);
5108 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005109 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005110 continue_free:
5111 kfree(pathbuf);
5112 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005113 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005114 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005115 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005116 mutex_unlock(&cgroup_mutex);
5117}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005118
5119static int __init cgroup_disable(char *str)
5120{
5121 int i;
5122 char *token;
5123
5124 while ((token = strsep(&str, ",")) != NULL) {
5125 if (!*token)
5126 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005127 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005128 struct cgroup_subsys *ss = subsys[i];
5129
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005130 /*
5131 * cgroup_disable, being at boot time, can't
5132 * know about module subsystems, so we don't
5133 * worry about them.
5134 */
5135 if (!ss || ss->module)
5136 continue;
5137
Paul Menage8bab8dd2008-04-04 14:29:57 -07005138 if (!strcmp(token, ss->name)) {
5139 ss->disabled = 1;
5140 printk(KERN_INFO "Disabling %s control group"
5141 " subsystem\n", ss->name);
5142 break;
5143 }
5144 }
5145 }
5146 return 1;
5147}
5148__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005149
5150/*
5151 * Functons for CSS ID.
5152 */
5153
5154/*
5155 *To get ID other than 0, this should be called when !cgroup_is_removed().
5156 */
5157unsigned short css_id(struct cgroup_subsys_state *css)
5158{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005159 struct css_id *cssid;
5160
5161 /*
5162 * This css_id() can return correct value when somone has refcnt
5163 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5164 * it's unchanged until freed.
5165 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005166 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005167
5168 if (cssid)
5169 return cssid->id;
5170 return 0;
5171}
Ben Blum67523c42010-03-10 15:22:11 -08005172EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005173
5174unsigned short css_depth(struct cgroup_subsys_state *css)
5175{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005176 struct css_id *cssid;
5177
Tejun Heo28b4c272012-04-01 12:09:56 -07005178 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005179
5180 if (cssid)
5181 return cssid->depth;
5182 return 0;
5183}
Ben Blum67523c42010-03-10 15:22:11 -08005184EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005185
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005186/**
5187 * css_is_ancestor - test "root" css is an ancestor of "child"
5188 * @child: the css to be tested.
5189 * @root: the css supporsed to be an ancestor of the child.
5190 *
5191 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005192 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005193 * But, considering usual usage, the csses should be valid objects after test.
5194 * Assuming that the caller will do some action to the child if this returns
5195 * returns true, the caller must take "child";s reference count.
5196 * If "child" is valid object and this returns true, "root" is valid, too.
5197 */
5198
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005199bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005200 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005201{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005202 struct css_id *child_id;
5203 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005204
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005205 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005206 if (!child_id)
5207 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005208 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005209 if (!root_id)
5210 return false;
5211 if (child_id->depth < root_id->depth)
5212 return false;
5213 if (child_id->stack[root_id->depth] != root_id->id)
5214 return false;
5215 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005216}
5217
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005218void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5219{
5220 struct css_id *id = css->id;
5221 /* When this is called before css_id initialization, id can be NULL */
5222 if (!id)
5223 return;
5224
5225 BUG_ON(!ss->use_id);
5226
5227 rcu_assign_pointer(id->css, NULL);
5228 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005229 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005230 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005231 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005232 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005233}
Ben Blum67523c42010-03-10 15:22:11 -08005234EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005235
5236/*
5237 * This is called by init or create(). Then, calls to this function are
5238 * always serialized (By cgroup_mutex() at create()).
5239 */
5240
5241static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5242{
5243 struct css_id *newid;
5244 int myid, error, size;
5245
5246 BUG_ON(!ss->use_id);
5247
5248 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5249 newid = kzalloc(size, GFP_KERNEL);
5250 if (!newid)
5251 return ERR_PTR(-ENOMEM);
5252 /* get id */
5253 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
5254 error = -ENOMEM;
5255 goto err_out;
5256 }
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005257 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005258 /* Don't use 0. allocates an ID of 1-65535 */
5259 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005260 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005261
5262 /* Returns error when there are no free spaces for new ID.*/
5263 if (error) {
5264 error = -ENOSPC;
5265 goto err_out;
5266 }
5267 if (myid > CSS_ID_MAX)
5268 goto remove_idr;
5269
5270 newid->id = myid;
5271 newid->depth = depth;
5272 return newid;
5273remove_idr:
5274 error = -ENOSPC;
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005275 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005276 idr_remove(&ss->idr, myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005277 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005278err_out:
5279 kfree(newid);
5280 return ERR_PTR(error);
5281
5282}
5283
Ben Blume6a11052010-03-10 15:22:09 -08005284static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5285 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005286{
5287 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005288
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005289 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005290 idr_init(&ss->idr);
5291
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005292 newid = get_new_cssid(ss, 0);
5293 if (IS_ERR(newid))
5294 return PTR_ERR(newid);
5295
5296 newid->stack[0] = newid->id;
5297 newid->css = rootcss;
5298 rootcss->id = newid;
5299 return 0;
5300}
5301
5302static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5303 struct cgroup *child)
5304{
5305 int subsys_id, i, depth = 0;
5306 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005307 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005308
5309 subsys_id = ss->subsys_id;
5310 parent_css = parent->subsys[subsys_id];
5311 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005312 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005313 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005314
5315 child_id = get_new_cssid(ss, depth);
5316 if (IS_ERR(child_id))
5317 return PTR_ERR(child_id);
5318
5319 for (i = 0; i < depth; i++)
5320 child_id->stack[i] = parent_id->stack[i];
5321 child_id->stack[depth] = child_id->id;
5322 /*
5323 * child_id->css pointer will be set after this cgroup is available
5324 * see cgroup_populate_dir()
5325 */
5326 rcu_assign_pointer(child_css->id, child_id);
5327
5328 return 0;
5329}
5330
5331/**
5332 * css_lookup - lookup css by id
5333 * @ss: cgroup subsys to be looked into.
5334 * @id: the id
5335 *
5336 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5337 * NULL if not. Should be called under rcu_read_lock()
5338 */
5339struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5340{
5341 struct css_id *cssid = NULL;
5342
5343 BUG_ON(!ss->use_id);
5344 cssid = idr_find(&ss->idr, id);
5345
5346 if (unlikely(!cssid))
5347 return NULL;
5348
5349 return rcu_dereference(cssid->css);
5350}
Ben Blum67523c42010-03-10 15:22:11 -08005351EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005352
5353/**
5354 * css_get_next - lookup next cgroup under specified hierarchy.
5355 * @ss: pointer to subsystem
5356 * @id: current position of iteration.
5357 * @root: pointer to css. search tree under this.
5358 * @foundid: position of found object.
5359 *
5360 * Search next css under the specified hierarchy of rootid. Calling under
5361 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5362 */
5363struct cgroup_subsys_state *
5364css_get_next(struct cgroup_subsys *ss, int id,
5365 struct cgroup_subsys_state *root, int *foundid)
5366{
5367 struct cgroup_subsys_state *ret = NULL;
5368 struct css_id *tmp;
5369 int tmpid;
5370 int rootid = css_id(root);
5371 int depth = css_depth(root);
5372
5373 if (!rootid)
5374 return NULL;
5375
5376 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005377 WARN_ON_ONCE(!rcu_read_lock_held());
5378
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005379 /* fill start point for scan */
5380 tmpid = id;
5381 while (1) {
5382 /*
5383 * scan next entry from bitmap(tree), tmpid is updated after
5384 * idr_get_next().
5385 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005386 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005387 if (!tmp)
5388 break;
5389 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5390 ret = rcu_dereference(tmp->css);
5391 if (ret) {
5392 *foundid = tmpid;
5393 break;
5394 }
5395 }
5396 /* continue to scan from next id */
5397 tmpid = tmpid + 1;
5398 }
5399 return ret;
5400}
5401
Stephane Eraniane5d13672011-02-14 11:20:01 +02005402/*
5403 * get corresponding css from file open on cgroupfs directory
5404 */
5405struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5406{
5407 struct cgroup *cgrp;
5408 struct inode *inode;
5409 struct cgroup_subsys_state *css;
5410
5411 inode = f->f_dentry->d_inode;
5412 /* check in cgroup filesystem dir */
5413 if (inode->i_op != &cgroup_dir_inode_operations)
5414 return ERR_PTR(-EBADF);
5415
5416 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5417 return ERR_PTR(-EINVAL);
5418
5419 /* get cgroup */
5420 cgrp = __d_cgrp(f->f_dentry);
5421 css = cgrp->subsys[id];
5422 return css ? css : ERR_PTR(-ENOENT);
5423}
5424
Paul Menagefe693432009-09-23 15:56:20 -07005425#ifdef CONFIG_CGROUP_DEBUG
Li Zefan761b3ef2012-01-31 13:47:36 +08005426static struct cgroup_subsys_state *debug_create(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005427{
5428 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5429
5430 if (!css)
5431 return ERR_PTR(-ENOMEM);
5432
5433 return css;
5434}
5435
Li Zefan761b3ef2012-01-31 13:47:36 +08005436static void debug_destroy(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005437{
5438 kfree(cont->subsys[debug_subsys_id]);
5439}
5440
5441static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5442{
5443 return atomic_read(&cont->count);
5444}
5445
5446static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5447{
5448 return cgroup_task_count(cont);
5449}
5450
5451static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5452{
5453 return (u64)(unsigned long)current->cgroups;
5454}
5455
5456static u64 current_css_set_refcount_read(struct cgroup *cont,
5457 struct cftype *cft)
5458{
5459 u64 count;
5460
5461 rcu_read_lock();
5462 count = atomic_read(&current->cgroups->refcount);
5463 rcu_read_unlock();
5464 return count;
5465}
5466
Paul Menage7717f7b2009-09-23 15:56:22 -07005467static int current_css_set_cg_links_read(struct cgroup *cont,
5468 struct cftype *cft,
5469 struct seq_file *seq)
5470{
5471 struct cg_cgroup_link *link;
5472 struct css_set *cg;
5473
5474 read_lock(&css_set_lock);
5475 rcu_read_lock();
5476 cg = rcu_dereference(current->cgroups);
5477 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5478 struct cgroup *c = link->cgrp;
5479 const char *name;
5480
5481 if (c->dentry)
5482 name = c->dentry->d_name.name;
5483 else
5484 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005485 seq_printf(seq, "Root %d group %s\n",
5486 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005487 }
5488 rcu_read_unlock();
5489 read_unlock(&css_set_lock);
5490 return 0;
5491}
5492
5493#define MAX_TASKS_SHOWN_PER_CSS 25
5494static int cgroup_css_links_read(struct cgroup *cont,
5495 struct cftype *cft,
5496 struct seq_file *seq)
5497{
5498 struct cg_cgroup_link *link;
5499
5500 read_lock(&css_set_lock);
5501 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5502 struct css_set *cg = link->cg;
5503 struct task_struct *task;
5504 int count = 0;
5505 seq_printf(seq, "css_set %p\n", cg);
5506 list_for_each_entry(task, &cg->tasks, cg_list) {
5507 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5508 seq_puts(seq, " ...\n");
5509 break;
5510 } else {
5511 seq_printf(seq, " task %d\n",
5512 task_pid_vnr(task));
5513 }
5514 }
5515 }
5516 read_unlock(&css_set_lock);
5517 return 0;
5518}
5519
Paul Menagefe693432009-09-23 15:56:20 -07005520static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5521{
5522 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5523}
5524
5525static struct cftype debug_files[] = {
5526 {
5527 .name = "cgroup_refcount",
5528 .read_u64 = cgroup_refcount_read,
5529 },
5530 {
5531 .name = "taskcount",
5532 .read_u64 = debug_taskcount_read,
5533 },
5534
5535 {
5536 .name = "current_css_set",
5537 .read_u64 = current_css_set_read,
5538 },
5539
5540 {
5541 .name = "current_css_set_refcount",
5542 .read_u64 = current_css_set_refcount_read,
5543 },
5544
5545 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005546 .name = "current_css_set_cg_links",
5547 .read_seq_string = current_css_set_cg_links_read,
5548 },
5549
5550 {
5551 .name = "cgroup_css_links",
5552 .read_seq_string = cgroup_css_links_read,
5553 },
5554
5555 {
Paul Menagefe693432009-09-23 15:56:20 -07005556 .name = "releasable",
5557 .read_u64 = releasable_read,
5558 },
Paul Menagefe693432009-09-23 15:56:20 -07005559
Tejun Heo4baf6e32012-04-01 12:09:55 -07005560 { } /* terminate */
5561};
Paul Menagefe693432009-09-23 15:56:20 -07005562
5563struct cgroup_subsys debug_subsys = {
5564 .name = "debug",
5565 .create = debug_create,
5566 .destroy = debug_destroy,
Paul Menagefe693432009-09-23 15:56:20 -07005567 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005568 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005569};
5570#endif /* CONFIG_CGROUP_DEBUG */