blob: b893c8c94858bb203fe2fda7d2f44e9e8b4a5ca4 [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>
Balbir Singh846c7bb2007-10-18 23:39:44 -070048
Paul Menageddbcc7e2007-10-18 23:39:30 -070049#include <asm/atomic.h>
50
Paul Menage81a6a5c2007-10-18 23:39:38 -070051static DEFINE_MUTEX(cgroup_mutex);
52
Paul Menageddbcc7e2007-10-18 23:39:30 -070053/* Generate an array of cgroup subsystem pointers */
54#define SUBSYS(_x) &_x ## _subsys,
55
56static struct cgroup_subsys *subsys[] = {
57#include <linux/cgroup_subsys.h>
58};
59
60/*
61 * A cgroupfs_root represents the root of a cgroup hierarchy,
62 * and may be associated with a superblock to form an active
63 * hierarchy
64 */
65struct cgroupfs_root {
66 struct super_block *sb;
67
68 /*
69 * The bitmask of subsystems intended to be attached to this
70 * hierarchy
71 */
72 unsigned long subsys_bits;
73
74 /* The bitmask of subsystems currently attached to this hierarchy */
75 unsigned long actual_subsys_bits;
76
77 /* A list running through the attached subsystems */
78 struct list_head subsys_list;
79
80 /* The root cgroup for this hierarchy */
81 struct cgroup top_cgroup;
82
83 /* Tracks how many cgroups are currently defined in hierarchy.*/
84 int number_of_cgroups;
85
86 /* A list running through the mounted hierarchies */
87 struct list_head root_list;
88
89 /* Hierarchy-specific flags */
90 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -070091
92 /* The path to use for release notifications. No locking
93 * between setting and use - so if userspace updates this
94 * while child cgroups exist, you could miss a
95 * notification. We ensure that it's always a valid
96 * NUL-terminated string */
97 char release_agent_path[PATH_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -070098};
99
100
101/*
102 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
103 * subsystems that are otherwise unattached - it never has more than a
104 * single cgroup, and all tasks are part of that cgroup.
105 */
106static struct cgroupfs_root rootnode;
107
108/* The list of hierarchy roots */
109
110static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700111static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112
113/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
114#define dummytop (&rootnode.top_cgroup)
115
116/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800117 * check for fork/exit handlers to call. This avoids us having to do
118 * extra work in the fork/exit path if none of the subsystems need to
119 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120 */
121static int need_forkexit_callback;
122
Paul Menageddbcc7e2007-10-18 23:39:30 -0700123/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700124inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700125{
Paul Menagebd89aab2007-10-18 23:40:44 -0700126 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700127}
128
129/* bits in struct cgroupfs_root flags field */
130enum {
131 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
132};
133
Adrian Bunke9685a02008-02-07 00:13:46 -0800134static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700135{
136 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700137 (1 << CGRP_RELEASABLE) |
138 (1 << CGRP_NOTIFY_ON_RELEASE);
139 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700140}
141
Adrian Bunke9685a02008-02-07 00:13:46 -0800142static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700143{
Paul Menagebd89aab2007-10-18 23:40:44 -0700144 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700145}
146
Paul Menageddbcc7e2007-10-18 23:39:30 -0700147/*
148 * for_each_subsys() allows you to iterate on each subsystem attached to
149 * an active hierarchy
150 */
151#define for_each_subsys(_root, _ss) \
152list_for_each_entry(_ss, &_root->subsys_list, sibling)
153
154/* for_each_root() allows you to iterate across the active hierarchies */
155#define for_each_root(_root) \
156list_for_each_entry(_root, &roots, root_list)
157
Paul Menage81a6a5c2007-10-18 23:39:38 -0700158/* the list of cgroups eligible for automatic release. Protected by
159 * release_list_lock */
160static LIST_HEAD(release_list);
161static DEFINE_SPINLOCK(release_list_lock);
162static void cgroup_release_agent(struct work_struct *work);
163static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700164static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700165
Paul Menage817929e2007-10-18 23:39:36 -0700166/* Link structure for associating css_set objects with cgroups */
167struct cg_cgroup_link {
168 /*
169 * List running through cg_cgroup_links associated with a
170 * cgroup, anchored on cgroup->css_sets
171 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700172 struct list_head cgrp_link_list;
Paul Menage817929e2007-10-18 23:39:36 -0700173 /*
174 * List running through cg_cgroup_links pointing at a
175 * single css_set object, anchored on css_set->cg_links
176 */
177 struct list_head cg_link_list;
178 struct css_set *cg;
179};
180
181/* The default css_set - used by init and its children prior to any
182 * hierarchies being mounted. It contains a pointer to the root state
183 * for each subsystem. Also used to anchor the list of css_sets. Not
184 * reference-counted, to improve performance when child cgroups
185 * haven't been created.
186 */
187
188static struct css_set init_css_set;
189static struct cg_cgroup_link init_css_set_link;
190
191/* css_set_lock protects the list of css_set objects, and the
192 * chain of tasks off each css_set. Nests outside task->alloc_lock
193 * due to cgroup_iter_start() */
194static DEFINE_RWLOCK(css_set_lock);
195static int css_set_count;
196
Li Zefan472b1052008-04-29 01:00:11 -0700197/* hash table for cgroup groups. This improves the performance to
198 * find an existing css_set */
199#define CSS_SET_HASH_BITS 7
200#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
201static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
202
203static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
204{
205 int i;
206 int index;
207 unsigned long tmp = 0UL;
208
209 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
210 tmp += (unsigned long)css[i];
211 tmp = (tmp >> 16) ^ tmp;
212
213 index = hash_long(tmp, CSS_SET_HASH_BITS);
214
215 return &css_set_table[index];
216}
217
Paul Menage817929e2007-10-18 23:39:36 -0700218/* We don't maintain the lists running through each css_set to its
219 * task until after the first call to cgroup_iter_start(). This
220 * reduces the fork()/exit() overhead for people who have cgroups
221 * compiled into their kernel but not actually in use */
222static int use_task_css_set_links;
223
224/* When we create or destroy a css_set, the operation simply
225 * takes/releases a reference count on all the cgroups referenced
226 * by subsystems in this css_set. This can end up multiple-counting
227 * some cgroups, but that's OK - the ref-count is just a
228 * busy/not-busy indicator; ensuring that we only count each cgroup
229 * once would require taking a global lock to ensure that no
Paul Menageb4f48b62007-10-18 23:39:33 -0700230 * subsystems moved between hierarchies while we were doing so.
231 *
232 * Possible TODO: decide at boot time based on the number of
233 * registered subsystems and the number of CPUs or NUMA nodes whether
234 * it's better for performance to ref-count every subsystem, or to
235 * take a global lock and only add one ref count to each hierarchy.
236 */
Paul Menageb4f48b62007-10-18 23:39:33 -0700237
Paul Menage817929e2007-10-18 23:39:36 -0700238/*
239 * unlink a css_set from the list and free it
240 */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700241static void unlink_css_set(struct css_set *cg)
Paul Menageb4f48b62007-10-18 23:39:33 -0700242{
Paul Menage817929e2007-10-18 23:39:36 -0700243 write_lock(&css_set_lock);
Li Zefan472b1052008-04-29 01:00:11 -0700244 hlist_del(&cg->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700245 list_del(&cg->list);
246 css_set_count--;
247 while (!list_empty(&cg->cg_links)) {
248 struct cg_cgroup_link *link;
249 link = list_entry(cg->cg_links.next,
250 struct cg_cgroup_link, cg_link_list);
251 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700252 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700253 kfree(link);
254 }
255 write_unlock(&css_set_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700256}
257
258static void __release_css_set(struct kref *k, int taskexit)
259{
260 int i;
261 struct css_set *cg = container_of(k, struct css_set, ref);
262
263 unlink_css_set(cg);
264
265 rcu_read_lock();
266 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700267 struct cgroup *cgrp = cg->subsys[i]->cgroup;
268 if (atomic_dec_and_test(&cgrp->count) &&
269 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700270 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700271 set_bit(CGRP_RELEASABLE, &cgrp->flags);
272 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700273 }
274 }
275 rcu_read_unlock();
Paul Menage817929e2007-10-18 23:39:36 -0700276 kfree(cg);
277}
278
Paul Menage81a6a5c2007-10-18 23:39:38 -0700279static void release_css_set(struct kref *k)
280{
281 __release_css_set(k, 0);
282}
283
284static void release_css_set_taskexit(struct kref *k)
285{
286 __release_css_set(k, 1);
287}
288
Paul Menage817929e2007-10-18 23:39:36 -0700289/*
290 * refcounted get/put for css_set objects
291 */
292static inline void get_css_set(struct css_set *cg)
293{
294 kref_get(&cg->ref);
295}
296
297static inline void put_css_set(struct css_set *cg)
298{
299 kref_put(&cg->ref, release_css_set);
300}
301
Paul Menage81a6a5c2007-10-18 23:39:38 -0700302static inline void put_css_set_taskexit(struct css_set *cg)
303{
304 kref_put(&cg->ref, release_css_set_taskexit);
305}
306
Paul Menage817929e2007-10-18 23:39:36 -0700307/*
308 * find_existing_css_set() is a helper for
309 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700310 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700311 *
312 * oldcg: the cgroup group that we're using before the cgroup
313 * transition
314 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700315 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700316 *
317 * template: location in which to build the desired set of subsystem
318 * state objects for the new cgroup group
319 */
Paul Menage817929e2007-10-18 23:39:36 -0700320static struct css_set *find_existing_css_set(
321 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700322 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700323 struct cgroup_subsys_state *template[])
324{
325 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700326 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700327 struct hlist_head *hhead;
328 struct hlist_node *node;
329 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700330
331 /* Built the set of subsystem state objects that we want to
332 * see in the new css_set */
333 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800334 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700335 /* Subsystem is in this hierarchy. So we want
336 * the subsystem state from the new
337 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700338 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700339 } else {
340 /* Subsystem is not in this hierarchy, so we
341 * don't want to change the subsystem state */
342 template[i] = oldcg->subsys[i];
343 }
344 }
345
Li Zefan472b1052008-04-29 01:00:11 -0700346 hhead = css_set_hash(template);
347 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage817929e2007-10-18 23:39:36 -0700348 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
349 /* All subsystems matched */
350 return cg;
351 }
Li Zefan472b1052008-04-29 01:00:11 -0700352 }
Paul Menage817929e2007-10-18 23:39:36 -0700353
354 /* No existing cgroup group matched */
355 return NULL;
356}
357
358/*
359 * allocate_cg_links() allocates "count" cg_cgroup_link structures
Paul Menagebd89aab2007-10-18 23:40:44 -0700360 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
Paul Menage817929e2007-10-18 23:39:36 -0700361 * success or a negative error
362 */
Paul Menage817929e2007-10-18 23:39:36 -0700363static int allocate_cg_links(int count, struct list_head *tmp)
364{
365 struct cg_cgroup_link *link;
366 int i;
367 INIT_LIST_HEAD(tmp);
368 for (i = 0; i < count; i++) {
369 link = kmalloc(sizeof(*link), GFP_KERNEL);
370 if (!link) {
371 while (!list_empty(tmp)) {
372 link = list_entry(tmp->next,
373 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700374 cgrp_link_list);
375 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700376 kfree(link);
377 }
378 return -ENOMEM;
379 }
Paul Menagebd89aab2007-10-18 23:40:44 -0700380 list_add(&link->cgrp_link_list, tmp);
Paul Menage817929e2007-10-18 23:39:36 -0700381 }
382 return 0;
383}
384
385static void free_cg_links(struct list_head *tmp)
386{
387 while (!list_empty(tmp)) {
388 struct cg_cgroup_link *link;
389 link = list_entry(tmp->next,
390 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700391 cgrp_link_list);
392 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700393 kfree(link);
394 }
395}
396
397/*
398 * find_css_set() takes an existing cgroup group and a
399 * cgroup object, and returns a css_set object that's
400 * equivalent to the old group, but with the given cgroup
401 * substituted into the appropriate hierarchy. Must be called with
402 * cgroup_mutex held
403 */
Paul Menage817929e2007-10-18 23:39:36 -0700404static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700405 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700406{
407 struct css_set *res;
408 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
409 int i;
410
411 struct list_head tmp_cg_links;
412 struct cg_cgroup_link *link;
413
Li Zefan472b1052008-04-29 01:00:11 -0700414 struct hlist_head *hhead;
415
Paul Menage817929e2007-10-18 23:39:36 -0700416 /* First see if we already have a cgroup group that matches
417 * the desired set */
418 write_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700419 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700420 if (res)
421 get_css_set(res);
422 write_unlock(&css_set_lock);
423
424 if (res)
425 return res;
426
427 res = kmalloc(sizeof(*res), GFP_KERNEL);
428 if (!res)
429 return NULL;
430
431 /* Allocate all the cg_cgroup_link objects that we'll need */
432 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
433 kfree(res);
434 return NULL;
435 }
436
437 kref_init(&res->ref);
438 INIT_LIST_HEAD(&res->cg_links);
439 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700440 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700441
442 /* Copy the set of subsystem state objects generated in
443 * find_existing_css_set() */
444 memcpy(res->subsys, template, sizeof(res->subsys));
445
446 write_lock(&css_set_lock);
447 /* Add reference counts and links from the new css_set. */
448 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700449 struct cgroup *cgrp = res->subsys[i]->cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700450 struct cgroup_subsys *ss = subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -0700451 atomic_inc(&cgrp->count);
Paul Menage817929e2007-10-18 23:39:36 -0700452 /*
453 * We want to add a link once per cgroup, so we
454 * only do it for the first subsystem in each
455 * hierarchy
456 */
457 if (ss->root->subsys_list.next == &ss->sibling) {
458 BUG_ON(list_empty(&tmp_cg_links));
459 link = list_entry(tmp_cg_links.next,
460 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700461 cgrp_link_list);
462 list_del(&link->cgrp_link_list);
463 list_add(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage817929e2007-10-18 23:39:36 -0700464 link->cg = res;
465 list_add(&link->cg_link_list, &res->cg_links);
466 }
467 }
468 if (list_empty(&rootnode.subsys_list)) {
469 link = list_entry(tmp_cg_links.next,
470 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700471 cgrp_link_list);
472 list_del(&link->cgrp_link_list);
473 list_add(&link->cgrp_link_list, &dummytop->css_sets);
Paul Menage817929e2007-10-18 23:39:36 -0700474 link->cg = res;
475 list_add(&link->cg_link_list, &res->cg_links);
476 }
477
478 BUG_ON(!list_empty(&tmp_cg_links));
479
480 /* Link this cgroup group into the list */
481 list_add(&res->list, &init_css_set.list);
482 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700483
484 /* Add this cgroup group to the hash table */
485 hhead = css_set_hash(res->subsys);
486 hlist_add_head(&res->hlist, hhead);
487
Paul Menage817929e2007-10-18 23:39:36 -0700488 write_unlock(&css_set_lock);
489
490 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700491}
492
Paul Menageddbcc7e2007-10-18 23:39:30 -0700493/*
494 * There is one global cgroup mutex. We also require taking
495 * task_lock() when dereferencing a task's cgroup subsys pointers.
496 * See "The task_lock() exception", at the end of this comment.
497 *
498 * A task must hold cgroup_mutex to modify cgroups.
499 *
500 * Any task can increment and decrement the count field without lock.
501 * So in general, code holding cgroup_mutex can't rely on the count
502 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800503 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700504 * means that no tasks are currently attached, therefore there is no
505 * way a task attached to that cgroup can fork (the other way to
506 * increment the count). So code holding cgroup_mutex can safely
507 * assume that if the count is zero, it will stay zero. Similarly, if
508 * a task holds cgroup_mutex on a cgroup with zero count, it
509 * knows that the cgroup won't be removed, as cgroup_rmdir()
510 * needs that mutex.
511 *
512 * The cgroup_common_file_write handler for operations that modify
513 * the cgroup hierarchy holds cgroup_mutex across the entire operation,
514 * single threading all such cgroup modifications across the system.
515 *
516 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
517 * (usually) take cgroup_mutex. These are the two most performance
518 * critical pieces of code here. The exception occurs on cgroup_exit(),
519 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
520 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800521 * to the release agent with the name of the cgroup (path relative to
522 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700523 *
524 * A cgroup can only be deleted if both its 'count' of using tasks
525 * is zero, and its list of 'children' cgroups is empty. Since all
526 * tasks in the system use _some_ cgroup, and since there is always at
527 * least one task in the system (init, pid == 1), therefore, top_cgroup
528 * always has either children cgroups and/or using tasks. So we don't
529 * need a special hack to ensure that top_cgroup cannot be deleted.
530 *
531 * The task_lock() exception
532 *
533 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800534 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800535 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700536 * several performance critical places that need to reference
537 * task->cgroup without the expense of grabbing a system global
538 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800539 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700540 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
541 * the task_struct routinely used for such matters.
542 *
543 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800544 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700545 */
546
Paul Menageddbcc7e2007-10-18 23:39:30 -0700547/**
548 * cgroup_lock - lock out any changes to cgroup structures
549 *
550 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700551void cgroup_lock(void)
552{
553 mutex_lock(&cgroup_mutex);
554}
555
556/**
557 * cgroup_unlock - release lock on cgroup changes
558 *
559 * Undo the lock taken in a previous cgroup_lock() call.
560 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700561void cgroup_unlock(void)
562{
563 mutex_unlock(&cgroup_mutex);
564}
565
566/*
567 * A couple of forward declarations required, due to cyclic reference loop:
568 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
569 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
570 * -> cgroup_mkdir.
571 */
572
573static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
574static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700575static int cgroup_populate_dir(struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700576static struct inode_operations cgroup_dir_inode_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700577static struct file_operations proc_cgroupstats_operations;
578
579static struct backing_dev_info cgroup_backing_dev_info = {
580 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
581};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700582
583static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
584{
585 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700586
587 if (inode) {
588 inode->i_mode = mode;
589 inode->i_uid = current->fsuid;
590 inode->i_gid = current->fsgid;
591 inode->i_blocks = 0;
592 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
593 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
594 }
595 return inode;
596}
597
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800598/*
599 * Call subsys's pre_destroy handler.
600 * This is called before css refcnt check.
601 */
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800602static void cgroup_call_pre_destroy(struct cgroup *cgrp)
603{
604 struct cgroup_subsys *ss;
605 for_each_subsys(cgrp->root, ss)
606 if (ss->pre_destroy && cgrp->subsys[ss->subsys_id])
607 ss->pre_destroy(ss, cgrp);
608 return;
609}
610
Paul Menageddbcc7e2007-10-18 23:39:30 -0700611static void cgroup_diput(struct dentry *dentry, struct inode *inode)
612{
613 /* is dentry a directory ? if so, kfree() associated cgroup */
614 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700615 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800616 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700617 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700618 /* It's possible for external users to be holding css
619 * reference counts on a cgroup; css_put() needs to
620 * be able to access the cgroup after decrementing
621 * the reference count in order to know if it needs to
622 * queue the cgroup to be handled by the release
623 * agent */
624 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800625
626 mutex_lock(&cgroup_mutex);
627 /*
628 * Release the subsystem state objects.
629 */
630 for_each_subsys(cgrp->root, ss) {
631 if (cgrp->subsys[ss->subsys_id])
632 ss->destroy(ss, cgrp);
633 }
634
635 cgrp->root->number_of_cgroups--;
636 mutex_unlock(&cgroup_mutex);
637
638 /* Drop the active superblock reference that we took when we
639 * created the cgroup */
640 deactivate_super(cgrp->root->sb);
641
Paul Menagebd89aab2007-10-18 23:40:44 -0700642 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700643 }
644 iput(inode);
645}
646
647static void remove_dir(struct dentry *d)
648{
649 struct dentry *parent = dget(d->d_parent);
650
651 d_delete(d);
652 simple_rmdir(parent->d_inode, d);
653 dput(parent);
654}
655
656static void cgroup_clear_directory(struct dentry *dentry)
657{
658 struct list_head *node;
659
660 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
661 spin_lock(&dcache_lock);
662 node = dentry->d_subdirs.next;
663 while (node != &dentry->d_subdirs) {
664 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
665 list_del_init(node);
666 if (d->d_inode) {
667 /* This should never be called on a cgroup
668 * directory with child cgroups */
669 BUG_ON(d->d_inode->i_mode & S_IFDIR);
670 d = dget_locked(d);
671 spin_unlock(&dcache_lock);
672 d_delete(d);
673 simple_unlink(dentry->d_inode, d);
674 dput(d);
675 spin_lock(&dcache_lock);
676 }
677 node = dentry->d_subdirs.next;
678 }
679 spin_unlock(&dcache_lock);
680}
681
682/*
683 * NOTE : the dentry must have been dget()'ed
684 */
685static void cgroup_d_remove_dir(struct dentry *dentry)
686{
687 cgroup_clear_directory(dentry);
688
689 spin_lock(&dcache_lock);
690 list_del_init(&dentry->d_u.d_child);
691 spin_unlock(&dcache_lock);
692 remove_dir(dentry);
693}
694
695static int rebind_subsystems(struct cgroupfs_root *root,
696 unsigned long final_bits)
697{
698 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700699 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700700 int i;
701
702 removed_bits = root->actual_subsys_bits & ~final_bits;
703 added_bits = final_bits & ~root->actual_subsys_bits;
704 /* Check that any added subsystems are currently free */
705 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800706 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700707 struct cgroup_subsys *ss = subsys[i];
708 if (!(bit & added_bits))
709 continue;
710 if (ss->root != &rootnode) {
711 /* Subsystem isn't free */
712 return -EBUSY;
713 }
714 }
715
716 /* Currently we don't handle adding/removing subsystems when
717 * any child cgroups exist. This is theoretically supportable
718 * but involves complex error handling, so it's being left until
719 * later */
Paul Menagebd89aab2007-10-18 23:40:44 -0700720 if (!list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700721 return -EBUSY;
722
723 /* Process each subsystem */
724 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
725 struct cgroup_subsys *ss = subsys[i];
726 unsigned long bit = 1UL << i;
727 if (bit & added_bits) {
728 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700729 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700730 BUG_ON(!dummytop->subsys[i]);
731 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -0700732 cgrp->subsys[i] = dummytop->subsys[i];
733 cgrp->subsys[i]->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700734 list_add(&ss->sibling, &root->subsys_list);
735 rcu_assign_pointer(ss->root, root);
736 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700737 ss->bind(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700738
739 } else if (bit & removed_bits) {
740 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700741 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
742 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700743 if (ss->bind)
744 ss->bind(ss, dummytop);
745 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700746 cgrp->subsys[i] = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700747 rcu_assign_pointer(subsys[i]->root, &rootnode);
748 list_del(&ss->sibling);
749 } else if (bit & final_bits) {
750 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700751 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700752 } else {
753 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700754 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700755 }
756 }
757 root->subsys_bits = root->actual_subsys_bits = final_bits;
758 synchronize_rcu();
759
760 return 0;
761}
762
763static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
764{
765 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
766 struct cgroup_subsys *ss;
767
768 mutex_lock(&cgroup_mutex);
769 for_each_subsys(root, ss)
770 seq_printf(seq, ",%s", ss->name);
771 if (test_bit(ROOT_NOPREFIX, &root->flags))
772 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700773 if (strlen(root->release_agent_path))
774 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700775 mutex_unlock(&cgroup_mutex);
776 return 0;
777}
778
779struct cgroup_sb_opts {
780 unsigned long subsys_bits;
781 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700782 char *release_agent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700783};
784
785/* Convert a hierarchy specifier into a bitmask of subsystems and
786 * flags. */
787static int parse_cgroupfs_options(char *data,
788 struct cgroup_sb_opts *opts)
789{
790 char *token, *o = data ?: "all";
791
792 opts->subsys_bits = 0;
793 opts->flags = 0;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700794 opts->release_agent = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700795
796 while ((token = strsep(&o, ",")) != NULL) {
797 if (!*token)
798 return -EINVAL;
799 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700800 /* Add all non-disabled subsystems */
801 int i;
802 opts->subsys_bits = 0;
803 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
804 struct cgroup_subsys *ss = subsys[i];
805 if (!ss->disabled)
806 opts->subsys_bits |= 1ul << i;
807 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808 } else if (!strcmp(token, "noprefix")) {
809 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700810 } else if (!strncmp(token, "release_agent=", 14)) {
811 /* Specifying two release agents is forbidden */
812 if (opts->release_agent)
813 return -EINVAL;
814 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
815 if (!opts->release_agent)
816 return -ENOMEM;
817 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
818 opts->release_agent[PATH_MAX - 1] = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700819 } else {
820 struct cgroup_subsys *ss;
821 int i;
822 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
823 ss = subsys[i];
824 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700825 if (!ss->disabled)
826 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700827 break;
828 }
829 }
830 if (i == CGROUP_SUBSYS_COUNT)
831 return -ENOENT;
832 }
833 }
834
835 /* We can't have an empty hierarchy */
836 if (!opts->subsys_bits)
837 return -EINVAL;
838
839 return 0;
840}
841
842static int cgroup_remount(struct super_block *sb, int *flags, char *data)
843{
844 int ret = 0;
845 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -0700846 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700847 struct cgroup_sb_opts opts;
848
Paul Menagebd89aab2007-10-18 23:40:44 -0700849 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700850 mutex_lock(&cgroup_mutex);
851
852 /* See what subsystems are wanted */
853 ret = parse_cgroupfs_options(data, &opts);
854 if (ret)
855 goto out_unlock;
856
857 /* Don't allow flags to change at remount */
858 if (opts.flags != root->flags) {
859 ret = -EINVAL;
860 goto out_unlock;
861 }
862
863 ret = rebind_subsystems(root, opts.subsys_bits);
864
865 /* (re)populate subsystem files */
866 if (!ret)
Paul Menagebd89aab2007-10-18 23:40:44 -0700867 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700868
Paul Menage81a6a5c2007-10-18 23:39:38 -0700869 if (opts.release_agent)
870 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700871 out_unlock:
Paul Menage81a6a5c2007-10-18 23:39:38 -0700872 if (opts.release_agent)
873 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700874 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700875 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700876 return ret;
877}
878
879static struct super_operations cgroup_ops = {
880 .statfs = simple_statfs,
881 .drop_inode = generic_delete_inode,
882 .show_options = cgroup_show_options,
883 .remount_fs = cgroup_remount,
884};
885
886static void init_cgroup_root(struct cgroupfs_root *root)
887{
Paul Menagebd89aab2007-10-18 23:40:44 -0700888 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700889 INIT_LIST_HEAD(&root->subsys_list);
890 INIT_LIST_HEAD(&root->root_list);
891 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -0700892 cgrp->root = root;
893 cgrp->top_cgroup = cgrp;
894 INIT_LIST_HEAD(&cgrp->sibling);
895 INIT_LIST_HEAD(&cgrp->children);
896 INIT_LIST_HEAD(&cgrp->css_sets);
897 INIT_LIST_HEAD(&cgrp->release_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700898}
899
900static int cgroup_test_super(struct super_block *sb, void *data)
901{
902 struct cgroupfs_root *new = data;
903 struct cgroupfs_root *root = sb->s_fs_info;
904
905 /* First check subsystems */
906 if (new->subsys_bits != root->subsys_bits)
907 return 0;
908
909 /* Next check flags */
910 if (new->flags != root->flags)
911 return 0;
912
913 return 1;
914}
915
916static int cgroup_set_super(struct super_block *sb, void *data)
917{
918 int ret;
919 struct cgroupfs_root *root = data;
920
921 ret = set_anon_super(sb, NULL);
922 if (ret)
923 return ret;
924
925 sb->s_fs_info = root;
926 root->sb = sb;
927
928 sb->s_blocksize = PAGE_CACHE_SIZE;
929 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
930 sb->s_magic = CGROUP_SUPER_MAGIC;
931 sb->s_op = &cgroup_ops;
932
933 return 0;
934}
935
936static int cgroup_get_rootdir(struct super_block *sb)
937{
938 struct inode *inode =
939 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
940 struct dentry *dentry;
941
942 if (!inode)
943 return -ENOMEM;
944
Paul Menageddbcc7e2007-10-18 23:39:30 -0700945 inode->i_fop = &simple_dir_operations;
946 inode->i_op = &cgroup_dir_inode_operations;
947 /* directories start off with i_nlink == 2 (for "." entry) */
948 inc_nlink(inode);
949 dentry = d_alloc_root(inode);
950 if (!dentry) {
951 iput(inode);
952 return -ENOMEM;
953 }
954 sb->s_root = dentry;
955 return 0;
956}
957
958static int cgroup_get_sb(struct file_system_type *fs_type,
959 int flags, const char *unused_dev_name,
960 void *data, struct vfsmount *mnt)
961{
962 struct cgroup_sb_opts opts;
963 int ret = 0;
964 struct super_block *sb;
965 struct cgroupfs_root *root;
Paul Menage817929e2007-10-18 23:39:36 -0700966 struct list_head tmp_cg_links, *l;
967 INIT_LIST_HEAD(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968
969 /* First find the desired set of subsystems */
970 ret = parse_cgroupfs_options(data, &opts);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700971 if (ret) {
972 if (opts.release_agent)
973 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700974 return ret;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700975 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976
977 root = kzalloc(sizeof(*root), GFP_KERNEL);
Li Zefanf7770732008-02-23 15:24:10 -0800978 if (!root) {
979 if (opts.release_agent)
980 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700981 return -ENOMEM;
Li Zefanf7770732008-02-23 15:24:10 -0800982 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983
984 init_cgroup_root(root);
985 root->subsys_bits = opts.subsys_bits;
986 root->flags = opts.flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700987 if (opts.release_agent) {
988 strcpy(root->release_agent_path, opts.release_agent);
989 kfree(opts.release_agent);
990 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991
992 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
993
994 if (IS_ERR(sb)) {
995 kfree(root);
996 return PTR_ERR(sb);
997 }
998
999 if (sb->s_fs_info != root) {
1000 /* Reusing an existing superblock */
1001 BUG_ON(sb->s_root == NULL);
1002 kfree(root);
1003 root = NULL;
1004 } else {
1005 /* New superblock */
Paul Menagebd89aab2007-10-18 23:40:44 -07001006 struct cgroup *cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001007 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008
1009 BUG_ON(sb->s_root != NULL);
1010
1011 ret = cgroup_get_rootdir(sb);
1012 if (ret)
1013 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001014 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015
Paul Menage817929e2007-10-18 23:39:36 -07001016 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 mutex_lock(&cgroup_mutex);
1018
Paul Menage817929e2007-10-18 23:39:36 -07001019 /*
1020 * We're accessing css_set_count without locking
1021 * css_set_lock here, but that's OK - it can only be
1022 * increased by someone holding cgroup_lock, and
1023 * that's us. The worst that can happen is that we
1024 * have some link structures left over
1025 */
1026 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1027 if (ret) {
1028 mutex_unlock(&cgroup_mutex);
1029 mutex_unlock(&inode->i_mutex);
1030 goto drop_new_super;
1031 }
1032
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 ret = rebind_subsystems(root, root->subsys_bits);
1034 if (ret == -EBUSY) {
1035 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001036 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001037 goto drop_new_super;
1038 }
1039
1040 /* EBUSY should be the only error here */
1041 BUG_ON(ret);
1042
1043 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001044 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045
1046 sb->s_root->d_fsdata = &root->top_cgroup;
1047 root->top_cgroup.dentry = sb->s_root;
1048
Paul Menage817929e2007-10-18 23:39:36 -07001049 /* Link the top cgroup in this hierarchy into all
1050 * the css_set objects */
1051 write_lock(&css_set_lock);
1052 l = &init_css_set.list;
1053 do {
1054 struct css_set *cg;
1055 struct cg_cgroup_link *link;
1056 cg = list_entry(l, struct css_set, list);
1057 BUG_ON(list_empty(&tmp_cg_links));
1058 link = list_entry(tmp_cg_links.next,
1059 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -07001060 cgrp_link_list);
1061 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001062 link->cg = cg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001063 list_add(&link->cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07001064 &root->top_cgroup.css_sets);
1065 list_add(&link->cg_link_list, &cg->cg_links);
1066 l = l->next;
1067 } while (l != &init_css_set.list);
1068 write_unlock(&css_set_lock);
1069
1070 free_cg_links(&tmp_cg_links);
1071
Paul Menagebd89aab2007-10-18 23:40:44 -07001072 BUG_ON(!list_empty(&cgrp->sibling));
1073 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074 BUG_ON(root->number_of_cgroups != 1);
1075
Paul Menagebd89aab2007-10-18 23:40:44 -07001076 cgroup_populate_dir(cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001077 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078 mutex_unlock(&cgroup_mutex);
1079 }
1080
1081 return simple_set_mnt(mnt, sb);
1082
1083 drop_new_super:
1084 up_write(&sb->s_umount);
1085 deactivate_super(sb);
Paul Menage817929e2007-10-18 23:39:36 -07001086 free_cg_links(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 return ret;
1088}
1089
1090static void cgroup_kill_sb(struct super_block *sb) {
1091 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001092 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093 int ret;
1094
1095 BUG_ON(!root);
1096
1097 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001098 BUG_ON(!list_empty(&cgrp->children));
1099 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100
1101 mutex_lock(&cgroup_mutex);
1102
1103 /* Rebind all subsystems back to the default hierarchy */
1104 ret = rebind_subsystems(root, 0);
1105 /* Shouldn't be able to fail ... */
1106 BUG_ON(ret);
1107
Paul Menage817929e2007-10-18 23:39:36 -07001108 /*
1109 * Release all the links from css_sets to this hierarchy's
1110 * root cgroup
1111 */
1112 write_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001113 while (!list_empty(&cgrp->css_sets)) {
Paul Menage817929e2007-10-18 23:39:36 -07001114 struct cg_cgroup_link *link;
Paul Menagebd89aab2007-10-18 23:40:44 -07001115 link = list_entry(cgrp->css_sets.next,
1116 struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001117 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001118 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001119 kfree(link);
1120 }
1121 write_unlock(&css_set_lock);
1122
1123 if (!list_empty(&root->root_list)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124 list_del(&root->root_list);
Paul Menage817929e2007-10-18 23:39:36 -07001125 root_count--;
1126 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127 mutex_unlock(&cgroup_mutex);
1128
1129 kfree(root);
1130 kill_litter_super(sb);
1131}
1132
1133static struct file_system_type cgroup_fs_type = {
1134 .name = "cgroup",
1135 .get_sb = cgroup_get_sb,
1136 .kill_sb = cgroup_kill_sb,
1137};
1138
Paul Menagebd89aab2007-10-18 23:40:44 -07001139static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001140{
1141 return dentry->d_fsdata;
1142}
1143
1144static inline struct cftype *__d_cft(struct dentry *dentry)
1145{
1146 return dentry->d_fsdata;
1147}
1148
Li Zefana043e3b2008-02-23 15:24:09 -08001149/**
1150 * cgroup_path - generate the path of a cgroup
1151 * @cgrp: the cgroup in question
1152 * @buf: the buffer to write the path into
1153 * @buflen: the length of the buffer
1154 *
1155 * Called with cgroup_mutex held. Writes path of cgroup into buf.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001156 * Returns 0 on success, -errno on error.
1157 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001158int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001159{
1160 char *start;
1161
Paul Menagebd89aab2007-10-18 23:40:44 -07001162 if (cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001163 /*
1164 * Inactive subsystems have no dentry for their root
1165 * cgroup
1166 */
1167 strcpy(buf, "/");
1168 return 0;
1169 }
1170
1171 start = buf + buflen;
1172
1173 *--start = '\0';
1174 for (;;) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001175 int len = cgrp->dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001176 if ((start -= len) < buf)
1177 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001178 memcpy(start, cgrp->dentry->d_name.name, len);
1179 cgrp = cgrp->parent;
1180 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001181 break;
Paul Menagebd89aab2007-10-18 23:40:44 -07001182 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001183 continue;
1184 if (--start < buf)
1185 return -ENAMETOOLONG;
1186 *start = '/';
1187 }
1188 memmove(buf, start, buf + buflen - start);
1189 return 0;
1190}
1191
Paul Menagebbcb81d2007-10-18 23:39:32 -07001192/*
1193 * Return the first subsystem attached to a cgroup's hierarchy, and
1194 * its subsystem id.
1195 */
1196
Paul Menagebd89aab2007-10-18 23:40:44 -07001197static void get_first_subsys(const struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001198 struct cgroup_subsys_state **css, int *subsys_id)
1199{
Paul Menagebd89aab2007-10-18 23:40:44 -07001200 const struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001201 const struct cgroup_subsys *test_ss;
1202 BUG_ON(list_empty(&root->subsys_list));
1203 test_ss = list_entry(root->subsys_list.next,
1204 struct cgroup_subsys, sibling);
1205 if (css) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001206 *css = cgrp->subsys[test_ss->subsys_id];
Paul Menagebbcb81d2007-10-18 23:39:32 -07001207 BUG_ON(!*css);
1208 }
1209 if (subsys_id)
1210 *subsys_id = test_ss->subsys_id;
1211}
1212
Li Zefana043e3b2008-02-23 15:24:09 -08001213/**
1214 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1215 * @cgrp: the cgroup the task is attaching to
1216 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001217 *
Li Zefana043e3b2008-02-23 15:24:09 -08001218 * Call holding cgroup_mutex. May take task_lock of
1219 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001220 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001221int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001222{
1223 int retval = 0;
1224 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07001225 struct cgroup *oldcgrp;
Paul Menage817929e2007-10-18 23:39:36 -07001226 struct css_set *cg = tsk->cgroups;
1227 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001228 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001229 int subsys_id;
1230
Paul Menagebd89aab2007-10-18 23:40:44 -07001231 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001232
1233 /* Nothing to do if the task is already in that cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -07001234 oldcgrp = task_cgroup(tsk, subsys_id);
1235 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001236 return 0;
1237
1238 for_each_subsys(root, ss) {
1239 if (ss->can_attach) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001240 retval = ss->can_attach(ss, cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001241 if (retval)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001242 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001243 }
1244 }
1245
Paul Menage817929e2007-10-18 23:39:36 -07001246 /*
1247 * Locate or allocate a new css_set for this task,
1248 * based on its final set of cgroups
1249 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001250 newcg = find_css_set(cg, cgrp);
Paul Jacksone18f6312008-02-07 00:13:44 -08001251 if (!newcg)
Paul Menage817929e2007-10-18 23:39:36 -07001252 return -ENOMEM;
Paul Menage817929e2007-10-18 23:39:36 -07001253
Paul Menagebbcb81d2007-10-18 23:39:32 -07001254 task_lock(tsk);
1255 if (tsk->flags & PF_EXITING) {
1256 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001257 put_css_set(newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001258 return -ESRCH;
1259 }
Paul Menage817929e2007-10-18 23:39:36 -07001260 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001261 task_unlock(tsk);
1262
Paul Menage817929e2007-10-18 23:39:36 -07001263 /* Update the css_set linked lists if we're using them */
1264 write_lock(&css_set_lock);
1265 if (!list_empty(&tsk->cg_list)) {
1266 list_del(&tsk->cg_list);
1267 list_add(&tsk->cg_list, &newcg->tasks);
1268 }
1269 write_unlock(&css_set_lock);
1270
Paul Menagebbcb81d2007-10-18 23:39:32 -07001271 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001272 if (ss->attach)
Paul Menagebd89aab2007-10-18 23:40:44 -07001273 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001274 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001275 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001276 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001277 put_css_set(cg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001278 return 0;
1279}
1280
1281/*
Paul Menagebd89aab2007-10-18 23:40:44 -07001282 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with
Paul Menagebbcb81d2007-10-18 23:39:32 -07001283 * cgroup_mutex, may take task_lock of task
1284 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001285static int attach_task_by_pid(struct cgroup *cgrp, char *pidbuf)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001286{
1287 pid_t pid;
1288 struct task_struct *tsk;
1289 int ret;
1290
1291 if (sscanf(pidbuf, "%d", &pid) != 1)
1292 return -EIO;
1293
1294 if (pid) {
1295 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001296 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001297 if (!tsk || tsk->flags & PF_EXITING) {
1298 rcu_read_unlock();
1299 return -ESRCH;
1300 }
1301 get_task_struct(tsk);
1302 rcu_read_unlock();
1303
1304 if ((current->euid) && (current->euid != tsk->uid)
1305 && (current->euid != tsk->suid)) {
1306 put_task_struct(tsk);
1307 return -EACCES;
1308 }
1309 } else {
1310 tsk = current;
1311 get_task_struct(tsk);
1312 }
1313
Cliff Wickman956db3c2008-02-07 00:14:43 -08001314 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001315 put_task_struct(tsk);
1316 return ret;
1317}
1318
Paul Menageddbcc7e2007-10-18 23:39:30 -07001319/* The various types of files and directories in a cgroup file system */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001320enum cgroup_filetype {
1321 FILE_ROOT,
1322 FILE_DIR,
1323 FILE_TASKLIST,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001324 FILE_NOTIFY_ON_RELEASE,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001325 FILE_RELEASE_AGENT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326};
1327
Paul Menagee73d2c62008-04-29 01:00:06 -07001328static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001329 struct file *file,
1330 const char __user *userbuf,
1331 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001332{
1333 char buffer[64];
1334 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001335 char *end;
1336
1337 if (!nbytes)
1338 return -EINVAL;
1339 if (nbytes >= sizeof(buffer))
1340 return -E2BIG;
1341 if (copy_from_user(buffer, userbuf, nbytes))
1342 return -EFAULT;
1343
1344 buffer[nbytes] = 0; /* nul-terminate */
Paul Menageb7269df2008-04-29 00:59:59 -07001345 strstrip(buffer);
Paul Menagee73d2c62008-04-29 01:00:06 -07001346 if (cft->write_u64) {
1347 u64 val = simple_strtoull(buffer, &end, 0);
1348 if (*end)
1349 return -EINVAL;
1350 retval = cft->write_u64(cgrp, cft, val);
1351 } else {
1352 s64 val = simple_strtoll(buffer, &end, 0);
1353 if (*end)
1354 return -EINVAL;
1355 retval = cft->write_s64(cgrp, cft, val);
1356 }
Paul Menage355e0c42007-10-18 23:39:33 -07001357 if (!retval)
1358 retval = nbytes;
1359 return retval;
1360}
1361
Paul Menagebd89aab2007-10-18 23:40:44 -07001362static ssize_t cgroup_common_file_write(struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001363 struct cftype *cft,
1364 struct file *file,
1365 const char __user *userbuf,
1366 size_t nbytes, loff_t *unused_ppos)
1367{
1368 enum cgroup_filetype type = cft->private;
1369 char *buffer;
1370 int retval = 0;
1371
1372 if (nbytes >= PATH_MAX)
1373 return -E2BIG;
1374
1375 /* +1 for nul-terminator */
1376 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1377 if (buffer == NULL)
1378 return -ENOMEM;
1379
1380 if (copy_from_user(buffer, userbuf, nbytes)) {
1381 retval = -EFAULT;
1382 goto out1;
1383 }
1384 buffer[nbytes] = 0; /* nul-terminate */
Paul Jackson622d42c2008-02-07 00:13:44 -08001385 strstrip(buffer); /* strip -just- trailing whitespace */
Paul Menagebbcb81d2007-10-18 23:39:32 -07001386
1387 mutex_lock(&cgroup_mutex);
1388
Paul Menage8dc4f3e2008-02-07 00:13:45 -08001389 /*
1390 * This was already checked for in cgroup_file_write(), but
1391 * check again now we're holding cgroup_mutex.
1392 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001393 if (cgroup_is_removed(cgrp)) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07001394 retval = -ENODEV;
1395 goto out2;
1396 }
1397
1398 switch (type) {
1399 case FILE_TASKLIST:
Paul Menagebd89aab2007-10-18 23:40:44 -07001400 retval = attach_task_by_pid(cgrp, buffer);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001401 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001402 case FILE_NOTIFY_ON_RELEASE:
Paul Menagebd89aab2007-10-18 23:40:44 -07001403 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001404 if (simple_strtoul(buffer, NULL, 10) != 0)
Paul Menagebd89aab2007-10-18 23:40:44 -07001405 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001406 else
Paul Menagebd89aab2007-10-18 23:40:44 -07001407 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001408 break;
1409 case FILE_RELEASE_AGENT:
Paul Jackson622d42c2008-02-07 00:13:44 -08001410 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1411 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001412 break;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001413 default:
1414 retval = -EINVAL;
1415 goto out2;
1416 }
1417
1418 if (retval == 0)
1419 retval = nbytes;
1420out2:
1421 mutex_unlock(&cgroup_mutex);
1422out1:
1423 kfree(buffer);
1424 return retval;
1425}
1426
Paul Menageddbcc7e2007-10-18 23:39:30 -07001427static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1428 size_t nbytes, loff_t *ppos)
1429{
1430 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001431 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001432
Paul Menage8dc4f3e2008-02-07 00:13:45 -08001433 if (!cft || cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001434 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001435 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001436 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001437 if (cft->write_u64 || cft->write_s64)
1438 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001439 if (cft->trigger) {
1440 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1441 return ret ? ret : nbytes;
1442 }
Paul Menage355e0c42007-10-18 23:39:33 -07001443 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001444}
1445
Paul Menagef4c753b2008-04-29 00:59:56 -07001446static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1447 struct file *file,
1448 char __user *buf, size_t nbytes,
1449 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001450{
1451 char tmp[64];
Paul Menagef4c753b2008-04-29 00:59:56 -07001452 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001453 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1454
1455 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1456}
1457
Paul Menagee73d2c62008-04-29 01:00:06 -07001458static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1459 struct file *file,
1460 char __user *buf, size_t nbytes,
1461 loff_t *ppos)
1462{
1463 char tmp[64];
1464 s64 val = cft->read_s64(cgrp, cft);
1465 int len = sprintf(tmp, "%lld\n", (long long) val);
1466
1467 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1468}
1469
Paul Menagebd89aab2007-10-18 23:40:44 -07001470static ssize_t cgroup_common_file_read(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001471 struct cftype *cft,
1472 struct file *file,
1473 char __user *buf,
1474 size_t nbytes, loff_t *ppos)
1475{
1476 enum cgroup_filetype type = cft->private;
1477 char *page;
1478 ssize_t retval = 0;
1479 char *s;
1480
1481 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
1482 return -ENOMEM;
1483
1484 s = page;
1485
1486 switch (type) {
1487 case FILE_RELEASE_AGENT:
1488 {
1489 struct cgroupfs_root *root;
1490 size_t n;
1491 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001492 root = cgrp->root;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001493 n = strnlen(root->release_agent_path,
1494 sizeof(root->release_agent_path));
1495 n = min(n, (size_t) PAGE_SIZE);
1496 strncpy(s, root->release_agent_path, n);
1497 mutex_unlock(&cgroup_mutex);
1498 s += n;
1499 break;
1500 }
1501 default:
1502 retval = -EINVAL;
1503 goto out;
1504 }
1505 *s++ = '\n';
1506
1507 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
1508out:
1509 free_page((unsigned long)page);
1510 return retval;
1511}
1512
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1514 size_t nbytes, loff_t *ppos)
1515{
1516 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001517 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001518
Paul Menage8dc4f3e2008-02-07 00:13:45 -08001519 if (!cft || cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520 return -ENODEV;
1521
1522 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001523 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001524 if (cft->read_u64)
1525 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001526 if (cft->read_s64)
1527 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528 return -EINVAL;
1529}
1530
Paul Menage91796562008-04-29 01:00:01 -07001531/*
1532 * seqfile ops/methods for returning structured data. Currently just
1533 * supports string->u64 maps, but can be extended in future.
1534 */
1535
1536struct cgroup_seqfile_state {
1537 struct cftype *cft;
1538 struct cgroup *cgroup;
1539};
1540
1541static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1542{
1543 struct seq_file *sf = cb->state;
1544 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1545}
1546
1547static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1548{
1549 struct cgroup_seqfile_state *state = m->private;
1550 struct cftype *cft = state->cft;
1551 struct cgroup_map_cb cb = {
1552 .fill = cgroup_map_add,
1553 .state = m,
1554 };
1555 return cft->read_map(state->cgroup, cft, &cb);
1556}
1557
1558int cgroup_seqfile_release(struct inode *inode, struct file *file)
1559{
1560 struct seq_file *seq = file->private_data;
1561 kfree(seq->private);
1562 return single_release(inode, file);
1563}
1564
1565static struct file_operations cgroup_seqfile_operations = {
1566 .read = seq_read,
1567 .llseek = seq_lseek,
1568 .release = cgroup_seqfile_release,
1569};
1570
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571static int cgroup_file_open(struct inode *inode, struct file *file)
1572{
1573 int err;
1574 struct cftype *cft;
1575
1576 err = generic_file_open(inode, file);
1577 if (err)
1578 return err;
1579
1580 cft = __d_cft(file->f_dentry);
1581 if (!cft)
1582 return -ENODEV;
Paul Menage91796562008-04-29 01:00:01 -07001583 if (cft->read_map) {
1584 struct cgroup_seqfile_state *state =
1585 kzalloc(sizeof(*state), GFP_USER);
1586 if (!state)
1587 return -ENOMEM;
1588 state->cft = cft;
1589 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1590 file->f_op = &cgroup_seqfile_operations;
1591 err = single_open(file, cgroup_seqfile_show, state);
1592 if (err < 0)
1593 kfree(state);
1594 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595 err = cft->open(inode, file);
1596 else
1597 err = 0;
1598
1599 return err;
1600}
1601
1602static int cgroup_file_release(struct inode *inode, struct file *file)
1603{
1604 struct cftype *cft = __d_cft(file->f_dentry);
1605 if (cft->release)
1606 return cft->release(inode, file);
1607 return 0;
1608}
1609
1610/*
1611 * cgroup_rename - Only allow simple rename of directories in place.
1612 */
1613static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1614 struct inode *new_dir, struct dentry *new_dentry)
1615{
1616 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1617 return -ENOTDIR;
1618 if (new_dentry->d_inode)
1619 return -EEXIST;
1620 if (old_dir != new_dir)
1621 return -EIO;
1622 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1623}
1624
1625static struct file_operations cgroup_file_operations = {
1626 .read = cgroup_file_read,
1627 .write = cgroup_file_write,
1628 .llseek = generic_file_llseek,
1629 .open = cgroup_file_open,
1630 .release = cgroup_file_release,
1631};
1632
1633static struct inode_operations cgroup_dir_inode_operations = {
1634 .lookup = simple_lookup,
1635 .mkdir = cgroup_mkdir,
1636 .rmdir = cgroup_rmdir,
1637 .rename = cgroup_rename,
1638};
1639
1640static int cgroup_create_file(struct dentry *dentry, int mode,
1641 struct super_block *sb)
1642{
1643 static struct dentry_operations cgroup_dops = {
1644 .d_iput = cgroup_diput,
1645 };
1646
1647 struct inode *inode;
1648
1649 if (!dentry)
1650 return -ENOENT;
1651 if (dentry->d_inode)
1652 return -EEXIST;
1653
1654 inode = cgroup_new_inode(mode, sb);
1655 if (!inode)
1656 return -ENOMEM;
1657
1658 if (S_ISDIR(mode)) {
1659 inode->i_op = &cgroup_dir_inode_operations;
1660 inode->i_fop = &simple_dir_operations;
1661
1662 /* start off with i_nlink == 2 (for "." entry) */
1663 inc_nlink(inode);
1664
1665 /* start with the directory inode held, so that we can
1666 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07001667 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001668 } else if (S_ISREG(mode)) {
1669 inode->i_size = 0;
1670 inode->i_fop = &cgroup_file_operations;
1671 }
1672 dentry->d_op = &cgroup_dops;
1673 d_instantiate(dentry, inode);
1674 dget(dentry); /* Extra count - pin the dentry in core */
1675 return 0;
1676}
1677
1678/*
Li Zefana043e3b2008-02-23 15:24:09 -08001679 * cgroup_create_dir - create a directory for an object.
1680 * @cgrp: the cgroup we create the directory for. It must have a valid
1681 * ->parent field. And we are going to fill its ->dentry field.
1682 * @dentry: dentry of the new cgroup
1683 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001685static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001686 int mode)
1687{
1688 struct dentry *parent;
1689 int error = 0;
1690
Paul Menagebd89aab2007-10-18 23:40:44 -07001691 parent = cgrp->parent->dentry;
1692 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001694 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001695 inc_nlink(parent->d_inode);
Paul Menagebd89aab2007-10-18 23:40:44 -07001696 cgrp->dentry = dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001697 dget(dentry);
1698 }
1699 dput(dentry);
1700
1701 return error;
1702}
1703
Paul Menagebd89aab2007-10-18 23:40:44 -07001704int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705 struct cgroup_subsys *subsys,
1706 const struct cftype *cft)
1707{
Paul Menagebd89aab2007-10-18 23:40:44 -07001708 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001709 struct dentry *dentry;
1710 int error;
1711
1712 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07001713 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001714 strcpy(name, subsys->name);
1715 strcat(name, ".");
1716 }
1717 strcat(name, cft->name);
1718 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1719 dentry = lookup_one_len(name, dir, strlen(name));
1720 if (!IS_ERR(dentry)) {
1721 error = cgroup_create_file(dentry, 0644 | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07001722 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723 if (!error)
1724 dentry->d_fsdata = (void *)cft;
1725 dput(dentry);
1726 } else
1727 error = PTR_ERR(dentry);
1728 return error;
1729}
1730
Paul Menagebd89aab2007-10-18 23:40:44 -07001731int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732 struct cgroup_subsys *subsys,
1733 const struct cftype cft[],
1734 int count)
1735{
1736 int i, err;
1737 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001738 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739 if (err)
1740 return err;
1741 }
1742 return 0;
1743}
1744
Li Zefana043e3b2008-02-23 15:24:09 -08001745/**
1746 * cgroup_task_count - count the number of tasks in a cgroup.
1747 * @cgrp: the cgroup in question
1748 *
1749 * Return the number of tasks in the cgroup.
1750 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001751int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001752{
1753 int count = 0;
Paul Menage817929e2007-10-18 23:39:36 -07001754 struct list_head *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001755
Paul Menage817929e2007-10-18 23:39:36 -07001756 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001757 l = cgrp->css_sets.next;
1758 while (l != &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001759 struct cg_cgroup_link *link =
Paul Menagebd89aab2007-10-18 23:40:44 -07001760 list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001761 count += atomic_read(&link->cg->ref.refcount);
1762 l = l->next;
1763 }
1764 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001765 return count;
1766}
1767
1768/*
Paul Menage817929e2007-10-18 23:39:36 -07001769 * Advance a list_head iterator. The iterator should be positioned at
1770 * the start of a css_set
1771 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001772static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001773 struct cgroup_iter *it)
1774{
1775 struct list_head *l = it->cg_link;
1776 struct cg_cgroup_link *link;
1777 struct css_set *cg;
1778
1779 /* Advance to the next non-empty css_set */
1780 do {
1781 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07001782 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001783 it->cg_link = NULL;
1784 return;
1785 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001786 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001787 cg = link->cg;
1788 } while (list_empty(&cg->tasks));
1789 it->cg_link = l;
1790 it->task = cg->tasks.next;
1791}
1792
Cliff Wickman31a7df02008-02-07 00:14:42 -08001793/*
1794 * To reduce the fork() overhead for systems that are not actually
1795 * using their cgroups capability, we don't maintain the lists running
1796 * through each css_set to its tasks until we see the list actually
1797 * used - in other words after the first call to cgroup_iter_start().
1798 *
1799 * The tasklist_lock is not held here, as do_each_thread() and
1800 * while_each_thread() are protected by RCU.
1801 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07001802static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08001803{
1804 struct task_struct *p, *g;
1805 write_lock(&css_set_lock);
1806 use_task_css_set_links = 1;
1807 do_each_thread(g, p) {
1808 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08001809 /*
1810 * We should check if the process is exiting, otherwise
1811 * it will race with cgroup_exit() in that the list
1812 * entry won't be deleted though the process has exited.
1813 */
1814 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08001815 list_add(&p->cg_list, &p->cgroups->tasks);
1816 task_unlock(p);
1817 } while_each_thread(g, p);
1818 write_unlock(&css_set_lock);
1819}
1820
Paul Menagebd89aab2007-10-18 23:40:44 -07001821void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001822{
1823 /*
1824 * The first time anyone tries to iterate across a cgroup,
1825 * we need to enable the list linking each css_set to its
1826 * tasks, and fix up all existing tasks.
1827 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08001828 if (!use_task_css_set_links)
1829 cgroup_enable_task_cg_lists();
1830
Paul Menage817929e2007-10-18 23:39:36 -07001831 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001832 it->cg_link = &cgrp->css_sets;
1833 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001834}
1835
Paul Menagebd89aab2007-10-18 23:40:44 -07001836struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001837 struct cgroup_iter *it)
1838{
1839 struct task_struct *res;
1840 struct list_head *l = it->task;
1841
1842 /* If the iterator cg is NULL, we have no tasks */
1843 if (!it->cg_link)
1844 return NULL;
1845 res = list_entry(l, struct task_struct, cg_list);
1846 /* Advance iterator to find next entry */
1847 l = l->next;
1848 if (l == &res->cgroups->tasks) {
1849 /* We reached the end of this task list - move on to
1850 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07001851 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001852 } else {
1853 it->task = l;
1854 }
1855 return res;
1856}
1857
Paul Menagebd89aab2007-10-18 23:40:44 -07001858void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001859{
1860 read_unlock(&css_set_lock);
1861}
1862
Cliff Wickman31a7df02008-02-07 00:14:42 -08001863static inline int started_after_time(struct task_struct *t1,
1864 struct timespec *time,
1865 struct task_struct *t2)
1866{
1867 int start_diff = timespec_compare(&t1->start_time, time);
1868 if (start_diff > 0) {
1869 return 1;
1870 } else if (start_diff < 0) {
1871 return 0;
1872 } else {
1873 /*
1874 * Arbitrarily, if two processes started at the same
1875 * time, we'll say that the lower pointer value
1876 * started first. Note that t2 may have exited by now
1877 * so this may not be a valid pointer any longer, but
1878 * that's fine - it still serves to distinguish
1879 * between two tasks started (effectively) simultaneously.
1880 */
1881 return t1 > t2;
1882 }
1883}
1884
1885/*
1886 * This function is a callback from heap_insert() and is used to order
1887 * the heap.
1888 * In this case we order the heap in descending task start time.
1889 */
1890static inline int started_after(void *p1, void *p2)
1891{
1892 struct task_struct *t1 = p1;
1893 struct task_struct *t2 = p2;
1894 return started_after_time(t1, &t2->start_time, t2);
1895}
1896
1897/**
1898 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1899 * @scan: struct cgroup_scanner containing arguments for the scan
1900 *
1901 * Arguments include pointers to callback functions test_task() and
1902 * process_task().
1903 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1904 * and if it returns true, call process_task() for it also.
1905 * The test_task pointer may be NULL, meaning always true (select all tasks).
1906 * Effectively duplicates cgroup_iter_{start,next,end}()
1907 * but does not lock css_set_lock for the call to process_task().
1908 * The struct cgroup_scanner may be embedded in any structure of the caller's
1909 * creation.
1910 * It is guaranteed that process_task() will act on every task that
1911 * is a member of the cgroup for the duration of this call. This
1912 * function may or may not call process_task() for tasks that exit
1913 * or move to a different cgroup during the call, or are forked or
1914 * move into the cgroup during the call.
1915 *
1916 * Note that test_task() may be called with locks held, and may in some
1917 * situations be called multiple times for the same task, so it should
1918 * be cheap.
1919 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1920 * pre-allocated and will be used for heap operations (and its "gt" member will
1921 * be overwritten), else a temporary heap will be used (allocation of which
1922 * may cause this function to fail).
1923 */
1924int cgroup_scan_tasks(struct cgroup_scanner *scan)
1925{
1926 int retval, i;
1927 struct cgroup_iter it;
1928 struct task_struct *p, *dropped;
1929 /* Never dereference latest_task, since it's not refcounted */
1930 struct task_struct *latest_task = NULL;
1931 struct ptr_heap tmp_heap;
1932 struct ptr_heap *heap;
1933 struct timespec latest_time = { 0, 0 };
1934
1935 if (scan->heap) {
1936 /* The caller supplied our heap and pre-allocated its memory */
1937 heap = scan->heap;
1938 heap->gt = &started_after;
1939 } else {
1940 /* We need to allocate our own heap memory */
1941 heap = &tmp_heap;
1942 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1943 if (retval)
1944 /* cannot allocate the heap */
1945 return retval;
1946 }
1947
1948 again:
1949 /*
1950 * Scan tasks in the cgroup, using the scanner's "test_task" callback
1951 * to determine which are of interest, and using the scanner's
1952 * "process_task" callback to process any of them that need an update.
1953 * Since we don't want to hold any locks during the task updates,
1954 * gather tasks to be processed in a heap structure.
1955 * The heap is sorted by descending task start time.
1956 * If the statically-sized heap fills up, we overflow tasks that
1957 * started later, and in future iterations only consider tasks that
1958 * started after the latest task in the previous pass. This
1959 * guarantees forward progress and that we don't miss any tasks.
1960 */
1961 heap->size = 0;
1962 cgroup_iter_start(scan->cg, &it);
1963 while ((p = cgroup_iter_next(scan->cg, &it))) {
1964 /*
1965 * Only affect tasks that qualify per the caller's callback,
1966 * if he provided one
1967 */
1968 if (scan->test_task && !scan->test_task(p, scan))
1969 continue;
1970 /*
1971 * Only process tasks that started after the last task
1972 * we processed
1973 */
1974 if (!started_after_time(p, &latest_time, latest_task))
1975 continue;
1976 dropped = heap_insert(heap, p);
1977 if (dropped == NULL) {
1978 /*
1979 * The new task was inserted; the heap wasn't
1980 * previously full
1981 */
1982 get_task_struct(p);
1983 } else if (dropped != p) {
1984 /*
1985 * The new task was inserted, and pushed out a
1986 * different task
1987 */
1988 get_task_struct(p);
1989 put_task_struct(dropped);
1990 }
1991 /*
1992 * Else the new task was newer than anything already in
1993 * the heap and wasn't inserted
1994 */
1995 }
1996 cgroup_iter_end(scan->cg, &it);
1997
1998 if (heap->size) {
1999 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002000 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002001 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002002 latest_time = q->start_time;
2003 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002004 }
2005 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002006 scan->process_task(q, scan);
2007 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002008 }
2009 /*
2010 * If we had to process any tasks at all, scan again
2011 * in case some of them were in the middle of forking
2012 * children that didn't get processed.
2013 * Not the most efficient way to do it, but it avoids
2014 * having to take callback_mutex in the fork path
2015 */
2016 goto again;
2017 }
2018 if (heap == &tmp_heap)
2019 heap_free(&tmp_heap);
2020 return 0;
2021}
2022
Paul Menage817929e2007-10-18 23:39:36 -07002023/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07002024 * Stuff for reading the 'tasks' file.
2025 *
2026 * Reading this file can return large amounts of data if a cgroup has
2027 * *lots* of attached tasks. So it may need several calls to read(),
2028 * but we cannot guarantee that the information we produce is correct
2029 * unless we produce it entirely atomically.
2030 *
2031 * Upon tasks file open(), a struct ctr_struct is allocated, that
2032 * will have a pointer to an array (also allocated here). The struct
2033 * ctr_struct * is stored in file->private_data. Its resources will
2034 * be freed by release() when the file is closed. The array is used
2035 * to sprintf the PIDs and then used by read().
2036 */
2037struct ctr_struct {
2038 char *buf;
2039 int bufsz;
2040};
2041
2042/*
2043 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
Paul Menagebd89aab2007-10-18 23:40:44 -07002044 * 'cgrp'. Return actual number of pids loaded. No need to
Paul Menagebbcb81d2007-10-18 23:39:32 -07002045 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2046 * read section, so the css_set can't go away, and is
2047 * immutable after creation.
2048 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002049static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002050{
2051 int n = 0;
Paul Menage817929e2007-10-18 23:39:36 -07002052 struct cgroup_iter it;
2053 struct task_struct *tsk;
Paul Menagebd89aab2007-10-18 23:40:44 -07002054 cgroup_iter_start(cgrp, &it);
2055 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Paul Menage817929e2007-10-18 23:39:36 -07002056 if (unlikely(n == npids))
2057 break;
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002058 pidarray[n++] = task_pid_vnr(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002059 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002060 cgroup_iter_end(cgrp, &it);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002061 return n;
2062}
2063
Balbir Singh846c7bb2007-10-18 23:39:44 -07002064/**
Li Zefana043e3b2008-02-23 15:24:09 -08002065 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002066 * @stats: cgroupstats to fill information into
2067 * @dentry: A dentry entry belonging to the cgroup for which stats have
2068 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002069 *
2070 * Build and fill cgroupstats so that taskstats can export it to user
2071 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002072 */
2073int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2074{
2075 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002076 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002077 struct cgroup_iter it;
2078 struct task_struct *tsk;
2079 /*
2080 * Validate dentry by checking the superblock operations
2081 */
2082 if (dentry->d_sb->s_op != &cgroup_ops)
2083 goto err;
2084
2085 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002086 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002087 rcu_read_lock();
2088
Paul Menagebd89aab2007-10-18 23:40:44 -07002089 cgroup_iter_start(cgrp, &it);
2090 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002091 switch (tsk->state) {
2092 case TASK_RUNNING:
2093 stats->nr_running++;
2094 break;
2095 case TASK_INTERRUPTIBLE:
2096 stats->nr_sleeping++;
2097 break;
2098 case TASK_UNINTERRUPTIBLE:
2099 stats->nr_uninterruptible++;
2100 break;
2101 case TASK_STOPPED:
2102 stats->nr_stopped++;
2103 break;
2104 default:
2105 if (delayacct_is_task_waiting_on_io(tsk))
2106 stats->nr_io_wait++;
2107 break;
2108 }
2109 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002110 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002111
2112 rcu_read_unlock();
2113err:
2114 return ret;
2115}
2116
Paul Menagebbcb81d2007-10-18 23:39:32 -07002117static int cmppid(const void *a, const void *b)
2118{
2119 return *(pid_t *)a - *(pid_t *)b;
2120}
2121
2122/*
2123 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
2124 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
2125 * count 'cnt' of how many chars would be written if buf were large enough.
2126 */
2127static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
2128{
2129 int cnt = 0;
2130 int i;
2131
2132 for (i = 0; i < npids; i++)
2133 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
2134 return cnt;
2135}
2136
2137/*
2138 * Handle an open on 'tasks' file. Prepare a buffer listing the
2139 * process id's of tasks currently attached to the cgroup being opened.
2140 *
2141 * Does not require any specific cgroup mutexes, and does not take any.
2142 */
2143static int cgroup_tasks_open(struct inode *unused, struct file *file)
2144{
Paul Menagebd89aab2007-10-18 23:40:44 -07002145 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002146 struct ctr_struct *ctr;
2147 pid_t *pidarray;
2148 int npids;
2149 char c;
2150
2151 if (!(file->f_mode & FMODE_READ))
2152 return 0;
2153
2154 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
2155 if (!ctr)
2156 goto err0;
2157
2158 /*
2159 * If cgroup gets more users after we read count, we won't have
2160 * enough space - tough. This race is indistinguishable to the
2161 * caller from the case that the additional cgroup users didn't
2162 * show up until sometime later on.
2163 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002164 npids = cgroup_task_count(cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002165 if (npids) {
2166 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2167 if (!pidarray)
2168 goto err1;
2169
Paul Menagebd89aab2007-10-18 23:40:44 -07002170 npids = pid_array_load(pidarray, npids, cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002171 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
2172
2173 /* Call pid_array_to_buf() twice, first just to get bufsz */
2174 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
2175 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
2176 if (!ctr->buf)
2177 goto err2;
2178 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
2179
2180 kfree(pidarray);
2181 } else {
Al Viro9dce07f2008-03-29 03:07:28 +00002182 ctr->buf = NULL;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002183 ctr->bufsz = 0;
2184 }
2185 file->private_data = ctr;
2186 return 0;
2187
2188err2:
2189 kfree(pidarray);
2190err1:
2191 kfree(ctr);
2192err0:
2193 return -ENOMEM;
2194}
2195
Paul Menagebd89aab2007-10-18 23:40:44 -07002196static ssize_t cgroup_tasks_read(struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002197 struct cftype *cft,
2198 struct file *file, char __user *buf,
2199 size_t nbytes, loff_t *ppos)
2200{
2201 struct ctr_struct *ctr = file->private_data;
2202
2203 return simple_read_from_buffer(buf, nbytes, ppos, ctr->buf, ctr->bufsz);
2204}
2205
2206static int cgroup_tasks_release(struct inode *unused_inode,
2207 struct file *file)
2208{
2209 struct ctr_struct *ctr;
2210
2211 if (file->f_mode & FMODE_READ) {
2212 ctr = file->private_data;
2213 kfree(ctr->buf);
2214 kfree(ctr);
2215 }
2216 return 0;
2217}
2218
Paul Menagebd89aab2007-10-18 23:40:44 -07002219static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002220 struct cftype *cft)
2221{
Paul Menagebd89aab2007-10-18 23:40:44 -07002222 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002223}
2224
Paul Menagebbcb81d2007-10-18 23:39:32 -07002225/*
2226 * for the common functions, 'private' gives the type of file
2227 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07002228static struct cftype files[] = {
2229 {
2230 .name = "tasks",
2231 .open = cgroup_tasks_open,
2232 .read = cgroup_tasks_read,
2233 .write = cgroup_common_file_write,
2234 .release = cgroup_tasks_release,
2235 .private = FILE_TASKLIST,
2236 },
2237
2238 {
2239 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002240 .read_u64 = cgroup_read_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002241 .write = cgroup_common_file_write,
2242 .private = FILE_NOTIFY_ON_RELEASE,
2243 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002244};
2245
2246static struct cftype cft_release_agent = {
2247 .name = "release_agent",
2248 .read = cgroup_common_file_read,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002249 .write = cgroup_common_file_write,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002250 .private = FILE_RELEASE_AGENT,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002251};
2252
Paul Menagebd89aab2007-10-18 23:40:44 -07002253static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002254{
2255 int err;
2256 struct cgroup_subsys *ss;
2257
2258 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002259 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002260
Paul Menagebd89aab2007-10-18 23:40:44 -07002261 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002262 if (err < 0)
2263 return err;
2264
Paul Menagebd89aab2007-10-18 23:40:44 -07002265 if (cgrp == cgrp->top_cgroup) {
2266 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002267 return err;
2268 }
2269
Paul Menagebd89aab2007-10-18 23:40:44 -07002270 for_each_subsys(cgrp->root, ss) {
2271 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002272 return err;
2273 }
2274
2275 return 0;
2276}
2277
2278static void init_cgroup_css(struct cgroup_subsys_state *css,
2279 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002280 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002281{
Paul Menagebd89aab2007-10-18 23:40:44 -07002282 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002283 atomic_set(&css->refcnt, 0);
2284 css->flags = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002285 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002286 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002287 BUG_ON(cgrp->subsys[ss->subsys_id]);
2288 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002289}
2290
2291/*
Li Zefana043e3b2008-02-23 15:24:09 -08002292 * cgroup_create - create a cgroup
2293 * @parent: cgroup that will be parent of the new cgroup
2294 * @dentry: dentry of the new cgroup
2295 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002296 *
Li Zefana043e3b2008-02-23 15:24:09 -08002297 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002298 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002299static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2300 int mode)
2301{
Paul Menagebd89aab2007-10-18 23:40:44 -07002302 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002303 struct cgroupfs_root *root = parent->root;
2304 int err = 0;
2305 struct cgroup_subsys *ss;
2306 struct super_block *sb = root->sb;
2307
Paul Menagebd89aab2007-10-18 23:40:44 -07002308 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2309 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002310 return -ENOMEM;
2311
2312 /* Grab a reference on the superblock so the hierarchy doesn't
2313 * get deleted on unmount if there are child cgroups. This
2314 * can be done outside cgroup_mutex, since the sb can't
2315 * disappear while someone has an open control file on the
2316 * fs */
2317 atomic_inc(&sb->s_active);
2318
2319 mutex_lock(&cgroup_mutex);
2320
Paul Menagebd89aab2007-10-18 23:40:44 -07002321 INIT_LIST_HEAD(&cgrp->sibling);
2322 INIT_LIST_HEAD(&cgrp->children);
2323 INIT_LIST_HEAD(&cgrp->css_sets);
2324 INIT_LIST_HEAD(&cgrp->release_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002325
Paul Menagebd89aab2007-10-18 23:40:44 -07002326 cgrp->parent = parent;
2327 cgrp->root = parent->root;
2328 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002329
Li Zefanb6abdb02008-03-04 14:28:19 -08002330 if (notify_on_release(parent))
2331 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2332
Paul Menageddbcc7e2007-10-18 23:39:30 -07002333 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002334 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002335 if (IS_ERR(css)) {
2336 err = PTR_ERR(css);
2337 goto err_destroy;
2338 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002339 init_cgroup_css(css, ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002340 }
2341
Paul Menagebd89aab2007-10-18 23:40:44 -07002342 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002343 root->number_of_cgroups++;
2344
Paul Menagebd89aab2007-10-18 23:40:44 -07002345 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002346 if (err < 0)
2347 goto err_remove;
2348
2349 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07002350 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002351
Paul Menagebd89aab2007-10-18 23:40:44 -07002352 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002353 /* If err < 0, we have a half-filled directory - oh well ;) */
2354
2355 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002356 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002357
2358 return 0;
2359
2360 err_remove:
2361
Paul Menagebd89aab2007-10-18 23:40:44 -07002362 list_del(&cgrp->sibling);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002363 root->number_of_cgroups--;
2364
2365 err_destroy:
2366
2367 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002368 if (cgrp->subsys[ss->subsys_id])
2369 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002370 }
2371
2372 mutex_unlock(&cgroup_mutex);
2373
2374 /* Release the reference count that we took on the superblock */
2375 deactivate_super(sb);
2376
Paul Menagebd89aab2007-10-18 23:40:44 -07002377 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002378 return err;
2379}
2380
2381static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2382{
2383 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2384
2385 /* the vfs holds inode->i_mutex already */
2386 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2387}
2388
Paul Menagebd89aab2007-10-18 23:40:44 -07002389static inline int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002390{
2391 /* Check the reference count on each subsystem. Since we
2392 * already established that there are no tasks in the
2393 * cgroup, if the css refcount is also 0, then there should
2394 * be no outstanding references, so the subsystem is safe to
2395 * destroy. We scan across all subsystems rather than using
2396 * the per-hierarchy linked list of mounted subsystems since
2397 * we can be called via check_for_release() with no
2398 * synchronization other than RCU, and the subsystem linked
2399 * list isn't RCU-safe */
2400 int i;
2401 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2402 struct cgroup_subsys *ss = subsys[i];
2403 struct cgroup_subsys_state *css;
2404 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07002405 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002406 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07002407 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07002408 /* When called from check_for_release() it's possible
2409 * that by this point the cgroup has been removed
2410 * and the css deleted. But a false-positive doesn't
2411 * matter, since it can only happen if the cgroup
2412 * has been deleted and hence no longer needs the
2413 * release agent to be called anyway. */
Paul Jacksone18f6312008-02-07 00:13:44 -08002414 if (css && atomic_read(&css->refcnt))
Paul Menage81a6a5c2007-10-18 23:39:38 -07002415 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07002416 }
2417 return 0;
2418}
2419
Paul Menageddbcc7e2007-10-18 23:39:30 -07002420static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2421{
Paul Menagebd89aab2007-10-18 23:40:44 -07002422 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002423 struct dentry *d;
2424 struct cgroup *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002425 struct super_block *sb;
2426 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002427
2428 /* the vfs holds both inode->i_mutex already */
2429
2430 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002431 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432 mutex_unlock(&cgroup_mutex);
2433 return -EBUSY;
2434 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002435 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002436 mutex_unlock(&cgroup_mutex);
2437 return -EBUSY;
2438 }
2439
Paul Menagebd89aab2007-10-18 23:40:44 -07002440 parent = cgrp->parent;
2441 root = cgrp->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002442 sb = root->sb;
Li Zefana043e3b2008-02-23 15:24:09 -08002443
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002444 /*
Li Zefana043e3b2008-02-23 15:24:09 -08002445 * Call pre_destroy handlers of subsys. Notify subsystems
2446 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002447 */
2448 cgroup_call_pre_destroy(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002449
Paul Menagebd89aab2007-10-18 23:40:44 -07002450 if (cgroup_has_css_refs(cgrp)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002451 mutex_unlock(&cgroup_mutex);
2452 return -EBUSY;
2453 }
2454
Paul Menage81a6a5c2007-10-18 23:39:38 -07002455 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002456 set_bit(CGRP_REMOVED, &cgrp->flags);
2457 if (!list_empty(&cgrp->release_list))
2458 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002459 spin_unlock(&release_list_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002460 /* delete my sibling from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07002461 list_del(&cgrp->sibling);
2462 spin_lock(&cgrp->dentry->d_lock);
2463 d = dget(cgrp->dentry);
2464 cgrp->dentry = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002465 spin_unlock(&d->d_lock);
2466
2467 cgroup_d_remove_dir(d);
2468 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002469
Paul Menagebd89aab2007-10-18 23:40:44 -07002470 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002471 check_for_release(parent);
2472
Paul Menageddbcc7e2007-10-18 23:39:30 -07002473 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002474 return 0;
2475}
2476
Li Zefan06a11922008-04-29 01:00:07 -07002477static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002478{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002479 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08002480
2481 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002482
2483 /* Create the top cgroup state for this subsystem */
2484 ss->root = &rootnode;
2485 css = ss->create(ss, dummytop);
2486 /* We don't handle early failures gracefully */
2487 BUG_ON(IS_ERR(css));
2488 init_cgroup_css(css, ss, dummytop);
2489
Li Zefane8d55fd2008-04-29 01:00:13 -07002490 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07002491 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07002492 * newly registered, all tasks and hence the
2493 * init_css_set is in the subsystem's top cgroup. */
2494 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07002495
2496 need_forkexit_callback |= ss->fork || ss->exit;
2497
Li Zefane8d55fd2008-04-29 01:00:13 -07002498 /* At system boot, before all subsystems have been
2499 * registered, no tasks have been forked, so we don't
2500 * need to invoke fork callbacks here. */
2501 BUG_ON(!list_empty(&init_task.tasks));
2502
Paul Menageddbcc7e2007-10-18 23:39:30 -07002503 ss->active = 1;
2504}
2505
2506/**
Li Zefana043e3b2008-02-23 15:24:09 -08002507 * cgroup_init_early - cgroup initialization at system boot
2508 *
2509 * Initialize cgroups at system boot, and initialize any
2510 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002511 */
2512int __init cgroup_init_early(void)
2513{
2514 int i;
Paul Menage817929e2007-10-18 23:39:36 -07002515 kref_init(&init_css_set.ref);
2516 kref_get(&init_css_set.ref);
2517 INIT_LIST_HEAD(&init_css_set.list);
2518 INIT_LIST_HEAD(&init_css_set.cg_links);
2519 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07002520 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07002521 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002522 init_cgroup_root(&rootnode);
2523 list_add(&rootnode.root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07002524 root_count = 1;
2525 init_task.cgroups = &init_css_set;
2526
2527 init_css_set_link.cg = &init_css_set;
Paul Menagebd89aab2007-10-18 23:40:44 -07002528 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07002529 &rootnode.top_cgroup.css_sets);
2530 list_add(&init_css_set_link.cg_link_list,
2531 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002532
Li Zefan472b1052008-04-29 01:00:11 -07002533 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2534 INIT_HLIST_HEAD(&css_set_table[i]);
2535
Paul Menageddbcc7e2007-10-18 23:39:30 -07002536 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2537 struct cgroup_subsys *ss = subsys[i];
2538
2539 BUG_ON(!ss->name);
2540 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2541 BUG_ON(!ss->create);
2542 BUG_ON(!ss->destroy);
2543 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08002544 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07002545 ss->name, ss->subsys_id);
2546 BUG();
2547 }
2548
2549 if (ss->early_init)
2550 cgroup_init_subsys(ss);
2551 }
2552 return 0;
2553}
2554
2555/**
Li Zefana043e3b2008-02-23 15:24:09 -08002556 * cgroup_init - cgroup initialization
2557 *
2558 * Register cgroup filesystem and /proc file, and initialize
2559 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002560 */
2561int __init cgroup_init(void)
2562{
2563 int err;
2564 int i;
Li Zefan472b1052008-04-29 01:00:11 -07002565 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07002566
2567 err = bdi_init(&cgroup_backing_dev_info);
2568 if (err)
2569 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002570
2571 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2572 struct cgroup_subsys *ss = subsys[i];
2573 if (!ss->early_init)
2574 cgroup_init_subsys(ss);
2575 }
2576
Li Zefan472b1052008-04-29 01:00:11 -07002577 /* Add init_css_set to the hash table */
2578 hhead = css_set_hash(init_css_set.subsys);
2579 hlist_add_head(&init_css_set.hlist, hhead);
2580
Paul Menageddbcc7e2007-10-18 23:39:30 -07002581 err = register_filesystem(&cgroup_fs_type);
2582 if (err < 0)
2583 goto out;
2584
Li Zefan46ae2202008-04-29 01:00:08 -07002585 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07002586
Paul Menageddbcc7e2007-10-18 23:39:30 -07002587out:
Paul Menagea4243162007-10-18 23:39:35 -07002588 if (err)
2589 bdi_destroy(&cgroup_backing_dev_info);
2590
Paul Menageddbcc7e2007-10-18 23:39:30 -07002591 return err;
2592}
Paul Menageb4f48b62007-10-18 23:39:33 -07002593
Paul Menagea4243162007-10-18 23:39:35 -07002594/*
2595 * proc_cgroup_show()
2596 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2597 * - Used for /proc/<pid>/cgroup.
2598 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2599 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002600 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07002601 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2602 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2603 * cgroup to top_cgroup.
2604 */
2605
2606/* TODO: Use a proper seq_file iterator */
2607static int proc_cgroup_show(struct seq_file *m, void *v)
2608{
2609 struct pid *pid;
2610 struct task_struct *tsk;
2611 char *buf;
2612 int retval;
2613 struct cgroupfs_root *root;
2614
2615 retval = -ENOMEM;
2616 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2617 if (!buf)
2618 goto out;
2619
2620 retval = -ESRCH;
2621 pid = m->private;
2622 tsk = get_pid_task(pid, PIDTYPE_PID);
2623 if (!tsk)
2624 goto out_free;
2625
2626 retval = 0;
2627
2628 mutex_lock(&cgroup_mutex);
2629
2630 for_each_root(root) {
2631 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07002632 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07002633 int subsys_id;
2634 int count = 0;
2635
2636 /* Skip this hierarchy if it has no active subsystems */
2637 if (!root->actual_subsys_bits)
2638 continue;
Paul Menageb6c30062008-04-10 21:29:16 -07002639 seq_printf(m, "%lu:", root->subsys_bits);
Paul Menagea4243162007-10-18 23:39:35 -07002640 for_each_subsys(root, ss)
2641 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2642 seq_putc(m, ':');
2643 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07002644 cgrp = task_cgroup(tsk, subsys_id);
2645 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07002646 if (retval < 0)
2647 goto out_unlock;
2648 seq_puts(m, buf);
2649 seq_putc(m, '\n');
2650 }
2651
2652out_unlock:
2653 mutex_unlock(&cgroup_mutex);
2654 put_task_struct(tsk);
2655out_free:
2656 kfree(buf);
2657out:
2658 return retval;
2659}
2660
2661static int cgroup_open(struct inode *inode, struct file *file)
2662{
2663 struct pid *pid = PROC_I(inode)->pid;
2664 return single_open(file, proc_cgroup_show, pid);
2665}
2666
2667struct file_operations proc_cgroup_operations = {
2668 .open = cgroup_open,
2669 .read = seq_read,
2670 .llseek = seq_lseek,
2671 .release = single_release,
2672};
2673
2674/* Display information about each subsystem and each hierarchy */
2675static int proc_cgroupstats_show(struct seq_file *m, void *v)
2676{
2677 int i;
Paul Menagea4243162007-10-18 23:39:35 -07002678
Paul Menage8bab8dd2008-04-04 14:29:57 -07002679 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07002680 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07002681 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2682 struct cgroup_subsys *ss = subsys[i];
Paul Menage8bab8dd2008-04-04 14:29:57 -07002683 seq_printf(m, "%s\t%lu\t%d\t%d\n",
Paul Menage817929e2007-10-18 23:39:36 -07002684 ss->name, ss->root->subsys_bits,
Paul Menage8bab8dd2008-04-04 14:29:57 -07002685 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07002686 }
2687 mutex_unlock(&cgroup_mutex);
2688 return 0;
2689}
2690
2691static int cgroupstats_open(struct inode *inode, struct file *file)
2692{
Al Viro9dce07f2008-03-29 03:07:28 +00002693 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07002694}
2695
2696static struct file_operations proc_cgroupstats_operations = {
2697 .open = cgroupstats_open,
2698 .read = seq_read,
2699 .llseek = seq_lseek,
2700 .release = single_release,
2701};
2702
Paul Menageb4f48b62007-10-18 23:39:33 -07002703/**
2704 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08002705 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07002706 *
2707 * Description: A task inherits its parent's cgroup at fork().
2708 *
2709 * A pointer to the shared css_set was automatically copied in
2710 * fork.c by dup_task_struct(). However, we ignore that copy, since
2711 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08002712 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07002713 * have already changed current->cgroups, allowing the previously
2714 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07002715 *
2716 * At the point that cgroup_fork() is called, 'current' is the parent
2717 * task, and the passed argument 'child' points to the child task.
2718 */
2719void cgroup_fork(struct task_struct *child)
2720{
Paul Menage817929e2007-10-18 23:39:36 -07002721 task_lock(current);
2722 child->cgroups = current->cgroups;
2723 get_css_set(child->cgroups);
2724 task_unlock(current);
2725 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07002726}
2727
2728/**
Li Zefana043e3b2008-02-23 15:24:09 -08002729 * cgroup_fork_callbacks - run fork callbacks
2730 * @child: the new task
2731 *
2732 * Called on a new task very soon before adding it to the
2733 * tasklist. No need to take any locks since no-one can
2734 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07002735 */
2736void cgroup_fork_callbacks(struct task_struct *child)
2737{
2738 if (need_forkexit_callback) {
2739 int i;
2740 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2741 struct cgroup_subsys *ss = subsys[i];
2742 if (ss->fork)
2743 ss->fork(ss, child);
2744 }
2745 }
2746}
2747
2748/**
Li Zefana043e3b2008-02-23 15:24:09 -08002749 * cgroup_post_fork - called on a new task after adding it to the task list
2750 * @child: the task in question
2751 *
2752 * Adds the task to the list running through its css_set if necessary.
2753 * Has to be after the task is visible on the task list in case we race
2754 * with the first call to cgroup_iter_start() - to guarantee that the
2755 * new task ends up on its list.
2756 */
Paul Menage817929e2007-10-18 23:39:36 -07002757void cgroup_post_fork(struct task_struct *child)
2758{
2759 if (use_task_css_set_links) {
2760 write_lock(&css_set_lock);
2761 if (list_empty(&child->cg_list))
2762 list_add(&child->cg_list, &child->cgroups->tasks);
2763 write_unlock(&css_set_lock);
2764 }
2765}
2766/**
Paul Menageb4f48b62007-10-18 23:39:33 -07002767 * cgroup_exit - detach cgroup from exiting task
2768 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08002769 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07002770 *
2771 * Description: Detach cgroup from @tsk and release it.
2772 *
2773 * Note that cgroups marked notify_on_release force every task in
2774 * them to take the global cgroup_mutex mutex when exiting.
2775 * This could impact scaling on very large systems. Be reluctant to
2776 * use notify_on_release cgroups where very high task exit scaling
2777 * is required on large systems.
2778 *
2779 * the_top_cgroup_hack:
2780 *
2781 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2782 *
2783 * We call cgroup_exit() while the task is still competent to
2784 * handle notify_on_release(), then leave the task attached to the
2785 * root cgroup in each hierarchy for the remainder of its exit.
2786 *
2787 * To do this properly, we would increment the reference count on
2788 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2789 * code we would add a second cgroup function call, to drop that
2790 * reference. This would just create an unnecessary hot spot on
2791 * the top_cgroup reference count, to no avail.
2792 *
2793 * Normally, holding a reference to a cgroup without bumping its
2794 * count is unsafe. The cgroup could go away, or someone could
2795 * attach us to a different cgroup, decrementing the count on
2796 * the first cgroup that we never incremented. But in this case,
2797 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002798 * which wards off any cgroup_attach_task() attempts, or task is a failed
2799 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07002800 */
2801void cgroup_exit(struct task_struct *tsk, int run_callbacks)
2802{
2803 int i;
Paul Menage817929e2007-10-18 23:39:36 -07002804 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07002805
2806 if (run_callbacks && need_forkexit_callback) {
2807 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2808 struct cgroup_subsys *ss = subsys[i];
2809 if (ss->exit)
2810 ss->exit(ss, tsk);
2811 }
2812 }
Paul Menage817929e2007-10-18 23:39:36 -07002813
2814 /*
2815 * Unlink from the css_set task list if necessary.
2816 * Optimistically check cg_list before taking
2817 * css_set_lock
2818 */
2819 if (!list_empty(&tsk->cg_list)) {
2820 write_lock(&css_set_lock);
2821 if (!list_empty(&tsk->cg_list))
2822 list_del(&tsk->cg_list);
2823 write_unlock(&css_set_lock);
2824 }
2825
Paul Menageb4f48b62007-10-18 23:39:33 -07002826 /* Reassign the task to the init_css_set. */
2827 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002828 cg = tsk->cgroups;
2829 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07002830 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002831 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002832 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07002833}
Paul Menage697f4162007-10-18 23:39:34 -07002834
2835/**
Li Zefana043e3b2008-02-23 15:24:09 -08002836 * cgroup_clone - clone the cgroup the given subsystem is attached to
2837 * @tsk: the task to be moved
2838 * @subsys: the given subsystem
2839 *
2840 * Duplicate the current cgroup in the hierarchy that the given
2841 * subsystem is attached to, and move this task into the new
2842 * child.
Paul Menage697f4162007-10-18 23:39:34 -07002843 */
2844int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys)
2845{
2846 struct dentry *dentry;
2847 int ret = 0;
2848 char nodename[MAX_CGROUP_TYPE_NAMELEN];
2849 struct cgroup *parent, *child;
2850 struct inode *inode;
2851 struct css_set *cg;
2852 struct cgroupfs_root *root;
2853 struct cgroup_subsys *ss;
2854
2855 /* We shouldn't be called by an unregistered subsystem */
2856 BUG_ON(!subsys->active);
2857
2858 /* First figure out what hierarchy and cgroup we're dealing
2859 * with, and pin them so we can drop cgroup_mutex */
2860 mutex_lock(&cgroup_mutex);
2861 again:
2862 root = subsys->root;
2863 if (root == &rootnode) {
2864 printk(KERN_INFO
2865 "Not cloning cgroup for unused subsystem %s\n",
2866 subsys->name);
2867 mutex_unlock(&cgroup_mutex);
2868 return 0;
2869 }
Paul Menage817929e2007-10-18 23:39:36 -07002870 cg = tsk->cgroups;
Paul Menage697f4162007-10-18 23:39:34 -07002871 parent = task_cgroup(tsk, subsys->subsys_id);
2872
2873 snprintf(nodename, MAX_CGROUP_TYPE_NAMELEN, "node_%d", tsk->pid);
2874
2875 /* Pin the hierarchy */
2876 atomic_inc(&parent->root->sb->s_active);
2877
Paul Menage817929e2007-10-18 23:39:36 -07002878 /* Keep the cgroup alive */
2879 get_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07002880 mutex_unlock(&cgroup_mutex);
2881
2882 /* Now do the VFS work to create a cgroup */
2883 inode = parent->dentry->d_inode;
2884
2885 /* Hold the parent directory mutex across this operation to
2886 * stop anyone else deleting the new cgroup */
2887 mutex_lock(&inode->i_mutex);
2888 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
2889 if (IS_ERR(dentry)) {
2890 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08002891 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07002892 PTR_ERR(dentry));
2893 ret = PTR_ERR(dentry);
2894 goto out_release;
2895 }
2896
2897 /* Create the cgroup directory, which also creates the cgroup */
2898 ret = vfs_mkdir(inode, dentry, S_IFDIR | 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07002899 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07002900 dput(dentry);
2901 if (ret) {
2902 printk(KERN_INFO
2903 "Failed to create cgroup %s: %d\n", nodename,
2904 ret);
2905 goto out_release;
2906 }
2907
2908 if (!child) {
2909 printk(KERN_INFO
2910 "Couldn't find new cgroup %s\n", nodename);
2911 ret = -ENOMEM;
2912 goto out_release;
2913 }
2914
2915 /* The cgroup now exists. Retake cgroup_mutex and check
2916 * that we're still in the same state that we thought we
2917 * were. */
2918 mutex_lock(&cgroup_mutex);
2919 if ((root != subsys->root) ||
2920 (parent != task_cgroup(tsk, subsys->subsys_id))) {
2921 /* Aargh, we raced ... */
2922 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07002923 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07002924
2925 deactivate_super(parent->root->sb);
2926 /* The cgroup is still accessible in the VFS, but
2927 * we're not going to try to rmdir() it at this
2928 * point. */
2929 printk(KERN_INFO
2930 "Race in cgroup_clone() - leaking cgroup %s\n",
2931 nodename);
2932 goto again;
2933 }
2934
2935 /* do any required auto-setup */
2936 for_each_subsys(root, ss) {
2937 if (ss->post_clone)
2938 ss->post_clone(ss, child);
2939 }
2940
2941 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002942 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07002943 mutex_unlock(&cgroup_mutex);
2944
2945 out_release:
2946 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002947
2948 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07002949 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002950 mutex_unlock(&cgroup_mutex);
Paul Menage697f4162007-10-18 23:39:34 -07002951 deactivate_super(parent->root->sb);
2952 return ret;
2953}
2954
Li Zefana043e3b2008-02-23 15:24:09 -08002955/**
2956 * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
2957 * @cgrp: the cgroup in question
2958 *
2959 * See if @cgrp is a descendant of the current task's cgroup in
2960 * the appropriate hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07002961 *
2962 * If we are sending in dummytop, then presumably we are creating
2963 * the top cgroup in the subsystem.
2964 *
2965 * Called only by the ns (nsproxy) cgroup.
2966 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002967int cgroup_is_descendant(const struct cgroup *cgrp)
Paul Menage697f4162007-10-18 23:39:34 -07002968{
2969 int ret;
2970 struct cgroup *target;
2971 int subsys_id;
2972
Paul Menagebd89aab2007-10-18 23:40:44 -07002973 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07002974 return 1;
2975
Paul Menagebd89aab2007-10-18 23:40:44 -07002976 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menage697f4162007-10-18 23:39:34 -07002977 target = task_cgroup(current, subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07002978 while (cgrp != target && cgrp!= cgrp->top_cgroup)
2979 cgrp = cgrp->parent;
2980 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07002981 return ret;
2982}
Paul Menage81a6a5c2007-10-18 23:39:38 -07002983
Paul Menagebd89aab2007-10-18 23:40:44 -07002984static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002985{
2986 /* All of these checks rely on RCU to keep the cgroup
2987 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07002988 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
2989 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07002990 /* Control Group is currently removeable. If it's not
2991 * already queued for a userspace notification, queue
2992 * it now */
2993 int need_schedule_work = 0;
2994 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002995 if (!cgroup_is_removed(cgrp) &&
2996 list_empty(&cgrp->release_list)) {
2997 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002998 need_schedule_work = 1;
2999 }
3000 spin_unlock(&release_list_lock);
3001 if (need_schedule_work)
3002 schedule_work(&release_agent_work);
3003 }
3004}
3005
3006void __css_put(struct cgroup_subsys_state *css)
3007{
Paul Menagebd89aab2007-10-18 23:40:44 -07003008 struct cgroup *cgrp = css->cgroup;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003009 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -07003010 if (atomic_dec_and_test(&css->refcnt) && notify_on_release(cgrp)) {
3011 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3012 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003013 }
3014 rcu_read_unlock();
3015}
3016
3017/*
3018 * Notify userspace when a cgroup is released, by running the
3019 * configured release agent with the name of the cgroup (path
3020 * relative to the root of cgroup file system) as the argument.
3021 *
3022 * Most likely, this user command will try to rmdir this cgroup.
3023 *
3024 * This races with the possibility that some other task will be
3025 * attached to this cgroup before it is removed, or that some other
3026 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3027 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3028 * unused, and this cgroup will be reprieved from its death sentence,
3029 * to continue to serve a useful existence. Next time it's released,
3030 * we will get notified again, if it still has 'notify_on_release' set.
3031 *
3032 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3033 * means only wait until the task is successfully execve()'d. The
3034 * separate release agent task is forked by call_usermodehelper(),
3035 * then control in this thread returns here, without waiting for the
3036 * release agent task. We don't bother to wait because the caller of
3037 * this routine has no use for the exit status of the release agent
3038 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003039 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003040static void cgroup_release_agent(struct work_struct *work)
3041{
3042 BUG_ON(work != &release_agent_work);
3043 mutex_lock(&cgroup_mutex);
3044 spin_lock(&release_list_lock);
3045 while (!list_empty(&release_list)) {
3046 char *argv[3], *envp[3];
3047 int i;
3048 char *pathbuf;
Paul Menagebd89aab2007-10-18 23:40:44 -07003049 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003050 struct cgroup,
3051 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003052 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003053 spin_unlock(&release_list_lock);
3054 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3055 if (!pathbuf) {
3056 spin_lock(&release_list_lock);
3057 continue;
3058 }
3059
Paul Menagebd89aab2007-10-18 23:40:44 -07003060 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003061 kfree(pathbuf);
3062 spin_lock(&release_list_lock);
3063 continue;
3064 }
3065
3066 i = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003067 argv[i++] = cgrp->root->release_agent_path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003068 argv[i++] = (char *)pathbuf;
3069 argv[i] = NULL;
3070
3071 i = 0;
3072 /* minimal command environment */
3073 envp[i++] = "HOME=/";
3074 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3075 envp[i] = NULL;
3076
3077 /* Drop the lock while we invoke the usermode helper,
3078 * since the exec could involve hitting disk and hence
3079 * be a slow process */
3080 mutex_unlock(&cgroup_mutex);
3081 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
3082 kfree(pathbuf);
3083 mutex_lock(&cgroup_mutex);
3084 spin_lock(&release_list_lock);
3085 }
3086 spin_unlock(&release_list_lock);
3087 mutex_unlock(&cgroup_mutex);
3088}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003089
3090static int __init cgroup_disable(char *str)
3091{
3092 int i;
3093 char *token;
3094
3095 while ((token = strsep(&str, ",")) != NULL) {
3096 if (!*token)
3097 continue;
3098
3099 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3100 struct cgroup_subsys *ss = subsys[i];
3101
3102 if (!strcmp(token, ss->name)) {
3103 ss->disabled = 1;
3104 printk(KERN_INFO "Disabling %s control group"
3105 " subsystem\n", ss->name);
3106 break;
3107 }
3108 }
3109 }
3110 return 1;
3111}
3112__setup("cgroup_disable=", cgroup_disable);