blob: fea11c5c990c09f74de068aa850086df9c3e802c [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
11 *
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
14 *
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
19 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
23 */
24
25#include <linux/cgroup.h>
26#include <linux/errno.h>
27#include <linux/fs.h>
28#include <linux/kernel.h>
29#include <linux/list.h>
30#include <linux/mm.h>
31#include <linux/mutex.h>
32#include <linux/mount.h>
33#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070034#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/rcupdate.h>
36#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070037#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070038#include <linux/seq_file.h>
39#include <linux/slab.h>
40#include <linux/magic.h>
41#include <linux/spinlock.h>
42#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070043#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070044#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070045#include <linux/delayacct.h>
46#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070047#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040048#include <linux/namei.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070049
Paul Menageddbcc7e2007-10-18 23:39:30 -070050#include <asm/atomic.h>
51
Paul Menage81a6a5c2007-10-18 23:39:38 -070052static DEFINE_MUTEX(cgroup_mutex);
53
Paul Menageddbcc7e2007-10-18 23:39:30 -070054/* Generate an array of cgroup subsystem pointers */
55#define SUBSYS(_x) &_x ## _subsys,
56
57static struct cgroup_subsys *subsys[] = {
58#include <linux/cgroup_subsys.h>
59};
60
61/*
62 * A cgroupfs_root represents the root of a cgroup hierarchy,
63 * and may be associated with a superblock to form an active
64 * hierarchy
65 */
66struct cgroupfs_root {
67 struct super_block *sb;
68
69 /*
70 * The bitmask of subsystems intended to be attached to this
71 * hierarchy
72 */
73 unsigned long subsys_bits;
74
75 /* The bitmask of subsystems currently attached to this hierarchy */
76 unsigned long actual_subsys_bits;
77
78 /* A list running through the attached subsystems */
79 struct list_head subsys_list;
80
81 /* The root cgroup for this hierarchy */
82 struct cgroup top_cgroup;
83
84 /* Tracks how many cgroups are currently defined in hierarchy.*/
85 int number_of_cgroups;
86
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 Menagee788e062008-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
Paul Menageddbcc7e2007-10-18 23:39:30 -070097/*
98 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
99 * subsystems that are otherwise unattached - it never has more than a
100 * single cgroup, and all tasks are part of that cgroup.
101 */
102static struct cgroupfs_root rootnode;
103
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700104/*
105 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
106 * cgroup_subsys->use_id != 0.
107 */
108#define CSS_ID_MAX (65535)
109struct css_id {
110 /*
111 * The css to which this ID points. This pointer is set to valid value
112 * after cgroup is populated. If cgroup is removed, this will be NULL.
113 * This pointer is expected to be RCU-safe because destroy()
114 * is called after synchronize_rcu(). But for safe use, css_is_removed()
115 * css_tryget() should be used for avoiding race.
116 */
117 struct cgroup_subsys_state *css;
118 /*
119 * ID of this css.
120 */
121 unsigned short id;
122 /*
123 * Depth in hierarchy which this ID belongs to.
124 */
125 unsigned short depth;
126 /*
127 * ID is freed by RCU. (and lookup routine is RCU safe.)
128 */
129 struct rcu_head rcu_head;
130 /*
131 * Hierarchy of CSS ID belongs to.
132 */
133 unsigned short stack[0]; /* Array of Length (depth+1) */
134};
135
136
Paul Menageddbcc7e2007-10-18 23:39:30 -0700137/* The list of hierarchy roots */
138
139static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700140static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700141
142/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
143#define dummytop (&rootnode.top_cgroup)
144
145/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800146 * check for fork/exit handlers to call. This avoids us having to do
147 * extra work in the fork/exit path if none of the subsystems need to
148 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700149 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700150static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700151
Paul Menageddbcc7e2007-10-18 23:39:30 -0700152/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700153inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700154{
Paul Menagebd89aab2007-10-18 23:40:44 -0700155 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700156}
157
158/* bits in struct cgroupfs_root flags field */
159enum {
160 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
161};
162
Adrian Bunke9685a02008-02-07 00:13:46 -0800163static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700164{
165 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700166 (1 << CGRP_RELEASABLE) |
167 (1 << CGRP_NOTIFY_ON_RELEASE);
168 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700169}
170
Adrian Bunke9685a02008-02-07 00:13:46 -0800171static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700172{
Paul Menagebd89aab2007-10-18 23:40:44 -0700173 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700174}
175
Paul Menageddbcc7e2007-10-18 23:39:30 -0700176/*
177 * for_each_subsys() allows you to iterate on each subsystem attached to
178 * an active hierarchy
179 */
180#define for_each_subsys(_root, _ss) \
181list_for_each_entry(_ss, &_root->subsys_list, sibling)
182
Li Zefane5f6a862009-01-07 18:07:41 -0800183/* for_each_active_root() allows you to iterate across the active hierarchies */
184#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700185list_for_each_entry(_root, &roots, root_list)
186
Paul Menage81a6a5c2007-10-18 23:39:38 -0700187/* the list of cgroups eligible for automatic release. Protected by
188 * release_list_lock */
189static LIST_HEAD(release_list);
190static DEFINE_SPINLOCK(release_list_lock);
191static void cgroup_release_agent(struct work_struct *work);
192static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700193static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700194
Paul Menage817929e2007-10-18 23:39:36 -0700195/* Link structure for associating css_set objects with cgroups */
196struct cg_cgroup_link {
197 /*
198 * List running through cg_cgroup_links associated with a
199 * cgroup, anchored on cgroup->css_sets
200 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700201 struct list_head cgrp_link_list;
Paul Menage817929e2007-10-18 23:39:36 -0700202 /*
203 * List running through cg_cgroup_links pointing at a
204 * single css_set object, anchored on css_set->cg_links
205 */
206 struct list_head cg_link_list;
207 struct css_set *cg;
208};
209
210/* The default css_set - used by init and its children prior to any
211 * hierarchies being mounted. It contains a pointer to the root state
212 * for each subsystem. Also used to anchor the list of css_sets. Not
213 * reference-counted, to improve performance when child cgroups
214 * haven't been created.
215 */
216
217static struct css_set init_css_set;
218static struct cg_cgroup_link init_css_set_link;
219
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700220static int cgroup_subsys_init_idr(struct cgroup_subsys *ss);
221
Paul Menage817929e2007-10-18 23:39:36 -0700222/* css_set_lock protects the list of css_set objects, and the
223 * chain of tasks off each css_set. Nests outside task->alloc_lock
224 * due to cgroup_iter_start() */
225static DEFINE_RWLOCK(css_set_lock);
226static int css_set_count;
227
Li Zefan472b1052008-04-29 01:00:11 -0700228/* hash table for cgroup groups. This improves the performance to
229 * find an existing css_set */
230#define CSS_SET_HASH_BITS 7
231#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
232static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
233
234static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
235{
236 int i;
237 int index;
238 unsigned long tmp = 0UL;
239
240 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
241 tmp += (unsigned long)css[i];
242 tmp = (tmp >> 16) ^ tmp;
243
244 index = hash_long(tmp, CSS_SET_HASH_BITS);
245
246 return &css_set_table[index];
247}
248
Paul Menage817929e2007-10-18 23:39:36 -0700249/* We don't maintain the lists running through each css_set to its
250 * task until after the first call to cgroup_iter_start(). This
251 * reduces the fork()/exit() overhead for people who have cgroups
252 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700253static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700254
255/* When we create or destroy a css_set, the operation simply
256 * takes/releases a reference count on all the cgroups referenced
257 * by subsystems in this css_set. This can end up multiple-counting
258 * some cgroups, but that's OK - the ref-count is just a
259 * busy/not-busy indicator; ensuring that we only count each cgroup
260 * once would require taking a global lock to ensure that no
Paul Menageb4f48b62007-10-18 23:39:33 -0700261 * subsystems moved between hierarchies while we were doing so.
262 *
263 * Possible TODO: decide at boot time based on the number of
264 * registered subsystems and the number of CPUs or NUMA nodes whether
265 * it's better for performance to ref-count every subsystem, or to
266 * take a global lock and only add one ref count to each hierarchy.
267 */
Paul Menageb4f48b62007-10-18 23:39:33 -0700268
Paul Menage817929e2007-10-18 23:39:36 -0700269/*
270 * unlink a css_set from the list and free it
271 */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700272static void unlink_css_set(struct css_set *cg)
Paul Menageb4f48b62007-10-18 23:39:33 -0700273{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700274 struct cg_cgroup_link *link;
275 struct cg_cgroup_link *saved_link;
276
Li Zefan472b1052008-04-29 01:00:11 -0700277 hlist_del(&cg->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700278 css_set_count--;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700279
280 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
281 cg_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -0700282 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700283 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700284 kfree(link);
285 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700286}
287
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700288static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700289{
290 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700291 /*
292 * Ensure that the refcount doesn't hit zero while any readers
293 * can see it. Similar to atomic_dec_and_lock(), but for an
294 * rwlock
295 */
296 if (atomic_add_unless(&cg->refcount, -1, 1))
297 return;
298 write_lock(&css_set_lock);
299 if (!atomic_dec_and_test(&cg->refcount)) {
300 write_unlock(&css_set_lock);
301 return;
302 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700303 unlink_css_set(cg);
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700304 write_unlock(&css_set_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700305
306 rcu_read_lock();
307 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagea47295e2009-01-07 18:07:44 -0800308 struct cgroup *cgrp = rcu_dereference(cg->subsys[i]->cgroup);
Paul Menagebd89aab2007-10-18 23:40:44 -0700309 if (atomic_dec_and_test(&cgrp->count) &&
310 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700311 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700312 set_bit(CGRP_RELEASABLE, &cgrp->flags);
313 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700314 }
315 }
316 rcu_read_unlock();
Paul Menage817929e2007-10-18 23:39:36 -0700317 kfree(cg);
318}
319
320/*
321 * refcounted get/put for css_set objects
322 */
323static inline void get_css_set(struct css_set *cg)
324{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700325 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700326}
327
328static inline void put_css_set(struct css_set *cg)
329{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700330 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700331}
332
Paul Menage81a6a5c2007-10-18 23:39:38 -0700333static inline void put_css_set_taskexit(struct css_set *cg)
334{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700335 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700336}
337
Paul Menage817929e2007-10-18 23:39:36 -0700338/*
339 * find_existing_css_set() is a helper for
340 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700341 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700342 *
343 * oldcg: the cgroup group that we're using before the cgroup
344 * transition
345 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700346 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700347 *
348 * template: location in which to build the desired set of subsystem
349 * state objects for the new cgroup group
350 */
Paul Menage817929e2007-10-18 23:39:36 -0700351static struct css_set *find_existing_css_set(
352 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700353 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700354 struct cgroup_subsys_state *template[])
355{
356 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700357 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700358 struct hlist_head *hhead;
359 struct hlist_node *node;
360 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700361
362 /* Built the set of subsystem state objects that we want to
363 * see in the new css_set */
364 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800365 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700366 /* Subsystem is in this hierarchy. So we want
367 * the subsystem state from the new
368 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700369 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700370 } else {
371 /* Subsystem is not in this hierarchy, so we
372 * don't want to change the subsystem state */
373 template[i] = oldcg->subsys[i];
374 }
375 }
376
Li Zefan472b1052008-04-29 01:00:11 -0700377 hhead = css_set_hash(template);
378 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage817929e2007-10-18 23:39:36 -0700379 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
380 /* All subsystems matched */
381 return cg;
382 }
Li Zefan472b1052008-04-29 01:00:11 -0700383 }
Paul Menage817929e2007-10-18 23:39:36 -0700384
385 /* No existing cgroup group matched */
386 return NULL;
387}
388
Paul Menage817929e2007-10-18 23:39:36 -0700389static void free_cg_links(struct list_head *tmp)
390{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700391 struct cg_cgroup_link *link;
392 struct cg_cgroup_link *saved_link;
393
394 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700395 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700396 kfree(link);
397 }
398}
399
400/*
Li Zefan36553432008-07-29 22:33:19 -0700401 * allocate_cg_links() allocates "count" cg_cgroup_link structures
402 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
403 * success or a negative error
404 */
405static int allocate_cg_links(int count, struct list_head *tmp)
406{
407 struct cg_cgroup_link *link;
408 int i;
409 INIT_LIST_HEAD(tmp);
410 for (i = 0; i < count; i++) {
411 link = kmalloc(sizeof(*link), GFP_KERNEL);
412 if (!link) {
413 free_cg_links(tmp);
414 return -ENOMEM;
415 }
416 list_add(&link->cgrp_link_list, tmp);
417 }
418 return 0;
419}
420
Li Zefanc12f65d2009-01-07 18:07:42 -0800421/**
422 * link_css_set - a helper function to link a css_set to a cgroup
423 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
424 * @cg: the css_set to be linked
425 * @cgrp: the destination cgroup
426 */
427static void link_css_set(struct list_head *tmp_cg_links,
428 struct css_set *cg, struct cgroup *cgrp)
429{
430 struct cg_cgroup_link *link;
431
432 BUG_ON(list_empty(tmp_cg_links));
433 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
434 cgrp_link_list);
435 link->cg = cg;
436 list_move(&link->cgrp_link_list, &cgrp->css_sets);
437 list_add(&link->cg_link_list, &cg->cg_links);
438}
439
Li Zefan36553432008-07-29 22:33:19 -0700440/*
Paul Menage817929e2007-10-18 23:39:36 -0700441 * find_css_set() takes an existing cgroup group and a
442 * cgroup object, and returns a css_set object that's
443 * equivalent to the old group, but with the given cgroup
444 * substituted into the appropriate hierarchy. Must be called with
445 * cgroup_mutex held
446 */
Paul Menage817929e2007-10-18 23:39:36 -0700447static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700448 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700449{
450 struct css_set *res;
451 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
452 int i;
453
454 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700455
Li Zefan472b1052008-04-29 01:00:11 -0700456 struct hlist_head *hhead;
457
Paul Menage817929e2007-10-18 23:39:36 -0700458 /* First see if we already have a cgroup group that matches
459 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700460 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700461 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700462 if (res)
463 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700464 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700465
466 if (res)
467 return res;
468
469 res = kmalloc(sizeof(*res), GFP_KERNEL);
470 if (!res)
471 return NULL;
472
473 /* Allocate all the cg_cgroup_link objects that we'll need */
474 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
475 kfree(res);
476 return NULL;
477 }
478
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700479 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700480 INIT_LIST_HEAD(&res->cg_links);
481 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700482 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700483
484 /* Copy the set of subsystem state objects generated in
485 * find_existing_css_set() */
486 memcpy(res->subsys, template, sizeof(res->subsys));
487
488 write_lock(&css_set_lock);
489 /* Add reference counts and links from the new css_set. */
490 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700491 struct cgroup *cgrp = res->subsys[i]->cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700492 struct cgroup_subsys *ss = subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -0700493 atomic_inc(&cgrp->count);
Paul Menage817929e2007-10-18 23:39:36 -0700494 /*
495 * We want to add a link once per cgroup, so we
496 * only do it for the first subsystem in each
497 * hierarchy
498 */
Li Zefanc12f65d2009-01-07 18:07:42 -0800499 if (ss->root->subsys_list.next == &ss->sibling)
500 link_css_set(&tmp_cg_links, res, cgrp);
Paul Menage817929e2007-10-18 23:39:36 -0700501 }
Li Zefanc12f65d2009-01-07 18:07:42 -0800502 if (list_empty(&rootnode.subsys_list))
503 link_css_set(&tmp_cg_links, res, dummytop);
Paul Menage817929e2007-10-18 23:39:36 -0700504
505 BUG_ON(!list_empty(&tmp_cg_links));
506
Paul Menage817929e2007-10-18 23:39:36 -0700507 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700508
509 /* Add this cgroup group to the hash table */
510 hhead = css_set_hash(res->subsys);
511 hlist_add_head(&res->hlist, hhead);
512
Paul Menage817929e2007-10-18 23:39:36 -0700513 write_unlock(&css_set_lock);
514
515 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700516}
517
Paul Menageddbcc7e2007-10-18 23:39:30 -0700518/*
519 * There is one global cgroup mutex. We also require taking
520 * task_lock() when dereferencing a task's cgroup subsys pointers.
521 * See "The task_lock() exception", at the end of this comment.
522 *
523 * A task must hold cgroup_mutex to modify cgroups.
524 *
525 * Any task can increment and decrement the count field without lock.
526 * So in general, code holding cgroup_mutex can't rely on the count
527 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800528 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700529 * means that no tasks are currently attached, therefore there is no
530 * way a task attached to that cgroup can fork (the other way to
531 * increment the count). So code holding cgroup_mutex can safely
532 * assume that if the count is zero, it will stay zero. Similarly, if
533 * a task holds cgroup_mutex on a cgroup with zero count, it
534 * knows that the cgroup won't be removed, as cgroup_rmdir()
535 * needs that mutex.
536 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700537 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
538 * (usually) take cgroup_mutex. These are the two most performance
539 * critical pieces of code here. The exception occurs on cgroup_exit(),
540 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
541 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800542 * to the release agent with the name of the cgroup (path relative to
543 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700544 *
545 * A cgroup can only be deleted if both its 'count' of using tasks
546 * is zero, and its list of 'children' cgroups is empty. Since all
547 * tasks in the system use _some_ cgroup, and since there is always at
548 * least one task in the system (init, pid == 1), therefore, top_cgroup
549 * always has either children cgroups and/or using tasks. So we don't
550 * need a special hack to ensure that top_cgroup cannot be deleted.
551 *
552 * The task_lock() exception
553 *
554 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800555 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800556 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700557 * several performance critical places that need to reference
558 * task->cgroup without the expense of grabbing a system global
559 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800560 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700561 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
562 * the task_struct routinely used for such matters.
563 *
564 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800565 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700566 */
567
Paul Menageddbcc7e2007-10-18 23:39:30 -0700568/**
569 * cgroup_lock - lock out any changes to cgroup structures
570 *
571 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700572void cgroup_lock(void)
573{
574 mutex_lock(&cgroup_mutex);
575}
576
577/**
578 * cgroup_unlock - release lock on cgroup changes
579 *
580 * Undo the lock taken in a previous cgroup_lock() call.
581 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700582void cgroup_unlock(void)
583{
584 mutex_unlock(&cgroup_mutex);
585}
586
587/*
588 * A couple of forward declarations required, due to cyclic reference loop:
589 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
590 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
591 * -> cgroup_mkdir.
592 */
593
594static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
595static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700596static int cgroup_populate_dir(struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700597static struct inode_operations cgroup_dir_inode_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700598static struct file_operations proc_cgroupstats_operations;
599
600static struct backing_dev_info cgroup_backing_dev_info = {
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700601 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700602};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700603
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700604static int alloc_css_id(struct cgroup_subsys *ss,
605 struct cgroup *parent, struct cgroup *child);
606
Paul Menageddbcc7e2007-10-18 23:39:30 -0700607static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
608{
609 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700610
611 if (inode) {
612 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100613 inode->i_uid = current_fsuid();
614 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700615 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
616 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
617 }
618 return inode;
619}
620
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800621/*
622 * Call subsys's pre_destroy handler.
623 * This is called before css refcnt check.
624 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700625static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800626{
627 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700628 int ret = 0;
629
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800630 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700631 if (ss->pre_destroy) {
632 ret = ss->pre_destroy(ss, cgrp);
633 if (ret)
634 break;
635 }
636 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800637}
638
Paul Menagea47295e2009-01-07 18:07:44 -0800639static void free_cgroup_rcu(struct rcu_head *obj)
640{
641 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
642
643 kfree(cgrp);
644}
645
Paul Menageddbcc7e2007-10-18 23:39:30 -0700646static void cgroup_diput(struct dentry *dentry, struct inode *inode)
647{
648 /* is dentry a directory ? if so, kfree() associated cgroup */
649 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700650 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800651 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700652 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700653 /* It's possible for external users to be holding css
654 * reference counts on a cgroup; css_put() needs to
655 * be able to access the cgroup after decrementing
656 * the reference count in order to know if it needs to
657 * queue the cgroup to be handled by the release
658 * agent */
659 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800660
661 mutex_lock(&cgroup_mutex);
662 /*
663 * Release the subsystem state objects.
664 */
Li Zefan75139b82009-01-07 18:07:33 -0800665 for_each_subsys(cgrp->root, ss)
666 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800667
668 cgrp->root->number_of_cgroups--;
669 mutex_unlock(&cgroup_mutex);
670
Paul Menagea47295e2009-01-07 18:07:44 -0800671 /*
672 * Drop the active superblock reference that we took when we
673 * created the cgroup
674 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800675 deactivate_super(cgrp->root->sb);
676
Paul Menagea47295e2009-01-07 18:07:44 -0800677 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700678 }
679 iput(inode);
680}
681
682static void remove_dir(struct dentry *d)
683{
684 struct dentry *parent = dget(d->d_parent);
685
686 d_delete(d);
687 simple_rmdir(parent->d_inode, d);
688 dput(parent);
689}
690
691static void cgroup_clear_directory(struct dentry *dentry)
692{
693 struct list_head *node;
694
695 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
696 spin_lock(&dcache_lock);
697 node = dentry->d_subdirs.next;
698 while (node != &dentry->d_subdirs) {
699 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
700 list_del_init(node);
701 if (d->d_inode) {
702 /* This should never be called on a cgroup
703 * directory with child cgroups */
704 BUG_ON(d->d_inode->i_mode & S_IFDIR);
705 d = dget_locked(d);
706 spin_unlock(&dcache_lock);
707 d_delete(d);
708 simple_unlink(dentry->d_inode, d);
709 dput(d);
710 spin_lock(&dcache_lock);
711 }
712 node = dentry->d_subdirs.next;
713 }
714 spin_unlock(&dcache_lock);
715}
716
717/*
718 * NOTE : the dentry must have been dget()'ed
719 */
720static void cgroup_d_remove_dir(struct dentry *dentry)
721{
722 cgroup_clear_directory(dentry);
723
724 spin_lock(&dcache_lock);
725 list_del_init(&dentry->d_u.d_child);
726 spin_unlock(&dcache_lock);
727 remove_dir(dentry);
728}
729
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700730/*
731 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
732 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
733 * reference to css->refcnt. In general, this refcnt is expected to goes down
734 * to zero, soon.
735 *
736 * CGRP_WAIT_ON_RMDIR flag is modified under cgroup's inode->i_mutex;
737 */
738DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
739
740static void cgroup_wakeup_rmdir_waiters(const struct cgroup *cgrp)
741{
742 if (unlikely(test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
743 wake_up_all(&cgroup_rmdir_waitq);
744}
745
Paul Menageddbcc7e2007-10-18 23:39:30 -0700746static int rebind_subsystems(struct cgroupfs_root *root,
747 unsigned long final_bits)
748{
749 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700750 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700751 int i;
752
753 removed_bits = root->actual_subsys_bits & ~final_bits;
754 added_bits = final_bits & ~root->actual_subsys_bits;
755 /* Check that any added subsystems are currently free */
756 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800757 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700758 struct cgroup_subsys *ss = subsys[i];
759 if (!(bit & added_bits))
760 continue;
761 if (ss->root != &rootnode) {
762 /* Subsystem isn't free */
763 return -EBUSY;
764 }
765 }
766
767 /* Currently we don't handle adding/removing subsystems when
768 * any child cgroups exist. This is theoretically supportable
769 * but involves complex error handling, so it's being left until
770 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800771 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700772 return -EBUSY;
773
774 /* Process each subsystem */
775 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
776 struct cgroup_subsys *ss = subsys[i];
777 unsigned long bit = 1UL << i;
778 if (bit & added_bits) {
779 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700780 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700781 BUG_ON(!dummytop->subsys[i]);
782 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800783 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700784 cgrp->subsys[i] = dummytop->subsys[i];
785 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800786 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800787 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700789 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800790 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791 } else if (bit & removed_bits) {
792 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700793 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
794 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800795 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796 if (ss->bind)
797 ss->bind(ss, dummytop);
798 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700799 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800800 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -0800801 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -0800802 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803 } else if (bit & final_bits) {
804 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700805 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806 } else {
807 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700808 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809 }
810 }
811 root->subsys_bits = root->actual_subsys_bits = final_bits;
812 synchronize_rcu();
813
814 return 0;
815}
816
817static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
818{
819 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
820 struct cgroup_subsys *ss;
821
822 mutex_lock(&cgroup_mutex);
823 for_each_subsys(root, ss)
824 seq_printf(seq, ",%s", ss->name);
825 if (test_bit(ROOT_NOPREFIX, &root->flags))
826 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700827 if (strlen(root->release_agent_path))
828 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700829 mutex_unlock(&cgroup_mutex);
830 return 0;
831}
832
833struct cgroup_sb_opts {
834 unsigned long subsys_bits;
835 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700836 char *release_agent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700837};
838
839/* Convert a hierarchy specifier into a bitmask of subsystems and
840 * flags. */
841static int parse_cgroupfs_options(char *data,
842 struct cgroup_sb_opts *opts)
843{
844 char *token, *o = data ?: "all";
845
846 opts->subsys_bits = 0;
847 opts->flags = 0;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700848 opts->release_agent = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700849
850 while ((token = strsep(&o, ",")) != NULL) {
851 if (!*token)
852 return -EINVAL;
853 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700854 /* Add all non-disabled subsystems */
855 int i;
856 opts->subsys_bits = 0;
857 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
858 struct cgroup_subsys *ss = subsys[i];
859 if (!ss->disabled)
860 opts->subsys_bits |= 1ul << i;
861 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700862 } else if (!strcmp(token, "noprefix")) {
863 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700864 } else if (!strncmp(token, "release_agent=", 14)) {
865 /* Specifying two release agents is forbidden */
866 if (opts->release_agent)
867 return -EINVAL;
868 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
869 if (!opts->release_agent)
870 return -ENOMEM;
871 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
872 opts->release_agent[PATH_MAX - 1] = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700873 } else {
874 struct cgroup_subsys *ss;
875 int i;
876 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
877 ss = subsys[i];
878 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700879 if (!ss->disabled)
880 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700881 break;
882 }
883 }
884 if (i == CGROUP_SUBSYS_COUNT)
885 return -ENOENT;
886 }
887 }
888
889 /* We can't have an empty hierarchy */
890 if (!opts->subsys_bits)
891 return -EINVAL;
892
893 return 0;
894}
895
896static int cgroup_remount(struct super_block *sb, int *flags, char *data)
897{
898 int ret = 0;
899 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -0700900 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700901 struct cgroup_sb_opts opts;
902
Paul Menagebd89aab2007-10-18 23:40:44 -0700903 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700904 mutex_lock(&cgroup_mutex);
905
906 /* See what subsystems are wanted */
907 ret = parse_cgroupfs_options(data, &opts);
908 if (ret)
909 goto out_unlock;
910
911 /* Don't allow flags to change at remount */
912 if (opts.flags != root->flags) {
913 ret = -EINVAL;
914 goto out_unlock;
915 }
916
917 ret = rebind_subsystems(root, opts.subsys_bits);
918
919 /* (re)populate subsystem files */
920 if (!ret)
Paul Menagebd89aab2007-10-18 23:40:44 -0700921 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700922
Paul Menage81a6a5c2007-10-18 23:39:38 -0700923 if (opts.release_agent)
924 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700925 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -0700926 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700927 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700928 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700929 return ret;
930}
931
932static struct super_operations cgroup_ops = {
933 .statfs = simple_statfs,
934 .drop_inode = generic_delete_inode,
935 .show_options = cgroup_show_options,
936 .remount_fs = cgroup_remount,
937};
938
Paul Menagecc31edc2008-10-18 20:28:04 -0700939static void init_cgroup_housekeeping(struct cgroup *cgrp)
940{
941 INIT_LIST_HEAD(&cgrp->sibling);
942 INIT_LIST_HEAD(&cgrp->children);
943 INIT_LIST_HEAD(&cgrp->css_sets);
944 INIT_LIST_HEAD(&cgrp->release_list);
945 init_rwsem(&cgrp->pids_mutex);
946}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700947static void init_cgroup_root(struct cgroupfs_root *root)
948{
Paul Menagebd89aab2007-10-18 23:40:44 -0700949 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700950 INIT_LIST_HEAD(&root->subsys_list);
951 INIT_LIST_HEAD(&root->root_list);
952 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -0700953 cgrp->root = root;
954 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -0700955 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700956}
957
958static int cgroup_test_super(struct super_block *sb, void *data)
959{
960 struct cgroupfs_root *new = data;
961 struct cgroupfs_root *root = sb->s_fs_info;
962
963 /* First check subsystems */
964 if (new->subsys_bits != root->subsys_bits)
965 return 0;
966
967 /* Next check flags */
968 if (new->flags != root->flags)
969 return 0;
970
971 return 1;
972}
973
974static int cgroup_set_super(struct super_block *sb, void *data)
975{
976 int ret;
977 struct cgroupfs_root *root = data;
978
979 ret = set_anon_super(sb, NULL);
980 if (ret)
981 return ret;
982
983 sb->s_fs_info = root;
984 root->sb = sb;
985
986 sb->s_blocksize = PAGE_CACHE_SIZE;
987 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
988 sb->s_magic = CGROUP_SUPER_MAGIC;
989 sb->s_op = &cgroup_ops;
990
991 return 0;
992}
993
994static int cgroup_get_rootdir(struct super_block *sb)
995{
996 struct inode *inode =
997 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
998 struct dentry *dentry;
999
1000 if (!inode)
1001 return -ENOMEM;
1002
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 inode->i_fop = &simple_dir_operations;
1004 inode->i_op = &cgroup_dir_inode_operations;
1005 /* directories start off with i_nlink == 2 (for "." entry) */
1006 inc_nlink(inode);
1007 dentry = d_alloc_root(inode);
1008 if (!dentry) {
1009 iput(inode);
1010 return -ENOMEM;
1011 }
1012 sb->s_root = dentry;
1013 return 0;
1014}
1015
1016static int cgroup_get_sb(struct file_system_type *fs_type,
1017 int flags, const char *unused_dev_name,
1018 void *data, struct vfsmount *mnt)
1019{
1020 struct cgroup_sb_opts opts;
1021 int ret = 0;
1022 struct super_block *sb;
1023 struct cgroupfs_root *root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001024 struct list_head tmp_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025
1026 /* First find the desired set of subsystems */
1027 ret = parse_cgroupfs_options(data, &opts);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001028 if (ret) {
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001029 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030 return ret;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001031 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032
1033 root = kzalloc(sizeof(*root), GFP_KERNEL);
Li Zefanf7770732008-02-23 15:24:10 -08001034 if (!root) {
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001035 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 return -ENOMEM;
Li Zefanf7770732008-02-23 15:24:10 -08001037 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001038
1039 init_cgroup_root(root);
1040 root->subsys_bits = opts.subsys_bits;
1041 root->flags = opts.flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001042 if (opts.release_agent) {
1043 strcpy(root->release_agent_path, opts.release_agent);
1044 kfree(opts.release_agent);
1045 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046
1047 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
1048
1049 if (IS_ERR(sb)) {
1050 kfree(root);
1051 return PTR_ERR(sb);
1052 }
1053
1054 if (sb->s_fs_info != root) {
1055 /* Reusing an existing superblock */
1056 BUG_ON(sb->s_root == NULL);
1057 kfree(root);
1058 root = NULL;
1059 } else {
1060 /* New superblock */
Li Zefanc12f65d2009-01-07 18:07:42 -08001061 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001062 struct inode *inode;
Li Zefan28fd5df2008-04-29 01:00:13 -07001063 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001064
1065 BUG_ON(sb->s_root != NULL);
1066
1067 ret = cgroup_get_rootdir(sb);
1068 if (ret)
1069 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001070 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071
Paul Menage817929e2007-10-18 23:39:36 -07001072 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073 mutex_lock(&cgroup_mutex);
1074
Paul Menage817929e2007-10-18 23:39:36 -07001075 /*
1076 * We're accessing css_set_count without locking
1077 * css_set_lock here, but that's OK - it can only be
1078 * increased by someone holding cgroup_lock, and
1079 * that's us. The worst that can happen is that we
1080 * have some link structures left over
1081 */
1082 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1083 if (ret) {
1084 mutex_unlock(&cgroup_mutex);
1085 mutex_unlock(&inode->i_mutex);
1086 goto drop_new_super;
1087 }
1088
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089 ret = rebind_subsystems(root, root->subsys_bits);
1090 if (ret == -EBUSY) {
1091 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001092 mutex_unlock(&inode->i_mutex);
Li Zefan20ca9b32008-12-23 13:57:14 -08001093 goto free_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 }
1095
1096 /* EBUSY should be the only error here */
1097 BUG_ON(ret);
1098
1099 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001100 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101
Li Zefanc12f65d2009-01-07 18:07:42 -08001102 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001103 root->top_cgroup.dentry = sb->s_root;
1104
Paul Menage817929e2007-10-18 23:39:36 -07001105 /* Link the top cgroup in this hierarchy into all
1106 * the css_set objects */
1107 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001108 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1109 struct hlist_head *hhead = &css_set_table[i];
1110 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001111 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001112
Li Zefanc12f65d2009-01-07 18:07:42 -08001113 hlist_for_each_entry(cg, node, hhead, hlist)
1114 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001115 }
Paul Menage817929e2007-10-18 23:39:36 -07001116 write_unlock(&css_set_lock);
1117
1118 free_cg_links(&tmp_cg_links);
1119
Li Zefanc12f65d2009-01-07 18:07:42 -08001120 BUG_ON(!list_empty(&root_cgrp->sibling));
1121 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001122 BUG_ON(root->number_of_cgroups != 1);
1123
Li Zefanc12f65d2009-01-07 18:07:42 -08001124 cgroup_populate_dir(root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001125 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001126 mutex_unlock(&cgroup_mutex);
1127 }
1128
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001129 simple_set_mnt(mnt, sb);
1130 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131
Li Zefan20ca9b32008-12-23 13:57:14 -08001132 free_cg_links:
1133 free_cg_links(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001134 drop_new_super:
1135 up_write(&sb->s_umount);
1136 deactivate_super(sb);
1137 return ret;
1138}
1139
1140static void cgroup_kill_sb(struct super_block *sb) {
1141 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001142 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001143 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001144 struct cg_cgroup_link *link;
1145 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001146
1147 BUG_ON(!root);
1148
1149 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001150 BUG_ON(!list_empty(&cgrp->children));
1151 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001152
1153 mutex_lock(&cgroup_mutex);
1154
1155 /* Rebind all subsystems back to the default hierarchy */
1156 ret = rebind_subsystems(root, 0);
1157 /* Shouldn't be able to fail ... */
1158 BUG_ON(ret);
1159
Paul Menage817929e2007-10-18 23:39:36 -07001160 /*
1161 * Release all the links from css_sets to this hierarchy's
1162 * root cgroup
1163 */
1164 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001165
1166 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1167 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001168 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001169 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001170 kfree(link);
1171 }
1172 write_unlock(&css_set_lock);
1173
Paul Menage839ec542009-01-29 14:25:22 -08001174 if (!list_empty(&root->root_list)) {
1175 list_del(&root->root_list);
1176 root_count--;
1177 }
Li Zefane5f6a862009-01-07 18:07:41 -08001178
Paul Menageddbcc7e2007-10-18 23:39:30 -07001179 mutex_unlock(&cgroup_mutex);
1180
Paul Menageddbcc7e2007-10-18 23:39:30 -07001181 kill_litter_super(sb);
Li Zefan67e055d2009-02-18 14:48:20 -08001182 kfree(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001183}
1184
1185static struct file_system_type cgroup_fs_type = {
1186 .name = "cgroup",
1187 .get_sb = cgroup_get_sb,
1188 .kill_sb = cgroup_kill_sb,
1189};
1190
Paul Menagebd89aab2007-10-18 23:40:44 -07001191static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001192{
1193 return dentry->d_fsdata;
1194}
1195
1196static inline struct cftype *__d_cft(struct dentry *dentry)
1197{
1198 return dentry->d_fsdata;
1199}
1200
Li Zefana043e3b2008-02-23 15:24:09 -08001201/**
1202 * cgroup_path - generate the path of a cgroup
1203 * @cgrp: the cgroup in question
1204 * @buf: the buffer to write the path into
1205 * @buflen: the length of the buffer
1206 *
Paul Menagea47295e2009-01-07 18:07:44 -08001207 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1208 * reference. Writes path of cgroup into buf. Returns 0 on success,
1209 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001210 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001211int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001212{
1213 char *start;
Paul Menagea47295e2009-01-07 18:07:44 -08001214 struct dentry *dentry = rcu_dereference(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001215
Paul Menagea47295e2009-01-07 18:07:44 -08001216 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001217 /*
1218 * Inactive subsystems have no dentry for their root
1219 * cgroup
1220 */
1221 strcpy(buf, "/");
1222 return 0;
1223 }
1224
1225 start = buf + buflen;
1226
1227 *--start = '\0';
1228 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001229 int len = dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001230 if ((start -= len) < buf)
1231 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001232 memcpy(start, cgrp->dentry->d_name.name, len);
1233 cgrp = cgrp->parent;
1234 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001235 break;
Paul Menagea47295e2009-01-07 18:07:44 -08001236 dentry = rcu_dereference(cgrp->dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001237 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001238 continue;
1239 if (--start < buf)
1240 return -ENAMETOOLONG;
1241 *start = '/';
1242 }
1243 memmove(buf, start, buf + buflen - start);
1244 return 0;
1245}
1246
Paul Menagebbcb81d2007-10-18 23:39:32 -07001247/*
1248 * Return the first subsystem attached to a cgroup's hierarchy, and
1249 * its subsystem id.
1250 */
1251
Paul Menagebd89aab2007-10-18 23:40:44 -07001252static void get_first_subsys(const struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001253 struct cgroup_subsys_state **css, int *subsys_id)
1254{
Paul Menagebd89aab2007-10-18 23:40:44 -07001255 const struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001256 const struct cgroup_subsys *test_ss;
1257 BUG_ON(list_empty(&root->subsys_list));
1258 test_ss = list_entry(root->subsys_list.next,
1259 struct cgroup_subsys, sibling);
1260 if (css) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001261 *css = cgrp->subsys[test_ss->subsys_id];
Paul Menagebbcb81d2007-10-18 23:39:32 -07001262 BUG_ON(!*css);
1263 }
1264 if (subsys_id)
1265 *subsys_id = test_ss->subsys_id;
1266}
1267
Li Zefana043e3b2008-02-23 15:24:09 -08001268/**
1269 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1270 * @cgrp: the cgroup the task is attaching to
1271 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001272 *
Li Zefana043e3b2008-02-23 15:24:09 -08001273 * Call holding cgroup_mutex. May take task_lock of
1274 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001275 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001276int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001277{
1278 int retval = 0;
1279 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07001280 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001281 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001282 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001283 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001284 int subsys_id;
1285
Paul Menagebd89aab2007-10-18 23:40:44 -07001286 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001287
1288 /* Nothing to do if the task is already in that cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -07001289 oldcgrp = task_cgroup(tsk, subsys_id);
1290 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001291 return 0;
1292
1293 for_each_subsys(root, ss) {
1294 if (ss->can_attach) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001295 retval = ss->can_attach(ss, cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001296 if (retval)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001297 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001298 }
1299 }
1300
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001301 task_lock(tsk);
1302 cg = tsk->cgroups;
1303 get_css_set(cg);
1304 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001305 /*
1306 * Locate or allocate a new css_set for this task,
1307 * based on its final set of cgroups
1308 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001309 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001310 put_css_set(cg);
Paul Jacksone18f6312008-02-07 00:13:44 -08001311 if (!newcg)
Paul Menage817929e2007-10-18 23:39:36 -07001312 return -ENOMEM;
Paul Menage817929e2007-10-18 23:39:36 -07001313
Paul Menagebbcb81d2007-10-18 23:39:32 -07001314 task_lock(tsk);
1315 if (tsk->flags & PF_EXITING) {
1316 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001317 put_css_set(newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001318 return -ESRCH;
1319 }
Paul Menage817929e2007-10-18 23:39:36 -07001320 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001321 task_unlock(tsk);
1322
Paul Menage817929e2007-10-18 23:39:36 -07001323 /* Update the css_set linked lists if we're using them */
1324 write_lock(&css_set_lock);
1325 if (!list_empty(&tsk->cg_list)) {
1326 list_del(&tsk->cg_list);
1327 list_add(&tsk->cg_list, &newcg->tasks);
1328 }
1329 write_unlock(&css_set_lock);
1330
Paul Menagebbcb81d2007-10-18 23:39:32 -07001331 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001332 if (ss->attach)
Paul Menagebd89aab2007-10-18 23:40:44 -07001333 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001334 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001335 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001336 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001337 put_css_set(cg);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001338
1339 /*
1340 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1341 * is no longer empty.
1342 */
1343 cgroup_wakeup_rmdir_waiters(cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001344 return 0;
1345}
1346
1347/*
Paul Menageaf351022008-07-25 01:47:01 -07001348 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1349 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001350 */
Paul Menageaf351022008-07-25 01:47:01 -07001351static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001352{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001353 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001354 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001355 int ret;
1356
Paul Menagebbcb81d2007-10-18 23:39:32 -07001357 if (pid) {
1358 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001359 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001360 if (!tsk || tsk->flags & PF_EXITING) {
1361 rcu_read_unlock();
1362 return -ESRCH;
1363 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001364
David Howellsc69e8d92008-11-14 10:39:19 +11001365 tcred = __task_cred(tsk);
1366 if (cred->euid &&
1367 cred->euid != tcred->uid &&
1368 cred->euid != tcred->suid) {
1369 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001370 return -EACCES;
1371 }
David Howellsc69e8d92008-11-14 10:39:19 +11001372 get_task_struct(tsk);
1373 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001374 } else {
1375 tsk = current;
1376 get_task_struct(tsk);
1377 }
1378
Cliff Wickman956db3c2008-02-07 00:14:43 -08001379 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001380 put_task_struct(tsk);
1381 return ret;
1382}
1383
Paul Menageaf351022008-07-25 01:47:01 -07001384static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1385{
1386 int ret;
1387 if (!cgroup_lock_live_group(cgrp))
1388 return -ENODEV;
1389 ret = attach_task_by_pid(cgrp, pid);
1390 cgroup_unlock();
1391 return ret;
1392}
1393
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394/* The various types of files and directories in a cgroup file system */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001395enum cgroup_filetype {
1396 FILE_ROOT,
1397 FILE_DIR,
1398 FILE_TASKLIST,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001399 FILE_NOTIFY_ON_RELEASE,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001400 FILE_RELEASE_AGENT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401};
1402
Paul Menagee788e062008-07-25 01:46:59 -07001403/**
1404 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1405 * @cgrp: the cgroup to be checked for liveness
1406 *
Paul Menage84eea842008-07-25 01:47:00 -07001407 * On success, returns true; the lock should be later released with
1408 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07001409 */
Paul Menage84eea842008-07-25 01:47:00 -07001410bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07001411{
1412 mutex_lock(&cgroup_mutex);
1413 if (cgroup_is_removed(cgrp)) {
1414 mutex_unlock(&cgroup_mutex);
1415 return false;
1416 }
1417 return true;
1418}
1419
1420static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1421 const char *buffer)
1422{
1423 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1424 if (!cgroup_lock_live_group(cgrp))
1425 return -ENODEV;
1426 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001427 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001428 return 0;
1429}
1430
1431static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1432 struct seq_file *seq)
1433{
1434 if (!cgroup_lock_live_group(cgrp))
1435 return -ENODEV;
1436 seq_puts(seq, cgrp->root->release_agent_path);
1437 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001438 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001439 return 0;
1440}
1441
Paul Menage84eea842008-07-25 01:47:00 -07001442/* A buffer size big enough for numbers or short strings */
1443#define CGROUP_LOCAL_BUFFER_SIZE 64
1444
Paul Menagee73d2c62008-04-29 01:00:06 -07001445static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001446 struct file *file,
1447 const char __user *userbuf,
1448 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001449{
Paul Menage84eea842008-07-25 01:47:00 -07001450 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001451 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001452 char *end;
1453
1454 if (!nbytes)
1455 return -EINVAL;
1456 if (nbytes >= sizeof(buffer))
1457 return -E2BIG;
1458 if (copy_from_user(buffer, userbuf, nbytes))
1459 return -EFAULT;
1460
1461 buffer[nbytes] = 0; /* nul-terminate */
Paul Menageb7269df2008-04-29 00:59:59 -07001462 strstrip(buffer);
Paul Menagee73d2c62008-04-29 01:00:06 -07001463 if (cft->write_u64) {
1464 u64 val = simple_strtoull(buffer, &end, 0);
1465 if (*end)
1466 return -EINVAL;
1467 retval = cft->write_u64(cgrp, cft, val);
1468 } else {
1469 s64 val = simple_strtoll(buffer, &end, 0);
1470 if (*end)
1471 return -EINVAL;
1472 retval = cft->write_s64(cgrp, cft, val);
1473 }
Paul Menage355e0c42007-10-18 23:39:33 -07001474 if (!retval)
1475 retval = nbytes;
1476 return retval;
1477}
1478
Paul Menagedb3b1492008-07-25 01:46:58 -07001479static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1480 struct file *file,
1481 const char __user *userbuf,
1482 size_t nbytes, loff_t *unused_ppos)
1483{
Paul Menage84eea842008-07-25 01:47:00 -07001484 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001485 int retval = 0;
1486 size_t max_bytes = cft->max_write_len;
1487 char *buffer = local_buffer;
1488
1489 if (!max_bytes)
1490 max_bytes = sizeof(local_buffer) - 1;
1491 if (nbytes >= max_bytes)
1492 return -E2BIG;
1493 /* Allocate a dynamic buffer if we need one */
1494 if (nbytes >= sizeof(local_buffer)) {
1495 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1496 if (buffer == NULL)
1497 return -ENOMEM;
1498 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001499 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1500 retval = -EFAULT;
1501 goto out;
1502 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001503
1504 buffer[nbytes] = 0; /* nul-terminate */
1505 strstrip(buffer);
1506 retval = cft->write_string(cgrp, cft, buffer);
1507 if (!retval)
1508 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001509out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001510 if (buffer != local_buffer)
1511 kfree(buffer);
1512 return retval;
1513}
1514
Paul Menageddbcc7e2007-10-18 23:39:30 -07001515static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1516 size_t nbytes, loff_t *ppos)
1517{
1518 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001519 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520
Li Zefan75139b82009-01-07 18:07:33 -08001521 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001522 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001523 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001524 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001525 if (cft->write_u64 || cft->write_s64)
1526 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001527 if (cft->write_string)
1528 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001529 if (cft->trigger) {
1530 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1531 return ret ? ret : nbytes;
1532 }
Paul Menage355e0c42007-10-18 23:39:33 -07001533 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001534}
1535
Paul Menagef4c753b2008-04-29 00:59:56 -07001536static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1537 struct file *file,
1538 char __user *buf, size_t nbytes,
1539 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001540{
Paul Menage84eea842008-07-25 01:47:00 -07001541 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001542 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1544
1545 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1546}
1547
Paul Menagee73d2c62008-04-29 01:00:06 -07001548static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1549 struct file *file,
1550 char __user *buf, size_t nbytes,
1551 loff_t *ppos)
1552{
Paul Menage84eea842008-07-25 01:47:00 -07001553 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001554 s64 val = cft->read_s64(cgrp, cft);
1555 int len = sprintf(tmp, "%lld\n", (long long) val);
1556
1557 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1558}
1559
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1561 size_t nbytes, loff_t *ppos)
1562{
1563 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001564 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565
Li Zefan75139b82009-01-07 18:07:33 -08001566 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567 return -ENODEV;
1568
1569 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001570 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001571 if (cft->read_u64)
1572 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001573 if (cft->read_s64)
1574 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575 return -EINVAL;
1576}
1577
Paul Menage91796562008-04-29 01:00:01 -07001578/*
1579 * seqfile ops/methods for returning structured data. Currently just
1580 * supports string->u64 maps, but can be extended in future.
1581 */
1582
1583struct cgroup_seqfile_state {
1584 struct cftype *cft;
1585 struct cgroup *cgroup;
1586};
1587
1588static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1589{
1590 struct seq_file *sf = cb->state;
1591 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1592}
1593
1594static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1595{
1596 struct cgroup_seqfile_state *state = m->private;
1597 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001598 if (cft->read_map) {
1599 struct cgroup_map_cb cb = {
1600 .fill = cgroup_map_add,
1601 .state = m,
1602 };
1603 return cft->read_map(state->cgroup, cft, &cb);
1604 }
1605 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001606}
1607
Adrian Bunk96930a62008-07-25 19:46:21 -07001608static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07001609{
1610 struct seq_file *seq = file->private_data;
1611 kfree(seq->private);
1612 return single_release(inode, file);
1613}
1614
1615static struct file_operations cgroup_seqfile_operations = {
1616 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07001617 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07001618 .llseek = seq_lseek,
1619 .release = cgroup_seqfile_release,
1620};
1621
Paul Menageddbcc7e2007-10-18 23:39:30 -07001622static int cgroup_file_open(struct inode *inode, struct file *file)
1623{
1624 int err;
1625 struct cftype *cft;
1626
1627 err = generic_file_open(inode, file);
1628 if (err)
1629 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08001631
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001632 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001633 struct cgroup_seqfile_state *state =
1634 kzalloc(sizeof(*state), GFP_USER);
1635 if (!state)
1636 return -ENOMEM;
1637 state->cft = cft;
1638 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1639 file->f_op = &cgroup_seqfile_operations;
1640 err = single_open(file, cgroup_seqfile_show, state);
1641 if (err < 0)
1642 kfree(state);
1643 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644 err = cft->open(inode, file);
1645 else
1646 err = 0;
1647
1648 return err;
1649}
1650
1651static int cgroup_file_release(struct inode *inode, struct file *file)
1652{
1653 struct cftype *cft = __d_cft(file->f_dentry);
1654 if (cft->release)
1655 return cft->release(inode, file);
1656 return 0;
1657}
1658
1659/*
1660 * cgroup_rename - Only allow simple rename of directories in place.
1661 */
1662static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1663 struct inode *new_dir, struct dentry *new_dentry)
1664{
1665 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1666 return -ENOTDIR;
1667 if (new_dentry->d_inode)
1668 return -EEXIST;
1669 if (old_dir != new_dir)
1670 return -EIO;
1671 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1672}
1673
1674static struct file_operations cgroup_file_operations = {
1675 .read = cgroup_file_read,
1676 .write = cgroup_file_write,
1677 .llseek = generic_file_llseek,
1678 .open = cgroup_file_open,
1679 .release = cgroup_file_release,
1680};
1681
1682static struct inode_operations cgroup_dir_inode_operations = {
1683 .lookup = simple_lookup,
1684 .mkdir = cgroup_mkdir,
1685 .rmdir = cgroup_rmdir,
1686 .rename = cgroup_rename,
1687};
1688
Li Zefan099fca32009-04-02 16:57:29 -07001689static int cgroup_create_file(struct dentry *dentry, mode_t mode,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001690 struct super_block *sb)
1691{
Al Viro3ba13d12009-02-20 06:02:22 +00001692 static const struct dentry_operations cgroup_dops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693 .d_iput = cgroup_diput,
1694 };
1695
1696 struct inode *inode;
1697
1698 if (!dentry)
1699 return -ENOENT;
1700 if (dentry->d_inode)
1701 return -EEXIST;
1702
1703 inode = cgroup_new_inode(mode, sb);
1704 if (!inode)
1705 return -ENOMEM;
1706
1707 if (S_ISDIR(mode)) {
1708 inode->i_op = &cgroup_dir_inode_operations;
1709 inode->i_fop = &simple_dir_operations;
1710
1711 /* start off with i_nlink == 2 (for "." entry) */
1712 inc_nlink(inode);
1713
1714 /* start with the directory inode held, so that we can
1715 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07001716 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001717 } else if (S_ISREG(mode)) {
1718 inode->i_size = 0;
1719 inode->i_fop = &cgroup_file_operations;
1720 }
1721 dentry->d_op = &cgroup_dops;
1722 d_instantiate(dentry, inode);
1723 dget(dentry); /* Extra count - pin the dentry in core */
1724 return 0;
1725}
1726
1727/*
Li Zefana043e3b2008-02-23 15:24:09 -08001728 * cgroup_create_dir - create a directory for an object.
1729 * @cgrp: the cgroup we create the directory for. It must have a valid
1730 * ->parent field. And we are going to fill its ->dentry field.
1731 * @dentry: dentry of the new cgroup
1732 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001734static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07001735 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736{
1737 struct dentry *parent;
1738 int error = 0;
1739
Paul Menagebd89aab2007-10-18 23:40:44 -07001740 parent = cgrp->parent->dentry;
1741 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001742 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001743 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08001745 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001746 dget(dentry);
1747 }
1748 dput(dentry);
1749
1750 return error;
1751}
1752
Li Zefan099fca32009-04-02 16:57:29 -07001753/**
1754 * cgroup_file_mode - deduce file mode of a control file
1755 * @cft: the control file in question
1756 *
1757 * returns cft->mode if ->mode is not 0
1758 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1759 * returns S_IRUGO if it has only a read handler
1760 * returns S_IWUSR if it has only a write hander
1761 */
1762static mode_t cgroup_file_mode(const struct cftype *cft)
1763{
1764 mode_t mode = 0;
1765
1766 if (cft->mode)
1767 return cft->mode;
1768
1769 if (cft->read || cft->read_u64 || cft->read_s64 ||
1770 cft->read_map || cft->read_seq_string)
1771 mode |= S_IRUGO;
1772
1773 if (cft->write || cft->write_u64 || cft->write_s64 ||
1774 cft->write_string || cft->trigger)
1775 mode |= S_IWUSR;
1776
1777 return mode;
1778}
1779
Paul Menagebd89aab2007-10-18 23:40:44 -07001780int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781 struct cgroup_subsys *subsys,
1782 const struct cftype *cft)
1783{
Paul Menagebd89aab2007-10-18 23:40:44 -07001784 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001785 struct dentry *dentry;
1786 int error;
Li Zefan099fca32009-04-02 16:57:29 -07001787 mode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001788
1789 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07001790 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791 strcpy(name, subsys->name);
1792 strcat(name, ".");
1793 }
1794 strcat(name, cft->name);
1795 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1796 dentry = lookup_one_len(name, dir, strlen(name));
1797 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07001798 mode = cgroup_file_mode(cft);
1799 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07001800 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001801 if (!error)
1802 dentry->d_fsdata = (void *)cft;
1803 dput(dentry);
1804 } else
1805 error = PTR_ERR(dentry);
1806 return error;
1807}
1808
Paul Menagebd89aab2007-10-18 23:40:44 -07001809int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810 struct cgroup_subsys *subsys,
1811 const struct cftype cft[],
1812 int count)
1813{
1814 int i, err;
1815 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001816 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001817 if (err)
1818 return err;
1819 }
1820 return 0;
1821}
1822
Li Zefana043e3b2008-02-23 15:24:09 -08001823/**
1824 * cgroup_task_count - count the number of tasks in a cgroup.
1825 * @cgrp: the cgroup in question
1826 *
1827 * Return the number of tasks in the cgroup.
1828 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001829int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001830{
1831 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001832 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001833
Paul Menage817929e2007-10-18 23:39:36 -07001834 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001835 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07001836 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07001837 }
1838 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001839 return count;
1840}
1841
1842/*
Paul Menage817929e2007-10-18 23:39:36 -07001843 * Advance a list_head iterator. The iterator should be positioned at
1844 * the start of a css_set
1845 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001846static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001847 struct cgroup_iter *it)
1848{
1849 struct list_head *l = it->cg_link;
1850 struct cg_cgroup_link *link;
1851 struct css_set *cg;
1852
1853 /* Advance to the next non-empty css_set */
1854 do {
1855 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07001856 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001857 it->cg_link = NULL;
1858 return;
1859 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001860 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001861 cg = link->cg;
1862 } while (list_empty(&cg->tasks));
1863 it->cg_link = l;
1864 it->task = cg->tasks.next;
1865}
1866
Cliff Wickman31a7df02008-02-07 00:14:42 -08001867/*
1868 * To reduce the fork() overhead for systems that are not actually
1869 * using their cgroups capability, we don't maintain the lists running
1870 * through each css_set to its tasks until we see the list actually
1871 * used - in other words after the first call to cgroup_iter_start().
1872 *
1873 * The tasklist_lock is not held here, as do_each_thread() and
1874 * while_each_thread() are protected by RCU.
1875 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07001876static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08001877{
1878 struct task_struct *p, *g;
1879 write_lock(&css_set_lock);
1880 use_task_css_set_links = 1;
1881 do_each_thread(g, p) {
1882 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08001883 /*
1884 * We should check if the process is exiting, otherwise
1885 * it will race with cgroup_exit() in that the list
1886 * entry won't be deleted though the process has exited.
1887 */
1888 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08001889 list_add(&p->cg_list, &p->cgroups->tasks);
1890 task_unlock(p);
1891 } while_each_thread(g, p);
1892 write_unlock(&css_set_lock);
1893}
1894
Paul Menagebd89aab2007-10-18 23:40:44 -07001895void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001896{
1897 /*
1898 * The first time anyone tries to iterate across a cgroup,
1899 * we need to enable the list linking each css_set to its
1900 * tasks, and fix up all existing tasks.
1901 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08001902 if (!use_task_css_set_links)
1903 cgroup_enable_task_cg_lists();
1904
Paul Menage817929e2007-10-18 23:39:36 -07001905 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001906 it->cg_link = &cgrp->css_sets;
1907 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001908}
1909
Paul Menagebd89aab2007-10-18 23:40:44 -07001910struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001911 struct cgroup_iter *it)
1912{
1913 struct task_struct *res;
1914 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001915 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07001916
1917 /* If the iterator cg is NULL, we have no tasks */
1918 if (!it->cg_link)
1919 return NULL;
1920 res = list_entry(l, struct task_struct, cg_list);
1921 /* Advance iterator to find next entry */
1922 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001923 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
1924 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07001925 /* We reached the end of this task list - move on to
1926 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07001927 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001928 } else {
1929 it->task = l;
1930 }
1931 return res;
1932}
1933
Paul Menagebd89aab2007-10-18 23:40:44 -07001934void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001935{
1936 read_unlock(&css_set_lock);
1937}
1938
Cliff Wickman31a7df02008-02-07 00:14:42 -08001939static inline int started_after_time(struct task_struct *t1,
1940 struct timespec *time,
1941 struct task_struct *t2)
1942{
1943 int start_diff = timespec_compare(&t1->start_time, time);
1944 if (start_diff > 0) {
1945 return 1;
1946 } else if (start_diff < 0) {
1947 return 0;
1948 } else {
1949 /*
1950 * Arbitrarily, if two processes started at the same
1951 * time, we'll say that the lower pointer value
1952 * started first. Note that t2 may have exited by now
1953 * so this may not be a valid pointer any longer, but
1954 * that's fine - it still serves to distinguish
1955 * between two tasks started (effectively) simultaneously.
1956 */
1957 return t1 > t2;
1958 }
1959}
1960
1961/*
1962 * This function is a callback from heap_insert() and is used to order
1963 * the heap.
1964 * In this case we order the heap in descending task start time.
1965 */
1966static inline int started_after(void *p1, void *p2)
1967{
1968 struct task_struct *t1 = p1;
1969 struct task_struct *t2 = p2;
1970 return started_after_time(t1, &t2->start_time, t2);
1971}
1972
1973/**
1974 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1975 * @scan: struct cgroup_scanner containing arguments for the scan
1976 *
1977 * Arguments include pointers to callback functions test_task() and
1978 * process_task().
1979 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1980 * and if it returns true, call process_task() for it also.
1981 * The test_task pointer may be NULL, meaning always true (select all tasks).
1982 * Effectively duplicates cgroup_iter_{start,next,end}()
1983 * but does not lock css_set_lock for the call to process_task().
1984 * The struct cgroup_scanner may be embedded in any structure of the caller's
1985 * creation.
1986 * It is guaranteed that process_task() will act on every task that
1987 * is a member of the cgroup for the duration of this call. This
1988 * function may or may not call process_task() for tasks that exit
1989 * or move to a different cgroup during the call, or are forked or
1990 * move into the cgroup during the call.
1991 *
1992 * Note that test_task() may be called with locks held, and may in some
1993 * situations be called multiple times for the same task, so it should
1994 * be cheap.
1995 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1996 * pre-allocated and will be used for heap operations (and its "gt" member will
1997 * be overwritten), else a temporary heap will be used (allocation of which
1998 * may cause this function to fail).
1999 */
2000int cgroup_scan_tasks(struct cgroup_scanner *scan)
2001{
2002 int retval, i;
2003 struct cgroup_iter it;
2004 struct task_struct *p, *dropped;
2005 /* Never dereference latest_task, since it's not refcounted */
2006 struct task_struct *latest_task = NULL;
2007 struct ptr_heap tmp_heap;
2008 struct ptr_heap *heap;
2009 struct timespec latest_time = { 0, 0 };
2010
2011 if (scan->heap) {
2012 /* The caller supplied our heap and pre-allocated its memory */
2013 heap = scan->heap;
2014 heap->gt = &started_after;
2015 } else {
2016 /* We need to allocate our own heap memory */
2017 heap = &tmp_heap;
2018 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2019 if (retval)
2020 /* cannot allocate the heap */
2021 return retval;
2022 }
2023
2024 again:
2025 /*
2026 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2027 * to determine which are of interest, and using the scanner's
2028 * "process_task" callback to process any of them that need an update.
2029 * Since we don't want to hold any locks during the task updates,
2030 * gather tasks to be processed in a heap structure.
2031 * The heap is sorted by descending task start time.
2032 * If the statically-sized heap fills up, we overflow tasks that
2033 * started later, and in future iterations only consider tasks that
2034 * started after the latest task in the previous pass. This
2035 * guarantees forward progress and that we don't miss any tasks.
2036 */
2037 heap->size = 0;
2038 cgroup_iter_start(scan->cg, &it);
2039 while ((p = cgroup_iter_next(scan->cg, &it))) {
2040 /*
2041 * Only affect tasks that qualify per the caller's callback,
2042 * if he provided one
2043 */
2044 if (scan->test_task && !scan->test_task(p, scan))
2045 continue;
2046 /*
2047 * Only process tasks that started after the last task
2048 * we processed
2049 */
2050 if (!started_after_time(p, &latest_time, latest_task))
2051 continue;
2052 dropped = heap_insert(heap, p);
2053 if (dropped == NULL) {
2054 /*
2055 * The new task was inserted; the heap wasn't
2056 * previously full
2057 */
2058 get_task_struct(p);
2059 } else if (dropped != p) {
2060 /*
2061 * The new task was inserted, and pushed out a
2062 * different task
2063 */
2064 get_task_struct(p);
2065 put_task_struct(dropped);
2066 }
2067 /*
2068 * Else the new task was newer than anything already in
2069 * the heap and wasn't inserted
2070 */
2071 }
2072 cgroup_iter_end(scan->cg, &it);
2073
2074 if (heap->size) {
2075 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002076 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002077 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002078 latest_time = q->start_time;
2079 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002080 }
2081 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002082 scan->process_task(q, scan);
2083 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002084 }
2085 /*
2086 * If we had to process any tasks at all, scan again
2087 * in case some of them were in the middle of forking
2088 * children that didn't get processed.
2089 * Not the most efficient way to do it, but it avoids
2090 * having to take callback_mutex in the fork path
2091 */
2092 goto again;
2093 }
2094 if (heap == &tmp_heap)
2095 heap_free(&tmp_heap);
2096 return 0;
2097}
2098
Paul Menage817929e2007-10-18 23:39:36 -07002099/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07002100 * Stuff for reading the 'tasks' file.
2101 *
2102 * Reading this file can return large amounts of data if a cgroup has
2103 * *lots* of attached tasks. So it may need several calls to read(),
2104 * but we cannot guarantee that the information we produce is correct
2105 * unless we produce it entirely atomically.
2106 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002107 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002108
2109/*
2110 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
Paul Menagebd89aab2007-10-18 23:40:44 -07002111 * 'cgrp'. Return actual number of pids loaded. No need to
Paul Menagebbcb81d2007-10-18 23:39:32 -07002112 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2113 * read section, so the css_set can't go away, and is
2114 * immutable after creation.
2115 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002116static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002117{
Gowrishankar Me7b80bb2009-01-07 18:07:43 -08002118 int n = 0, pid;
Paul Menage817929e2007-10-18 23:39:36 -07002119 struct cgroup_iter it;
2120 struct task_struct *tsk;
Paul Menagebd89aab2007-10-18 23:40:44 -07002121 cgroup_iter_start(cgrp, &it);
2122 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Paul Menage817929e2007-10-18 23:39:36 -07002123 if (unlikely(n == npids))
2124 break;
Gowrishankar Me7b80bb2009-01-07 18:07:43 -08002125 pid = task_pid_vnr(tsk);
2126 if (pid > 0)
2127 pidarray[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07002128 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002129 cgroup_iter_end(cgrp, &it);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002130 return n;
2131}
2132
Balbir Singh846c7bb2007-10-18 23:39:44 -07002133/**
Li Zefana043e3b2008-02-23 15:24:09 -08002134 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002135 * @stats: cgroupstats to fill information into
2136 * @dentry: A dentry entry belonging to the cgroup for which stats have
2137 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002138 *
2139 * Build and fill cgroupstats so that taskstats can export it to user
2140 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002141 */
2142int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2143{
2144 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002145 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002146 struct cgroup_iter it;
2147 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002148
Balbir Singh846c7bb2007-10-18 23:39:44 -07002149 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002150 * Validate dentry by checking the superblock operations,
2151 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002152 */
Li Zefan33d283b2008-11-19 15:36:48 -08002153 if (dentry->d_sb->s_op != &cgroup_ops ||
2154 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002155 goto err;
2156
2157 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002158 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002159
Paul Menagebd89aab2007-10-18 23:40:44 -07002160 cgroup_iter_start(cgrp, &it);
2161 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002162 switch (tsk->state) {
2163 case TASK_RUNNING:
2164 stats->nr_running++;
2165 break;
2166 case TASK_INTERRUPTIBLE:
2167 stats->nr_sleeping++;
2168 break;
2169 case TASK_UNINTERRUPTIBLE:
2170 stats->nr_uninterruptible++;
2171 break;
2172 case TASK_STOPPED:
2173 stats->nr_stopped++;
2174 break;
2175 default:
2176 if (delayacct_is_task_waiting_on_io(tsk))
2177 stats->nr_io_wait++;
2178 break;
2179 }
2180 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002181 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002182
Balbir Singh846c7bb2007-10-18 23:39:44 -07002183err:
2184 return ret;
2185}
2186
Paul Menagebbcb81d2007-10-18 23:39:32 -07002187static int cmppid(const void *a, const void *b)
2188{
2189 return *(pid_t *)a - *(pid_t *)b;
2190}
2191
Paul Menagebbcb81d2007-10-18 23:39:32 -07002192
Paul Menagecc31edc2008-10-18 20:28:04 -07002193/*
2194 * seq_file methods for the "tasks" file. The seq_file position is the
2195 * next pid to display; the seq_file iterator is a pointer to the pid
2196 * in the cgroup->tasks_pids array.
2197 */
2198
2199static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
2200{
2201 /*
2202 * Initially we receive a position value that corresponds to
2203 * one more than the last pid shown (or 0 on the first call or
2204 * after a seek to the start). Use a binary-search to find the
2205 * next pid to display, if any
2206 */
2207 struct cgroup *cgrp = s->private;
2208 int index = 0, pid = *pos;
2209 int *iter;
2210
2211 down_read(&cgrp->pids_mutex);
2212 if (pid) {
2213 int end = cgrp->pids_length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002214
Paul Menagecc31edc2008-10-18 20:28:04 -07002215 while (index < end) {
2216 int mid = (index + end) / 2;
2217 if (cgrp->tasks_pids[mid] == pid) {
2218 index = mid;
2219 break;
2220 } else if (cgrp->tasks_pids[mid] <= pid)
2221 index = mid + 1;
2222 else
2223 end = mid;
2224 }
2225 }
2226 /* If we're off the end of the array, we're done */
2227 if (index >= cgrp->pids_length)
2228 return NULL;
2229 /* Update the abstract position to be the actual pid that we found */
2230 iter = cgrp->tasks_pids + index;
2231 *pos = *iter;
2232 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002233}
2234
Paul Menagecc31edc2008-10-18 20:28:04 -07002235static void cgroup_tasks_stop(struct seq_file *s, void *v)
2236{
2237 struct cgroup *cgrp = s->private;
2238 up_read(&cgrp->pids_mutex);
2239}
2240
2241static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
2242{
2243 struct cgroup *cgrp = s->private;
2244 int *p = v;
2245 int *end = cgrp->tasks_pids + cgrp->pids_length;
2246
2247 /*
2248 * Advance to the next pid in the array. If this goes off the
2249 * end, we're done
2250 */
2251 p++;
2252 if (p >= end) {
2253 return NULL;
2254 } else {
2255 *pos = *p;
2256 return p;
2257 }
2258}
2259
2260static int cgroup_tasks_show(struct seq_file *s, void *v)
2261{
2262 return seq_printf(s, "%d\n", *(int *)v);
2263}
2264
2265static struct seq_operations cgroup_tasks_seq_operations = {
2266 .start = cgroup_tasks_start,
2267 .stop = cgroup_tasks_stop,
2268 .next = cgroup_tasks_next,
2269 .show = cgroup_tasks_show,
2270};
2271
2272static void release_cgroup_pid_array(struct cgroup *cgrp)
2273{
2274 down_write(&cgrp->pids_mutex);
2275 BUG_ON(!cgrp->pids_use_count);
2276 if (!--cgrp->pids_use_count) {
2277 kfree(cgrp->tasks_pids);
2278 cgrp->tasks_pids = NULL;
2279 cgrp->pids_length = 0;
2280 }
2281 up_write(&cgrp->pids_mutex);
2282}
2283
2284static int cgroup_tasks_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002285{
Paul Menagebd89aab2007-10-18 23:40:44 -07002286 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002287
2288 if (!(file->f_mode & FMODE_READ))
2289 return 0;
2290
Paul Menagecc31edc2008-10-18 20:28:04 -07002291 release_cgroup_pid_array(cgrp);
2292 return seq_release(inode, file);
2293}
2294
2295static struct file_operations cgroup_tasks_operations = {
2296 .read = seq_read,
2297 .llseek = seq_lseek,
2298 .write = cgroup_file_write,
2299 .release = cgroup_tasks_release,
2300};
2301
2302/*
2303 * Handle an open on 'tasks' file. Prepare an array containing the
2304 * process id's of tasks currently attached to the cgroup being opened.
2305 */
2306
2307static int cgroup_tasks_open(struct inode *unused, struct file *file)
2308{
2309 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2310 pid_t *pidarray;
2311 int npids;
2312 int retval;
2313
2314 /* Nothing to do for write-only files */
2315 if (!(file->f_mode & FMODE_READ))
2316 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002317
2318 /*
2319 * If cgroup gets more users after we read count, we won't have
2320 * enough space - tough. This race is indistinguishable to the
2321 * caller from the case that the additional cgroup users didn't
2322 * show up until sometime later on.
2323 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002324 npids = cgroup_task_count(cgrp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002325 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2326 if (!pidarray)
2327 return -ENOMEM;
2328 npids = pid_array_load(pidarray, npids, cgrp);
2329 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002330
Paul Menagecc31edc2008-10-18 20:28:04 -07002331 /*
2332 * Store the array in the cgroup, freeing the old
2333 * array if necessary
2334 */
2335 down_write(&cgrp->pids_mutex);
2336 kfree(cgrp->tasks_pids);
2337 cgrp->tasks_pids = pidarray;
2338 cgrp->pids_length = npids;
2339 cgrp->pids_use_count++;
2340 up_write(&cgrp->pids_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002341
Paul Menagecc31edc2008-10-18 20:28:04 -07002342 file->f_op = &cgroup_tasks_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002343
Paul Menagecc31edc2008-10-18 20:28:04 -07002344 retval = seq_open(file, &cgroup_tasks_seq_operations);
2345 if (retval) {
2346 release_cgroup_pid_array(cgrp);
2347 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002348 }
Paul Menagecc31edc2008-10-18 20:28:04 -07002349 ((struct seq_file *)file->private_data)->private = cgrp;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002350 return 0;
2351}
2352
Paul Menagebd89aab2007-10-18 23:40:44 -07002353static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002354 struct cftype *cft)
2355{
Paul Menagebd89aab2007-10-18 23:40:44 -07002356 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002357}
2358
Paul Menage6379c102008-07-25 01:47:01 -07002359static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2360 struct cftype *cft,
2361 u64 val)
2362{
2363 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2364 if (val)
2365 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2366 else
2367 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2368 return 0;
2369}
2370
Paul Menagebbcb81d2007-10-18 23:39:32 -07002371/*
2372 * for the common functions, 'private' gives the type of file
2373 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07002374static struct cftype files[] = {
2375 {
2376 .name = "tasks",
2377 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07002378 .write_u64 = cgroup_tasks_write,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002379 .release = cgroup_tasks_release,
2380 .private = FILE_TASKLIST,
Li Zefan099fca32009-04-02 16:57:29 -07002381 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002382 },
2383
2384 {
2385 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002386 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07002387 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002388 .private = FILE_NOTIFY_ON_RELEASE,
2389 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002390};
2391
2392static struct cftype cft_release_agent = {
2393 .name = "release_agent",
Paul Menagee788e062008-07-25 01:46:59 -07002394 .read_seq_string = cgroup_release_agent_show,
2395 .write_string = cgroup_release_agent_write,
2396 .max_write_len = PATH_MAX,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002397 .private = FILE_RELEASE_AGENT,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002398};
2399
Paul Menagebd89aab2007-10-18 23:40:44 -07002400static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002401{
2402 int err;
2403 struct cgroup_subsys *ss;
2404
2405 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002406 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002407
Paul Menagebd89aab2007-10-18 23:40:44 -07002408 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002409 if (err < 0)
2410 return err;
2411
Paul Menagebd89aab2007-10-18 23:40:44 -07002412 if (cgrp == cgrp->top_cgroup) {
2413 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002414 return err;
2415 }
2416
Paul Menagebd89aab2007-10-18 23:40:44 -07002417 for_each_subsys(cgrp->root, ss) {
2418 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002419 return err;
2420 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002421 /* This cgroup is ready now */
2422 for_each_subsys(cgrp->root, ss) {
2423 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2424 /*
2425 * Update id->css pointer and make this css visible from
2426 * CSS ID functions. This pointer will be dereferened
2427 * from RCU-read-side without locks.
2428 */
2429 if (css->id)
2430 rcu_assign_pointer(css->id->css, css);
2431 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432
2433 return 0;
2434}
2435
2436static void init_cgroup_css(struct cgroup_subsys_state *css,
2437 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002438 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002439{
Paul Menagebd89aab2007-10-18 23:40:44 -07002440 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08002441 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002442 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002443 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002444 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002445 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002446 BUG_ON(cgrp->subsys[ss->subsys_id]);
2447 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002448}
2449
Paul Menage999cd8a2009-01-07 18:08:36 -08002450static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2451{
2452 /* We need to take each hierarchy_mutex in a consistent order */
2453 int i;
2454
2455 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2456 struct cgroup_subsys *ss = subsys[i];
2457 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08002458 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08002459 }
2460}
2461
2462static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2463{
2464 int i;
2465
2466 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2467 struct cgroup_subsys *ss = subsys[i];
2468 if (ss->root == root)
2469 mutex_unlock(&ss->hierarchy_mutex);
2470 }
2471}
2472
Paul Menageddbcc7e2007-10-18 23:39:30 -07002473/*
Li Zefana043e3b2008-02-23 15:24:09 -08002474 * cgroup_create - create a cgroup
2475 * @parent: cgroup that will be parent of the new cgroup
2476 * @dentry: dentry of the new cgroup
2477 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002478 *
Li Zefana043e3b2008-02-23 15:24:09 -08002479 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002480 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002481static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002482 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002483{
Paul Menagebd89aab2007-10-18 23:40:44 -07002484 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002485 struct cgroupfs_root *root = parent->root;
2486 int err = 0;
2487 struct cgroup_subsys *ss;
2488 struct super_block *sb = root->sb;
2489
Paul Menagebd89aab2007-10-18 23:40:44 -07002490 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2491 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002492 return -ENOMEM;
2493
2494 /* Grab a reference on the superblock so the hierarchy doesn't
2495 * get deleted on unmount if there are child cgroups. This
2496 * can be done outside cgroup_mutex, since the sb can't
2497 * disappear while someone has an open control file on the
2498 * fs */
2499 atomic_inc(&sb->s_active);
2500
2501 mutex_lock(&cgroup_mutex);
2502
Paul Menagecc31edc2008-10-18 20:28:04 -07002503 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002504
Paul Menagebd89aab2007-10-18 23:40:44 -07002505 cgrp->parent = parent;
2506 cgrp->root = parent->root;
2507 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002508
Li Zefanb6abdb02008-03-04 14:28:19 -08002509 if (notify_on_release(parent))
2510 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2511
Paul Menageddbcc7e2007-10-18 23:39:30 -07002512 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002513 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002514 if (IS_ERR(css)) {
2515 err = PTR_ERR(css);
2516 goto err_destroy;
2517 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002518 init_cgroup_css(css, ss, cgrp);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002519 if (ss->use_id)
2520 if (alloc_css_id(ss, parent, cgrp))
2521 goto err_destroy;
2522 /* At error, ->destroy() callback has to free assigned ID. */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002523 }
2524
Paul Menage999cd8a2009-01-07 18:08:36 -08002525 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002526 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08002527 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002528 root->number_of_cgroups++;
2529
Paul Menagebd89aab2007-10-18 23:40:44 -07002530 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002531 if (err < 0)
2532 goto err_remove;
2533
2534 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07002535 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002536
Paul Menagebd89aab2007-10-18 23:40:44 -07002537 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002538 /* If err < 0, we have a half-filled directory - oh well ;) */
2539
2540 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002541 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002542
2543 return 0;
2544
2545 err_remove:
2546
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002547 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002548 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002549 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002550 root->number_of_cgroups--;
2551
2552 err_destroy:
2553
2554 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002555 if (cgrp->subsys[ss->subsys_id])
2556 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002557 }
2558
2559 mutex_unlock(&cgroup_mutex);
2560
2561 /* Release the reference count that we took on the superblock */
2562 deactivate_super(sb);
2563
Paul Menagebd89aab2007-10-18 23:40:44 -07002564 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002565 return err;
2566}
2567
2568static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2569{
2570 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2571
2572 /* the vfs holds inode->i_mutex already */
2573 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2574}
2575
Li Zefan55b6fd02008-07-29 22:33:20 -07002576static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002577{
2578 /* Check the reference count on each subsystem. Since we
2579 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08002580 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07002581 * be no outstanding references, so the subsystem is safe to
2582 * destroy. We scan across all subsystems rather than using
2583 * the per-hierarchy linked list of mounted subsystems since
2584 * we can be called via check_for_release() with no
2585 * synchronization other than RCU, and the subsystem linked
2586 * list isn't RCU-safe */
2587 int i;
2588 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2589 struct cgroup_subsys *ss = subsys[i];
2590 struct cgroup_subsys_state *css;
2591 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07002592 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002593 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07002594 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07002595 /* When called from check_for_release() it's possible
2596 * that by this point the cgroup has been removed
2597 * and the css deleted. But a false-positive doesn't
2598 * matter, since it can only happen if the cgroup
2599 * has been deleted and hence no longer needs the
2600 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08002601 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07002602 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07002603 }
2604 return 0;
2605}
2606
Paul Menagee7c5ec92009-01-07 18:08:38 -08002607/*
2608 * Atomically mark all (or else none) of the cgroup's CSS objects as
2609 * CSS_REMOVED. Return true on success, or false if the cgroup has
2610 * busy subsystems. Call with cgroup_mutex held
2611 */
2612
2613static int cgroup_clear_css_refs(struct cgroup *cgrp)
2614{
2615 struct cgroup_subsys *ss;
2616 unsigned long flags;
2617 bool failed = false;
2618 local_irq_save(flags);
2619 for_each_subsys(cgrp->root, ss) {
2620 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2621 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08002622 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08002623 /* We can only remove a CSS with a refcnt==1 */
2624 refcnt = atomic_read(&css->refcnt);
2625 if (refcnt > 1) {
2626 failed = true;
2627 goto done;
2628 }
2629 BUG_ON(!refcnt);
2630 /*
2631 * Drop the refcnt to 0 while we check other
2632 * subsystems. This will cause any racing
2633 * css_tryget() to spin until we set the
2634 * CSS_REMOVED bits or abort
2635 */
Paul Menage804b3c22009-01-29 14:25:21 -08002636 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
2637 break;
2638 cpu_relax();
2639 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08002640 }
2641 done:
2642 for_each_subsys(cgrp->root, ss) {
2643 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2644 if (failed) {
2645 /*
2646 * Restore old refcnt if we previously managed
2647 * to clear it from 1 to 0
2648 */
2649 if (!atomic_read(&css->refcnt))
2650 atomic_set(&css->refcnt, 1);
2651 } else {
2652 /* Commit the fact that the CSS is removed */
2653 set_bit(CSS_REMOVED, &css->flags);
2654 }
2655 }
2656 local_irq_restore(flags);
2657 return !failed;
2658}
2659
Paul Menageddbcc7e2007-10-18 23:39:30 -07002660static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2661{
Paul Menagebd89aab2007-10-18 23:40:44 -07002662 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002663 struct dentry *d;
2664 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002665 DEFINE_WAIT(wait);
2666 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002667
2668 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002669again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07002670 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002671 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002672 mutex_unlock(&cgroup_mutex);
2673 return -EBUSY;
2674 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002675 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002676 mutex_unlock(&cgroup_mutex);
2677 return -EBUSY;
2678 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002679 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08002680
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002681 /*
Li Zefana043e3b2008-02-23 15:24:09 -08002682 * Call pre_destroy handlers of subsys. Notify subsystems
2683 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002684 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002685 ret = cgroup_call_pre_destroy(cgrp);
2686 if (ret)
2687 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002688
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002689 mutex_lock(&cgroup_mutex);
2690 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002691 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002692 mutex_unlock(&cgroup_mutex);
2693 return -EBUSY;
2694 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002695 /*
2696 * css_put/get is provided for subsys to grab refcnt to css. In typical
2697 * case, subsystem has no reference after pre_destroy(). But, under
2698 * hierarchy management, some *temporal* refcnt can be hold.
2699 * To avoid returning -EBUSY to a user, waitqueue is used. If subsys
2700 * is really busy, it should return -EBUSY at pre_destroy(). wake_up
2701 * is called when css_put() is called and refcnt goes down to 0.
2702 */
2703 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2704 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
2705
2706 if (!cgroup_clear_css_refs(cgrp)) {
2707 mutex_unlock(&cgroup_mutex);
2708 schedule();
2709 finish_wait(&cgroup_rmdir_waitq, &wait);
2710 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2711 if (signal_pending(current))
2712 return -EINTR;
2713 goto again;
2714 }
2715 /* NO css_tryget() can success after here. */
2716 finish_wait(&cgroup_rmdir_waitq, &wait);
2717 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002718
Paul Menage81a6a5c2007-10-18 23:39:38 -07002719 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002720 set_bit(CGRP_REMOVED, &cgrp->flags);
2721 if (!list_empty(&cgrp->release_list))
2722 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002723 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08002724
2725 cgroup_lock_hierarchy(cgrp->root);
2726 /* delete this cgroup from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07002727 list_del(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08002728 cgroup_unlock_hierarchy(cgrp->root);
2729
Paul Menagebd89aab2007-10-18 23:40:44 -07002730 spin_lock(&cgrp->dentry->d_lock);
2731 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002732 spin_unlock(&d->d_lock);
2733
2734 cgroup_d_remove_dir(d);
2735 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002736
Paul Menagebd89aab2007-10-18 23:40:44 -07002737 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002738 check_for_release(parent);
2739
Paul Menageddbcc7e2007-10-18 23:39:30 -07002740 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002741 return 0;
2742}
2743
Li Zefan06a11922008-04-29 01:00:07 -07002744static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002745{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002746 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08002747
2748 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002749
2750 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08002751 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002752 ss->root = &rootnode;
2753 css = ss->create(ss, dummytop);
2754 /* We don't handle early failures gracefully */
2755 BUG_ON(IS_ERR(css));
2756 init_cgroup_css(css, ss, dummytop);
2757
Li Zefane8d55fd2008-04-29 01:00:13 -07002758 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07002759 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07002760 * newly registered, all tasks and hence the
2761 * init_css_set is in the subsystem's top cgroup. */
2762 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07002763
2764 need_forkexit_callback |= ss->fork || ss->exit;
2765
Li Zefane8d55fd2008-04-29 01:00:13 -07002766 /* At system boot, before all subsystems have been
2767 * registered, no tasks have been forked, so we don't
2768 * need to invoke fork callbacks here. */
2769 BUG_ON(!list_empty(&init_task.tasks));
2770
Paul Menage999cd8a2009-01-07 18:08:36 -08002771 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08002772 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002773 ss->active = 1;
2774}
2775
2776/**
Li Zefana043e3b2008-02-23 15:24:09 -08002777 * cgroup_init_early - cgroup initialization at system boot
2778 *
2779 * Initialize cgroups at system boot, and initialize any
2780 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002781 */
2782int __init cgroup_init_early(void)
2783{
2784 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002785 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07002786 INIT_LIST_HEAD(&init_css_set.cg_links);
2787 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07002788 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07002789 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002790 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07002791 root_count = 1;
2792 init_task.cgroups = &init_css_set;
2793
2794 init_css_set_link.cg = &init_css_set;
Paul Menagebd89aab2007-10-18 23:40:44 -07002795 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07002796 &rootnode.top_cgroup.css_sets);
2797 list_add(&init_css_set_link.cg_link_list,
2798 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002799
Li Zefan472b1052008-04-29 01:00:11 -07002800 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2801 INIT_HLIST_HEAD(&css_set_table[i]);
2802
Paul Menageddbcc7e2007-10-18 23:39:30 -07002803 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2804 struct cgroup_subsys *ss = subsys[i];
2805
2806 BUG_ON(!ss->name);
2807 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2808 BUG_ON(!ss->create);
2809 BUG_ON(!ss->destroy);
2810 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08002811 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07002812 ss->name, ss->subsys_id);
2813 BUG();
2814 }
2815
2816 if (ss->early_init)
2817 cgroup_init_subsys(ss);
2818 }
2819 return 0;
2820}
2821
2822/**
Li Zefana043e3b2008-02-23 15:24:09 -08002823 * cgroup_init - cgroup initialization
2824 *
2825 * Register cgroup filesystem and /proc file, and initialize
2826 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002827 */
2828int __init cgroup_init(void)
2829{
2830 int err;
2831 int i;
Li Zefan472b1052008-04-29 01:00:11 -07002832 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07002833
2834 err = bdi_init(&cgroup_backing_dev_info);
2835 if (err)
2836 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002837
2838 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2839 struct cgroup_subsys *ss = subsys[i];
2840 if (!ss->early_init)
2841 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002842 if (ss->use_id)
2843 cgroup_subsys_init_idr(ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002844 }
2845
Li Zefan472b1052008-04-29 01:00:11 -07002846 /* Add init_css_set to the hash table */
2847 hhead = css_set_hash(init_css_set.subsys);
2848 hlist_add_head(&init_css_set.hlist, hhead);
2849
Paul Menageddbcc7e2007-10-18 23:39:30 -07002850 err = register_filesystem(&cgroup_fs_type);
2851 if (err < 0)
2852 goto out;
2853
Li Zefan46ae2202008-04-29 01:00:08 -07002854 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07002855
Paul Menageddbcc7e2007-10-18 23:39:30 -07002856out:
Paul Menagea4243162007-10-18 23:39:35 -07002857 if (err)
2858 bdi_destroy(&cgroup_backing_dev_info);
2859
Paul Menageddbcc7e2007-10-18 23:39:30 -07002860 return err;
2861}
Paul Menageb4f48b62007-10-18 23:39:33 -07002862
Paul Menagea4243162007-10-18 23:39:35 -07002863/*
2864 * proc_cgroup_show()
2865 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2866 * - Used for /proc/<pid>/cgroup.
2867 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2868 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002869 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07002870 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2871 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2872 * cgroup to top_cgroup.
2873 */
2874
2875/* TODO: Use a proper seq_file iterator */
2876static int proc_cgroup_show(struct seq_file *m, void *v)
2877{
2878 struct pid *pid;
2879 struct task_struct *tsk;
2880 char *buf;
2881 int retval;
2882 struct cgroupfs_root *root;
2883
2884 retval = -ENOMEM;
2885 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2886 if (!buf)
2887 goto out;
2888
2889 retval = -ESRCH;
2890 pid = m->private;
2891 tsk = get_pid_task(pid, PIDTYPE_PID);
2892 if (!tsk)
2893 goto out_free;
2894
2895 retval = 0;
2896
2897 mutex_lock(&cgroup_mutex);
2898
Li Zefane5f6a862009-01-07 18:07:41 -08002899 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07002900 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07002901 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07002902 int subsys_id;
2903 int count = 0;
2904
Paul Menageb6c30062008-04-10 21:29:16 -07002905 seq_printf(m, "%lu:", root->subsys_bits);
Paul Menagea4243162007-10-18 23:39:35 -07002906 for_each_subsys(root, ss)
2907 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2908 seq_putc(m, ':');
2909 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07002910 cgrp = task_cgroup(tsk, subsys_id);
2911 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07002912 if (retval < 0)
2913 goto out_unlock;
2914 seq_puts(m, buf);
2915 seq_putc(m, '\n');
2916 }
2917
2918out_unlock:
2919 mutex_unlock(&cgroup_mutex);
2920 put_task_struct(tsk);
2921out_free:
2922 kfree(buf);
2923out:
2924 return retval;
2925}
2926
2927static int cgroup_open(struct inode *inode, struct file *file)
2928{
2929 struct pid *pid = PROC_I(inode)->pid;
2930 return single_open(file, proc_cgroup_show, pid);
2931}
2932
2933struct file_operations proc_cgroup_operations = {
2934 .open = cgroup_open,
2935 .read = seq_read,
2936 .llseek = seq_lseek,
2937 .release = single_release,
2938};
2939
2940/* Display information about each subsystem and each hierarchy */
2941static int proc_cgroupstats_show(struct seq_file *m, void *v)
2942{
2943 int i;
Paul Menagea4243162007-10-18 23:39:35 -07002944
Paul Menage8bab8dd2008-04-04 14:29:57 -07002945 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07002946 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07002947 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2948 struct cgroup_subsys *ss = subsys[i];
Paul Menage8bab8dd2008-04-04 14:29:57 -07002949 seq_printf(m, "%s\t%lu\t%d\t%d\n",
Paul Menage817929e2007-10-18 23:39:36 -07002950 ss->name, ss->root->subsys_bits,
Paul Menage8bab8dd2008-04-04 14:29:57 -07002951 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07002952 }
2953 mutex_unlock(&cgroup_mutex);
2954 return 0;
2955}
2956
2957static int cgroupstats_open(struct inode *inode, struct file *file)
2958{
Al Viro9dce07f2008-03-29 03:07:28 +00002959 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07002960}
2961
2962static struct file_operations proc_cgroupstats_operations = {
2963 .open = cgroupstats_open,
2964 .read = seq_read,
2965 .llseek = seq_lseek,
2966 .release = single_release,
2967};
2968
Paul Menageb4f48b62007-10-18 23:39:33 -07002969/**
2970 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08002971 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07002972 *
2973 * Description: A task inherits its parent's cgroup at fork().
2974 *
2975 * A pointer to the shared css_set was automatically copied in
2976 * fork.c by dup_task_struct(). However, we ignore that copy, since
2977 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08002978 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07002979 * have already changed current->cgroups, allowing the previously
2980 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07002981 *
2982 * At the point that cgroup_fork() is called, 'current' is the parent
2983 * task, and the passed argument 'child' points to the child task.
2984 */
2985void cgroup_fork(struct task_struct *child)
2986{
Paul Menage817929e2007-10-18 23:39:36 -07002987 task_lock(current);
2988 child->cgroups = current->cgroups;
2989 get_css_set(child->cgroups);
2990 task_unlock(current);
2991 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07002992}
2993
2994/**
Li Zefana043e3b2008-02-23 15:24:09 -08002995 * cgroup_fork_callbacks - run fork callbacks
2996 * @child: the new task
2997 *
2998 * Called on a new task very soon before adding it to the
2999 * tasklist. No need to take any locks since no-one can
3000 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003001 */
3002void cgroup_fork_callbacks(struct task_struct *child)
3003{
3004 if (need_forkexit_callback) {
3005 int i;
3006 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3007 struct cgroup_subsys *ss = subsys[i];
3008 if (ss->fork)
3009 ss->fork(ss, child);
3010 }
3011 }
3012}
3013
3014/**
Li Zefana043e3b2008-02-23 15:24:09 -08003015 * cgroup_post_fork - called on a new task after adding it to the task list
3016 * @child: the task in question
3017 *
3018 * Adds the task to the list running through its css_set if necessary.
3019 * Has to be after the task is visible on the task list in case we race
3020 * with the first call to cgroup_iter_start() - to guarantee that the
3021 * new task ends up on its list.
3022 */
Paul Menage817929e2007-10-18 23:39:36 -07003023void cgroup_post_fork(struct task_struct *child)
3024{
3025 if (use_task_css_set_links) {
3026 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003027 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003028 if (list_empty(&child->cg_list))
3029 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003030 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003031 write_unlock(&css_set_lock);
3032 }
3033}
3034/**
Paul Menageb4f48b62007-10-18 23:39:33 -07003035 * cgroup_exit - detach cgroup from exiting task
3036 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08003037 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07003038 *
3039 * Description: Detach cgroup from @tsk and release it.
3040 *
3041 * Note that cgroups marked notify_on_release force every task in
3042 * them to take the global cgroup_mutex mutex when exiting.
3043 * This could impact scaling on very large systems. Be reluctant to
3044 * use notify_on_release cgroups where very high task exit scaling
3045 * is required on large systems.
3046 *
3047 * the_top_cgroup_hack:
3048 *
3049 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3050 *
3051 * We call cgroup_exit() while the task is still competent to
3052 * handle notify_on_release(), then leave the task attached to the
3053 * root cgroup in each hierarchy for the remainder of its exit.
3054 *
3055 * To do this properly, we would increment the reference count on
3056 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3057 * code we would add a second cgroup function call, to drop that
3058 * reference. This would just create an unnecessary hot spot on
3059 * the top_cgroup reference count, to no avail.
3060 *
3061 * Normally, holding a reference to a cgroup without bumping its
3062 * count is unsafe. The cgroup could go away, or someone could
3063 * attach us to a different cgroup, decrementing the count on
3064 * the first cgroup that we never incremented. But in this case,
3065 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003066 * which wards off any cgroup_attach_task() attempts, or task is a failed
3067 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003068 */
3069void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3070{
3071 int i;
Paul Menage817929e2007-10-18 23:39:36 -07003072 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07003073
3074 if (run_callbacks && need_forkexit_callback) {
3075 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3076 struct cgroup_subsys *ss = subsys[i];
3077 if (ss->exit)
3078 ss->exit(ss, tsk);
3079 }
3080 }
Paul Menage817929e2007-10-18 23:39:36 -07003081
3082 /*
3083 * Unlink from the css_set task list if necessary.
3084 * Optimistically check cg_list before taking
3085 * css_set_lock
3086 */
3087 if (!list_empty(&tsk->cg_list)) {
3088 write_lock(&css_set_lock);
3089 if (!list_empty(&tsk->cg_list))
3090 list_del(&tsk->cg_list);
3091 write_unlock(&css_set_lock);
3092 }
3093
Paul Menageb4f48b62007-10-18 23:39:33 -07003094 /* Reassign the task to the init_css_set. */
3095 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003096 cg = tsk->cgroups;
3097 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07003098 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003099 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003100 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07003101}
Paul Menage697f4162007-10-18 23:39:34 -07003102
3103/**
Li Zefana043e3b2008-02-23 15:24:09 -08003104 * cgroup_clone - clone the cgroup the given subsystem is attached to
3105 * @tsk: the task to be moved
3106 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003107 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08003108 *
3109 * Duplicate the current cgroup in the hierarchy that the given
3110 * subsystem is attached to, and move this task into the new
3111 * child.
Paul Menage697f4162007-10-18 23:39:34 -07003112 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003113int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3114 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07003115{
3116 struct dentry *dentry;
3117 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07003118 struct cgroup *parent, *child;
3119 struct inode *inode;
3120 struct css_set *cg;
3121 struct cgroupfs_root *root;
3122 struct cgroup_subsys *ss;
3123
3124 /* We shouldn't be called by an unregistered subsystem */
3125 BUG_ON(!subsys->active);
3126
3127 /* First figure out what hierarchy and cgroup we're dealing
3128 * with, and pin them so we can drop cgroup_mutex */
3129 mutex_lock(&cgroup_mutex);
3130 again:
3131 root = subsys->root;
3132 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07003133 mutex_unlock(&cgroup_mutex);
3134 return 0;
3135 }
Paul Menage697f4162007-10-18 23:39:34 -07003136
Paul Menage697f4162007-10-18 23:39:34 -07003137 /* Pin the hierarchy */
Li Zefan1404f062009-01-29 14:25:21 -08003138 if (!atomic_inc_not_zero(&root->sb->s_active)) {
Li Zefan7b574b72009-01-04 12:00:45 -08003139 /* We race with the final deactivate_super() */
3140 mutex_unlock(&cgroup_mutex);
3141 return 0;
3142 }
Paul Menage697f4162007-10-18 23:39:34 -07003143
Paul Menage817929e2007-10-18 23:39:36 -07003144 /* Keep the cgroup alive */
Li Zefan1404f062009-01-29 14:25:21 -08003145 task_lock(tsk);
3146 parent = task_cgroup(tsk, subsys->subsys_id);
3147 cg = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07003148 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08003149 task_unlock(tsk);
Li Zefan1404f062009-01-29 14:25:21 -08003150
Paul Menage697f4162007-10-18 23:39:34 -07003151 mutex_unlock(&cgroup_mutex);
3152
3153 /* Now do the VFS work to create a cgroup */
3154 inode = parent->dentry->d_inode;
3155
3156 /* Hold the parent directory mutex across this operation to
3157 * stop anyone else deleting the new cgroup */
3158 mutex_lock(&inode->i_mutex);
3159 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3160 if (IS_ERR(dentry)) {
3161 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08003162 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07003163 PTR_ERR(dentry));
3164 ret = PTR_ERR(dentry);
3165 goto out_release;
3166 }
3167
3168 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08003169 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07003170 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07003171 dput(dentry);
3172 if (ret) {
3173 printk(KERN_INFO
3174 "Failed to create cgroup %s: %d\n", nodename,
3175 ret);
3176 goto out_release;
3177 }
3178
Paul Menage697f4162007-10-18 23:39:34 -07003179 /* The cgroup now exists. Retake cgroup_mutex and check
3180 * that we're still in the same state that we thought we
3181 * were. */
3182 mutex_lock(&cgroup_mutex);
3183 if ((root != subsys->root) ||
3184 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3185 /* Aargh, we raced ... */
3186 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003187 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07003188
Li Zefan1404f062009-01-29 14:25:21 -08003189 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003190 /* The cgroup is still accessible in the VFS, but
3191 * we're not going to try to rmdir() it at this
3192 * point. */
3193 printk(KERN_INFO
3194 "Race in cgroup_clone() - leaking cgroup %s\n",
3195 nodename);
3196 goto again;
3197 }
3198
3199 /* do any required auto-setup */
3200 for_each_subsys(root, ss) {
3201 if (ss->post_clone)
3202 ss->post_clone(ss, child);
3203 }
3204
3205 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08003206 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07003207 mutex_unlock(&cgroup_mutex);
3208
3209 out_release:
3210 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003211
3212 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003213 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003214 mutex_unlock(&cgroup_mutex);
Li Zefan1404f062009-01-29 14:25:21 -08003215 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003216 return ret;
3217}
3218
Li Zefana043e3b2008-02-23 15:24:09 -08003219/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003220 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08003221 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003222 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08003223 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003224 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3225 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07003226 *
3227 * If we are sending in dummytop, then presumably we are creating
3228 * the top cgroup in the subsystem.
3229 *
3230 * Called only by the ns (nsproxy) cgroup.
3231 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003232int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07003233{
3234 int ret;
3235 struct cgroup *target;
3236 int subsys_id;
3237
Paul Menagebd89aab2007-10-18 23:40:44 -07003238 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07003239 return 1;
3240
Paul Menagebd89aab2007-10-18 23:40:44 -07003241 get_first_subsys(cgrp, NULL, &subsys_id);
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003242 target = task_cgroup(task, subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07003243 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3244 cgrp = cgrp->parent;
3245 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07003246 return ret;
3247}
Paul Menage81a6a5c2007-10-18 23:39:38 -07003248
Paul Menagebd89aab2007-10-18 23:40:44 -07003249static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003250{
3251 /* All of these checks rely on RCU to keep the cgroup
3252 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07003253 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3254 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003255 /* Control Group is currently removeable. If it's not
3256 * already queued for a userspace notification, queue
3257 * it now */
3258 int need_schedule_work = 0;
3259 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003260 if (!cgroup_is_removed(cgrp) &&
3261 list_empty(&cgrp->release_list)) {
3262 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003263 need_schedule_work = 1;
3264 }
3265 spin_unlock(&release_list_lock);
3266 if (need_schedule_work)
3267 schedule_work(&release_agent_work);
3268 }
3269}
3270
3271void __css_put(struct cgroup_subsys_state *css)
3272{
Paul Menagebd89aab2007-10-18 23:40:44 -07003273 struct cgroup *cgrp = css->cgroup;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003274 rcu_read_lock();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003275 if (atomic_dec_return(&css->refcnt) == 1) {
3276 if (notify_on_release(cgrp)) {
3277 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3278 check_for_release(cgrp);
3279 }
3280 cgroup_wakeup_rmdir_waiters(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003281 }
3282 rcu_read_unlock();
3283}
3284
3285/*
3286 * Notify userspace when a cgroup is released, by running the
3287 * configured release agent with the name of the cgroup (path
3288 * relative to the root of cgroup file system) as the argument.
3289 *
3290 * Most likely, this user command will try to rmdir this cgroup.
3291 *
3292 * This races with the possibility that some other task will be
3293 * attached to this cgroup before it is removed, or that some other
3294 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3295 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3296 * unused, and this cgroup will be reprieved from its death sentence,
3297 * to continue to serve a useful existence. Next time it's released,
3298 * we will get notified again, if it still has 'notify_on_release' set.
3299 *
3300 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3301 * means only wait until the task is successfully execve()'d. The
3302 * separate release agent task is forked by call_usermodehelper(),
3303 * then control in this thread returns here, without waiting for the
3304 * release agent task. We don't bother to wait because the caller of
3305 * this routine has no use for the exit status of the release agent
3306 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003307 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003308static void cgroup_release_agent(struct work_struct *work)
3309{
3310 BUG_ON(work != &release_agent_work);
3311 mutex_lock(&cgroup_mutex);
3312 spin_lock(&release_list_lock);
3313 while (!list_empty(&release_list)) {
3314 char *argv[3], *envp[3];
3315 int i;
Paul Menagee788e062008-07-25 01:46:59 -07003316 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003317 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003318 struct cgroup,
3319 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003320 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003321 spin_unlock(&release_list_lock);
3322 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07003323 if (!pathbuf)
3324 goto continue_free;
3325 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3326 goto continue_free;
3327 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3328 if (!agentbuf)
3329 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003330
3331 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07003332 argv[i++] = agentbuf;
3333 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003334 argv[i] = NULL;
3335
3336 i = 0;
3337 /* minimal command environment */
3338 envp[i++] = "HOME=/";
3339 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3340 envp[i] = NULL;
3341
3342 /* Drop the lock while we invoke the usermode helper,
3343 * since the exec could involve hitting disk and hence
3344 * be a slow process */
3345 mutex_unlock(&cgroup_mutex);
3346 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003347 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07003348 continue_free:
3349 kfree(pathbuf);
3350 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003351 spin_lock(&release_list_lock);
3352 }
3353 spin_unlock(&release_list_lock);
3354 mutex_unlock(&cgroup_mutex);
3355}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003356
3357static int __init cgroup_disable(char *str)
3358{
3359 int i;
3360 char *token;
3361
3362 while ((token = strsep(&str, ",")) != NULL) {
3363 if (!*token)
3364 continue;
3365
3366 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3367 struct cgroup_subsys *ss = subsys[i];
3368
3369 if (!strcmp(token, ss->name)) {
3370 ss->disabled = 1;
3371 printk(KERN_INFO "Disabling %s control group"
3372 " subsystem\n", ss->name);
3373 break;
3374 }
3375 }
3376 }
3377 return 1;
3378}
3379__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003380
3381/*
3382 * Functons for CSS ID.
3383 */
3384
3385/*
3386 *To get ID other than 0, this should be called when !cgroup_is_removed().
3387 */
3388unsigned short css_id(struct cgroup_subsys_state *css)
3389{
3390 struct css_id *cssid = rcu_dereference(css->id);
3391
3392 if (cssid)
3393 return cssid->id;
3394 return 0;
3395}
3396
3397unsigned short css_depth(struct cgroup_subsys_state *css)
3398{
3399 struct css_id *cssid = rcu_dereference(css->id);
3400
3401 if (cssid)
3402 return cssid->depth;
3403 return 0;
3404}
3405
3406bool css_is_ancestor(struct cgroup_subsys_state *child,
3407 struct cgroup_subsys_state *root)
3408{
3409 struct css_id *child_id = rcu_dereference(child->id);
3410 struct css_id *root_id = rcu_dereference(root->id);
3411
3412 if (!child_id || !root_id || (child_id->depth < root_id->depth))
3413 return false;
3414 return child_id->stack[root_id->depth] == root_id->id;
3415}
3416
3417static void __free_css_id_cb(struct rcu_head *head)
3418{
3419 struct css_id *id;
3420
3421 id = container_of(head, struct css_id, rcu_head);
3422 kfree(id);
3423}
3424
3425void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
3426{
3427 struct css_id *id = css->id;
3428 /* When this is called before css_id initialization, id can be NULL */
3429 if (!id)
3430 return;
3431
3432 BUG_ON(!ss->use_id);
3433
3434 rcu_assign_pointer(id->css, NULL);
3435 rcu_assign_pointer(css->id, NULL);
3436 spin_lock(&ss->id_lock);
3437 idr_remove(&ss->idr, id->id);
3438 spin_unlock(&ss->id_lock);
3439 call_rcu(&id->rcu_head, __free_css_id_cb);
3440}
3441
3442/*
3443 * This is called by init or create(). Then, calls to this function are
3444 * always serialized (By cgroup_mutex() at create()).
3445 */
3446
3447static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
3448{
3449 struct css_id *newid;
3450 int myid, error, size;
3451
3452 BUG_ON(!ss->use_id);
3453
3454 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
3455 newid = kzalloc(size, GFP_KERNEL);
3456 if (!newid)
3457 return ERR_PTR(-ENOMEM);
3458 /* get id */
3459 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
3460 error = -ENOMEM;
3461 goto err_out;
3462 }
3463 spin_lock(&ss->id_lock);
3464 /* Don't use 0. allocates an ID of 1-65535 */
3465 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
3466 spin_unlock(&ss->id_lock);
3467
3468 /* Returns error when there are no free spaces for new ID.*/
3469 if (error) {
3470 error = -ENOSPC;
3471 goto err_out;
3472 }
3473 if (myid > CSS_ID_MAX)
3474 goto remove_idr;
3475
3476 newid->id = myid;
3477 newid->depth = depth;
3478 return newid;
3479remove_idr:
3480 error = -ENOSPC;
3481 spin_lock(&ss->id_lock);
3482 idr_remove(&ss->idr, myid);
3483 spin_unlock(&ss->id_lock);
3484err_out:
3485 kfree(newid);
3486 return ERR_PTR(error);
3487
3488}
3489
3490static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss)
3491{
3492 struct css_id *newid;
3493 struct cgroup_subsys_state *rootcss;
3494
3495 spin_lock_init(&ss->id_lock);
3496 idr_init(&ss->idr);
3497
3498 rootcss = init_css_set.subsys[ss->subsys_id];
3499 newid = get_new_cssid(ss, 0);
3500 if (IS_ERR(newid))
3501 return PTR_ERR(newid);
3502
3503 newid->stack[0] = newid->id;
3504 newid->css = rootcss;
3505 rootcss->id = newid;
3506 return 0;
3507}
3508
3509static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
3510 struct cgroup *child)
3511{
3512 int subsys_id, i, depth = 0;
3513 struct cgroup_subsys_state *parent_css, *child_css;
3514 struct css_id *child_id, *parent_id = NULL;
3515
3516 subsys_id = ss->subsys_id;
3517 parent_css = parent->subsys[subsys_id];
3518 child_css = child->subsys[subsys_id];
3519 depth = css_depth(parent_css) + 1;
3520 parent_id = parent_css->id;
3521
3522 child_id = get_new_cssid(ss, depth);
3523 if (IS_ERR(child_id))
3524 return PTR_ERR(child_id);
3525
3526 for (i = 0; i < depth; i++)
3527 child_id->stack[i] = parent_id->stack[i];
3528 child_id->stack[depth] = child_id->id;
3529 /*
3530 * child_id->css pointer will be set after this cgroup is available
3531 * see cgroup_populate_dir()
3532 */
3533 rcu_assign_pointer(child_css->id, child_id);
3534
3535 return 0;
3536}
3537
3538/**
3539 * css_lookup - lookup css by id
3540 * @ss: cgroup subsys to be looked into.
3541 * @id: the id
3542 *
3543 * Returns pointer to cgroup_subsys_state if there is valid one with id.
3544 * NULL if not. Should be called under rcu_read_lock()
3545 */
3546struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
3547{
3548 struct css_id *cssid = NULL;
3549
3550 BUG_ON(!ss->use_id);
3551 cssid = idr_find(&ss->idr, id);
3552
3553 if (unlikely(!cssid))
3554 return NULL;
3555
3556 return rcu_dereference(cssid->css);
3557}
3558
3559/**
3560 * css_get_next - lookup next cgroup under specified hierarchy.
3561 * @ss: pointer to subsystem
3562 * @id: current position of iteration.
3563 * @root: pointer to css. search tree under this.
3564 * @foundid: position of found object.
3565 *
3566 * Search next css under the specified hierarchy of rootid. Calling under
3567 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
3568 */
3569struct cgroup_subsys_state *
3570css_get_next(struct cgroup_subsys *ss, int id,
3571 struct cgroup_subsys_state *root, int *foundid)
3572{
3573 struct cgroup_subsys_state *ret = NULL;
3574 struct css_id *tmp;
3575 int tmpid;
3576 int rootid = css_id(root);
3577 int depth = css_depth(root);
3578
3579 if (!rootid)
3580 return NULL;
3581
3582 BUG_ON(!ss->use_id);
3583 /* fill start point for scan */
3584 tmpid = id;
3585 while (1) {
3586 /*
3587 * scan next entry from bitmap(tree), tmpid is updated after
3588 * idr_get_next().
3589 */
3590 spin_lock(&ss->id_lock);
3591 tmp = idr_get_next(&ss->idr, &tmpid);
3592 spin_unlock(&ss->id_lock);
3593
3594 if (!tmp)
3595 break;
3596 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
3597 ret = rcu_dereference(tmp->css);
3598 if (ret) {
3599 *foundid = tmpid;
3600 break;
3601 }
3602 }
3603 /* continue to scan from next id */
3604 tmpid = tmpid + 1;
3605 }
3606 return ret;
3607}
3608