blob: c7ece8f027f2bca3200199fc323c65939cee0453 [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 *
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
11 *
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
14 *
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
19 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
23 */
24
25#include <linux/cgroup.h>
26#include <linux/errno.h>
27#include <linux/fs.h>
28#include <linux/kernel.h>
29#include <linux/list.h>
30#include <linux/mm.h>
31#include <linux/mutex.h>
32#include <linux/mount.h>
33#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070034#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/rcupdate.h>
36#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070037#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070038#include <linux/seq_file.h>
39#include <linux/slab.h>
40#include <linux/magic.h>
41#include <linux/spinlock.h>
42#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070043#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070044#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070045#include <linux/delayacct.h>
46#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070047#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040048#include <linux/namei.h>
Alessio Igor Bogani337eb002009-05-12 15:10:54 +020049#include <linux/smp_lock.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070050#include <linux/pid_namespace.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070051
Paul Menageddbcc7e2007-10-18 23:39:30 -070052#include <asm/atomic.h>
53
Paul Menage81a6a5c2007-10-18 23:39:38 -070054static DEFINE_MUTEX(cgroup_mutex);
55
Paul Menageddbcc7e2007-10-18 23:39:30 -070056/* Generate an array of cgroup subsystem pointers */
57#define SUBSYS(_x) &_x ## _subsys,
58
59static struct cgroup_subsys *subsys[] = {
60#include <linux/cgroup_subsys.h>
61};
62
63/*
64 * A cgroupfs_root represents the root of a cgroup hierarchy,
65 * and may be associated with a superblock to form an active
66 * hierarchy
67 */
68struct cgroupfs_root {
69 struct super_block *sb;
70
71 /*
72 * The bitmask of subsystems intended to be attached to this
73 * hierarchy
74 */
75 unsigned long subsys_bits;
76
77 /* The bitmask of subsystems currently attached to this hierarchy */
78 unsigned long actual_subsys_bits;
79
80 /* A list running through the attached subsystems */
81 struct list_head subsys_list;
82
83 /* The root cgroup for this hierarchy */
84 struct cgroup top_cgroup;
85
86 /* Tracks how many cgroups are currently defined in hierarchy.*/
87 int number_of_cgroups;
88
Li Zefane5f6a862009-01-07 18:07:41 -080089 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -070090 struct list_head root_list;
91
92 /* Hierarchy-specific flags */
93 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -070094
Paul Menagee788e062008-07-25 01:46:59 -070095 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -070096 char release_agent_path[PATH_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -070097};
98
Paul Menageddbcc7e2007-10-18 23:39:30 -070099/*
100 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
101 * subsystems that are otherwise unattached - it never has more than a
102 * single cgroup, and all tasks are part of that cgroup.
103 */
104static struct cgroupfs_root rootnode;
105
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700106/*
107 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
108 * cgroup_subsys->use_id != 0.
109 */
110#define CSS_ID_MAX (65535)
111struct css_id {
112 /*
113 * The css to which this ID points. This pointer is set to valid value
114 * after cgroup is populated. If cgroup is removed, this will be NULL.
115 * This pointer is expected to be RCU-safe because destroy()
116 * is called after synchronize_rcu(). But for safe use, css_is_removed()
117 * css_tryget() should be used for avoiding race.
118 */
119 struct cgroup_subsys_state *css;
120 /*
121 * ID of this css.
122 */
123 unsigned short id;
124 /*
125 * Depth in hierarchy which this ID belongs to.
126 */
127 unsigned short depth;
128 /*
129 * ID is freed by RCU. (and lookup routine is RCU safe.)
130 */
131 struct rcu_head rcu_head;
132 /*
133 * Hierarchy of CSS ID belongs to.
134 */
135 unsigned short stack[0]; /* Array of Length (depth+1) */
136};
137
138
Paul Menageddbcc7e2007-10-18 23:39:30 -0700139/* The list of hierarchy roots */
140
141static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700142static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700143
144/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
145#define dummytop (&rootnode.top_cgroup)
146
147/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800148 * check for fork/exit handlers to call. This avoids us having to do
149 * extra work in the fork/exit path if none of the subsystems need to
150 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700151 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700152static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700153
Paul Menageddbcc7e2007-10-18 23:39:30 -0700154/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700155inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700156{
Paul Menagebd89aab2007-10-18 23:40:44 -0700157 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700158}
159
160/* bits in struct cgroupfs_root flags field */
161enum {
162 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
163};
164
Adrian Bunke9685a02008-02-07 00:13:46 -0800165static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700166{
167 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700168 (1 << CGRP_RELEASABLE) |
169 (1 << CGRP_NOTIFY_ON_RELEASE);
170 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700171}
172
Adrian Bunke9685a02008-02-07 00:13:46 -0800173static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700174{
Paul Menagebd89aab2007-10-18 23:40:44 -0700175 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700176}
177
Paul Menageddbcc7e2007-10-18 23:39:30 -0700178/*
179 * for_each_subsys() allows you to iterate on each subsystem attached to
180 * an active hierarchy
181 */
182#define for_each_subsys(_root, _ss) \
183list_for_each_entry(_ss, &_root->subsys_list, sibling)
184
Li Zefane5f6a862009-01-07 18:07:41 -0800185/* for_each_active_root() allows you to iterate across the active hierarchies */
186#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187list_for_each_entry(_root, &roots, root_list)
188
Paul Menage81a6a5c2007-10-18 23:39:38 -0700189/* the list of cgroups eligible for automatic release. Protected by
190 * release_list_lock */
191static LIST_HEAD(release_list);
192static DEFINE_SPINLOCK(release_list_lock);
193static void cgroup_release_agent(struct work_struct *work);
194static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700195static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700196
Paul Menage817929e2007-10-18 23:39:36 -0700197/* Link structure for associating css_set objects with cgroups */
198struct cg_cgroup_link {
199 /*
200 * List running through cg_cgroup_links associated with a
201 * cgroup, anchored on cgroup->css_sets
202 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700203 struct list_head cgrp_link_list;
Paul Menage817929e2007-10-18 23:39:36 -0700204 /*
205 * List running through cg_cgroup_links pointing at a
206 * single css_set object, anchored on css_set->cg_links
207 */
208 struct list_head cg_link_list;
209 struct css_set *cg;
210};
211
212/* The default css_set - used by init and its children prior to any
213 * hierarchies being mounted. It contains a pointer to the root state
214 * for each subsystem. Also used to anchor the list of css_sets. Not
215 * reference-counted, to improve performance when child cgroups
216 * haven't been created.
217 */
218
219static struct css_set init_css_set;
220static struct cg_cgroup_link init_css_set_link;
221
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700222static int cgroup_subsys_init_idr(struct cgroup_subsys *ss);
223
Paul Menage817929e2007-10-18 23:39:36 -0700224/* css_set_lock protects the list of css_set objects, and the
225 * chain of tasks off each css_set. Nests outside task->alloc_lock
226 * due to cgroup_iter_start() */
227static DEFINE_RWLOCK(css_set_lock);
228static int css_set_count;
229
Li Zefan472b1052008-04-29 01:00:11 -0700230/* hash table for cgroup groups. This improves the performance to
231 * find an existing css_set */
232#define CSS_SET_HASH_BITS 7
233#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
234static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
235
236static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
237{
238 int i;
239 int index;
240 unsigned long tmp = 0UL;
241
242 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
243 tmp += (unsigned long)css[i];
244 tmp = (tmp >> 16) ^ tmp;
245
246 index = hash_long(tmp, CSS_SET_HASH_BITS);
247
248 return &css_set_table[index];
249}
250
Paul Menage817929e2007-10-18 23:39:36 -0700251/* We don't maintain the lists running through each css_set to its
252 * task until after the first call to cgroup_iter_start(). This
253 * reduces the fork()/exit() overhead for people who have cgroups
254 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700255static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700256
257/* When we create or destroy a css_set, the operation simply
258 * takes/releases a reference count on all the cgroups referenced
259 * by subsystems in this css_set. This can end up multiple-counting
260 * some cgroups, but that's OK - the ref-count is just a
261 * busy/not-busy indicator; ensuring that we only count each cgroup
262 * once would require taking a global lock to ensure that no
Paul Menageb4f48b62007-10-18 23:39:33 -0700263 * subsystems moved between hierarchies while we were doing so.
264 *
265 * Possible TODO: decide at boot time based on the number of
266 * registered subsystems and the number of CPUs or NUMA nodes whether
267 * it's better for performance to ref-count every subsystem, or to
268 * take a global lock and only add one ref count to each hierarchy.
269 */
Paul Menageb4f48b62007-10-18 23:39:33 -0700270
Paul Menage817929e2007-10-18 23:39:36 -0700271/*
272 * unlink a css_set from the list and free it
273 */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700274static void unlink_css_set(struct css_set *cg)
Paul Menageb4f48b62007-10-18 23:39:33 -0700275{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700276 struct cg_cgroup_link *link;
277 struct cg_cgroup_link *saved_link;
278
Li Zefan472b1052008-04-29 01:00:11 -0700279 hlist_del(&cg->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700280 css_set_count--;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700281
282 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
283 cg_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -0700284 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700285 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700286 kfree(link);
287 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700288}
289
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700290static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700291{
292 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700293 /*
294 * Ensure that the refcount doesn't hit zero while any readers
295 * can see it. Similar to atomic_dec_and_lock(), but for an
296 * rwlock
297 */
298 if (atomic_add_unless(&cg->refcount, -1, 1))
299 return;
300 write_lock(&css_set_lock);
301 if (!atomic_dec_and_test(&cg->refcount)) {
302 write_unlock(&css_set_lock);
303 return;
304 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700305 unlink_css_set(cg);
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700306 write_unlock(&css_set_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700307
308 rcu_read_lock();
309 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagea47295e2009-01-07 18:07:44 -0800310 struct cgroup *cgrp = rcu_dereference(cg->subsys[i]->cgroup);
Paul Menagebd89aab2007-10-18 23:40:44 -0700311 if (atomic_dec_and_test(&cgrp->count) &&
312 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700313 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700314 set_bit(CGRP_RELEASABLE, &cgrp->flags);
315 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700316 }
317 }
318 rcu_read_unlock();
Paul Menage817929e2007-10-18 23:39:36 -0700319 kfree(cg);
320}
321
322/*
323 * refcounted get/put for css_set objects
324 */
325static inline void get_css_set(struct css_set *cg)
326{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700327 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700328}
329
330static inline void put_css_set(struct css_set *cg)
331{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700332 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700333}
334
Paul Menage81a6a5c2007-10-18 23:39:38 -0700335static inline void put_css_set_taskexit(struct css_set *cg)
336{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700337 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700338}
339
Paul Menage817929e2007-10-18 23:39:36 -0700340/*
341 * find_existing_css_set() is a helper for
342 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700343 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700344 *
345 * oldcg: the cgroup group that we're using before the cgroup
346 * transition
347 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700348 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700349 *
350 * template: location in which to build the desired set of subsystem
351 * state objects for the new cgroup group
352 */
Paul Menage817929e2007-10-18 23:39:36 -0700353static struct css_set *find_existing_css_set(
354 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700355 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700356 struct cgroup_subsys_state *template[])
357{
358 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700359 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700360 struct hlist_head *hhead;
361 struct hlist_node *node;
362 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700363
364 /* Built the set of subsystem state objects that we want to
365 * see in the new css_set */
366 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800367 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700368 /* Subsystem is in this hierarchy. So we want
369 * the subsystem state from the new
370 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700371 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700372 } else {
373 /* Subsystem is not in this hierarchy, so we
374 * don't want to change the subsystem state */
375 template[i] = oldcg->subsys[i];
376 }
377 }
378
Li Zefan472b1052008-04-29 01:00:11 -0700379 hhead = css_set_hash(template);
380 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage817929e2007-10-18 23:39:36 -0700381 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
382 /* All subsystems matched */
383 return cg;
384 }
Li Zefan472b1052008-04-29 01:00:11 -0700385 }
Paul Menage817929e2007-10-18 23:39:36 -0700386
387 /* No existing cgroup group matched */
388 return NULL;
389}
390
Paul Menage817929e2007-10-18 23:39:36 -0700391static void free_cg_links(struct list_head *tmp)
392{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700393 struct cg_cgroup_link *link;
394 struct cg_cgroup_link *saved_link;
395
396 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700397 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700398 kfree(link);
399 }
400}
401
402/*
Li Zefan36553432008-07-29 22:33:19 -0700403 * allocate_cg_links() allocates "count" cg_cgroup_link structures
404 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
405 * success or a negative error
406 */
407static int allocate_cg_links(int count, struct list_head *tmp)
408{
409 struct cg_cgroup_link *link;
410 int i;
411 INIT_LIST_HEAD(tmp);
412 for (i = 0; i < count; i++) {
413 link = kmalloc(sizeof(*link), GFP_KERNEL);
414 if (!link) {
415 free_cg_links(tmp);
416 return -ENOMEM;
417 }
418 list_add(&link->cgrp_link_list, tmp);
419 }
420 return 0;
421}
422
Li Zefanc12f65d2009-01-07 18:07:42 -0800423/**
424 * link_css_set - a helper function to link a css_set to a cgroup
425 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
426 * @cg: the css_set to be linked
427 * @cgrp: the destination cgroup
428 */
429static void link_css_set(struct list_head *tmp_cg_links,
430 struct css_set *cg, struct cgroup *cgrp)
431{
432 struct cg_cgroup_link *link;
433
434 BUG_ON(list_empty(tmp_cg_links));
435 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
436 cgrp_link_list);
437 link->cg = cg;
438 list_move(&link->cgrp_link_list, &cgrp->css_sets);
439 list_add(&link->cg_link_list, &cg->cg_links);
440}
441
Li Zefan36553432008-07-29 22:33:19 -0700442/*
Paul Menage817929e2007-10-18 23:39:36 -0700443 * find_css_set() takes an existing cgroup group and a
444 * cgroup object, and returns a css_set object that's
445 * equivalent to the old group, but with the given cgroup
446 * substituted into the appropriate hierarchy. Must be called with
447 * cgroup_mutex held
448 */
Paul Menage817929e2007-10-18 23:39:36 -0700449static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700450 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700451{
452 struct css_set *res;
453 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
454 int i;
455
456 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700457
Li Zefan472b1052008-04-29 01:00:11 -0700458 struct hlist_head *hhead;
459
Paul Menage817929e2007-10-18 23:39:36 -0700460 /* First see if we already have a cgroup group that matches
461 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700462 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700463 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700464 if (res)
465 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700466 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700467
468 if (res)
469 return res;
470
471 res = kmalloc(sizeof(*res), GFP_KERNEL);
472 if (!res)
473 return NULL;
474
475 /* Allocate all the cg_cgroup_link objects that we'll need */
476 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
477 kfree(res);
478 return NULL;
479 }
480
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700481 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700482 INIT_LIST_HEAD(&res->cg_links);
483 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700484 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700485
486 /* Copy the set of subsystem state objects generated in
487 * find_existing_css_set() */
488 memcpy(res->subsys, template, sizeof(res->subsys));
489
490 write_lock(&css_set_lock);
491 /* Add reference counts and links from the new css_set. */
492 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700493 struct cgroup *cgrp = res->subsys[i]->cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700494 struct cgroup_subsys *ss = subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -0700495 atomic_inc(&cgrp->count);
Paul Menage817929e2007-10-18 23:39:36 -0700496 /*
497 * We want to add a link once per cgroup, so we
498 * only do it for the first subsystem in each
499 * hierarchy
500 */
Li Zefanc12f65d2009-01-07 18:07:42 -0800501 if (ss->root->subsys_list.next == &ss->sibling)
502 link_css_set(&tmp_cg_links, res, cgrp);
Paul Menage817929e2007-10-18 23:39:36 -0700503 }
Li Zefanc12f65d2009-01-07 18:07:42 -0800504 if (list_empty(&rootnode.subsys_list))
505 link_css_set(&tmp_cg_links, res, dummytop);
Paul Menage817929e2007-10-18 23:39:36 -0700506
507 BUG_ON(!list_empty(&tmp_cg_links));
508
Paul Menage817929e2007-10-18 23:39:36 -0700509 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700510
511 /* Add this cgroup group to the hash table */
512 hhead = css_set_hash(res->subsys);
513 hlist_add_head(&res->hlist, hhead);
514
Paul Menage817929e2007-10-18 23:39:36 -0700515 write_unlock(&css_set_lock);
516
517 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700518}
519
Paul Menageddbcc7e2007-10-18 23:39:30 -0700520/*
521 * There is one global cgroup mutex. We also require taking
522 * task_lock() when dereferencing a task's cgroup subsys pointers.
523 * See "The task_lock() exception", at the end of this comment.
524 *
525 * A task must hold cgroup_mutex to modify cgroups.
526 *
527 * Any task can increment and decrement the count field without lock.
528 * So in general, code holding cgroup_mutex can't rely on the count
529 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800530 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700531 * means that no tasks are currently attached, therefore there is no
532 * way a task attached to that cgroup can fork (the other way to
533 * increment the count). So code holding cgroup_mutex can safely
534 * assume that if the count is zero, it will stay zero. Similarly, if
535 * a task holds cgroup_mutex on a cgroup with zero count, it
536 * knows that the cgroup won't be removed, as cgroup_rmdir()
537 * needs that mutex.
538 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700539 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
540 * (usually) take cgroup_mutex. These are the two most performance
541 * critical pieces of code here. The exception occurs on cgroup_exit(),
542 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
543 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800544 * to the release agent with the name of the cgroup (path relative to
545 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700546 *
547 * A cgroup can only be deleted if both its 'count' of using tasks
548 * is zero, and its list of 'children' cgroups is empty. Since all
549 * tasks in the system use _some_ cgroup, and since there is always at
550 * least one task in the system (init, pid == 1), therefore, top_cgroup
551 * always has either children cgroups and/or using tasks. So we don't
552 * need a special hack to ensure that top_cgroup cannot be deleted.
553 *
554 * The task_lock() exception
555 *
556 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800557 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800558 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700559 * several performance critical places that need to reference
560 * task->cgroup without the expense of grabbing a system global
561 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800562 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700563 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
564 * the task_struct routinely used for such matters.
565 *
566 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800567 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700568 */
569
Paul Menageddbcc7e2007-10-18 23:39:30 -0700570/**
571 * cgroup_lock - lock out any changes to cgroup structures
572 *
573 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700574void cgroup_lock(void)
575{
576 mutex_lock(&cgroup_mutex);
577}
578
579/**
580 * cgroup_unlock - release lock on cgroup changes
581 *
582 * Undo the lock taken in a previous cgroup_lock() call.
583 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700584void cgroup_unlock(void)
585{
586 mutex_unlock(&cgroup_mutex);
587}
588
589/*
590 * A couple of forward declarations required, due to cyclic reference loop:
591 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
592 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
593 * -> cgroup_mkdir.
594 */
595
596static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
597static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700598static int cgroup_populate_dir(struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700599static struct inode_operations cgroup_dir_inode_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700600static struct file_operations proc_cgroupstats_operations;
601
602static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200603 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700604 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700605};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700606
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700607static int alloc_css_id(struct cgroup_subsys *ss,
608 struct cgroup *parent, struct cgroup *child);
609
Paul Menageddbcc7e2007-10-18 23:39:30 -0700610static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
611{
612 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700613
614 if (inode) {
615 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100616 inode->i_uid = current_fsuid();
617 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700618 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
619 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
620 }
621 return inode;
622}
623
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800624/*
625 * Call subsys's pre_destroy handler.
626 * This is called before css refcnt check.
627 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700628static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800629{
630 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700631 int ret = 0;
632
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800633 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700634 if (ss->pre_destroy) {
635 ret = ss->pre_destroy(ss, cgrp);
636 if (ret)
637 break;
638 }
639 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800640}
641
Paul Menagea47295e2009-01-07 18:07:44 -0800642static void free_cgroup_rcu(struct rcu_head *obj)
643{
644 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
645
646 kfree(cgrp);
647}
648
Paul Menageddbcc7e2007-10-18 23:39:30 -0700649static void cgroup_diput(struct dentry *dentry, struct inode *inode)
650{
651 /* is dentry a directory ? if so, kfree() associated cgroup */
652 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700653 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800654 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700655 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700656 /* It's possible for external users to be holding css
657 * reference counts on a cgroup; css_put() needs to
658 * be able to access the cgroup after decrementing
659 * the reference count in order to know if it needs to
660 * queue the cgroup to be handled by the release
661 * agent */
662 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800663
664 mutex_lock(&cgroup_mutex);
665 /*
666 * Release the subsystem state objects.
667 */
Li Zefan75139b82009-01-07 18:07:33 -0800668 for_each_subsys(cgrp->root, ss)
669 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800670
671 cgrp->root->number_of_cgroups--;
672 mutex_unlock(&cgroup_mutex);
673
Paul Menagea47295e2009-01-07 18:07:44 -0800674 /*
675 * Drop the active superblock reference that we took when we
676 * created the cgroup
677 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800678 deactivate_super(cgrp->root->sb);
679
Paul Menagea47295e2009-01-07 18:07:44 -0800680 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700681 }
682 iput(inode);
683}
684
685static void remove_dir(struct dentry *d)
686{
687 struct dentry *parent = dget(d->d_parent);
688
689 d_delete(d);
690 simple_rmdir(parent->d_inode, d);
691 dput(parent);
692}
693
694static void cgroup_clear_directory(struct dentry *dentry)
695{
696 struct list_head *node;
697
698 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
699 spin_lock(&dcache_lock);
700 node = dentry->d_subdirs.next;
701 while (node != &dentry->d_subdirs) {
702 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
703 list_del_init(node);
704 if (d->d_inode) {
705 /* This should never be called on a cgroup
706 * directory with child cgroups */
707 BUG_ON(d->d_inode->i_mode & S_IFDIR);
708 d = dget_locked(d);
709 spin_unlock(&dcache_lock);
710 d_delete(d);
711 simple_unlink(dentry->d_inode, d);
712 dput(d);
713 spin_lock(&dcache_lock);
714 }
715 node = dentry->d_subdirs.next;
716 }
717 spin_unlock(&dcache_lock);
718}
719
720/*
721 * NOTE : the dentry must have been dget()'ed
722 */
723static void cgroup_d_remove_dir(struct dentry *dentry)
724{
725 cgroup_clear_directory(dentry);
726
727 spin_lock(&dcache_lock);
728 list_del_init(&dentry->d_u.d_child);
729 spin_unlock(&dcache_lock);
730 remove_dir(dentry);
731}
732
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700733/*
734 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
735 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
736 * reference to css->refcnt. In general, this refcnt is expected to goes down
737 * to zero, soon.
738 *
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700739 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700740 */
741DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
742
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700743static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700744{
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700745 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700746 wake_up_all(&cgroup_rmdir_waitq);
747}
748
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700749void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
750{
751 css_get(css);
752}
753
754void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
755{
756 cgroup_wakeup_rmdir_waiter(css->cgroup);
757 css_put(css);
758}
759
760
Paul Menageddbcc7e2007-10-18 23:39:30 -0700761static int rebind_subsystems(struct cgroupfs_root *root,
762 unsigned long final_bits)
763{
764 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700765 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700766 int i;
767
768 removed_bits = root->actual_subsys_bits & ~final_bits;
769 added_bits = final_bits & ~root->actual_subsys_bits;
770 /* Check that any added subsystems are currently free */
771 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800772 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773 struct cgroup_subsys *ss = subsys[i];
774 if (!(bit & added_bits))
775 continue;
776 if (ss->root != &rootnode) {
777 /* Subsystem isn't free */
778 return -EBUSY;
779 }
780 }
781
782 /* Currently we don't handle adding/removing subsystems when
783 * any child cgroups exist. This is theoretically supportable
784 * but involves complex error handling, so it's being left until
785 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800786 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700787 return -EBUSY;
788
789 /* Process each subsystem */
790 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
791 struct cgroup_subsys *ss = subsys[i];
792 unsigned long bit = 1UL << i;
793 if (bit & added_bits) {
794 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700795 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796 BUG_ON(!dummytop->subsys[i]);
797 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800798 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700799 cgrp->subsys[i] = dummytop->subsys[i];
800 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800801 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800802 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700804 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800805 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806 } else if (bit & removed_bits) {
807 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700808 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
809 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800810 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700811 if (ss->bind)
812 ss->bind(ss, dummytop);
813 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700814 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800815 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -0800816 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -0800817 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700818 } else if (bit & final_bits) {
819 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700820 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700821 } else {
822 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700823 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700824 }
825 }
826 root->subsys_bits = root->actual_subsys_bits = final_bits;
827 synchronize_rcu();
828
829 return 0;
830}
831
832static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
833{
834 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
835 struct cgroup_subsys *ss;
836
837 mutex_lock(&cgroup_mutex);
838 for_each_subsys(root, ss)
839 seq_printf(seq, ",%s", ss->name);
840 if (test_bit(ROOT_NOPREFIX, &root->flags))
841 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700842 if (strlen(root->release_agent_path))
843 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700844 mutex_unlock(&cgroup_mutex);
845 return 0;
846}
847
848struct cgroup_sb_opts {
849 unsigned long subsys_bits;
850 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700851 char *release_agent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700852};
853
854/* Convert a hierarchy specifier into a bitmask of subsystems and
855 * flags. */
856static int parse_cgroupfs_options(char *data,
857 struct cgroup_sb_opts *opts)
858{
859 char *token, *o = data ?: "all";
Li Zefanf9ab5b52009-06-17 16:26:33 -0700860 unsigned long mask = (unsigned long)-1;
861
862#ifdef CONFIG_CPUSETS
863 mask = ~(1UL << cpuset_subsys_id);
864#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -0700865
866 opts->subsys_bits = 0;
867 opts->flags = 0;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700868 opts->release_agent = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700869
870 while ((token = strsep(&o, ",")) != NULL) {
871 if (!*token)
872 return -EINVAL;
873 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700874 /* Add all non-disabled subsystems */
875 int i;
876 opts->subsys_bits = 0;
877 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
878 struct cgroup_subsys *ss = subsys[i];
879 if (!ss->disabled)
880 opts->subsys_bits |= 1ul << i;
881 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700882 } else if (!strcmp(token, "noprefix")) {
883 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700884 } else if (!strncmp(token, "release_agent=", 14)) {
885 /* Specifying two release agents is forbidden */
886 if (opts->release_agent)
887 return -EINVAL;
888 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
889 if (!opts->release_agent)
890 return -ENOMEM;
891 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
892 opts->release_agent[PATH_MAX - 1] = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700893 } else {
894 struct cgroup_subsys *ss;
895 int i;
896 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
897 ss = subsys[i];
898 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700899 if (!ss->disabled)
900 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700901 break;
902 }
903 }
904 if (i == CGROUP_SUBSYS_COUNT)
905 return -ENOENT;
906 }
907 }
908
Li Zefanf9ab5b52009-06-17 16:26:33 -0700909 /*
910 * Option noprefix was introduced just for backward compatibility
911 * with the old cpuset, so we allow noprefix only if mounting just
912 * the cpuset subsystem.
913 */
914 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
915 (opts->subsys_bits & mask))
916 return -EINVAL;
917
Paul Menageddbcc7e2007-10-18 23:39:30 -0700918 /* We can't have an empty hierarchy */
919 if (!opts->subsys_bits)
920 return -EINVAL;
921
922 return 0;
923}
924
925static int cgroup_remount(struct super_block *sb, int *flags, char *data)
926{
927 int ret = 0;
928 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -0700929 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700930 struct cgroup_sb_opts opts;
931
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200932 lock_kernel();
Paul Menagebd89aab2007-10-18 23:40:44 -0700933 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700934 mutex_lock(&cgroup_mutex);
935
936 /* See what subsystems are wanted */
937 ret = parse_cgroupfs_options(data, &opts);
938 if (ret)
939 goto out_unlock;
940
941 /* Don't allow flags to change at remount */
942 if (opts.flags != root->flags) {
943 ret = -EINVAL;
944 goto out_unlock;
945 }
946
947 ret = rebind_subsystems(root, opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -0700948 if (ret)
949 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700950
951 /* (re)populate subsystem files */
Li Zefan0670e082009-04-02 16:57:30 -0700952 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700953
Paul Menage81a6a5c2007-10-18 23:39:38 -0700954 if (opts.release_agent)
955 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700956 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -0700957 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700958 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700959 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200960 unlock_kernel();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700961 return ret;
962}
963
964static struct super_operations cgroup_ops = {
965 .statfs = simple_statfs,
966 .drop_inode = generic_delete_inode,
967 .show_options = cgroup_show_options,
968 .remount_fs = cgroup_remount,
969};
970
Paul Menagecc31edc2008-10-18 20:28:04 -0700971static void init_cgroup_housekeeping(struct cgroup *cgrp)
972{
973 INIT_LIST_HEAD(&cgrp->sibling);
974 INIT_LIST_HEAD(&cgrp->children);
975 INIT_LIST_HEAD(&cgrp->css_sets);
976 INIT_LIST_HEAD(&cgrp->release_list);
Li Zefan096b7fe2009-07-29 15:04:04 -0700977 INIT_LIST_HEAD(&cgrp->pids_list);
Paul Menagecc31edc2008-10-18 20:28:04 -0700978 init_rwsem(&cgrp->pids_mutex);
979}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980static void init_cgroup_root(struct cgroupfs_root *root)
981{
Paul Menagebd89aab2007-10-18 23:40:44 -0700982 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983 INIT_LIST_HEAD(&root->subsys_list);
984 INIT_LIST_HEAD(&root->root_list);
985 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -0700986 cgrp->root = root;
987 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -0700988 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700989}
990
991static int cgroup_test_super(struct super_block *sb, void *data)
992{
993 struct cgroupfs_root *new = data;
994 struct cgroupfs_root *root = sb->s_fs_info;
995
996 /* First check subsystems */
997 if (new->subsys_bits != root->subsys_bits)
998 return 0;
999
1000 /* Next check flags */
1001 if (new->flags != root->flags)
1002 return 0;
1003
1004 return 1;
1005}
1006
1007static int cgroup_set_super(struct super_block *sb, void *data)
1008{
1009 int ret;
1010 struct cgroupfs_root *root = data;
1011
1012 ret = set_anon_super(sb, NULL);
1013 if (ret)
1014 return ret;
1015
1016 sb->s_fs_info = root;
1017 root->sb = sb;
1018
1019 sb->s_blocksize = PAGE_CACHE_SIZE;
1020 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1021 sb->s_magic = CGROUP_SUPER_MAGIC;
1022 sb->s_op = &cgroup_ops;
1023
1024 return 0;
1025}
1026
1027static int cgroup_get_rootdir(struct super_block *sb)
1028{
1029 struct inode *inode =
1030 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1031 struct dentry *dentry;
1032
1033 if (!inode)
1034 return -ENOMEM;
1035
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 inode->i_fop = &simple_dir_operations;
1037 inode->i_op = &cgroup_dir_inode_operations;
1038 /* directories start off with i_nlink == 2 (for "." entry) */
1039 inc_nlink(inode);
1040 dentry = d_alloc_root(inode);
1041 if (!dentry) {
1042 iput(inode);
1043 return -ENOMEM;
1044 }
1045 sb->s_root = dentry;
1046 return 0;
1047}
1048
1049static int cgroup_get_sb(struct file_system_type *fs_type,
1050 int flags, const char *unused_dev_name,
1051 void *data, struct vfsmount *mnt)
1052{
1053 struct cgroup_sb_opts opts;
1054 int ret = 0;
1055 struct super_block *sb;
1056 struct cgroupfs_root *root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001057 struct list_head tmp_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058
1059 /* First find the desired set of subsystems */
1060 ret = parse_cgroupfs_options(data, &opts);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001061 if (ret) {
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001062 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 return ret;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001064 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065
1066 root = kzalloc(sizeof(*root), GFP_KERNEL);
Li Zefanf7770732008-02-23 15:24:10 -08001067 if (!root) {
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001068 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001069 return -ENOMEM;
Li Zefanf7770732008-02-23 15:24:10 -08001070 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071
1072 init_cgroup_root(root);
1073 root->subsys_bits = opts.subsys_bits;
1074 root->flags = opts.flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001075 if (opts.release_agent) {
1076 strcpy(root->release_agent_path, opts.release_agent);
1077 kfree(opts.release_agent);
1078 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079
1080 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
1081
1082 if (IS_ERR(sb)) {
1083 kfree(root);
1084 return PTR_ERR(sb);
1085 }
1086
1087 if (sb->s_fs_info != root) {
1088 /* Reusing an existing superblock */
1089 BUG_ON(sb->s_root == NULL);
1090 kfree(root);
1091 root = NULL;
1092 } else {
1093 /* New superblock */
Li Zefanc12f65d2009-01-07 18:07:42 -08001094 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001095 struct inode *inode;
Li Zefan28fd5df2008-04-29 01:00:13 -07001096 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097
1098 BUG_ON(sb->s_root != NULL);
1099
1100 ret = cgroup_get_rootdir(sb);
1101 if (ret)
1102 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001103 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104
Paul Menage817929e2007-10-18 23:39:36 -07001105 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106 mutex_lock(&cgroup_mutex);
1107
Paul Menage817929e2007-10-18 23:39:36 -07001108 /*
1109 * We're accessing css_set_count without locking
1110 * css_set_lock here, but that's OK - it can only be
1111 * increased by someone holding cgroup_lock, and
1112 * that's us. The worst that can happen is that we
1113 * have some link structures left over
1114 */
1115 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1116 if (ret) {
1117 mutex_unlock(&cgroup_mutex);
1118 mutex_unlock(&inode->i_mutex);
1119 goto drop_new_super;
1120 }
1121
Paul Menageddbcc7e2007-10-18 23:39:30 -07001122 ret = rebind_subsystems(root, root->subsys_bits);
1123 if (ret == -EBUSY) {
1124 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001125 mutex_unlock(&inode->i_mutex);
Li Zefan20ca9b32008-12-23 13:57:14 -08001126 goto free_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127 }
1128
1129 /* EBUSY should be the only error here */
1130 BUG_ON(ret);
1131
1132 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001133 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001134
Li Zefanc12f65d2009-01-07 18:07:42 -08001135 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136 root->top_cgroup.dentry = sb->s_root;
1137
Paul Menage817929e2007-10-18 23:39:36 -07001138 /* Link the top cgroup in this hierarchy into all
1139 * the css_set objects */
1140 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001141 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1142 struct hlist_head *hhead = &css_set_table[i];
1143 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001144 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001145
Li Zefanc12f65d2009-01-07 18:07:42 -08001146 hlist_for_each_entry(cg, node, hhead, hlist)
1147 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001148 }
Paul Menage817929e2007-10-18 23:39:36 -07001149 write_unlock(&css_set_lock);
1150
1151 free_cg_links(&tmp_cg_links);
1152
Li Zefanc12f65d2009-01-07 18:07:42 -08001153 BUG_ON(!list_empty(&root_cgrp->sibling));
1154 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001155 BUG_ON(root->number_of_cgroups != 1);
1156
Li Zefanc12f65d2009-01-07 18:07:42 -08001157 cgroup_populate_dir(root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001158 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001159 mutex_unlock(&cgroup_mutex);
1160 }
1161
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001162 simple_set_mnt(mnt, sb);
1163 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001164
Li Zefan20ca9b32008-12-23 13:57:14 -08001165 free_cg_links:
1166 free_cg_links(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001167 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001168 deactivate_locked_super(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001169 return ret;
1170}
1171
1172static void cgroup_kill_sb(struct super_block *sb) {
1173 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001174 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001175 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001176 struct cg_cgroup_link *link;
1177 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001178
1179 BUG_ON(!root);
1180
1181 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001182 BUG_ON(!list_empty(&cgrp->children));
1183 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001184
1185 mutex_lock(&cgroup_mutex);
1186
1187 /* Rebind all subsystems back to the default hierarchy */
1188 ret = rebind_subsystems(root, 0);
1189 /* Shouldn't be able to fail ... */
1190 BUG_ON(ret);
1191
Paul Menage817929e2007-10-18 23:39:36 -07001192 /*
1193 * Release all the links from css_sets to this hierarchy's
1194 * root cgroup
1195 */
1196 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001197
1198 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1199 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001200 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001201 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001202 kfree(link);
1203 }
1204 write_unlock(&css_set_lock);
1205
Paul Menage839ec542009-01-29 14:25:22 -08001206 if (!list_empty(&root->root_list)) {
1207 list_del(&root->root_list);
1208 root_count--;
1209 }
Li Zefane5f6a862009-01-07 18:07:41 -08001210
Paul Menageddbcc7e2007-10-18 23:39:30 -07001211 mutex_unlock(&cgroup_mutex);
1212
Paul Menageddbcc7e2007-10-18 23:39:30 -07001213 kill_litter_super(sb);
Li Zefan67e055d2009-02-18 14:48:20 -08001214 kfree(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001215}
1216
1217static struct file_system_type cgroup_fs_type = {
1218 .name = "cgroup",
1219 .get_sb = cgroup_get_sb,
1220 .kill_sb = cgroup_kill_sb,
1221};
1222
Paul Menagebd89aab2007-10-18 23:40:44 -07001223static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001224{
1225 return dentry->d_fsdata;
1226}
1227
1228static inline struct cftype *__d_cft(struct dentry *dentry)
1229{
1230 return dentry->d_fsdata;
1231}
1232
Li Zefana043e3b2008-02-23 15:24:09 -08001233/**
1234 * cgroup_path - generate the path of a cgroup
1235 * @cgrp: the cgroup in question
1236 * @buf: the buffer to write the path into
1237 * @buflen: the length of the buffer
1238 *
Paul Menagea47295e2009-01-07 18:07:44 -08001239 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1240 * reference. Writes path of cgroup into buf. Returns 0 on success,
1241 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001242 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001243int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001244{
1245 char *start;
Paul Menagea47295e2009-01-07 18:07:44 -08001246 struct dentry *dentry = rcu_dereference(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001247
Paul Menagea47295e2009-01-07 18:07:44 -08001248 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001249 /*
1250 * Inactive subsystems have no dentry for their root
1251 * cgroup
1252 */
1253 strcpy(buf, "/");
1254 return 0;
1255 }
1256
1257 start = buf + buflen;
1258
1259 *--start = '\0';
1260 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001261 int len = dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001262 if ((start -= len) < buf)
1263 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001264 memcpy(start, cgrp->dentry->d_name.name, len);
1265 cgrp = cgrp->parent;
1266 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001267 break;
Paul Menagea47295e2009-01-07 18:07:44 -08001268 dentry = rcu_dereference(cgrp->dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001269 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001270 continue;
1271 if (--start < buf)
1272 return -ENAMETOOLONG;
1273 *start = '/';
1274 }
1275 memmove(buf, start, buf + buflen - start);
1276 return 0;
1277}
1278
Paul Menagebbcb81d2007-10-18 23:39:32 -07001279/*
1280 * Return the first subsystem attached to a cgroup's hierarchy, and
1281 * its subsystem id.
1282 */
1283
Paul Menagebd89aab2007-10-18 23:40:44 -07001284static void get_first_subsys(const struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001285 struct cgroup_subsys_state **css, int *subsys_id)
1286{
Paul Menagebd89aab2007-10-18 23:40:44 -07001287 const struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001288 const struct cgroup_subsys *test_ss;
1289 BUG_ON(list_empty(&root->subsys_list));
1290 test_ss = list_entry(root->subsys_list.next,
1291 struct cgroup_subsys, sibling);
1292 if (css) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001293 *css = cgrp->subsys[test_ss->subsys_id];
Paul Menagebbcb81d2007-10-18 23:39:32 -07001294 BUG_ON(!*css);
1295 }
1296 if (subsys_id)
1297 *subsys_id = test_ss->subsys_id;
1298}
1299
Li Zefana043e3b2008-02-23 15:24:09 -08001300/**
1301 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1302 * @cgrp: the cgroup the task is attaching to
1303 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001304 *
Li Zefana043e3b2008-02-23 15:24:09 -08001305 * Call holding cgroup_mutex. May take task_lock of
1306 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001307 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001308int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001309{
1310 int retval = 0;
1311 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07001312 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001313 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001314 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001315 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001316 int subsys_id;
1317
Paul Menagebd89aab2007-10-18 23:40:44 -07001318 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001319
1320 /* Nothing to do if the task is already in that cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -07001321 oldcgrp = task_cgroup(tsk, subsys_id);
1322 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001323 return 0;
1324
1325 for_each_subsys(root, ss) {
1326 if (ss->can_attach) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001327 retval = ss->can_attach(ss, cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001328 if (retval)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001329 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001330 }
1331 }
1332
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001333 task_lock(tsk);
1334 cg = tsk->cgroups;
1335 get_css_set(cg);
1336 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001337 /*
1338 * Locate or allocate a new css_set for this task,
1339 * based on its final set of cgroups
1340 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001341 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001342 put_css_set(cg);
Paul Jacksone18f6312008-02-07 00:13:44 -08001343 if (!newcg)
Paul Menage817929e2007-10-18 23:39:36 -07001344 return -ENOMEM;
Paul Menage817929e2007-10-18 23:39:36 -07001345
Paul Menagebbcb81d2007-10-18 23:39:32 -07001346 task_lock(tsk);
1347 if (tsk->flags & PF_EXITING) {
1348 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001349 put_css_set(newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001350 return -ESRCH;
1351 }
Paul Menage817929e2007-10-18 23:39:36 -07001352 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001353 task_unlock(tsk);
1354
Paul Menage817929e2007-10-18 23:39:36 -07001355 /* Update the css_set linked lists if we're using them */
1356 write_lock(&css_set_lock);
1357 if (!list_empty(&tsk->cg_list)) {
1358 list_del(&tsk->cg_list);
1359 list_add(&tsk->cg_list, &newcg->tasks);
1360 }
1361 write_unlock(&css_set_lock);
1362
Paul Menagebbcb81d2007-10-18 23:39:32 -07001363 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001364 if (ss->attach)
Paul Menagebd89aab2007-10-18 23:40:44 -07001365 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001366 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001367 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001368 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001369 put_css_set(cg);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001370
1371 /*
1372 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1373 * is no longer empty.
1374 */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001375 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001376 return 0;
1377}
1378
1379/*
Paul Menageaf351022008-07-25 01:47:01 -07001380 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1381 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001382 */
Paul Menageaf351022008-07-25 01:47:01 -07001383static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001384{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001385 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001386 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001387 int ret;
1388
Paul Menagebbcb81d2007-10-18 23:39:32 -07001389 if (pid) {
1390 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001391 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001392 if (!tsk || tsk->flags & PF_EXITING) {
1393 rcu_read_unlock();
1394 return -ESRCH;
1395 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001396
David Howellsc69e8d92008-11-14 10:39:19 +11001397 tcred = __task_cred(tsk);
1398 if (cred->euid &&
1399 cred->euid != tcred->uid &&
1400 cred->euid != tcred->suid) {
1401 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001402 return -EACCES;
1403 }
David Howellsc69e8d92008-11-14 10:39:19 +11001404 get_task_struct(tsk);
1405 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001406 } else {
1407 tsk = current;
1408 get_task_struct(tsk);
1409 }
1410
Cliff Wickman956db3c2008-02-07 00:14:43 -08001411 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001412 put_task_struct(tsk);
1413 return ret;
1414}
1415
Paul Menageaf351022008-07-25 01:47:01 -07001416static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1417{
1418 int ret;
1419 if (!cgroup_lock_live_group(cgrp))
1420 return -ENODEV;
1421 ret = attach_task_by_pid(cgrp, pid);
1422 cgroup_unlock();
1423 return ret;
1424}
1425
Paul Menageddbcc7e2007-10-18 23:39:30 -07001426/* The various types of files and directories in a cgroup file system */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001427enum cgroup_filetype {
1428 FILE_ROOT,
1429 FILE_DIR,
1430 FILE_TASKLIST,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001431 FILE_NOTIFY_ON_RELEASE,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001432 FILE_RELEASE_AGENT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001433};
1434
Paul Menagee788e062008-07-25 01:46:59 -07001435/**
1436 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1437 * @cgrp: the cgroup to be checked for liveness
1438 *
Paul Menage84eea842008-07-25 01:47:00 -07001439 * On success, returns true; the lock should be later released with
1440 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07001441 */
Paul Menage84eea842008-07-25 01:47:00 -07001442bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07001443{
1444 mutex_lock(&cgroup_mutex);
1445 if (cgroup_is_removed(cgrp)) {
1446 mutex_unlock(&cgroup_mutex);
1447 return false;
1448 }
1449 return true;
1450}
1451
1452static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1453 const char *buffer)
1454{
1455 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1456 if (!cgroup_lock_live_group(cgrp))
1457 return -ENODEV;
1458 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001459 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001460 return 0;
1461}
1462
1463static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1464 struct seq_file *seq)
1465{
1466 if (!cgroup_lock_live_group(cgrp))
1467 return -ENODEV;
1468 seq_puts(seq, cgrp->root->release_agent_path);
1469 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001470 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001471 return 0;
1472}
1473
Paul Menage84eea842008-07-25 01:47:00 -07001474/* A buffer size big enough for numbers or short strings */
1475#define CGROUP_LOCAL_BUFFER_SIZE 64
1476
Paul Menagee73d2c62008-04-29 01:00:06 -07001477static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001478 struct file *file,
1479 const char __user *userbuf,
1480 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001481{
Paul Menage84eea842008-07-25 01:47:00 -07001482 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001483 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001484 char *end;
1485
1486 if (!nbytes)
1487 return -EINVAL;
1488 if (nbytes >= sizeof(buffer))
1489 return -E2BIG;
1490 if (copy_from_user(buffer, userbuf, nbytes))
1491 return -EFAULT;
1492
1493 buffer[nbytes] = 0; /* nul-terminate */
Paul Menageb7269df2008-04-29 00:59:59 -07001494 strstrip(buffer);
Paul Menagee73d2c62008-04-29 01:00:06 -07001495 if (cft->write_u64) {
1496 u64 val = simple_strtoull(buffer, &end, 0);
1497 if (*end)
1498 return -EINVAL;
1499 retval = cft->write_u64(cgrp, cft, val);
1500 } else {
1501 s64 val = simple_strtoll(buffer, &end, 0);
1502 if (*end)
1503 return -EINVAL;
1504 retval = cft->write_s64(cgrp, cft, val);
1505 }
Paul Menage355e0c42007-10-18 23:39:33 -07001506 if (!retval)
1507 retval = nbytes;
1508 return retval;
1509}
1510
Paul Menagedb3b1492008-07-25 01:46:58 -07001511static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1512 struct file *file,
1513 const char __user *userbuf,
1514 size_t nbytes, loff_t *unused_ppos)
1515{
Paul Menage84eea842008-07-25 01:47:00 -07001516 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001517 int retval = 0;
1518 size_t max_bytes = cft->max_write_len;
1519 char *buffer = local_buffer;
1520
1521 if (!max_bytes)
1522 max_bytes = sizeof(local_buffer) - 1;
1523 if (nbytes >= max_bytes)
1524 return -E2BIG;
1525 /* Allocate a dynamic buffer if we need one */
1526 if (nbytes >= sizeof(local_buffer)) {
1527 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1528 if (buffer == NULL)
1529 return -ENOMEM;
1530 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001531 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1532 retval = -EFAULT;
1533 goto out;
1534 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001535
1536 buffer[nbytes] = 0; /* nul-terminate */
1537 strstrip(buffer);
1538 retval = cft->write_string(cgrp, cft, buffer);
1539 if (!retval)
1540 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001541out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001542 if (buffer != local_buffer)
1543 kfree(buffer);
1544 return retval;
1545}
1546
Paul Menageddbcc7e2007-10-18 23:39:30 -07001547static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1548 size_t nbytes, loff_t *ppos)
1549{
1550 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001551 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552
Li Zefan75139b82009-01-07 18:07:33 -08001553 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001555 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001556 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001557 if (cft->write_u64 || cft->write_s64)
1558 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001559 if (cft->write_string)
1560 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001561 if (cft->trigger) {
1562 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1563 return ret ? ret : nbytes;
1564 }
Paul Menage355e0c42007-10-18 23:39:33 -07001565 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566}
1567
Paul Menagef4c753b2008-04-29 00:59:56 -07001568static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1569 struct file *file,
1570 char __user *buf, size_t nbytes,
1571 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001572{
Paul Menage84eea842008-07-25 01:47:00 -07001573 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001574 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1576
1577 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1578}
1579
Paul Menagee73d2c62008-04-29 01:00:06 -07001580static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1581 struct file *file,
1582 char __user *buf, size_t nbytes,
1583 loff_t *ppos)
1584{
Paul Menage84eea842008-07-25 01:47:00 -07001585 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001586 s64 val = cft->read_s64(cgrp, cft);
1587 int len = sprintf(tmp, "%lld\n", (long long) val);
1588
1589 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1590}
1591
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1593 size_t nbytes, loff_t *ppos)
1594{
1595 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001596 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001597
Li Zefan75139b82009-01-07 18:07:33 -08001598 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001599 return -ENODEV;
1600
1601 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001602 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001603 if (cft->read_u64)
1604 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001605 if (cft->read_s64)
1606 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607 return -EINVAL;
1608}
1609
Paul Menage91796562008-04-29 01:00:01 -07001610/*
1611 * seqfile ops/methods for returning structured data. Currently just
1612 * supports string->u64 maps, but can be extended in future.
1613 */
1614
1615struct cgroup_seqfile_state {
1616 struct cftype *cft;
1617 struct cgroup *cgroup;
1618};
1619
1620static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1621{
1622 struct seq_file *sf = cb->state;
1623 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1624}
1625
1626static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1627{
1628 struct cgroup_seqfile_state *state = m->private;
1629 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001630 if (cft->read_map) {
1631 struct cgroup_map_cb cb = {
1632 .fill = cgroup_map_add,
1633 .state = m,
1634 };
1635 return cft->read_map(state->cgroup, cft, &cb);
1636 }
1637 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001638}
1639
Adrian Bunk96930a62008-07-25 19:46:21 -07001640static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07001641{
1642 struct seq_file *seq = file->private_data;
1643 kfree(seq->private);
1644 return single_release(inode, file);
1645}
1646
1647static struct file_operations cgroup_seqfile_operations = {
1648 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07001649 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07001650 .llseek = seq_lseek,
1651 .release = cgroup_seqfile_release,
1652};
1653
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654static int cgroup_file_open(struct inode *inode, struct file *file)
1655{
1656 int err;
1657 struct cftype *cft;
1658
1659 err = generic_file_open(inode, file);
1660 if (err)
1661 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08001663
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001664 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001665 struct cgroup_seqfile_state *state =
1666 kzalloc(sizeof(*state), GFP_USER);
1667 if (!state)
1668 return -ENOMEM;
1669 state->cft = cft;
1670 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1671 file->f_op = &cgroup_seqfile_operations;
1672 err = single_open(file, cgroup_seqfile_show, state);
1673 if (err < 0)
1674 kfree(state);
1675 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 err = cft->open(inode, file);
1677 else
1678 err = 0;
1679
1680 return err;
1681}
1682
1683static int cgroup_file_release(struct inode *inode, struct file *file)
1684{
1685 struct cftype *cft = __d_cft(file->f_dentry);
1686 if (cft->release)
1687 return cft->release(inode, file);
1688 return 0;
1689}
1690
1691/*
1692 * cgroup_rename - Only allow simple rename of directories in place.
1693 */
1694static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1695 struct inode *new_dir, struct dentry *new_dentry)
1696{
1697 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1698 return -ENOTDIR;
1699 if (new_dentry->d_inode)
1700 return -EEXIST;
1701 if (old_dir != new_dir)
1702 return -EIO;
1703 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1704}
1705
1706static struct file_operations cgroup_file_operations = {
1707 .read = cgroup_file_read,
1708 .write = cgroup_file_write,
1709 .llseek = generic_file_llseek,
1710 .open = cgroup_file_open,
1711 .release = cgroup_file_release,
1712};
1713
1714static struct inode_operations cgroup_dir_inode_operations = {
1715 .lookup = simple_lookup,
1716 .mkdir = cgroup_mkdir,
1717 .rmdir = cgroup_rmdir,
1718 .rename = cgroup_rename,
1719};
1720
Li Zefan099fca32009-04-02 16:57:29 -07001721static int cgroup_create_file(struct dentry *dentry, mode_t mode,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001722 struct super_block *sb)
1723{
Al Viro3ba13d12009-02-20 06:02:22 +00001724 static const struct dentry_operations cgroup_dops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001725 .d_iput = cgroup_diput,
1726 };
1727
1728 struct inode *inode;
1729
1730 if (!dentry)
1731 return -ENOENT;
1732 if (dentry->d_inode)
1733 return -EEXIST;
1734
1735 inode = cgroup_new_inode(mode, sb);
1736 if (!inode)
1737 return -ENOMEM;
1738
1739 if (S_ISDIR(mode)) {
1740 inode->i_op = &cgroup_dir_inode_operations;
1741 inode->i_fop = &simple_dir_operations;
1742
1743 /* start off with i_nlink == 2 (for "." entry) */
1744 inc_nlink(inode);
1745
1746 /* start with the directory inode held, so that we can
1747 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07001748 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749 } else if (S_ISREG(mode)) {
1750 inode->i_size = 0;
1751 inode->i_fop = &cgroup_file_operations;
1752 }
1753 dentry->d_op = &cgroup_dops;
1754 d_instantiate(dentry, inode);
1755 dget(dentry); /* Extra count - pin the dentry in core */
1756 return 0;
1757}
1758
1759/*
Li Zefana043e3b2008-02-23 15:24:09 -08001760 * cgroup_create_dir - create a directory for an object.
1761 * @cgrp: the cgroup we create the directory for. It must have a valid
1762 * ->parent field. And we are going to fill its ->dentry field.
1763 * @dentry: dentry of the new cgroup
1764 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001766static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07001767 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001768{
1769 struct dentry *parent;
1770 int error = 0;
1771
Paul Menagebd89aab2007-10-18 23:40:44 -07001772 parent = cgrp->parent->dentry;
1773 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001775 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08001777 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778 dget(dentry);
1779 }
1780 dput(dentry);
1781
1782 return error;
1783}
1784
Li Zefan099fca32009-04-02 16:57:29 -07001785/**
1786 * cgroup_file_mode - deduce file mode of a control file
1787 * @cft: the control file in question
1788 *
1789 * returns cft->mode if ->mode is not 0
1790 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1791 * returns S_IRUGO if it has only a read handler
1792 * returns S_IWUSR if it has only a write hander
1793 */
1794static mode_t cgroup_file_mode(const struct cftype *cft)
1795{
1796 mode_t mode = 0;
1797
1798 if (cft->mode)
1799 return cft->mode;
1800
1801 if (cft->read || cft->read_u64 || cft->read_s64 ||
1802 cft->read_map || cft->read_seq_string)
1803 mode |= S_IRUGO;
1804
1805 if (cft->write || cft->write_u64 || cft->write_s64 ||
1806 cft->write_string || cft->trigger)
1807 mode |= S_IWUSR;
1808
1809 return mode;
1810}
1811
Paul Menagebd89aab2007-10-18 23:40:44 -07001812int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813 struct cgroup_subsys *subsys,
1814 const struct cftype *cft)
1815{
Paul Menagebd89aab2007-10-18 23:40:44 -07001816 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001817 struct dentry *dentry;
1818 int error;
Li Zefan099fca32009-04-02 16:57:29 -07001819 mode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820
1821 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07001822 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001823 strcpy(name, subsys->name);
1824 strcat(name, ".");
1825 }
1826 strcat(name, cft->name);
1827 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1828 dentry = lookup_one_len(name, dir, strlen(name));
1829 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07001830 mode = cgroup_file_mode(cft);
1831 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07001832 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001833 if (!error)
1834 dentry->d_fsdata = (void *)cft;
1835 dput(dentry);
1836 } else
1837 error = PTR_ERR(dentry);
1838 return error;
1839}
1840
Paul Menagebd89aab2007-10-18 23:40:44 -07001841int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001842 struct cgroup_subsys *subsys,
1843 const struct cftype cft[],
1844 int count)
1845{
1846 int i, err;
1847 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001848 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001849 if (err)
1850 return err;
1851 }
1852 return 0;
1853}
1854
Li Zefana043e3b2008-02-23 15:24:09 -08001855/**
1856 * cgroup_task_count - count the number of tasks in a cgroup.
1857 * @cgrp: the cgroup in question
1858 *
1859 * Return the number of tasks in the cgroup.
1860 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001861int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001862{
1863 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001864 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001865
Paul Menage817929e2007-10-18 23:39:36 -07001866 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001867 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07001868 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07001869 }
1870 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001871 return count;
1872}
1873
1874/*
Paul Menage817929e2007-10-18 23:39:36 -07001875 * Advance a list_head iterator. The iterator should be positioned at
1876 * the start of a css_set
1877 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001878static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001879 struct cgroup_iter *it)
1880{
1881 struct list_head *l = it->cg_link;
1882 struct cg_cgroup_link *link;
1883 struct css_set *cg;
1884
1885 /* Advance to the next non-empty css_set */
1886 do {
1887 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07001888 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001889 it->cg_link = NULL;
1890 return;
1891 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001892 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001893 cg = link->cg;
1894 } while (list_empty(&cg->tasks));
1895 it->cg_link = l;
1896 it->task = cg->tasks.next;
1897}
1898
Cliff Wickman31a7df02008-02-07 00:14:42 -08001899/*
1900 * To reduce the fork() overhead for systems that are not actually
1901 * using their cgroups capability, we don't maintain the lists running
1902 * through each css_set to its tasks until we see the list actually
1903 * used - in other words after the first call to cgroup_iter_start().
1904 *
1905 * The tasklist_lock is not held here, as do_each_thread() and
1906 * while_each_thread() are protected by RCU.
1907 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07001908static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08001909{
1910 struct task_struct *p, *g;
1911 write_lock(&css_set_lock);
1912 use_task_css_set_links = 1;
1913 do_each_thread(g, p) {
1914 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08001915 /*
1916 * We should check if the process is exiting, otherwise
1917 * it will race with cgroup_exit() in that the list
1918 * entry won't be deleted though the process has exited.
1919 */
1920 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08001921 list_add(&p->cg_list, &p->cgroups->tasks);
1922 task_unlock(p);
1923 } while_each_thread(g, p);
1924 write_unlock(&css_set_lock);
1925}
1926
Paul Menagebd89aab2007-10-18 23:40:44 -07001927void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001928{
1929 /*
1930 * The first time anyone tries to iterate across a cgroup,
1931 * we need to enable the list linking each css_set to its
1932 * tasks, and fix up all existing tasks.
1933 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08001934 if (!use_task_css_set_links)
1935 cgroup_enable_task_cg_lists();
1936
Paul Menage817929e2007-10-18 23:39:36 -07001937 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001938 it->cg_link = &cgrp->css_sets;
1939 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001940}
1941
Paul Menagebd89aab2007-10-18 23:40:44 -07001942struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001943 struct cgroup_iter *it)
1944{
1945 struct task_struct *res;
1946 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001947 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07001948
1949 /* If the iterator cg is NULL, we have no tasks */
1950 if (!it->cg_link)
1951 return NULL;
1952 res = list_entry(l, struct task_struct, cg_list);
1953 /* Advance iterator to find next entry */
1954 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001955 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
1956 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07001957 /* We reached the end of this task list - move on to
1958 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07001959 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001960 } else {
1961 it->task = l;
1962 }
1963 return res;
1964}
1965
Paul Menagebd89aab2007-10-18 23:40:44 -07001966void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001967{
1968 read_unlock(&css_set_lock);
1969}
1970
Cliff Wickman31a7df02008-02-07 00:14:42 -08001971static inline int started_after_time(struct task_struct *t1,
1972 struct timespec *time,
1973 struct task_struct *t2)
1974{
1975 int start_diff = timespec_compare(&t1->start_time, time);
1976 if (start_diff > 0) {
1977 return 1;
1978 } else if (start_diff < 0) {
1979 return 0;
1980 } else {
1981 /*
1982 * Arbitrarily, if two processes started at the same
1983 * time, we'll say that the lower pointer value
1984 * started first. Note that t2 may have exited by now
1985 * so this may not be a valid pointer any longer, but
1986 * that's fine - it still serves to distinguish
1987 * between two tasks started (effectively) simultaneously.
1988 */
1989 return t1 > t2;
1990 }
1991}
1992
1993/*
1994 * This function is a callback from heap_insert() and is used to order
1995 * the heap.
1996 * In this case we order the heap in descending task start time.
1997 */
1998static inline int started_after(void *p1, void *p2)
1999{
2000 struct task_struct *t1 = p1;
2001 struct task_struct *t2 = p2;
2002 return started_after_time(t1, &t2->start_time, t2);
2003}
2004
2005/**
2006 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2007 * @scan: struct cgroup_scanner containing arguments for the scan
2008 *
2009 * Arguments include pointers to callback functions test_task() and
2010 * process_task().
2011 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2012 * and if it returns true, call process_task() for it also.
2013 * The test_task pointer may be NULL, meaning always true (select all tasks).
2014 * Effectively duplicates cgroup_iter_{start,next,end}()
2015 * but does not lock css_set_lock for the call to process_task().
2016 * The struct cgroup_scanner may be embedded in any structure of the caller's
2017 * creation.
2018 * It is guaranteed that process_task() will act on every task that
2019 * is a member of the cgroup for the duration of this call. This
2020 * function may or may not call process_task() for tasks that exit
2021 * or move to a different cgroup during the call, or are forked or
2022 * move into the cgroup during the call.
2023 *
2024 * Note that test_task() may be called with locks held, and may in some
2025 * situations be called multiple times for the same task, so it should
2026 * be cheap.
2027 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2028 * pre-allocated and will be used for heap operations (and its "gt" member will
2029 * be overwritten), else a temporary heap will be used (allocation of which
2030 * may cause this function to fail).
2031 */
2032int cgroup_scan_tasks(struct cgroup_scanner *scan)
2033{
2034 int retval, i;
2035 struct cgroup_iter it;
2036 struct task_struct *p, *dropped;
2037 /* Never dereference latest_task, since it's not refcounted */
2038 struct task_struct *latest_task = NULL;
2039 struct ptr_heap tmp_heap;
2040 struct ptr_heap *heap;
2041 struct timespec latest_time = { 0, 0 };
2042
2043 if (scan->heap) {
2044 /* The caller supplied our heap and pre-allocated its memory */
2045 heap = scan->heap;
2046 heap->gt = &started_after;
2047 } else {
2048 /* We need to allocate our own heap memory */
2049 heap = &tmp_heap;
2050 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2051 if (retval)
2052 /* cannot allocate the heap */
2053 return retval;
2054 }
2055
2056 again:
2057 /*
2058 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2059 * to determine which are of interest, and using the scanner's
2060 * "process_task" callback to process any of them that need an update.
2061 * Since we don't want to hold any locks during the task updates,
2062 * gather tasks to be processed in a heap structure.
2063 * The heap is sorted by descending task start time.
2064 * If the statically-sized heap fills up, we overflow tasks that
2065 * started later, and in future iterations only consider tasks that
2066 * started after the latest task in the previous pass. This
2067 * guarantees forward progress and that we don't miss any tasks.
2068 */
2069 heap->size = 0;
2070 cgroup_iter_start(scan->cg, &it);
2071 while ((p = cgroup_iter_next(scan->cg, &it))) {
2072 /*
2073 * Only affect tasks that qualify per the caller's callback,
2074 * if he provided one
2075 */
2076 if (scan->test_task && !scan->test_task(p, scan))
2077 continue;
2078 /*
2079 * Only process tasks that started after the last task
2080 * we processed
2081 */
2082 if (!started_after_time(p, &latest_time, latest_task))
2083 continue;
2084 dropped = heap_insert(heap, p);
2085 if (dropped == NULL) {
2086 /*
2087 * The new task was inserted; the heap wasn't
2088 * previously full
2089 */
2090 get_task_struct(p);
2091 } else if (dropped != p) {
2092 /*
2093 * The new task was inserted, and pushed out a
2094 * different task
2095 */
2096 get_task_struct(p);
2097 put_task_struct(dropped);
2098 }
2099 /*
2100 * Else the new task was newer than anything already in
2101 * the heap and wasn't inserted
2102 */
2103 }
2104 cgroup_iter_end(scan->cg, &it);
2105
2106 if (heap->size) {
2107 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002108 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002109 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002110 latest_time = q->start_time;
2111 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002112 }
2113 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002114 scan->process_task(q, scan);
2115 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002116 }
2117 /*
2118 * If we had to process any tasks at all, scan again
2119 * in case some of them were in the middle of forking
2120 * children that didn't get processed.
2121 * Not the most efficient way to do it, but it avoids
2122 * having to take callback_mutex in the fork path
2123 */
2124 goto again;
2125 }
2126 if (heap == &tmp_heap)
2127 heap_free(&tmp_heap);
2128 return 0;
2129}
2130
Paul Menage817929e2007-10-18 23:39:36 -07002131/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07002132 * Stuff for reading the 'tasks' file.
2133 *
2134 * Reading this file can return large amounts of data if a cgroup has
2135 * *lots* of attached tasks. So it may need several calls to read(),
2136 * but we cannot guarantee that the information we produce is correct
2137 * unless we produce it entirely atomically.
2138 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002139 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002140
2141/*
2142 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
Paul Menagebd89aab2007-10-18 23:40:44 -07002143 * 'cgrp'. Return actual number of pids loaded. No need to
Paul Menagebbcb81d2007-10-18 23:39:32 -07002144 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2145 * read section, so the css_set can't go away, and is
2146 * immutable after creation.
2147 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002148static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002149{
Gowrishankar Me7b80bb2009-01-07 18:07:43 -08002150 int n = 0, pid;
Paul Menage817929e2007-10-18 23:39:36 -07002151 struct cgroup_iter it;
2152 struct task_struct *tsk;
Paul Menagebd89aab2007-10-18 23:40:44 -07002153 cgroup_iter_start(cgrp, &it);
2154 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Paul Menage817929e2007-10-18 23:39:36 -07002155 if (unlikely(n == npids))
2156 break;
Gowrishankar Me7b80bb2009-01-07 18:07:43 -08002157 pid = task_pid_vnr(tsk);
2158 if (pid > 0)
2159 pidarray[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07002160 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002161 cgroup_iter_end(cgrp, &it);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002162 return n;
2163}
2164
Balbir Singh846c7bb2007-10-18 23:39:44 -07002165/**
Li Zefana043e3b2008-02-23 15:24:09 -08002166 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002167 * @stats: cgroupstats to fill information into
2168 * @dentry: A dentry entry belonging to the cgroup for which stats have
2169 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002170 *
2171 * Build and fill cgroupstats so that taskstats can export it to user
2172 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002173 */
2174int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2175{
2176 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002177 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002178 struct cgroup_iter it;
2179 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002180
Balbir Singh846c7bb2007-10-18 23:39:44 -07002181 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002182 * Validate dentry by checking the superblock operations,
2183 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002184 */
Li Zefan33d283b2008-11-19 15:36:48 -08002185 if (dentry->d_sb->s_op != &cgroup_ops ||
2186 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002187 goto err;
2188
2189 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002190 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002191
Paul Menagebd89aab2007-10-18 23:40:44 -07002192 cgroup_iter_start(cgrp, &it);
2193 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002194 switch (tsk->state) {
2195 case TASK_RUNNING:
2196 stats->nr_running++;
2197 break;
2198 case TASK_INTERRUPTIBLE:
2199 stats->nr_sleeping++;
2200 break;
2201 case TASK_UNINTERRUPTIBLE:
2202 stats->nr_uninterruptible++;
2203 break;
2204 case TASK_STOPPED:
2205 stats->nr_stopped++;
2206 break;
2207 default:
2208 if (delayacct_is_task_waiting_on_io(tsk))
2209 stats->nr_io_wait++;
2210 break;
2211 }
2212 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002213 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002214
Balbir Singh846c7bb2007-10-18 23:39:44 -07002215err:
2216 return ret;
2217}
2218
Li Zefan096b7fe2009-07-29 15:04:04 -07002219/*
2220 * Cache pids for all threads in the same pid namespace that are
2221 * opening the same "tasks" file.
2222 */
2223struct cgroup_pids {
2224 /* The node in cgrp->pids_list */
2225 struct list_head list;
2226 /* The cgroup those pids belong to */
2227 struct cgroup *cgrp;
2228 /* The namepsace those pids belong to */
2229 struct pid_namespace *ns;
2230 /* Array of process ids in the cgroup */
2231 pid_t *tasks_pids;
2232 /* How many files are using the this tasks_pids array */
2233 int use_count;
2234 /* Length of the current tasks_pids array */
2235 int length;
2236};
2237
Paul Menagebbcb81d2007-10-18 23:39:32 -07002238static int cmppid(const void *a, const void *b)
2239{
2240 return *(pid_t *)a - *(pid_t *)b;
2241}
2242
Paul Menagecc31edc2008-10-18 20:28:04 -07002243/*
2244 * seq_file methods for the "tasks" file. The seq_file position is the
2245 * next pid to display; the seq_file iterator is a pointer to the pid
2246 * in the cgroup->tasks_pids array.
2247 */
2248
2249static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
2250{
2251 /*
2252 * Initially we receive a position value that corresponds to
2253 * one more than the last pid shown (or 0 on the first call or
2254 * after a seek to the start). Use a binary-search to find the
2255 * next pid to display, if any
2256 */
Li Zefan096b7fe2009-07-29 15:04:04 -07002257 struct cgroup_pids *cp = s->private;
2258 struct cgroup *cgrp = cp->cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07002259 int index = 0, pid = *pos;
2260 int *iter;
2261
2262 down_read(&cgrp->pids_mutex);
2263 if (pid) {
Li Zefan096b7fe2009-07-29 15:04:04 -07002264 int end = cp->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002265
Paul Menagecc31edc2008-10-18 20:28:04 -07002266 while (index < end) {
2267 int mid = (index + end) / 2;
Li Zefan096b7fe2009-07-29 15:04:04 -07002268 if (cp->tasks_pids[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07002269 index = mid;
2270 break;
Li Zefan096b7fe2009-07-29 15:04:04 -07002271 } else if (cp->tasks_pids[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07002272 index = mid + 1;
2273 else
2274 end = mid;
2275 }
2276 }
2277 /* If we're off the end of the array, we're done */
Li Zefan096b7fe2009-07-29 15:04:04 -07002278 if (index >= cp->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07002279 return NULL;
2280 /* Update the abstract position to be the actual pid that we found */
Li Zefan096b7fe2009-07-29 15:04:04 -07002281 iter = cp->tasks_pids + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07002282 *pos = *iter;
2283 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002284}
2285
Paul Menagecc31edc2008-10-18 20:28:04 -07002286static void cgroup_tasks_stop(struct seq_file *s, void *v)
2287{
Li Zefan096b7fe2009-07-29 15:04:04 -07002288 struct cgroup_pids *cp = s->private;
2289 struct cgroup *cgrp = cp->cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07002290 up_read(&cgrp->pids_mutex);
2291}
2292
2293static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
2294{
Li Zefan096b7fe2009-07-29 15:04:04 -07002295 struct cgroup_pids *cp = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07002296 int *p = v;
Li Zefan096b7fe2009-07-29 15:04:04 -07002297 int *end = cp->tasks_pids + cp->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07002298
2299 /*
2300 * Advance to the next pid in the array. If this goes off the
2301 * end, we're done
2302 */
2303 p++;
2304 if (p >= end) {
2305 return NULL;
2306 } else {
2307 *pos = *p;
2308 return p;
2309 }
2310}
2311
2312static int cgroup_tasks_show(struct seq_file *s, void *v)
2313{
2314 return seq_printf(s, "%d\n", *(int *)v);
2315}
2316
2317static struct seq_operations cgroup_tasks_seq_operations = {
2318 .start = cgroup_tasks_start,
2319 .stop = cgroup_tasks_stop,
2320 .next = cgroup_tasks_next,
2321 .show = cgroup_tasks_show,
2322};
2323
Li Zefan096b7fe2009-07-29 15:04:04 -07002324static void release_cgroup_pid_array(struct cgroup_pids *cp)
Paul Menagecc31edc2008-10-18 20:28:04 -07002325{
Li Zefan096b7fe2009-07-29 15:04:04 -07002326 struct cgroup *cgrp = cp->cgrp;
2327
Paul Menagecc31edc2008-10-18 20:28:04 -07002328 down_write(&cgrp->pids_mutex);
Li Zefan096b7fe2009-07-29 15:04:04 -07002329 BUG_ON(!cp->use_count);
2330 if (!--cp->use_count) {
2331 list_del(&cp->list);
2332 put_pid_ns(cp->ns);
2333 kfree(cp->tasks_pids);
2334 kfree(cp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002335 }
2336 up_write(&cgrp->pids_mutex);
2337}
2338
2339static int cgroup_tasks_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002340{
Li Zefan096b7fe2009-07-29 15:04:04 -07002341 struct seq_file *seq;
2342 struct cgroup_pids *cp;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002343
2344 if (!(file->f_mode & FMODE_READ))
2345 return 0;
2346
Li Zefan096b7fe2009-07-29 15:04:04 -07002347 seq = file->private_data;
2348 cp = seq->private;
2349
2350 release_cgroup_pid_array(cp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002351 return seq_release(inode, file);
2352}
2353
2354static struct file_operations cgroup_tasks_operations = {
2355 .read = seq_read,
2356 .llseek = seq_lseek,
2357 .write = cgroup_file_write,
2358 .release = cgroup_tasks_release,
2359};
2360
2361/*
2362 * Handle an open on 'tasks' file. Prepare an array containing the
2363 * process id's of tasks currently attached to the cgroup being opened.
2364 */
2365
2366static int cgroup_tasks_open(struct inode *unused, struct file *file)
2367{
2368 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Li Zefan096b7fe2009-07-29 15:04:04 -07002369 struct pid_namespace *ns = current->nsproxy->pid_ns;
2370 struct cgroup_pids *cp;
Paul Menagecc31edc2008-10-18 20:28:04 -07002371 pid_t *pidarray;
2372 int npids;
2373 int retval;
2374
2375 /* Nothing to do for write-only files */
2376 if (!(file->f_mode & FMODE_READ))
2377 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002378
2379 /*
2380 * If cgroup gets more users after we read count, we won't have
2381 * enough space - tough. This race is indistinguishable to the
2382 * caller from the case that the additional cgroup users didn't
2383 * show up until sometime later on.
2384 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002385 npids = cgroup_task_count(cgrp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002386 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2387 if (!pidarray)
2388 return -ENOMEM;
2389 npids = pid_array_load(pidarray, npids, cgrp);
2390 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002391
Paul Menagecc31edc2008-10-18 20:28:04 -07002392 /*
2393 * Store the array in the cgroup, freeing the old
2394 * array if necessary
2395 */
2396 down_write(&cgrp->pids_mutex);
Li Zefan096b7fe2009-07-29 15:04:04 -07002397
2398 list_for_each_entry(cp, &cgrp->pids_list, list) {
2399 if (ns == cp->ns)
2400 goto found;
2401 }
2402
2403 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2404 if (!cp) {
2405 up_write(&cgrp->pids_mutex);
2406 kfree(pidarray);
2407 return -ENOMEM;
2408 }
2409 cp->cgrp = cgrp;
2410 cp->ns = ns;
2411 get_pid_ns(ns);
2412 list_add(&cp->list, &cgrp->pids_list);
2413found:
2414 kfree(cp->tasks_pids);
2415 cp->tasks_pids = pidarray;
2416 cp->length = npids;
2417 cp->use_count++;
Paul Menagecc31edc2008-10-18 20:28:04 -07002418 up_write(&cgrp->pids_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002419
Paul Menagecc31edc2008-10-18 20:28:04 -07002420 file->f_op = &cgroup_tasks_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002421
Paul Menagecc31edc2008-10-18 20:28:04 -07002422 retval = seq_open(file, &cgroup_tasks_seq_operations);
2423 if (retval) {
Li Zefan096b7fe2009-07-29 15:04:04 -07002424 release_cgroup_pid_array(cp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002425 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002426 }
Li Zefan096b7fe2009-07-29 15:04:04 -07002427 ((struct seq_file *)file->private_data)->private = cp;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002428 return 0;
2429}
2430
Paul Menagebd89aab2007-10-18 23:40:44 -07002431static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002432 struct cftype *cft)
2433{
Paul Menagebd89aab2007-10-18 23:40:44 -07002434 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002435}
2436
Paul Menage6379c102008-07-25 01:47:01 -07002437static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2438 struct cftype *cft,
2439 u64 val)
2440{
2441 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2442 if (val)
2443 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2444 else
2445 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2446 return 0;
2447}
2448
Paul Menagebbcb81d2007-10-18 23:39:32 -07002449/*
2450 * for the common functions, 'private' gives the type of file
2451 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07002452static struct cftype files[] = {
2453 {
2454 .name = "tasks",
2455 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07002456 .write_u64 = cgroup_tasks_write,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002457 .release = cgroup_tasks_release,
2458 .private = FILE_TASKLIST,
Li Zefan099fca32009-04-02 16:57:29 -07002459 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002460 },
2461
2462 {
2463 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002464 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07002465 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002466 .private = FILE_NOTIFY_ON_RELEASE,
2467 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002468};
2469
2470static struct cftype cft_release_agent = {
2471 .name = "release_agent",
Paul Menagee788e062008-07-25 01:46:59 -07002472 .read_seq_string = cgroup_release_agent_show,
2473 .write_string = cgroup_release_agent_write,
2474 .max_write_len = PATH_MAX,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002475 .private = FILE_RELEASE_AGENT,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002476};
2477
Paul Menagebd89aab2007-10-18 23:40:44 -07002478static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002479{
2480 int err;
2481 struct cgroup_subsys *ss;
2482
2483 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002484 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002485
Paul Menagebd89aab2007-10-18 23:40:44 -07002486 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002487 if (err < 0)
2488 return err;
2489
Paul Menagebd89aab2007-10-18 23:40:44 -07002490 if (cgrp == cgrp->top_cgroup) {
2491 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002492 return err;
2493 }
2494
Paul Menagebd89aab2007-10-18 23:40:44 -07002495 for_each_subsys(cgrp->root, ss) {
2496 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002497 return err;
2498 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002499 /* This cgroup is ready now */
2500 for_each_subsys(cgrp->root, ss) {
2501 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2502 /*
2503 * Update id->css pointer and make this css visible from
2504 * CSS ID functions. This pointer will be dereferened
2505 * from RCU-read-side without locks.
2506 */
2507 if (css->id)
2508 rcu_assign_pointer(css->id->css, css);
2509 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002510
2511 return 0;
2512}
2513
2514static void init_cgroup_css(struct cgroup_subsys_state *css,
2515 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002516 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002517{
Paul Menagebd89aab2007-10-18 23:40:44 -07002518 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08002519 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002520 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002521 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002522 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002523 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002524 BUG_ON(cgrp->subsys[ss->subsys_id]);
2525 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002526}
2527
Paul Menage999cd8a2009-01-07 18:08:36 -08002528static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2529{
2530 /* We need to take each hierarchy_mutex in a consistent order */
2531 int i;
2532
2533 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2534 struct cgroup_subsys *ss = subsys[i];
2535 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08002536 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08002537 }
2538}
2539
2540static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2541{
2542 int i;
2543
2544 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2545 struct cgroup_subsys *ss = subsys[i];
2546 if (ss->root == root)
2547 mutex_unlock(&ss->hierarchy_mutex);
2548 }
2549}
2550
Paul Menageddbcc7e2007-10-18 23:39:30 -07002551/*
Li Zefana043e3b2008-02-23 15:24:09 -08002552 * cgroup_create - create a cgroup
2553 * @parent: cgroup that will be parent of the new cgroup
2554 * @dentry: dentry of the new cgroup
2555 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002556 *
Li Zefana043e3b2008-02-23 15:24:09 -08002557 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002558 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002559static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002560 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002561{
Paul Menagebd89aab2007-10-18 23:40:44 -07002562 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002563 struct cgroupfs_root *root = parent->root;
2564 int err = 0;
2565 struct cgroup_subsys *ss;
2566 struct super_block *sb = root->sb;
2567
Paul Menagebd89aab2007-10-18 23:40:44 -07002568 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2569 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002570 return -ENOMEM;
2571
2572 /* Grab a reference on the superblock so the hierarchy doesn't
2573 * get deleted on unmount if there are child cgroups. This
2574 * can be done outside cgroup_mutex, since the sb can't
2575 * disappear while someone has an open control file on the
2576 * fs */
2577 atomic_inc(&sb->s_active);
2578
2579 mutex_lock(&cgroup_mutex);
2580
Paul Menagecc31edc2008-10-18 20:28:04 -07002581 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002582
Paul Menagebd89aab2007-10-18 23:40:44 -07002583 cgrp->parent = parent;
2584 cgrp->root = parent->root;
2585 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002586
Li Zefanb6abdb02008-03-04 14:28:19 -08002587 if (notify_on_release(parent))
2588 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2589
Paul Menageddbcc7e2007-10-18 23:39:30 -07002590 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002591 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002592 if (IS_ERR(css)) {
2593 err = PTR_ERR(css);
2594 goto err_destroy;
2595 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002596 init_cgroup_css(css, ss, cgrp);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002597 if (ss->use_id)
2598 if (alloc_css_id(ss, parent, cgrp))
2599 goto err_destroy;
2600 /* At error, ->destroy() callback has to free assigned ID. */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002601 }
2602
Paul Menage999cd8a2009-01-07 18:08:36 -08002603 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002604 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08002605 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002606 root->number_of_cgroups++;
2607
Paul Menagebd89aab2007-10-18 23:40:44 -07002608 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002609 if (err < 0)
2610 goto err_remove;
2611
2612 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07002613 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002614
Paul Menagebd89aab2007-10-18 23:40:44 -07002615 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002616 /* If err < 0, we have a half-filled directory - oh well ;) */
2617
2618 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002619 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002620
2621 return 0;
2622
2623 err_remove:
2624
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002625 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002626 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002627 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002628 root->number_of_cgroups--;
2629
2630 err_destroy:
2631
2632 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002633 if (cgrp->subsys[ss->subsys_id])
2634 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002635 }
2636
2637 mutex_unlock(&cgroup_mutex);
2638
2639 /* Release the reference count that we took on the superblock */
2640 deactivate_super(sb);
2641
Paul Menagebd89aab2007-10-18 23:40:44 -07002642 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002643 return err;
2644}
2645
2646static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2647{
2648 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2649
2650 /* the vfs holds inode->i_mutex already */
2651 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2652}
2653
Li Zefan55b6fd02008-07-29 22:33:20 -07002654static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002655{
2656 /* Check the reference count on each subsystem. Since we
2657 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08002658 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07002659 * be no outstanding references, so the subsystem is safe to
2660 * destroy. We scan across all subsystems rather than using
2661 * the per-hierarchy linked list of mounted subsystems since
2662 * we can be called via check_for_release() with no
2663 * synchronization other than RCU, and the subsystem linked
2664 * list isn't RCU-safe */
2665 int i;
2666 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2667 struct cgroup_subsys *ss = subsys[i];
2668 struct cgroup_subsys_state *css;
2669 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07002670 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002671 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07002672 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07002673 /* When called from check_for_release() it's possible
2674 * that by this point the cgroup has been removed
2675 * and the css deleted. But a false-positive doesn't
2676 * matter, since it can only happen if the cgroup
2677 * has been deleted and hence no longer needs the
2678 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08002679 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07002680 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07002681 }
2682 return 0;
2683}
2684
Paul Menagee7c5ec92009-01-07 18:08:38 -08002685/*
2686 * Atomically mark all (or else none) of the cgroup's CSS objects as
2687 * CSS_REMOVED. Return true on success, or false if the cgroup has
2688 * busy subsystems. Call with cgroup_mutex held
2689 */
2690
2691static int cgroup_clear_css_refs(struct cgroup *cgrp)
2692{
2693 struct cgroup_subsys *ss;
2694 unsigned long flags;
2695 bool failed = false;
2696 local_irq_save(flags);
2697 for_each_subsys(cgrp->root, ss) {
2698 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2699 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08002700 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08002701 /* We can only remove a CSS with a refcnt==1 */
2702 refcnt = atomic_read(&css->refcnt);
2703 if (refcnt > 1) {
2704 failed = true;
2705 goto done;
2706 }
2707 BUG_ON(!refcnt);
2708 /*
2709 * Drop the refcnt to 0 while we check other
2710 * subsystems. This will cause any racing
2711 * css_tryget() to spin until we set the
2712 * CSS_REMOVED bits or abort
2713 */
Paul Menage804b3c22009-01-29 14:25:21 -08002714 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
2715 break;
2716 cpu_relax();
2717 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08002718 }
2719 done:
2720 for_each_subsys(cgrp->root, ss) {
2721 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2722 if (failed) {
2723 /*
2724 * Restore old refcnt if we previously managed
2725 * to clear it from 1 to 0
2726 */
2727 if (!atomic_read(&css->refcnt))
2728 atomic_set(&css->refcnt, 1);
2729 } else {
2730 /* Commit the fact that the CSS is removed */
2731 set_bit(CSS_REMOVED, &css->flags);
2732 }
2733 }
2734 local_irq_restore(flags);
2735 return !failed;
2736}
2737
Paul Menageddbcc7e2007-10-18 23:39:30 -07002738static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2739{
Paul Menagebd89aab2007-10-18 23:40:44 -07002740 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002741 struct dentry *d;
2742 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002743 DEFINE_WAIT(wait);
2744 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002745
2746 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002747again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07002748 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002749 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002750 mutex_unlock(&cgroup_mutex);
2751 return -EBUSY;
2752 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002753 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002754 mutex_unlock(&cgroup_mutex);
2755 return -EBUSY;
2756 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002757 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08002758
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002759 /*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002760 * In general, subsystem has no css->refcnt after pre_destroy(). But
2761 * in racy cases, subsystem may have to get css->refcnt after
2762 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
2763 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
2764 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
2765 * and subsystem's reference count handling. Please see css_get/put
2766 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
2767 */
2768 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2769
2770 /*
Li Zefana043e3b2008-02-23 15:24:09 -08002771 * Call pre_destroy handlers of subsys. Notify subsystems
2772 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002773 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002774 ret = cgroup_call_pre_destroy(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002775 if (ret) {
2776 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002777 return ret;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002778 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002779
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002780 mutex_lock(&cgroup_mutex);
2781 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002782 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002783 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002784 mutex_unlock(&cgroup_mutex);
2785 return -EBUSY;
2786 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002787 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002788 if (!cgroup_clear_css_refs(cgrp)) {
2789 mutex_unlock(&cgroup_mutex);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002790 /*
2791 * Because someone may call cgroup_wakeup_rmdir_waiter() before
2792 * prepare_to_wait(), we need to check this flag.
2793 */
2794 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
2795 schedule();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002796 finish_wait(&cgroup_rmdir_waitq, &wait);
2797 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2798 if (signal_pending(current))
2799 return -EINTR;
2800 goto again;
2801 }
2802 /* NO css_tryget() can success after here. */
2803 finish_wait(&cgroup_rmdir_waitq, &wait);
2804 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002805
Paul Menage81a6a5c2007-10-18 23:39:38 -07002806 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002807 set_bit(CGRP_REMOVED, &cgrp->flags);
2808 if (!list_empty(&cgrp->release_list))
2809 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002810 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08002811
2812 cgroup_lock_hierarchy(cgrp->root);
2813 /* delete this cgroup from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07002814 list_del(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08002815 cgroup_unlock_hierarchy(cgrp->root);
2816
Paul Menagebd89aab2007-10-18 23:40:44 -07002817 spin_lock(&cgrp->dentry->d_lock);
2818 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002819 spin_unlock(&d->d_lock);
2820
2821 cgroup_d_remove_dir(d);
2822 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002823
Paul Menagebd89aab2007-10-18 23:40:44 -07002824 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002825 check_for_release(parent);
2826
Paul Menageddbcc7e2007-10-18 23:39:30 -07002827 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002828 return 0;
2829}
2830
Li Zefan06a11922008-04-29 01:00:07 -07002831static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002832{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002833 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08002834
2835 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002836
2837 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08002838 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002839 ss->root = &rootnode;
2840 css = ss->create(ss, dummytop);
2841 /* We don't handle early failures gracefully */
2842 BUG_ON(IS_ERR(css));
2843 init_cgroup_css(css, ss, dummytop);
2844
Li Zefane8d55fd2008-04-29 01:00:13 -07002845 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07002846 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07002847 * newly registered, all tasks and hence the
2848 * init_css_set is in the subsystem's top cgroup. */
2849 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07002850
2851 need_forkexit_callback |= ss->fork || ss->exit;
2852
Li Zefane8d55fd2008-04-29 01:00:13 -07002853 /* At system boot, before all subsystems have been
2854 * registered, no tasks have been forked, so we don't
2855 * need to invoke fork callbacks here. */
2856 BUG_ON(!list_empty(&init_task.tasks));
2857
Paul Menage999cd8a2009-01-07 18:08:36 -08002858 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08002859 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002860 ss->active = 1;
2861}
2862
2863/**
Li Zefana043e3b2008-02-23 15:24:09 -08002864 * cgroup_init_early - cgroup initialization at system boot
2865 *
2866 * Initialize cgroups at system boot, and initialize any
2867 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002868 */
2869int __init cgroup_init_early(void)
2870{
2871 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002872 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07002873 INIT_LIST_HEAD(&init_css_set.cg_links);
2874 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07002875 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07002876 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002877 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07002878 root_count = 1;
2879 init_task.cgroups = &init_css_set;
2880
2881 init_css_set_link.cg = &init_css_set;
Paul Menagebd89aab2007-10-18 23:40:44 -07002882 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07002883 &rootnode.top_cgroup.css_sets);
2884 list_add(&init_css_set_link.cg_link_list,
2885 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002886
Li Zefan472b1052008-04-29 01:00:11 -07002887 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2888 INIT_HLIST_HEAD(&css_set_table[i]);
2889
Paul Menageddbcc7e2007-10-18 23:39:30 -07002890 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2891 struct cgroup_subsys *ss = subsys[i];
2892
2893 BUG_ON(!ss->name);
2894 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2895 BUG_ON(!ss->create);
2896 BUG_ON(!ss->destroy);
2897 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08002898 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07002899 ss->name, ss->subsys_id);
2900 BUG();
2901 }
2902
2903 if (ss->early_init)
2904 cgroup_init_subsys(ss);
2905 }
2906 return 0;
2907}
2908
2909/**
Li Zefana043e3b2008-02-23 15:24:09 -08002910 * cgroup_init - cgroup initialization
2911 *
2912 * Register cgroup filesystem and /proc file, and initialize
2913 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002914 */
2915int __init cgroup_init(void)
2916{
2917 int err;
2918 int i;
Li Zefan472b1052008-04-29 01:00:11 -07002919 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07002920
2921 err = bdi_init(&cgroup_backing_dev_info);
2922 if (err)
2923 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002924
2925 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2926 struct cgroup_subsys *ss = subsys[i];
2927 if (!ss->early_init)
2928 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002929 if (ss->use_id)
2930 cgroup_subsys_init_idr(ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002931 }
2932
Li Zefan472b1052008-04-29 01:00:11 -07002933 /* Add init_css_set to the hash table */
2934 hhead = css_set_hash(init_css_set.subsys);
2935 hlist_add_head(&init_css_set.hlist, hhead);
2936
Paul Menageddbcc7e2007-10-18 23:39:30 -07002937 err = register_filesystem(&cgroup_fs_type);
2938 if (err < 0)
2939 goto out;
2940
Li Zefan46ae2202008-04-29 01:00:08 -07002941 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07002942
Paul Menageddbcc7e2007-10-18 23:39:30 -07002943out:
Paul Menagea4243162007-10-18 23:39:35 -07002944 if (err)
2945 bdi_destroy(&cgroup_backing_dev_info);
2946
Paul Menageddbcc7e2007-10-18 23:39:30 -07002947 return err;
2948}
Paul Menageb4f48b62007-10-18 23:39:33 -07002949
Paul Menagea4243162007-10-18 23:39:35 -07002950/*
2951 * proc_cgroup_show()
2952 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2953 * - Used for /proc/<pid>/cgroup.
2954 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2955 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002956 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07002957 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2958 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2959 * cgroup to top_cgroup.
2960 */
2961
2962/* TODO: Use a proper seq_file iterator */
2963static int proc_cgroup_show(struct seq_file *m, void *v)
2964{
2965 struct pid *pid;
2966 struct task_struct *tsk;
2967 char *buf;
2968 int retval;
2969 struct cgroupfs_root *root;
2970
2971 retval = -ENOMEM;
2972 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2973 if (!buf)
2974 goto out;
2975
2976 retval = -ESRCH;
2977 pid = m->private;
2978 tsk = get_pid_task(pid, PIDTYPE_PID);
2979 if (!tsk)
2980 goto out_free;
2981
2982 retval = 0;
2983
2984 mutex_lock(&cgroup_mutex);
2985
Li Zefane5f6a862009-01-07 18:07:41 -08002986 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07002987 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07002988 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07002989 int subsys_id;
2990 int count = 0;
2991
Paul Menageb6c30062008-04-10 21:29:16 -07002992 seq_printf(m, "%lu:", root->subsys_bits);
Paul Menagea4243162007-10-18 23:39:35 -07002993 for_each_subsys(root, ss)
2994 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2995 seq_putc(m, ':');
2996 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07002997 cgrp = task_cgroup(tsk, subsys_id);
2998 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07002999 if (retval < 0)
3000 goto out_unlock;
3001 seq_puts(m, buf);
3002 seq_putc(m, '\n');
3003 }
3004
3005out_unlock:
3006 mutex_unlock(&cgroup_mutex);
3007 put_task_struct(tsk);
3008out_free:
3009 kfree(buf);
3010out:
3011 return retval;
3012}
3013
3014static int cgroup_open(struct inode *inode, struct file *file)
3015{
3016 struct pid *pid = PROC_I(inode)->pid;
3017 return single_open(file, proc_cgroup_show, pid);
3018}
3019
3020struct file_operations proc_cgroup_operations = {
3021 .open = cgroup_open,
3022 .read = seq_read,
3023 .llseek = seq_lseek,
3024 .release = single_release,
3025};
3026
3027/* Display information about each subsystem and each hierarchy */
3028static int proc_cgroupstats_show(struct seq_file *m, void *v)
3029{
3030 int i;
Paul Menagea4243162007-10-18 23:39:35 -07003031
Paul Menage8bab8dd2008-04-04 14:29:57 -07003032 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07003033 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07003034 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3035 struct cgroup_subsys *ss = subsys[i];
Paul Menage8bab8dd2008-04-04 14:29:57 -07003036 seq_printf(m, "%s\t%lu\t%d\t%d\n",
Paul Menage817929e2007-10-18 23:39:36 -07003037 ss->name, ss->root->subsys_bits,
Paul Menage8bab8dd2008-04-04 14:29:57 -07003038 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07003039 }
3040 mutex_unlock(&cgroup_mutex);
3041 return 0;
3042}
3043
3044static int cgroupstats_open(struct inode *inode, struct file *file)
3045{
Al Viro9dce07f2008-03-29 03:07:28 +00003046 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07003047}
3048
3049static struct file_operations proc_cgroupstats_operations = {
3050 .open = cgroupstats_open,
3051 .read = seq_read,
3052 .llseek = seq_lseek,
3053 .release = single_release,
3054};
3055
Paul Menageb4f48b62007-10-18 23:39:33 -07003056/**
3057 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08003058 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07003059 *
3060 * Description: A task inherits its parent's cgroup at fork().
3061 *
3062 * A pointer to the shared css_set was automatically copied in
3063 * fork.c by dup_task_struct(). However, we ignore that copy, since
3064 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08003065 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07003066 * have already changed current->cgroups, allowing the previously
3067 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07003068 *
3069 * At the point that cgroup_fork() is called, 'current' is the parent
3070 * task, and the passed argument 'child' points to the child task.
3071 */
3072void cgroup_fork(struct task_struct *child)
3073{
Paul Menage817929e2007-10-18 23:39:36 -07003074 task_lock(current);
3075 child->cgroups = current->cgroups;
3076 get_css_set(child->cgroups);
3077 task_unlock(current);
3078 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07003079}
3080
3081/**
Li Zefana043e3b2008-02-23 15:24:09 -08003082 * cgroup_fork_callbacks - run fork callbacks
3083 * @child: the new task
3084 *
3085 * Called on a new task very soon before adding it to the
3086 * tasklist. No need to take any locks since no-one can
3087 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003088 */
3089void cgroup_fork_callbacks(struct task_struct *child)
3090{
3091 if (need_forkexit_callback) {
3092 int i;
3093 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3094 struct cgroup_subsys *ss = subsys[i];
3095 if (ss->fork)
3096 ss->fork(ss, child);
3097 }
3098 }
3099}
3100
3101/**
Li Zefana043e3b2008-02-23 15:24:09 -08003102 * cgroup_post_fork - called on a new task after adding it to the task list
3103 * @child: the task in question
3104 *
3105 * Adds the task to the list running through its css_set if necessary.
3106 * Has to be after the task is visible on the task list in case we race
3107 * with the first call to cgroup_iter_start() - to guarantee that the
3108 * new task ends up on its list.
3109 */
Paul Menage817929e2007-10-18 23:39:36 -07003110void cgroup_post_fork(struct task_struct *child)
3111{
3112 if (use_task_css_set_links) {
3113 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003114 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003115 if (list_empty(&child->cg_list))
3116 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003117 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003118 write_unlock(&css_set_lock);
3119 }
3120}
3121/**
Paul Menageb4f48b62007-10-18 23:39:33 -07003122 * cgroup_exit - detach cgroup from exiting task
3123 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08003124 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07003125 *
3126 * Description: Detach cgroup from @tsk and release it.
3127 *
3128 * Note that cgroups marked notify_on_release force every task in
3129 * them to take the global cgroup_mutex mutex when exiting.
3130 * This could impact scaling on very large systems. Be reluctant to
3131 * use notify_on_release cgroups where very high task exit scaling
3132 * is required on large systems.
3133 *
3134 * the_top_cgroup_hack:
3135 *
3136 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3137 *
3138 * We call cgroup_exit() while the task is still competent to
3139 * handle notify_on_release(), then leave the task attached to the
3140 * root cgroup in each hierarchy for the remainder of its exit.
3141 *
3142 * To do this properly, we would increment the reference count on
3143 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3144 * code we would add a second cgroup function call, to drop that
3145 * reference. This would just create an unnecessary hot spot on
3146 * the top_cgroup reference count, to no avail.
3147 *
3148 * Normally, holding a reference to a cgroup without bumping its
3149 * count is unsafe. The cgroup could go away, or someone could
3150 * attach us to a different cgroup, decrementing the count on
3151 * the first cgroup that we never incremented. But in this case,
3152 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003153 * which wards off any cgroup_attach_task() attempts, or task is a failed
3154 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003155 */
3156void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3157{
3158 int i;
Paul Menage817929e2007-10-18 23:39:36 -07003159 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07003160
3161 if (run_callbacks && need_forkexit_callback) {
3162 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3163 struct cgroup_subsys *ss = subsys[i];
3164 if (ss->exit)
3165 ss->exit(ss, tsk);
3166 }
3167 }
Paul Menage817929e2007-10-18 23:39:36 -07003168
3169 /*
3170 * Unlink from the css_set task list if necessary.
3171 * Optimistically check cg_list before taking
3172 * css_set_lock
3173 */
3174 if (!list_empty(&tsk->cg_list)) {
3175 write_lock(&css_set_lock);
3176 if (!list_empty(&tsk->cg_list))
3177 list_del(&tsk->cg_list);
3178 write_unlock(&css_set_lock);
3179 }
3180
Paul Menageb4f48b62007-10-18 23:39:33 -07003181 /* Reassign the task to the init_css_set. */
3182 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003183 cg = tsk->cgroups;
3184 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07003185 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003186 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003187 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07003188}
Paul Menage697f4162007-10-18 23:39:34 -07003189
3190/**
Li Zefana043e3b2008-02-23 15:24:09 -08003191 * cgroup_clone - clone the cgroup the given subsystem is attached to
3192 * @tsk: the task to be moved
3193 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003194 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08003195 *
3196 * Duplicate the current cgroup in the hierarchy that the given
3197 * subsystem is attached to, and move this task into the new
3198 * child.
Paul Menage697f4162007-10-18 23:39:34 -07003199 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003200int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3201 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07003202{
3203 struct dentry *dentry;
3204 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07003205 struct cgroup *parent, *child;
3206 struct inode *inode;
3207 struct css_set *cg;
3208 struct cgroupfs_root *root;
3209 struct cgroup_subsys *ss;
3210
3211 /* We shouldn't be called by an unregistered subsystem */
3212 BUG_ON(!subsys->active);
3213
3214 /* First figure out what hierarchy and cgroup we're dealing
3215 * with, and pin them so we can drop cgroup_mutex */
3216 mutex_lock(&cgroup_mutex);
3217 again:
3218 root = subsys->root;
3219 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07003220 mutex_unlock(&cgroup_mutex);
3221 return 0;
3222 }
Paul Menage697f4162007-10-18 23:39:34 -07003223
Paul Menage697f4162007-10-18 23:39:34 -07003224 /* Pin the hierarchy */
Li Zefan1404f062009-01-29 14:25:21 -08003225 if (!atomic_inc_not_zero(&root->sb->s_active)) {
Li Zefan7b574b72009-01-04 12:00:45 -08003226 /* We race with the final deactivate_super() */
3227 mutex_unlock(&cgroup_mutex);
3228 return 0;
3229 }
Paul Menage697f4162007-10-18 23:39:34 -07003230
Paul Menage817929e2007-10-18 23:39:36 -07003231 /* Keep the cgroup alive */
Li Zefan1404f062009-01-29 14:25:21 -08003232 task_lock(tsk);
3233 parent = task_cgroup(tsk, subsys->subsys_id);
3234 cg = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07003235 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08003236 task_unlock(tsk);
Li Zefan1404f062009-01-29 14:25:21 -08003237
Paul Menage697f4162007-10-18 23:39:34 -07003238 mutex_unlock(&cgroup_mutex);
3239
3240 /* Now do the VFS work to create a cgroup */
3241 inode = parent->dentry->d_inode;
3242
3243 /* Hold the parent directory mutex across this operation to
3244 * stop anyone else deleting the new cgroup */
3245 mutex_lock(&inode->i_mutex);
3246 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3247 if (IS_ERR(dentry)) {
3248 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08003249 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07003250 PTR_ERR(dentry));
3251 ret = PTR_ERR(dentry);
3252 goto out_release;
3253 }
3254
3255 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08003256 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07003257 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07003258 dput(dentry);
3259 if (ret) {
3260 printk(KERN_INFO
3261 "Failed to create cgroup %s: %d\n", nodename,
3262 ret);
3263 goto out_release;
3264 }
3265
Paul Menage697f4162007-10-18 23:39:34 -07003266 /* The cgroup now exists. Retake cgroup_mutex and check
3267 * that we're still in the same state that we thought we
3268 * were. */
3269 mutex_lock(&cgroup_mutex);
3270 if ((root != subsys->root) ||
3271 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3272 /* Aargh, we raced ... */
3273 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003274 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07003275
Li Zefan1404f062009-01-29 14:25:21 -08003276 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003277 /* The cgroup is still accessible in the VFS, but
3278 * we're not going to try to rmdir() it at this
3279 * point. */
3280 printk(KERN_INFO
3281 "Race in cgroup_clone() - leaking cgroup %s\n",
3282 nodename);
3283 goto again;
3284 }
3285
3286 /* do any required auto-setup */
3287 for_each_subsys(root, ss) {
3288 if (ss->post_clone)
3289 ss->post_clone(ss, child);
3290 }
3291
3292 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08003293 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07003294 mutex_unlock(&cgroup_mutex);
3295
3296 out_release:
3297 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003298
3299 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003300 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003301 mutex_unlock(&cgroup_mutex);
Li Zefan1404f062009-01-29 14:25:21 -08003302 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003303 return ret;
3304}
3305
Li Zefana043e3b2008-02-23 15:24:09 -08003306/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003307 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08003308 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003309 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08003310 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003311 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3312 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07003313 *
3314 * If we are sending in dummytop, then presumably we are creating
3315 * the top cgroup in the subsystem.
3316 *
3317 * Called only by the ns (nsproxy) cgroup.
3318 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003319int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07003320{
3321 int ret;
3322 struct cgroup *target;
3323 int subsys_id;
3324
Paul Menagebd89aab2007-10-18 23:40:44 -07003325 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07003326 return 1;
3327
Paul Menagebd89aab2007-10-18 23:40:44 -07003328 get_first_subsys(cgrp, NULL, &subsys_id);
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003329 target = task_cgroup(task, subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07003330 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3331 cgrp = cgrp->parent;
3332 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07003333 return ret;
3334}
Paul Menage81a6a5c2007-10-18 23:39:38 -07003335
Paul Menagebd89aab2007-10-18 23:40:44 -07003336static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003337{
3338 /* All of these checks rely on RCU to keep the cgroup
3339 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07003340 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3341 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003342 /* Control Group is currently removeable. If it's not
3343 * already queued for a userspace notification, queue
3344 * it now */
3345 int need_schedule_work = 0;
3346 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003347 if (!cgroup_is_removed(cgrp) &&
3348 list_empty(&cgrp->release_list)) {
3349 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003350 need_schedule_work = 1;
3351 }
3352 spin_unlock(&release_list_lock);
3353 if (need_schedule_work)
3354 schedule_work(&release_agent_work);
3355 }
3356}
3357
3358void __css_put(struct cgroup_subsys_state *css)
3359{
Paul Menagebd89aab2007-10-18 23:40:44 -07003360 struct cgroup *cgrp = css->cgroup;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003361 rcu_read_lock();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003362 if (atomic_dec_return(&css->refcnt) == 1) {
3363 if (notify_on_release(cgrp)) {
3364 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3365 check_for_release(cgrp);
3366 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003367 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003368 }
3369 rcu_read_unlock();
3370}
3371
3372/*
3373 * Notify userspace when a cgroup is released, by running the
3374 * configured release agent with the name of the cgroup (path
3375 * relative to the root of cgroup file system) as the argument.
3376 *
3377 * Most likely, this user command will try to rmdir this cgroup.
3378 *
3379 * This races with the possibility that some other task will be
3380 * attached to this cgroup before it is removed, or that some other
3381 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3382 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3383 * unused, and this cgroup will be reprieved from its death sentence,
3384 * to continue to serve a useful existence. Next time it's released,
3385 * we will get notified again, if it still has 'notify_on_release' set.
3386 *
3387 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3388 * means only wait until the task is successfully execve()'d. The
3389 * separate release agent task is forked by call_usermodehelper(),
3390 * then control in this thread returns here, without waiting for the
3391 * release agent task. We don't bother to wait because the caller of
3392 * this routine has no use for the exit status of the release agent
3393 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003394 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003395static void cgroup_release_agent(struct work_struct *work)
3396{
3397 BUG_ON(work != &release_agent_work);
3398 mutex_lock(&cgroup_mutex);
3399 spin_lock(&release_list_lock);
3400 while (!list_empty(&release_list)) {
3401 char *argv[3], *envp[3];
3402 int i;
Paul Menagee788e062008-07-25 01:46:59 -07003403 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003404 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003405 struct cgroup,
3406 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003407 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003408 spin_unlock(&release_list_lock);
3409 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07003410 if (!pathbuf)
3411 goto continue_free;
3412 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3413 goto continue_free;
3414 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3415 if (!agentbuf)
3416 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003417
3418 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07003419 argv[i++] = agentbuf;
3420 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003421 argv[i] = NULL;
3422
3423 i = 0;
3424 /* minimal command environment */
3425 envp[i++] = "HOME=/";
3426 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3427 envp[i] = NULL;
3428
3429 /* Drop the lock while we invoke the usermode helper,
3430 * since the exec could involve hitting disk and hence
3431 * be a slow process */
3432 mutex_unlock(&cgroup_mutex);
3433 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003434 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07003435 continue_free:
3436 kfree(pathbuf);
3437 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003438 spin_lock(&release_list_lock);
3439 }
3440 spin_unlock(&release_list_lock);
3441 mutex_unlock(&cgroup_mutex);
3442}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003443
3444static int __init cgroup_disable(char *str)
3445{
3446 int i;
3447 char *token;
3448
3449 while ((token = strsep(&str, ",")) != NULL) {
3450 if (!*token)
3451 continue;
3452
3453 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3454 struct cgroup_subsys *ss = subsys[i];
3455
3456 if (!strcmp(token, ss->name)) {
3457 ss->disabled = 1;
3458 printk(KERN_INFO "Disabling %s control group"
3459 " subsystem\n", ss->name);
3460 break;
3461 }
3462 }
3463 }
3464 return 1;
3465}
3466__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003467
3468/*
3469 * Functons for CSS ID.
3470 */
3471
3472/*
3473 *To get ID other than 0, this should be called when !cgroup_is_removed().
3474 */
3475unsigned short css_id(struct cgroup_subsys_state *css)
3476{
3477 struct css_id *cssid = rcu_dereference(css->id);
3478
3479 if (cssid)
3480 return cssid->id;
3481 return 0;
3482}
3483
3484unsigned short css_depth(struct cgroup_subsys_state *css)
3485{
3486 struct css_id *cssid = rcu_dereference(css->id);
3487
3488 if (cssid)
3489 return cssid->depth;
3490 return 0;
3491}
3492
3493bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07003494 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003495{
3496 struct css_id *child_id = rcu_dereference(child->id);
3497 struct css_id *root_id = rcu_dereference(root->id);
3498
3499 if (!child_id || !root_id || (child_id->depth < root_id->depth))
3500 return false;
3501 return child_id->stack[root_id->depth] == root_id->id;
3502}
3503
3504static void __free_css_id_cb(struct rcu_head *head)
3505{
3506 struct css_id *id;
3507
3508 id = container_of(head, struct css_id, rcu_head);
3509 kfree(id);
3510}
3511
3512void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
3513{
3514 struct css_id *id = css->id;
3515 /* When this is called before css_id initialization, id can be NULL */
3516 if (!id)
3517 return;
3518
3519 BUG_ON(!ss->use_id);
3520
3521 rcu_assign_pointer(id->css, NULL);
3522 rcu_assign_pointer(css->id, NULL);
3523 spin_lock(&ss->id_lock);
3524 idr_remove(&ss->idr, id->id);
3525 spin_unlock(&ss->id_lock);
3526 call_rcu(&id->rcu_head, __free_css_id_cb);
3527}
3528
3529/*
3530 * This is called by init or create(). Then, calls to this function are
3531 * always serialized (By cgroup_mutex() at create()).
3532 */
3533
3534static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
3535{
3536 struct css_id *newid;
3537 int myid, error, size;
3538
3539 BUG_ON(!ss->use_id);
3540
3541 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
3542 newid = kzalloc(size, GFP_KERNEL);
3543 if (!newid)
3544 return ERR_PTR(-ENOMEM);
3545 /* get id */
3546 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
3547 error = -ENOMEM;
3548 goto err_out;
3549 }
3550 spin_lock(&ss->id_lock);
3551 /* Don't use 0. allocates an ID of 1-65535 */
3552 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
3553 spin_unlock(&ss->id_lock);
3554
3555 /* Returns error when there are no free spaces for new ID.*/
3556 if (error) {
3557 error = -ENOSPC;
3558 goto err_out;
3559 }
3560 if (myid > CSS_ID_MAX)
3561 goto remove_idr;
3562
3563 newid->id = myid;
3564 newid->depth = depth;
3565 return newid;
3566remove_idr:
3567 error = -ENOSPC;
3568 spin_lock(&ss->id_lock);
3569 idr_remove(&ss->idr, myid);
3570 spin_unlock(&ss->id_lock);
3571err_out:
3572 kfree(newid);
3573 return ERR_PTR(error);
3574
3575}
3576
3577static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss)
3578{
3579 struct css_id *newid;
3580 struct cgroup_subsys_state *rootcss;
3581
3582 spin_lock_init(&ss->id_lock);
3583 idr_init(&ss->idr);
3584
3585 rootcss = init_css_set.subsys[ss->subsys_id];
3586 newid = get_new_cssid(ss, 0);
3587 if (IS_ERR(newid))
3588 return PTR_ERR(newid);
3589
3590 newid->stack[0] = newid->id;
3591 newid->css = rootcss;
3592 rootcss->id = newid;
3593 return 0;
3594}
3595
3596static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
3597 struct cgroup *child)
3598{
3599 int subsys_id, i, depth = 0;
3600 struct cgroup_subsys_state *parent_css, *child_css;
3601 struct css_id *child_id, *parent_id = NULL;
3602
3603 subsys_id = ss->subsys_id;
3604 parent_css = parent->subsys[subsys_id];
3605 child_css = child->subsys[subsys_id];
3606 depth = css_depth(parent_css) + 1;
3607 parent_id = parent_css->id;
3608
3609 child_id = get_new_cssid(ss, depth);
3610 if (IS_ERR(child_id))
3611 return PTR_ERR(child_id);
3612
3613 for (i = 0; i < depth; i++)
3614 child_id->stack[i] = parent_id->stack[i];
3615 child_id->stack[depth] = child_id->id;
3616 /*
3617 * child_id->css pointer will be set after this cgroup is available
3618 * see cgroup_populate_dir()
3619 */
3620 rcu_assign_pointer(child_css->id, child_id);
3621
3622 return 0;
3623}
3624
3625/**
3626 * css_lookup - lookup css by id
3627 * @ss: cgroup subsys to be looked into.
3628 * @id: the id
3629 *
3630 * Returns pointer to cgroup_subsys_state if there is valid one with id.
3631 * NULL if not. Should be called under rcu_read_lock()
3632 */
3633struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
3634{
3635 struct css_id *cssid = NULL;
3636
3637 BUG_ON(!ss->use_id);
3638 cssid = idr_find(&ss->idr, id);
3639
3640 if (unlikely(!cssid))
3641 return NULL;
3642
3643 return rcu_dereference(cssid->css);
3644}
3645
3646/**
3647 * css_get_next - lookup next cgroup under specified hierarchy.
3648 * @ss: pointer to subsystem
3649 * @id: current position of iteration.
3650 * @root: pointer to css. search tree under this.
3651 * @foundid: position of found object.
3652 *
3653 * Search next css under the specified hierarchy of rootid. Calling under
3654 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
3655 */
3656struct cgroup_subsys_state *
3657css_get_next(struct cgroup_subsys *ss, int id,
3658 struct cgroup_subsys_state *root, int *foundid)
3659{
3660 struct cgroup_subsys_state *ret = NULL;
3661 struct css_id *tmp;
3662 int tmpid;
3663 int rootid = css_id(root);
3664 int depth = css_depth(root);
3665
3666 if (!rootid)
3667 return NULL;
3668
3669 BUG_ON(!ss->use_id);
3670 /* fill start point for scan */
3671 tmpid = id;
3672 while (1) {
3673 /*
3674 * scan next entry from bitmap(tree), tmpid is updated after
3675 * idr_get_next().
3676 */
3677 spin_lock(&ss->id_lock);
3678 tmp = idr_get_next(&ss->idr, &tmpid);
3679 spin_unlock(&ss->id_lock);
3680
3681 if (!tmp)
3682 break;
3683 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
3684 ret = rcu_dereference(tmp->css);
3685 if (ret) {
3686 *foundid = tmpid;
3687 break;
3688 }
3689 }
3690 /* continue to scan from next id */
3691 tmpid = tmpid + 1;
3692 }
3693 return ret;
3694}
3695