blob: abc433772e5a7290e4951e9aadbf9270ae630709 [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 css_set_count--;
246 while (!list_empty(&cg->cg_links)) {
247 struct cg_cgroup_link *link;
248 link = list_entry(cg->cg_links.next,
249 struct cg_cgroup_link, cg_link_list);
250 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700251 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700252 kfree(link);
253 }
254 write_unlock(&css_set_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700255}
256
257static void __release_css_set(struct kref *k, int taskexit)
258{
259 int i;
260 struct css_set *cg = container_of(k, struct css_set, ref);
261
262 unlink_css_set(cg);
263
264 rcu_read_lock();
265 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700266 struct cgroup *cgrp = cg->subsys[i]->cgroup;
267 if (atomic_dec_and_test(&cgrp->count) &&
268 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700269 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700270 set_bit(CGRP_RELEASABLE, &cgrp->flags);
271 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700272 }
273 }
274 rcu_read_unlock();
Paul Menage817929e2007-10-18 23:39:36 -0700275 kfree(cg);
276}
277
Paul Menage81a6a5c2007-10-18 23:39:38 -0700278static void release_css_set(struct kref *k)
279{
280 __release_css_set(k, 0);
281}
282
283static void release_css_set_taskexit(struct kref *k)
284{
285 __release_css_set(k, 1);
286}
287
Paul Menage817929e2007-10-18 23:39:36 -0700288/*
289 * refcounted get/put for css_set objects
290 */
291static inline void get_css_set(struct css_set *cg)
292{
293 kref_get(&cg->ref);
294}
295
296static inline void put_css_set(struct css_set *cg)
297{
298 kref_put(&cg->ref, release_css_set);
299}
300
Paul Menage81a6a5c2007-10-18 23:39:38 -0700301static inline void put_css_set_taskexit(struct css_set *cg)
302{
303 kref_put(&cg->ref, release_css_set_taskexit);
304}
305
Paul Menage817929e2007-10-18 23:39:36 -0700306/*
307 * find_existing_css_set() is a helper for
308 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700309 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700310 *
311 * oldcg: the cgroup group that we're using before the cgroup
312 * transition
313 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700314 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700315 *
316 * template: location in which to build the desired set of subsystem
317 * state objects for the new cgroup group
318 */
Paul Menage817929e2007-10-18 23:39:36 -0700319static struct css_set *find_existing_css_set(
320 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700321 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700322 struct cgroup_subsys_state *template[])
323{
324 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700325 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700326 struct hlist_head *hhead;
327 struct hlist_node *node;
328 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700329
330 /* Built the set of subsystem state objects that we want to
331 * see in the new css_set */
332 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800333 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700334 /* Subsystem is in this hierarchy. So we want
335 * the subsystem state from the new
336 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700337 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700338 } else {
339 /* Subsystem is not in this hierarchy, so we
340 * don't want to change the subsystem state */
341 template[i] = oldcg->subsys[i];
342 }
343 }
344
Li Zefan472b1052008-04-29 01:00:11 -0700345 hhead = css_set_hash(template);
346 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage817929e2007-10-18 23:39:36 -0700347 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
348 /* All subsystems matched */
349 return cg;
350 }
Li Zefan472b1052008-04-29 01:00:11 -0700351 }
Paul Menage817929e2007-10-18 23:39:36 -0700352
353 /* No existing cgroup group matched */
354 return NULL;
355}
356
357/*
358 * allocate_cg_links() allocates "count" cg_cgroup_link structures
Paul Menagebd89aab2007-10-18 23:40:44 -0700359 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
Paul Menage817929e2007-10-18 23:39:36 -0700360 * success or a negative error
361 */
Paul Menage817929e2007-10-18 23:39:36 -0700362static int allocate_cg_links(int count, struct list_head *tmp)
363{
364 struct cg_cgroup_link *link;
365 int i;
366 INIT_LIST_HEAD(tmp);
367 for (i = 0; i < count; i++) {
368 link = kmalloc(sizeof(*link), GFP_KERNEL);
369 if (!link) {
370 while (!list_empty(tmp)) {
371 link = list_entry(tmp->next,
372 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700373 cgrp_link_list);
374 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700375 kfree(link);
376 }
377 return -ENOMEM;
378 }
Paul Menagebd89aab2007-10-18 23:40:44 -0700379 list_add(&link->cgrp_link_list, tmp);
Paul Menage817929e2007-10-18 23:39:36 -0700380 }
381 return 0;
382}
383
384static void free_cg_links(struct list_head *tmp)
385{
386 while (!list_empty(tmp)) {
387 struct cg_cgroup_link *link;
388 link = list_entry(tmp->next,
389 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700390 cgrp_link_list);
391 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700392 kfree(link);
393 }
394}
395
396/*
397 * find_css_set() takes an existing cgroup group and a
398 * cgroup object, and returns a css_set object that's
399 * equivalent to the old group, but with the given cgroup
400 * substituted into the appropriate hierarchy. Must be called with
401 * cgroup_mutex held
402 */
Paul Menage817929e2007-10-18 23:39:36 -0700403static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700404 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700405{
406 struct css_set *res;
407 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
408 int i;
409
410 struct list_head tmp_cg_links;
411 struct cg_cgroup_link *link;
412
Li Zefan472b1052008-04-29 01:00:11 -0700413 struct hlist_head *hhead;
414
Paul Menage817929e2007-10-18 23:39:36 -0700415 /* First see if we already have a cgroup group that matches
416 * the desired set */
417 write_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700418 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700419 if (res)
420 get_css_set(res);
421 write_unlock(&css_set_lock);
422
423 if (res)
424 return res;
425
426 res = kmalloc(sizeof(*res), GFP_KERNEL);
427 if (!res)
428 return NULL;
429
430 /* Allocate all the cg_cgroup_link objects that we'll need */
431 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
432 kfree(res);
433 return NULL;
434 }
435
436 kref_init(&res->ref);
437 INIT_LIST_HEAD(&res->cg_links);
438 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700439 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700440
441 /* Copy the set of subsystem state objects generated in
442 * find_existing_css_set() */
443 memcpy(res->subsys, template, sizeof(res->subsys));
444
445 write_lock(&css_set_lock);
446 /* Add reference counts and links from the new css_set. */
447 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700448 struct cgroup *cgrp = res->subsys[i]->cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700449 struct cgroup_subsys *ss = subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -0700450 atomic_inc(&cgrp->count);
Paul Menage817929e2007-10-18 23:39:36 -0700451 /*
452 * We want to add a link once per cgroup, so we
453 * only do it for the first subsystem in each
454 * hierarchy
455 */
456 if (ss->root->subsys_list.next == &ss->sibling) {
457 BUG_ON(list_empty(&tmp_cg_links));
458 link = list_entry(tmp_cg_links.next,
459 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700460 cgrp_link_list);
461 list_del(&link->cgrp_link_list);
462 list_add(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage817929e2007-10-18 23:39:36 -0700463 link->cg = res;
464 list_add(&link->cg_link_list, &res->cg_links);
465 }
466 }
467 if (list_empty(&rootnode.subsys_list)) {
468 link = list_entry(tmp_cg_links.next,
469 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700470 cgrp_link_list);
471 list_del(&link->cgrp_link_list);
472 list_add(&link->cgrp_link_list, &dummytop->css_sets);
Paul Menage817929e2007-10-18 23:39:36 -0700473 link->cg = res;
474 list_add(&link->cg_link_list, &res->cg_links);
475 }
476
477 BUG_ON(!list_empty(&tmp_cg_links));
478
Paul Menage817929e2007-10-18 23:39:36 -0700479 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700480
481 /* Add this cgroup group to the hash table */
482 hhead = css_set_hash(res->subsys);
483 hlist_add_head(&res->hlist, hhead);
484
Paul Menage817929e2007-10-18 23:39:36 -0700485 write_unlock(&css_set_lock);
486
487 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700488}
489
Paul Menageddbcc7e2007-10-18 23:39:30 -0700490/*
491 * There is one global cgroup mutex. We also require taking
492 * task_lock() when dereferencing a task's cgroup subsys pointers.
493 * See "The task_lock() exception", at the end of this comment.
494 *
495 * A task must hold cgroup_mutex to modify cgroups.
496 *
497 * Any task can increment and decrement the count field without lock.
498 * So in general, code holding cgroup_mutex can't rely on the count
499 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800500 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700501 * means that no tasks are currently attached, therefore there is no
502 * way a task attached to that cgroup can fork (the other way to
503 * increment the count). So code holding cgroup_mutex can safely
504 * assume that if the count is zero, it will stay zero. Similarly, if
505 * a task holds cgroup_mutex on a cgroup with zero count, it
506 * knows that the cgroup won't be removed, as cgroup_rmdir()
507 * needs that mutex.
508 *
509 * The cgroup_common_file_write handler for operations that modify
510 * the cgroup hierarchy holds cgroup_mutex across the entire operation,
511 * single threading all such cgroup modifications across the system.
512 *
513 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
514 * (usually) take cgroup_mutex. These are the two most performance
515 * critical pieces of code here. The exception occurs on cgroup_exit(),
516 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
517 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800518 * to the release agent with the name of the cgroup (path relative to
519 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700520 *
521 * A cgroup can only be deleted if both its 'count' of using tasks
522 * is zero, and its list of 'children' cgroups is empty. Since all
523 * tasks in the system use _some_ cgroup, and since there is always at
524 * least one task in the system (init, pid == 1), therefore, top_cgroup
525 * always has either children cgroups and/or using tasks. So we don't
526 * need a special hack to ensure that top_cgroup cannot be deleted.
527 *
528 * The task_lock() exception
529 *
530 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800531 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800532 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700533 * several performance critical places that need to reference
534 * task->cgroup without the expense of grabbing a system global
535 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800536 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700537 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
538 * the task_struct routinely used for such matters.
539 *
540 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800541 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700542 */
543
Paul Menageddbcc7e2007-10-18 23:39:30 -0700544/**
545 * cgroup_lock - lock out any changes to cgroup structures
546 *
547 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700548void cgroup_lock(void)
549{
550 mutex_lock(&cgroup_mutex);
551}
552
553/**
554 * cgroup_unlock - release lock on cgroup changes
555 *
556 * Undo the lock taken in a previous cgroup_lock() call.
557 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700558void cgroup_unlock(void)
559{
560 mutex_unlock(&cgroup_mutex);
561}
562
563/*
564 * A couple of forward declarations required, due to cyclic reference loop:
565 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
566 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
567 * -> cgroup_mkdir.
568 */
569
570static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
571static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700572static int cgroup_populate_dir(struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700573static struct inode_operations cgroup_dir_inode_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700574static struct file_operations proc_cgroupstats_operations;
575
576static struct backing_dev_info cgroup_backing_dev_info = {
577 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
578};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700579
580static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
581{
582 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700583
584 if (inode) {
585 inode->i_mode = mode;
586 inode->i_uid = current->fsuid;
587 inode->i_gid = current->fsgid;
588 inode->i_blocks = 0;
589 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
590 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
591 }
592 return inode;
593}
594
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800595/*
596 * Call subsys's pre_destroy handler.
597 * This is called before css refcnt check.
598 */
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800599static void cgroup_call_pre_destroy(struct cgroup *cgrp)
600{
601 struct cgroup_subsys *ss;
602 for_each_subsys(cgrp->root, ss)
603 if (ss->pre_destroy && cgrp->subsys[ss->subsys_id])
604 ss->pre_destroy(ss, cgrp);
605 return;
606}
607
Paul Menageddbcc7e2007-10-18 23:39:30 -0700608static void cgroup_diput(struct dentry *dentry, struct inode *inode)
609{
610 /* is dentry a directory ? if so, kfree() associated cgroup */
611 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700612 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800613 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700614 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700615 /* It's possible for external users to be holding css
616 * reference counts on a cgroup; css_put() needs to
617 * be able to access the cgroup after decrementing
618 * the reference count in order to know if it needs to
619 * queue the cgroup to be handled by the release
620 * agent */
621 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800622
623 mutex_lock(&cgroup_mutex);
624 /*
625 * Release the subsystem state objects.
626 */
627 for_each_subsys(cgrp->root, ss) {
628 if (cgrp->subsys[ss->subsys_id])
629 ss->destroy(ss, cgrp);
630 }
631
632 cgrp->root->number_of_cgroups--;
633 mutex_unlock(&cgroup_mutex);
634
635 /* Drop the active superblock reference that we took when we
636 * created the cgroup */
637 deactivate_super(cgrp->root->sb);
638
Paul Menagebd89aab2007-10-18 23:40:44 -0700639 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700640 }
641 iput(inode);
642}
643
644static void remove_dir(struct dentry *d)
645{
646 struct dentry *parent = dget(d->d_parent);
647
648 d_delete(d);
649 simple_rmdir(parent->d_inode, d);
650 dput(parent);
651}
652
653static void cgroup_clear_directory(struct dentry *dentry)
654{
655 struct list_head *node;
656
657 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
658 spin_lock(&dcache_lock);
659 node = dentry->d_subdirs.next;
660 while (node != &dentry->d_subdirs) {
661 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
662 list_del_init(node);
663 if (d->d_inode) {
664 /* This should never be called on a cgroup
665 * directory with child cgroups */
666 BUG_ON(d->d_inode->i_mode & S_IFDIR);
667 d = dget_locked(d);
668 spin_unlock(&dcache_lock);
669 d_delete(d);
670 simple_unlink(dentry->d_inode, d);
671 dput(d);
672 spin_lock(&dcache_lock);
673 }
674 node = dentry->d_subdirs.next;
675 }
676 spin_unlock(&dcache_lock);
677}
678
679/*
680 * NOTE : the dentry must have been dget()'ed
681 */
682static void cgroup_d_remove_dir(struct dentry *dentry)
683{
684 cgroup_clear_directory(dentry);
685
686 spin_lock(&dcache_lock);
687 list_del_init(&dentry->d_u.d_child);
688 spin_unlock(&dcache_lock);
689 remove_dir(dentry);
690}
691
692static int rebind_subsystems(struct cgroupfs_root *root,
693 unsigned long final_bits)
694{
695 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700696 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700697 int i;
698
699 removed_bits = root->actual_subsys_bits & ~final_bits;
700 added_bits = final_bits & ~root->actual_subsys_bits;
701 /* Check that any added subsystems are currently free */
702 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800703 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700704 struct cgroup_subsys *ss = subsys[i];
705 if (!(bit & added_bits))
706 continue;
707 if (ss->root != &rootnode) {
708 /* Subsystem isn't free */
709 return -EBUSY;
710 }
711 }
712
713 /* Currently we don't handle adding/removing subsystems when
714 * any child cgroups exist. This is theoretically supportable
715 * but involves complex error handling, so it's being left until
716 * later */
Paul Menagebd89aab2007-10-18 23:40:44 -0700717 if (!list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700718 return -EBUSY;
719
720 /* Process each subsystem */
721 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
722 struct cgroup_subsys *ss = subsys[i];
723 unsigned long bit = 1UL << i;
724 if (bit & added_bits) {
725 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700726 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700727 BUG_ON(!dummytop->subsys[i]);
728 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -0700729 cgrp->subsys[i] = dummytop->subsys[i];
730 cgrp->subsys[i]->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700731 list_add(&ss->sibling, &root->subsys_list);
732 rcu_assign_pointer(ss->root, root);
733 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700734 ss->bind(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700735
736 } else if (bit & removed_bits) {
737 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700738 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
739 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700740 if (ss->bind)
741 ss->bind(ss, dummytop);
742 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700743 cgrp->subsys[i] = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700744 rcu_assign_pointer(subsys[i]->root, &rootnode);
745 list_del(&ss->sibling);
746 } else if (bit & final_bits) {
747 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700748 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700749 } else {
750 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700751 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700752 }
753 }
754 root->subsys_bits = root->actual_subsys_bits = final_bits;
755 synchronize_rcu();
756
757 return 0;
758}
759
760static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
761{
762 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
763 struct cgroup_subsys *ss;
764
765 mutex_lock(&cgroup_mutex);
766 for_each_subsys(root, ss)
767 seq_printf(seq, ",%s", ss->name);
768 if (test_bit(ROOT_NOPREFIX, &root->flags))
769 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700770 if (strlen(root->release_agent_path))
771 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700772 mutex_unlock(&cgroup_mutex);
773 return 0;
774}
775
776struct cgroup_sb_opts {
777 unsigned long subsys_bits;
778 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700779 char *release_agent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700780};
781
782/* Convert a hierarchy specifier into a bitmask of subsystems and
783 * flags. */
784static int parse_cgroupfs_options(char *data,
785 struct cgroup_sb_opts *opts)
786{
787 char *token, *o = data ?: "all";
788
789 opts->subsys_bits = 0;
790 opts->flags = 0;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700791 opts->release_agent = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792
793 while ((token = strsep(&o, ",")) != NULL) {
794 if (!*token)
795 return -EINVAL;
796 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700797 /* Add all non-disabled subsystems */
798 int i;
799 opts->subsys_bits = 0;
800 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
801 struct cgroup_subsys *ss = subsys[i];
802 if (!ss->disabled)
803 opts->subsys_bits |= 1ul << i;
804 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700805 } else if (!strcmp(token, "noprefix")) {
806 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700807 } else if (!strncmp(token, "release_agent=", 14)) {
808 /* Specifying two release agents is forbidden */
809 if (opts->release_agent)
810 return -EINVAL;
811 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
812 if (!opts->release_agent)
813 return -ENOMEM;
814 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
815 opts->release_agent[PATH_MAX - 1] = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700816 } else {
817 struct cgroup_subsys *ss;
818 int i;
819 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
820 ss = subsys[i];
821 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700822 if (!ss->disabled)
823 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700824 break;
825 }
826 }
827 if (i == CGROUP_SUBSYS_COUNT)
828 return -ENOENT;
829 }
830 }
831
832 /* We can't have an empty hierarchy */
833 if (!opts->subsys_bits)
834 return -EINVAL;
835
836 return 0;
837}
838
839static int cgroup_remount(struct super_block *sb, int *flags, char *data)
840{
841 int ret = 0;
842 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -0700843 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700844 struct cgroup_sb_opts opts;
845
Paul Menagebd89aab2007-10-18 23:40:44 -0700846 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700847 mutex_lock(&cgroup_mutex);
848
849 /* See what subsystems are wanted */
850 ret = parse_cgroupfs_options(data, &opts);
851 if (ret)
852 goto out_unlock;
853
854 /* Don't allow flags to change at remount */
855 if (opts.flags != root->flags) {
856 ret = -EINVAL;
857 goto out_unlock;
858 }
859
860 ret = rebind_subsystems(root, opts.subsys_bits);
861
862 /* (re)populate subsystem files */
863 if (!ret)
Paul Menagebd89aab2007-10-18 23:40:44 -0700864 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700865
Paul Menage81a6a5c2007-10-18 23:39:38 -0700866 if (opts.release_agent)
867 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700868 out_unlock:
Paul Menage81a6a5c2007-10-18 23:39:38 -0700869 if (opts.release_agent)
870 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700871 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700872 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700873 return ret;
874}
875
876static struct super_operations cgroup_ops = {
877 .statfs = simple_statfs,
878 .drop_inode = generic_delete_inode,
879 .show_options = cgroup_show_options,
880 .remount_fs = cgroup_remount,
881};
882
883static void init_cgroup_root(struct cgroupfs_root *root)
884{
Paul Menagebd89aab2007-10-18 23:40:44 -0700885 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700886 INIT_LIST_HEAD(&root->subsys_list);
887 INIT_LIST_HEAD(&root->root_list);
888 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -0700889 cgrp->root = root;
890 cgrp->top_cgroup = cgrp;
891 INIT_LIST_HEAD(&cgrp->sibling);
892 INIT_LIST_HEAD(&cgrp->children);
893 INIT_LIST_HEAD(&cgrp->css_sets);
894 INIT_LIST_HEAD(&cgrp->release_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700895}
896
897static int cgroup_test_super(struct super_block *sb, void *data)
898{
899 struct cgroupfs_root *new = data;
900 struct cgroupfs_root *root = sb->s_fs_info;
901
902 /* First check subsystems */
903 if (new->subsys_bits != root->subsys_bits)
904 return 0;
905
906 /* Next check flags */
907 if (new->flags != root->flags)
908 return 0;
909
910 return 1;
911}
912
913static int cgroup_set_super(struct super_block *sb, void *data)
914{
915 int ret;
916 struct cgroupfs_root *root = data;
917
918 ret = set_anon_super(sb, NULL);
919 if (ret)
920 return ret;
921
922 sb->s_fs_info = root;
923 root->sb = sb;
924
925 sb->s_blocksize = PAGE_CACHE_SIZE;
926 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
927 sb->s_magic = CGROUP_SUPER_MAGIC;
928 sb->s_op = &cgroup_ops;
929
930 return 0;
931}
932
933static int cgroup_get_rootdir(struct super_block *sb)
934{
935 struct inode *inode =
936 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
937 struct dentry *dentry;
938
939 if (!inode)
940 return -ENOMEM;
941
Paul Menageddbcc7e2007-10-18 23:39:30 -0700942 inode->i_fop = &simple_dir_operations;
943 inode->i_op = &cgroup_dir_inode_operations;
944 /* directories start off with i_nlink == 2 (for "." entry) */
945 inc_nlink(inode);
946 dentry = d_alloc_root(inode);
947 if (!dentry) {
948 iput(inode);
949 return -ENOMEM;
950 }
951 sb->s_root = dentry;
952 return 0;
953}
954
955static int cgroup_get_sb(struct file_system_type *fs_type,
956 int flags, const char *unused_dev_name,
957 void *data, struct vfsmount *mnt)
958{
959 struct cgroup_sb_opts opts;
960 int ret = 0;
961 struct super_block *sb;
962 struct cgroupfs_root *root;
Li Zefan28fd5df2008-04-29 01:00:13 -0700963 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700964 INIT_LIST_HEAD(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700965
966 /* First find the desired set of subsystems */
967 ret = parse_cgroupfs_options(data, &opts);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700968 if (ret) {
969 if (opts.release_agent)
970 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700971 return ret;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700972 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973
974 root = kzalloc(sizeof(*root), GFP_KERNEL);
Li Zefanf7770732008-02-23 15:24:10 -0800975 if (!root) {
976 if (opts.release_agent)
977 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700978 return -ENOMEM;
Li Zefanf7770732008-02-23 15:24:10 -0800979 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980
981 init_cgroup_root(root);
982 root->subsys_bits = opts.subsys_bits;
983 root->flags = opts.flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700984 if (opts.release_agent) {
985 strcpy(root->release_agent_path, opts.release_agent);
986 kfree(opts.release_agent);
987 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700988
989 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
990
991 if (IS_ERR(sb)) {
992 kfree(root);
993 return PTR_ERR(sb);
994 }
995
996 if (sb->s_fs_info != root) {
997 /* Reusing an existing superblock */
998 BUG_ON(sb->s_root == NULL);
999 kfree(root);
1000 root = NULL;
1001 } else {
1002 /* New superblock */
Paul Menagebd89aab2007-10-18 23:40:44 -07001003 struct cgroup *cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001004 struct inode *inode;
Li Zefan28fd5df2008-04-29 01:00:13 -07001005 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006
1007 BUG_ON(sb->s_root != NULL);
1008
1009 ret = cgroup_get_rootdir(sb);
1010 if (ret)
1011 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001012 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013
Paul Menage817929e2007-10-18 23:39:36 -07001014 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015 mutex_lock(&cgroup_mutex);
1016
Paul Menage817929e2007-10-18 23:39:36 -07001017 /*
1018 * We're accessing css_set_count without locking
1019 * css_set_lock here, but that's OK - it can only be
1020 * increased by someone holding cgroup_lock, and
1021 * that's us. The worst that can happen is that we
1022 * have some link structures left over
1023 */
1024 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1025 if (ret) {
1026 mutex_unlock(&cgroup_mutex);
1027 mutex_unlock(&inode->i_mutex);
1028 goto drop_new_super;
1029 }
1030
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031 ret = rebind_subsystems(root, root->subsys_bits);
1032 if (ret == -EBUSY) {
1033 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001034 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 goto drop_new_super;
1036 }
1037
1038 /* EBUSY should be the only error here */
1039 BUG_ON(ret);
1040
1041 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001042 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043
1044 sb->s_root->d_fsdata = &root->top_cgroup;
1045 root->top_cgroup.dentry = sb->s_root;
1046
Paul Menage817929e2007-10-18 23:39:36 -07001047 /* Link the top cgroup in this hierarchy into all
1048 * the css_set objects */
1049 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001050 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1051 struct hlist_head *hhead = &css_set_table[i];
1052 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001053 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001054
1055 hlist_for_each_entry(cg, node, hhead, hlist) {
1056 struct cg_cgroup_link *link;
1057
1058 BUG_ON(list_empty(&tmp_cg_links));
1059 link = list_entry(tmp_cg_links.next,
1060 struct cg_cgroup_link,
1061 cgrp_link_list);
1062 list_del(&link->cgrp_link_list);
1063 link->cg = cg;
1064 list_add(&link->cgrp_link_list,
1065 &root->top_cgroup.css_sets);
1066 list_add(&link->cg_link_list, &cg->cg_links);
1067 }
1068 }
Paul Menage817929e2007-10-18 23:39:36 -07001069 write_unlock(&css_set_lock);
1070
1071 free_cg_links(&tmp_cg_links);
1072
Paul Menagebd89aab2007-10-18 23:40:44 -07001073 BUG_ON(!list_empty(&cgrp->sibling));
1074 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 BUG_ON(root->number_of_cgroups != 1);
1076
Paul Menagebd89aab2007-10-18 23:40:44 -07001077 cgroup_populate_dir(cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001078 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079 mutex_unlock(&cgroup_mutex);
1080 }
1081
1082 return simple_set_mnt(mnt, sb);
1083
1084 drop_new_super:
1085 up_write(&sb->s_umount);
1086 deactivate_super(sb);
Paul Menage817929e2007-10-18 23:39:36 -07001087 free_cg_links(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 return ret;
1089}
1090
1091static void cgroup_kill_sb(struct super_block *sb) {
1092 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001093 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 int ret;
1095
1096 BUG_ON(!root);
1097
1098 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001099 BUG_ON(!list_empty(&cgrp->children));
1100 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101
1102 mutex_lock(&cgroup_mutex);
1103
1104 /* Rebind all subsystems back to the default hierarchy */
1105 ret = rebind_subsystems(root, 0);
1106 /* Shouldn't be able to fail ... */
1107 BUG_ON(ret);
1108
Paul Menage817929e2007-10-18 23:39:36 -07001109 /*
1110 * Release all the links from css_sets to this hierarchy's
1111 * root cgroup
1112 */
1113 write_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001114 while (!list_empty(&cgrp->css_sets)) {
Paul Menage817929e2007-10-18 23:39:36 -07001115 struct cg_cgroup_link *link;
Paul Menagebd89aab2007-10-18 23:40:44 -07001116 link = list_entry(cgrp->css_sets.next,
1117 struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001118 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001119 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001120 kfree(link);
1121 }
1122 write_unlock(&css_set_lock);
1123
1124 if (!list_empty(&root->root_list)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125 list_del(&root->root_list);
Paul Menage817929e2007-10-18 23:39:36 -07001126 root_count--;
1127 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128 mutex_unlock(&cgroup_mutex);
1129
1130 kfree(root);
1131 kill_litter_super(sb);
1132}
1133
1134static struct file_system_type cgroup_fs_type = {
1135 .name = "cgroup",
1136 .get_sb = cgroup_get_sb,
1137 .kill_sb = cgroup_kill_sb,
1138};
1139
Paul Menagebd89aab2007-10-18 23:40:44 -07001140static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001141{
1142 return dentry->d_fsdata;
1143}
1144
1145static inline struct cftype *__d_cft(struct dentry *dentry)
1146{
1147 return dentry->d_fsdata;
1148}
1149
Li Zefana043e3b2008-02-23 15:24:09 -08001150/**
1151 * cgroup_path - generate the path of a cgroup
1152 * @cgrp: the cgroup in question
1153 * @buf: the buffer to write the path into
1154 * @buflen: the length of the buffer
1155 *
1156 * Called with cgroup_mutex held. Writes path of cgroup into buf.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001157 * Returns 0 on success, -errno on error.
1158 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001159int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001160{
1161 char *start;
1162
Paul Menagebd89aab2007-10-18 23:40:44 -07001163 if (cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001164 /*
1165 * Inactive subsystems have no dentry for their root
1166 * cgroup
1167 */
1168 strcpy(buf, "/");
1169 return 0;
1170 }
1171
1172 start = buf + buflen;
1173
1174 *--start = '\0';
1175 for (;;) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001176 int len = cgrp->dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001177 if ((start -= len) < buf)
1178 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001179 memcpy(start, cgrp->dentry->d_name.name, len);
1180 cgrp = cgrp->parent;
1181 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001182 break;
Paul Menagebd89aab2007-10-18 23:40:44 -07001183 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001184 continue;
1185 if (--start < buf)
1186 return -ENAMETOOLONG;
1187 *start = '/';
1188 }
1189 memmove(buf, start, buf + buflen - start);
1190 return 0;
1191}
1192
Paul Menagebbcb81d2007-10-18 23:39:32 -07001193/*
1194 * Return the first subsystem attached to a cgroup's hierarchy, and
1195 * its subsystem id.
1196 */
1197
Paul Menagebd89aab2007-10-18 23:40:44 -07001198static void get_first_subsys(const struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001199 struct cgroup_subsys_state **css, int *subsys_id)
1200{
Paul Menagebd89aab2007-10-18 23:40:44 -07001201 const struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001202 const struct cgroup_subsys *test_ss;
1203 BUG_ON(list_empty(&root->subsys_list));
1204 test_ss = list_entry(root->subsys_list.next,
1205 struct cgroup_subsys, sibling);
1206 if (css) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001207 *css = cgrp->subsys[test_ss->subsys_id];
Paul Menagebbcb81d2007-10-18 23:39:32 -07001208 BUG_ON(!*css);
1209 }
1210 if (subsys_id)
1211 *subsys_id = test_ss->subsys_id;
1212}
1213
Li Zefana043e3b2008-02-23 15:24:09 -08001214/**
1215 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1216 * @cgrp: the cgroup the task is attaching to
1217 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001218 *
Li Zefana043e3b2008-02-23 15:24:09 -08001219 * Call holding cgroup_mutex. May take task_lock of
1220 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001221 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001222int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001223{
1224 int retval = 0;
1225 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07001226 struct cgroup *oldcgrp;
Paul Menage817929e2007-10-18 23:39:36 -07001227 struct css_set *cg = tsk->cgroups;
1228 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001229 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001230 int subsys_id;
1231
Paul Menagebd89aab2007-10-18 23:40:44 -07001232 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001233
1234 /* Nothing to do if the task is already in that cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -07001235 oldcgrp = task_cgroup(tsk, subsys_id);
1236 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001237 return 0;
1238
1239 for_each_subsys(root, ss) {
1240 if (ss->can_attach) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001241 retval = ss->can_attach(ss, cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001242 if (retval)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001243 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001244 }
1245 }
1246
Paul Menage817929e2007-10-18 23:39:36 -07001247 /*
1248 * Locate or allocate a new css_set for this task,
1249 * based on its final set of cgroups
1250 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001251 newcg = find_css_set(cg, cgrp);
Paul Jacksone18f6312008-02-07 00:13:44 -08001252 if (!newcg)
Paul Menage817929e2007-10-18 23:39:36 -07001253 return -ENOMEM;
Paul Menage817929e2007-10-18 23:39:36 -07001254
Paul Menagebbcb81d2007-10-18 23:39:32 -07001255 task_lock(tsk);
1256 if (tsk->flags & PF_EXITING) {
1257 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001258 put_css_set(newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001259 return -ESRCH;
1260 }
Paul Menage817929e2007-10-18 23:39:36 -07001261 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001262 task_unlock(tsk);
1263
Paul Menage817929e2007-10-18 23:39:36 -07001264 /* Update the css_set linked lists if we're using them */
1265 write_lock(&css_set_lock);
1266 if (!list_empty(&tsk->cg_list)) {
1267 list_del(&tsk->cg_list);
1268 list_add(&tsk->cg_list, &newcg->tasks);
1269 }
1270 write_unlock(&css_set_lock);
1271
Paul Menagebbcb81d2007-10-18 23:39:32 -07001272 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001273 if (ss->attach)
Paul Menagebd89aab2007-10-18 23:40:44 -07001274 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001275 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001276 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001277 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001278 put_css_set(cg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001279 return 0;
1280}
1281
1282/*
Paul Menagebd89aab2007-10-18 23:40:44 -07001283 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with
Paul Menagebbcb81d2007-10-18 23:39:32 -07001284 * cgroup_mutex, may take task_lock of task
1285 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001286static int attach_task_by_pid(struct cgroup *cgrp, char *pidbuf)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001287{
1288 pid_t pid;
1289 struct task_struct *tsk;
1290 int ret;
1291
1292 if (sscanf(pidbuf, "%d", &pid) != 1)
1293 return -EIO;
1294
1295 if (pid) {
1296 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001297 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001298 if (!tsk || tsk->flags & PF_EXITING) {
1299 rcu_read_unlock();
1300 return -ESRCH;
1301 }
1302 get_task_struct(tsk);
1303 rcu_read_unlock();
1304
1305 if ((current->euid) && (current->euid != tsk->uid)
1306 && (current->euid != tsk->suid)) {
1307 put_task_struct(tsk);
1308 return -EACCES;
1309 }
1310 } else {
1311 tsk = current;
1312 get_task_struct(tsk);
1313 }
1314
Cliff Wickman956db3c2008-02-07 00:14:43 -08001315 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001316 put_task_struct(tsk);
1317 return ret;
1318}
1319
Paul Menageddbcc7e2007-10-18 23:39:30 -07001320/* The various types of files and directories in a cgroup file system */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001321enum cgroup_filetype {
1322 FILE_ROOT,
1323 FILE_DIR,
1324 FILE_TASKLIST,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001325 FILE_NOTIFY_ON_RELEASE,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001326 FILE_RELEASE_AGENT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001327};
1328
Paul Menagee73d2c62008-04-29 01:00:06 -07001329static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001330 struct file *file,
1331 const char __user *userbuf,
1332 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001333{
1334 char buffer[64];
1335 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001336 char *end;
1337
1338 if (!nbytes)
1339 return -EINVAL;
1340 if (nbytes >= sizeof(buffer))
1341 return -E2BIG;
1342 if (copy_from_user(buffer, userbuf, nbytes))
1343 return -EFAULT;
1344
1345 buffer[nbytes] = 0; /* nul-terminate */
Paul Menageb7269df2008-04-29 00:59:59 -07001346 strstrip(buffer);
Paul Menagee73d2c62008-04-29 01:00:06 -07001347 if (cft->write_u64) {
1348 u64 val = simple_strtoull(buffer, &end, 0);
1349 if (*end)
1350 return -EINVAL;
1351 retval = cft->write_u64(cgrp, cft, val);
1352 } else {
1353 s64 val = simple_strtoll(buffer, &end, 0);
1354 if (*end)
1355 return -EINVAL;
1356 retval = cft->write_s64(cgrp, cft, val);
1357 }
Paul Menage355e0c42007-10-18 23:39:33 -07001358 if (!retval)
1359 retval = nbytes;
1360 return retval;
1361}
1362
Paul Menagebd89aab2007-10-18 23:40:44 -07001363static ssize_t cgroup_common_file_write(struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001364 struct cftype *cft,
1365 struct file *file,
1366 const char __user *userbuf,
1367 size_t nbytes, loff_t *unused_ppos)
1368{
1369 enum cgroup_filetype type = cft->private;
1370 char *buffer;
1371 int retval = 0;
1372
1373 if (nbytes >= PATH_MAX)
1374 return -E2BIG;
1375
1376 /* +1 for nul-terminator */
1377 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1378 if (buffer == NULL)
1379 return -ENOMEM;
1380
1381 if (copy_from_user(buffer, userbuf, nbytes)) {
1382 retval = -EFAULT;
1383 goto out1;
1384 }
1385 buffer[nbytes] = 0; /* nul-terminate */
Paul Jackson622d42c2008-02-07 00:13:44 -08001386 strstrip(buffer); /* strip -just- trailing whitespace */
Paul Menagebbcb81d2007-10-18 23:39:32 -07001387
1388 mutex_lock(&cgroup_mutex);
1389
Paul Menage8dc4f3e2008-02-07 00:13:45 -08001390 /*
1391 * This was already checked for in cgroup_file_write(), but
1392 * check again now we're holding cgroup_mutex.
1393 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001394 if (cgroup_is_removed(cgrp)) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07001395 retval = -ENODEV;
1396 goto out2;
1397 }
1398
1399 switch (type) {
1400 case FILE_TASKLIST:
Paul Menagebd89aab2007-10-18 23:40:44 -07001401 retval = attach_task_by_pid(cgrp, buffer);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001402 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001403 case FILE_NOTIFY_ON_RELEASE:
Paul Menagebd89aab2007-10-18 23:40:44 -07001404 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001405 if (simple_strtoul(buffer, NULL, 10) != 0)
Paul Menagebd89aab2007-10-18 23:40:44 -07001406 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001407 else
Paul Menagebd89aab2007-10-18 23:40:44 -07001408 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001409 break;
1410 case FILE_RELEASE_AGENT:
Paul Jackson622d42c2008-02-07 00:13:44 -08001411 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1412 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001413 break;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001414 default:
1415 retval = -EINVAL;
1416 goto out2;
1417 }
1418
1419 if (retval == 0)
1420 retval = nbytes;
1421out2:
1422 mutex_unlock(&cgroup_mutex);
1423out1:
1424 kfree(buffer);
1425 return retval;
1426}
1427
Paul Menageddbcc7e2007-10-18 23:39:30 -07001428static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1429 size_t nbytes, loff_t *ppos)
1430{
1431 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001432 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001433
Paul Menage8dc4f3e2008-02-07 00:13:45 -08001434 if (!cft || cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001435 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001436 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001437 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001438 if (cft->write_u64 || cft->write_s64)
1439 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001440 if (cft->trigger) {
1441 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1442 return ret ? ret : nbytes;
1443 }
Paul Menage355e0c42007-10-18 23:39:33 -07001444 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001445}
1446
Paul Menagef4c753b2008-04-29 00:59:56 -07001447static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1448 struct file *file,
1449 char __user *buf, size_t nbytes,
1450 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001451{
1452 char tmp[64];
Paul Menagef4c753b2008-04-29 00:59:56 -07001453 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1455
1456 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1457}
1458
Paul Menagee73d2c62008-04-29 01:00:06 -07001459static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1460 struct file *file,
1461 char __user *buf, size_t nbytes,
1462 loff_t *ppos)
1463{
1464 char tmp[64];
1465 s64 val = cft->read_s64(cgrp, cft);
1466 int len = sprintf(tmp, "%lld\n", (long long) val);
1467
1468 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1469}
1470
Paul Menagebd89aab2007-10-18 23:40:44 -07001471static ssize_t cgroup_common_file_read(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001472 struct cftype *cft,
1473 struct file *file,
1474 char __user *buf,
1475 size_t nbytes, loff_t *ppos)
1476{
1477 enum cgroup_filetype type = cft->private;
1478 char *page;
1479 ssize_t retval = 0;
1480 char *s;
1481
1482 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
1483 return -ENOMEM;
1484
1485 s = page;
1486
1487 switch (type) {
1488 case FILE_RELEASE_AGENT:
1489 {
1490 struct cgroupfs_root *root;
1491 size_t n;
1492 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001493 root = cgrp->root;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001494 n = strnlen(root->release_agent_path,
1495 sizeof(root->release_agent_path));
1496 n = min(n, (size_t) PAGE_SIZE);
1497 strncpy(s, root->release_agent_path, n);
1498 mutex_unlock(&cgroup_mutex);
1499 s += n;
1500 break;
1501 }
1502 default:
1503 retval = -EINVAL;
1504 goto out;
1505 }
1506 *s++ = '\n';
1507
1508 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
1509out:
1510 free_page((unsigned long)page);
1511 return retval;
1512}
1513
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1515 size_t nbytes, loff_t *ppos)
1516{
1517 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001518 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001519
Paul Menage8dc4f3e2008-02-07 00:13:45 -08001520 if (!cft || cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521 return -ENODEV;
1522
1523 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001524 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001525 if (cft->read_u64)
1526 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001527 if (cft->read_s64)
1528 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001529 return -EINVAL;
1530}
1531
Paul Menage91796562008-04-29 01:00:01 -07001532/*
1533 * seqfile ops/methods for returning structured data. Currently just
1534 * supports string->u64 maps, but can be extended in future.
1535 */
1536
1537struct cgroup_seqfile_state {
1538 struct cftype *cft;
1539 struct cgroup *cgroup;
1540};
1541
1542static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1543{
1544 struct seq_file *sf = cb->state;
1545 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1546}
1547
1548static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1549{
1550 struct cgroup_seqfile_state *state = m->private;
1551 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001552 if (cft->read_map) {
1553 struct cgroup_map_cb cb = {
1554 .fill = cgroup_map_add,
1555 .state = m,
1556 };
1557 return cft->read_map(state->cgroup, cft, &cb);
1558 }
1559 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001560}
1561
1562int cgroup_seqfile_release(struct inode *inode, struct file *file)
1563{
1564 struct seq_file *seq = file->private_data;
1565 kfree(seq->private);
1566 return single_release(inode, file);
1567}
1568
1569static struct file_operations cgroup_seqfile_operations = {
1570 .read = seq_read,
1571 .llseek = seq_lseek,
1572 .release = cgroup_seqfile_release,
1573};
1574
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575static int cgroup_file_open(struct inode *inode, struct file *file)
1576{
1577 int err;
1578 struct cftype *cft;
1579
1580 err = generic_file_open(inode, file);
1581 if (err)
1582 return err;
1583
1584 cft = __d_cft(file->f_dentry);
1585 if (!cft)
1586 return -ENODEV;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001587 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001588 struct cgroup_seqfile_state *state =
1589 kzalloc(sizeof(*state), GFP_USER);
1590 if (!state)
1591 return -ENOMEM;
1592 state->cft = cft;
1593 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1594 file->f_op = &cgroup_seqfile_operations;
1595 err = single_open(file, cgroup_seqfile_show, state);
1596 if (err < 0)
1597 kfree(state);
1598 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001599 err = cft->open(inode, file);
1600 else
1601 err = 0;
1602
1603 return err;
1604}
1605
1606static int cgroup_file_release(struct inode *inode, struct file *file)
1607{
1608 struct cftype *cft = __d_cft(file->f_dentry);
1609 if (cft->release)
1610 return cft->release(inode, file);
1611 return 0;
1612}
1613
1614/*
1615 * cgroup_rename - Only allow simple rename of directories in place.
1616 */
1617static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1618 struct inode *new_dir, struct dentry *new_dentry)
1619{
1620 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1621 return -ENOTDIR;
1622 if (new_dentry->d_inode)
1623 return -EEXIST;
1624 if (old_dir != new_dir)
1625 return -EIO;
1626 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1627}
1628
1629static struct file_operations cgroup_file_operations = {
1630 .read = cgroup_file_read,
1631 .write = cgroup_file_write,
1632 .llseek = generic_file_llseek,
1633 .open = cgroup_file_open,
1634 .release = cgroup_file_release,
1635};
1636
1637static struct inode_operations cgroup_dir_inode_operations = {
1638 .lookup = simple_lookup,
1639 .mkdir = cgroup_mkdir,
1640 .rmdir = cgroup_rmdir,
1641 .rename = cgroup_rename,
1642};
1643
1644static int cgroup_create_file(struct dentry *dentry, int mode,
1645 struct super_block *sb)
1646{
1647 static struct dentry_operations cgroup_dops = {
1648 .d_iput = cgroup_diput,
1649 };
1650
1651 struct inode *inode;
1652
1653 if (!dentry)
1654 return -ENOENT;
1655 if (dentry->d_inode)
1656 return -EEXIST;
1657
1658 inode = cgroup_new_inode(mode, sb);
1659 if (!inode)
1660 return -ENOMEM;
1661
1662 if (S_ISDIR(mode)) {
1663 inode->i_op = &cgroup_dir_inode_operations;
1664 inode->i_fop = &simple_dir_operations;
1665
1666 /* start off with i_nlink == 2 (for "." entry) */
1667 inc_nlink(inode);
1668
1669 /* start with the directory inode held, so that we can
1670 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07001671 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672 } else if (S_ISREG(mode)) {
1673 inode->i_size = 0;
1674 inode->i_fop = &cgroup_file_operations;
1675 }
1676 dentry->d_op = &cgroup_dops;
1677 d_instantiate(dentry, inode);
1678 dget(dentry); /* Extra count - pin the dentry in core */
1679 return 0;
1680}
1681
1682/*
Li Zefana043e3b2008-02-23 15:24:09 -08001683 * cgroup_create_dir - create a directory for an object.
1684 * @cgrp: the cgroup we create the directory for. It must have a valid
1685 * ->parent field. And we are going to fill its ->dentry field.
1686 * @dentry: dentry of the new cgroup
1687 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001688 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001689static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001690 int mode)
1691{
1692 struct dentry *parent;
1693 int error = 0;
1694
Paul Menagebd89aab2007-10-18 23:40:44 -07001695 parent = cgrp->parent->dentry;
1696 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001697 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001698 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001699 inc_nlink(parent->d_inode);
Paul Menagebd89aab2007-10-18 23:40:44 -07001700 cgrp->dentry = dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701 dget(dentry);
1702 }
1703 dput(dentry);
1704
1705 return error;
1706}
1707
Paul Menagebd89aab2007-10-18 23:40:44 -07001708int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001709 struct cgroup_subsys *subsys,
1710 const struct cftype *cft)
1711{
Paul Menagebd89aab2007-10-18 23:40:44 -07001712 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001713 struct dentry *dentry;
1714 int error;
1715
1716 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07001717 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718 strcpy(name, subsys->name);
1719 strcat(name, ".");
1720 }
1721 strcat(name, cft->name);
1722 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1723 dentry = lookup_one_len(name, dir, strlen(name));
1724 if (!IS_ERR(dentry)) {
1725 error = cgroup_create_file(dentry, 0644 | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07001726 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727 if (!error)
1728 dentry->d_fsdata = (void *)cft;
1729 dput(dentry);
1730 } else
1731 error = PTR_ERR(dentry);
1732 return error;
1733}
1734
Paul Menagebd89aab2007-10-18 23:40:44 -07001735int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736 struct cgroup_subsys *subsys,
1737 const struct cftype cft[],
1738 int count)
1739{
1740 int i, err;
1741 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001742 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001743 if (err)
1744 return err;
1745 }
1746 return 0;
1747}
1748
Li Zefana043e3b2008-02-23 15:24:09 -08001749/**
1750 * cgroup_task_count - count the number of tasks in a cgroup.
1751 * @cgrp: the cgroup in question
1752 *
1753 * Return the number of tasks in the cgroup.
1754 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001755int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001756{
1757 int count = 0;
Paul Menage817929e2007-10-18 23:39:36 -07001758 struct list_head *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001759
Paul Menage817929e2007-10-18 23:39:36 -07001760 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001761 l = cgrp->css_sets.next;
1762 while (l != &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001763 struct cg_cgroup_link *link =
Paul Menagebd89aab2007-10-18 23:40:44 -07001764 list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001765 count += atomic_read(&link->cg->ref.refcount);
1766 l = l->next;
1767 }
1768 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001769 return count;
1770}
1771
1772/*
Paul Menage817929e2007-10-18 23:39:36 -07001773 * Advance a list_head iterator. The iterator should be positioned at
1774 * the start of a css_set
1775 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001776static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001777 struct cgroup_iter *it)
1778{
1779 struct list_head *l = it->cg_link;
1780 struct cg_cgroup_link *link;
1781 struct css_set *cg;
1782
1783 /* Advance to the next non-empty css_set */
1784 do {
1785 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07001786 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001787 it->cg_link = NULL;
1788 return;
1789 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001790 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001791 cg = link->cg;
1792 } while (list_empty(&cg->tasks));
1793 it->cg_link = l;
1794 it->task = cg->tasks.next;
1795}
1796
Cliff Wickman31a7df02008-02-07 00:14:42 -08001797/*
1798 * To reduce the fork() overhead for systems that are not actually
1799 * using their cgroups capability, we don't maintain the lists running
1800 * through each css_set to its tasks until we see the list actually
1801 * used - in other words after the first call to cgroup_iter_start().
1802 *
1803 * The tasklist_lock is not held here, as do_each_thread() and
1804 * while_each_thread() are protected by RCU.
1805 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07001806static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08001807{
1808 struct task_struct *p, *g;
1809 write_lock(&css_set_lock);
1810 use_task_css_set_links = 1;
1811 do_each_thread(g, p) {
1812 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08001813 /*
1814 * We should check if the process is exiting, otherwise
1815 * it will race with cgroup_exit() in that the list
1816 * entry won't be deleted though the process has exited.
1817 */
1818 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08001819 list_add(&p->cg_list, &p->cgroups->tasks);
1820 task_unlock(p);
1821 } while_each_thread(g, p);
1822 write_unlock(&css_set_lock);
1823}
1824
Paul Menagebd89aab2007-10-18 23:40:44 -07001825void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001826{
1827 /*
1828 * The first time anyone tries to iterate across a cgroup,
1829 * we need to enable the list linking each css_set to its
1830 * tasks, and fix up all existing tasks.
1831 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08001832 if (!use_task_css_set_links)
1833 cgroup_enable_task_cg_lists();
1834
Paul Menage817929e2007-10-18 23:39:36 -07001835 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001836 it->cg_link = &cgrp->css_sets;
1837 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001838}
1839
Paul Menagebd89aab2007-10-18 23:40:44 -07001840struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001841 struct cgroup_iter *it)
1842{
1843 struct task_struct *res;
1844 struct list_head *l = it->task;
1845
1846 /* If the iterator cg is NULL, we have no tasks */
1847 if (!it->cg_link)
1848 return NULL;
1849 res = list_entry(l, struct task_struct, cg_list);
1850 /* Advance iterator to find next entry */
1851 l = l->next;
1852 if (l == &res->cgroups->tasks) {
1853 /* We reached the end of this task list - move on to
1854 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07001855 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001856 } else {
1857 it->task = l;
1858 }
1859 return res;
1860}
1861
Paul Menagebd89aab2007-10-18 23:40:44 -07001862void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001863{
1864 read_unlock(&css_set_lock);
1865}
1866
Cliff Wickman31a7df02008-02-07 00:14:42 -08001867static inline int started_after_time(struct task_struct *t1,
1868 struct timespec *time,
1869 struct task_struct *t2)
1870{
1871 int start_diff = timespec_compare(&t1->start_time, time);
1872 if (start_diff > 0) {
1873 return 1;
1874 } else if (start_diff < 0) {
1875 return 0;
1876 } else {
1877 /*
1878 * Arbitrarily, if two processes started at the same
1879 * time, we'll say that the lower pointer value
1880 * started first. Note that t2 may have exited by now
1881 * so this may not be a valid pointer any longer, but
1882 * that's fine - it still serves to distinguish
1883 * between two tasks started (effectively) simultaneously.
1884 */
1885 return t1 > t2;
1886 }
1887}
1888
1889/*
1890 * This function is a callback from heap_insert() and is used to order
1891 * the heap.
1892 * In this case we order the heap in descending task start time.
1893 */
1894static inline int started_after(void *p1, void *p2)
1895{
1896 struct task_struct *t1 = p1;
1897 struct task_struct *t2 = p2;
1898 return started_after_time(t1, &t2->start_time, t2);
1899}
1900
1901/**
1902 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1903 * @scan: struct cgroup_scanner containing arguments for the scan
1904 *
1905 * Arguments include pointers to callback functions test_task() and
1906 * process_task().
1907 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1908 * and if it returns true, call process_task() for it also.
1909 * The test_task pointer may be NULL, meaning always true (select all tasks).
1910 * Effectively duplicates cgroup_iter_{start,next,end}()
1911 * but does not lock css_set_lock for the call to process_task().
1912 * The struct cgroup_scanner may be embedded in any structure of the caller's
1913 * creation.
1914 * It is guaranteed that process_task() will act on every task that
1915 * is a member of the cgroup for the duration of this call. This
1916 * function may or may not call process_task() for tasks that exit
1917 * or move to a different cgroup during the call, or are forked or
1918 * move into the cgroup during the call.
1919 *
1920 * Note that test_task() may be called with locks held, and may in some
1921 * situations be called multiple times for the same task, so it should
1922 * be cheap.
1923 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1924 * pre-allocated and will be used for heap operations (and its "gt" member will
1925 * be overwritten), else a temporary heap will be used (allocation of which
1926 * may cause this function to fail).
1927 */
1928int cgroup_scan_tasks(struct cgroup_scanner *scan)
1929{
1930 int retval, i;
1931 struct cgroup_iter it;
1932 struct task_struct *p, *dropped;
1933 /* Never dereference latest_task, since it's not refcounted */
1934 struct task_struct *latest_task = NULL;
1935 struct ptr_heap tmp_heap;
1936 struct ptr_heap *heap;
1937 struct timespec latest_time = { 0, 0 };
1938
1939 if (scan->heap) {
1940 /* The caller supplied our heap and pre-allocated its memory */
1941 heap = scan->heap;
1942 heap->gt = &started_after;
1943 } else {
1944 /* We need to allocate our own heap memory */
1945 heap = &tmp_heap;
1946 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1947 if (retval)
1948 /* cannot allocate the heap */
1949 return retval;
1950 }
1951
1952 again:
1953 /*
1954 * Scan tasks in the cgroup, using the scanner's "test_task" callback
1955 * to determine which are of interest, and using the scanner's
1956 * "process_task" callback to process any of them that need an update.
1957 * Since we don't want to hold any locks during the task updates,
1958 * gather tasks to be processed in a heap structure.
1959 * The heap is sorted by descending task start time.
1960 * If the statically-sized heap fills up, we overflow tasks that
1961 * started later, and in future iterations only consider tasks that
1962 * started after the latest task in the previous pass. This
1963 * guarantees forward progress and that we don't miss any tasks.
1964 */
1965 heap->size = 0;
1966 cgroup_iter_start(scan->cg, &it);
1967 while ((p = cgroup_iter_next(scan->cg, &it))) {
1968 /*
1969 * Only affect tasks that qualify per the caller's callback,
1970 * if he provided one
1971 */
1972 if (scan->test_task && !scan->test_task(p, scan))
1973 continue;
1974 /*
1975 * Only process tasks that started after the last task
1976 * we processed
1977 */
1978 if (!started_after_time(p, &latest_time, latest_task))
1979 continue;
1980 dropped = heap_insert(heap, p);
1981 if (dropped == NULL) {
1982 /*
1983 * The new task was inserted; the heap wasn't
1984 * previously full
1985 */
1986 get_task_struct(p);
1987 } else if (dropped != p) {
1988 /*
1989 * The new task was inserted, and pushed out a
1990 * different task
1991 */
1992 get_task_struct(p);
1993 put_task_struct(dropped);
1994 }
1995 /*
1996 * Else the new task was newer than anything already in
1997 * the heap and wasn't inserted
1998 */
1999 }
2000 cgroup_iter_end(scan->cg, &it);
2001
2002 if (heap->size) {
2003 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002004 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002005 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002006 latest_time = q->start_time;
2007 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002008 }
2009 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002010 scan->process_task(q, scan);
2011 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002012 }
2013 /*
2014 * If we had to process any tasks at all, scan again
2015 * in case some of them were in the middle of forking
2016 * children that didn't get processed.
2017 * Not the most efficient way to do it, but it avoids
2018 * having to take callback_mutex in the fork path
2019 */
2020 goto again;
2021 }
2022 if (heap == &tmp_heap)
2023 heap_free(&tmp_heap);
2024 return 0;
2025}
2026
Paul Menage817929e2007-10-18 23:39:36 -07002027/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07002028 * Stuff for reading the 'tasks' file.
2029 *
2030 * Reading this file can return large amounts of data if a cgroup has
2031 * *lots* of attached tasks. So it may need several calls to read(),
2032 * but we cannot guarantee that the information we produce is correct
2033 * unless we produce it entirely atomically.
2034 *
2035 * Upon tasks file open(), a struct ctr_struct is allocated, that
2036 * will have a pointer to an array (also allocated here). The struct
2037 * ctr_struct * is stored in file->private_data. Its resources will
2038 * be freed by release() when the file is closed. The array is used
2039 * to sprintf the PIDs and then used by read().
2040 */
2041struct ctr_struct {
2042 char *buf;
2043 int bufsz;
2044};
2045
2046/*
2047 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
Paul Menagebd89aab2007-10-18 23:40:44 -07002048 * 'cgrp'. Return actual number of pids loaded. No need to
Paul Menagebbcb81d2007-10-18 23:39:32 -07002049 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2050 * read section, so the css_set can't go away, and is
2051 * immutable after creation.
2052 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002053static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002054{
2055 int n = 0;
Paul Menage817929e2007-10-18 23:39:36 -07002056 struct cgroup_iter it;
2057 struct task_struct *tsk;
Paul Menagebd89aab2007-10-18 23:40:44 -07002058 cgroup_iter_start(cgrp, &it);
2059 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Paul Menage817929e2007-10-18 23:39:36 -07002060 if (unlikely(n == npids))
2061 break;
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002062 pidarray[n++] = task_pid_vnr(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002063 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002064 cgroup_iter_end(cgrp, &it);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002065 return n;
2066}
2067
Balbir Singh846c7bb2007-10-18 23:39:44 -07002068/**
Li Zefana043e3b2008-02-23 15:24:09 -08002069 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002070 * @stats: cgroupstats to fill information into
2071 * @dentry: A dentry entry belonging to the cgroup for which stats have
2072 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002073 *
2074 * Build and fill cgroupstats so that taskstats can export it to user
2075 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002076 */
2077int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2078{
2079 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002080 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002081 struct cgroup_iter it;
2082 struct task_struct *tsk;
2083 /*
2084 * Validate dentry by checking the superblock operations
2085 */
2086 if (dentry->d_sb->s_op != &cgroup_ops)
2087 goto err;
2088
2089 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002090 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002091 rcu_read_lock();
2092
Paul Menagebd89aab2007-10-18 23:40:44 -07002093 cgroup_iter_start(cgrp, &it);
2094 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002095 switch (tsk->state) {
2096 case TASK_RUNNING:
2097 stats->nr_running++;
2098 break;
2099 case TASK_INTERRUPTIBLE:
2100 stats->nr_sleeping++;
2101 break;
2102 case TASK_UNINTERRUPTIBLE:
2103 stats->nr_uninterruptible++;
2104 break;
2105 case TASK_STOPPED:
2106 stats->nr_stopped++;
2107 break;
2108 default:
2109 if (delayacct_is_task_waiting_on_io(tsk))
2110 stats->nr_io_wait++;
2111 break;
2112 }
2113 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002114 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002115
2116 rcu_read_unlock();
2117err:
2118 return ret;
2119}
2120
Paul Menagebbcb81d2007-10-18 23:39:32 -07002121static int cmppid(const void *a, const void *b)
2122{
2123 return *(pid_t *)a - *(pid_t *)b;
2124}
2125
2126/*
2127 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
2128 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
2129 * count 'cnt' of how many chars would be written if buf were large enough.
2130 */
2131static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
2132{
2133 int cnt = 0;
2134 int i;
2135
2136 for (i = 0; i < npids; i++)
2137 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
2138 return cnt;
2139}
2140
2141/*
2142 * Handle an open on 'tasks' file. Prepare a buffer listing the
2143 * process id's of tasks currently attached to the cgroup being opened.
2144 *
2145 * Does not require any specific cgroup mutexes, and does not take any.
2146 */
2147static int cgroup_tasks_open(struct inode *unused, struct file *file)
2148{
Paul Menagebd89aab2007-10-18 23:40:44 -07002149 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002150 struct ctr_struct *ctr;
2151 pid_t *pidarray;
2152 int npids;
2153 char c;
2154
2155 if (!(file->f_mode & FMODE_READ))
2156 return 0;
2157
2158 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
2159 if (!ctr)
2160 goto err0;
2161
2162 /*
2163 * If cgroup gets more users after we read count, we won't have
2164 * enough space - tough. This race is indistinguishable to the
2165 * caller from the case that the additional cgroup users didn't
2166 * show up until sometime later on.
2167 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002168 npids = cgroup_task_count(cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002169 if (npids) {
2170 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2171 if (!pidarray)
2172 goto err1;
2173
Paul Menagebd89aab2007-10-18 23:40:44 -07002174 npids = pid_array_load(pidarray, npids, cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002175 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
2176
2177 /* Call pid_array_to_buf() twice, first just to get bufsz */
2178 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
2179 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
2180 if (!ctr->buf)
2181 goto err2;
2182 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
2183
2184 kfree(pidarray);
2185 } else {
Al Viro9dce07f2008-03-29 03:07:28 +00002186 ctr->buf = NULL;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002187 ctr->bufsz = 0;
2188 }
2189 file->private_data = ctr;
2190 return 0;
2191
2192err2:
2193 kfree(pidarray);
2194err1:
2195 kfree(ctr);
2196err0:
2197 return -ENOMEM;
2198}
2199
Paul Menagebd89aab2007-10-18 23:40:44 -07002200static ssize_t cgroup_tasks_read(struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002201 struct cftype *cft,
2202 struct file *file, char __user *buf,
2203 size_t nbytes, loff_t *ppos)
2204{
2205 struct ctr_struct *ctr = file->private_data;
2206
2207 return simple_read_from_buffer(buf, nbytes, ppos, ctr->buf, ctr->bufsz);
2208}
2209
2210static int cgroup_tasks_release(struct inode *unused_inode,
2211 struct file *file)
2212{
2213 struct ctr_struct *ctr;
2214
2215 if (file->f_mode & FMODE_READ) {
2216 ctr = file->private_data;
2217 kfree(ctr->buf);
2218 kfree(ctr);
2219 }
2220 return 0;
2221}
2222
Paul Menagebd89aab2007-10-18 23:40:44 -07002223static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002224 struct cftype *cft)
2225{
Paul Menagebd89aab2007-10-18 23:40:44 -07002226 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002227}
2228
Paul Menagebbcb81d2007-10-18 23:39:32 -07002229/*
2230 * for the common functions, 'private' gives the type of file
2231 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07002232static struct cftype files[] = {
2233 {
2234 .name = "tasks",
2235 .open = cgroup_tasks_open,
2236 .read = cgroup_tasks_read,
2237 .write = cgroup_common_file_write,
2238 .release = cgroup_tasks_release,
2239 .private = FILE_TASKLIST,
2240 },
2241
2242 {
2243 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002244 .read_u64 = cgroup_read_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002245 .write = cgroup_common_file_write,
2246 .private = FILE_NOTIFY_ON_RELEASE,
2247 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002248};
2249
2250static struct cftype cft_release_agent = {
2251 .name = "release_agent",
2252 .read = cgroup_common_file_read,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002253 .write = cgroup_common_file_write,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002254 .private = FILE_RELEASE_AGENT,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002255};
2256
Paul Menagebd89aab2007-10-18 23:40:44 -07002257static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002258{
2259 int err;
2260 struct cgroup_subsys *ss;
2261
2262 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002263 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002264
Paul Menagebd89aab2007-10-18 23:40:44 -07002265 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002266 if (err < 0)
2267 return err;
2268
Paul Menagebd89aab2007-10-18 23:40:44 -07002269 if (cgrp == cgrp->top_cgroup) {
2270 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002271 return err;
2272 }
2273
Paul Menagebd89aab2007-10-18 23:40:44 -07002274 for_each_subsys(cgrp->root, ss) {
2275 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002276 return err;
2277 }
2278
2279 return 0;
2280}
2281
2282static void init_cgroup_css(struct cgroup_subsys_state *css,
2283 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002284 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002285{
Paul Menagebd89aab2007-10-18 23:40:44 -07002286 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002287 atomic_set(&css->refcnt, 0);
2288 css->flags = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002289 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002290 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002291 BUG_ON(cgrp->subsys[ss->subsys_id]);
2292 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002293}
2294
2295/*
Li Zefana043e3b2008-02-23 15:24:09 -08002296 * cgroup_create - create a cgroup
2297 * @parent: cgroup that will be parent of the new cgroup
2298 * @dentry: dentry of the new cgroup
2299 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002300 *
Li Zefana043e3b2008-02-23 15:24:09 -08002301 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002302 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002303static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2304 int mode)
2305{
Paul Menagebd89aab2007-10-18 23:40:44 -07002306 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002307 struct cgroupfs_root *root = parent->root;
2308 int err = 0;
2309 struct cgroup_subsys *ss;
2310 struct super_block *sb = root->sb;
2311
Paul Menagebd89aab2007-10-18 23:40:44 -07002312 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2313 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002314 return -ENOMEM;
2315
2316 /* Grab a reference on the superblock so the hierarchy doesn't
2317 * get deleted on unmount if there are child cgroups. This
2318 * can be done outside cgroup_mutex, since the sb can't
2319 * disappear while someone has an open control file on the
2320 * fs */
2321 atomic_inc(&sb->s_active);
2322
2323 mutex_lock(&cgroup_mutex);
2324
Paul Menagebd89aab2007-10-18 23:40:44 -07002325 INIT_LIST_HEAD(&cgrp->sibling);
2326 INIT_LIST_HEAD(&cgrp->children);
2327 INIT_LIST_HEAD(&cgrp->css_sets);
2328 INIT_LIST_HEAD(&cgrp->release_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002329
Paul Menagebd89aab2007-10-18 23:40:44 -07002330 cgrp->parent = parent;
2331 cgrp->root = parent->root;
2332 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002333
Li Zefanb6abdb02008-03-04 14:28:19 -08002334 if (notify_on_release(parent))
2335 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2336
Paul Menageddbcc7e2007-10-18 23:39:30 -07002337 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002338 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002339 if (IS_ERR(css)) {
2340 err = PTR_ERR(css);
2341 goto err_destroy;
2342 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002343 init_cgroup_css(css, ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002344 }
2345
Paul Menagebd89aab2007-10-18 23:40:44 -07002346 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002347 root->number_of_cgroups++;
2348
Paul Menagebd89aab2007-10-18 23:40:44 -07002349 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002350 if (err < 0)
2351 goto err_remove;
2352
2353 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07002354 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002355
Paul Menagebd89aab2007-10-18 23:40:44 -07002356 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002357 /* If err < 0, we have a half-filled directory - oh well ;) */
2358
2359 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002360 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002361
2362 return 0;
2363
2364 err_remove:
2365
Paul Menagebd89aab2007-10-18 23:40:44 -07002366 list_del(&cgrp->sibling);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002367 root->number_of_cgroups--;
2368
2369 err_destroy:
2370
2371 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002372 if (cgrp->subsys[ss->subsys_id])
2373 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002374 }
2375
2376 mutex_unlock(&cgroup_mutex);
2377
2378 /* Release the reference count that we took on the superblock */
2379 deactivate_super(sb);
2380
Paul Menagebd89aab2007-10-18 23:40:44 -07002381 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002382 return err;
2383}
2384
2385static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2386{
2387 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2388
2389 /* the vfs holds inode->i_mutex already */
2390 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2391}
2392
Paul Menagebd89aab2007-10-18 23:40:44 -07002393static inline int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002394{
2395 /* Check the reference count on each subsystem. Since we
2396 * already established that there are no tasks in the
2397 * cgroup, if the css refcount is also 0, then there should
2398 * be no outstanding references, so the subsystem is safe to
2399 * destroy. We scan across all subsystems rather than using
2400 * the per-hierarchy linked list of mounted subsystems since
2401 * we can be called via check_for_release() with no
2402 * synchronization other than RCU, and the subsystem linked
2403 * list isn't RCU-safe */
2404 int i;
2405 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2406 struct cgroup_subsys *ss = subsys[i];
2407 struct cgroup_subsys_state *css;
2408 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07002409 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002410 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07002411 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07002412 /* When called from check_for_release() it's possible
2413 * that by this point the cgroup has been removed
2414 * and the css deleted. But a false-positive doesn't
2415 * matter, since it can only happen if the cgroup
2416 * has been deleted and hence no longer needs the
2417 * release agent to be called anyway. */
Paul Jacksone18f6312008-02-07 00:13:44 -08002418 if (css && atomic_read(&css->refcnt))
Paul Menage81a6a5c2007-10-18 23:39:38 -07002419 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07002420 }
2421 return 0;
2422}
2423
Paul Menageddbcc7e2007-10-18 23:39:30 -07002424static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2425{
Paul Menagebd89aab2007-10-18 23:40:44 -07002426 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002427 struct dentry *d;
2428 struct cgroup *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002429 struct super_block *sb;
2430 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002431
2432 /* the vfs holds both inode->i_mutex already */
2433
2434 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002435 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002436 mutex_unlock(&cgroup_mutex);
2437 return -EBUSY;
2438 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002439 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002440 mutex_unlock(&cgroup_mutex);
2441 return -EBUSY;
2442 }
2443
Paul Menagebd89aab2007-10-18 23:40:44 -07002444 parent = cgrp->parent;
2445 root = cgrp->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002446 sb = root->sb;
Li Zefana043e3b2008-02-23 15:24:09 -08002447
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002448 /*
Li Zefana043e3b2008-02-23 15:24:09 -08002449 * Call pre_destroy handlers of subsys. Notify subsystems
2450 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002451 */
2452 cgroup_call_pre_destroy(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002453
Paul Menagebd89aab2007-10-18 23:40:44 -07002454 if (cgroup_has_css_refs(cgrp)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002455 mutex_unlock(&cgroup_mutex);
2456 return -EBUSY;
2457 }
2458
Paul Menage81a6a5c2007-10-18 23:39:38 -07002459 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002460 set_bit(CGRP_REMOVED, &cgrp->flags);
2461 if (!list_empty(&cgrp->release_list))
2462 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002463 spin_unlock(&release_list_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002464 /* delete my sibling from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07002465 list_del(&cgrp->sibling);
2466 spin_lock(&cgrp->dentry->d_lock);
2467 d = dget(cgrp->dentry);
2468 cgrp->dentry = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002469 spin_unlock(&d->d_lock);
2470
2471 cgroup_d_remove_dir(d);
2472 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002473
Paul Menagebd89aab2007-10-18 23:40:44 -07002474 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002475 check_for_release(parent);
2476
Paul Menageddbcc7e2007-10-18 23:39:30 -07002477 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002478 return 0;
2479}
2480
Li Zefan06a11922008-04-29 01:00:07 -07002481static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002482{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002483 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08002484
2485 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002486
2487 /* Create the top cgroup state for this subsystem */
2488 ss->root = &rootnode;
2489 css = ss->create(ss, dummytop);
2490 /* We don't handle early failures gracefully */
2491 BUG_ON(IS_ERR(css));
2492 init_cgroup_css(css, ss, dummytop);
2493
Li Zefane8d55fd2008-04-29 01:00:13 -07002494 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07002495 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07002496 * newly registered, all tasks and hence the
2497 * init_css_set is in the subsystem's top cgroup. */
2498 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07002499
2500 need_forkexit_callback |= ss->fork || ss->exit;
2501
Li Zefane8d55fd2008-04-29 01:00:13 -07002502 /* At system boot, before all subsystems have been
2503 * registered, no tasks have been forked, so we don't
2504 * need to invoke fork callbacks here. */
2505 BUG_ON(!list_empty(&init_task.tasks));
2506
Paul Menageddbcc7e2007-10-18 23:39:30 -07002507 ss->active = 1;
2508}
2509
2510/**
Li Zefana043e3b2008-02-23 15:24:09 -08002511 * cgroup_init_early - cgroup initialization at system boot
2512 *
2513 * Initialize cgroups at system boot, and initialize any
2514 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002515 */
2516int __init cgroup_init_early(void)
2517{
2518 int i;
Paul Menage817929e2007-10-18 23:39:36 -07002519 kref_init(&init_css_set.ref);
2520 kref_get(&init_css_set.ref);
Paul Menage817929e2007-10-18 23:39:36 -07002521 INIT_LIST_HEAD(&init_css_set.cg_links);
2522 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07002523 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07002524 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002525 init_cgroup_root(&rootnode);
2526 list_add(&rootnode.root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07002527 root_count = 1;
2528 init_task.cgroups = &init_css_set;
2529
2530 init_css_set_link.cg = &init_css_set;
Paul Menagebd89aab2007-10-18 23:40:44 -07002531 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07002532 &rootnode.top_cgroup.css_sets);
2533 list_add(&init_css_set_link.cg_link_list,
2534 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002535
Li Zefan472b1052008-04-29 01:00:11 -07002536 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2537 INIT_HLIST_HEAD(&css_set_table[i]);
2538
Paul Menageddbcc7e2007-10-18 23:39:30 -07002539 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2540 struct cgroup_subsys *ss = subsys[i];
2541
2542 BUG_ON(!ss->name);
2543 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2544 BUG_ON(!ss->create);
2545 BUG_ON(!ss->destroy);
2546 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08002547 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07002548 ss->name, ss->subsys_id);
2549 BUG();
2550 }
2551
2552 if (ss->early_init)
2553 cgroup_init_subsys(ss);
2554 }
2555 return 0;
2556}
2557
2558/**
Li Zefana043e3b2008-02-23 15:24:09 -08002559 * cgroup_init - cgroup initialization
2560 *
2561 * Register cgroup filesystem and /proc file, and initialize
2562 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002563 */
2564int __init cgroup_init(void)
2565{
2566 int err;
2567 int i;
Li Zefan472b1052008-04-29 01:00:11 -07002568 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07002569
2570 err = bdi_init(&cgroup_backing_dev_info);
2571 if (err)
2572 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002573
2574 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2575 struct cgroup_subsys *ss = subsys[i];
2576 if (!ss->early_init)
2577 cgroup_init_subsys(ss);
2578 }
2579
Li Zefan472b1052008-04-29 01:00:11 -07002580 /* Add init_css_set to the hash table */
2581 hhead = css_set_hash(init_css_set.subsys);
2582 hlist_add_head(&init_css_set.hlist, hhead);
2583
Paul Menageddbcc7e2007-10-18 23:39:30 -07002584 err = register_filesystem(&cgroup_fs_type);
2585 if (err < 0)
2586 goto out;
2587
Li Zefan46ae2202008-04-29 01:00:08 -07002588 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07002589
Paul Menageddbcc7e2007-10-18 23:39:30 -07002590out:
Paul Menagea4243162007-10-18 23:39:35 -07002591 if (err)
2592 bdi_destroy(&cgroup_backing_dev_info);
2593
Paul Menageddbcc7e2007-10-18 23:39:30 -07002594 return err;
2595}
Paul Menageb4f48b62007-10-18 23:39:33 -07002596
Paul Menagea4243162007-10-18 23:39:35 -07002597/*
2598 * proc_cgroup_show()
2599 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2600 * - Used for /proc/<pid>/cgroup.
2601 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2602 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002603 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07002604 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2605 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2606 * cgroup to top_cgroup.
2607 */
2608
2609/* TODO: Use a proper seq_file iterator */
2610static int proc_cgroup_show(struct seq_file *m, void *v)
2611{
2612 struct pid *pid;
2613 struct task_struct *tsk;
2614 char *buf;
2615 int retval;
2616 struct cgroupfs_root *root;
2617
2618 retval = -ENOMEM;
2619 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2620 if (!buf)
2621 goto out;
2622
2623 retval = -ESRCH;
2624 pid = m->private;
2625 tsk = get_pid_task(pid, PIDTYPE_PID);
2626 if (!tsk)
2627 goto out_free;
2628
2629 retval = 0;
2630
2631 mutex_lock(&cgroup_mutex);
2632
2633 for_each_root(root) {
2634 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07002635 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07002636 int subsys_id;
2637 int count = 0;
2638
2639 /* Skip this hierarchy if it has no active subsystems */
2640 if (!root->actual_subsys_bits)
2641 continue;
Paul Menageb6c30062008-04-10 21:29:16 -07002642 seq_printf(m, "%lu:", root->subsys_bits);
Paul Menagea4243162007-10-18 23:39:35 -07002643 for_each_subsys(root, ss)
2644 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2645 seq_putc(m, ':');
2646 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07002647 cgrp = task_cgroup(tsk, subsys_id);
2648 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07002649 if (retval < 0)
2650 goto out_unlock;
2651 seq_puts(m, buf);
2652 seq_putc(m, '\n');
2653 }
2654
2655out_unlock:
2656 mutex_unlock(&cgroup_mutex);
2657 put_task_struct(tsk);
2658out_free:
2659 kfree(buf);
2660out:
2661 return retval;
2662}
2663
2664static int cgroup_open(struct inode *inode, struct file *file)
2665{
2666 struct pid *pid = PROC_I(inode)->pid;
2667 return single_open(file, proc_cgroup_show, pid);
2668}
2669
2670struct file_operations proc_cgroup_operations = {
2671 .open = cgroup_open,
2672 .read = seq_read,
2673 .llseek = seq_lseek,
2674 .release = single_release,
2675};
2676
2677/* Display information about each subsystem and each hierarchy */
2678static int proc_cgroupstats_show(struct seq_file *m, void *v)
2679{
2680 int i;
Paul Menagea4243162007-10-18 23:39:35 -07002681
Paul Menage8bab8dd2008-04-04 14:29:57 -07002682 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07002683 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07002684 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2685 struct cgroup_subsys *ss = subsys[i];
Paul Menage8bab8dd2008-04-04 14:29:57 -07002686 seq_printf(m, "%s\t%lu\t%d\t%d\n",
Paul Menage817929e2007-10-18 23:39:36 -07002687 ss->name, ss->root->subsys_bits,
Paul Menage8bab8dd2008-04-04 14:29:57 -07002688 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07002689 }
2690 mutex_unlock(&cgroup_mutex);
2691 return 0;
2692}
2693
2694static int cgroupstats_open(struct inode *inode, struct file *file)
2695{
Al Viro9dce07f2008-03-29 03:07:28 +00002696 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07002697}
2698
2699static struct file_operations proc_cgroupstats_operations = {
2700 .open = cgroupstats_open,
2701 .read = seq_read,
2702 .llseek = seq_lseek,
2703 .release = single_release,
2704};
2705
Paul Menageb4f48b62007-10-18 23:39:33 -07002706/**
2707 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08002708 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07002709 *
2710 * Description: A task inherits its parent's cgroup at fork().
2711 *
2712 * A pointer to the shared css_set was automatically copied in
2713 * fork.c by dup_task_struct(). However, we ignore that copy, since
2714 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08002715 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07002716 * have already changed current->cgroups, allowing the previously
2717 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07002718 *
2719 * At the point that cgroup_fork() is called, 'current' is the parent
2720 * task, and the passed argument 'child' points to the child task.
2721 */
2722void cgroup_fork(struct task_struct *child)
2723{
Paul Menage817929e2007-10-18 23:39:36 -07002724 task_lock(current);
2725 child->cgroups = current->cgroups;
2726 get_css_set(child->cgroups);
2727 task_unlock(current);
2728 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07002729}
2730
2731/**
Li Zefana043e3b2008-02-23 15:24:09 -08002732 * cgroup_fork_callbacks - run fork callbacks
2733 * @child: the new task
2734 *
2735 * Called on a new task very soon before adding it to the
2736 * tasklist. No need to take any locks since no-one can
2737 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07002738 */
2739void cgroup_fork_callbacks(struct task_struct *child)
2740{
2741 if (need_forkexit_callback) {
2742 int i;
2743 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2744 struct cgroup_subsys *ss = subsys[i];
2745 if (ss->fork)
2746 ss->fork(ss, child);
2747 }
2748 }
2749}
2750
2751/**
Li Zefana043e3b2008-02-23 15:24:09 -08002752 * cgroup_post_fork - called on a new task after adding it to the task list
2753 * @child: the task in question
2754 *
2755 * Adds the task to the list running through its css_set if necessary.
2756 * Has to be after the task is visible on the task list in case we race
2757 * with the first call to cgroup_iter_start() - to guarantee that the
2758 * new task ends up on its list.
2759 */
Paul Menage817929e2007-10-18 23:39:36 -07002760void cgroup_post_fork(struct task_struct *child)
2761{
2762 if (use_task_css_set_links) {
2763 write_lock(&css_set_lock);
2764 if (list_empty(&child->cg_list))
2765 list_add(&child->cg_list, &child->cgroups->tasks);
2766 write_unlock(&css_set_lock);
2767 }
2768}
2769/**
Paul Menageb4f48b62007-10-18 23:39:33 -07002770 * cgroup_exit - detach cgroup from exiting task
2771 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08002772 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07002773 *
2774 * Description: Detach cgroup from @tsk and release it.
2775 *
2776 * Note that cgroups marked notify_on_release force every task in
2777 * them to take the global cgroup_mutex mutex when exiting.
2778 * This could impact scaling on very large systems. Be reluctant to
2779 * use notify_on_release cgroups where very high task exit scaling
2780 * is required on large systems.
2781 *
2782 * the_top_cgroup_hack:
2783 *
2784 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2785 *
2786 * We call cgroup_exit() while the task is still competent to
2787 * handle notify_on_release(), then leave the task attached to the
2788 * root cgroup in each hierarchy for the remainder of its exit.
2789 *
2790 * To do this properly, we would increment the reference count on
2791 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2792 * code we would add a second cgroup function call, to drop that
2793 * reference. This would just create an unnecessary hot spot on
2794 * the top_cgroup reference count, to no avail.
2795 *
2796 * Normally, holding a reference to a cgroup without bumping its
2797 * count is unsafe. The cgroup could go away, or someone could
2798 * attach us to a different cgroup, decrementing the count on
2799 * the first cgroup that we never incremented. But in this case,
2800 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002801 * which wards off any cgroup_attach_task() attempts, or task is a failed
2802 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07002803 */
2804void cgroup_exit(struct task_struct *tsk, int run_callbacks)
2805{
2806 int i;
Paul Menage817929e2007-10-18 23:39:36 -07002807 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07002808
2809 if (run_callbacks && need_forkexit_callback) {
2810 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2811 struct cgroup_subsys *ss = subsys[i];
2812 if (ss->exit)
2813 ss->exit(ss, tsk);
2814 }
2815 }
Paul Menage817929e2007-10-18 23:39:36 -07002816
2817 /*
2818 * Unlink from the css_set task list if necessary.
2819 * Optimistically check cg_list before taking
2820 * css_set_lock
2821 */
2822 if (!list_empty(&tsk->cg_list)) {
2823 write_lock(&css_set_lock);
2824 if (!list_empty(&tsk->cg_list))
2825 list_del(&tsk->cg_list);
2826 write_unlock(&css_set_lock);
2827 }
2828
Paul Menageb4f48b62007-10-18 23:39:33 -07002829 /* Reassign the task to the init_css_set. */
2830 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002831 cg = tsk->cgroups;
2832 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07002833 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002834 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002835 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07002836}
Paul Menage697f4162007-10-18 23:39:34 -07002837
2838/**
Li Zefana043e3b2008-02-23 15:24:09 -08002839 * cgroup_clone - clone the cgroup the given subsystem is attached to
2840 * @tsk: the task to be moved
2841 * @subsys: the given subsystem
2842 *
2843 * Duplicate the current cgroup in the hierarchy that the given
2844 * subsystem is attached to, and move this task into the new
2845 * child.
Paul Menage697f4162007-10-18 23:39:34 -07002846 */
2847int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys)
2848{
2849 struct dentry *dentry;
2850 int ret = 0;
2851 char nodename[MAX_CGROUP_TYPE_NAMELEN];
2852 struct cgroup *parent, *child;
2853 struct inode *inode;
2854 struct css_set *cg;
2855 struct cgroupfs_root *root;
2856 struct cgroup_subsys *ss;
2857
2858 /* We shouldn't be called by an unregistered subsystem */
2859 BUG_ON(!subsys->active);
2860
2861 /* First figure out what hierarchy and cgroup we're dealing
2862 * with, and pin them so we can drop cgroup_mutex */
2863 mutex_lock(&cgroup_mutex);
2864 again:
2865 root = subsys->root;
2866 if (root == &rootnode) {
2867 printk(KERN_INFO
2868 "Not cloning cgroup for unused subsystem %s\n",
2869 subsys->name);
2870 mutex_unlock(&cgroup_mutex);
2871 return 0;
2872 }
Paul Menage817929e2007-10-18 23:39:36 -07002873 cg = tsk->cgroups;
Paul Menage697f4162007-10-18 23:39:34 -07002874 parent = task_cgroup(tsk, subsys->subsys_id);
2875
2876 snprintf(nodename, MAX_CGROUP_TYPE_NAMELEN, "node_%d", tsk->pid);
2877
2878 /* Pin the hierarchy */
2879 atomic_inc(&parent->root->sb->s_active);
2880
Paul Menage817929e2007-10-18 23:39:36 -07002881 /* Keep the cgroup alive */
2882 get_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07002883 mutex_unlock(&cgroup_mutex);
2884
2885 /* Now do the VFS work to create a cgroup */
2886 inode = parent->dentry->d_inode;
2887
2888 /* Hold the parent directory mutex across this operation to
2889 * stop anyone else deleting the new cgroup */
2890 mutex_lock(&inode->i_mutex);
2891 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
2892 if (IS_ERR(dentry)) {
2893 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08002894 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07002895 PTR_ERR(dentry));
2896 ret = PTR_ERR(dentry);
2897 goto out_release;
2898 }
2899
2900 /* Create the cgroup directory, which also creates the cgroup */
2901 ret = vfs_mkdir(inode, dentry, S_IFDIR | 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07002902 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07002903 dput(dentry);
2904 if (ret) {
2905 printk(KERN_INFO
2906 "Failed to create cgroup %s: %d\n", nodename,
2907 ret);
2908 goto out_release;
2909 }
2910
2911 if (!child) {
2912 printk(KERN_INFO
2913 "Couldn't find new cgroup %s\n", nodename);
2914 ret = -ENOMEM;
2915 goto out_release;
2916 }
2917
2918 /* The cgroup now exists. Retake cgroup_mutex and check
2919 * that we're still in the same state that we thought we
2920 * were. */
2921 mutex_lock(&cgroup_mutex);
2922 if ((root != subsys->root) ||
2923 (parent != task_cgroup(tsk, subsys->subsys_id))) {
2924 /* Aargh, we raced ... */
2925 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07002926 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07002927
2928 deactivate_super(parent->root->sb);
2929 /* The cgroup is still accessible in the VFS, but
2930 * we're not going to try to rmdir() it at this
2931 * point. */
2932 printk(KERN_INFO
2933 "Race in cgroup_clone() - leaking cgroup %s\n",
2934 nodename);
2935 goto again;
2936 }
2937
2938 /* do any required auto-setup */
2939 for_each_subsys(root, ss) {
2940 if (ss->post_clone)
2941 ss->post_clone(ss, child);
2942 }
2943
2944 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002945 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07002946 mutex_unlock(&cgroup_mutex);
2947
2948 out_release:
2949 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002950
2951 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07002952 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002953 mutex_unlock(&cgroup_mutex);
Paul Menage697f4162007-10-18 23:39:34 -07002954 deactivate_super(parent->root->sb);
2955 return ret;
2956}
2957
Li Zefana043e3b2008-02-23 15:24:09 -08002958/**
2959 * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
2960 * @cgrp: the cgroup in question
2961 *
2962 * See if @cgrp is a descendant of the current task's cgroup in
2963 * the appropriate hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07002964 *
2965 * If we are sending in dummytop, then presumably we are creating
2966 * the top cgroup in the subsystem.
2967 *
2968 * Called only by the ns (nsproxy) cgroup.
2969 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002970int cgroup_is_descendant(const struct cgroup *cgrp)
Paul Menage697f4162007-10-18 23:39:34 -07002971{
2972 int ret;
2973 struct cgroup *target;
2974 int subsys_id;
2975
Paul Menagebd89aab2007-10-18 23:40:44 -07002976 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07002977 return 1;
2978
Paul Menagebd89aab2007-10-18 23:40:44 -07002979 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menage697f4162007-10-18 23:39:34 -07002980 target = task_cgroup(current, subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07002981 while (cgrp != target && cgrp!= cgrp->top_cgroup)
2982 cgrp = cgrp->parent;
2983 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07002984 return ret;
2985}
Paul Menage81a6a5c2007-10-18 23:39:38 -07002986
Paul Menagebd89aab2007-10-18 23:40:44 -07002987static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002988{
2989 /* All of these checks rely on RCU to keep the cgroup
2990 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07002991 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
2992 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07002993 /* Control Group is currently removeable. If it's not
2994 * already queued for a userspace notification, queue
2995 * it now */
2996 int need_schedule_work = 0;
2997 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002998 if (!cgroup_is_removed(cgrp) &&
2999 list_empty(&cgrp->release_list)) {
3000 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003001 need_schedule_work = 1;
3002 }
3003 spin_unlock(&release_list_lock);
3004 if (need_schedule_work)
3005 schedule_work(&release_agent_work);
3006 }
3007}
3008
3009void __css_put(struct cgroup_subsys_state *css)
3010{
Paul Menagebd89aab2007-10-18 23:40:44 -07003011 struct cgroup *cgrp = css->cgroup;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003012 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -07003013 if (atomic_dec_and_test(&css->refcnt) && notify_on_release(cgrp)) {
3014 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3015 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003016 }
3017 rcu_read_unlock();
3018}
3019
3020/*
3021 * Notify userspace when a cgroup is released, by running the
3022 * configured release agent with the name of the cgroup (path
3023 * relative to the root of cgroup file system) as the argument.
3024 *
3025 * Most likely, this user command will try to rmdir this cgroup.
3026 *
3027 * This races with the possibility that some other task will be
3028 * attached to this cgroup before it is removed, or that some other
3029 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3030 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3031 * unused, and this cgroup will be reprieved from its death sentence,
3032 * to continue to serve a useful existence. Next time it's released,
3033 * we will get notified again, if it still has 'notify_on_release' set.
3034 *
3035 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3036 * means only wait until the task is successfully execve()'d. The
3037 * separate release agent task is forked by call_usermodehelper(),
3038 * then control in this thread returns here, without waiting for the
3039 * release agent task. We don't bother to wait because the caller of
3040 * this routine has no use for the exit status of the release agent
3041 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003042 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003043static void cgroup_release_agent(struct work_struct *work)
3044{
3045 BUG_ON(work != &release_agent_work);
3046 mutex_lock(&cgroup_mutex);
3047 spin_lock(&release_list_lock);
3048 while (!list_empty(&release_list)) {
3049 char *argv[3], *envp[3];
3050 int i;
3051 char *pathbuf;
Paul Menagebd89aab2007-10-18 23:40:44 -07003052 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003053 struct cgroup,
3054 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003055 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003056 spin_unlock(&release_list_lock);
3057 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3058 if (!pathbuf) {
3059 spin_lock(&release_list_lock);
3060 continue;
3061 }
3062
Paul Menagebd89aab2007-10-18 23:40:44 -07003063 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003064 kfree(pathbuf);
3065 spin_lock(&release_list_lock);
3066 continue;
3067 }
3068
3069 i = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003070 argv[i++] = cgrp->root->release_agent_path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003071 argv[i++] = (char *)pathbuf;
3072 argv[i] = NULL;
3073
3074 i = 0;
3075 /* minimal command environment */
3076 envp[i++] = "HOME=/";
3077 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3078 envp[i] = NULL;
3079
3080 /* Drop the lock while we invoke the usermode helper,
3081 * since the exec could involve hitting disk and hence
3082 * be a slow process */
3083 mutex_unlock(&cgroup_mutex);
3084 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
3085 kfree(pathbuf);
3086 mutex_lock(&cgroup_mutex);
3087 spin_lock(&release_list_lock);
3088 }
3089 spin_unlock(&release_list_lock);
3090 mutex_unlock(&cgroup_mutex);
3091}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003092
3093static int __init cgroup_disable(char *str)
3094{
3095 int i;
3096 char *token;
3097
3098 while ((token = strsep(&str, ",")) != NULL) {
3099 if (!*token)
3100 continue;
3101
3102 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3103 struct cgroup_subsys *ss = subsys[i];
3104
3105 if (!strcmp(token, ss->name)) {
3106 ss->disabled = 1;
3107 printk(KERN_INFO "Disabling %s control group"
3108 " subsystem\n", ss->name);
3109 break;
3110 }
3111 }
3112 }
3113 return 1;
3114}
3115__setup("cgroup_disable=", cgroup_disable);