blob: 5a54ff42874ee916997139c859b8cfda725b9a33 [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 Zefan472b10532008-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
Li Zefane5f6a862009-01-07 18:07:41 -080087 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -070088 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
Li Zefane5f6a862009-01-07 18:07:41 -0800151/* for_each_active_root() allows you to iterate across the active hierarchies */
152#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700153list_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 Zefan472b10532008-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 Zefan472b10532008-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 Menagea47295e2009-01-07 18:07:44 -0800274 struct cgroup *cgrp = rcu_dereference(cg->subsys[i]->cgroup);
Paul Menagebd89aab2007-10-18 23:40:44 -0700275 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 Zefan472b10532008-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 Zefan472b10532008-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 Zefan472b10532008-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 Zefan472b10532008-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
Li Zefanc12f65d2009-01-07 18:07:42 -0800387/**
388 * link_css_set - a helper function to link a css_set to a cgroup
389 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
390 * @cg: the css_set to be linked
391 * @cgrp: the destination cgroup
392 */
393static void link_css_set(struct list_head *tmp_cg_links,
394 struct css_set *cg, struct cgroup *cgrp)
395{
396 struct cg_cgroup_link *link;
397
398 BUG_ON(list_empty(tmp_cg_links));
399 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
400 cgrp_link_list);
401 link->cg = cg;
402 list_move(&link->cgrp_link_list, &cgrp->css_sets);
403 list_add(&link->cg_link_list, &cg->cg_links);
404}
405
Li Zefan36553432008-07-29 22:33:19 -0700406/*
Paul Menage817929e2007-10-18 23:39:36 -0700407 * find_css_set() takes an existing cgroup group and a
408 * cgroup object, and returns a css_set object that's
409 * equivalent to the old group, but with the given cgroup
410 * substituted into the appropriate hierarchy. Must be called with
411 * cgroup_mutex held
412 */
Paul Menage817929e2007-10-18 23:39:36 -0700413static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700414 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700415{
416 struct css_set *res;
417 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
418 int i;
419
420 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700421
Li Zefan472b10532008-04-29 01:00:11 -0700422 struct hlist_head *hhead;
423
Paul Menage817929e2007-10-18 23:39:36 -0700424 /* First see if we already have a cgroup group that matches
425 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700426 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700427 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700428 if (res)
429 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700430 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700431
432 if (res)
433 return res;
434
435 res = kmalloc(sizeof(*res), GFP_KERNEL);
436 if (!res)
437 return NULL;
438
439 /* Allocate all the cg_cgroup_link objects that we'll need */
440 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
441 kfree(res);
442 return NULL;
443 }
444
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700445 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700446 INIT_LIST_HEAD(&res->cg_links);
447 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b10532008-04-29 01:00:11 -0700448 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700449
450 /* Copy the set of subsystem state objects generated in
451 * find_existing_css_set() */
452 memcpy(res->subsys, template, sizeof(res->subsys));
453
454 write_lock(&css_set_lock);
455 /* Add reference counts and links from the new css_set. */
456 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700457 struct cgroup *cgrp = res->subsys[i]->cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700458 struct cgroup_subsys *ss = subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -0700459 atomic_inc(&cgrp->count);
Paul Menage817929e2007-10-18 23:39:36 -0700460 /*
461 * We want to add a link once per cgroup, so we
462 * only do it for the first subsystem in each
463 * hierarchy
464 */
Li Zefanc12f65d2009-01-07 18:07:42 -0800465 if (ss->root->subsys_list.next == &ss->sibling)
466 link_css_set(&tmp_cg_links, res, cgrp);
Paul Menage817929e2007-10-18 23:39:36 -0700467 }
Li Zefanc12f65d2009-01-07 18:07:42 -0800468 if (list_empty(&rootnode.subsys_list))
469 link_css_set(&tmp_cg_links, res, dummytop);
Paul Menage817929e2007-10-18 23:39:36 -0700470
471 BUG_ON(!list_empty(&tmp_cg_links));
472
Paul Menage817929e2007-10-18 23:39:36 -0700473 css_set_count++;
Li Zefan472b10532008-04-29 01:00:11 -0700474
475 /* Add this cgroup group to the hash table */
476 hhead = css_set_hash(res->subsys);
477 hlist_add_head(&res->hlist, hhead);
478
Paul Menage817929e2007-10-18 23:39:36 -0700479 write_unlock(&css_set_lock);
480
481 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700482}
483
Paul Menageddbcc7e2007-10-18 23:39:30 -0700484/*
485 * There is one global cgroup mutex. We also require taking
486 * task_lock() when dereferencing a task's cgroup subsys pointers.
487 * See "The task_lock() exception", at the end of this comment.
488 *
489 * A task must hold cgroup_mutex to modify cgroups.
490 *
491 * Any task can increment and decrement the count field without lock.
492 * So in general, code holding cgroup_mutex can't rely on the count
493 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800494 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700495 * means that no tasks are currently attached, therefore there is no
496 * way a task attached to that cgroup can fork (the other way to
497 * increment the count). So code holding cgroup_mutex can safely
498 * assume that if the count is zero, it will stay zero. Similarly, if
499 * a task holds cgroup_mutex on a cgroup with zero count, it
500 * knows that the cgroup won't be removed, as cgroup_rmdir()
501 * needs that mutex.
502 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700503 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
504 * (usually) take cgroup_mutex. These are the two most performance
505 * critical pieces of code here. The exception occurs on cgroup_exit(),
506 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
507 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800508 * to the release agent with the name of the cgroup (path relative to
509 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700510 *
511 * A cgroup can only be deleted if both its 'count' of using tasks
512 * is zero, and its list of 'children' cgroups is empty. Since all
513 * tasks in the system use _some_ cgroup, and since there is always at
514 * least one task in the system (init, pid == 1), therefore, top_cgroup
515 * always has either children cgroups and/or using tasks. So we don't
516 * need a special hack to ensure that top_cgroup cannot be deleted.
517 *
518 * The task_lock() exception
519 *
520 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800521 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800522 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700523 * several performance critical places that need to reference
524 * task->cgroup without the expense of grabbing a system global
525 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800526 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700527 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
528 * the task_struct routinely used for such matters.
529 *
530 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800531 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700532 */
533
Paul Menageddbcc7e2007-10-18 23:39:30 -0700534/**
535 * cgroup_lock - lock out any changes to cgroup structures
536 *
537 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700538void cgroup_lock(void)
539{
540 mutex_lock(&cgroup_mutex);
541}
542
543/**
544 * cgroup_unlock - release lock on cgroup changes
545 *
546 * Undo the lock taken in a previous cgroup_lock() call.
547 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700548void cgroup_unlock(void)
549{
550 mutex_unlock(&cgroup_mutex);
551}
552
553/*
554 * A couple of forward declarations required, due to cyclic reference loop:
555 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
556 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
557 * -> cgroup_mkdir.
558 */
559
560static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
561static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700562static int cgroup_populate_dir(struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700563static struct inode_operations cgroup_dir_inode_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700564static struct file_operations proc_cgroupstats_operations;
565
566static struct backing_dev_info cgroup_backing_dev_info = {
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700567 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700568};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700569
570static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
571{
572 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700573
574 if (inode) {
575 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100576 inode->i_uid = current_fsuid();
577 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700578 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
579 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
580 }
581 return inode;
582}
583
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800584/*
585 * Call subsys's pre_destroy handler.
586 * This is called before css refcnt check.
587 */
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800588static void cgroup_call_pre_destroy(struct cgroup *cgrp)
589{
590 struct cgroup_subsys *ss;
591 for_each_subsys(cgrp->root, ss)
Li Zefan75139b82009-01-07 18:07:33 -0800592 if (ss->pre_destroy)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800593 ss->pre_destroy(ss, cgrp);
594 return;
595}
596
Paul Menagea47295e2009-01-07 18:07:44 -0800597static void free_cgroup_rcu(struct rcu_head *obj)
598{
599 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
600
601 kfree(cgrp);
602}
603
Paul Menageddbcc7e2007-10-18 23:39:30 -0700604static void cgroup_diput(struct dentry *dentry, struct inode *inode)
605{
606 /* is dentry a directory ? if so, kfree() associated cgroup */
607 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700608 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800609 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700610 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700611 /* It's possible for external users to be holding css
612 * reference counts on a cgroup; css_put() needs to
613 * be able to access the cgroup after decrementing
614 * the reference count in order to know if it needs to
615 * queue the cgroup to be handled by the release
616 * agent */
617 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800618
619 mutex_lock(&cgroup_mutex);
620 /*
621 * Release the subsystem state objects.
622 */
Li Zefan75139b82009-01-07 18:07:33 -0800623 for_each_subsys(cgrp->root, ss)
624 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800625
626 cgrp->root->number_of_cgroups--;
627 mutex_unlock(&cgroup_mutex);
628
Paul Menagea47295e2009-01-07 18:07:44 -0800629 /*
630 * Drop the active superblock reference that we took when we
631 * created the cgroup
632 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800633 deactivate_super(cgrp->root->sb);
634
Paul Menagea47295e2009-01-07 18:07:44 -0800635 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700636 }
637 iput(inode);
638}
639
640static void remove_dir(struct dentry *d)
641{
642 struct dentry *parent = dget(d->d_parent);
643
644 d_delete(d);
645 simple_rmdir(parent->d_inode, d);
646 dput(parent);
647}
648
649static void cgroup_clear_directory(struct dentry *dentry)
650{
651 struct list_head *node;
652
653 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
654 spin_lock(&dcache_lock);
655 node = dentry->d_subdirs.next;
656 while (node != &dentry->d_subdirs) {
657 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
658 list_del_init(node);
659 if (d->d_inode) {
660 /* This should never be called on a cgroup
661 * directory with child cgroups */
662 BUG_ON(d->d_inode->i_mode & S_IFDIR);
663 d = dget_locked(d);
664 spin_unlock(&dcache_lock);
665 d_delete(d);
666 simple_unlink(dentry->d_inode, d);
667 dput(d);
668 spin_lock(&dcache_lock);
669 }
670 node = dentry->d_subdirs.next;
671 }
672 spin_unlock(&dcache_lock);
673}
674
675/*
676 * NOTE : the dentry must have been dget()'ed
677 */
678static void cgroup_d_remove_dir(struct dentry *dentry)
679{
680 cgroup_clear_directory(dentry);
681
682 spin_lock(&dcache_lock);
683 list_del_init(&dentry->d_u.d_child);
684 spin_unlock(&dcache_lock);
685 remove_dir(dentry);
686}
687
688static int rebind_subsystems(struct cgroupfs_root *root,
689 unsigned long final_bits)
690{
691 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700692 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700693 int i;
694
695 removed_bits = root->actual_subsys_bits & ~final_bits;
696 added_bits = final_bits & ~root->actual_subsys_bits;
697 /* Check that any added subsystems are currently free */
698 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800699 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700700 struct cgroup_subsys *ss = subsys[i];
701 if (!(bit & added_bits))
702 continue;
703 if (ss->root != &rootnode) {
704 /* Subsystem isn't free */
705 return -EBUSY;
706 }
707 }
708
709 /* Currently we don't handle adding/removing subsystems when
710 * any child cgroups exist. This is theoretically supportable
711 * but involves complex error handling, so it's being left until
712 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800713 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700714 return -EBUSY;
715
716 /* Process each subsystem */
717 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
718 struct cgroup_subsys *ss = subsys[i];
719 unsigned long bit = 1UL << i;
720 if (bit & added_bits) {
721 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700722 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700723 BUG_ON(!dummytop->subsys[i]);
724 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800725 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700726 cgrp->subsys[i] = dummytop->subsys[i];
727 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800728 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800729 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700730 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700731 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800732 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700733 } else if (bit & removed_bits) {
734 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700735 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
736 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800737 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700738 if (ss->bind)
739 ss->bind(ss, dummytop);
740 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700741 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800742 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -0800743 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -0800744 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700745 } else if (bit & final_bits) {
746 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700747 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700748 } else {
749 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700750 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700751 }
752 }
753 root->subsys_bits = root->actual_subsys_bits = final_bits;
754 synchronize_rcu();
755
756 return 0;
757}
758
759static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
760{
761 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
762 struct cgroup_subsys *ss;
763
764 mutex_lock(&cgroup_mutex);
765 for_each_subsys(root, ss)
766 seq_printf(seq, ",%s", ss->name);
767 if (test_bit(ROOT_NOPREFIX, &root->flags))
768 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700769 if (strlen(root->release_agent_path))
770 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700771 mutex_unlock(&cgroup_mutex);
772 return 0;
773}
774
775struct cgroup_sb_opts {
776 unsigned long subsys_bits;
777 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700778 char *release_agent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700779};
780
781/* Convert a hierarchy specifier into a bitmask of subsystems and
782 * flags. */
783static int parse_cgroupfs_options(char *data,
784 struct cgroup_sb_opts *opts)
785{
786 char *token, *o = data ?: "all";
787
788 opts->subsys_bits = 0;
789 opts->flags = 0;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700790 opts->release_agent = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791
792 while ((token = strsep(&o, ",")) != NULL) {
793 if (!*token)
794 return -EINVAL;
795 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700796 /* Add all non-disabled subsystems */
797 int i;
798 opts->subsys_bits = 0;
799 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
800 struct cgroup_subsys *ss = subsys[i];
801 if (!ss->disabled)
802 opts->subsys_bits |= 1ul << i;
803 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700804 } else if (!strcmp(token, "noprefix")) {
805 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700806 } else if (!strncmp(token, "release_agent=", 14)) {
807 /* Specifying two release agents is forbidden */
808 if (opts->release_agent)
809 return -EINVAL;
810 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
811 if (!opts->release_agent)
812 return -ENOMEM;
813 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
814 opts->release_agent[PATH_MAX - 1] = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815 } else {
816 struct cgroup_subsys *ss;
817 int i;
818 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
819 ss = subsys[i];
820 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700821 if (!ss->disabled)
822 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700823 break;
824 }
825 }
826 if (i == CGROUP_SUBSYS_COUNT)
827 return -ENOENT;
828 }
829 }
830
831 /* We can't have an empty hierarchy */
832 if (!opts->subsys_bits)
833 return -EINVAL;
834
835 return 0;
836}
837
838static int cgroup_remount(struct super_block *sb, int *flags, char *data)
839{
840 int ret = 0;
841 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -0700842 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700843 struct cgroup_sb_opts opts;
844
Paul Menagebd89aab2007-10-18 23:40:44 -0700845 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700846 mutex_lock(&cgroup_mutex);
847
848 /* See what subsystems are wanted */
849 ret = parse_cgroupfs_options(data, &opts);
850 if (ret)
851 goto out_unlock;
852
853 /* Don't allow flags to change at remount */
854 if (opts.flags != root->flags) {
855 ret = -EINVAL;
856 goto out_unlock;
857 }
858
859 ret = rebind_subsystems(root, opts.subsys_bits);
860
861 /* (re)populate subsystem files */
862 if (!ret)
Paul Menagebd89aab2007-10-18 23:40:44 -0700863 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700864
Paul Menage81a6a5c2007-10-18 23:39:38 -0700865 if (opts.release_agent)
866 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700867 out_unlock:
Paul Menage81a6a5c2007-10-18 23:39:38 -0700868 if (opts.release_agent)
869 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700870 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700871 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700872 return ret;
873}
874
875static struct super_operations cgroup_ops = {
876 .statfs = simple_statfs,
877 .drop_inode = generic_delete_inode,
878 .show_options = cgroup_show_options,
879 .remount_fs = cgroup_remount,
880};
881
Paul Menagecc31edc2008-10-18 20:28:04 -0700882static void init_cgroup_housekeeping(struct cgroup *cgrp)
883{
884 INIT_LIST_HEAD(&cgrp->sibling);
885 INIT_LIST_HEAD(&cgrp->children);
886 INIT_LIST_HEAD(&cgrp->css_sets);
887 INIT_LIST_HEAD(&cgrp->release_list);
888 init_rwsem(&cgrp->pids_mutex);
889}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700890static void init_cgroup_root(struct cgroupfs_root *root)
891{
Paul Menagebd89aab2007-10-18 23:40:44 -0700892 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700893 INIT_LIST_HEAD(&root->subsys_list);
894 INIT_LIST_HEAD(&root->root_list);
895 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -0700896 cgrp->root = root;
897 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -0700898 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700899}
900
901static int cgroup_test_super(struct super_block *sb, void *data)
902{
903 struct cgroupfs_root *new = data;
904 struct cgroupfs_root *root = sb->s_fs_info;
905
906 /* First check subsystems */
907 if (new->subsys_bits != root->subsys_bits)
908 return 0;
909
910 /* Next check flags */
911 if (new->flags != root->flags)
912 return 0;
913
914 return 1;
915}
916
917static int cgroup_set_super(struct super_block *sb, void *data)
918{
919 int ret;
920 struct cgroupfs_root *root = data;
921
922 ret = set_anon_super(sb, NULL);
923 if (ret)
924 return ret;
925
926 sb->s_fs_info = root;
927 root->sb = sb;
928
929 sb->s_blocksize = PAGE_CACHE_SIZE;
930 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
931 sb->s_magic = CGROUP_SUPER_MAGIC;
932 sb->s_op = &cgroup_ops;
933
934 return 0;
935}
936
937static int cgroup_get_rootdir(struct super_block *sb)
938{
939 struct inode *inode =
940 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
941 struct dentry *dentry;
942
943 if (!inode)
944 return -ENOMEM;
945
Paul Menageddbcc7e2007-10-18 23:39:30 -0700946 inode->i_fop = &simple_dir_operations;
947 inode->i_op = &cgroup_dir_inode_operations;
948 /* directories start off with i_nlink == 2 (for "." entry) */
949 inc_nlink(inode);
950 dentry = d_alloc_root(inode);
951 if (!dentry) {
952 iput(inode);
953 return -ENOMEM;
954 }
955 sb->s_root = dentry;
956 return 0;
957}
958
959static int cgroup_get_sb(struct file_system_type *fs_type,
960 int flags, const char *unused_dev_name,
961 void *data, struct vfsmount *mnt)
962{
963 struct cgroup_sb_opts opts;
964 int ret = 0;
965 struct super_block *sb;
966 struct cgroupfs_root *root;
Li Zefan28fd5df2008-04-29 01:00:13 -0700967 struct list_head tmp_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968
969 /* First find the desired set of subsystems */
970 ret = parse_cgroupfs_options(data, &opts);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700971 if (ret) {
972 if (opts.release_agent)
973 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700974 return ret;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700975 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976
977 root = kzalloc(sizeof(*root), GFP_KERNEL);
Li Zefanf7770732008-02-23 15:24:10 -0800978 if (!root) {
979 if (opts.release_agent)
980 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700981 return -ENOMEM;
Li Zefanf7770732008-02-23 15:24:10 -0800982 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983
984 init_cgroup_root(root);
985 root->subsys_bits = opts.subsys_bits;
986 root->flags = opts.flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700987 if (opts.release_agent) {
988 strcpy(root->release_agent_path, opts.release_agent);
989 kfree(opts.release_agent);
990 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991
992 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
993
994 if (IS_ERR(sb)) {
995 kfree(root);
996 return PTR_ERR(sb);
997 }
998
999 if (sb->s_fs_info != root) {
1000 /* Reusing an existing superblock */
1001 BUG_ON(sb->s_root == NULL);
1002 kfree(root);
1003 root = NULL;
1004 } else {
1005 /* New superblock */
Li Zefanc12f65d2009-01-07 18:07:42 -08001006 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001007 struct inode *inode;
Li Zefan28fd5df2008-04-29 01:00:13 -07001008 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009
1010 BUG_ON(sb->s_root != NULL);
1011
1012 ret = cgroup_get_rootdir(sb);
1013 if (ret)
1014 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001015 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016
Paul Menage817929e2007-10-18 23:39:36 -07001017 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 mutex_lock(&cgroup_mutex);
1019
Paul Menage817929e2007-10-18 23:39:36 -07001020 /*
1021 * We're accessing css_set_count without locking
1022 * css_set_lock here, but that's OK - it can only be
1023 * increased by someone holding cgroup_lock, and
1024 * that's us. The worst that can happen is that we
1025 * have some link structures left over
1026 */
1027 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1028 if (ret) {
1029 mutex_unlock(&cgroup_mutex);
1030 mutex_unlock(&inode->i_mutex);
1031 goto drop_new_super;
1032 }
1033
Paul Menageddbcc7e2007-10-18 23:39:30 -07001034 ret = rebind_subsystems(root, root->subsys_bits);
1035 if (ret == -EBUSY) {
1036 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001037 mutex_unlock(&inode->i_mutex);
Li Zefan20ca9b32008-12-23 13:57:14 -08001038 goto free_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 }
1040
1041 /* EBUSY should be the only error here */
1042 BUG_ON(ret);
1043
1044 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001045 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046
Li Zefanc12f65d2009-01-07 18:07:42 -08001047 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048 root->top_cgroup.dentry = sb->s_root;
1049
Paul Menage817929e2007-10-18 23:39:36 -07001050 /* Link the top cgroup in this hierarchy into all
1051 * the css_set objects */
1052 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001053 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1054 struct hlist_head *hhead = &css_set_table[i];
1055 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001056 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001057
Li Zefanc12f65d2009-01-07 18:07:42 -08001058 hlist_for_each_entry(cg, node, hhead, hlist)
1059 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001060 }
Paul Menage817929e2007-10-18 23:39:36 -07001061 write_unlock(&css_set_lock);
1062
1063 free_cg_links(&tmp_cg_links);
1064
Li Zefanc12f65d2009-01-07 18:07:42 -08001065 BUG_ON(!list_empty(&root_cgrp->sibling));
1066 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067 BUG_ON(root->number_of_cgroups != 1);
1068
Li Zefanc12f65d2009-01-07 18:07:42 -08001069 cgroup_populate_dir(root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001070 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071 mutex_unlock(&cgroup_mutex);
1072 }
1073
1074 return simple_set_mnt(mnt, sb);
1075
Li Zefan20ca9b32008-12-23 13:57:14 -08001076 free_cg_links:
1077 free_cg_links(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078 drop_new_super:
1079 up_write(&sb->s_umount);
1080 deactivate_super(sb);
1081 return ret;
1082}
1083
1084static void cgroup_kill_sb(struct super_block *sb) {
1085 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001086 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001088 struct cg_cgroup_link *link;
1089 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001090
1091 BUG_ON(!root);
1092
1093 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001094 BUG_ON(!list_empty(&cgrp->children));
1095 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096
1097 mutex_lock(&cgroup_mutex);
1098
1099 /* Rebind all subsystems back to the default hierarchy */
1100 ret = rebind_subsystems(root, 0);
1101 /* Shouldn't be able to fail ... */
1102 BUG_ON(ret);
1103
Paul Menage817929e2007-10-18 23:39:36 -07001104 /*
1105 * Release all the links from css_sets to this hierarchy's
1106 * root cgroup
1107 */
1108 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001109
1110 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1111 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001112 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001113 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001114 kfree(link);
1115 }
1116 write_unlock(&css_set_lock);
1117
Paul Menage839ec542009-01-29 14:25:22 -08001118 if (!list_empty(&root->root_list)) {
1119 list_del(&root->root_list);
1120 root_count--;
1121 }
Li Zefane5f6a862009-01-07 18:07:41 -08001122
Paul Menageddbcc7e2007-10-18 23:39:30 -07001123 mutex_unlock(&cgroup_mutex);
1124
1125 kfree(root);
1126 kill_litter_super(sb);
1127}
1128
1129static struct file_system_type cgroup_fs_type = {
1130 .name = "cgroup",
1131 .get_sb = cgroup_get_sb,
1132 .kill_sb = cgroup_kill_sb,
1133};
1134
Paul Menagebd89aab2007-10-18 23:40:44 -07001135static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136{
1137 return dentry->d_fsdata;
1138}
1139
1140static inline struct cftype *__d_cft(struct dentry *dentry)
1141{
1142 return dentry->d_fsdata;
1143}
1144
Li Zefana043e3b2008-02-23 15:24:09 -08001145/**
1146 * cgroup_path - generate the path of a cgroup
1147 * @cgrp: the cgroup in question
1148 * @buf: the buffer to write the path into
1149 * @buflen: the length of the buffer
1150 *
Paul Menagea47295e2009-01-07 18:07:44 -08001151 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1152 * reference. Writes path of cgroup into buf. Returns 0 on success,
1153 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001154 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001155int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001156{
1157 char *start;
Paul Menagea47295e2009-01-07 18:07:44 -08001158 struct dentry *dentry = rcu_dereference(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001159
Paul Menagea47295e2009-01-07 18:07:44 -08001160 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001161 /*
1162 * Inactive subsystems have no dentry for their root
1163 * cgroup
1164 */
1165 strcpy(buf, "/");
1166 return 0;
1167 }
1168
1169 start = buf + buflen;
1170
1171 *--start = '\0';
1172 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001173 int len = dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001174 if ((start -= len) < buf)
1175 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001176 memcpy(start, cgrp->dentry->d_name.name, len);
1177 cgrp = cgrp->parent;
1178 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001179 break;
Paul Menagea47295e2009-01-07 18:07:44 -08001180 dentry = rcu_dereference(cgrp->dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001181 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001182 continue;
1183 if (--start < buf)
1184 return -ENAMETOOLONG;
1185 *start = '/';
1186 }
1187 memmove(buf, start, buf + buflen - start);
1188 return 0;
1189}
1190
Paul Menagebbcb81d2007-10-18 23:39:32 -07001191/*
1192 * Return the first subsystem attached to a cgroup's hierarchy, and
1193 * its subsystem id.
1194 */
1195
Paul Menagebd89aab2007-10-18 23:40:44 -07001196static void get_first_subsys(const struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001197 struct cgroup_subsys_state **css, int *subsys_id)
1198{
Paul Menagebd89aab2007-10-18 23:40:44 -07001199 const struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001200 const struct cgroup_subsys *test_ss;
1201 BUG_ON(list_empty(&root->subsys_list));
1202 test_ss = list_entry(root->subsys_list.next,
1203 struct cgroup_subsys, sibling);
1204 if (css) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001205 *css = cgrp->subsys[test_ss->subsys_id];
Paul Menagebbcb81d2007-10-18 23:39:32 -07001206 BUG_ON(!*css);
1207 }
1208 if (subsys_id)
1209 *subsys_id = test_ss->subsys_id;
1210}
1211
Li Zefana043e3b2008-02-23 15:24:09 -08001212/**
1213 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1214 * @cgrp: the cgroup the task is attaching to
1215 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001216 *
Li Zefana043e3b2008-02-23 15:24:09 -08001217 * Call holding cgroup_mutex. May take task_lock of
1218 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001219 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001220int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001221{
1222 int retval = 0;
1223 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07001224 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001225 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001226 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001227 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001228 int subsys_id;
1229
Paul Menagebd89aab2007-10-18 23:40:44 -07001230 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001231
1232 /* Nothing to do if the task is already in that cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -07001233 oldcgrp = task_cgroup(tsk, subsys_id);
1234 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001235 return 0;
1236
1237 for_each_subsys(root, ss) {
1238 if (ss->can_attach) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001239 retval = ss->can_attach(ss, cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001240 if (retval)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001241 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001242 }
1243 }
1244
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001245 task_lock(tsk);
1246 cg = tsk->cgroups;
1247 get_css_set(cg);
1248 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001249 /*
1250 * Locate or allocate a new css_set for this task,
1251 * based on its final set of cgroups
1252 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001253 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001254 put_css_set(cg);
Paul Jacksone18f6312008-02-07 00:13:44 -08001255 if (!newcg)
Paul Menage817929e2007-10-18 23:39:36 -07001256 return -ENOMEM;
Paul Menage817929e2007-10-18 23:39:36 -07001257
Paul Menagebbcb81d2007-10-18 23:39:32 -07001258 task_lock(tsk);
1259 if (tsk->flags & PF_EXITING) {
1260 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001261 put_css_set(newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001262 return -ESRCH;
1263 }
Paul Menage817929e2007-10-18 23:39:36 -07001264 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001265 task_unlock(tsk);
1266
Paul Menage817929e2007-10-18 23:39:36 -07001267 /* Update the css_set linked lists if we're using them */
1268 write_lock(&css_set_lock);
1269 if (!list_empty(&tsk->cg_list)) {
1270 list_del(&tsk->cg_list);
1271 list_add(&tsk->cg_list, &newcg->tasks);
1272 }
1273 write_unlock(&css_set_lock);
1274
Paul Menagebbcb81d2007-10-18 23:39:32 -07001275 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001276 if (ss->attach)
Paul Menagebd89aab2007-10-18 23:40:44 -07001277 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001278 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001279 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001280 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001281 put_css_set(cg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001282 return 0;
1283}
1284
1285/*
Paul Menageaf351022008-07-25 01:47:01 -07001286 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1287 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001288 */
Paul Menageaf351022008-07-25 01:47:01 -07001289static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001290{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001291 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001292 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001293 int ret;
1294
Paul Menagebbcb81d2007-10-18 23:39:32 -07001295 if (pid) {
1296 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001297 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001298 if (!tsk || tsk->flags & PF_EXITING) {
1299 rcu_read_unlock();
1300 return -ESRCH;
1301 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001302
David Howellsc69e8d92008-11-14 10:39:19 +11001303 tcred = __task_cred(tsk);
1304 if (cred->euid &&
1305 cred->euid != tcred->uid &&
1306 cred->euid != tcred->suid) {
1307 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001308 return -EACCES;
1309 }
David Howellsc69e8d92008-11-14 10:39:19 +11001310 get_task_struct(tsk);
1311 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001312 } else {
1313 tsk = current;
1314 get_task_struct(tsk);
1315 }
1316
Cliff Wickman956db3c2008-02-07 00:14:43 -08001317 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001318 put_task_struct(tsk);
1319 return ret;
1320}
1321
Paul Menageaf351022008-07-25 01:47:01 -07001322static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1323{
1324 int ret;
1325 if (!cgroup_lock_live_group(cgrp))
1326 return -ENODEV;
1327 ret = attach_task_by_pid(cgrp, pid);
1328 cgroup_unlock();
1329 return ret;
1330}
1331
Paul Menageddbcc7e2007-10-18 23:39:30 -07001332/* The various types of files and directories in a cgroup file system */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333enum cgroup_filetype {
1334 FILE_ROOT,
1335 FILE_DIR,
1336 FILE_TASKLIST,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001337 FILE_NOTIFY_ON_RELEASE,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001338 FILE_RELEASE_AGENT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001339};
1340
Paul Menagee788e0662008-07-25 01:46:59 -07001341/**
1342 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1343 * @cgrp: the cgroup to be checked for liveness
1344 *
Paul Menage84eea842008-07-25 01:47:00 -07001345 * On success, returns true; the lock should be later released with
1346 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e0662008-07-25 01:46:59 -07001347 */
Paul Menage84eea842008-07-25 01:47:00 -07001348bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e0662008-07-25 01:46:59 -07001349{
1350 mutex_lock(&cgroup_mutex);
1351 if (cgroup_is_removed(cgrp)) {
1352 mutex_unlock(&cgroup_mutex);
1353 return false;
1354 }
1355 return true;
1356}
1357
1358static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1359 const char *buffer)
1360{
1361 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1362 if (!cgroup_lock_live_group(cgrp))
1363 return -ENODEV;
1364 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001365 cgroup_unlock();
Paul Menagee788e0662008-07-25 01:46:59 -07001366 return 0;
1367}
1368
1369static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1370 struct seq_file *seq)
1371{
1372 if (!cgroup_lock_live_group(cgrp))
1373 return -ENODEV;
1374 seq_puts(seq, cgrp->root->release_agent_path);
1375 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001376 cgroup_unlock();
Paul Menagee788e0662008-07-25 01:46:59 -07001377 return 0;
1378}
1379
Paul Menage84eea842008-07-25 01:47:00 -07001380/* A buffer size big enough for numbers or short strings */
1381#define CGROUP_LOCAL_BUFFER_SIZE 64
1382
Paul Menagee73d2c62008-04-29 01:00:06 -07001383static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001384 struct file *file,
1385 const char __user *userbuf,
1386 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001387{
Paul Menage84eea842008-07-25 01:47:00 -07001388 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001389 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001390 char *end;
1391
1392 if (!nbytes)
1393 return -EINVAL;
1394 if (nbytes >= sizeof(buffer))
1395 return -E2BIG;
1396 if (copy_from_user(buffer, userbuf, nbytes))
1397 return -EFAULT;
1398
1399 buffer[nbytes] = 0; /* nul-terminate */
Paul Menageb7269df2008-04-29 00:59:59 -07001400 strstrip(buffer);
Paul Menagee73d2c62008-04-29 01:00:06 -07001401 if (cft->write_u64) {
1402 u64 val = simple_strtoull(buffer, &end, 0);
1403 if (*end)
1404 return -EINVAL;
1405 retval = cft->write_u64(cgrp, cft, val);
1406 } else {
1407 s64 val = simple_strtoll(buffer, &end, 0);
1408 if (*end)
1409 return -EINVAL;
1410 retval = cft->write_s64(cgrp, cft, val);
1411 }
Paul Menage355e0c42007-10-18 23:39:33 -07001412 if (!retval)
1413 retval = nbytes;
1414 return retval;
1415}
1416
Paul Menagedb3b1492008-07-25 01:46:58 -07001417static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1418 struct file *file,
1419 const char __user *userbuf,
1420 size_t nbytes, loff_t *unused_ppos)
1421{
Paul Menage84eea842008-07-25 01:47:00 -07001422 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001423 int retval = 0;
1424 size_t max_bytes = cft->max_write_len;
1425 char *buffer = local_buffer;
1426
1427 if (!max_bytes)
1428 max_bytes = sizeof(local_buffer) - 1;
1429 if (nbytes >= max_bytes)
1430 return -E2BIG;
1431 /* Allocate a dynamic buffer if we need one */
1432 if (nbytes >= sizeof(local_buffer)) {
1433 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1434 if (buffer == NULL)
1435 return -ENOMEM;
1436 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001437 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1438 retval = -EFAULT;
1439 goto out;
1440 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001441
1442 buffer[nbytes] = 0; /* nul-terminate */
1443 strstrip(buffer);
1444 retval = cft->write_string(cgrp, cft, buffer);
1445 if (!retval)
1446 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001447out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001448 if (buffer != local_buffer)
1449 kfree(buffer);
1450 return retval;
1451}
1452
Paul Menageddbcc7e2007-10-18 23:39:30 -07001453static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1454 size_t nbytes, loff_t *ppos)
1455{
1456 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001457 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001458
Li Zefan75139b82009-01-07 18:07:33 -08001459 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001460 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001461 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001462 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001463 if (cft->write_u64 || cft->write_s64)
1464 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001465 if (cft->write_string)
1466 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001467 if (cft->trigger) {
1468 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1469 return ret ? ret : nbytes;
1470 }
Paul Menage355e0c42007-10-18 23:39:33 -07001471 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001472}
1473
Paul Menagef4c753b2008-04-29 00:59:56 -07001474static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1475 struct file *file,
1476 char __user *buf, size_t nbytes,
1477 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478{
Paul Menage84eea842008-07-25 01:47:00 -07001479 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001480 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001481 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1482
1483 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1484}
1485
Paul Menagee73d2c62008-04-29 01:00:06 -07001486static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1487 struct file *file,
1488 char __user *buf, size_t nbytes,
1489 loff_t *ppos)
1490{
Paul Menage84eea842008-07-25 01:47:00 -07001491 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001492 s64 val = cft->read_s64(cgrp, cft);
1493 int len = sprintf(tmp, "%lld\n", (long long) val);
1494
1495 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1496}
1497
Paul Menageddbcc7e2007-10-18 23:39:30 -07001498static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1499 size_t nbytes, loff_t *ppos)
1500{
1501 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001502 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001503
Li Zefan75139b82009-01-07 18:07:33 -08001504 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001505 return -ENODEV;
1506
1507 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001508 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001509 if (cft->read_u64)
1510 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001511 if (cft->read_s64)
1512 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513 return -EINVAL;
1514}
1515
Paul Menage91796562008-04-29 01:00:01 -07001516/*
1517 * seqfile ops/methods for returning structured data. Currently just
1518 * supports string->u64 maps, but can be extended in future.
1519 */
1520
1521struct cgroup_seqfile_state {
1522 struct cftype *cft;
1523 struct cgroup *cgroup;
1524};
1525
1526static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1527{
1528 struct seq_file *sf = cb->state;
1529 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1530}
1531
1532static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1533{
1534 struct cgroup_seqfile_state *state = m->private;
1535 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001536 if (cft->read_map) {
1537 struct cgroup_map_cb cb = {
1538 .fill = cgroup_map_add,
1539 .state = m,
1540 };
1541 return cft->read_map(state->cgroup, cft, &cb);
1542 }
1543 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001544}
1545
Adrian Bunk96930a62008-07-25 19:46:21 -07001546static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07001547{
1548 struct seq_file *seq = file->private_data;
1549 kfree(seq->private);
1550 return single_release(inode, file);
1551}
1552
1553static struct file_operations cgroup_seqfile_operations = {
1554 .read = seq_read,
Paul Menagee788e0662008-07-25 01:46:59 -07001555 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07001556 .llseek = seq_lseek,
1557 .release = cgroup_seqfile_release,
1558};
1559
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560static int cgroup_file_open(struct inode *inode, struct file *file)
1561{
1562 int err;
1563 struct cftype *cft;
1564
1565 err = generic_file_open(inode, file);
1566 if (err)
1567 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08001569
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001570 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001571 struct cgroup_seqfile_state *state =
1572 kzalloc(sizeof(*state), GFP_USER);
1573 if (!state)
1574 return -ENOMEM;
1575 state->cft = cft;
1576 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1577 file->f_op = &cgroup_seqfile_operations;
1578 err = single_open(file, cgroup_seqfile_show, state);
1579 if (err < 0)
1580 kfree(state);
1581 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582 err = cft->open(inode, file);
1583 else
1584 err = 0;
1585
1586 return err;
1587}
1588
1589static int cgroup_file_release(struct inode *inode, struct file *file)
1590{
1591 struct cftype *cft = __d_cft(file->f_dentry);
1592 if (cft->release)
1593 return cft->release(inode, file);
1594 return 0;
1595}
1596
1597/*
1598 * cgroup_rename - Only allow simple rename of directories in place.
1599 */
1600static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1601 struct inode *new_dir, struct dentry *new_dentry)
1602{
1603 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1604 return -ENOTDIR;
1605 if (new_dentry->d_inode)
1606 return -EEXIST;
1607 if (old_dir != new_dir)
1608 return -EIO;
1609 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1610}
1611
1612static struct file_operations cgroup_file_operations = {
1613 .read = cgroup_file_read,
1614 .write = cgroup_file_write,
1615 .llseek = generic_file_llseek,
1616 .open = cgroup_file_open,
1617 .release = cgroup_file_release,
1618};
1619
1620static struct inode_operations cgroup_dir_inode_operations = {
1621 .lookup = simple_lookup,
1622 .mkdir = cgroup_mkdir,
1623 .rmdir = cgroup_rmdir,
1624 .rename = cgroup_rename,
1625};
1626
1627static int cgroup_create_file(struct dentry *dentry, int mode,
1628 struct super_block *sb)
1629{
1630 static struct dentry_operations cgroup_dops = {
1631 .d_iput = cgroup_diput,
1632 };
1633
1634 struct inode *inode;
1635
1636 if (!dentry)
1637 return -ENOENT;
1638 if (dentry->d_inode)
1639 return -EEXIST;
1640
1641 inode = cgroup_new_inode(mode, sb);
1642 if (!inode)
1643 return -ENOMEM;
1644
1645 if (S_ISDIR(mode)) {
1646 inode->i_op = &cgroup_dir_inode_operations;
1647 inode->i_fop = &simple_dir_operations;
1648
1649 /* start off with i_nlink == 2 (for "." entry) */
1650 inc_nlink(inode);
1651
1652 /* start with the directory inode held, so that we can
1653 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07001654 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001655 } else if (S_ISREG(mode)) {
1656 inode->i_size = 0;
1657 inode->i_fop = &cgroup_file_operations;
1658 }
1659 dentry->d_op = &cgroup_dops;
1660 d_instantiate(dentry, inode);
1661 dget(dentry); /* Extra count - pin the dentry in core */
1662 return 0;
1663}
1664
1665/*
Li Zefana043e3b2008-02-23 15:24:09 -08001666 * cgroup_create_dir - create a directory for an object.
1667 * @cgrp: the cgroup we create the directory for. It must have a valid
1668 * ->parent field. And we are going to fill its ->dentry field.
1669 * @dentry: dentry of the new cgroup
1670 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001671 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001672static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673 int mode)
1674{
1675 struct dentry *parent;
1676 int error = 0;
1677
Paul Menagebd89aab2007-10-18 23:40:44 -07001678 parent = cgrp->parent->dentry;
1679 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001680 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001681 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08001683 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684 dget(dentry);
1685 }
1686 dput(dentry);
1687
1688 return error;
1689}
1690
Paul Menagebd89aab2007-10-18 23:40:44 -07001691int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001692 struct cgroup_subsys *subsys,
1693 const struct cftype *cft)
1694{
Paul Menagebd89aab2007-10-18 23:40:44 -07001695 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 struct dentry *dentry;
1697 int error;
1698
1699 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07001700 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701 strcpy(name, subsys->name);
1702 strcat(name, ".");
1703 }
1704 strcat(name, cft->name);
1705 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1706 dentry = lookup_one_len(name, dir, strlen(name));
1707 if (!IS_ERR(dentry)) {
1708 error = cgroup_create_file(dentry, 0644 | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07001709 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710 if (!error)
1711 dentry->d_fsdata = (void *)cft;
1712 dput(dentry);
1713 } else
1714 error = PTR_ERR(dentry);
1715 return error;
1716}
1717
Paul Menagebd89aab2007-10-18 23:40:44 -07001718int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719 struct cgroup_subsys *subsys,
1720 const struct cftype cft[],
1721 int count)
1722{
1723 int i, err;
1724 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001725 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001726 if (err)
1727 return err;
1728 }
1729 return 0;
1730}
1731
Li Zefana043e3b2008-02-23 15:24:09 -08001732/**
1733 * cgroup_task_count - count the number of tasks in a cgroup.
1734 * @cgrp: the cgroup in question
1735 *
1736 * Return the number of tasks in the cgroup.
1737 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001738int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001739{
1740 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001741 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001742
Paul Menage817929e2007-10-18 23:39:36 -07001743 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001744 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07001745 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07001746 }
1747 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001748 return count;
1749}
1750
1751/*
Paul Menage817929e2007-10-18 23:39:36 -07001752 * Advance a list_head iterator. The iterator should be positioned at
1753 * the start of a css_set
1754 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001755static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001756 struct cgroup_iter *it)
1757{
1758 struct list_head *l = it->cg_link;
1759 struct cg_cgroup_link *link;
1760 struct css_set *cg;
1761
1762 /* Advance to the next non-empty css_set */
1763 do {
1764 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07001765 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001766 it->cg_link = NULL;
1767 return;
1768 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001769 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001770 cg = link->cg;
1771 } while (list_empty(&cg->tasks));
1772 it->cg_link = l;
1773 it->task = cg->tasks.next;
1774}
1775
Cliff Wickman31a7df02008-02-07 00:14:42 -08001776/*
1777 * To reduce the fork() overhead for systems that are not actually
1778 * using their cgroups capability, we don't maintain the lists running
1779 * through each css_set to its tasks until we see the list actually
1780 * used - in other words after the first call to cgroup_iter_start().
1781 *
1782 * The tasklist_lock is not held here, as do_each_thread() and
1783 * while_each_thread() are protected by RCU.
1784 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07001785static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08001786{
1787 struct task_struct *p, *g;
1788 write_lock(&css_set_lock);
1789 use_task_css_set_links = 1;
1790 do_each_thread(g, p) {
1791 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08001792 /*
1793 * We should check if the process is exiting, otherwise
1794 * it will race with cgroup_exit() in that the list
1795 * entry won't be deleted though the process has exited.
1796 */
1797 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08001798 list_add(&p->cg_list, &p->cgroups->tasks);
1799 task_unlock(p);
1800 } while_each_thread(g, p);
1801 write_unlock(&css_set_lock);
1802}
1803
Paul Menagebd89aab2007-10-18 23:40:44 -07001804void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001805{
1806 /*
1807 * The first time anyone tries to iterate across a cgroup,
1808 * we need to enable the list linking each css_set to its
1809 * tasks, and fix up all existing tasks.
1810 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08001811 if (!use_task_css_set_links)
1812 cgroup_enable_task_cg_lists();
1813
Paul Menage817929e2007-10-18 23:39:36 -07001814 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001815 it->cg_link = &cgrp->css_sets;
1816 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001817}
1818
Paul Menagebd89aab2007-10-18 23:40:44 -07001819struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001820 struct cgroup_iter *it)
1821{
1822 struct task_struct *res;
1823 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001824 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07001825
1826 /* If the iterator cg is NULL, we have no tasks */
1827 if (!it->cg_link)
1828 return NULL;
1829 res = list_entry(l, struct task_struct, cg_list);
1830 /* Advance iterator to find next entry */
1831 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001832 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
1833 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07001834 /* We reached the end of this task list - move on to
1835 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07001836 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001837 } else {
1838 it->task = l;
1839 }
1840 return res;
1841}
1842
Paul Menagebd89aab2007-10-18 23:40:44 -07001843void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001844{
1845 read_unlock(&css_set_lock);
1846}
1847
Cliff Wickman31a7df02008-02-07 00:14:42 -08001848static inline int started_after_time(struct task_struct *t1,
1849 struct timespec *time,
1850 struct task_struct *t2)
1851{
1852 int start_diff = timespec_compare(&t1->start_time, time);
1853 if (start_diff > 0) {
1854 return 1;
1855 } else if (start_diff < 0) {
1856 return 0;
1857 } else {
1858 /*
1859 * Arbitrarily, if two processes started at the same
1860 * time, we'll say that the lower pointer value
1861 * started first. Note that t2 may have exited by now
1862 * so this may not be a valid pointer any longer, but
1863 * that's fine - it still serves to distinguish
1864 * between two tasks started (effectively) simultaneously.
1865 */
1866 return t1 > t2;
1867 }
1868}
1869
1870/*
1871 * This function is a callback from heap_insert() and is used to order
1872 * the heap.
1873 * In this case we order the heap in descending task start time.
1874 */
1875static inline int started_after(void *p1, void *p2)
1876{
1877 struct task_struct *t1 = p1;
1878 struct task_struct *t2 = p2;
1879 return started_after_time(t1, &t2->start_time, t2);
1880}
1881
1882/**
1883 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1884 * @scan: struct cgroup_scanner containing arguments for the scan
1885 *
1886 * Arguments include pointers to callback functions test_task() and
1887 * process_task().
1888 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1889 * and if it returns true, call process_task() for it also.
1890 * The test_task pointer may be NULL, meaning always true (select all tasks).
1891 * Effectively duplicates cgroup_iter_{start,next,end}()
1892 * but does not lock css_set_lock for the call to process_task().
1893 * The struct cgroup_scanner may be embedded in any structure of the caller's
1894 * creation.
1895 * It is guaranteed that process_task() will act on every task that
1896 * is a member of the cgroup for the duration of this call. This
1897 * function may or may not call process_task() for tasks that exit
1898 * or move to a different cgroup during the call, or are forked or
1899 * move into the cgroup during the call.
1900 *
1901 * Note that test_task() may be called with locks held, and may in some
1902 * situations be called multiple times for the same task, so it should
1903 * be cheap.
1904 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1905 * pre-allocated and will be used for heap operations (and its "gt" member will
1906 * be overwritten), else a temporary heap will be used (allocation of which
1907 * may cause this function to fail).
1908 */
1909int cgroup_scan_tasks(struct cgroup_scanner *scan)
1910{
1911 int retval, i;
1912 struct cgroup_iter it;
1913 struct task_struct *p, *dropped;
1914 /* Never dereference latest_task, since it's not refcounted */
1915 struct task_struct *latest_task = NULL;
1916 struct ptr_heap tmp_heap;
1917 struct ptr_heap *heap;
1918 struct timespec latest_time = { 0, 0 };
1919
1920 if (scan->heap) {
1921 /* The caller supplied our heap and pre-allocated its memory */
1922 heap = scan->heap;
1923 heap->gt = &started_after;
1924 } else {
1925 /* We need to allocate our own heap memory */
1926 heap = &tmp_heap;
1927 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1928 if (retval)
1929 /* cannot allocate the heap */
1930 return retval;
1931 }
1932
1933 again:
1934 /*
1935 * Scan tasks in the cgroup, using the scanner's "test_task" callback
1936 * to determine which are of interest, and using the scanner's
1937 * "process_task" callback to process any of them that need an update.
1938 * Since we don't want to hold any locks during the task updates,
1939 * gather tasks to be processed in a heap structure.
1940 * The heap is sorted by descending task start time.
1941 * If the statically-sized heap fills up, we overflow tasks that
1942 * started later, and in future iterations only consider tasks that
1943 * started after the latest task in the previous pass. This
1944 * guarantees forward progress and that we don't miss any tasks.
1945 */
1946 heap->size = 0;
1947 cgroup_iter_start(scan->cg, &it);
1948 while ((p = cgroup_iter_next(scan->cg, &it))) {
1949 /*
1950 * Only affect tasks that qualify per the caller's callback,
1951 * if he provided one
1952 */
1953 if (scan->test_task && !scan->test_task(p, scan))
1954 continue;
1955 /*
1956 * Only process tasks that started after the last task
1957 * we processed
1958 */
1959 if (!started_after_time(p, &latest_time, latest_task))
1960 continue;
1961 dropped = heap_insert(heap, p);
1962 if (dropped == NULL) {
1963 /*
1964 * The new task was inserted; the heap wasn't
1965 * previously full
1966 */
1967 get_task_struct(p);
1968 } else if (dropped != p) {
1969 /*
1970 * The new task was inserted, and pushed out a
1971 * different task
1972 */
1973 get_task_struct(p);
1974 put_task_struct(dropped);
1975 }
1976 /*
1977 * Else the new task was newer than anything already in
1978 * the heap and wasn't inserted
1979 */
1980 }
1981 cgroup_iter_end(scan->cg, &it);
1982
1983 if (heap->size) {
1984 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07001985 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08001986 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07001987 latest_time = q->start_time;
1988 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08001989 }
1990 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07001991 scan->process_task(q, scan);
1992 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08001993 }
1994 /*
1995 * If we had to process any tasks at all, scan again
1996 * in case some of them were in the middle of forking
1997 * children that didn't get processed.
1998 * Not the most efficient way to do it, but it avoids
1999 * having to take callback_mutex in the fork path
2000 */
2001 goto again;
2002 }
2003 if (heap == &tmp_heap)
2004 heap_free(&tmp_heap);
2005 return 0;
2006}
2007
Paul Menage817929e2007-10-18 23:39:36 -07002008/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07002009 * Stuff for reading the 'tasks' file.
2010 *
2011 * Reading this file can return large amounts of data if a cgroup has
2012 * *lots* of attached tasks. So it may need several calls to read(),
2013 * but we cannot guarantee that the information we produce is correct
2014 * unless we produce it entirely atomically.
2015 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002016 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002017
2018/*
2019 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
Paul Menagebd89aab2007-10-18 23:40:44 -07002020 * 'cgrp'. Return actual number of pids loaded. No need to
Paul Menagebbcb81d2007-10-18 23:39:32 -07002021 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2022 * read section, so the css_set can't go away, and is
2023 * immutable after creation.
2024 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002025static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002026{
Gowrishankar Me7b80bb2009-01-07 18:07:43 -08002027 int n = 0, pid;
Paul Menage817929e2007-10-18 23:39:36 -07002028 struct cgroup_iter it;
2029 struct task_struct *tsk;
Paul Menagebd89aab2007-10-18 23:40:44 -07002030 cgroup_iter_start(cgrp, &it);
2031 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Paul Menage817929e2007-10-18 23:39:36 -07002032 if (unlikely(n == npids))
2033 break;
Gowrishankar Me7b80bb2009-01-07 18:07:43 -08002034 pid = task_pid_vnr(tsk);
2035 if (pid > 0)
2036 pidarray[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07002037 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002038 cgroup_iter_end(cgrp, &it);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002039 return n;
2040}
2041
Balbir Singh846c7bb2007-10-18 23:39:44 -07002042/**
Li Zefana043e3b2008-02-23 15:24:09 -08002043 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002044 * @stats: cgroupstats to fill information into
2045 * @dentry: A dentry entry belonging to the cgroup for which stats have
2046 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002047 *
2048 * Build and fill cgroupstats so that taskstats can export it to user
2049 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002050 */
2051int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2052{
2053 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002054 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002055 struct cgroup_iter it;
2056 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002057
Balbir Singh846c7bb2007-10-18 23:39:44 -07002058 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002059 * Validate dentry by checking the superblock operations,
2060 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002061 */
Li Zefan33d283b2008-11-19 15:36:48 -08002062 if (dentry->d_sb->s_op != &cgroup_ops ||
2063 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002064 goto err;
2065
2066 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002067 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002068
Paul Menagebd89aab2007-10-18 23:40:44 -07002069 cgroup_iter_start(cgrp, &it);
2070 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002071 switch (tsk->state) {
2072 case TASK_RUNNING:
2073 stats->nr_running++;
2074 break;
2075 case TASK_INTERRUPTIBLE:
2076 stats->nr_sleeping++;
2077 break;
2078 case TASK_UNINTERRUPTIBLE:
2079 stats->nr_uninterruptible++;
2080 break;
2081 case TASK_STOPPED:
2082 stats->nr_stopped++;
2083 break;
2084 default:
2085 if (delayacct_is_task_waiting_on_io(tsk))
2086 stats->nr_io_wait++;
2087 break;
2088 }
2089 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002090 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002091
Balbir Singh846c7bb2007-10-18 23:39:44 -07002092err:
2093 return ret;
2094}
2095
Paul Menagebbcb81d2007-10-18 23:39:32 -07002096static int cmppid(const void *a, const void *b)
2097{
2098 return *(pid_t *)a - *(pid_t *)b;
2099}
2100
Paul Menagebbcb81d2007-10-18 23:39:32 -07002101
Paul Menagecc31edc2008-10-18 20:28:04 -07002102/*
2103 * seq_file methods for the "tasks" file. The seq_file position is the
2104 * next pid to display; the seq_file iterator is a pointer to the pid
2105 * in the cgroup->tasks_pids array.
2106 */
2107
2108static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
2109{
2110 /*
2111 * Initially we receive a position value that corresponds to
2112 * one more than the last pid shown (or 0 on the first call or
2113 * after a seek to the start). Use a binary-search to find the
2114 * next pid to display, if any
2115 */
2116 struct cgroup *cgrp = s->private;
2117 int index = 0, pid = *pos;
2118 int *iter;
2119
2120 down_read(&cgrp->pids_mutex);
2121 if (pid) {
2122 int end = cgrp->pids_length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002123
Paul Menagecc31edc2008-10-18 20:28:04 -07002124 while (index < end) {
2125 int mid = (index + end) / 2;
2126 if (cgrp->tasks_pids[mid] == pid) {
2127 index = mid;
2128 break;
2129 } else if (cgrp->tasks_pids[mid] <= pid)
2130 index = mid + 1;
2131 else
2132 end = mid;
2133 }
2134 }
2135 /* If we're off the end of the array, we're done */
2136 if (index >= cgrp->pids_length)
2137 return NULL;
2138 /* Update the abstract position to be the actual pid that we found */
2139 iter = cgrp->tasks_pids + index;
2140 *pos = *iter;
2141 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002142}
2143
Paul Menagecc31edc2008-10-18 20:28:04 -07002144static void cgroup_tasks_stop(struct seq_file *s, void *v)
2145{
2146 struct cgroup *cgrp = s->private;
2147 up_read(&cgrp->pids_mutex);
2148}
2149
2150static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
2151{
2152 struct cgroup *cgrp = s->private;
2153 int *p = v;
2154 int *end = cgrp->tasks_pids + cgrp->pids_length;
2155
2156 /*
2157 * Advance to the next pid in the array. If this goes off the
2158 * end, we're done
2159 */
2160 p++;
2161 if (p >= end) {
2162 return NULL;
2163 } else {
2164 *pos = *p;
2165 return p;
2166 }
2167}
2168
2169static int cgroup_tasks_show(struct seq_file *s, void *v)
2170{
2171 return seq_printf(s, "%d\n", *(int *)v);
2172}
2173
2174static struct seq_operations cgroup_tasks_seq_operations = {
2175 .start = cgroup_tasks_start,
2176 .stop = cgroup_tasks_stop,
2177 .next = cgroup_tasks_next,
2178 .show = cgroup_tasks_show,
2179};
2180
2181static void release_cgroup_pid_array(struct cgroup *cgrp)
2182{
2183 down_write(&cgrp->pids_mutex);
2184 BUG_ON(!cgrp->pids_use_count);
2185 if (!--cgrp->pids_use_count) {
2186 kfree(cgrp->tasks_pids);
2187 cgrp->tasks_pids = NULL;
2188 cgrp->pids_length = 0;
2189 }
2190 up_write(&cgrp->pids_mutex);
2191}
2192
2193static int cgroup_tasks_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002194{
Paul Menagebd89aab2007-10-18 23:40:44 -07002195 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002196
2197 if (!(file->f_mode & FMODE_READ))
2198 return 0;
2199
Paul Menagecc31edc2008-10-18 20:28:04 -07002200 release_cgroup_pid_array(cgrp);
2201 return seq_release(inode, file);
2202}
2203
2204static struct file_operations cgroup_tasks_operations = {
2205 .read = seq_read,
2206 .llseek = seq_lseek,
2207 .write = cgroup_file_write,
2208 .release = cgroup_tasks_release,
2209};
2210
2211/*
2212 * Handle an open on 'tasks' file. Prepare an array containing the
2213 * process id's of tasks currently attached to the cgroup being opened.
2214 */
2215
2216static int cgroup_tasks_open(struct inode *unused, struct file *file)
2217{
2218 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2219 pid_t *pidarray;
2220 int npids;
2221 int retval;
2222
2223 /* Nothing to do for write-only files */
2224 if (!(file->f_mode & FMODE_READ))
2225 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002226
2227 /*
2228 * If cgroup gets more users after we read count, we won't have
2229 * enough space - tough. This race is indistinguishable to the
2230 * caller from the case that the additional cgroup users didn't
2231 * show up until sometime later on.
2232 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002233 npids = cgroup_task_count(cgrp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002234 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2235 if (!pidarray)
2236 return -ENOMEM;
2237 npids = pid_array_load(pidarray, npids, cgrp);
2238 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002239
Paul Menagecc31edc2008-10-18 20:28:04 -07002240 /*
2241 * Store the array in the cgroup, freeing the old
2242 * array if necessary
2243 */
2244 down_write(&cgrp->pids_mutex);
2245 kfree(cgrp->tasks_pids);
2246 cgrp->tasks_pids = pidarray;
2247 cgrp->pids_length = npids;
2248 cgrp->pids_use_count++;
2249 up_write(&cgrp->pids_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002250
Paul Menagecc31edc2008-10-18 20:28:04 -07002251 file->f_op = &cgroup_tasks_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002252
Paul Menagecc31edc2008-10-18 20:28:04 -07002253 retval = seq_open(file, &cgroup_tasks_seq_operations);
2254 if (retval) {
2255 release_cgroup_pid_array(cgrp);
2256 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002257 }
Paul Menagecc31edc2008-10-18 20:28:04 -07002258 ((struct seq_file *)file->private_data)->private = cgrp;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002259 return 0;
2260}
2261
Paul Menagebd89aab2007-10-18 23:40:44 -07002262static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002263 struct cftype *cft)
2264{
Paul Menagebd89aab2007-10-18 23:40:44 -07002265 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002266}
2267
Paul Menage6379c102008-07-25 01:47:01 -07002268static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2269 struct cftype *cft,
2270 u64 val)
2271{
2272 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2273 if (val)
2274 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2275 else
2276 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2277 return 0;
2278}
2279
Paul Menagebbcb81d2007-10-18 23:39:32 -07002280/*
2281 * for the common functions, 'private' gives the type of file
2282 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07002283static struct cftype files[] = {
2284 {
2285 .name = "tasks",
2286 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07002287 .write_u64 = cgroup_tasks_write,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002288 .release = cgroup_tasks_release,
2289 .private = FILE_TASKLIST,
2290 },
2291
2292 {
2293 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002294 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07002295 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002296 .private = FILE_NOTIFY_ON_RELEASE,
2297 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002298};
2299
2300static struct cftype cft_release_agent = {
2301 .name = "release_agent",
Paul Menagee788e0662008-07-25 01:46:59 -07002302 .read_seq_string = cgroup_release_agent_show,
2303 .write_string = cgroup_release_agent_write,
2304 .max_write_len = PATH_MAX,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002305 .private = FILE_RELEASE_AGENT,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002306};
2307
Paul Menagebd89aab2007-10-18 23:40:44 -07002308static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002309{
2310 int err;
2311 struct cgroup_subsys *ss;
2312
2313 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002314 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002315
Paul Menagebd89aab2007-10-18 23:40:44 -07002316 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002317 if (err < 0)
2318 return err;
2319
Paul Menagebd89aab2007-10-18 23:40:44 -07002320 if (cgrp == cgrp->top_cgroup) {
2321 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002322 return err;
2323 }
2324
Paul Menagebd89aab2007-10-18 23:40:44 -07002325 for_each_subsys(cgrp->root, ss) {
2326 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002327 return err;
2328 }
2329
2330 return 0;
2331}
2332
2333static void init_cgroup_css(struct cgroup_subsys_state *css,
2334 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002335 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002336{
Paul Menagebd89aab2007-10-18 23:40:44 -07002337 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08002338 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002339 css->flags = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002340 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002341 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002342 BUG_ON(cgrp->subsys[ss->subsys_id]);
2343 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002344}
2345
Paul Menage999cd8a2009-01-07 18:08:36 -08002346static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2347{
2348 /* We need to take each hierarchy_mutex in a consistent order */
2349 int i;
2350
2351 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2352 struct cgroup_subsys *ss = subsys[i];
2353 if (ss->root == root)
2354 mutex_lock_nested(&ss->hierarchy_mutex, i);
2355 }
2356}
2357
2358static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2359{
2360 int i;
2361
2362 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2363 struct cgroup_subsys *ss = subsys[i];
2364 if (ss->root == root)
2365 mutex_unlock(&ss->hierarchy_mutex);
2366 }
2367}
2368
Paul Menageddbcc7e2007-10-18 23:39:30 -07002369/*
Li Zefana043e3b2008-02-23 15:24:09 -08002370 * cgroup_create - create a cgroup
2371 * @parent: cgroup that will be parent of the new cgroup
2372 * @dentry: dentry of the new cgroup
2373 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002374 *
Li Zefana043e3b2008-02-23 15:24:09 -08002375 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002376 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2378 int mode)
2379{
Paul Menagebd89aab2007-10-18 23:40:44 -07002380 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002381 struct cgroupfs_root *root = parent->root;
2382 int err = 0;
2383 struct cgroup_subsys *ss;
2384 struct super_block *sb = root->sb;
2385
Paul Menagebd89aab2007-10-18 23:40:44 -07002386 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2387 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002388 return -ENOMEM;
2389
2390 /* Grab a reference on the superblock so the hierarchy doesn't
2391 * get deleted on unmount if there are child cgroups. This
2392 * can be done outside cgroup_mutex, since the sb can't
2393 * disappear while someone has an open control file on the
2394 * fs */
2395 atomic_inc(&sb->s_active);
2396
2397 mutex_lock(&cgroup_mutex);
2398
Paul Menagecc31edc2008-10-18 20:28:04 -07002399 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002400
Paul Menagebd89aab2007-10-18 23:40:44 -07002401 cgrp->parent = parent;
2402 cgrp->root = parent->root;
2403 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002404
Li Zefanb6abdb02008-03-04 14:28:19 -08002405 if (notify_on_release(parent))
2406 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2407
Paul Menageddbcc7e2007-10-18 23:39:30 -07002408 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002409 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002410 if (IS_ERR(css)) {
2411 err = PTR_ERR(css);
2412 goto err_destroy;
2413 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002414 init_cgroup_css(css, ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002415 }
2416
Paul Menage999cd8a2009-01-07 18:08:36 -08002417 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002418 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08002419 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002420 root->number_of_cgroups++;
2421
Paul Menagebd89aab2007-10-18 23:40:44 -07002422 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002423 if (err < 0)
2424 goto err_remove;
2425
2426 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07002427 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002428
Paul Menagebd89aab2007-10-18 23:40:44 -07002429 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002430 /* If err < 0, we have a half-filled directory - oh well ;) */
2431
2432 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002433 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002434
2435 return 0;
2436
2437 err_remove:
2438
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002439 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002440 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002441 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002442 root->number_of_cgroups--;
2443
2444 err_destroy:
2445
2446 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002447 if (cgrp->subsys[ss->subsys_id])
2448 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002449 }
2450
2451 mutex_unlock(&cgroup_mutex);
2452
2453 /* Release the reference count that we took on the superblock */
2454 deactivate_super(sb);
2455
Paul Menagebd89aab2007-10-18 23:40:44 -07002456 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002457 return err;
2458}
2459
2460static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2461{
2462 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2463
2464 /* the vfs holds inode->i_mutex already */
2465 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2466}
2467
Li Zefan55b6fd02008-07-29 22:33:20 -07002468static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002469{
2470 /* Check the reference count on each subsystem. Since we
2471 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08002472 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07002473 * be no outstanding references, so the subsystem is safe to
2474 * destroy. We scan across all subsystems rather than using
2475 * the per-hierarchy linked list of mounted subsystems since
2476 * we can be called via check_for_release() with no
2477 * synchronization other than RCU, and the subsystem linked
2478 * list isn't RCU-safe */
2479 int i;
2480 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2481 struct cgroup_subsys *ss = subsys[i];
2482 struct cgroup_subsys_state *css;
2483 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07002484 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002485 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07002486 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07002487 /* When called from check_for_release() it's possible
2488 * that by this point the cgroup has been removed
2489 * and the css deleted. But a false-positive doesn't
2490 * matter, since it can only happen if the cgroup
2491 * has been deleted and hence no longer needs the
2492 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08002493 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07002494 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07002495 }
2496 return 0;
2497}
2498
Paul Menagee7c5ec92009-01-07 18:08:38 -08002499/*
2500 * Atomically mark all (or else none) of the cgroup's CSS objects as
2501 * CSS_REMOVED. Return true on success, or false if the cgroup has
2502 * busy subsystems. Call with cgroup_mutex held
2503 */
2504
2505static int cgroup_clear_css_refs(struct cgroup *cgrp)
2506{
2507 struct cgroup_subsys *ss;
2508 unsigned long flags;
2509 bool failed = false;
2510 local_irq_save(flags);
2511 for_each_subsys(cgrp->root, ss) {
2512 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2513 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08002514 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08002515 /* We can only remove a CSS with a refcnt==1 */
2516 refcnt = atomic_read(&css->refcnt);
2517 if (refcnt > 1) {
2518 failed = true;
2519 goto done;
2520 }
2521 BUG_ON(!refcnt);
2522 /*
2523 * Drop the refcnt to 0 while we check other
2524 * subsystems. This will cause any racing
2525 * css_tryget() to spin until we set the
2526 * CSS_REMOVED bits or abort
2527 */
Paul Menage804b3c22009-01-29 14:25:21 -08002528 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
2529 break;
2530 cpu_relax();
2531 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08002532 }
2533 done:
2534 for_each_subsys(cgrp->root, ss) {
2535 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2536 if (failed) {
2537 /*
2538 * Restore old refcnt if we previously managed
2539 * to clear it from 1 to 0
2540 */
2541 if (!atomic_read(&css->refcnt))
2542 atomic_set(&css->refcnt, 1);
2543 } else {
2544 /* Commit the fact that the CSS is removed */
2545 set_bit(CSS_REMOVED, &css->flags);
2546 }
2547 }
2548 local_irq_restore(flags);
2549 return !failed;
2550}
2551
Paul Menageddbcc7e2007-10-18 23:39:30 -07002552static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2553{
Paul Menagebd89aab2007-10-18 23:40:44 -07002554 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002555 struct dentry *d;
2556 struct cgroup *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002557
2558 /* the vfs holds both inode->i_mutex already */
2559
2560 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002561 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002562 mutex_unlock(&cgroup_mutex);
2563 return -EBUSY;
2564 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002565 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002566 mutex_unlock(&cgroup_mutex);
2567 return -EBUSY;
2568 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002569 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08002570
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002571 /*
Li Zefana043e3b2008-02-23 15:24:09 -08002572 * Call pre_destroy handlers of subsys. Notify subsystems
2573 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002574 */
2575 cgroup_call_pre_destroy(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002576
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002577 mutex_lock(&cgroup_mutex);
2578 parent = cgrp->parent;
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002579
2580 if (atomic_read(&cgrp->count)
2581 || !list_empty(&cgrp->children)
Paul Menagee7c5ec92009-01-07 18:08:38 -08002582 || !cgroup_clear_css_refs(cgrp)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002583 mutex_unlock(&cgroup_mutex);
2584 return -EBUSY;
2585 }
2586
Paul Menage81a6a5c2007-10-18 23:39:38 -07002587 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002588 set_bit(CGRP_REMOVED, &cgrp->flags);
2589 if (!list_empty(&cgrp->release_list))
2590 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002591 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08002592
2593 cgroup_lock_hierarchy(cgrp->root);
2594 /* delete this cgroup from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07002595 list_del(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08002596 cgroup_unlock_hierarchy(cgrp->root);
2597
Paul Menagebd89aab2007-10-18 23:40:44 -07002598 spin_lock(&cgrp->dentry->d_lock);
2599 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002600 spin_unlock(&d->d_lock);
2601
2602 cgroup_d_remove_dir(d);
2603 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002604
Paul Menagebd89aab2007-10-18 23:40:44 -07002605 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002606 check_for_release(parent);
2607
Paul Menageddbcc7e2007-10-18 23:39:30 -07002608 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002609 return 0;
2610}
2611
Li Zefan06a11922008-04-29 01:00:07 -07002612static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002613{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002614 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08002615
2616 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002617
2618 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08002619 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002620 ss->root = &rootnode;
2621 css = ss->create(ss, dummytop);
2622 /* We don't handle early failures gracefully */
2623 BUG_ON(IS_ERR(css));
2624 init_cgroup_css(css, ss, dummytop);
2625
Li Zefane8d55fd2008-04-29 01:00:13 -07002626 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07002627 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07002628 * newly registered, all tasks and hence the
2629 * init_css_set is in the subsystem's top cgroup. */
2630 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07002631
2632 need_forkexit_callback |= ss->fork || ss->exit;
2633
Li Zefane8d55fd2008-04-29 01:00:13 -07002634 /* At system boot, before all subsystems have been
2635 * registered, no tasks have been forked, so we don't
2636 * need to invoke fork callbacks here. */
2637 BUG_ON(!list_empty(&init_task.tasks));
2638
Paul Menage999cd8a2009-01-07 18:08:36 -08002639 mutex_init(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002640 ss->active = 1;
2641}
2642
2643/**
Li Zefana043e3b2008-02-23 15:24:09 -08002644 * cgroup_init_early - cgroup initialization at system boot
2645 *
2646 * Initialize cgroups at system boot, and initialize any
2647 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002648 */
2649int __init cgroup_init_early(void)
2650{
2651 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002652 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07002653 INIT_LIST_HEAD(&init_css_set.cg_links);
2654 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b10532008-04-29 01:00:11 -07002655 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07002656 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002657 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07002658 root_count = 1;
2659 init_task.cgroups = &init_css_set;
2660
2661 init_css_set_link.cg = &init_css_set;
Paul Menagebd89aab2007-10-18 23:40:44 -07002662 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07002663 &rootnode.top_cgroup.css_sets);
2664 list_add(&init_css_set_link.cg_link_list,
2665 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002666
Li Zefan472b10532008-04-29 01:00:11 -07002667 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2668 INIT_HLIST_HEAD(&css_set_table[i]);
2669
Paul Menageddbcc7e2007-10-18 23:39:30 -07002670 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2671 struct cgroup_subsys *ss = subsys[i];
2672
2673 BUG_ON(!ss->name);
2674 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2675 BUG_ON(!ss->create);
2676 BUG_ON(!ss->destroy);
2677 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08002678 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07002679 ss->name, ss->subsys_id);
2680 BUG();
2681 }
2682
2683 if (ss->early_init)
2684 cgroup_init_subsys(ss);
2685 }
2686 return 0;
2687}
2688
2689/**
Li Zefana043e3b2008-02-23 15:24:09 -08002690 * cgroup_init - cgroup initialization
2691 *
2692 * Register cgroup filesystem and /proc file, and initialize
2693 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002694 */
2695int __init cgroup_init(void)
2696{
2697 int err;
2698 int i;
Li Zefan472b10532008-04-29 01:00:11 -07002699 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07002700
2701 err = bdi_init(&cgroup_backing_dev_info);
2702 if (err)
2703 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002704
2705 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2706 struct cgroup_subsys *ss = subsys[i];
2707 if (!ss->early_init)
2708 cgroup_init_subsys(ss);
2709 }
2710
Li Zefan472b10532008-04-29 01:00:11 -07002711 /* Add init_css_set to the hash table */
2712 hhead = css_set_hash(init_css_set.subsys);
2713 hlist_add_head(&init_css_set.hlist, hhead);
2714
Paul Menageddbcc7e2007-10-18 23:39:30 -07002715 err = register_filesystem(&cgroup_fs_type);
2716 if (err < 0)
2717 goto out;
2718
Li Zefan46ae2202008-04-29 01:00:08 -07002719 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07002720
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721out:
Paul Menagea4243162007-10-18 23:39:35 -07002722 if (err)
2723 bdi_destroy(&cgroup_backing_dev_info);
2724
Paul Menageddbcc7e2007-10-18 23:39:30 -07002725 return err;
2726}
Paul Menageb4f48b62007-10-18 23:39:33 -07002727
Paul Menagea4243162007-10-18 23:39:35 -07002728/*
2729 * proc_cgroup_show()
2730 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2731 * - Used for /proc/<pid>/cgroup.
2732 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2733 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002734 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07002735 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2736 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2737 * cgroup to top_cgroup.
2738 */
2739
2740/* TODO: Use a proper seq_file iterator */
2741static int proc_cgroup_show(struct seq_file *m, void *v)
2742{
2743 struct pid *pid;
2744 struct task_struct *tsk;
2745 char *buf;
2746 int retval;
2747 struct cgroupfs_root *root;
2748
2749 retval = -ENOMEM;
2750 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2751 if (!buf)
2752 goto out;
2753
2754 retval = -ESRCH;
2755 pid = m->private;
2756 tsk = get_pid_task(pid, PIDTYPE_PID);
2757 if (!tsk)
2758 goto out_free;
2759
2760 retval = 0;
2761
2762 mutex_lock(&cgroup_mutex);
2763
Li Zefane5f6a862009-01-07 18:07:41 -08002764 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07002765 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07002766 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07002767 int subsys_id;
2768 int count = 0;
2769
Paul Menageb6c30062008-04-10 21:29:16 -07002770 seq_printf(m, "%lu:", root->subsys_bits);
Paul Menagea4243162007-10-18 23:39:35 -07002771 for_each_subsys(root, ss)
2772 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2773 seq_putc(m, ':');
2774 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07002775 cgrp = task_cgroup(tsk, subsys_id);
2776 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07002777 if (retval < 0)
2778 goto out_unlock;
2779 seq_puts(m, buf);
2780 seq_putc(m, '\n');
2781 }
2782
2783out_unlock:
2784 mutex_unlock(&cgroup_mutex);
2785 put_task_struct(tsk);
2786out_free:
2787 kfree(buf);
2788out:
2789 return retval;
2790}
2791
2792static int cgroup_open(struct inode *inode, struct file *file)
2793{
2794 struct pid *pid = PROC_I(inode)->pid;
2795 return single_open(file, proc_cgroup_show, pid);
2796}
2797
2798struct file_operations proc_cgroup_operations = {
2799 .open = cgroup_open,
2800 .read = seq_read,
2801 .llseek = seq_lseek,
2802 .release = single_release,
2803};
2804
2805/* Display information about each subsystem and each hierarchy */
2806static int proc_cgroupstats_show(struct seq_file *m, void *v)
2807{
2808 int i;
Paul Menagea4243162007-10-18 23:39:35 -07002809
Paul Menage8bab8dd2008-04-04 14:29:57 -07002810 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07002811 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07002812 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2813 struct cgroup_subsys *ss = subsys[i];
Paul Menage8bab8dd2008-04-04 14:29:57 -07002814 seq_printf(m, "%s\t%lu\t%d\t%d\n",
Paul Menage817929e2007-10-18 23:39:36 -07002815 ss->name, ss->root->subsys_bits,
Paul Menage8bab8dd2008-04-04 14:29:57 -07002816 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07002817 }
2818 mutex_unlock(&cgroup_mutex);
2819 return 0;
2820}
2821
2822static int cgroupstats_open(struct inode *inode, struct file *file)
2823{
Al Viro9dce07f2008-03-29 03:07:28 +00002824 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07002825}
2826
2827static struct file_operations proc_cgroupstats_operations = {
2828 .open = cgroupstats_open,
2829 .read = seq_read,
2830 .llseek = seq_lseek,
2831 .release = single_release,
2832};
2833
Paul Menageb4f48b62007-10-18 23:39:33 -07002834/**
2835 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08002836 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07002837 *
2838 * Description: A task inherits its parent's cgroup at fork().
2839 *
2840 * A pointer to the shared css_set was automatically copied in
2841 * fork.c by dup_task_struct(). However, we ignore that copy, since
2842 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08002843 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07002844 * have already changed current->cgroups, allowing the previously
2845 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07002846 *
2847 * At the point that cgroup_fork() is called, 'current' is the parent
2848 * task, and the passed argument 'child' points to the child task.
2849 */
2850void cgroup_fork(struct task_struct *child)
2851{
Paul Menage817929e2007-10-18 23:39:36 -07002852 task_lock(current);
2853 child->cgroups = current->cgroups;
2854 get_css_set(child->cgroups);
2855 task_unlock(current);
2856 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07002857}
2858
2859/**
Li Zefana043e3b2008-02-23 15:24:09 -08002860 * cgroup_fork_callbacks - run fork callbacks
2861 * @child: the new task
2862 *
2863 * Called on a new task very soon before adding it to the
2864 * tasklist. No need to take any locks since no-one can
2865 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07002866 */
2867void cgroup_fork_callbacks(struct task_struct *child)
2868{
2869 if (need_forkexit_callback) {
2870 int i;
2871 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2872 struct cgroup_subsys *ss = subsys[i];
2873 if (ss->fork)
2874 ss->fork(ss, child);
2875 }
2876 }
2877}
2878
2879/**
Li Zefana043e3b2008-02-23 15:24:09 -08002880 * cgroup_post_fork - called on a new task after adding it to the task list
2881 * @child: the task in question
2882 *
2883 * Adds the task to the list running through its css_set if necessary.
2884 * Has to be after the task is visible on the task list in case we race
2885 * with the first call to cgroup_iter_start() - to guarantee that the
2886 * new task ends up on its list.
2887 */
Paul Menage817929e2007-10-18 23:39:36 -07002888void cgroup_post_fork(struct task_struct *child)
2889{
2890 if (use_task_css_set_links) {
2891 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08002892 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07002893 if (list_empty(&child->cg_list))
2894 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08002895 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07002896 write_unlock(&css_set_lock);
2897 }
2898}
2899/**
Paul Menageb4f48b62007-10-18 23:39:33 -07002900 * cgroup_exit - detach cgroup from exiting task
2901 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08002902 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07002903 *
2904 * Description: Detach cgroup from @tsk and release it.
2905 *
2906 * Note that cgroups marked notify_on_release force every task in
2907 * them to take the global cgroup_mutex mutex when exiting.
2908 * This could impact scaling on very large systems. Be reluctant to
2909 * use notify_on_release cgroups where very high task exit scaling
2910 * is required on large systems.
2911 *
2912 * the_top_cgroup_hack:
2913 *
2914 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2915 *
2916 * We call cgroup_exit() while the task is still competent to
2917 * handle notify_on_release(), then leave the task attached to the
2918 * root cgroup in each hierarchy for the remainder of its exit.
2919 *
2920 * To do this properly, we would increment the reference count on
2921 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2922 * code we would add a second cgroup function call, to drop that
2923 * reference. This would just create an unnecessary hot spot on
2924 * the top_cgroup reference count, to no avail.
2925 *
2926 * Normally, holding a reference to a cgroup without bumping its
2927 * count is unsafe. The cgroup could go away, or someone could
2928 * attach us to a different cgroup, decrementing the count on
2929 * the first cgroup that we never incremented. But in this case,
2930 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002931 * which wards off any cgroup_attach_task() attempts, or task is a failed
2932 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07002933 */
2934void cgroup_exit(struct task_struct *tsk, int run_callbacks)
2935{
2936 int i;
Paul Menage817929e2007-10-18 23:39:36 -07002937 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07002938
2939 if (run_callbacks && need_forkexit_callback) {
2940 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2941 struct cgroup_subsys *ss = subsys[i];
2942 if (ss->exit)
2943 ss->exit(ss, tsk);
2944 }
2945 }
Paul Menage817929e2007-10-18 23:39:36 -07002946
2947 /*
2948 * Unlink from the css_set task list if necessary.
2949 * Optimistically check cg_list before taking
2950 * css_set_lock
2951 */
2952 if (!list_empty(&tsk->cg_list)) {
2953 write_lock(&css_set_lock);
2954 if (!list_empty(&tsk->cg_list))
2955 list_del(&tsk->cg_list);
2956 write_unlock(&css_set_lock);
2957 }
2958
Paul Menageb4f48b62007-10-18 23:39:33 -07002959 /* Reassign the task to the init_css_set. */
2960 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002961 cg = tsk->cgroups;
2962 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07002963 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07002964 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002965 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07002966}
Paul Menage697f4162007-10-18 23:39:34 -07002967
2968/**
Li Zefana043e3b2008-02-23 15:24:09 -08002969 * cgroup_clone - clone the cgroup the given subsystem is attached to
2970 * @tsk: the task to be moved
2971 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07002972 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08002973 *
2974 * Duplicate the current cgroup in the hierarchy that the given
2975 * subsystem is attached to, and move this task into the new
2976 * child.
Paul Menage697f4162007-10-18 23:39:34 -07002977 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07002978int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
2979 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07002980{
2981 struct dentry *dentry;
2982 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07002983 struct cgroup *parent, *child;
2984 struct inode *inode;
2985 struct css_set *cg;
2986 struct cgroupfs_root *root;
2987 struct cgroup_subsys *ss;
2988
2989 /* We shouldn't be called by an unregistered subsystem */
2990 BUG_ON(!subsys->active);
2991
2992 /* First figure out what hierarchy and cgroup we're dealing
2993 * with, and pin them so we can drop cgroup_mutex */
2994 mutex_lock(&cgroup_mutex);
2995 again:
2996 root = subsys->root;
2997 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07002998 mutex_unlock(&cgroup_mutex);
2999 return 0;
3000 }
Paul Menage697f4162007-10-18 23:39:34 -07003001
Paul Menage697f4162007-10-18 23:39:34 -07003002 /* Pin the hierarchy */
Li Zefan1404f062009-01-29 14:25:21 -08003003 if (!atomic_inc_not_zero(&root->sb->s_active)) {
Li Zefan7b574b72009-01-04 12:00:45 -08003004 /* We race with the final deactivate_super() */
3005 mutex_unlock(&cgroup_mutex);
3006 return 0;
3007 }
Paul Menage697f4162007-10-18 23:39:34 -07003008
Paul Menage817929e2007-10-18 23:39:36 -07003009 /* Keep the cgroup alive */
Li Zefan1404f062009-01-29 14:25:21 -08003010 task_lock(tsk);
3011 parent = task_cgroup(tsk, subsys->subsys_id);
3012 cg = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07003013 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08003014 task_unlock(tsk);
Li Zefan1404f062009-01-29 14:25:21 -08003015
Paul Menage697f4162007-10-18 23:39:34 -07003016 mutex_unlock(&cgroup_mutex);
3017
3018 /* Now do the VFS work to create a cgroup */
3019 inode = parent->dentry->d_inode;
3020
3021 /* Hold the parent directory mutex across this operation to
3022 * stop anyone else deleting the new cgroup */
3023 mutex_lock(&inode->i_mutex);
3024 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3025 if (IS_ERR(dentry)) {
3026 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08003027 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07003028 PTR_ERR(dentry));
3029 ret = PTR_ERR(dentry);
3030 goto out_release;
3031 }
3032
3033 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08003034 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07003035 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07003036 dput(dentry);
3037 if (ret) {
3038 printk(KERN_INFO
3039 "Failed to create cgroup %s: %d\n", nodename,
3040 ret);
3041 goto out_release;
3042 }
3043
Paul Menage697f4162007-10-18 23:39:34 -07003044 /* The cgroup now exists. Retake cgroup_mutex and check
3045 * that we're still in the same state that we thought we
3046 * were. */
3047 mutex_lock(&cgroup_mutex);
3048 if ((root != subsys->root) ||
3049 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3050 /* Aargh, we raced ... */
3051 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003052 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07003053
Li Zefan1404f062009-01-29 14:25:21 -08003054 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003055 /* The cgroup is still accessible in the VFS, but
3056 * we're not going to try to rmdir() it at this
3057 * point. */
3058 printk(KERN_INFO
3059 "Race in cgroup_clone() - leaking cgroup %s\n",
3060 nodename);
3061 goto again;
3062 }
3063
3064 /* do any required auto-setup */
3065 for_each_subsys(root, ss) {
3066 if (ss->post_clone)
3067 ss->post_clone(ss, child);
3068 }
3069
3070 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08003071 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07003072 mutex_unlock(&cgroup_mutex);
3073
3074 out_release:
3075 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003076
3077 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003078 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003079 mutex_unlock(&cgroup_mutex);
Li Zefan1404f062009-01-29 14:25:21 -08003080 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003081 return ret;
3082}
3083
Li Zefana043e3b2008-02-23 15:24:09 -08003084/**
3085 * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
3086 * @cgrp: the cgroup in question
3087 *
3088 * See if @cgrp is a descendant of the current task's cgroup in
3089 * the appropriate hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07003090 *
3091 * If we are sending in dummytop, then presumably we are creating
3092 * the top cgroup in the subsystem.
3093 *
3094 * Called only by the ns (nsproxy) cgroup.
3095 */
Paul Menagebd89aab2007-10-18 23:40:44 -07003096int cgroup_is_descendant(const struct cgroup *cgrp)
Paul Menage697f4162007-10-18 23:39:34 -07003097{
3098 int ret;
3099 struct cgroup *target;
3100 int subsys_id;
3101
Paul Menagebd89aab2007-10-18 23:40:44 -07003102 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07003103 return 1;
3104
Paul Menagebd89aab2007-10-18 23:40:44 -07003105 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menage697f4162007-10-18 23:39:34 -07003106 target = task_cgroup(current, subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07003107 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3108 cgrp = cgrp->parent;
3109 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07003110 return ret;
3111}
Paul Menage81a6a5c2007-10-18 23:39:38 -07003112
Paul Menagebd89aab2007-10-18 23:40:44 -07003113static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003114{
3115 /* All of these checks rely on RCU to keep the cgroup
3116 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07003117 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3118 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003119 /* Control Group is currently removeable. If it's not
3120 * already queued for a userspace notification, queue
3121 * it now */
3122 int need_schedule_work = 0;
3123 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003124 if (!cgroup_is_removed(cgrp) &&
3125 list_empty(&cgrp->release_list)) {
3126 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003127 need_schedule_work = 1;
3128 }
3129 spin_unlock(&release_list_lock);
3130 if (need_schedule_work)
3131 schedule_work(&release_agent_work);
3132 }
3133}
3134
3135void __css_put(struct cgroup_subsys_state *css)
3136{
Paul Menagebd89aab2007-10-18 23:40:44 -07003137 struct cgroup *cgrp = css->cgroup;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003138 rcu_read_lock();
Paul Menagee7c5ec92009-01-07 18:08:38 -08003139 if ((atomic_dec_return(&css->refcnt) == 1) &&
3140 notify_on_release(cgrp)) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003141 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3142 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003143 }
3144 rcu_read_unlock();
3145}
3146
3147/*
3148 * Notify userspace when a cgroup is released, by running the
3149 * configured release agent with the name of the cgroup (path
3150 * relative to the root of cgroup file system) as the argument.
3151 *
3152 * Most likely, this user command will try to rmdir this cgroup.
3153 *
3154 * This races with the possibility that some other task will be
3155 * attached to this cgroup before it is removed, or that some other
3156 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3157 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3158 * unused, and this cgroup will be reprieved from its death sentence,
3159 * to continue to serve a useful existence. Next time it's released,
3160 * we will get notified again, if it still has 'notify_on_release' set.
3161 *
3162 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3163 * means only wait until the task is successfully execve()'d. The
3164 * separate release agent task is forked by call_usermodehelper(),
3165 * then control in this thread returns here, without waiting for the
3166 * release agent task. We don't bother to wait because the caller of
3167 * this routine has no use for the exit status of the release agent
3168 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003169 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003170static void cgroup_release_agent(struct work_struct *work)
3171{
3172 BUG_ON(work != &release_agent_work);
3173 mutex_lock(&cgroup_mutex);
3174 spin_lock(&release_list_lock);
3175 while (!list_empty(&release_list)) {
3176 char *argv[3], *envp[3];
3177 int i;
Paul Menagee788e0662008-07-25 01:46:59 -07003178 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003179 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003180 struct cgroup,
3181 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003182 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003183 spin_unlock(&release_list_lock);
3184 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e0662008-07-25 01:46:59 -07003185 if (!pathbuf)
3186 goto continue_free;
3187 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3188 goto continue_free;
3189 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3190 if (!agentbuf)
3191 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003192
3193 i = 0;
Paul Menagee788e0662008-07-25 01:46:59 -07003194 argv[i++] = agentbuf;
3195 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003196 argv[i] = NULL;
3197
3198 i = 0;
3199 /* minimal command environment */
3200 envp[i++] = "HOME=/";
3201 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3202 envp[i] = NULL;
3203
3204 /* Drop the lock while we invoke the usermode helper,
3205 * since the exec could involve hitting disk and hence
3206 * be a slow process */
3207 mutex_unlock(&cgroup_mutex);
3208 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003209 mutex_lock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07003210 continue_free:
3211 kfree(pathbuf);
3212 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003213 spin_lock(&release_list_lock);
3214 }
3215 spin_unlock(&release_list_lock);
3216 mutex_unlock(&cgroup_mutex);
3217}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003218
3219static int __init cgroup_disable(char *str)
3220{
3221 int i;
3222 char *token;
3223
3224 while ((token = strsep(&str, ",")) != NULL) {
3225 if (!*token)
3226 continue;
3227
3228 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3229 struct cgroup_subsys *ss = subsys[i];
3230
3231 if (!strcmp(token, ss->name)) {
3232 ss->disabled = 1;
3233 printk(KERN_INFO "Disabling %s control group"
3234 " subsystem\n", ss->name);
3235 break;
3236 }
3237 }
3238 }
3239 return 1;
3240}
3241__setup("cgroup_disable=", cgroup_disable);