blob: 61e92c5867ea03d9e26bb4c3bfc35717de350234 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
11 *
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
14 *
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
19 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
23 */
24
25#include <linux/cgroup.h>
26#include <linux/errno.h>
27#include <linux/fs.h>
28#include <linux/kernel.h>
29#include <linux/list.h>
30#include <linux/mm.h>
31#include <linux/mutex.h>
32#include <linux/mount.h>
33#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070034#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/rcupdate.h>
36#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070037#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070038#include <linux/seq_file.h>
39#include <linux/slab.h>
40#include <linux/magic.h>
41#include <linux/spinlock.h>
42#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070043#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070044#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070045#include <linux/delayacct.h>
46#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070047#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040048#include <linux/namei.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070049
Paul Menageddbcc7e2007-10-18 23:39:30 -070050#include <asm/atomic.h>
51
Paul Menage81a6a5c2007-10-18 23:39:38 -070052static DEFINE_MUTEX(cgroup_mutex);
53
Paul Menageddbcc7e2007-10-18 23:39:30 -070054/* Generate an array of cgroup subsystem pointers */
55#define SUBSYS(_x) &_x ## _subsys,
56
57static struct cgroup_subsys *subsys[] = {
58#include <linux/cgroup_subsys.h>
59};
60
61/*
62 * A cgroupfs_root represents the root of a cgroup hierarchy,
63 * and may be associated with a superblock to form an active
64 * hierarchy
65 */
66struct cgroupfs_root {
67 struct super_block *sb;
68
69 /*
70 * The bitmask of subsystems intended to be attached to this
71 * hierarchy
72 */
73 unsigned long subsys_bits;
74
75 /* The bitmask of subsystems currently attached to this hierarchy */
76 unsigned long actual_subsys_bits;
77
78 /* A list running through the attached subsystems */
79 struct list_head subsys_list;
80
81 /* The root cgroup for this hierarchy */
82 struct cgroup top_cgroup;
83
84 /* Tracks how many cgroups are currently defined in hierarchy.*/
85 int number_of_cgroups;
86
87 /* A list running through the mounted hierarchies */
88 struct list_head root_list;
89
90 /* Hierarchy-specific flags */
91 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -070092
Paul Menagee788e0662008-07-25 01:46:59 -070093 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -070094 char release_agent_path[PATH_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -070095};
96
97
98/*
99 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
100 * subsystems that are otherwise unattached - it never has more than a
101 * single cgroup, and all tasks are part of that cgroup.
102 */
103static struct cgroupfs_root rootnode;
104
105/* The list of hierarchy roots */
106
107static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700108static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700109
110/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
111#define dummytop (&rootnode.top_cgroup)
112
113/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800114 * check for fork/exit handlers to call. This avoids us having to do
115 * extra work in the fork/exit path if none of the subsystems need to
116 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700117 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700118static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700119
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700121inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122{
Paul Menagebd89aab2007-10-18 23:40:44 -0700123 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700124}
125
126/* bits in struct cgroupfs_root flags field */
127enum {
128 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
129};
130
Adrian Bunke9685a02008-02-07 00:13:46 -0800131static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700132{
133 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700134 (1 << CGRP_RELEASABLE) |
135 (1 << CGRP_NOTIFY_ON_RELEASE);
136 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700137}
138
Adrian Bunke9685a02008-02-07 00:13:46 -0800139static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700140{
Paul Menagebd89aab2007-10-18 23:40:44 -0700141 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700142}
143
Paul Menageddbcc7e2007-10-18 23:39:30 -0700144/*
145 * for_each_subsys() allows you to iterate on each subsystem attached to
146 * an active hierarchy
147 */
148#define for_each_subsys(_root, _ss) \
149list_for_each_entry(_ss, &_root->subsys_list, sibling)
150
151/* for_each_root() allows you to iterate across the active hierarchies */
152#define for_each_root(_root) \
153list_for_each_entry(_root, &roots, root_list)
154
Paul Menage81a6a5c2007-10-18 23:39:38 -0700155/* the list of cgroups eligible for automatic release. Protected by
156 * release_list_lock */
157static LIST_HEAD(release_list);
158static DEFINE_SPINLOCK(release_list_lock);
159static void cgroup_release_agent(struct work_struct *work);
160static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700161static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700162
Paul Menage817929e2007-10-18 23:39:36 -0700163/* Link structure for associating css_set objects with cgroups */
164struct cg_cgroup_link {
165 /*
166 * List running through cg_cgroup_links associated with a
167 * cgroup, anchored on cgroup->css_sets
168 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700169 struct list_head cgrp_link_list;
Paul Menage817929e2007-10-18 23:39:36 -0700170 /*
171 * List running through cg_cgroup_links pointing at a
172 * single css_set object, anchored on css_set->cg_links
173 */
174 struct list_head cg_link_list;
175 struct css_set *cg;
176};
177
178/* The default css_set - used by init and its children prior to any
179 * hierarchies being mounted. It contains a pointer to the root state
180 * for each subsystem. Also used to anchor the list of css_sets. Not
181 * reference-counted, to improve performance when child cgroups
182 * haven't been created.
183 */
184
185static struct css_set init_css_set;
186static struct cg_cgroup_link init_css_set_link;
187
188/* css_set_lock protects the list of css_set objects, and the
189 * chain of tasks off each css_set. Nests outside task->alloc_lock
190 * due to cgroup_iter_start() */
191static DEFINE_RWLOCK(css_set_lock);
192static int css_set_count;
193
Li Zefan472b1052008-04-29 01:00:11 -0700194/* hash table for cgroup groups. This improves the performance to
195 * find an existing css_set */
196#define CSS_SET_HASH_BITS 7
197#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
198static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
199
200static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
201{
202 int i;
203 int index;
204 unsigned long tmp = 0UL;
205
206 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
207 tmp += (unsigned long)css[i];
208 tmp = (tmp >> 16) ^ tmp;
209
210 index = hash_long(tmp, CSS_SET_HASH_BITS);
211
212 return &css_set_table[index];
213}
214
Paul Menage817929e2007-10-18 23:39:36 -0700215/* We don't maintain the lists running through each css_set to its
216 * task until after the first call to cgroup_iter_start(). This
217 * reduces the fork()/exit() overhead for people who have cgroups
218 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700219static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700220
221/* When we create or destroy a css_set, the operation simply
222 * takes/releases a reference count on all the cgroups referenced
223 * by subsystems in this css_set. This can end up multiple-counting
224 * some cgroups, but that's OK - the ref-count is just a
225 * busy/not-busy indicator; ensuring that we only count each cgroup
226 * once would require taking a global lock to ensure that no
Paul Menageb4f48b62007-10-18 23:39:33 -0700227 * subsystems moved between hierarchies while we were doing so.
228 *
229 * Possible TODO: decide at boot time based on the number of
230 * registered subsystems and the number of CPUs or NUMA nodes whether
231 * it's better for performance to ref-count every subsystem, or to
232 * take a global lock and only add one ref count to each hierarchy.
233 */
Paul Menageb4f48b62007-10-18 23:39:33 -0700234
Paul Menage817929e2007-10-18 23:39:36 -0700235/*
236 * unlink a css_set from the list and free it
237 */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700238static void unlink_css_set(struct css_set *cg)
Paul Menageb4f48b62007-10-18 23:39:33 -0700239{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700240 struct cg_cgroup_link *link;
241 struct cg_cgroup_link *saved_link;
242
Li Zefan472b1052008-04-29 01:00:11 -0700243 hlist_del(&cg->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700244 css_set_count--;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700245
246 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
247 cg_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -0700248 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700249 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700250 kfree(link);
251 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700252}
253
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700254static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700255{
256 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700257 /*
258 * Ensure that the refcount doesn't hit zero while any readers
259 * can see it. Similar to atomic_dec_and_lock(), but for an
260 * rwlock
261 */
262 if (atomic_add_unless(&cg->refcount, -1, 1))
263 return;
264 write_lock(&css_set_lock);
265 if (!atomic_dec_and_test(&cg->refcount)) {
266 write_unlock(&css_set_lock);
267 return;
268 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700269 unlink_css_set(cg);
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700270 write_unlock(&css_set_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700271
272 rcu_read_lock();
273 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700274 struct cgroup *cgrp = cg->subsys[i]->cgroup;
275 if (atomic_dec_and_test(&cgrp->count) &&
276 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700277 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700278 set_bit(CGRP_RELEASABLE, &cgrp->flags);
279 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700280 }
281 }
282 rcu_read_unlock();
Paul Menage817929e2007-10-18 23:39:36 -0700283 kfree(cg);
284}
285
286/*
287 * refcounted get/put for css_set objects
288 */
289static inline void get_css_set(struct css_set *cg)
290{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700291 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700292}
293
294static inline void put_css_set(struct css_set *cg)
295{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700296 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700297}
298
Paul Menage81a6a5c2007-10-18 23:39:38 -0700299static inline void put_css_set_taskexit(struct css_set *cg)
300{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700301 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700302}
303
Paul Menage817929e2007-10-18 23:39:36 -0700304/*
305 * find_existing_css_set() is a helper for
306 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700307 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700308 *
309 * oldcg: the cgroup group that we're using before the cgroup
310 * transition
311 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700312 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700313 *
314 * template: location in which to build the desired set of subsystem
315 * state objects for the new cgroup group
316 */
Paul Menage817929e2007-10-18 23:39:36 -0700317static struct css_set *find_existing_css_set(
318 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700319 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700320 struct cgroup_subsys_state *template[])
321{
322 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700323 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700324 struct hlist_head *hhead;
325 struct hlist_node *node;
326 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700327
328 /* Built the set of subsystem state objects that we want to
329 * see in the new css_set */
330 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800331 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700332 /* Subsystem is in this hierarchy. So we want
333 * the subsystem state from the new
334 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700335 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700336 } else {
337 /* Subsystem is not in this hierarchy, so we
338 * don't want to change the subsystem state */
339 template[i] = oldcg->subsys[i];
340 }
341 }
342
Li Zefan472b1052008-04-29 01:00:11 -0700343 hhead = css_set_hash(template);
344 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage817929e2007-10-18 23:39:36 -0700345 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
346 /* All subsystems matched */
347 return cg;
348 }
Li Zefan472b1052008-04-29 01:00:11 -0700349 }
Paul Menage817929e2007-10-18 23:39:36 -0700350
351 /* No existing cgroup group matched */
352 return NULL;
353}
354
Paul Menage817929e2007-10-18 23:39:36 -0700355static void free_cg_links(struct list_head *tmp)
356{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700357 struct cg_cgroup_link *link;
358 struct cg_cgroup_link *saved_link;
359
360 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700361 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700362 kfree(link);
363 }
364}
365
366/*
Li Zefan36553432008-07-29 22:33:19 -0700367 * allocate_cg_links() allocates "count" cg_cgroup_link structures
368 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
369 * success or a negative error
370 */
371static int allocate_cg_links(int count, struct list_head *tmp)
372{
373 struct cg_cgroup_link *link;
374 int i;
375 INIT_LIST_HEAD(tmp);
376 for (i = 0; i < count; i++) {
377 link = kmalloc(sizeof(*link), GFP_KERNEL);
378 if (!link) {
379 free_cg_links(tmp);
380 return -ENOMEM;
381 }
382 list_add(&link->cgrp_link_list, tmp);
383 }
384 return 0;
385}
386
387/*
Paul Menage817929e2007-10-18 23:39:36 -0700388 * find_css_set() takes an existing cgroup group and a
389 * cgroup object, and returns a css_set object that's
390 * equivalent to the old group, but with the given cgroup
391 * substituted into the appropriate hierarchy. Must be called with
392 * cgroup_mutex held
393 */
Paul Menage817929e2007-10-18 23:39:36 -0700394static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700395 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700396{
397 struct css_set *res;
398 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
399 int i;
400
401 struct list_head tmp_cg_links;
402 struct cg_cgroup_link *link;
403
Li Zefan472b1052008-04-29 01:00:11 -0700404 struct hlist_head *hhead;
405
Paul Menage817929e2007-10-18 23:39:36 -0700406 /* First see if we already have a cgroup group that matches
407 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700408 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700409 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700410 if (res)
411 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700412 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700413
414 if (res)
415 return res;
416
417 res = kmalloc(sizeof(*res), GFP_KERNEL);
418 if (!res)
419 return NULL;
420
421 /* Allocate all the cg_cgroup_link objects that we'll need */
422 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
423 kfree(res);
424 return NULL;
425 }
426
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700427 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700428 INIT_LIST_HEAD(&res->cg_links);
429 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700430 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700431
432 /* Copy the set of subsystem state objects generated in
433 * find_existing_css_set() */
434 memcpy(res->subsys, template, sizeof(res->subsys));
435
436 write_lock(&css_set_lock);
437 /* Add reference counts and links from the new css_set. */
438 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700439 struct cgroup *cgrp = res->subsys[i]->cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700440 struct cgroup_subsys *ss = subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -0700441 atomic_inc(&cgrp->count);
Paul Menage817929e2007-10-18 23:39:36 -0700442 /*
443 * We want to add a link once per cgroup, so we
444 * only do it for the first subsystem in each
445 * hierarchy
446 */
447 if (ss->root->subsys_list.next == &ss->sibling) {
448 BUG_ON(list_empty(&tmp_cg_links));
449 link = list_entry(tmp_cg_links.next,
450 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700451 cgrp_link_list);
452 list_del(&link->cgrp_link_list);
453 list_add(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage817929e2007-10-18 23:39:36 -0700454 link->cg = res;
455 list_add(&link->cg_link_list, &res->cg_links);
456 }
457 }
458 if (list_empty(&rootnode.subsys_list)) {
459 link = list_entry(tmp_cg_links.next,
460 struct cg_cgroup_link,
Paul Menagebd89aab2007-10-18 23:40:44 -0700461 cgrp_link_list);
462 list_del(&link->cgrp_link_list);
463 list_add(&link->cgrp_link_list, &dummytop->css_sets);
Paul Menage817929e2007-10-18 23:39:36 -0700464 link->cg = res;
465 list_add(&link->cg_link_list, &res->cg_links);
466 }
467
468 BUG_ON(!list_empty(&tmp_cg_links));
469
Paul Menage817929e2007-10-18 23:39:36 -0700470 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700471
472 /* Add this cgroup group to the hash table */
473 hhead = css_set_hash(res->subsys);
474 hlist_add_head(&res->hlist, hhead);
475
Paul Menage817929e2007-10-18 23:39:36 -0700476 write_unlock(&css_set_lock);
477
478 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700479}
480
Paul Menageddbcc7e2007-10-18 23:39:30 -0700481/*
482 * There is one global cgroup mutex. We also require taking
483 * task_lock() when dereferencing a task's cgroup subsys pointers.
484 * See "The task_lock() exception", at the end of this comment.
485 *
486 * A task must hold cgroup_mutex to modify cgroups.
487 *
488 * Any task can increment and decrement the count field without lock.
489 * So in general, code holding cgroup_mutex can't rely on the count
490 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800491 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700492 * means that no tasks are currently attached, therefore there is no
493 * way a task attached to that cgroup can fork (the other way to
494 * increment the count). So code holding cgroup_mutex can safely
495 * assume that if the count is zero, it will stay zero. Similarly, if
496 * a task holds cgroup_mutex on a cgroup with zero count, it
497 * knows that the cgroup won't be removed, as cgroup_rmdir()
498 * needs that mutex.
499 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700500 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
501 * (usually) take cgroup_mutex. These are the two most performance
502 * critical pieces of code here. The exception occurs on cgroup_exit(),
503 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
504 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800505 * to the release agent with the name of the cgroup (path relative to
506 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700507 *
508 * A cgroup can only be deleted if both its 'count' of using tasks
509 * is zero, and its list of 'children' cgroups is empty. Since all
510 * tasks in the system use _some_ cgroup, and since there is always at
511 * least one task in the system (init, pid == 1), therefore, top_cgroup
512 * always has either children cgroups and/or using tasks. So we don't
513 * need a special hack to ensure that top_cgroup cannot be deleted.
514 *
515 * The task_lock() exception
516 *
517 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800518 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800519 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700520 * several performance critical places that need to reference
521 * task->cgroup without the expense of grabbing a system global
522 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800523 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700524 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
525 * the task_struct routinely used for such matters.
526 *
527 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800528 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700529 */
530
Paul Menageddbcc7e2007-10-18 23:39:30 -0700531/**
532 * cgroup_lock - lock out any changes to cgroup structures
533 *
534 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700535void cgroup_lock(void)
536{
537 mutex_lock(&cgroup_mutex);
538}
539
540/**
541 * cgroup_unlock - release lock on cgroup changes
542 *
543 * Undo the lock taken in a previous cgroup_lock() call.
544 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700545void cgroup_unlock(void)
546{
547 mutex_unlock(&cgroup_mutex);
548}
549
550/*
551 * A couple of forward declarations required, due to cyclic reference loop:
552 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
553 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
554 * -> cgroup_mkdir.
555 */
556
557static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
558static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700559static int cgroup_populate_dir(struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700560static struct inode_operations cgroup_dir_inode_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700561static struct file_operations proc_cgroupstats_operations;
562
563static struct backing_dev_info cgroup_backing_dev_info = {
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700564 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700565};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700566
567static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
568{
569 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700570
571 if (inode) {
572 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100573 inode->i_uid = current_fsuid();
574 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700575 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
576 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
577 }
578 return inode;
579}
580
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800581/*
582 * Call subsys's pre_destroy handler.
583 * This is called before css refcnt check.
584 */
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800585static void cgroup_call_pre_destroy(struct cgroup *cgrp)
586{
587 struct cgroup_subsys *ss;
588 for_each_subsys(cgrp->root, ss)
Li Zefan75139b82009-01-07 18:07:33 -0800589 if (ss->pre_destroy)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800590 ss->pre_destroy(ss, cgrp);
591 return;
592}
593
Paul Menageddbcc7e2007-10-18 23:39:30 -0700594static void cgroup_diput(struct dentry *dentry, struct inode *inode)
595{
596 /* is dentry a directory ? if so, kfree() associated cgroup */
597 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700598 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800599 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700600 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700601 /* It's possible for external users to be holding css
602 * reference counts on a cgroup; css_put() needs to
603 * be able to access the cgroup after decrementing
604 * the reference count in order to know if it needs to
605 * queue the cgroup to be handled by the release
606 * agent */
607 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800608
609 mutex_lock(&cgroup_mutex);
610 /*
611 * Release the subsystem state objects.
612 */
Li Zefan75139b82009-01-07 18:07:33 -0800613 for_each_subsys(cgrp->root, ss)
614 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800615
616 cgrp->root->number_of_cgroups--;
617 mutex_unlock(&cgroup_mutex);
618
619 /* Drop the active superblock reference that we took when we
620 * created the cgroup */
621 deactivate_super(cgrp->root->sb);
622
Paul Menagebd89aab2007-10-18 23:40:44 -0700623 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700624 }
625 iput(inode);
626}
627
628static void remove_dir(struct dentry *d)
629{
630 struct dentry *parent = dget(d->d_parent);
631
632 d_delete(d);
633 simple_rmdir(parent->d_inode, d);
634 dput(parent);
635}
636
637static void cgroup_clear_directory(struct dentry *dentry)
638{
639 struct list_head *node;
640
641 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
642 spin_lock(&dcache_lock);
643 node = dentry->d_subdirs.next;
644 while (node != &dentry->d_subdirs) {
645 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
646 list_del_init(node);
647 if (d->d_inode) {
648 /* This should never be called on a cgroup
649 * directory with child cgroups */
650 BUG_ON(d->d_inode->i_mode & S_IFDIR);
651 d = dget_locked(d);
652 spin_unlock(&dcache_lock);
653 d_delete(d);
654 simple_unlink(dentry->d_inode, d);
655 dput(d);
656 spin_lock(&dcache_lock);
657 }
658 node = dentry->d_subdirs.next;
659 }
660 spin_unlock(&dcache_lock);
661}
662
663/*
664 * NOTE : the dentry must have been dget()'ed
665 */
666static void cgroup_d_remove_dir(struct dentry *dentry)
667{
668 cgroup_clear_directory(dentry);
669
670 spin_lock(&dcache_lock);
671 list_del_init(&dentry->d_u.d_child);
672 spin_unlock(&dcache_lock);
673 remove_dir(dentry);
674}
675
676static int rebind_subsystems(struct cgroupfs_root *root,
677 unsigned long final_bits)
678{
679 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700680 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700681 int i;
682
683 removed_bits = root->actual_subsys_bits & ~final_bits;
684 added_bits = final_bits & ~root->actual_subsys_bits;
685 /* Check that any added subsystems are currently free */
686 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800687 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700688 struct cgroup_subsys *ss = subsys[i];
689 if (!(bit & added_bits))
690 continue;
691 if (ss->root != &rootnode) {
692 /* Subsystem isn't free */
693 return -EBUSY;
694 }
695 }
696
697 /* Currently we don't handle adding/removing subsystems when
698 * any child cgroups exist. This is theoretically supportable
699 * but involves complex error handling, so it's being left until
700 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800701 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700702 return -EBUSY;
703
704 /* Process each subsystem */
705 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
706 struct cgroup_subsys *ss = subsys[i];
707 unsigned long bit = 1UL << i;
708 if (bit & added_bits) {
709 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700710 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700711 BUG_ON(!dummytop->subsys[i]);
712 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -0700713 cgrp->subsys[i] = dummytop->subsys[i];
714 cgrp->subsys[i]->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700715 list_add(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800716 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700717 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700718 ss->bind(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700719
720 } else if (bit & removed_bits) {
721 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700722 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
723 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700724 if (ss->bind)
725 ss->bind(ss, dummytop);
726 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700727 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800728 subsys[i]->root = &rootnode;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700729 list_del(&ss->sibling);
730 } else if (bit & final_bits) {
731 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700732 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700733 } else {
734 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700735 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700736 }
737 }
738 root->subsys_bits = root->actual_subsys_bits = final_bits;
739 synchronize_rcu();
740
741 return 0;
742}
743
744static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
745{
746 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
747 struct cgroup_subsys *ss;
748
749 mutex_lock(&cgroup_mutex);
750 for_each_subsys(root, ss)
751 seq_printf(seq, ",%s", ss->name);
752 if (test_bit(ROOT_NOPREFIX, &root->flags))
753 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700754 if (strlen(root->release_agent_path))
755 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700756 mutex_unlock(&cgroup_mutex);
757 return 0;
758}
759
760struct cgroup_sb_opts {
761 unsigned long subsys_bits;
762 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700763 char *release_agent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700764};
765
766/* Convert a hierarchy specifier into a bitmask of subsystems and
767 * flags. */
768static int parse_cgroupfs_options(char *data,
769 struct cgroup_sb_opts *opts)
770{
771 char *token, *o = data ?: "all";
772
773 opts->subsys_bits = 0;
774 opts->flags = 0;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700775 opts->release_agent = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700776
777 while ((token = strsep(&o, ",")) != NULL) {
778 if (!*token)
779 return -EINVAL;
780 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700781 /* Add all non-disabled subsystems */
782 int i;
783 opts->subsys_bits = 0;
784 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
785 struct cgroup_subsys *ss = subsys[i];
786 if (!ss->disabled)
787 opts->subsys_bits |= 1ul << i;
788 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700789 } else if (!strcmp(token, "noprefix")) {
790 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700791 } else if (!strncmp(token, "release_agent=", 14)) {
792 /* Specifying two release agents is forbidden */
793 if (opts->release_agent)
794 return -EINVAL;
795 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
796 if (!opts->release_agent)
797 return -ENOMEM;
798 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
799 opts->release_agent[PATH_MAX - 1] = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700800 } else {
801 struct cgroup_subsys *ss;
802 int i;
803 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
804 ss = subsys[i];
805 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700806 if (!ss->disabled)
807 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808 break;
809 }
810 }
811 if (i == CGROUP_SUBSYS_COUNT)
812 return -ENOENT;
813 }
814 }
815
816 /* We can't have an empty hierarchy */
817 if (!opts->subsys_bits)
818 return -EINVAL;
819
820 return 0;
821}
822
823static int cgroup_remount(struct super_block *sb, int *flags, char *data)
824{
825 int ret = 0;
826 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -0700827 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700828 struct cgroup_sb_opts opts;
829
Paul Menagebd89aab2007-10-18 23:40:44 -0700830 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700831 mutex_lock(&cgroup_mutex);
832
833 /* See what subsystems are wanted */
834 ret = parse_cgroupfs_options(data, &opts);
835 if (ret)
836 goto out_unlock;
837
838 /* Don't allow flags to change at remount */
839 if (opts.flags != root->flags) {
840 ret = -EINVAL;
841 goto out_unlock;
842 }
843
844 ret = rebind_subsystems(root, opts.subsys_bits);
845
846 /* (re)populate subsystem files */
847 if (!ret)
Paul Menagebd89aab2007-10-18 23:40:44 -0700848 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700849
Paul Menage81a6a5c2007-10-18 23:39:38 -0700850 if (opts.release_agent)
851 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700852 out_unlock:
Paul Menage81a6a5c2007-10-18 23:39:38 -0700853 if (opts.release_agent)
854 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700855 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700856 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700857 return ret;
858}
859
860static struct super_operations cgroup_ops = {
861 .statfs = simple_statfs,
862 .drop_inode = generic_delete_inode,
863 .show_options = cgroup_show_options,
864 .remount_fs = cgroup_remount,
865};
866
Paul Menagecc31edc2008-10-18 20:28:04 -0700867static void init_cgroup_housekeeping(struct cgroup *cgrp)
868{
869 INIT_LIST_HEAD(&cgrp->sibling);
870 INIT_LIST_HEAD(&cgrp->children);
871 INIT_LIST_HEAD(&cgrp->css_sets);
872 INIT_LIST_HEAD(&cgrp->release_list);
873 init_rwsem(&cgrp->pids_mutex);
874}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700875static void init_cgroup_root(struct cgroupfs_root *root)
876{
Paul Menagebd89aab2007-10-18 23:40:44 -0700877 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700878 INIT_LIST_HEAD(&root->subsys_list);
879 INIT_LIST_HEAD(&root->root_list);
880 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -0700881 cgrp->root = root;
882 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -0700883 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700884}
885
886static int cgroup_test_super(struct super_block *sb, void *data)
887{
888 struct cgroupfs_root *new = data;
889 struct cgroupfs_root *root = sb->s_fs_info;
890
891 /* First check subsystems */
892 if (new->subsys_bits != root->subsys_bits)
893 return 0;
894
895 /* Next check flags */
896 if (new->flags != root->flags)
897 return 0;
898
899 return 1;
900}
901
902static int cgroup_set_super(struct super_block *sb, void *data)
903{
904 int ret;
905 struct cgroupfs_root *root = data;
906
907 ret = set_anon_super(sb, NULL);
908 if (ret)
909 return ret;
910
911 sb->s_fs_info = root;
912 root->sb = sb;
913
914 sb->s_blocksize = PAGE_CACHE_SIZE;
915 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
916 sb->s_magic = CGROUP_SUPER_MAGIC;
917 sb->s_op = &cgroup_ops;
918
919 return 0;
920}
921
922static int cgroup_get_rootdir(struct super_block *sb)
923{
924 struct inode *inode =
925 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
926 struct dentry *dentry;
927
928 if (!inode)
929 return -ENOMEM;
930
Paul Menageddbcc7e2007-10-18 23:39:30 -0700931 inode->i_fop = &simple_dir_operations;
932 inode->i_op = &cgroup_dir_inode_operations;
933 /* directories start off with i_nlink == 2 (for "." entry) */
934 inc_nlink(inode);
935 dentry = d_alloc_root(inode);
936 if (!dentry) {
937 iput(inode);
938 return -ENOMEM;
939 }
940 sb->s_root = dentry;
941 return 0;
942}
943
944static int cgroup_get_sb(struct file_system_type *fs_type,
945 int flags, const char *unused_dev_name,
946 void *data, struct vfsmount *mnt)
947{
948 struct cgroup_sb_opts opts;
949 int ret = 0;
950 struct super_block *sb;
951 struct cgroupfs_root *root;
Li Zefan28fd5df2008-04-29 01:00:13 -0700952 struct list_head tmp_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700953
954 /* First find the desired set of subsystems */
955 ret = parse_cgroupfs_options(data, &opts);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700956 if (ret) {
957 if (opts.release_agent)
958 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700959 return ret;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700960 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700961
962 root = kzalloc(sizeof(*root), GFP_KERNEL);
Li Zefanf7770732008-02-23 15:24:10 -0800963 if (!root) {
964 if (opts.release_agent)
965 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700966 return -ENOMEM;
Li Zefanf7770732008-02-23 15:24:10 -0800967 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968
969 init_cgroup_root(root);
970 root->subsys_bits = opts.subsys_bits;
971 root->flags = opts.flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700972 if (opts.release_agent) {
973 strcpy(root->release_agent_path, opts.release_agent);
974 kfree(opts.release_agent);
975 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976
977 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
978
979 if (IS_ERR(sb)) {
980 kfree(root);
981 return PTR_ERR(sb);
982 }
983
984 if (sb->s_fs_info != root) {
985 /* Reusing an existing superblock */
986 BUG_ON(sb->s_root == NULL);
987 kfree(root);
988 root = NULL;
989 } else {
990 /* New superblock */
Paul Menagebd89aab2007-10-18 23:40:44 -0700991 struct cgroup *cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700992 struct inode *inode;
Li Zefan28fd5df2008-04-29 01:00:13 -0700993 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994
995 BUG_ON(sb->s_root != NULL);
996
997 ret = cgroup_get_rootdir(sb);
998 if (ret)
999 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001000 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001
Paul Menage817929e2007-10-18 23:39:36 -07001002 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 mutex_lock(&cgroup_mutex);
1004
Paul Menage817929e2007-10-18 23:39:36 -07001005 /*
1006 * We're accessing css_set_count without locking
1007 * css_set_lock here, but that's OK - it can only be
1008 * increased by someone holding cgroup_lock, and
1009 * that's us. The worst that can happen is that we
1010 * have some link structures left over
1011 */
1012 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1013 if (ret) {
1014 mutex_unlock(&cgroup_mutex);
1015 mutex_unlock(&inode->i_mutex);
1016 goto drop_new_super;
1017 }
1018
Paul Menageddbcc7e2007-10-18 23:39:30 -07001019 ret = rebind_subsystems(root, root->subsys_bits);
1020 if (ret == -EBUSY) {
1021 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001022 mutex_unlock(&inode->i_mutex);
Li Zefan20ca9b32008-12-23 13:57:14 -08001023 goto free_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024 }
1025
1026 /* EBUSY should be the only error here */
1027 BUG_ON(ret);
1028
1029 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001030 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031
1032 sb->s_root->d_fsdata = &root->top_cgroup;
1033 root->top_cgroup.dentry = sb->s_root;
1034
Paul Menage817929e2007-10-18 23:39:36 -07001035 /* Link the top cgroup in this hierarchy into all
1036 * the css_set objects */
1037 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001038 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1039 struct hlist_head *hhead = &css_set_table[i];
1040 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001041 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001042
1043 hlist_for_each_entry(cg, node, hhead, hlist) {
1044 struct cg_cgroup_link *link;
1045
1046 BUG_ON(list_empty(&tmp_cg_links));
1047 link = list_entry(tmp_cg_links.next,
1048 struct cg_cgroup_link,
1049 cgrp_link_list);
1050 list_del(&link->cgrp_link_list);
1051 link->cg = cg;
1052 list_add(&link->cgrp_link_list,
1053 &root->top_cgroup.css_sets);
1054 list_add(&link->cg_link_list, &cg->cg_links);
1055 }
1056 }
Paul Menage817929e2007-10-18 23:39:36 -07001057 write_unlock(&css_set_lock);
1058
1059 free_cg_links(&tmp_cg_links);
1060
Paul Menagebd89aab2007-10-18 23:40:44 -07001061 BUG_ON(!list_empty(&cgrp->sibling));
1062 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 BUG_ON(root->number_of_cgroups != 1);
1064
Paul Menagebd89aab2007-10-18 23:40:44 -07001065 cgroup_populate_dir(cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001066 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067 mutex_unlock(&cgroup_mutex);
1068 }
1069
1070 return simple_set_mnt(mnt, sb);
1071
Li Zefan20ca9b32008-12-23 13:57:14 -08001072 free_cg_links:
1073 free_cg_links(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074 drop_new_super:
1075 up_write(&sb->s_umount);
1076 deactivate_super(sb);
1077 return ret;
1078}
1079
1080static void cgroup_kill_sb(struct super_block *sb) {
1081 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001082 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001084 struct cg_cgroup_link *link;
1085 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086
1087 BUG_ON(!root);
1088
1089 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001090 BUG_ON(!list_empty(&cgrp->children));
1091 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092
1093 mutex_lock(&cgroup_mutex);
1094
1095 /* Rebind all subsystems back to the default hierarchy */
1096 ret = rebind_subsystems(root, 0);
1097 /* Shouldn't be able to fail ... */
1098 BUG_ON(ret);
1099
Paul Menage817929e2007-10-18 23:39:36 -07001100 /*
1101 * Release all the links from css_sets to this hierarchy's
1102 * root cgroup
1103 */
1104 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001105
1106 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1107 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001108 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001109 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001110 kfree(link);
1111 }
1112 write_unlock(&css_set_lock);
1113
1114 if (!list_empty(&root->root_list)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001115 list_del(&root->root_list);
Paul Menage817929e2007-10-18 23:39:36 -07001116 root_count--;
1117 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118 mutex_unlock(&cgroup_mutex);
1119
1120 kfree(root);
1121 kill_litter_super(sb);
1122}
1123
1124static struct file_system_type cgroup_fs_type = {
1125 .name = "cgroup",
1126 .get_sb = cgroup_get_sb,
1127 .kill_sb = cgroup_kill_sb,
1128};
1129
Paul Menagebd89aab2007-10-18 23:40:44 -07001130static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131{
1132 return dentry->d_fsdata;
1133}
1134
1135static inline struct cftype *__d_cft(struct dentry *dentry)
1136{
1137 return dentry->d_fsdata;
1138}
1139
Li Zefana043e3b2008-02-23 15:24:09 -08001140/**
1141 * cgroup_path - generate the path of a cgroup
1142 * @cgrp: the cgroup in question
1143 * @buf: the buffer to write the path into
1144 * @buflen: the length of the buffer
1145 *
1146 * Called with cgroup_mutex held. Writes path of cgroup into buf.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001147 * Returns 0 on success, -errno on error.
1148 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001149int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001150{
1151 char *start;
1152
Paul Menagebd89aab2007-10-18 23:40:44 -07001153 if (cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001154 /*
1155 * Inactive subsystems have no dentry for their root
1156 * cgroup
1157 */
1158 strcpy(buf, "/");
1159 return 0;
1160 }
1161
1162 start = buf + buflen;
1163
1164 *--start = '\0';
1165 for (;;) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001166 int len = cgrp->dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001167 if ((start -= len) < buf)
1168 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001169 memcpy(start, cgrp->dentry->d_name.name, len);
1170 cgrp = cgrp->parent;
1171 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001172 break;
Paul Menagebd89aab2007-10-18 23:40:44 -07001173 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001174 continue;
1175 if (--start < buf)
1176 return -ENAMETOOLONG;
1177 *start = '/';
1178 }
1179 memmove(buf, start, buf + buflen - start);
1180 return 0;
1181}
1182
Paul Menagebbcb81d2007-10-18 23:39:32 -07001183/*
1184 * Return the first subsystem attached to a cgroup's hierarchy, and
1185 * its subsystem id.
1186 */
1187
Paul Menagebd89aab2007-10-18 23:40:44 -07001188static void get_first_subsys(const struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001189 struct cgroup_subsys_state **css, int *subsys_id)
1190{
Paul Menagebd89aab2007-10-18 23:40:44 -07001191 const struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001192 const struct cgroup_subsys *test_ss;
1193 BUG_ON(list_empty(&root->subsys_list));
1194 test_ss = list_entry(root->subsys_list.next,
1195 struct cgroup_subsys, sibling);
1196 if (css) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001197 *css = cgrp->subsys[test_ss->subsys_id];
Paul Menagebbcb81d2007-10-18 23:39:32 -07001198 BUG_ON(!*css);
1199 }
1200 if (subsys_id)
1201 *subsys_id = test_ss->subsys_id;
1202}
1203
Li Zefana043e3b2008-02-23 15:24:09 -08001204/**
1205 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1206 * @cgrp: the cgroup the task is attaching to
1207 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001208 *
Li Zefana043e3b2008-02-23 15:24:09 -08001209 * Call holding cgroup_mutex. May take task_lock of
1210 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001211 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001212int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001213{
1214 int retval = 0;
1215 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07001216 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001217 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001218 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001219 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001220 int subsys_id;
1221
Paul Menagebd89aab2007-10-18 23:40:44 -07001222 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001223
1224 /* Nothing to do if the task is already in that cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -07001225 oldcgrp = task_cgroup(tsk, subsys_id);
1226 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001227 return 0;
1228
1229 for_each_subsys(root, ss) {
1230 if (ss->can_attach) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001231 retval = ss->can_attach(ss, cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001232 if (retval)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001233 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001234 }
1235 }
1236
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001237 task_lock(tsk);
1238 cg = tsk->cgroups;
1239 get_css_set(cg);
1240 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001241 /*
1242 * Locate or allocate a new css_set for this task,
1243 * based on its final set of cgroups
1244 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001245 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001246 put_css_set(cg);
Paul Jacksone18f6312008-02-07 00:13:44 -08001247 if (!newcg)
Paul Menage817929e2007-10-18 23:39:36 -07001248 return -ENOMEM;
Paul Menage817929e2007-10-18 23:39:36 -07001249
Paul Menagebbcb81d2007-10-18 23:39:32 -07001250 task_lock(tsk);
1251 if (tsk->flags & PF_EXITING) {
1252 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001253 put_css_set(newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001254 return -ESRCH;
1255 }
Paul Menage817929e2007-10-18 23:39:36 -07001256 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001257 task_unlock(tsk);
1258
Paul Menage817929e2007-10-18 23:39:36 -07001259 /* Update the css_set linked lists if we're using them */
1260 write_lock(&css_set_lock);
1261 if (!list_empty(&tsk->cg_list)) {
1262 list_del(&tsk->cg_list);
1263 list_add(&tsk->cg_list, &newcg->tasks);
1264 }
1265 write_unlock(&css_set_lock);
1266
Paul Menagebbcb81d2007-10-18 23:39:32 -07001267 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001268 if (ss->attach)
Paul Menagebd89aab2007-10-18 23:40:44 -07001269 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001270 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001271 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001272 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001273 put_css_set(cg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001274 return 0;
1275}
1276
1277/*
Paul Menageaf351022008-07-25 01:47:01 -07001278 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1279 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001280 */
Paul Menageaf351022008-07-25 01:47:01 -07001281static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001282{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001283 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001284 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001285 int ret;
1286
Paul Menagebbcb81d2007-10-18 23:39:32 -07001287 if (pid) {
1288 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001289 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001290 if (!tsk || tsk->flags & PF_EXITING) {
1291 rcu_read_unlock();
1292 return -ESRCH;
1293 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001294
David Howellsc69e8d92008-11-14 10:39:19 +11001295 tcred = __task_cred(tsk);
1296 if (cred->euid &&
1297 cred->euid != tcred->uid &&
1298 cred->euid != tcred->suid) {
1299 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001300 return -EACCES;
1301 }
David Howellsc69e8d92008-11-14 10:39:19 +11001302 get_task_struct(tsk);
1303 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001304 } else {
1305 tsk = current;
1306 get_task_struct(tsk);
1307 }
1308
Cliff Wickman956db3c2008-02-07 00:14:43 -08001309 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001310 put_task_struct(tsk);
1311 return ret;
1312}
1313
Paul Menageaf351022008-07-25 01:47:01 -07001314static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1315{
1316 int ret;
1317 if (!cgroup_lock_live_group(cgrp))
1318 return -ENODEV;
1319 ret = attach_task_by_pid(cgrp, pid);
1320 cgroup_unlock();
1321 return ret;
1322}
1323
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324/* The various types of files and directories in a cgroup file system */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325enum cgroup_filetype {
1326 FILE_ROOT,
1327 FILE_DIR,
1328 FILE_TASKLIST,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001329 FILE_NOTIFY_ON_RELEASE,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001330 FILE_RELEASE_AGENT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331};
1332
Paul Menagee788e0662008-07-25 01:46:59 -07001333/**
1334 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1335 * @cgrp: the cgroup to be checked for liveness
1336 *
Paul Menage84eea842008-07-25 01:47:00 -07001337 * On success, returns true; the lock should be later released with
1338 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e0662008-07-25 01:46:59 -07001339 */
Paul Menage84eea842008-07-25 01:47:00 -07001340bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e0662008-07-25 01:46:59 -07001341{
1342 mutex_lock(&cgroup_mutex);
1343 if (cgroup_is_removed(cgrp)) {
1344 mutex_unlock(&cgroup_mutex);
1345 return false;
1346 }
1347 return true;
1348}
1349
1350static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1351 const char *buffer)
1352{
1353 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1354 if (!cgroup_lock_live_group(cgrp))
1355 return -ENODEV;
1356 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001357 cgroup_unlock();
Paul Menagee788e0662008-07-25 01:46:59 -07001358 return 0;
1359}
1360
1361static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1362 struct seq_file *seq)
1363{
1364 if (!cgroup_lock_live_group(cgrp))
1365 return -ENODEV;
1366 seq_puts(seq, cgrp->root->release_agent_path);
1367 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001368 cgroup_unlock();
Paul Menagee788e0662008-07-25 01:46:59 -07001369 return 0;
1370}
1371
Paul Menage84eea842008-07-25 01:47:00 -07001372/* A buffer size big enough for numbers or short strings */
1373#define CGROUP_LOCAL_BUFFER_SIZE 64
1374
Paul Menagee73d2c62008-04-29 01:00:06 -07001375static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001376 struct file *file,
1377 const char __user *userbuf,
1378 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001379{
Paul Menage84eea842008-07-25 01:47:00 -07001380 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001381 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001382 char *end;
1383
1384 if (!nbytes)
1385 return -EINVAL;
1386 if (nbytes >= sizeof(buffer))
1387 return -E2BIG;
1388 if (copy_from_user(buffer, userbuf, nbytes))
1389 return -EFAULT;
1390
1391 buffer[nbytes] = 0; /* nul-terminate */
Paul Menageb7269df2008-04-29 00:59:59 -07001392 strstrip(buffer);
Paul Menagee73d2c62008-04-29 01:00:06 -07001393 if (cft->write_u64) {
1394 u64 val = simple_strtoull(buffer, &end, 0);
1395 if (*end)
1396 return -EINVAL;
1397 retval = cft->write_u64(cgrp, cft, val);
1398 } else {
1399 s64 val = simple_strtoll(buffer, &end, 0);
1400 if (*end)
1401 return -EINVAL;
1402 retval = cft->write_s64(cgrp, cft, val);
1403 }
Paul Menage355e0c42007-10-18 23:39:33 -07001404 if (!retval)
1405 retval = nbytes;
1406 return retval;
1407}
1408
Paul Menagedb3b1492008-07-25 01:46:58 -07001409static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1410 struct file *file,
1411 const char __user *userbuf,
1412 size_t nbytes, loff_t *unused_ppos)
1413{
Paul Menage84eea842008-07-25 01:47:00 -07001414 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001415 int retval = 0;
1416 size_t max_bytes = cft->max_write_len;
1417 char *buffer = local_buffer;
1418
1419 if (!max_bytes)
1420 max_bytes = sizeof(local_buffer) - 1;
1421 if (nbytes >= max_bytes)
1422 return -E2BIG;
1423 /* Allocate a dynamic buffer if we need one */
1424 if (nbytes >= sizeof(local_buffer)) {
1425 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1426 if (buffer == NULL)
1427 return -ENOMEM;
1428 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001429 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1430 retval = -EFAULT;
1431 goto out;
1432 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001433
1434 buffer[nbytes] = 0; /* nul-terminate */
1435 strstrip(buffer);
1436 retval = cft->write_string(cgrp, cft, buffer);
1437 if (!retval)
1438 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001439out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001440 if (buffer != local_buffer)
1441 kfree(buffer);
1442 return retval;
1443}
1444
Paul Menageddbcc7e2007-10-18 23:39:30 -07001445static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1446 size_t nbytes, loff_t *ppos)
1447{
1448 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001449 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001450
Li Zefan75139b82009-01-07 18:07:33 -08001451 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001452 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001453 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001454 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001455 if (cft->write_u64 || cft->write_s64)
1456 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001457 if (cft->write_string)
1458 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001459 if (cft->trigger) {
1460 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1461 return ret ? ret : nbytes;
1462 }
Paul Menage355e0c42007-10-18 23:39:33 -07001463 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001464}
1465
Paul Menagef4c753b2008-04-29 00:59:56 -07001466static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1467 struct file *file,
1468 char __user *buf, size_t nbytes,
1469 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001470{
Paul Menage84eea842008-07-25 01:47:00 -07001471 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001472 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001473 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1474
1475 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1476}
1477
Paul Menagee73d2c62008-04-29 01:00:06 -07001478static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1479 struct file *file,
1480 char __user *buf, size_t nbytes,
1481 loff_t *ppos)
1482{
Paul Menage84eea842008-07-25 01:47:00 -07001483 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001484 s64 val = cft->read_s64(cgrp, cft);
1485 int len = sprintf(tmp, "%lld\n", (long long) val);
1486
1487 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1488}
1489
Paul Menageddbcc7e2007-10-18 23:39:30 -07001490static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1491 size_t nbytes, loff_t *ppos)
1492{
1493 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001494 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001495
Li Zefan75139b82009-01-07 18:07:33 -08001496 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001497 return -ENODEV;
1498
1499 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001500 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001501 if (cft->read_u64)
1502 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001503 if (cft->read_s64)
1504 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001505 return -EINVAL;
1506}
1507
Paul Menage91796562008-04-29 01:00:01 -07001508/*
1509 * seqfile ops/methods for returning structured data. Currently just
1510 * supports string->u64 maps, but can be extended in future.
1511 */
1512
1513struct cgroup_seqfile_state {
1514 struct cftype *cft;
1515 struct cgroup *cgroup;
1516};
1517
1518static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1519{
1520 struct seq_file *sf = cb->state;
1521 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1522}
1523
1524static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1525{
1526 struct cgroup_seqfile_state *state = m->private;
1527 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001528 if (cft->read_map) {
1529 struct cgroup_map_cb cb = {
1530 .fill = cgroup_map_add,
1531 .state = m,
1532 };
1533 return cft->read_map(state->cgroup, cft, &cb);
1534 }
1535 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001536}
1537
Adrian Bunk96930a62008-07-25 19:46:21 -07001538static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07001539{
1540 struct seq_file *seq = file->private_data;
1541 kfree(seq->private);
1542 return single_release(inode, file);
1543}
1544
1545static struct file_operations cgroup_seqfile_operations = {
1546 .read = seq_read,
Paul Menagee788e0662008-07-25 01:46:59 -07001547 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07001548 .llseek = seq_lseek,
1549 .release = cgroup_seqfile_release,
1550};
1551
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552static int cgroup_file_open(struct inode *inode, struct file *file)
1553{
1554 int err;
1555 struct cftype *cft;
1556
1557 err = generic_file_open(inode, file);
1558 if (err)
1559 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08001561
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001562 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001563 struct cgroup_seqfile_state *state =
1564 kzalloc(sizeof(*state), GFP_USER);
1565 if (!state)
1566 return -ENOMEM;
1567 state->cft = cft;
1568 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1569 file->f_op = &cgroup_seqfile_operations;
1570 err = single_open(file, cgroup_seqfile_show, state);
1571 if (err < 0)
1572 kfree(state);
1573 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574 err = cft->open(inode, file);
1575 else
1576 err = 0;
1577
1578 return err;
1579}
1580
1581static int cgroup_file_release(struct inode *inode, struct file *file)
1582{
1583 struct cftype *cft = __d_cft(file->f_dentry);
1584 if (cft->release)
1585 return cft->release(inode, file);
1586 return 0;
1587}
1588
1589/*
1590 * cgroup_rename - Only allow simple rename of directories in place.
1591 */
1592static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1593 struct inode *new_dir, struct dentry *new_dentry)
1594{
1595 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1596 return -ENOTDIR;
1597 if (new_dentry->d_inode)
1598 return -EEXIST;
1599 if (old_dir != new_dir)
1600 return -EIO;
1601 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1602}
1603
1604static struct file_operations cgroup_file_operations = {
1605 .read = cgroup_file_read,
1606 .write = cgroup_file_write,
1607 .llseek = generic_file_llseek,
1608 .open = cgroup_file_open,
1609 .release = cgroup_file_release,
1610};
1611
1612static struct inode_operations cgroup_dir_inode_operations = {
1613 .lookup = simple_lookup,
1614 .mkdir = cgroup_mkdir,
1615 .rmdir = cgroup_rmdir,
1616 .rename = cgroup_rename,
1617};
1618
1619static int cgroup_create_file(struct dentry *dentry, int mode,
1620 struct super_block *sb)
1621{
1622 static struct dentry_operations cgroup_dops = {
1623 .d_iput = cgroup_diput,
1624 };
1625
1626 struct inode *inode;
1627
1628 if (!dentry)
1629 return -ENOENT;
1630 if (dentry->d_inode)
1631 return -EEXIST;
1632
1633 inode = cgroup_new_inode(mode, sb);
1634 if (!inode)
1635 return -ENOMEM;
1636
1637 if (S_ISDIR(mode)) {
1638 inode->i_op = &cgroup_dir_inode_operations;
1639 inode->i_fop = &simple_dir_operations;
1640
1641 /* start off with i_nlink == 2 (for "." entry) */
1642 inc_nlink(inode);
1643
1644 /* start with the directory inode held, so that we can
1645 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07001646 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647 } else if (S_ISREG(mode)) {
1648 inode->i_size = 0;
1649 inode->i_fop = &cgroup_file_operations;
1650 }
1651 dentry->d_op = &cgroup_dops;
1652 d_instantiate(dentry, inode);
1653 dget(dentry); /* Extra count - pin the dentry in core */
1654 return 0;
1655}
1656
1657/*
Li Zefana043e3b2008-02-23 15:24:09 -08001658 * cgroup_create_dir - create a directory for an object.
1659 * @cgrp: the cgroup we create the directory for. It must have a valid
1660 * ->parent field. And we are going to fill its ->dentry field.
1661 * @dentry: dentry of the new cgroup
1662 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001664static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665 int mode)
1666{
1667 struct dentry *parent;
1668 int error = 0;
1669
Paul Menagebd89aab2007-10-18 23:40:44 -07001670 parent = cgrp->parent->dentry;
1671 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001673 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674 inc_nlink(parent->d_inode);
Paul Menagebd89aab2007-10-18 23:40:44 -07001675 cgrp->dentry = dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 dget(dentry);
1677 }
1678 dput(dentry);
1679
1680 return error;
1681}
1682
Paul Menagebd89aab2007-10-18 23:40:44 -07001683int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684 struct cgroup_subsys *subsys,
1685 const struct cftype *cft)
1686{
Paul Menagebd89aab2007-10-18 23:40:44 -07001687 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001688 struct dentry *dentry;
1689 int error;
1690
1691 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07001692 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693 strcpy(name, subsys->name);
1694 strcat(name, ".");
1695 }
1696 strcat(name, cft->name);
1697 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1698 dentry = lookup_one_len(name, dir, strlen(name));
1699 if (!IS_ERR(dentry)) {
1700 error = cgroup_create_file(dentry, 0644 | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07001701 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001702 if (!error)
1703 dentry->d_fsdata = (void *)cft;
1704 dput(dentry);
1705 } else
1706 error = PTR_ERR(dentry);
1707 return error;
1708}
1709
Paul Menagebd89aab2007-10-18 23:40:44 -07001710int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001711 struct cgroup_subsys *subsys,
1712 const struct cftype cft[],
1713 int count)
1714{
1715 int i, err;
1716 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001717 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718 if (err)
1719 return err;
1720 }
1721 return 0;
1722}
1723
Li Zefana043e3b2008-02-23 15:24:09 -08001724/**
1725 * cgroup_task_count - count the number of tasks in a cgroup.
1726 * @cgrp: the cgroup in question
1727 *
1728 * Return the number of tasks in the cgroup.
1729 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001730int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001731{
1732 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001733 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001734
Paul Menage817929e2007-10-18 23:39:36 -07001735 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001736 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07001737 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07001738 }
1739 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001740 return count;
1741}
1742
1743/*
Paul Menage817929e2007-10-18 23:39:36 -07001744 * Advance a list_head iterator. The iterator should be positioned at
1745 * the start of a css_set
1746 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001747static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001748 struct cgroup_iter *it)
1749{
1750 struct list_head *l = it->cg_link;
1751 struct cg_cgroup_link *link;
1752 struct css_set *cg;
1753
1754 /* Advance to the next non-empty css_set */
1755 do {
1756 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07001757 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001758 it->cg_link = NULL;
1759 return;
1760 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001761 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001762 cg = link->cg;
1763 } while (list_empty(&cg->tasks));
1764 it->cg_link = l;
1765 it->task = cg->tasks.next;
1766}
1767
Cliff Wickman31a7df02008-02-07 00:14:42 -08001768/*
1769 * To reduce the fork() overhead for systems that are not actually
1770 * using their cgroups capability, we don't maintain the lists running
1771 * through each css_set to its tasks until we see the list actually
1772 * used - in other words after the first call to cgroup_iter_start().
1773 *
1774 * The tasklist_lock is not held here, as do_each_thread() and
1775 * while_each_thread() are protected by RCU.
1776 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07001777static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08001778{
1779 struct task_struct *p, *g;
1780 write_lock(&css_set_lock);
1781 use_task_css_set_links = 1;
1782 do_each_thread(g, p) {
1783 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08001784 /*
1785 * We should check if the process is exiting, otherwise
1786 * it will race with cgroup_exit() in that the list
1787 * entry won't be deleted though the process has exited.
1788 */
1789 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08001790 list_add(&p->cg_list, &p->cgroups->tasks);
1791 task_unlock(p);
1792 } while_each_thread(g, p);
1793 write_unlock(&css_set_lock);
1794}
1795
Paul Menagebd89aab2007-10-18 23:40:44 -07001796void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001797{
1798 /*
1799 * The first time anyone tries to iterate across a cgroup,
1800 * we need to enable the list linking each css_set to its
1801 * tasks, and fix up all existing tasks.
1802 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08001803 if (!use_task_css_set_links)
1804 cgroup_enable_task_cg_lists();
1805
Paul Menage817929e2007-10-18 23:39:36 -07001806 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001807 it->cg_link = &cgrp->css_sets;
1808 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001809}
1810
Paul Menagebd89aab2007-10-18 23:40:44 -07001811struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001812 struct cgroup_iter *it)
1813{
1814 struct task_struct *res;
1815 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001816 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07001817
1818 /* If the iterator cg is NULL, we have no tasks */
1819 if (!it->cg_link)
1820 return NULL;
1821 res = list_entry(l, struct task_struct, cg_list);
1822 /* Advance iterator to find next entry */
1823 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001824 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
1825 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07001826 /* We reached the end of this task list - move on to
1827 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07001828 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001829 } else {
1830 it->task = l;
1831 }
1832 return res;
1833}
1834
Paul Menagebd89aab2007-10-18 23:40:44 -07001835void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001836{
1837 read_unlock(&css_set_lock);
1838}
1839
Cliff Wickman31a7df02008-02-07 00:14:42 -08001840static inline int started_after_time(struct task_struct *t1,
1841 struct timespec *time,
1842 struct task_struct *t2)
1843{
1844 int start_diff = timespec_compare(&t1->start_time, time);
1845 if (start_diff > 0) {
1846 return 1;
1847 } else if (start_diff < 0) {
1848 return 0;
1849 } else {
1850 /*
1851 * Arbitrarily, if two processes started at the same
1852 * time, we'll say that the lower pointer value
1853 * started first. Note that t2 may have exited by now
1854 * so this may not be a valid pointer any longer, but
1855 * that's fine - it still serves to distinguish
1856 * between two tasks started (effectively) simultaneously.
1857 */
1858 return t1 > t2;
1859 }
1860}
1861
1862/*
1863 * This function is a callback from heap_insert() and is used to order
1864 * the heap.
1865 * In this case we order the heap in descending task start time.
1866 */
1867static inline int started_after(void *p1, void *p2)
1868{
1869 struct task_struct *t1 = p1;
1870 struct task_struct *t2 = p2;
1871 return started_after_time(t1, &t2->start_time, t2);
1872}
1873
1874/**
1875 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1876 * @scan: struct cgroup_scanner containing arguments for the scan
1877 *
1878 * Arguments include pointers to callback functions test_task() and
1879 * process_task().
1880 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1881 * and if it returns true, call process_task() for it also.
1882 * The test_task pointer may be NULL, meaning always true (select all tasks).
1883 * Effectively duplicates cgroup_iter_{start,next,end}()
1884 * but does not lock css_set_lock for the call to process_task().
1885 * The struct cgroup_scanner may be embedded in any structure of the caller's
1886 * creation.
1887 * It is guaranteed that process_task() will act on every task that
1888 * is a member of the cgroup for the duration of this call. This
1889 * function may or may not call process_task() for tasks that exit
1890 * or move to a different cgroup during the call, or are forked or
1891 * move into the cgroup during the call.
1892 *
1893 * Note that test_task() may be called with locks held, and may in some
1894 * situations be called multiple times for the same task, so it should
1895 * be cheap.
1896 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1897 * pre-allocated and will be used for heap operations (and its "gt" member will
1898 * be overwritten), else a temporary heap will be used (allocation of which
1899 * may cause this function to fail).
1900 */
1901int cgroup_scan_tasks(struct cgroup_scanner *scan)
1902{
1903 int retval, i;
1904 struct cgroup_iter it;
1905 struct task_struct *p, *dropped;
1906 /* Never dereference latest_task, since it's not refcounted */
1907 struct task_struct *latest_task = NULL;
1908 struct ptr_heap tmp_heap;
1909 struct ptr_heap *heap;
1910 struct timespec latest_time = { 0, 0 };
1911
1912 if (scan->heap) {
1913 /* The caller supplied our heap and pre-allocated its memory */
1914 heap = scan->heap;
1915 heap->gt = &started_after;
1916 } else {
1917 /* We need to allocate our own heap memory */
1918 heap = &tmp_heap;
1919 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1920 if (retval)
1921 /* cannot allocate the heap */
1922 return retval;
1923 }
1924
1925 again:
1926 /*
1927 * Scan tasks in the cgroup, using the scanner's "test_task" callback
1928 * to determine which are of interest, and using the scanner's
1929 * "process_task" callback to process any of them that need an update.
1930 * Since we don't want to hold any locks during the task updates,
1931 * gather tasks to be processed in a heap structure.
1932 * The heap is sorted by descending task start time.
1933 * If the statically-sized heap fills up, we overflow tasks that
1934 * started later, and in future iterations only consider tasks that
1935 * started after the latest task in the previous pass. This
1936 * guarantees forward progress and that we don't miss any tasks.
1937 */
1938 heap->size = 0;
1939 cgroup_iter_start(scan->cg, &it);
1940 while ((p = cgroup_iter_next(scan->cg, &it))) {
1941 /*
1942 * Only affect tasks that qualify per the caller's callback,
1943 * if he provided one
1944 */
1945 if (scan->test_task && !scan->test_task(p, scan))
1946 continue;
1947 /*
1948 * Only process tasks that started after the last task
1949 * we processed
1950 */
1951 if (!started_after_time(p, &latest_time, latest_task))
1952 continue;
1953 dropped = heap_insert(heap, p);
1954 if (dropped == NULL) {
1955 /*
1956 * The new task was inserted; the heap wasn't
1957 * previously full
1958 */
1959 get_task_struct(p);
1960 } else if (dropped != p) {
1961 /*
1962 * The new task was inserted, and pushed out a
1963 * different task
1964 */
1965 get_task_struct(p);
1966 put_task_struct(dropped);
1967 }
1968 /*
1969 * Else the new task was newer than anything already in
1970 * the heap and wasn't inserted
1971 */
1972 }
1973 cgroup_iter_end(scan->cg, &it);
1974
1975 if (heap->size) {
1976 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07001977 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08001978 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07001979 latest_time = q->start_time;
1980 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08001981 }
1982 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07001983 scan->process_task(q, scan);
1984 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08001985 }
1986 /*
1987 * If we had to process any tasks at all, scan again
1988 * in case some of them were in the middle of forking
1989 * children that didn't get processed.
1990 * Not the most efficient way to do it, but it avoids
1991 * having to take callback_mutex in the fork path
1992 */
1993 goto again;
1994 }
1995 if (heap == &tmp_heap)
1996 heap_free(&tmp_heap);
1997 return 0;
1998}
1999
Paul Menage817929e2007-10-18 23:39:36 -07002000/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07002001 * Stuff for reading the 'tasks' file.
2002 *
2003 * Reading this file can return large amounts of data if a cgroup has
2004 * *lots* of attached tasks. So it may need several calls to read(),
2005 * but we cannot guarantee that the information we produce is correct
2006 * unless we produce it entirely atomically.
2007 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002008 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002009
2010/*
2011 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
Paul Menagebd89aab2007-10-18 23:40:44 -07002012 * 'cgrp'. Return actual number of pids loaded. No need to
Paul Menagebbcb81d2007-10-18 23:39:32 -07002013 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2014 * read section, so the css_set can't go away, and is
2015 * immutable after creation.
2016 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002017static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002018{
2019 int n = 0;
Paul Menage817929e2007-10-18 23:39:36 -07002020 struct cgroup_iter it;
2021 struct task_struct *tsk;
Paul Menagebd89aab2007-10-18 23:40:44 -07002022 cgroup_iter_start(cgrp, &it);
2023 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Paul Menage817929e2007-10-18 23:39:36 -07002024 if (unlikely(n == npids))
2025 break;
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002026 pidarray[n++] = task_pid_vnr(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002027 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002028 cgroup_iter_end(cgrp, &it);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002029 return n;
2030}
2031
Balbir Singh846c7bb2007-10-18 23:39:44 -07002032/**
Li Zefana043e3b2008-02-23 15:24:09 -08002033 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002034 * @stats: cgroupstats to fill information into
2035 * @dentry: A dentry entry belonging to the cgroup for which stats have
2036 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002037 *
2038 * Build and fill cgroupstats so that taskstats can export it to user
2039 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002040 */
2041int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2042{
2043 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002044 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002045 struct cgroup_iter it;
2046 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002047
Balbir Singh846c7bb2007-10-18 23:39:44 -07002048 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002049 * Validate dentry by checking the superblock operations,
2050 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002051 */
Li Zefan33d283b2008-11-19 15:36:48 -08002052 if (dentry->d_sb->s_op != &cgroup_ops ||
2053 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002054 goto err;
2055
2056 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002057 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002058 rcu_read_lock();
2059
Paul Menagebd89aab2007-10-18 23:40:44 -07002060 cgroup_iter_start(cgrp, &it);
2061 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002062 switch (tsk->state) {
2063 case TASK_RUNNING:
2064 stats->nr_running++;
2065 break;
2066 case TASK_INTERRUPTIBLE:
2067 stats->nr_sleeping++;
2068 break;
2069 case TASK_UNINTERRUPTIBLE:
2070 stats->nr_uninterruptible++;
2071 break;
2072 case TASK_STOPPED:
2073 stats->nr_stopped++;
2074 break;
2075 default:
2076 if (delayacct_is_task_waiting_on_io(tsk))
2077 stats->nr_io_wait++;
2078 break;
2079 }
2080 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002081 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002082
2083 rcu_read_unlock();
2084err:
2085 return ret;
2086}
2087
Paul Menagebbcb81d2007-10-18 23:39:32 -07002088static int cmppid(const void *a, const void *b)
2089{
2090 return *(pid_t *)a - *(pid_t *)b;
2091}
2092
Paul Menagebbcb81d2007-10-18 23:39:32 -07002093
Paul Menagecc31edc2008-10-18 20:28:04 -07002094/*
2095 * seq_file methods for the "tasks" file. The seq_file position is the
2096 * next pid to display; the seq_file iterator is a pointer to the pid
2097 * in the cgroup->tasks_pids array.
2098 */
2099
2100static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
2101{
2102 /*
2103 * Initially we receive a position value that corresponds to
2104 * one more than the last pid shown (or 0 on the first call or
2105 * after a seek to the start). Use a binary-search to find the
2106 * next pid to display, if any
2107 */
2108 struct cgroup *cgrp = s->private;
2109 int index = 0, pid = *pos;
2110 int *iter;
2111
2112 down_read(&cgrp->pids_mutex);
2113 if (pid) {
2114 int end = cgrp->pids_length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002115
Paul Menagecc31edc2008-10-18 20:28:04 -07002116 while (index < end) {
2117 int mid = (index + end) / 2;
2118 if (cgrp->tasks_pids[mid] == pid) {
2119 index = mid;
2120 break;
2121 } else if (cgrp->tasks_pids[mid] <= pid)
2122 index = mid + 1;
2123 else
2124 end = mid;
2125 }
2126 }
2127 /* If we're off the end of the array, we're done */
2128 if (index >= cgrp->pids_length)
2129 return NULL;
2130 /* Update the abstract position to be the actual pid that we found */
2131 iter = cgrp->tasks_pids + index;
2132 *pos = *iter;
2133 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002134}
2135
Paul Menagecc31edc2008-10-18 20:28:04 -07002136static void cgroup_tasks_stop(struct seq_file *s, void *v)
2137{
2138 struct cgroup *cgrp = s->private;
2139 up_read(&cgrp->pids_mutex);
2140}
2141
2142static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
2143{
2144 struct cgroup *cgrp = s->private;
2145 int *p = v;
2146 int *end = cgrp->tasks_pids + cgrp->pids_length;
2147
2148 /*
2149 * Advance to the next pid in the array. If this goes off the
2150 * end, we're done
2151 */
2152 p++;
2153 if (p >= end) {
2154 return NULL;
2155 } else {
2156 *pos = *p;
2157 return p;
2158 }
2159}
2160
2161static int cgroup_tasks_show(struct seq_file *s, void *v)
2162{
2163 return seq_printf(s, "%d\n", *(int *)v);
2164}
2165
2166static struct seq_operations cgroup_tasks_seq_operations = {
2167 .start = cgroup_tasks_start,
2168 .stop = cgroup_tasks_stop,
2169 .next = cgroup_tasks_next,
2170 .show = cgroup_tasks_show,
2171};
2172
2173static void release_cgroup_pid_array(struct cgroup *cgrp)
2174{
2175 down_write(&cgrp->pids_mutex);
2176 BUG_ON(!cgrp->pids_use_count);
2177 if (!--cgrp->pids_use_count) {
2178 kfree(cgrp->tasks_pids);
2179 cgrp->tasks_pids = NULL;
2180 cgrp->pids_length = 0;
2181 }
2182 up_write(&cgrp->pids_mutex);
2183}
2184
2185static int cgroup_tasks_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002186{
Paul Menagebd89aab2007-10-18 23:40:44 -07002187 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002188
2189 if (!(file->f_mode & FMODE_READ))
2190 return 0;
2191
Paul Menagecc31edc2008-10-18 20:28:04 -07002192 release_cgroup_pid_array(cgrp);
2193 return seq_release(inode, file);
2194}
2195
2196static struct file_operations cgroup_tasks_operations = {
2197 .read = seq_read,
2198 .llseek = seq_lseek,
2199 .write = cgroup_file_write,
2200 .release = cgroup_tasks_release,
2201};
2202
2203/*
2204 * Handle an open on 'tasks' file. Prepare an array containing the
2205 * process id's of tasks currently attached to the cgroup being opened.
2206 */
2207
2208static int cgroup_tasks_open(struct inode *unused, struct file *file)
2209{
2210 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2211 pid_t *pidarray;
2212 int npids;
2213 int retval;
2214
2215 /* Nothing to do for write-only files */
2216 if (!(file->f_mode & FMODE_READ))
2217 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002218
2219 /*
2220 * If cgroup gets more users after we read count, we won't have
2221 * enough space - tough. This race is indistinguishable to the
2222 * caller from the case that the additional cgroup users didn't
2223 * show up until sometime later on.
2224 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002225 npids = cgroup_task_count(cgrp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002226 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2227 if (!pidarray)
2228 return -ENOMEM;
2229 npids = pid_array_load(pidarray, npids, cgrp);
2230 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002231
Paul Menagecc31edc2008-10-18 20:28:04 -07002232 /*
2233 * Store the array in the cgroup, freeing the old
2234 * array if necessary
2235 */
2236 down_write(&cgrp->pids_mutex);
2237 kfree(cgrp->tasks_pids);
2238 cgrp->tasks_pids = pidarray;
2239 cgrp->pids_length = npids;
2240 cgrp->pids_use_count++;
2241 up_write(&cgrp->pids_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002242
Paul Menagecc31edc2008-10-18 20:28:04 -07002243 file->f_op = &cgroup_tasks_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002244
Paul Menagecc31edc2008-10-18 20:28:04 -07002245 retval = seq_open(file, &cgroup_tasks_seq_operations);
2246 if (retval) {
2247 release_cgroup_pid_array(cgrp);
2248 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002249 }
Paul Menagecc31edc2008-10-18 20:28:04 -07002250 ((struct seq_file *)file->private_data)->private = cgrp;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002251 return 0;
2252}
2253
Paul Menagebd89aab2007-10-18 23:40:44 -07002254static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002255 struct cftype *cft)
2256{
Paul Menagebd89aab2007-10-18 23:40:44 -07002257 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002258}
2259
Paul Menage6379c102008-07-25 01:47:01 -07002260static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2261 struct cftype *cft,
2262 u64 val)
2263{
2264 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2265 if (val)
2266 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2267 else
2268 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2269 return 0;
2270}
2271
Paul Menagebbcb81d2007-10-18 23:39:32 -07002272/*
2273 * for the common functions, 'private' gives the type of file
2274 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07002275static struct cftype files[] = {
2276 {
2277 .name = "tasks",
2278 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07002279 .write_u64 = cgroup_tasks_write,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002280 .release = cgroup_tasks_release,
2281 .private = FILE_TASKLIST,
2282 },
2283
2284 {
2285 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002286 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07002287 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002288 .private = FILE_NOTIFY_ON_RELEASE,
2289 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002290};
2291
2292static struct cftype cft_release_agent = {
2293 .name = "release_agent",
Paul Menagee788e0662008-07-25 01:46:59 -07002294 .read_seq_string = cgroup_release_agent_show,
2295 .write_string = cgroup_release_agent_write,
2296 .max_write_len = PATH_MAX,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002297 .private = FILE_RELEASE_AGENT,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002298};
2299
Paul Menagebd89aab2007-10-18 23:40:44 -07002300static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002301{
2302 int err;
2303 struct cgroup_subsys *ss;
2304
2305 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002306 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002307
Paul Menagebd89aab2007-10-18 23:40:44 -07002308 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002309 if (err < 0)
2310 return err;
2311
Paul Menagebd89aab2007-10-18 23:40:44 -07002312 if (cgrp == cgrp->top_cgroup) {
2313 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002314 return err;
2315 }
2316
Paul Menagebd89aab2007-10-18 23:40:44 -07002317 for_each_subsys(cgrp->root, ss) {
2318 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002319 return err;
2320 }
2321
2322 return 0;
2323}
2324
2325static void init_cgroup_css(struct cgroup_subsys_state *css,
2326 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002327 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002328{
Paul Menagebd89aab2007-10-18 23:40:44 -07002329 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002330 atomic_set(&css->refcnt, 0);
2331 css->flags = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002332 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002333 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002334 BUG_ON(cgrp->subsys[ss->subsys_id]);
2335 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002336}
2337
2338/*
Li Zefana043e3b2008-02-23 15:24:09 -08002339 * cgroup_create - create a cgroup
2340 * @parent: cgroup that will be parent of the new cgroup
2341 * @dentry: dentry of the new cgroup
2342 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002343 *
Li Zefana043e3b2008-02-23 15:24:09 -08002344 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002345 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002346static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2347 int mode)
2348{
Paul Menagebd89aab2007-10-18 23:40:44 -07002349 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002350 struct cgroupfs_root *root = parent->root;
2351 int err = 0;
2352 struct cgroup_subsys *ss;
2353 struct super_block *sb = root->sb;
2354
Paul Menagebd89aab2007-10-18 23:40:44 -07002355 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2356 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002357 return -ENOMEM;
2358
2359 /* Grab a reference on the superblock so the hierarchy doesn't
2360 * get deleted on unmount if there are child cgroups. This
2361 * can be done outside cgroup_mutex, since the sb can't
2362 * disappear while someone has an open control file on the
2363 * fs */
2364 atomic_inc(&sb->s_active);
2365
2366 mutex_lock(&cgroup_mutex);
2367
Paul Menagecc31edc2008-10-18 20:28:04 -07002368 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002369
Paul Menagebd89aab2007-10-18 23:40:44 -07002370 cgrp->parent = parent;
2371 cgrp->root = parent->root;
2372 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002373
Li Zefanb6abdb02008-03-04 14:28:19 -08002374 if (notify_on_release(parent))
2375 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2376
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002378 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002379 if (IS_ERR(css)) {
2380 err = PTR_ERR(css);
2381 goto err_destroy;
2382 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002383 init_cgroup_css(css, ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002384 }
2385
Paul Menagebd89aab2007-10-18 23:40:44 -07002386 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002387 root->number_of_cgroups++;
2388
Paul Menagebd89aab2007-10-18 23:40:44 -07002389 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390 if (err < 0)
2391 goto err_remove;
2392
2393 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07002394 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002395
Paul Menagebd89aab2007-10-18 23:40:44 -07002396 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002397 /* If err < 0, we have a half-filled directory - oh well ;) */
2398
2399 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002400 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002401
2402 return 0;
2403
2404 err_remove:
2405
Paul Menagebd89aab2007-10-18 23:40:44 -07002406 list_del(&cgrp->sibling);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002407 root->number_of_cgroups--;
2408
2409 err_destroy:
2410
2411 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002412 if (cgrp->subsys[ss->subsys_id])
2413 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002414 }
2415
2416 mutex_unlock(&cgroup_mutex);
2417
2418 /* Release the reference count that we took on the superblock */
2419 deactivate_super(sb);
2420
Paul Menagebd89aab2007-10-18 23:40:44 -07002421 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002422 return err;
2423}
2424
2425static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2426{
2427 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2428
2429 /* the vfs holds inode->i_mutex already */
2430 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2431}
2432
Li Zefan55b6fd02008-07-29 22:33:20 -07002433static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002434{
2435 /* Check the reference count on each subsystem. Since we
2436 * already established that there are no tasks in the
2437 * cgroup, if the css refcount is also 0, then there should
2438 * be no outstanding references, so the subsystem is safe to
2439 * destroy. We scan across all subsystems rather than using
2440 * the per-hierarchy linked list of mounted subsystems since
2441 * we can be called via check_for_release() with no
2442 * synchronization other than RCU, and the subsystem linked
2443 * list isn't RCU-safe */
2444 int i;
2445 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2446 struct cgroup_subsys *ss = subsys[i];
2447 struct cgroup_subsys_state *css;
2448 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07002449 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002450 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07002451 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07002452 /* When called from check_for_release() it's possible
2453 * that by this point the cgroup has been removed
2454 * and the css deleted. But a false-positive doesn't
2455 * matter, since it can only happen if the cgroup
2456 * has been deleted and hence no longer needs the
2457 * release agent to be called anyway. */
Paul Jacksone18f6312008-02-07 00:13:44 -08002458 if (css && atomic_read(&css->refcnt))
Paul Menage81a6a5c2007-10-18 23:39:38 -07002459 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07002460 }
2461 return 0;
2462}
2463
Paul Menageddbcc7e2007-10-18 23:39:30 -07002464static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2465{
Paul Menagebd89aab2007-10-18 23:40:44 -07002466 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002467 struct dentry *d;
2468 struct cgroup *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002469
2470 /* the vfs holds both inode->i_mutex already */
2471
2472 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002473 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002474 mutex_unlock(&cgroup_mutex);
2475 return -EBUSY;
2476 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002477 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002478 mutex_unlock(&cgroup_mutex);
2479 return -EBUSY;
2480 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002481 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08002482
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002483 /*
Li Zefana043e3b2008-02-23 15:24:09 -08002484 * Call pre_destroy handlers of subsys. Notify subsystems
2485 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002486 */
2487 cgroup_call_pre_destroy(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002488
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002489 mutex_lock(&cgroup_mutex);
2490 parent = cgrp->parent;
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002491
2492 if (atomic_read(&cgrp->count)
2493 || !list_empty(&cgrp->children)
2494 || cgroup_has_css_refs(cgrp)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002495 mutex_unlock(&cgroup_mutex);
2496 return -EBUSY;
2497 }
2498
Paul Menage81a6a5c2007-10-18 23:39:38 -07002499 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002500 set_bit(CGRP_REMOVED, &cgrp->flags);
2501 if (!list_empty(&cgrp->release_list))
2502 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002503 spin_unlock(&release_list_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002504 /* delete my sibling from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07002505 list_del(&cgrp->sibling);
2506 spin_lock(&cgrp->dentry->d_lock);
2507 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002508 spin_unlock(&d->d_lock);
2509
2510 cgroup_d_remove_dir(d);
2511 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002512
Paul Menagebd89aab2007-10-18 23:40:44 -07002513 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002514 check_for_release(parent);
2515
Paul Menageddbcc7e2007-10-18 23:39:30 -07002516 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002517 return 0;
2518}
2519
Li Zefan06a11922008-04-29 01:00:07 -07002520static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002521{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002522 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08002523
2524 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002525
2526 /* Create the top cgroup state for this subsystem */
2527 ss->root = &rootnode;
2528 css = ss->create(ss, dummytop);
2529 /* We don't handle early failures gracefully */
2530 BUG_ON(IS_ERR(css));
2531 init_cgroup_css(css, ss, dummytop);
2532
Li Zefane8d55fd2008-04-29 01:00:13 -07002533 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07002534 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07002535 * newly registered, all tasks and hence the
2536 * init_css_set is in the subsystem's top cgroup. */
2537 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07002538
2539 need_forkexit_callback |= ss->fork || ss->exit;
2540
Li Zefane8d55fd2008-04-29 01:00:13 -07002541 /* At system boot, before all subsystems have been
2542 * registered, no tasks have been forked, so we don't
2543 * need to invoke fork callbacks here. */
2544 BUG_ON(!list_empty(&init_task.tasks));
2545
Paul Menageddbcc7e2007-10-18 23:39:30 -07002546 ss->active = 1;
2547}
2548
2549/**
Li Zefana043e3b2008-02-23 15:24:09 -08002550 * cgroup_init_early - cgroup initialization at system boot
2551 *
2552 * Initialize cgroups at system boot, and initialize any
2553 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002554 */
2555int __init cgroup_init_early(void)
2556{
2557 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002558 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07002559 INIT_LIST_HEAD(&init_css_set.cg_links);
2560 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07002561 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07002562 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002563 init_cgroup_root(&rootnode);
2564 list_add(&rootnode.root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07002565 root_count = 1;
2566 init_task.cgroups = &init_css_set;
2567
2568 init_css_set_link.cg = &init_css_set;
Paul Menagebd89aab2007-10-18 23:40:44 -07002569 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07002570 &rootnode.top_cgroup.css_sets);
2571 list_add(&init_css_set_link.cg_link_list,
2572 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002573
Li Zefan472b1052008-04-29 01:00:11 -07002574 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2575 INIT_HLIST_HEAD(&css_set_table[i]);
2576
Paul Menageddbcc7e2007-10-18 23:39:30 -07002577 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2578 struct cgroup_subsys *ss = subsys[i];
2579
2580 BUG_ON(!ss->name);
2581 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2582 BUG_ON(!ss->create);
2583 BUG_ON(!ss->destroy);
2584 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08002585 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07002586 ss->name, ss->subsys_id);
2587 BUG();
2588 }
2589
2590 if (ss->early_init)
2591 cgroup_init_subsys(ss);
2592 }
2593 return 0;
2594}
2595
2596/**
Li Zefana043e3b2008-02-23 15:24:09 -08002597 * cgroup_init - cgroup initialization
2598 *
2599 * Register cgroup filesystem and /proc file, and initialize
2600 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002601 */
2602int __init cgroup_init(void)
2603{
2604 int err;
2605 int i;
Li Zefan472b1052008-04-29 01:00:11 -07002606 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07002607
2608 err = bdi_init(&cgroup_backing_dev_info);
2609 if (err)
2610 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002611
2612 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2613 struct cgroup_subsys *ss = subsys[i];
2614 if (!ss->early_init)
2615 cgroup_init_subsys(ss);
2616 }
2617
Li Zefan472b1052008-04-29 01:00:11 -07002618 /* Add init_css_set to the hash table */
2619 hhead = css_set_hash(init_css_set.subsys);
2620 hlist_add_head(&init_css_set.hlist, hhead);
2621
Paul Menageddbcc7e2007-10-18 23:39:30 -07002622 err = register_filesystem(&cgroup_fs_type);
2623 if (err < 0)
2624 goto out;
2625
Li Zefan46ae2202008-04-29 01:00:08 -07002626 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07002627
Paul Menageddbcc7e2007-10-18 23:39:30 -07002628out:
Paul Menagea4243162007-10-18 23:39:35 -07002629 if (err)
2630 bdi_destroy(&cgroup_backing_dev_info);
2631
Paul Menageddbcc7e2007-10-18 23:39:30 -07002632 return err;
2633}
Paul Menageb4f48b62007-10-18 23:39:33 -07002634
Paul Menagea4243162007-10-18 23:39:35 -07002635/*
2636 * proc_cgroup_show()
2637 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2638 * - Used for /proc/<pid>/cgroup.
2639 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2640 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002641 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07002642 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2643 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2644 * cgroup to top_cgroup.
2645 */
2646
2647/* TODO: Use a proper seq_file iterator */
2648static int proc_cgroup_show(struct seq_file *m, void *v)
2649{
2650 struct pid *pid;
2651 struct task_struct *tsk;
2652 char *buf;
2653 int retval;
2654 struct cgroupfs_root *root;
2655
2656 retval = -ENOMEM;
2657 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2658 if (!buf)
2659 goto out;
2660
2661 retval = -ESRCH;
2662 pid = m->private;
2663 tsk = get_pid_task(pid, PIDTYPE_PID);
2664 if (!tsk)
2665 goto out_free;
2666
2667 retval = 0;
2668
2669 mutex_lock(&cgroup_mutex);
2670
2671 for_each_root(root) {
2672 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07002673 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07002674 int subsys_id;
2675 int count = 0;
2676
2677 /* Skip this hierarchy if it has no active subsystems */
2678 if (!root->actual_subsys_bits)
2679 continue;
Paul Menageb6c30062008-04-10 21:29:16 -07002680 seq_printf(m, "%lu:", root->subsys_bits);
Paul Menagea4243162007-10-18 23:39:35 -07002681 for_each_subsys(root, ss)
2682 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2683 seq_putc(m, ':');
2684 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07002685 cgrp = task_cgroup(tsk, subsys_id);
2686 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07002687 if (retval < 0)
2688 goto out_unlock;
2689 seq_puts(m, buf);
2690 seq_putc(m, '\n');
2691 }
2692
2693out_unlock:
2694 mutex_unlock(&cgroup_mutex);
2695 put_task_struct(tsk);
2696out_free:
2697 kfree(buf);
2698out:
2699 return retval;
2700}
2701
2702static int cgroup_open(struct inode *inode, struct file *file)
2703{
2704 struct pid *pid = PROC_I(inode)->pid;
2705 return single_open(file, proc_cgroup_show, pid);
2706}
2707
2708struct file_operations proc_cgroup_operations = {
2709 .open = cgroup_open,
2710 .read = seq_read,
2711 .llseek = seq_lseek,
2712 .release = single_release,
2713};
2714
2715/* Display information about each subsystem and each hierarchy */
2716static int proc_cgroupstats_show(struct seq_file *m, void *v)
2717{
2718 int i;
Paul Menagea4243162007-10-18 23:39:35 -07002719
Paul Menage8bab8dd2008-04-04 14:29:57 -07002720 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07002721 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07002722 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2723 struct cgroup_subsys *ss = subsys[i];
Paul Menage8bab8dd2008-04-04 14:29:57 -07002724 seq_printf(m, "%s\t%lu\t%d\t%d\n",
Paul Menage817929e2007-10-18 23:39:36 -07002725 ss->name, ss->root->subsys_bits,
Paul Menage8bab8dd2008-04-04 14:29:57 -07002726 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07002727 }
2728 mutex_unlock(&cgroup_mutex);
2729 return 0;
2730}
2731
2732static int cgroupstats_open(struct inode *inode, struct file *file)
2733{
Al Viro9dce07f2008-03-29 03:07:28 +00002734 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07002735}
2736
2737static struct file_operations proc_cgroupstats_operations = {
2738 .open = cgroupstats_open,
2739 .read = seq_read,
2740 .llseek = seq_lseek,
2741 .release = single_release,
2742};
2743
Paul Menageb4f48b62007-10-18 23:39:33 -07002744/**
2745 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08002746 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07002747 *
2748 * Description: A task inherits its parent's cgroup at fork().
2749 *
2750 * A pointer to the shared css_set was automatically copied in
2751 * fork.c by dup_task_struct(). However, we ignore that copy, since
2752 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08002753 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07002754 * have already changed current->cgroups, allowing the previously
2755 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07002756 *
2757 * At the point that cgroup_fork() is called, 'current' is the parent
2758 * task, and the passed argument 'child' points to the child task.
2759 */
2760void cgroup_fork(struct task_struct *child)
2761{
Paul Menage817929e2007-10-18 23:39:36 -07002762 task_lock(current);
2763 child->cgroups = current->cgroups;
2764 get_css_set(child->cgroups);
2765 task_unlock(current);
2766 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07002767}
2768
2769/**
Li Zefana043e3b2008-02-23 15:24:09 -08002770 * cgroup_fork_callbacks - run fork callbacks
2771 * @child: the new task
2772 *
2773 * Called on a new task very soon before adding it to the
2774 * tasklist. No need to take any locks since no-one can
2775 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07002776 */
2777void cgroup_fork_callbacks(struct task_struct *child)
2778{
2779 if (need_forkexit_callback) {
2780 int i;
2781 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2782 struct cgroup_subsys *ss = subsys[i];
2783 if (ss->fork)
2784 ss->fork(ss, child);
2785 }
2786 }
2787}
2788
2789/**
Li Zefana043e3b2008-02-23 15:24:09 -08002790 * cgroup_post_fork - called on a new task after adding it to the task list
2791 * @child: the task in question
2792 *
2793 * Adds the task to the list running through its css_set if necessary.
2794 * Has to be after the task is visible on the task list in case we race
2795 * with the first call to cgroup_iter_start() - to guarantee that the
2796 * new task ends up on its list.
2797 */
Paul Menage817929e2007-10-18 23:39:36 -07002798void cgroup_post_fork(struct task_struct *child)
2799{
2800 if (use_task_css_set_links) {
2801 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08002802 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07002803 if (list_empty(&child->cg_list))
2804 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08002805 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07002806 write_unlock(&css_set_lock);
2807 }
2808}
2809/**
Paul Menageb4f48b62007-10-18 23:39:33 -07002810 * cgroup_exit - detach cgroup from exiting task
2811 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08002812 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07002813 *
2814 * Description: Detach cgroup from @tsk and release it.
2815 *
2816 * Note that cgroups marked notify_on_release force every task in
2817 * them to take the global cgroup_mutex mutex when exiting.
2818 * This could impact scaling on very large systems. Be reluctant to
2819 * use notify_on_release cgroups where very high task exit scaling
2820 * is required on large systems.
2821 *
2822 * the_top_cgroup_hack:
2823 *
2824 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2825 *
2826 * We call cgroup_exit() while the task is still competent to
2827 * handle notify_on_release(), then leave the task attached to the
2828 * root cgroup in each hierarchy for the remainder of its exit.
2829 *
2830 * To do this properly, we would increment the reference count on
2831 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2832 * code we would add a second cgroup function call, to drop that
2833 * reference. This would just create an unnecessary hot spot on
2834 * the top_cgroup reference count, to no avail.
2835 *
2836 * Normally, holding a reference to a cgroup without bumping its
2837 * count is unsafe. The cgroup could go away, or someone could
2838 * attach us to a different cgroup, decrementing the count on
2839 * the first cgroup that we never incremented. But in this case,
2840 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002841 * which wards off any cgroup_attach_task() attempts, or task is a failed
2842 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07002843 */
2844void cgroup_exit(struct task_struct *tsk, int run_callbacks)
2845{
2846 int i;
Paul Menage817929e2007-10-18 23:39:36 -07002847 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07002848
2849 if (run_callbacks && need_forkexit_callback) {
2850 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2851 struct cgroup_subsys *ss = subsys[i];
2852 if (ss->exit)
2853 ss->exit(ss, tsk);
2854 }
2855 }
Paul Menage817929e2007-10-18 23:39:36 -07002856
2857 /*
2858 * Unlink from the css_set task list if necessary.
2859 * Optimistically check cg_list before taking
2860 * css_set_lock
2861 */
2862 if (!list_empty(&tsk->cg_list)) {
2863 write_lock(&css_set_lock);
2864 if (!list_empty(&tsk->cg_list))
2865 list_del(&tsk->cg_list);
2866 write_unlock(&css_set_lock);
2867 }
2868
Paul Menageb4f48b62007-10-18 23:39:33 -07002869 /* Reassign the task to the init_css_set. */
2870 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002871 cg = tsk->cgroups;
2872 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07002873 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002874 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002875 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07002876}
Paul Menage697f4162007-10-18 23:39:34 -07002877
2878/**
Li Zefana043e3b2008-02-23 15:24:09 -08002879 * cgroup_clone - clone the cgroup the given subsystem is attached to
2880 * @tsk: the task to be moved
2881 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07002882 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08002883 *
2884 * Duplicate the current cgroup in the hierarchy that the given
2885 * subsystem is attached to, and move this task into the new
2886 * child.
Paul Menage697f4162007-10-18 23:39:34 -07002887 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07002888int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
2889 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07002890{
2891 struct dentry *dentry;
2892 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07002893 struct cgroup *parent, *child;
2894 struct inode *inode;
2895 struct css_set *cg;
2896 struct cgroupfs_root *root;
2897 struct cgroup_subsys *ss;
2898
2899 /* We shouldn't be called by an unregistered subsystem */
2900 BUG_ON(!subsys->active);
2901
2902 /* First figure out what hierarchy and cgroup we're dealing
2903 * with, and pin them so we can drop cgroup_mutex */
2904 mutex_lock(&cgroup_mutex);
2905 again:
2906 root = subsys->root;
2907 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07002908 mutex_unlock(&cgroup_mutex);
2909 return 0;
2910 }
Lai Jiangshan104cbd52009-01-07 18:07:38 -08002911 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002912 cg = tsk->cgroups;
Paul Menage697f4162007-10-18 23:39:34 -07002913 parent = task_cgroup(tsk, subsys->subsys_id);
2914
Paul Menage697f4162007-10-18 23:39:34 -07002915 /* Pin the hierarchy */
Li Zefan7b574b72009-01-04 12:00:45 -08002916 if (!atomic_inc_not_zero(&parent->root->sb->s_active)) {
2917 /* We race with the final deactivate_super() */
2918 mutex_unlock(&cgroup_mutex);
2919 return 0;
2920 }
Paul Menage697f4162007-10-18 23:39:34 -07002921
Paul Menage817929e2007-10-18 23:39:36 -07002922 /* Keep the cgroup alive */
2923 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08002924 task_unlock(tsk);
Paul Menage697f4162007-10-18 23:39:34 -07002925 mutex_unlock(&cgroup_mutex);
2926
2927 /* Now do the VFS work to create a cgroup */
2928 inode = parent->dentry->d_inode;
2929
2930 /* Hold the parent directory mutex across this operation to
2931 * stop anyone else deleting the new cgroup */
2932 mutex_lock(&inode->i_mutex);
2933 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
2934 if (IS_ERR(dentry)) {
2935 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08002936 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07002937 PTR_ERR(dentry));
2938 ret = PTR_ERR(dentry);
2939 goto out_release;
2940 }
2941
2942 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08002943 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07002944 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07002945 dput(dentry);
2946 if (ret) {
2947 printk(KERN_INFO
2948 "Failed to create cgroup %s: %d\n", nodename,
2949 ret);
2950 goto out_release;
2951 }
2952
Paul Menage697f4162007-10-18 23:39:34 -07002953 /* The cgroup now exists. Retake cgroup_mutex and check
2954 * that we're still in the same state that we thought we
2955 * were. */
2956 mutex_lock(&cgroup_mutex);
2957 if ((root != subsys->root) ||
2958 (parent != task_cgroup(tsk, subsys->subsys_id))) {
2959 /* Aargh, we raced ... */
2960 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07002961 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07002962
2963 deactivate_super(parent->root->sb);
2964 /* The cgroup is still accessible in the VFS, but
2965 * we're not going to try to rmdir() it at this
2966 * point. */
2967 printk(KERN_INFO
2968 "Race in cgroup_clone() - leaking cgroup %s\n",
2969 nodename);
2970 goto again;
2971 }
2972
2973 /* do any required auto-setup */
2974 for_each_subsys(root, ss) {
2975 if (ss->post_clone)
2976 ss->post_clone(ss, child);
2977 }
2978
2979 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002980 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07002981 mutex_unlock(&cgroup_mutex);
2982
2983 out_release:
2984 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002985
2986 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07002987 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002988 mutex_unlock(&cgroup_mutex);
Paul Menage697f4162007-10-18 23:39:34 -07002989 deactivate_super(parent->root->sb);
2990 return ret;
2991}
2992
Li Zefana043e3b2008-02-23 15:24:09 -08002993/**
2994 * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
2995 * @cgrp: the cgroup in question
2996 *
2997 * See if @cgrp is a descendant of the current task's cgroup in
2998 * the appropriate hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07002999 *
3000 * If we are sending in dummytop, then presumably we are creating
3001 * the top cgroup in the subsystem.
3002 *
3003 * Called only by the ns (nsproxy) cgroup.
3004 */
Paul Menagebd89aab2007-10-18 23:40:44 -07003005int cgroup_is_descendant(const struct cgroup *cgrp)
Paul Menage697f4162007-10-18 23:39:34 -07003006{
3007 int ret;
3008 struct cgroup *target;
3009 int subsys_id;
3010
Paul Menagebd89aab2007-10-18 23:40:44 -07003011 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07003012 return 1;
3013
Paul Menagebd89aab2007-10-18 23:40:44 -07003014 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menage697f4162007-10-18 23:39:34 -07003015 target = task_cgroup(current, subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07003016 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3017 cgrp = cgrp->parent;
3018 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07003019 return ret;
3020}
Paul Menage81a6a5c2007-10-18 23:39:38 -07003021
Paul Menagebd89aab2007-10-18 23:40:44 -07003022static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003023{
3024 /* All of these checks rely on RCU to keep the cgroup
3025 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07003026 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3027 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003028 /* Control Group is currently removeable. If it's not
3029 * already queued for a userspace notification, queue
3030 * it now */
3031 int need_schedule_work = 0;
3032 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003033 if (!cgroup_is_removed(cgrp) &&
3034 list_empty(&cgrp->release_list)) {
3035 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003036 need_schedule_work = 1;
3037 }
3038 spin_unlock(&release_list_lock);
3039 if (need_schedule_work)
3040 schedule_work(&release_agent_work);
3041 }
3042}
3043
3044void __css_put(struct cgroup_subsys_state *css)
3045{
Paul Menagebd89aab2007-10-18 23:40:44 -07003046 struct cgroup *cgrp = css->cgroup;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003047 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -07003048 if (atomic_dec_and_test(&css->refcnt) && notify_on_release(cgrp)) {
3049 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3050 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003051 }
3052 rcu_read_unlock();
3053}
3054
3055/*
3056 * Notify userspace when a cgroup is released, by running the
3057 * configured release agent with the name of the cgroup (path
3058 * relative to the root of cgroup file system) as the argument.
3059 *
3060 * Most likely, this user command will try to rmdir this cgroup.
3061 *
3062 * This races with the possibility that some other task will be
3063 * attached to this cgroup before it is removed, or that some other
3064 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3065 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3066 * unused, and this cgroup will be reprieved from its death sentence,
3067 * to continue to serve a useful existence. Next time it's released,
3068 * we will get notified again, if it still has 'notify_on_release' set.
3069 *
3070 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3071 * means only wait until the task is successfully execve()'d. The
3072 * separate release agent task is forked by call_usermodehelper(),
3073 * then control in this thread returns here, without waiting for the
3074 * release agent task. We don't bother to wait because the caller of
3075 * this routine has no use for the exit status of the release agent
3076 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003077 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003078static void cgroup_release_agent(struct work_struct *work)
3079{
3080 BUG_ON(work != &release_agent_work);
3081 mutex_lock(&cgroup_mutex);
3082 spin_lock(&release_list_lock);
3083 while (!list_empty(&release_list)) {
3084 char *argv[3], *envp[3];
3085 int i;
Paul Menagee788e0662008-07-25 01:46:59 -07003086 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003087 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003088 struct cgroup,
3089 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003090 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003091 spin_unlock(&release_list_lock);
3092 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e0662008-07-25 01:46:59 -07003093 if (!pathbuf)
3094 goto continue_free;
3095 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3096 goto continue_free;
3097 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3098 if (!agentbuf)
3099 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003100
3101 i = 0;
Paul Menagee788e0662008-07-25 01:46:59 -07003102 argv[i++] = agentbuf;
3103 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003104 argv[i] = NULL;
3105
3106 i = 0;
3107 /* minimal command environment */
3108 envp[i++] = "HOME=/";
3109 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3110 envp[i] = NULL;
3111
3112 /* Drop the lock while we invoke the usermode helper,
3113 * since the exec could involve hitting disk and hence
3114 * be a slow process */
3115 mutex_unlock(&cgroup_mutex);
3116 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003117 mutex_lock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07003118 continue_free:
3119 kfree(pathbuf);
3120 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003121 spin_lock(&release_list_lock);
3122 }
3123 spin_unlock(&release_list_lock);
3124 mutex_unlock(&cgroup_mutex);
3125}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003126
3127static int __init cgroup_disable(char *str)
3128{
3129 int i;
3130 char *token;
3131
3132 while ((token = strsep(&str, ",")) != NULL) {
3133 if (!*token)
3134 continue;
3135
3136 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3137 struct cgroup_subsys *ss = subsys[i];
3138
3139 if (!strcmp(token, ss->name)) {
3140 ss->disabled = 1;
3141 printk(KERN_INFO "Disabling %s control group"
3142 " subsystem\n", ss->name);
3143 break;
3144 }
3145 }
3146 }
3147 return 1;
3148}
3149__setup("cgroup_disable=", cgroup_disable);