blob: 250dac05680feb16bb2fb647750a267216a97ed4 [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>
Alessio Igor Bogani337eb002009-05-12 15:10:54 +020049#include <linux/smp_lock.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070050#include <linux/pid_namespace.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070051
Paul Menageddbcc7e2007-10-18 23:39:30 -070052#include <asm/atomic.h>
53
Paul Menage81a6a5c2007-10-18 23:39:38 -070054static DEFINE_MUTEX(cgroup_mutex);
55
Paul Menageddbcc7e2007-10-18 23:39:30 -070056/* Generate an array of cgroup subsystem pointers */
57#define SUBSYS(_x) &_x ## _subsys,
58
59static struct cgroup_subsys *subsys[] = {
60#include <linux/cgroup_subsys.h>
61};
62
63/*
64 * A cgroupfs_root represents the root of a cgroup hierarchy,
65 * and may be associated with a superblock to form an active
66 * hierarchy
67 */
68struct cgroupfs_root {
69 struct super_block *sb;
70
71 /*
72 * The bitmask of subsystems intended to be attached to this
73 * hierarchy
74 */
75 unsigned long subsys_bits;
76
77 /* The bitmask of subsystems currently attached to this hierarchy */
78 unsigned long actual_subsys_bits;
79
80 /* A list running through the attached subsystems */
81 struct list_head subsys_list;
82
83 /* The root cgroup for this hierarchy */
84 struct cgroup top_cgroup;
85
86 /* Tracks how many cgroups are currently defined in hierarchy.*/
87 int number_of_cgroups;
88
Li Zefane5f6a862009-01-07 18:07:41 -080089 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -070090 struct list_head root_list;
91
92 /* Hierarchy-specific flags */
93 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -070094
Paul Menagee788e062008-07-25 01:46:59 -070095 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -070096 char release_agent_path[PATH_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -070097};
98
Paul Menageddbcc7e2007-10-18 23:39:30 -070099/*
100 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
101 * subsystems that are otherwise unattached - it never has more than a
102 * single cgroup, and all tasks are part of that cgroup.
103 */
104static struct cgroupfs_root rootnode;
105
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700106/*
107 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
108 * cgroup_subsys->use_id != 0.
109 */
110#define CSS_ID_MAX (65535)
111struct css_id {
112 /*
113 * The css to which this ID points. This pointer is set to valid value
114 * after cgroup is populated. If cgroup is removed, this will be NULL.
115 * This pointer is expected to be RCU-safe because destroy()
116 * is called after synchronize_rcu(). But for safe use, css_is_removed()
117 * css_tryget() should be used for avoiding race.
118 */
119 struct cgroup_subsys_state *css;
120 /*
121 * ID of this css.
122 */
123 unsigned short id;
124 /*
125 * Depth in hierarchy which this ID belongs to.
126 */
127 unsigned short depth;
128 /*
129 * ID is freed by RCU. (and lookup routine is RCU safe.)
130 */
131 struct rcu_head rcu_head;
132 /*
133 * Hierarchy of CSS ID belongs to.
134 */
135 unsigned short stack[0]; /* Array of Length (depth+1) */
136};
137
138
Paul Menageddbcc7e2007-10-18 23:39:30 -0700139/* The list of hierarchy roots */
140
141static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700142static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700143
144/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
145#define dummytop (&rootnode.top_cgroup)
146
147/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800148 * check for fork/exit handlers to call. This avoids us having to do
149 * extra work in the fork/exit path if none of the subsystems need to
150 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700151 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700152static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700153
Paul Menageddbcc7e2007-10-18 23:39:30 -0700154/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700155inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700156{
Paul Menagebd89aab2007-10-18 23:40:44 -0700157 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700158}
159
160/* bits in struct cgroupfs_root flags field */
161enum {
162 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
163};
164
Adrian Bunke9685a02008-02-07 00:13:46 -0800165static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700166{
167 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700168 (1 << CGRP_RELEASABLE) |
169 (1 << CGRP_NOTIFY_ON_RELEASE);
170 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700171}
172
Adrian Bunke9685a02008-02-07 00:13:46 -0800173static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700174{
Paul Menagebd89aab2007-10-18 23:40:44 -0700175 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700176}
177
Paul Menageddbcc7e2007-10-18 23:39:30 -0700178/*
179 * for_each_subsys() allows you to iterate on each subsystem attached to
180 * an active hierarchy
181 */
182#define for_each_subsys(_root, _ss) \
183list_for_each_entry(_ss, &_root->subsys_list, sibling)
184
Li Zefane5f6a862009-01-07 18:07:41 -0800185/* for_each_active_root() allows you to iterate across the active hierarchies */
186#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187list_for_each_entry(_root, &roots, root_list)
188
Paul Menage81a6a5c2007-10-18 23:39:38 -0700189/* the list of cgroups eligible for automatic release. Protected by
190 * release_list_lock */
191static LIST_HEAD(release_list);
192static DEFINE_SPINLOCK(release_list_lock);
193static void cgroup_release_agent(struct work_struct *work);
194static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700195static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700196
Paul Menage817929e2007-10-18 23:39:36 -0700197/* Link structure for associating css_set objects with cgroups */
198struct cg_cgroup_link {
199 /*
200 * List running through cg_cgroup_links associated with a
201 * cgroup, anchored on cgroup->css_sets
202 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700203 struct list_head cgrp_link_list;
Paul Menage817929e2007-10-18 23:39:36 -0700204 /*
205 * List running through cg_cgroup_links pointing at a
206 * single css_set object, anchored on css_set->cg_links
207 */
208 struct list_head cg_link_list;
209 struct css_set *cg;
210};
211
212/* The default css_set - used by init and its children prior to any
213 * hierarchies being mounted. It contains a pointer to the root state
214 * for each subsystem. Also used to anchor the list of css_sets. Not
215 * reference-counted, to improve performance when child cgroups
216 * haven't been created.
217 */
218
219static struct css_set init_css_set;
220static struct cg_cgroup_link init_css_set_link;
221
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700222static int cgroup_subsys_init_idr(struct cgroup_subsys *ss);
223
Paul Menage817929e2007-10-18 23:39:36 -0700224/* css_set_lock protects the list of css_set objects, and the
225 * chain of tasks off each css_set. Nests outside task->alloc_lock
226 * due to cgroup_iter_start() */
227static DEFINE_RWLOCK(css_set_lock);
228static int css_set_count;
229
Li Zefan472b1052008-04-29 01:00:11 -0700230/* hash table for cgroup groups. This improves the performance to
231 * find an existing css_set */
232#define CSS_SET_HASH_BITS 7
233#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
234static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
235
236static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
237{
238 int i;
239 int index;
240 unsigned long tmp = 0UL;
241
242 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
243 tmp += (unsigned long)css[i];
244 tmp = (tmp >> 16) ^ tmp;
245
246 index = hash_long(tmp, CSS_SET_HASH_BITS);
247
248 return &css_set_table[index];
249}
250
Paul Menage817929e2007-10-18 23:39:36 -0700251/* We don't maintain the lists running through each css_set to its
252 * task until after the first call to cgroup_iter_start(). This
253 * reduces the fork()/exit() overhead for people who have cgroups
254 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700255static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700256
257/* When we create or destroy a css_set, the operation simply
258 * takes/releases a reference count on all the cgroups referenced
259 * by subsystems in this css_set. This can end up multiple-counting
260 * some cgroups, but that's OK - the ref-count is just a
261 * busy/not-busy indicator; ensuring that we only count each cgroup
262 * once would require taking a global lock to ensure that no
Paul Menageb4f48b62007-10-18 23:39:33 -0700263 * subsystems moved between hierarchies while we were doing so.
264 *
265 * Possible TODO: decide at boot time based on the number of
266 * registered subsystems and the number of CPUs or NUMA nodes whether
267 * it's better for performance to ref-count every subsystem, or to
268 * take a global lock and only add one ref count to each hierarchy.
269 */
Paul Menageb4f48b62007-10-18 23:39:33 -0700270
Paul Menage817929e2007-10-18 23:39:36 -0700271/*
272 * unlink a css_set from the list and free it
273 */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700274static void unlink_css_set(struct css_set *cg)
Paul Menageb4f48b62007-10-18 23:39:33 -0700275{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700276 struct cg_cgroup_link *link;
277 struct cg_cgroup_link *saved_link;
278
Li Zefan472b1052008-04-29 01:00:11 -0700279 hlist_del(&cg->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700280 css_set_count--;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700281
282 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
283 cg_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -0700284 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700285 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700286 kfree(link);
287 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700288}
289
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700290static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700291{
292 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700293 /*
294 * Ensure that the refcount doesn't hit zero while any readers
295 * can see it. Similar to atomic_dec_and_lock(), but for an
296 * rwlock
297 */
298 if (atomic_add_unless(&cg->refcount, -1, 1))
299 return;
300 write_lock(&css_set_lock);
301 if (!atomic_dec_and_test(&cg->refcount)) {
302 write_unlock(&css_set_lock);
303 return;
304 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700305 unlink_css_set(cg);
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700306 write_unlock(&css_set_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700307
308 rcu_read_lock();
309 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagea47295e2009-01-07 18:07:44 -0800310 struct cgroup *cgrp = rcu_dereference(cg->subsys[i]->cgroup);
Paul Menagebd89aab2007-10-18 23:40:44 -0700311 if (atomic_dec_and_test(&cgrp->count) &&
312 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700313 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700314 set_bit(CGRP_RELEASABLE, &cgrp->flags);
315 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700316 }
317 }
318 rcu_read_unlock();
Paul Menage817929e2007-10-18 23:39:36 -0700319 kfree(cg);
320}
321
322/*
323 * refcounted get/put for css_set objects
324 */
325static inline void get_css_set(struct css_set *cg)
326{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700327 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700328}
329
330static inline void put_css_set(struct css_set *cg)
331{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700332 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700333}
334
Paul Menage81a6a5c2007-10-18 23:39:38 -0700335static inline void put_css_set_taskexit(struct css_set *cg)
336{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700337 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700338}
339
Paul Menage817929e2007-10-18 23:39:36 -0700340/*
341 * find_existing_css_set() is a helper for
342 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700343 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700344 *
345 * oldcg: the cgroup group that we're using before the cgroup
346 * transition
347 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700348 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700349 *
350 * template: location in which to build the desired set of subsystem
351 * state objects for the new cgroup group
352 */
Paul Menage817929e2007-10-18 23:39:36 -0700353static struct css_set *find_existing_css_set(
354 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700355 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700356 struct cgroup_subsys_state *template[])
357{
358 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700359 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700360 struct hlist_head *hhead;
361 struct hlist_node *node;
362 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700363
364 /* Built the set of subsystem state objects that we want to
365 * see in the new css_set */
366 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800367 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700368 /* Subsystem is in this hierarchy. So we want
369 * the subsystem state from the new
370 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700371 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700372 } else {
373 /* Subsystem is not in this hierarchy, so we
374 * don't want to change the subsystem state */
375 template[i] = oldcg->subsys[i];
376 }
377 }
378
Li Zefan472b1052008-04-29 01:00:11 -0700379 hhead = css_set_hash(template);
380 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage817929e2007-10-18 23:39:36 -0700381 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
382 /* All subsystems matched */
383 return cg;
384 }
Li Zefan472b1052008-04-29 01:00:11 -0700385 }
Paul Menage817929e2007-10-18 23:39:36 -0700386
387 /* No existing cgroup group matched */
388 return NULL;
389}
390
Paul Menage817929e2007-10-18 23:39:36 -0700391static void free_cg_links(struct list_head *tmp)
392{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700393 struct cg_cgroup_link *link;
394 struct cg_cgroup_link *saved_link;
395
396 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700397 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700398 kfree(link);
399 }
400}
401
402/*
Li Zefan36553432008-07-29 22:33:19 -0700403 * allocate_cg_links() allocates "count" cg_cgroup_link structures
404 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
405 * success or a negative error
406 */
407static int allocate_cg_links(int count, struct list_head *tmp)
408{
409 struct cg_cgroup_link *link;
410 int i;
411 INIT_LIST_HEAD(tmp);
412 for (i = 0; i < count; i++) {
413 link = kmalloc(sizeof(*link), GFP_KERNEL);
414 if (!link) {
415 free_cg_links(tmp);
416 return -ENOMEM;
417 }
418 list_add(&link->cgrp_link_list, tmp);
419 }
420 return 0;
421}
422
Li Zefanc12f65d2009-01-07 18:07:42 -0800423/**
424 * link_css_set - a helper function to link a css_set to a cgroup
425 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
426 * @cg: the css_set to be linked
427 * @cgrp: the destination cgroup
428 */
429static void link_css_set(struct list_head *tmp_cg_links,
430 struct css_set *cg, struct cgroup *cgrp)
431{
432 struct cg_cgroup_link *link;
433
434 BUG_ON(list_empty(tmp_cg_links));
435 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
436 cgrp_link_list);
437 link->cg = cg;
438 list_move(&link->cgrp_link_list, &cgrp->css_sets);
439 list_add(&link->cg_link_list, &cg->cg_links);
440}
441
Li Zefan36553432008-07-29 22:33:19 -0700442/*
Paul Menage817929e2007-10-18 23:39:36 -0700443 * find_css_set() takes an existing cgroup group and a
444 * cgroup object, and returns a css_set object that's
445 * equivalent to the old group, but with the given cgroup
446 * substituted into the appropriate hierarchy. Must be called with
447 * cgroup_mutex held
448 */
Paul Menage817929e2007-10-18 23:39:36 -0700449static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700450 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700451{
452 struct css_set *res;
453 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
454 int i;
455
456 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700457
Li Zefan472b1052008-04-29 01:00:11 -0700458 struct hlist_head *hhead;
459
Paul Menage817929e2007-10-18 23:39:36 -0700460 /* First see if we already have a cgroup group that matches
461 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700462 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700463 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700464 if (res)
465 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700466 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700467
468 if (res)
469 return res;
470
471 res = kmalloc(sizeof(*res), GFP_KERNEL);
472 if (!res)
473 return NULL;
474
475 /* Allocate all the cg_cgroup_link objects that we'll need */
476 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
477 kfree(res);
478 return NULL;
479 }
480
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700481 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700482 INIT_LIST_HEAD(&res->cg_links);
483 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700484 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700485
486 /* Copy the set of subsystem state objects generated in
487 * find_existing_css_set() */
488 memcpy(res->subsys, template, sizeof(res->subsys));
489
490 write_lock(&css_set_lock);
491 /* Add reference counts and links from the new css_set. */
492 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700493 struct cgroup *cgrp = res->subsys[i]->cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700494 struct cgroup_subsys *ss = subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -0700495 atomic_inc(&cgrp->count);
Paul Menage817929e2007-10-18 23:39:36 -0700496 /*
497 * We want to add a link once per cgroup, so we
498 * only do it for the first subsystem in each
499 * hierarchy
500 */
Li Zefanc12f65d2009-01-07 18:07:42 -0800501 if (ss->root->subsys_list.next == &ss->sibling)
502 link_css_set(&tmp_cg_links, res, cgrp);
Paul Menage817929e2007-10-18 23:39:36 -0700503 }
Li Zefanc12f65d2009-01-07 18:07:42 -0800504 if (list_empty(&rootnode.subsys_list))
505 link_css_set(&tmp_cg_links, res, dummytop);
Paul Menage817929e2007-10-18 23:39:36 -0700506
507 BUG_ON(!list_empty(&tmp_cg_links));
508
Paul Menage817929e2007-10-18 23:39:36 -0700509 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700510
511 /* Add this cgroup group to the hash table */
512 hhead = css_set_hash(res->subsys);
513 hlist_add_head(&res->hlist, hhead);
514
Paul Menage817929e2007-10-18 23:39:36 -0700515 write_unlock(&css_set_lock);
516
517 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700518}
519
Paul Menageddbcc7e2007-10-18 23:39:30 -0700520/*
521 * There is one global cgroup mutex. We also require taking
522 * task_lock() when dereferencing a task's cgroup subsys pointers.
523 * See "The task_lock() exception", at the end of this comment.
524 *
525 * A task must hold cgroup_mutex to modify cgroups.
526 *
527 * Any task can increment and decrement the count field without lock.
528 * So in general, code holding cgroup_mutex can't rely on the count
529 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800530 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700531 * means that no tasks are currently attached, therefore there is no
532 * way a task attached to that cgroup can fork (the other way to
533 * increment the count). So code holding cgroup_mutex can safely
534 * assume that if the count is zero, it will stay zero. Similarly, if
535 * a task holds cgroup_mutex on a cgroup with zero count, it
536 * knows that the cgroup won't be removed, as cgroup_rmdir()
537 * needs that mutex.
538 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700539 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
540 * (usually) take cgroup_mutex. These are the two most performance
541 * critical pieces of code here. The exception occurs on cgroup_exit(),
542 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
543 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800544 * to the release agent with the name of the cgroup (path relative to
545 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700546 *
547 * A cgroup can only be deleted if both its 'count' of using tasks
548 * is zero, and its list of 'children' cgroups is empty. Since all
549 * tasks in the system use _some_ cgroup, and since there is always at
550 * least one task in the system (init, pid == 1), therefore, top_cgroup
551 * always has either children cgroups and/or using tasks. So we don't
552 * need a special hack to ensure that top_cgroup cannot be deleted.
553 *
554 * The task_lock() exception
555 *
556 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800557 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800558 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700559 * several performance critical places that need to reference
560 * task->cgroup without the expense of grabbing a system global
561 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800562 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700563 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
564 * the task_struct routinely used for such matters.
565 *
566 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800567 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700568 */
569
Paul Menageddbcc7e2007-10-18 23:39:30 -0700570/**
571 * cgroup_lock - lock out any changes to cgroup structures
572 *
573 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700574void cgroup_lock(void)
575{
576 mutex_lock(&cgroup_mutex);
577}
578
579/**
580 * cgroup_unlock - release lock on cgroup changes
581 *
582 * Undo the lock taken in a previous cgroup_lock() call.
583 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700584void cgroup_unlock(void)
585{
586 mutex_unlock(&cgroup_mutex);
587}
588
589/*
590 * A couple of forward declarations required, due to cyclic reference loop:
591 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
592 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
593 * -> cgroup_mkdir.
594 */
595
596static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
597static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700598static int cgroup_populate_dir(struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700599static struct inode_operations cgroup_dir_inode_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700600static struct file_operations proc_cgroupstats_operations;
601
602static struct backing_dev_info cgroup_backing_dev_info = {
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700603 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700604};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700605
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700606static int alloc_css_id(struct cgroup_subsys *ss,
607 struct cgroup *parent, struct cgroup *child);
608
Paul Menageddbcc7e2007-10-18 23:39:30 -0700609static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
610{
611 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700612
613 if (inode) {
614 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100615 inode->i_uid = current_fsuid();
616 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700617 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
618 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
619 }
620 return inode;
621}
622
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800623/*
624 * Call subsys's pre_destroy handler.
625 * This is called before css refcnt check.
626 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700627static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800628{
629 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700630 int ret = 0;
631
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800632 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700633 if (ss->pre_destroy) {
634 ret = ss->pre_destroy(ss, cgrp);
635 if (ret)
636 break;
637 }
638 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800639}
640
Paul Menagea47295e2009-01-07 18:07:44 -0800641static void free_cgroup_rcu(struct rcu_head *obj)
642{
643 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
644
645 kfree(cgrp);
646}
647
Paul Menageddbcc7e2007-10-18 23:39:30 -0700648static void cgroup_diput(struct dentry *dentry, struct inode *inode)
649{
650 /* is dentry a directory ? if so, kfree() associated cgroup */
651 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700652 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800653 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700654 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700655 /* It's possible for external users to be holding css
656 * reference counts on a cgroup; css_put() needs to
657 * be able to access the cgroup after decrementing
658 * the reference count in order to know if it needs to
659 * queue the cgroup to be handled by the release
660 * agent */
661 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800662
663 mutex_lock(&cgroup_mutex);
664 /*
665 * Release the subsystem state objects.
666 */
Li Zefan75139b82009-01-07 18:07:33 -0800667 for_each_subsys(cgrp->root, ss)
668 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800669
670 cgrp->root->number_of_cgroups--;
671 mutex_unlock(&cgroup_mutex);
672
Paul Menagea47295e2009-01-07 18:07:44 -0800673 /*
674 * Drop the active superblock reference that we took when we
675 * created the cgroup
676 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800677 deactivate_super(cgrp->root->sb);
678
Paul Menagea47295e2009-01-07 18:07:44 -0800679 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700680 }
681 iput(inode);
682}
683
684static void remove_dir(struct dentry *d)
685{
686 struct dentry *parent = dget(d->d_parent);
687
688 d_delete(d);
689 simple_rmdir(parent->d_inode, d);
690 dput(parent);
691}
692
693static void cgroup_clear_directory(struct dentry *dentry)
694{
695 struct list_head *node;
696
697 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
698 spin_lock(&dcache_lock);
699 node = dentry->d_subdirs.next;
700 while (node != &dentry->d_subdirs) {
701 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
702 list_del_init(node);
703 if (d->d_inode) {
704 /* This should never be called on a cgroup
705 * directory with child cgroups */
706 BUG_ON(d->d_inode->i_mode & S_IFDIR);
707 d = dget_locked(d);
708 spin_unlock(&dcache_lock);
709 d_delete(d);
710 simple_unlink(dentry->d_inode, d);
711 dput(d);
712 spin_lock(&dcache_lock);
713 }
714 node = dentry->d_subdirs.next;
715 }
716 spin_unlock(&dcache_lock);
717}
718
719/*
720 * NOTE : the dentry must have been dget()'ed
721 */
722static void cgroup_d_remove_dir(struct dentry *dentry)
723{
724 cgroup_clear_directory(dentry);
725
726 spin_lock(&dcache_lock);
727 list_del_init(&dentry->d_u.d_child);
728 spin_unlock(&dcache_lock);
729 remove_dir(dentry);
730}
731
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700732/*
733 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
734 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
735 * reference to css->refcnt. In general, this refcnt is expected to goes down
736 * to zero, soon.
737 *
738 * CGRP_WAIT_ON_RMDIR flag is modified under cgroup's inode->i_mutex;
739 */
740DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
741
742static void cgroup_wakeup_rmdir_waiters(const struct cgroup *cgrp)
743{
744 if (unlikely(test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
745 wake_up_all(&cgroup_rmdir_waitq);
746}
747
Paul Menageddbcc7e2007-10-18 23:39:30 -0700748static int rebind_subsystems(struct cgroupfs_root *root,
749 unsigned long final_bits)
750{
751 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700752 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700753 int i;
754
755 removed_bits = root->actual_subsys_bits & ~final_bits;
756 added_bits = final_bits & ~root->actual_subsys_bits;
757 /* Check that any added subsystems are currently free */
758 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800759 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700760 struct cgroup_subsys *ss = subsys[i];
761 if (!(bit & added_bits))
762 continue;
763 if (ss->root != &rootnode) {
764 /* Subsystem isn't free */
765 return -EBUSY;
766 }
767 }
768
769 /* Currently we don't handle adding/removing subsystems when
770 * any child cgroups exist. This is theoretically supportable
771 * but involves complex error handling, so it's being left until
772 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800773 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700774 return -EBUSY;
775
776 /* Process each subsystem */
777 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
778 struct cgroup_subsys *ss = subsys[i];
779 unsigned long bit = 1UL << i;
780 if (bit & added_bits) {
781 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700782 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700783 BUG_ON(!dummytop->subsys[i]);
784 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800785 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700786 cgrp->subsys[i] = dummytop->subsys[i];
787 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800788 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800789 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700790 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700791 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800792 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793 } else if (bit & removed_bits) {
794 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700795 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
796 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800797 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798 if (ss->bind)
799 ss->bind(ss, dummytop);
800 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700801 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800802 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -0800803 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -0800804 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700805 } else if (bit & final_bits) {
806 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700807 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808 } else {
809 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700810 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700811 }
812 }
813 root->subsys_bits = root->actual_subsys_bits = final_bits;
814 synchronize_rcu();
815
816 return 0;
817}
818
819static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
820{
821 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
822 struct cgroup_subsys *ss;
823
824 mutex_lock(&cgroup_mutex);
825 for_each_subsys(root, ss)
826 seq_printf(seq, ",%s", ss->name);
827 if (test_bit(ROOT_NOPREFIX, &root->flags))
828 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700829 if (strlen(root->release_agent_path))
830 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700831 mutex_unlock(&cgroup_mutex);
832 return 0;
833}
834
835struct cgroup_sb_opts {
836 unsigned long subsys_bits;
837 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700838 char *release_agent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700839};
840
841/* Convert a hierarchy specifier into a bitmask of subsystems and
842 * flags. */
843static int parse_cgroupfs_options(char *data,
844 struct cgroup_sb_opts *opts)
845{
846 char *token, *o = data ?: "all";
Li Zefanf9ab5b52009-06-17 16:26:33 -0700847 unsigned long mask = (unsigned long)-1;
848
849#ifdef CONFIG_CPUSETS
850 mask = ~(1UL << cpuset_subsys_id);
851#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -0700852
853 opts->subsys_bits = 0;
854 opts->flags = 0;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700855 opts->release_agent = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700856
857 while ((token = strsep(&o, ",")) != NULL) {
858 if (!*token)
859 return -EINVAL;
860 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700861 /* Add all non-disabled subsystems */
862 int i;
863 opts->subsys_bits = 0;
864 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
865 struct cgroup_subsys *ss = subsys[i];
866 if (!ss->disabled)
867 opts->subsys_bits |= 1ul << i;
868 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700869 } else if (!strcmp(token, "noprefix")) {
870 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700871 } else if (!strncmp(token, "release_agent=", 14)) {
872 /* Specifying two release agents is forbidden */
873 if (opts->release_agent)
874 return -EINVAL;
875 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
876 if (!opts->release_agent)
877 return -ENOMEM;
878 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
879 opts->release_agent[PATH_MAX - 1] = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700880 } else {
881 struct cgroup_subsys *ss;
882 int i;
883 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
884 ss = subsys[i];
885 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700886 if (!ss->disabled)
887 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700888 break;
889 }
890 }
891 if (i == CGROUP_SUBSYS_COUNT)
892 return -ENOENT;
893 }
894 }
895
Li Zefanf9ab5b52009-06-17 16:26:33 -0700896 /*
897 * Option noprefix was introduced just for backward compatibility
898 * with the old cpuset, so we allow noprefix only if mounting just
899 * the cpuset subsystem.
900 */
901 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
902 (opts->subsys_bits & mask))
903 return -EINVAL;
904
Paul Menageddbcc7e2007-10-18 23:39:30 -0700905 /* We can't have an empty hierarchy */
906 if (!opts->subsys_bits)
907 return -EINVAL;
908
909 return 0;
910}
911
912static int cgroup_remount(struct super_block *sb, int *flags, char *data)
913{
914 int ret = 0;
915 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -0700916 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700917 struct cgroup_sb_opts opts;
918
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200919 lock_kernel();
Paul Menagebd89aab2007-10-18 23:40:44 -0700920 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700921 mutex_lock(&cgroup_mutex);
922
923 /* See what subsystems are wanted */
924 ret = parse_cgroupfs_options(data, &opts);
925 if (ret)
926 goto out_unlock;
927
928 /* Don't allow flags to change at remount */
929 if (opts.flags != root->flags) {
930 ret = -EINVAL;
931 goto out_unlock;
932 }
933
934 ret = rebind_subsystems(root, opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -0700935 if (ret)
936 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700937
938 /* (re)populate subsystem files */
Li Zefan0670e082009-04-02 16:57:30 -0700939 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700940
Paul Menage81a6a5c2007-10-18 23:39:38 -0700941 if (opts.release_agent)
942 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700943 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -0700944 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700945 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700946 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200947 unlock_kernel();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700948 return ret;
949}
950
951static struct super_operations cgroup_ops = {
952 .statfs = simple_statfs,
953 .drop_inode = generic_delete_inode,
954 .show_options = cgroup_show_options,
955 .remount_fs = cgroup_remount,
956};
957
Paul Menagecc31edc2008-10-18 20:28:04 -0700958static void init_cgroup_housekeeping(struct cgroup *cgrp)
959{
960 INIT_LIST_HEAD(&cgrp->sibling);
961 INIT_LIST_HEAD(&cgrp->children);
962 INIT_LIST_HEAD(&cgrp->css_sets);
963 INIT_LIST_HEAD(&cgrp->release_list);
Li Zefan096b7fe2009-07-29 15:04:04 -0700964 INIT_LIST_HEAD(&cgrp->pids_list);
Paul Menagecc31edc2008-10-18 20:28:04 -0700965 init_rwsem(&cgrp->pids_mutex);
966}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700967static void init_cgroup_root(struct cgroupfs_root *root)
968{
Paul Menagebd89aab2007-10-18 23:40:44 -0700969 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700970 INIT_LIST_HEAD(&root->subsys_list);
971 INIT_LIST_HEAD(&root->root_list);
972 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -0700973 cgrp->root = root;
974 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -0700975 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976}
977
978static int cgroup_test_super(struct super_block *sb, void *data)
979{
980 struct cgroupfs_root *new = data;
981 struct cgroupfs_root *root = sb->s_fs_info;
982
983 /* First check subsystems */
984 if (new->subsys_bits != root->subsys_bits)
985 return 0;
986
987 /* Next check flags */
988 if (new->flags != root->flags)
989 return 0;
990
991 return 1;
992}
993
994static int cgroup_set_super(struct super_block *sb, void *data)
995{
996 int ret;
997 struct cgroupfs_root *root = data;
998
999 ret = set_anon_super(sb, NULL);
1000 if (ret)
1001 return ret;
1002
1003 sb->s_fs_info = root;
1004 root->sb = sb;
1005
1006 sb->s_blocksize = PAGE_CACHE_SIZE;
1007 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1008 sb->s_magic = CGROUP_SUPER_MAGIC;
1009 sb->s_op = &cgroup_ops;
1010
1011 return 0;
1012}
1013
1014static int cgroup_get_rootdir(struct super_block *sb)
1015{
1016 struct inode *inode =
1017 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1018 struct dentry *dentry;
1019
1020 if (!inode)
1021 return -ENOMEM;
1022
Paul Menageddbcc7e2007-10-18 23:39:30 -07001023 inode->i_fop = &simple_dir_operations;
1024 inode->i_op = &cgroup_dir_inode_operations;
1025 /* directories start off with i_nlink == 2 (for "." entry) */
1026 inc_nlink(inode);
1027 dentry = d_alloc_root(inode);
1028 if (!dentry) {
1029 iput(inode);
1030 return -ENOMEM;
1031 }
1032 sb->s_root = dentry;
1033 return 0;
1034}
1035
1036static int cgroup_get_sb(struct file_system_type *fs_type,
1037 int flags, const char *unused_dev_name,
1038 void *data, struct vfsmount *mnt)
1039{
1040 struct cgroup_sb_opts opts;
1041 int ret = 0;
1042 struct super_block *sb;
1043 struct cgroupfs_root *root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001044 struct list_head tmp_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045
1046 /* First find the desired set of subsystems */
1047 ret = parse_cgroupfs_options(data, &opts);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001048 if (ret) {
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001049 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001050 return ret;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001051 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052
1053 root = kzalloc(sizeof(*root), GFP_KERNEL);
Li Zefanf7770732008-02-23 15:24:10 -08001054 if (!root) {
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001055 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056 return -ENOMEM;
Li Zefanf7770732008-02-23 15:24:10 -08001057 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058
1059 init_cgroup_root(root);
1060 root->subsys_bits = opts.subsys_bits;
1061 root->flags = opts.flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001062 if (opts.release_agent) {
1063 strcpy(root->release_agent_path, opts.release_agent);
1064 kfree(opts.release_agent);
1065 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066
1067 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
1068
1069 if (IS_ERR(sb)) {
1070 kfree(root);
1071 return PTR_ERR(sb);
1072 }
1073
1074 if (sb->s_fs_info != root) {
1075 /* Reusing an existing superblock */
1076 BUG_ON(sb->s_root == NULL);
1077 kfree(root);
1078 root = NULL;
1079 } else {
1080 /* New superblock */
Li Zefanc12f65d2009-01-07 18:07:42 -08001081 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001082 struct inode *inode;
Li Zefan28fd5df2008-04-29 01:00:13 -07001083 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001084
1085 BUG_ON(sb->s_root != NULL);
1086
1087 ret = cgroup_get_rootdir(sb);
1088 if (ret)
1089 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001090 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091
Paul Menage817929e2007-10-18 23:39:36 -07001092 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093 mutex_lock(&cgroup_mutex);
1094
Paul Menage817929e2007-10-18 23:39:36 -07001095 /*
1096 * We're accessing css_set_count without locking
1097 * css_set_lock here, but that's OK - it can only be
1098 * increased by someone holding cgroup_lock, and
1099 * that's us. The worst that can happen is that we
1100 * have some link structures left over
1101 */
1102 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1103 if (ret) {
1104 mutex_unlock(&cgroup_mutex);
1105 mutex_unlock(&inode->i_mutex);
1106 goto drop_new_super;
1107 }
1108
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109 ret = rebind_subsystems(root, root->subsys_bits);
1110 if (ret == -EBUSY) {
1111 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001112 mutex_unlock(&inode->i_mutex);
Li Zefan20ca9b32008-12-23 13:57:14 -08001113 goto free_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114 }
1115
1116 /* EBUSY should be the only error here */
1117 BUG_ON(ret);
1118
1119 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001120 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001121
Li Zefanc12f65d2009-01-07 18:07:42 -08001122 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001123 root->top_cgroup.dentry = sb->s_root;
1124
Paul Menage817929e2007-10-18 23:39:36 -07001125 /* Link the top cgroup in this hierarchy into all
1126 * the css_set objects */
1127 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001128 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1129 struct hlist_head *hhead = &css_set_table[i];
1130 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001131 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001132
Li Zefanc12f65d2009-01-07 18:07:42 -08001133 hlist_for_each_entry(cg, node, hhead, hlist)
1134 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001135 }
Paul Menage817929e2007-10-18 23:39:36 -07001136 write_unlock(&css_set_lock);
1137
1138 free_cg_links(&tmp_cg_links);
1139
Li Zefanc12f65d2009-01-07 18:07:42 -08001140 BUG_ON(!list_empty(&root_cgrp->sibling));
1141 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001142 BUG_ON(root->number_of_cgroups != 1);
1143
Li Zefanc12f65d2009-01-07 18:07:42 -08001144 cgroup_populate_dir(root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001145 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001146 mutex_unlock(&cgroup_mutex);
1147 }
1148
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001149 simple_set_mnt(mnt, sb);
1150 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001151
Li Zefan20ca9b32008-12-23 13:57:14 -08001152 free_cg_links:
1153 free_cg_links(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001154 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001155 deactivate_locked_super(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001156 return ret;
1157}
1158
1159static void cgroup_kill_sb(struct super_block *sb) {
1160 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001161 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001162 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001163 struct cg_cgroup_link *link;
1164 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001165
1166 BUG_ON(!root);
1167
1168 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001169 BUG_ON(!list_empty(&cgrp->children));
1170 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001171
1172 mutex_lock(&cgroup_mutex);
1173
1174 /* Rebind all subsystems back to the default hierarchy */
1175 ret = rebind_subsystems(root, 0);
1176 /* Shouldn't be able to fail ... */
1177 BUG_ON(ret);
1178
Paul Menage817929e2007-10-18 23:39:36 -07001179 /*
1180 * Release all the links from css_sets to this hierarchy's
1181 * root cgroup
1182 */
1183 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001184
1185 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1186 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001187 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001188 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001189 kfree(link);
1190 }
1191 write_unlock(&css_set_lock);
1192
Paul Menage839ec542009-01-29 14:25:22 -08001193 if (!list_empty(&root->root_list)) {
1194 list_del(&root->root_list);
1195 root_count--;
1196 }
Li Zefane5f6a862009-01-07 18:07:41 -08001197
Paul Menageddbcc7e2007-10-18 23:39:30 -07001198 mutex_unlock(&cgroup_mutex);
1199
Paul Menageddbcc7e2007-10-18 23:39:30 -07001200 kill_litter_super(sb);
Li Zefan67e055d2009-02-18 14:48:20 -08001201 kfree(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001202}
1203
1204static struct file_system_type cgroup_fs_type = {
1205 .name = "cgroup",
1206 .get_sb = cgroup_get_sb,
1207 .kill_sb = cgroup_kill_sb,
1208};
1209
Paul Menagebd89aab2007-10-18 23:40:44 -07001210static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001211{
1212 return dentry->d_fsdata;
1213}
1214
1215static inline struct cftype *__d_cft(struct dentry *dentry)
1216{
1217 return dentry->d_fsdata;
1218}
1219
Li Zefana043e3b2008-02-23 15:24:09 -08001220/**
1221 * cgroup_path - generate the path of a cgroup
1222 * @cgrp: the cgroup in question
1223 * @buf: the buffer to write the path into
1224 * @buflen: the length of the buffer
1225 *
Paul Menagea47295e2009-01-07 18:07:44 -08001226 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1227 * reference. Writes path of cgroup into buf. Returns 0 on success,
1228 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001229 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001230int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001231{
1232 char *start;
Paul Menagea47295e2009-01-07 18:07:44 -08001233 struct dentry *dentry = rcu_dereference(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001234
Paul Menagea47295e2009-01-07 18:07:44 -08001235 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001236 /*
1237 * Inactive subsystems have no dentry for their root
1238 * cgroup
1239 */
1240 strcpy(buf, "/");
1241 return 0;
1242 }
1243
1244 start = buf + buflen;
1245
1246 *--start = '\0';
1247 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001248 int len = dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001249 if ((start -= len) < buf)
1250 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001251 memcpy(start, cgrp->dentry->d_name.name, len);
1252 cgrp = cgrp->parent;
1253 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001254 break;
Paul Menagea47295e2009-01-07 18:07:44 -08001255 dentry = rcu_dereference(cgrp->dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001256 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001257 continue;
1258 if (--start < buf)
1259 return -ENAMETOOLONG;
1260 *start = '/';
1261 }
1262 memmove(buf, start, buf + buflen - start);
1263 return 0;
1264}
1265
Paul Menagebbcb81d2007-10-18 23:39:32 -07001266/*
1267 * Return the first subsystem attached to a cgroup's hierarchy, and
1268 * its subsystem id.
1269 */
1270
Paul Menagebd89aab2007-10-18 23:40:44 -07001271static void get_first_subsys(const struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001272 struct cgroup_subsys_state **css, int *subsys_id)
1273{
Paul Menagebd89aab2007-10-18 23:40:44 -07001274 const struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001275 const struct cgroup_subsys *test_ss;
1276 BUG_ON(list_empty(&root->subsys_list));
1277 test_ss = list_entry(root->subsys_list.next,
1278 struct cgroup_subsys, sibling);
1279 if (css) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001280 *css = cgrp->subsys[test_ss->subsys_id];
Paul Menagebbcb81d2007-10-18 23:39:32 -07001281 BUG_ON(!*css);
1282 }
1283 if (subsys_id)
1284 *subsys_id = test_ss->subsys_id;
1285}
1286
Li Zefana043e3b2008-02-23 15:24:09 -08001287/**
1288 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1289 * @cgrp: the cgroup the task is attaching to
1290 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001291 *
Li Zefana043e3b2008-02-23 15:24:09 -08001292 * Call holding cgroup_mutex. May take task_lock of
1293 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001294 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001295int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001296{
1297 int retval = 0;
1298 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07001299 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001300 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001301 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001302 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001303 int subsys_id;
1304
Paul Menagebd89aab2007-10-18 23:40:44 -07001305 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001306
1307 /* Nothing to do if the task is already in that cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -07001308 oldcgrp = task_cgroup(tsk, subsys_id);
1309 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001310 return 0;
1311
1312 for_each_subsys(root, ss) {
1313 if (ss->can_attach) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001314 retval = ss->can_attach(ss, cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001315 if (retval)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001316 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001317 }
1318 }
1319
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001320 task_lock(tsk);
1321 cg = tsk->cgroups;
1322 get_css_set(cg);
1323 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001324 /*
1325 * Locate or allocate a new css_set for this task,
1326 * based on its final set of cgroups
1327 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001328 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001329 put_css_set(cg);
Paul Jacksone18f6312008-02-07 00:13:44 -08001330 if (!newcg)
Paul Menage817929e2007-10-18 23:39:36 -07001331 return -ENOMEM;
Paul Menage817929e2007-10-18 23:39:36 -07001332
Paul Menagebbcb81d2007-10-18 23:39:32 -07001333 task_lock(tsk);
1334 if (tsk->flags & PF_EXITING) {
1335 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001336 put_css_set(newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001337 return -ESRCH;
1338 }
Paul Menage817929e2007-10-18 23:39:36 -07001339 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001340 task_unlock(tsk);
1341
Paul Menage817929e2007-10-18 23:39:36 -07001342 /* Update the css_set linked lists if we're using them */
1343 write_lock(&css_set_lock);
1344 if (!list_empty(&tsk->cg_list)) {
1345 list_del(&tsk->cg_list);
1346 list_add(&tsk->cg_list, &newcg->tasks);
1347 }
1348 write_unlock(&css_set_lock);
1349
Paul Menagebbcb81d2007-10-18 23:39:32 -07001350 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001351 if (ss->attach)
Paul Menagebd89aab2007-10-18 23:40:44 -07001352 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001353 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001354 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001355 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001356 put_css_set(cg);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001357
1358 /*
1359 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1360 * is no longer empty.
1361 */
1362 cgroup_wakeup_rmdir_waiters(cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001363 return 0;
1364}
1365
1366/*
Paul Menageaf351022008-07-25 01:47:01 -07001367 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1368 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001369 */
Paul Menageaf351022008-07-25 01:47:01 -07001370static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001371{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001372 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001373 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001374 int ret;
1375
Paul Menagebbcb81d2007-10-18 23:39:32 -07001376 if (pid) {
1377 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001378 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001379 if (!tsk || tsk->flags & PF_EXITING) {
1380 rcu_read_unlock();
1381 return -ESRCH;
1382 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001383
David Howellsc69e8d92008-11-14 10:39:19 +11001384 tcred = __task_cred(tsk);
1385 if (cred->euid &&
1386 cred->euid != tcred->uid &&
1387 cred->euid != tcred->suid) {
1388 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001389 return -EACCES;
1390 }
David Howellsc69e8d92008-11-14 10:39:19 +11001391 get_task_struct(tsk);
1392 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001393 } else {
1394 tsk = current;
1395 get_task_struct(tsk);
1396 }
1397
Cliff Wickman956db3c2008-02-07 00:14:43 -08001398 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001399 put_task_struct(tsk);
1400 return ret;
1401}
1402
Paul Menageaf351022008-07-25 01:47:01 -07001403static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1404{
1405 int ret;
1406 if (!cgroup_lock_live_group(cgrp))
1407 return -ENODEV;
1408 ret = attach_task_by_pid(cgrp, pid);
1409 cgroup_unlock();
1410 return ret;
1411}
1412
Paul Menageddbcc7e2007-10-18 23:39:30 -07001413/* The various types of files and directories in a cgroup file system */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414enum cgroup_filetype {
1415 FILE_ROOT,
1416 FILE_DIR,
1417 FILE_TASKLIST,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001418 FILE_NOTIFY_ON_RELEASE,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001419 FILE_RELEASE_AGENT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001420};
1421
Paul Menagee788e062008-07-25 01:46:59 -07001422/**
1423 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1424 * @cgrp: the cgroup to be checked for liveness
1425 *
Paul Menage84eea842008-07-25 01:47:00 -07001426 * On success, returns true; the lock should be later released with
1427 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07001428 */
Paul Menage84eea842008-07-25 01:47:00 -07001429bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07001430{
1431 mutex_lock(&cgroup_mutex);
1432 if (cgroup_is_removed(cgrp)) {
1433 mutex_unlock(&cgroup_mutex);
1434 return false;
1435 }
1436 return true;
1437}
1438
1439static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1440 const char *buffer)
1441{
1442 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1443 if (!cgroup_lock_live_group(cgrp))
1444 return -ENODEV;
1445 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001446 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001447 return 0;
1448}
1449
1450static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1451 struct seq_file *seq)
1452{
1453 if (!cgroup_lock_live_group(cgrp))
1454 return -ENODEV;
1455 seq_puts(seq, cgrp->root->release_agent_path);
1456 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001457 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001458 return 0;
1459}
1460
Paul Menage84eea842008-07-25 01:47:00 -07001461/* A buffer size big enough for numbers or short strings */
1462#define CGROUP_LOCAL_BUFFER_SIZE 64
1463
Paul Menagee73d2c62008-04-29 01:00:06 -07001464static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001465 struct file *file,
1466 const char __user *userbuf,
1467 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001468{
Paul Menage84eea842008-07-25 01:47:00 -07001469 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001470 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001471 char *end;
1472
1473 if (!nbytes)
1474 return -EINVAL;
1475 if (nbytes >= sizeof(buffer))
1476 return -E2BIG;
1477 if (copy_from_user(buffer, userbuf, nbytes))
1478 return -EFAULT;
1479
1480 buffer[nbytes] = 0; /* nul-terminate */
Paul Menageb7269df2008-04-29 00:59:59 -07001481 strstrip(buffer);
Paul Menagee73d2c62008-04-29 01:00:06 -07001482 if (cft->write_u64) {
1483 u64 val = simple_strtoull(buffer, &end, 0);
1484 if (*end)
1485 return -EINVAL;
1486 retval = cft->write_u64(cgrp, cft, val);
1487 } else {
1488 s64 val = simple_strtoll(buffer, &end, 0);
1489 if (*end)
1490 return -EINVAL;
1491 retval = cft->write_s64(cgrp, cft, val);
1492 }
Paul Menage355e0c42007-10-18 23:39:33 -07001493 if (!retval)
1494 retval = nbytes;
1495 return retval;
1496}
1497
Paul Menagedb3b1492008-07-25 01:46:58 -07001498static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1499 struct file *file,
1500 const char __user *userbuf,
1501 size_t nbytes, loff_t *unused_ppos)
1502{
Paul Menage84eea842008-07-25 01:47:00 -07001503 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001504 int retval = 0;
1505 size_t max_bytes = cft->max_write_len;
1506 char *buffer = local_buffer;
1507
1508 if (!max_bytes)
1509 max_bytes = sizeof(local_buffer) - 1;
1510 if (nbytes >= max_bytes)
1511 return -E2BIG;
1512 /* Allocate a dynamic buffer if we need one */
1513 if (nbytes >= sizeof(local_buffer)) {
1514 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1515 if (buffer == NULL)
1516 return -ENOMEM;
1517 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001518 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1519 retval = -EFAULT;
1520 goto out;
1521 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001522
1523 buffer[nbytes] = 0; /* nul-terminate */
1524 strstrip(buffer);
1525 retval = cft->write_string(cgrp, cft, buffer);
1526 if (!retval)
1527 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001528out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001529 if (buffer != local_buffer)
1530 kfree(buffer);
1531 return retval;
1532}
1533
Paul Menageddbcc7e2007-10-18 23:39:30 -07001534static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1535 size_t nbytes, loff_t *ppos)
1536{
1537 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001538 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539
Li Zefan75139b82009-01-07 18:07:33 -08001540 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001542 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001543 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001544 if (cft->write_u64 || cft->write_s64)
1545 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001546 if (cft->write_string)
1547 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001548 if (cft->trigger) {
1549 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1550 return ret ? ret : nbytes;
1551 }
Paul Menage355e0c42007-10-18 23:39:33 -07001552 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553}
1554
Paul Menagef4c753b2008-04-29 00:59:56 -07001555static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1556 struct file *file,
1557 char __user *buf, size_t nbytes,
1558 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559{
Paul Menage84eea842008-07-25 01:47:00 -07001560 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001561 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1563
1564 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1565}
1566
Paul Menagee73d2c62008-04-29 01:00:06 -07001567static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1568 struct file *file,
1569 char __user *buf, size_t nbytes,
1570 loff_t *ppos)
1571{
Paul Menage84eea842008-07-25 01:47:00 -07001572 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001573 s64 val = cft->read_s64(cgrp, cft);
1574 int len = sprintf(tmp, "%lld\n", (long long) val);
1575
1576 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1577}
1578
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1580 size_t nbytes, loff_t *ppos)
1581{
1582 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001583 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584
Li Zefan75139b82009-01-07 18:07:33 -08001585 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586 return -ENODEV;
1587
1588 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001589 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001590 if (cft->read_u64)
1591 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001592 if (cft->read_s64)
1593 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001594 return -EINVAL;
1595}
1596
Paul Menage91796562008-04-29 01:00:01 -07001597/*
1598 * seqfile ops/methods for returning structured data. Currently just
1599 * supports string->u64 maps, but can be extended in future.
1600 */
1601
1602struct cgroup_seqfile_state {
1603 struct cftype *cft;
1604 struct cgroup *cgroup;
1605};
1606
1607static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1608{
1609 struct seq_file *sf = cb->state;
1610 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1611}
1612
1613static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1614{
1615 struct cgroup_seqfile_state *state = m->private;
1616 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001617 if (cft->read_map) {
1618 struct cgroup_map_cb cb = {
1619 .fill = cgroup_map_add,
1620 .state = m,
1621 };
1622 return cft->read_map(state->cgroup, cft, &cb);
1623 }
1624 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001625}
1626
Adrian Bunk96930a62008-07-25 19:46:21 -07001627static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07001628{
1629 struct seq_file *seq = file->private_data;
1630 kfree(seq->private);
1631 return single_release(inode, file);
1632}
1633
1634static struct file_operations cgroup_seqfile_operations = {
1635 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07001636 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07001637 .llseek = seq_lseek,
1638 .release = cgroup_seqfile_release,
1639};
1640
Paul Menageddbcc7e2007-10-18 23:39:30 -07001641static int cgroup_file_open(struct inode *inode, struct file *file)
1642{
1643 int err;
1644 struct cftype *cft;
1645
1646 err = generic_file_open(inode, file);
1647 if (err)
1648 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08001650
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001651 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001652 struct cgroup_seqfile_state *state =
1653 kzalloc(sizeof(*state), GFP_USER);
1654 if (!state)
1655 return -ENOMEM;
1656 state->cft = cft;
1657 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1658 file->f_op = &cgroup_seqfile_operations;
1659 err = single_open(file, cgroup_seqfile_show, state);
1660 if (err < 0)
1661 kfree(state);
1662 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663 err = cft->open(inode, file);
1664 else
1665 err = 0;
1666
1667 return err;
1668}
1669
1670static int cgroup_file_release(struct inode *inode, struct file *file)
1671{
1672 struct cftype *cft = __d_cft(file->f_dentry);
1673 if (cft->release)
1674 return cft->release(inode, file);
1675 return 0;
1676}
1677
1678/*
1679 * cgroup_rename - Only allow simple rename of directories in place.
1680 */
1681static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1682 struct inode *new_dir, struct dentry *new_dentry)
1683{
1684 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1685 return -ENOTDIR;
1686 if (new_dentry->d_inode)
1687 return -EEXIST;
1688 if (old_dir != new_dir)
1689 return -EIO;
1690 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1691}
1692
1693static struct file_operations cgroup_file_operations = {
1694 .read = cgroup_file_read,
1695 .write = cgroup_file_write,
1696 .llseek = generic_file_llseek,
1697 .open = cgroup_file_open,
1698 .release = cgroup_file_release,
1699};
1700
1701static struct inode_operations cgroup_dir_inode_operations = {
1702 .lookup = simple_lookup,
1703 .mkdir = cgroup_mkdir,
1704 .rmdir = cgroup_rmdir,
1705 .rename = cgroup_rename,
1706};
1707
Li Zefan099fca32009-04-02 16:57:29 -07001708static int cgroup_create_file(struct dentry *dentry, mode_t mode,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001709 struct super_block *sb)
1710{
Al Viro3ba13d12009-02-20 06:02:22 +00001711 static const struct dentry_operations cgroup_dops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001712 .d_iput = cgroup_diput,
1713 };
1714
1715 struct inode *inode;
1716
1717 if (!dentry)
1718 return -ENOENT;
1719 if (dentry->d_inode)
1720 return -EEXIST;
1721
1722 inode = cgroup_new_inode(mode, sb);
1723 if (!inode)
1724 return -ENOMEM;
1725
1726 if (S_ISDIR(mode)) {
1727 inode->i_op = &cgroup_dir_inode_operations;
1728 inode->i_fop = &simple_dir_operations;
1729
1730 /* start off with i_nlink == 2 (for "." entry) */
1731 inc_nlink(inode);
1732
1733 /* start with the directory inode held, so that we can
1734 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07001735 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736 } else if (S_ISREG(mode)) {
1737 inode->i_size = 0;
1738 inode->i_fop = &cgroup_file_operations;
1739 }
1740 dentry->d_op = &cgroup_dops;
1741 d_instantiate(dentry, inode);
1742 dget(dentry); /* Extra count - pin the dentry in core */
1743 return 0;
1744}
1745
1746/*
Li Zefana043e3b2008-02-23 15:24:09 -08001747 * cgroup_create_dir - create a directory for an object.
1748 * @cgrp: the cgroup we create the directory for. It must have a valid
1749 * ->parent field. And we are going to fill its ->dentry field.
1750 * @dentry: dentry of the new cgroup
1751 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001752 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001753static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07001754 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001755{
1756 struct dentry *parent;
1757 int error = 0;
1758
Paul Menagebd89aab2007-10-18 23:40:44 -07001759 parent = cgrp->parent->dentry;
1760 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001762 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001763 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08001764 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 dget(dentry);
1766 }
1767 dput(dentry);
1768
1769 return error;
1770}
1771
Li Zefan099fca32009-04-02 16:57:29 -07001772/**
1773 * cgroup_file_mode - deduce file mode of a control file
1774 * @cft: the control file in question
1775 *
1776 * returns cft->mode if ->mode is not 0
1777 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1778 * returns S_IRUGO if it has only a read handler
1779 * returns S_IWUSR if it has only a write hander
1780 */
1781static mode_t cgroup_file_mode(const struct cftype *cft)
1782{
1783 mode_t mode = 0;
1784
1785 if (cft->mode)
1786 return cft->mode;
1787
1788 if (cft->read || cft->read_u64 || cft->read_s64 ||
1789 cft->read_map || cft->read_seq_string)
1790 mode |= S_IRUGO;
1791
1792 if (cft->write || cft->write_u64 || cft->write_s64 ||
1793 cft->write_string || cft->trigger)
1794 mode |= S_IWUSR;
1795
1796 return mode;
1797}
1798
Paul Menagebd89aab2007-10-18 23:40:44 -07001799int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001800 struct cgroup_subsys *subsys,
1801 const struct cftype *cft)
1802{
Paul Menagebd89aab2007-10-18 23:40:44 -07001803 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001804 struct dentry *dentry;
1805 int error;
Li Zefan099fca32009-04-02 16:57:29 -07001806 mode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001807
1808 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07001809 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810 strcpy(name, subsys->name);
1811 strcat(name, ".");
1812 }
1813 strcat(name, cft->name);
1814 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1815 dentry = lookup_one_len(name, dir, strlen(name));
1816 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07001817 mode = cgroup_file_mode(cft);
1818 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07001819 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820 if (!error)
1821 dentry->d_fsdata = (void *)cft;
1822 dput(dentry);
1823 } else
1824 error = PTR_ERR(dentry);
1825 return error;
1826}
1827
Paul Menagebd89aab2007-10-18 23:40:44 -07001828int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001829 struct cgroup_subsys *subsys,
1830 const struct cftype cft[],
1831 int count)
1832{
1833 int i, err;
1834 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001835 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001836 if (err)
1837 return err;
1838 }
1839 return 0;
1840}
1841
Li Zefana043e3b2008-02-23 15:24:09 -08001842/**
1843 * cgroup_task_count - count the number of tasks in a cgroup.
1844 * @cgrp: the cgroup in question
1845 *
1846 * Return the number of tasks in the cgroup.
1847 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001848int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001849{
1850 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001851 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001852
Paul Menage817929e2007-10-18 23:39:36 -07001853 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001854 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07001855 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07001856 }
1857 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001858 return count;
1859}
1860
1861/*
Paul Menage817929e2007-10-18 23:39:36 -07001862 * Advance a list_head iterator. The iterator should be positioned at
1863 * the start of a css_set
1864 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001865static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001866 struct cgroup_iter *it)
1867{
1868 struct list_head *l = it->cg_link;
1869 struct cg_cgroup_link *link;
1870 struct css_set *cg;
1871
1872 /* Advance to the next non-empty css_set */
1873 do {
1874 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07001875 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001876 it->cg_link = NULL;
1877 return;
1878 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001879 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001880 cg = link->cg;
1881 } while (list_empty(&cg->tasks));
1882 it->cg_link = l;
1883 it->task = cg->tasks.next;
1884}
1885
Cliff Wickman31a7df02008-02-07 00:14:42 -08001886/*
1887 * To reduce the fork() overhead for systems that are not actually
1888 * using their cgroups capability, we don't maintain the lists running
1889 * through each css_set to its tasks until we see the list actually
1890 * used - in other words after the first call to cgroup_iter_start().
1891 *
1892 * The tasklist_lock is not held here, as do_each_thread() and
1893 * while_each_thread() are protected by RCU.
1894 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07001895static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08001896{
1897 struct task_struct *p, *g;
1898 write_lock(&css_set_lock);
1899 use_task_css_set_links = 1;
1900 do_each_thread(g, p) {
1901 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08001902 /*
1903 * We should check if the process is exiting, otherwise
1904 * it will race with cgroup_exit() in that the list
1905 * entry won't be deleted though the process has exited.
1906 */
1907 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08001908 list_add(&p->cg_list, &p->cgroups->tasks);
1909 task_unlock(p);
1910 } while_each_thread(g, p);
1911 write_unlock(&css_set_lock);
1912}
1913
Paul Menagebd89aab2007-10-18 23:40:44 -07001914void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001915{
1916 /*
1917 * The first time anyone tries to iterate across a cgroup,
1918 * we need to enable the list linking each css_set to its
1919 * tasks, and fix up all existing tasks.
1920 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08001921 if (!use_task_css_set_links)
1922 cgroup_enable_task_cg_lists();
1923
Paul Menage817929e2007-10-18 23:39:36 -07001924 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001925 it->cg_link = &cgrp->css_sets;
1926 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001927}
1928
Paul Menagebd89aab2007-10-18 23:40:44 -07001929struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001930 struct cgroup_iter *it)
1931{
1932 struct task_struct *res;
1933 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001934 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07001935
1936 /* If the iterator cg is NULL, we have no tasks */
1937 if (!it->cg_link)
1938 return NULL;
1939 res = list_entry(l, struct task_struct, cg_list);
1940 /* Advance iterator to find next entry */
1941 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001942 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
1943 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07001944 /* We reached the end of this task list - move on to
1945 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07001946 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001947 } else {
1948 it->task = l;
1949 }
1950 return res;
1951}
1952
Paul Menagebd89aab2007-10-18 23:40:44 -07001953void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001954{
1955 read_unlock(&css_set_lock);
1956}
1957
Cliff Wickman31a7df02008-02-07 00:14:42 -08001958static inline int started_after_time(struct task_struct *t1,
1959 struct timespec *time,
1960 struct task_struct *t2)
1961{
1962 int start_diff = timespec_compare(&t1->start_time, time);
1963 if (start_diff > 0) {
1964 return 1;
1965 } else if (start_diff < 0) {
1966 return 0;
1967 } else {
1968 /*
1969 * Arbitrarily, if two processes started at the same
1970 * time, we'll say that the lower pointer value
1971 * started first. Note that t2 may have exited by now
1972 * so this may not be a valid pointer any longer, but
1973 * that's fine - it still serves to distinguish
1974 * between two tasks started (effectively) simultaneously.
1975 */
1976 return t1 > t2;
1977 }
1978}
1979
1980/*
1981 * This function is a callback from heap_insert() and is used to order
1982 * the heap.
1983 * In this case we order the heap in descending task start time.
1984 */
1985static inline int started_after(void *p1, void *p2)
1986{
1987 struct task_struct *t1 = p1;
1988 struct task_struct *t2 = p2;
1989 return started_after_time(t1, &t2->start_time, t2);
1990}
1991
1992/**
1993 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1994 * @scan: struct cgroup_scanner containing arguments for the scan
1995 *
1996 * Arguments include pointers to callback functions test_task() and
1997 * process_task().
1998 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1999 * and if it returns true, call process_task() for it also.
2000 * The test_task pointer may be NULL, meaning always true (select all tasks).
2001 * Effectively duplicates cgroup_iter_{start,next,end}()
2002 * but does not lock css_set_lock for the call to process_task().
2003 * The struct cgroup_scanner may be embedded in any structure of the caller's
2004 * creation.
2005 * It is guaranteed that process_task() will act on every task that
2006 * is a member of the cgroup for the duration of this call. This
2007 * function may or may not call process_task() for tasks that exit
2008 * or move to a different cgroup during the call, or are forked or
2009 * move into the cgroup during the call.
2010 *
2011 * Note that test_task() may be called with locks held, and may in some
2012 * situations be called multiple times for the same task, so it should
2013 * be cheap.
2014 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2015 * pre-allocated and will be used for heap operations (and its "gt" member will
2016 * be overwritten), else a temporary heap will be used (allocation of which
2017 * may cause this function to fail).
2018 */
2019int cgroup_scan_tasks(struct cgroup_scanner *scan)
2020{
2021 int retval, i;
2022 struct cgroup_iter it;
2023 struct task_struct *p, *dropped;
2024 /* Never dereference latest_task, since it's not refcounted */
2025 struct task_struct *latest_task = NULL;
2026 struct ptr_heap tmp_heap;
2027 struct ptr_heap *heap;
2028 struct timespec latest_time = { 0, 0 };
2029
2030 if (scan->heap) {
2031 /* The caller supplied our heap and pre-allocated its memory */
2032 heap = scan->heap;
2033 heap->gt = &started_after;
2034 } else {
2035 /* We need to allocate our own heap memory */
2036 heap = &tmp_heap;
2037 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2038 if (retval)
2039 /* cannot allocate the heap */
2040 return retval;
2041 }
2042
2043 again:
2044 /*
2045 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2046 * to determine which are of interest, and using the scanner's
2047 * "process_task" callback to process any of them that need an update.
2048 * Since we don't want to hold any locks during the task updates,
2049 * gather tasks to be processed in a heap structure.
2050 * The heap is sorted by descending task start time.
2051 * If the statically-sized heap fills up, we overflow tasks that
2052 * started later, and in future iterations only consider tasks that
2053 * started after the latest task in the previous pass. This
2054 * guarantees forward progress and that we don't miss any tasks.
2055 */
2056 heap->size = 0;
2057 cgroup_iter_start(scan->cg, &it);
2058 while ((p = cgroup_iter_next(scan->cg, &it))) {
2059 /*
2060 * Only affect tasks that qualify per the caller's callback,
2061 * if he provided one
2062 */
2063 if (scan->test_task && !scan->test_task(p, scan))
2064 continue;
2065 /*
2066 * Only process tasks that started after the last task
2067 * we processed
2068 */
2069 if (!started_after_time(p, &latest_time, latest_task))
2070 continue;
2071 dropped = heap_insert(heap, p);
2072 if (dropped == NULL) {
2073 /*
2074 * The new task was inserted; the heap wasn't
2075 * previously full
2076 */
2077 get_task_struct(p);
2078 } else if (dropped != p) {
2079 /*
2080 * The new task was inserted, and pushed out a
2081 * different task
2082 */
2083 get_task_struct(p);
2084 put_task_struct(dropped);
2085 }
2086 /*
2087 * Else the new task was newer than anything already in
2088 * the heap and wasn't inserted
2089 */
2090 }
2091 cgroup_iter_end(scan->cg, &it);
2092
2093 if (heap->size) {
2094 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002095 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002096 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002097 latest_time = q->start_time;
2098 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002099 }
2100 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002101 scan->process_task(q, scan);
2102 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002103 }
2104 /*
2105 * If we had to process any tasks at all, scan again
2106 * in case some of them were in the middle of forking
2107 * children that didn't get processed.
2108 * Not the most efficient way to do it, but it avoids
2109 * having to take callback_mutex in the fork path
2110 */
2111 goto again;
2112 }
2113 if (heap == &tmp_heap)
2114 heap_free(&tmp_heap);
2115 return 0;
2116}
2117
Paul Menage817929e2007-10-18 23:39:36 -07002118/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07002119 * Stuff for reading the 'tasks' file.
2120 *
2121 * Reading this file can return large amounts of data if a cgroup has
2122 * *lots* of attached tasks. So it may need several calls to read(),
2123 * but we cannot guarantee that the information we produce is correct
2124 * unless we produce it entirely atomically.
2125 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002126 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002127
2128/*
2129 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
Paul Menagebd89aab2007-10-18 23:40:44 -07002130 * 'cgrp'. Return actual number of pids loaded. No need to
Paul Menagebbcb81d2007-10-18 23:39:32 -07002131 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2132 * read section, so the css_set can't go away, and is
2133 * immutable after creation.
2134 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002135static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002136{
Gowrishankar Me7b80bb2009-01-07 18:07:43 -08002137 int n = 0, pid;
Paul Menage817929e2007-10-18 23:39:36 -07002138 struct cgroup_iter it;
2139 struct task_struct *tsk;
Paul Menagebd89aab2007-10-18 23:40:44 -07002140 cgroup_iter_start(cgrp, &it);
2141 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Paul Menage817929e2007-10-18 23:39:36 -07002142 if (unlikely(n == npids))
2143 break;
Gowrishankar Me7b80bb2009-01-07 18:07:43 -08002144 pid = task_pid_vnr(tsk);
2145 if (pid > 0)
2146 pidarray[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07002147 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002148 cgroup_iter_end(cgrp, &it);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002149 return n;
2150}
2151
Balbir Singh846c7bb2007-10-18 23:39:44 -07002152/**
Li Zefana043e3b2008-02-23 15:24:09 -08002153 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002154 * @stats: cgroupstats to fill information into
2155 * @dentry: A dentry entry belonging to the cgroup for which stats have
2156 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002157 *
2158 * Build and fill cgroupstats so that taskstats can export it to user
2159 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002160 */
2161int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2162{
2163 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002164 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002165 struct cgroup_iter it;
2166 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002167
Balbir Singh846c7bb2007-10-18 23:39:44 -07002168 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002169 * Validate dentry by checking the superblock operations,
2170 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002171 */
Li Zefan33d283b2008-11-19 15:36:48 -08002172 if (dentry->d_sb->s_op != &cgroup_ops ||
2173 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002174 goto err;
2175
2176 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002177 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002178
Paul Menagebd89aab2007-10-18 23:40:44 -07002179 cgroup_iter_start(cgrp, &it);
2180 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002181 switch (tsk->state) {
2182 case TASK_RUNNING:
2183 stats->nr_running++;
2184 break;
2185 case TASK_INTERRUPTIBLE:
2186 stats->nr_sleeping++;
2187 break;
2188 case TASK_UNINTERRUPTIBLE:
2189 stats->nr_uninterruptible++;
2190 break;
2191 case TASK_STOPPED:
2192 stats->nr_stopped++;
2193 break;
2194 default:
2195 if (delayacct_is_task_waiting_on_io(tsk))
2196 stats->nr_io_wait++;
2197 break;
2198 }
2199 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002200 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002201
Balbir Singh846c7bb2007-10-18 23:39:44 -07002202err:
2203 return ret;
2204}
2205
Li Zefan096b7fe2009-07-29 15:04:04 -07002206/*
2207 * Cache pids for all threads in the same pid namespace that are
2208 * opening the same "tasks" file.
2209 */
2210struct cgroup_pids {
2211 /* The node in cgrp->pids_list */
2212 struct list_head list;
2213 /* The cgroup those pids belong to */
2214 struct cgroup *cgrp;
2215 /* The namepsace those pids belong to */
2216 struct pid_namespace *ns;
2217 /* Array of process ids in the cgroup */
2218 pid_t *tasks_pids;
2219 /* How many files are using the this tasks_pids array */
2220 int use_count;
2221 /* Length of the current tasks_pids array */
2222 int length;
2223};
2224
Paul Menagebbcb81d2007-10-18 23:39:32 -07002225static int cmppid(const void *a, const void *b)
2226{
2227 return *(pid_t *)a - *(pid_t *)b;
2228}
2229
Paul Menagecc31edc2008-10-18 20:28:04 -07002230/*
2231 * seq_file methods for the "tasks" file. The seq_file position is the
2232 * next pid to display; the seq_file iterator is a pointer to the pid
2233 * in the cgroup->tasks_pids array.
2234 */
2235
2236static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
2237{
2238 /*
2239 * Initially we receive a position value that corresponds to
2240 * one more than the last pid shown (or 0 on the first call or
2241 * after a seek to the start). Use a binary-search to find the
2242 * next pid to display, if any
2243 */
Li Zefan096b7fe2009-07-29 15:04:04 -07002244 struct cgroup_pids *cp = s->private;
2245 struct cgroup *cgrp = cp->cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07002246 int index = 0, pid = *pos;
2247 int *iter;
2248
2249 down_read(&cgrp->pids_mutex);
2250 if (pid) {
Li Zefan096b7fe2009-07-29 15:04:04 -07002251 int end = cp->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002252
Paul Menagecc31edc2008-10-18 20:28:04 -07002253 while (index < end) {
2254 int mid = (index + end) / 2;
Li Zefan096b7fe2009-07-29 15:04:04 -07002255 if (cp->tasks_pids[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07002256 index = mid;
2257 break;
Li Zefan096b7fe2009-07-29 15:04:04 -07002258 } else if (cp->tasks_pids[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07002259 index = mid + 1;
2260 else
2261 end = mid;
2262 }
2263 }
2264 /* If we're off the end of the array, we're done */
Li Zefan096b7fe2009-07-29 15:04:04 -07002265 if (index >= cp->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07002266 return NULL;
2267 /* Update the abstract position to be the actual pid that we found */
Li Zefan096b7fe2009-07-29 15:04:04 -07002268 iter = cp->tasks_pids + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07002269 *pos = *iter;
2270 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002271}
2272
Paul Menagecc31edc2008-10-18 20:28:04 -07002273static void cgroup_tasks_stop(struct seq_file *s, void *v)
2274{
Li Zefan096b7fe2009-07-29 15:04:04 -07002275 struct cgroup_pids *cp = s->private;
2276 struct cgroup *cgrp = cp->cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07002277 up_read(&cgrp->pids_mutex);
2278}
2279
2280static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
2281{
Li Zefan096b7fe2009-07-29 15:04:04 -07002282 struct cgroup_pids *cp = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07002283 int *p = v;
Li Zefan096b7fe2009-07-29 15:04:04 -07002284 int *end = cp->tasks_pids + cp->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07002285
2286 /*
2287 * Advance to the next pid in the array. If this goes off the
2288 * end, we're done
2289 */
2290 p++;
2291 if (p >= end) {
2292 return NULL;
2293 } else {
2294 *pos = *p;
2295 return p;
2296 }
2297}
2298
2299static int cgroup_tasks_show(struct seq_file *s, void *v)
2300{
2301 return seq_printf(s, "%d\n", *(int *)v);
2302}
2303
2304static struct seq_operations cgroup_tasks_seq_operations = {
2305 .start = cgroup_tasks_start,
2306 .stop = cgroup_tasks_stop,
2307 .next = cgroup_tasks_next,
2308 .show = cgroup_tasks_show,
2309};
2310
Li Zefan096b7fe2009-07-29 15:04:04 -07002311static void release_cgroup_pid_array(struct cgroup_pids *cp)
Paul Menagecc31edc2008-10-18 20:28:04 -07002312{
Li Zefan096b7fe2009-07-29 15:04:04 -07002313 struct cgroup *cgrp = cp->cgrp;
2314
Paul Menagecc31edc2008-10-18 20:28:04 -07002315 down_write(&cgrp->pids_mutex);
Li Zefan096b7fe2009-07-29 15:04:04 -07002316 BUG_ON(!cp->use_count);
2317 if (!--cp->use_count) {
2318 list_del(&cp->list);
2319 put_pid_ns(cp->ns);
2320 kfree(cp->tasks_pids);
2321 kfree(cp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002322 }
2323 up_write(&cgrp->pids_mutex);
2324}
2325
2326static int cgroup_tasks_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002327{
Li Zefan096b7fe2009-07-29 15:04:04 -07002328 struct seq_file *seq;
2329 struct cgroup_pids *cp;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002330
2331 if (!(file->f_mode & FMODE_READ))
2332 return 0;
2333
Li Zefan096b7fe2009-07-29 15:04:04 -07002334 seq = file->private_data;
2335 cp = seq->private;
2336
2337 release_cgroup_pid_array(cp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002338 return seq_release(inode, file);
2339}
2340
2341static struct file_operations cgroup_tasks_operations = {
2342 .read = seq_read,
2343 .llseek = seq_lseek,
2344 .write = cgroup_file_write,
2345 .release = cgroup_tasks_release,
2346};
2347
2348/*
2349 * Handle an open on 'tasks' file. Prepare an array containing the
2350 * process id's of tasks currently attached to the cgroup being opened.
2351 */
2352
2353static int cgroup_tasks_open(struct inode *unused, struct file *file)
2354{
2355 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Li Zefan096b7fe2009-07-29 15:04:04 -07002356 struct pid_namespace *ns = current->nsproxy->pid_ns;
2357 struct cgroup_pids *cp;
Paul Menagecc31edc2008-10-18 20:28:04 -07002358 pid_t *pidarray;
2359 int npids;
2360 int retval;
2361
2362 /* Nothing to do for write-only files */
2363 if (!(file->f_mode & FMODE_READ))
2364 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002365
2366 /*
2367 * If cgroup gets more users after we read count, we won't have
2368 * enough space - tough. This race is indistinguishable to the
2369 * caller from the case that the additional cgroup users didn't
2370 * show up until sometime later on.
2371 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002372 npids = cgroup_task_count(cgrp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002373 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2374 if (!pidarray)
2375 return -ENOMEM;
2376 npids = pid_array_load(pidarray, npids, cgrp);
2377 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002378
Paul Menagecc31edc2008-10-18 20:28:04 -07002379 /*
2380 * Store the array in the cgroup, freeing the old
2381 * array if necessary
2382 */
2383 down_write(&cgrp->pids_mutex);
Li Zefan096b7fe2009-07-29 15:04:04 -07002384
2385 list_for_each_entry(cp, &cgrp->pids_list, list) {
2386 if (ns == cp->ns)
2387 goto found;
2388 }
2389
2390 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2391 if (!cp) {
2392 up_write(&cgrp->pids_mutex);
2393 kfree(pidarray);
2394 return -ENOMEM;
2395 }
2396 cp->cgrp = cgrp;
2397 cp->ns = ns;
2398 get_pid_ns(ns);
2399 list_add(&cp->list, &cgrp->pids_list);
2400found:
2401 kfree(cp->tasks_pids);
2402 cp->tasks_pids = pidarray;
2403 cp->length = npids;
2404 cp->use_count++;
Paul Menagecc31edc2008-10-18 20:28:04 -07002405 up_write(&cgrp->pids_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002406
Paul Menagecc31edc2008-10-18 20:28:04 -07002407 file->f_op = &cgroup_tasks_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002408
Paul Menagecc31edc2008-10-18 20:28:04 -07002409 retval = seq_open(file, &cgroup_tasks_seq_operations);
2410 if (retval) {
Li Zefan096b7fe2009-07-29 15:04:04 -07002411 release_cgroup_pid_array(cp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002412 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002413 }
Li Zefan096b7fe2009-07-29 15:04:04 -07002414 ((struct seq_file *)file->private_data)->private = cp;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002415 return 0;
2416}
2417
Paul Menagebd89aab2007-10-18 23:40:44 -07002418static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002419 struct cftype *cft)
2420{
Paul Menagebd89aab2007-10-18 23:40:44 -07002421 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002422}
2423
Paul Menage6379c102008-07-25 01:47:01 -07002424static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2425 struct cftype *cft,
2426 u64 val)
2427{
2428 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2429 if (val)
2430 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2431 else
2432 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2433 return 0;
2434}
2435
Paul Menagebbcb81d2007-10-18 23:39:32 -07002436/*
2437 * for the common functions, 'private' gives the type of file
2438 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07002439static struct cftype files[] = {
2440 {
2441 .name = "tasks",
2442 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07002443 .write_u64 = cgroup_tasks_write,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002444 .release = cgroup_tasks_release,
2445 .private = FILE_TASKLIST,
Li Zefan099fca32009-04-02 16:57:29 -07002446 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002447 },
2448
2449 {
2450 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002451 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07002452 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002453 .private = FILE_NOTIFY_ON_RELEASE,
2454 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002455};
2456
2457static struct cftype cft_release_agent = {
2458 .name = "release_agent",
Paul Menagee788e062008-07-25 01:46:59 -07002459 .read_seq_string = cgroup_release_agent_show,
2460 .write_string = cgroup_release_agent_write,
2461 .max_write_len = PATH_MAX,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002462 .private = FILE_RELEASE_AGENT,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002463};
2464
Paul Menagebd89aab2007-10-18 23:40:44 -07002465static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002466{
2467 int err;
2468 struct cgroup_subsys *ss;
2469
2470 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002471 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002472
Paul Menagebd89aab2007-10-18 23:40:44 -07002473 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002474 if (err < 0)
2475 return err;
2476
Paul Menagebd89aab2007-10-18 23:40:44 -07002477 if (cgrp == cgrp->top_cgroup) {
2478 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002479 return err;
2480 }
2481
Paul Menagebd89aab2007-10-18 23:40:44 -07002482 for_each_subsys(cgrp->root, ss) {
2483 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002484 return err;
2485 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002486 /* This cgroup is ready now */
2487 for_each_subsys(cgrp->root, ss) {
2488 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2489 /*
2490 * Update id->css pointer and make this css visible from
2491 * CSS ID functions. This pointer will be dereferened
2492 * from RCU-read-side without locks.
2493 */
2494 if (css->id)
2495 rcu_assign_pointer(css->id->css, css);
2496 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002497
2498 return 0;
2499}
2500
2501static void init_cgroup_css(struct cgroup_subsys_state *css,
2502 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002503 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002504{
Paul Menagebd89aab2007-10-18 23:40:44 -07002505 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08002506 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002507 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002508 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002509 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002510 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002511 BUG_ON(cgrp->subsys[ss->subsys_id]);
2512 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002513}
2514
Paul Menage999cd8a2009-01-07 18:08:36 -08002515static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2516{
2517 /* We need to take each hierarchy_mutex in a consistent order */
2518 int i;
2519
2520 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2521 struct cgroup_subsys *ss = subsys[i];
2522 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08002523 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08002524 }
2525}
2526
2527static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2528{
2529 int i;
2530
2531 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2532 struct cgroup_subsys *ss = subsys[i];
2533 if (ss->root == root)
2534 mutex_unlock(&ss->hierarchy_mutex);
2535 }
2536}
2537
Paul Menageddbcc7e2007-10-18 23:39:30 -07002538/*
Li Zefana043e3b2008-02-23 15:24:09 -08002539 * cgroup_create - create a cgroup
2540 * @parent: cgroup that will be parent of the new cgroup
2541 * @dentry: dentry of the new cgroup
2542 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002543 *
Li Zefana043e3b2008-02-23 15:24:09 -08002544 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002545 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002546static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002547 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002548{
Paul Menagebd89aab2007-10-18 23:40:44 -07002549 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002550 struct cgroupfs_root *root = parent->root;
2551 int err = 0;
2552 struct cgroup_subsys *ss;
2553 struct super_block *sb = root->sb;
2554
Paul Menagebd89aab2007-10-18 23:40:44 -07002555 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2556 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002557 return -ENOMEM;
2558
2559 /* Grab a reference on the superblock so the hierarchy doesn't
2560 * get deleted on unmount if there are child cgroups. This
2561 * can be done outside cgroup_mutex, since the sb can't
2562 * disappear while someone has an open control file on the
2563 * fs */
2564 atomic_inc(&sb->s_active);
2565
2566 mutex_lock(&cgroup_mutex);
2567
Paul Menagecc31edc2008-10-18 20:28:04 -07002568 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002569
Paul Menagebd89aab2007-10-18 23:40:44 -07002570 cgrp->parent = parent;
2571 cgrp->root = parent->root;
2572 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002573
Li Zefanb6abdb02008-03-04 14:28:19 -08002574 if (notify_on_release(parent))
2575 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2576
Paul Menageddbcc7e2007-10-18 23:39:30 -07002577 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002578 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002579 if (IS_ERR(css)) {
2580 err = PTR_ERR(css);
2581 goto err_destroy;
2582 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002583 init_cgroup_css(css, ss, cgrp);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002584 if (ss->use_id)
2585 if (alloc_css_id(ss, parent, cgrp))
2586 goto err_destroy;
2587 /* At error, ->destroy() callback has to free assigned ID. */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002588 }
2589
Paul Menage999cd8a2009-01-07 18:08:36 -08002590 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002591 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08002592 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002593 root->number_of_cgroups++;
2594
Paul Menagebd89aab2007-10-18 23:40:44 -07002595 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002596 if (err < 0)
2597 goto err_remove;
2598
2599 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07002600 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002601
Paul Menagebd89aab2007-10-18 23:40:44 -07002602 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002603 /* If err < 0, we have a half-filled directory - oh well ;) */
2604
2605 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002606 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002607
2608 return 0;
2609
2610 err_remove:
2611
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002612 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002613 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002614 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002615 root->number_of_cgroups--;
2616
2617 err_destroy:
2618
2619 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002620 if (cgrp->subsys[ss->subsys_id])
2621 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002622 }
2623
2624 mutex_unlock(&cgroup_mutex);
2625
2626 /* Release the reference count that we took on the superblock */
2627 deactivate_super(sb);
2628
Paul Menagebd89aab2007-10-18 23:40:44 -07002629 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002630 return err;
2631}
2632
2633static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2634{
2635 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2636
2637 /* the vfs holds inode->i_mutex already */
2638 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2639}
2640
Li Zefan55b6fd02008-07-29 22:33:20 -07002641static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002642{
2643 /* Check the reference count on each subsystem. Since we
2644 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08002645 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07002646 * be no outstanding references, so the subsystem is safe to
2647 * destroy. We scan across all subsystems rather than using
2648 * the per-hierarchy linked list of mounted subsystems since
2649 * we can be called via check_for_release() with no
2650 * synchronization other than RCU, and the subsystem linked
2651 * list isn't RCU-safe */
2652 int i;
2653 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2654 struct cgroup_subsys *ss = subsys[i];
2655 struct cgroup_subsys_state *css;
2656 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07002657 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002658 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07002659 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07002660 /* When called from check_for_release() it's possible
2661 * that by this point the cgroup has been removed
2662 * and the css deleted. But a false-positive doesn't
2663 * matter, since it can only happen if the cgroup
2664 * has been deleted and hence no longer needs the
2665 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08002666 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07002667 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07002668 }
2669 return 0;
2670}
2671
Paul Menagee7c5ec92009-01-07 18:08:38 -08002672/*
2673 * Atomically mark all (or else none) of the cgroup's CSS objects as
2674 * CSS_REMOVED. Return true on success, or false if the cgroup has
2675 * busy subsystems. Call with cgroup_mutex held
2676 */
2677
2678static int cgroup_clear_css_refs(struct cgroup *cgrp)
2679{
2680 struct cgroup_subsys *ss;
2681 unsigned long flags;
2682 bool failed = false;
2683 local_irq_save(flags);
2684 for_each_subsys(cgrp->root, ss) {
2685 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2686 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08002687 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08002688 /* We can only remove a CSS with a refcnt==1 */
2689 refcnt = atomic_read(&css->refcnt);
2690 if (refcnt > 1) {
2691 failed = true;
2692 goto done;
2693 }
2694 BUG_ON(!refcnt);
2695 /*
2696 * Drop the refcnt to 0 while we check other
2697 * subsystems. This will cause any racing
2698 * css_tryget() to spin until we set the
2699 * CSS_REMOVED bits or abort
2700 */
Paul Menage804b3c22009-01-29 14:25:21 -08002701 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
2702 break;
2703 cpu_relax();
2704 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08002705 }
2706 done:
2707 for_each_subsys(cgrp->root, ss) {
2708 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2709 if (failed) {
2710 /*
2711 * Restore old refcnt if we previously managed
2712 * to clear it from 1 to 0
2713 */
2714 if (!atomic_read(&css->refcnt))
2715 atomic_set(&css->refcnt, 1);
2716 } else {
2717 /* Commit the fact that the CSS is removed */
2718 set_bit(CSS_REMOVED, &css->flags);
2719 }
2720 }
2721 local_irq_restore(flags);
2722 return !failed;
2723}
2724
Paul Menageddbcc7e2007-10-18 23:39:30 -07002725static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2726{
Paul Menagebd89aab2007-10-18 23:40:44 -07002727 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002728 struct dentry *d;
2729 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002730 DEFINE_WAIT(wait);
2731 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002732
2733 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002734again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07002735 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002736 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002737 mutex_unlock(&cgroup_mutex);
2738 return -EBUSY;
2739 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002740 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002741 mutex_unlock(&cgroup_mutex);
2742 return -EBUSY;
2743 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002744 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08002745
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002746 /*
Li Zefana043e3b2008-02-23 15:24:09 -08002747 * Call pre_destroy handlers of subsys. Notify subsystems
2748 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002749 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002750 ret = cgroup_call_pre_destroy(cgrp);
2751 if (ret)
2752 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002753
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002754 mutex_lock(&cgroup_mutex);
2755 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002756 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002757 mutex_unlock(&cgroup_mutex);
2758 return -EBUSY;
2759 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002760 /*
2761 * css_put/get is provided for subsys to grab refcnt to css. In typical
2762 * case, subsystem has no reference after pre_destroy(). But, under
2763 * hierarchy management, some *temporal* refcnt can be hold.
2764 * To avoid returning -EBUSY to a user, waitqueue is used. If subsys
2765 * is really busy, it should return -EBUSY at pre_destroy(). wake_up
2766 * is called when css_put() is called and refcnt goes down to 0.
2767 */
2768 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2769 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
2770
2771 if (!cgroup_clear_css_refs(cgrp)) {
2772 mutex_unlock(&cgroup_mutex);
2773 schedule();
2774 finish_wait(&cgroup_rmdir_waitq, &wait);
2775 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2776 if (signal_pending(current))
2777 return -EINTR;
2778 goto again;
2779 }
2780 /* NO css_tryget() can success after here. */
2781 finish_wait(&cgroup_rmdir_waitq, &wait);
2782 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002783
Paul Menage81a6a5c2007-10-18 23:39:38 -07002784 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002785 set_bit(CGRP_REMOVED, &cgrp->flags);
2786 if (!list_empty(&cgrp->release_list))
2787 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002788 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08002789
2790 cgroup_lock_hierarchy(cgrp->root);
2791 /* delete this cgroup from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07002792 list_del(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08002793 cgroup_unlock_hierarchy(cgrp->root);
2794
Paul Menagebd89aab2007-10-18 23:40:44 -07002795 spin_lock(&cgrp->dentry->d_lock);
2796 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002797 spin_unlock(&d->d_lock);
2798
2799 cgroup_d_remove_dir(d);
2800 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002801
Paul Menagebd89aab2007-10-18 23:40:44 -07002802 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002803 check_for_release(parent);
2804
Paul Menageddbcc7e2007-10-18 23:39:30 -07002805 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002806 return 0;
2807}
2808
Li Zefan06a11922008-04-29 01:00:07 -07002809static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002810{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002811 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08002812
2813 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002814
2815 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08002816 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002817 ss->root = &rootnode;
2818 css = ss->create(ss, dummytop);
2819 /* We don't handle early failures gracefully */
2820 BUG_ON(IS_ERR(css));
2821 init_cgroup_css(css, ss, dummytop);
2822
Li Zefane8d55fd2008-04-29 01:00:13 -07002823 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07002824 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07002825 * newly registered, all tasks and hence the
2826 * init_css_set is in the subsystem's top cgroup. */
2827 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07002828
2829 need_forkexit_callback |= ss->fork || ss->exit;
2830
Li Zefane8d55fd2008-04-29 01:00:13 -07002831 /* At system boot, before all subsystems have been
2832 * registered, no tasks have been forked, so we don't
2833 * need to invoke fork callbacks here. */
2834 BUG_ON(!list_empty(&init_task.tasks));
2835
Paul Menage999cd8a2009-01-07 18:08:36 -08002836 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08002837 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002838 ss->active = 1;
2839}
2840
2841/**
Li Zefana043e3b2008-02-23 15:24:09 -08002842 * cgroup_init_early - cgroup initialization at system boot
2843 *
2844 * Initialize cgroups at system boot, and initialize any
2845 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002846 */
2847int __init cgroup_init_early(void)
2848{
2849 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002850 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07002851 INIT_LIST_HEAD(&init_css_set.cg_links);
2852 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07002853 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07002854 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002855 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07002856 root_count = 1;
2857 init_task.cgroups = &init_css_set;
2858
2859 init_css_set_link.cg = &init_css_set;
Paul Menagebd89aab2007-10-18 23:40:44 -07002860 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07002861 &rootnode.top_cgroup.css_sets);
2862 list_add(&init_css_set_link.cg_link_list,
2863 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002864
Li Zefan472b1052008-04-29 01:00:11 -07002865 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2866 INIT_HLIST_HEAD(&css_set_table[i]);
2867
Paul Menageddbcc7e2007-10-18 23:39:30 -07002868 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2869 struct cgroup_subsys *ss = subsys[i];
2870
2871 BUG_ON(!ss->name);
2872 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2873 BUG_ON(!ss->create);
2874 BUG_ON(!ss->destroy);
2875 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08002876 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07002877 ss->name, ss->subsys_id);
2878 BUG();
2879 }
2880
2881 if (ss->early_init)
2882 cgroup_init_subsys(ss);
2883 }
2884 return 0;
2885}
2886
2887/**
Li Zefana043e3b2008-02-23 15:24:09 -08002888 * cgroup_init - cgroup initialization
2889 *
2890 * Register cgroup filesystem and /proc file, and initialize
2891 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002892 */
2893int __init cgroup_init(void)
2894{
2895 int err;
2896 int i;
Li Zefan472b1052008-04-29 01:00:11 -07002897 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07002898
2899 err = bdi_init(&cgroup_backing_dev_info);
2900 if (err)
2901 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002902
2903 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2904 struct cgroup_subsys *ss = subsys[i];
2905 if (!ss->early_init)
2906 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002907 if (ss->use_id)
2908 cgroup_subsys_init_idr(ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002909 }
2910
Li Zefan472b1052008-04-29 01:00:11 -07002911 /* Add init_css_set to the hash table */
2912 hhead = css_set_hash(init_css_set.subsys);
2913 hlist_add_head(&init_css_set.hlist, hhead);
2914
Paul Menageddbcc7e2007-10-18 23:39:30 -07002915 err = register_filesystem(&cgroup_fs_type);
2916 if (err < 0)
2917 goto out;
2918
Li Zefan46ae2202008-04-29 01:00:08 -07002919 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07002920
Paul Menageddbcc7e2007-10-18 23:39:30 -07002921out:
Paul Menagea4243162007-10-18 23:39:35 -07002922 if (err)
2923 bdi_destroy(&cgroup_backing_dev_info);
2924
Paul Menageddbcc7e2007-10-18 23:39:30 -07002925 return err;
2926}
Paul Menageb4f48b62007-10-18 23:39:33 -07002927
Paul Menagea4243162007-10-18 23:39:35 -07002928/*
2929 * proc_cgroup_show()
2930 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2931 * - Used for /proc/<pid>/cgroup.
2932 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2933 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002934 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07002935 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2936 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2937 * cgroup to top_cgroup.
2938 */
2939
2940/* TODO: Use a proper seq_file iterator */
2941static int proc_cgroup_show(struct seq_file *m, void *v)
2942{
2943 struct pid *pid;
2944 struct task_struct *tsk;
2945 char *buf;
2946 int retval;
2947 struct cgroupfs_root *root;
2948
2949 retval = -ENOMEM;
2950 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2951 if (!buf)
2952 goto out;
2953
2954 retval = -ESRCH;
2955 pid = m->private;
2956 tsk = get_pid_task(pid, PIDTYPE_PID);
2957 if (!tsk)
2958 goto out_free;
2959
2960 retval = 0;
2961
2962 mutex_lock(&cgroup_mutex);
2963
Li Zefane5f6a862009-01-07 18:07:41 -08002964 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07002965 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07002966 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07002967 int subsys_id;
2968 int count = 0;
2969
Paul Menageb6c30062008-04-10 21:29:16 -07002970 seq_printf(m, "%lu:", root->subsys_bits);
Paul Menagea4243162007-10-18 23:39:35 -07002971 for_each_subsys(root, ss)
2972 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2973 seq_putc(m, ':');
2974 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07002975 cgrp = task_cgroup(tsk, subsys_id);
2976 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07002977 if (retval < 0)
2978 goto out_unlock;
2979 seq_puts(m, buf);
2980 seq_putc(m, '\n');
2981 }
2982
2983out_unlock:
2984 mutex_unlock(&cgroup_mutex);
2985 put_task_struct(tsk);
2986out_free:
2987 kfree(buf);
2988out:
2989 return retval;
2990}
2991
2992static int cgroup_open(struct inode *inode, struct file *file)
2993{
2994 struct pid *pid = PROC_I(inode)->pid;
2995 return single_open(file, proc_cgroup_show, pid);
2996}
2997
2998struct file_operations proc_cgroup_operations = {
2999 .open = cgroup_open,
3000 .read = seq_read,
3001 .llseek = seq_lseek,
3002 .release = single_release,
3003};
3004
3005/* Display information about each subsystem and each hierarchy */
3006static int proc_cgroupstats_show(struct seq_file *m, void *v)
3007{
3008 int i;
Paul Menagea4243162007-10-18 23:39:35 -07003009
Paul Menage8bab8dd2008-04-04 14:29:57 -07003010 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07003011 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07003012 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3013 struct cgroup_subsys *ss = subsys[i];
Paul Menage8bab8dd2008-04-04 14:29:57 -07003014 seq_printf(m, "%s\t%lu\t%d\t%d\n",
Paul Menage817929e2007-10-18 23:39:36 -07003015 ss->name, ss->root->subsys_bits,
Paul Menage8bab8dd2008-04-04 14:29:57 -07003016 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07003017 }
3018 mutex_unlock(&cgroup_mutex);
3019 return 0;
3020}
3021
3022static int cgroupstats_open(struct inode *inode, struct file *file)
3023{
Al Viro9dce07f12008-03-29 03:07:28 +00003024 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07003025}
3026
3027static struct file_operations proc_cgroupstats_operations = {
3028 .open = cgroupstats_open,
3029 .read = seq_read,
3030 .llseek = seq_lseek,
3031 .release = single_release,
3032};
3033
Paul Menageb4f48b62007-10-18 23:39:33 -07003034/**
3035 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08003036 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07003037 *
3038 * Description: A task inherits its parent's cgroup at fork().
3039 *
3040 * A pointer to the shared css_set was automatically copied in
3041 * fork.c by dup_task_struct(). However, we ignore that copy, since
3042 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08003043 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07003044 * have already changed current->cgroups, allowing the previously
3045 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07003046 *
3047 * At the point that cgroup_fork() is called, 'current' is the parent
3048 * task, and the passed argument 'child' points to the child task.
3049 */
3050void cgroup_fork(struct task_struct *child)
3051{
Paul Menage817929e2007-10-18 23:39:36 -07003052 task_lock(current);
3053 child->cgroups = current->cgroups;
3054 get_css_set(child->cgroups);
3055 task_unlock(current);
3056 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07003057}
3058
3059/**
Li Zefana043e3b2008-02-23 15:24:09 -08003060 * cgroup_fork_callbacks - run fork callbacks
3061 * @child: the new task
3062 *
3063 * Called on a new task very soon before adding it to the
3064 * tasklist. No need to take any locks since no-one can
3065 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003066 */
3067void cgroup_fork_callbacks(struct task_struct *child)
3068{
3069 if (need_forkexit_callback) {
3070 int i;
3071 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3072 struct cgroup_subsys *ss = subsys[i];
3073 if (ss->fork)
3074 ss->fork(ss, child);
3075 }
3076 }
3077}
3078
3079/**
Li Zefana043e3b2008-02-23 15:24:09 -08003080 * cgroup_post_fork - called on a new task after adding it to the task list
3081 * @child: the task in question
3082 *
3083 * Adds the task to the list running through its css_set if necessary.
3084 * Has to be after the task is visible on the task list in case we race
3085 * with the first call to cgroup_iter_start() - to guarantee that the
3086 * new task ends up on its list.
3087 */
Paul Menage817929e2007-10-18 23:39:36 -07003088void cgroup_post_fork(struct task_struct *child)
3089{
3090 if (use_task_css_set_links) {
3091 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003092 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003093 if (list_empty(&child->cg_list))
3094 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003095 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003096 write_unlock(&css_set_lock);
3097 }
3098}
3099/**
Paul Menageb4f48b62007-10-18 23:39:33 -07003100 * cgroup_exit - detach cgroup from exiting task
3101 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08003102 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07003103 *
3104 * Description: Detach cgroup from @tsk and release it.
3105 *
3106 * Note that cgroups marked notify_on_release force every task in
3107 * them to take the global cgroup_mutex mutex when exiting.
3108 * This could impact scaling on very large systems. Be reluctant to
3109 * use notify_on_release cgroups where very high task exit scaling
3110 * is required on large systems.
3111 *
3112 * the_top_cgroup_hack:
3113 *
3114 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3115 *
3116 * We call cgroup_exit() while the task is still competent to
3117 * handle notify_on_release(), then leave the task attached to the
3118 * root cgroup in each hierarchy for the remainder of its exit.
3119 *
3120 * To do this properly, we would increment the reference count on
3121 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3122 * code we would add a second cgroup function call, to drop that
3123 * reference. This would just create an unnecessary hot spot on
3124 * the top_cgroup reference count, to no avail.
3125 *
3126 * Normally, holding a reference to a cgroup without bumping its
3127 * count is unsafe. The cgroup could go away, or someone could
3128 * attach us to a different cgroup, decrementing the count on
3129 * the first cgroup that we never incremented. But in this case,
3130 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003131 * which wards off any cgroup_attach_task() attempts, or task is a failed
3132 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003133 */
3134void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3135{
3136 int i;
Paul Menage817929e2007-10-18 23:39:36 -07003137 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07003138
3139 if (run_callbacks && need_forkexit_callback) {
3140 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3141 struct cgroup_subsys *ss = subsys[i];
3142 if (ss->exit)
3143 ss->exit(ss, tsk);
3144 }
3145 }
Paul Menage817929e2007-10-18 23:39:36 -07003146
3147 /*
3148 * Unlink from the css_set task list if necessary.
3149 * Optimistically check cg_list before taking
3150 * css_set_lock
3151 */
3152 if (!list_empty(&tsk->cg_list)) {
3153 write_lock(&css_set_lock);
3154 if (!list_empty(&tsk->cg_list))
3155 list_del(&tsk->cg_list);
3156 write_unlock(&css_set_lock);
3157 }
3158
Paul Menageb4f48b62007-10-18 23:39:33 -07003159 /* Reassign the task to the init_css_set. */
3160 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003161 cg = tsk->cgroups;
3162 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07003163 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003164 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003165 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07003166}
Paul Menage697f4162007-10-18 23:39:34 -07003167
3168/**
Li Zefana043e3b2008-02-23 15:24:09 -08003169 * cgroup_clone - clone the cgroup the given subsystem is attached to
3170 * @tsk: the task to be moved
3171 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003172 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08003173 *
3174 * Duplicate the current cgroup in the hierarchy that the given
3175 * subsystem is attached to, and move this task into the new
3176 * child.
Paul Menage697f4162007-10-18 23:39:34 -07003177 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003178int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3179 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07003180{
3181 struct dentry *dentry;
3182 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07003183 struct cgroup *parent, *child;
3184 struct inode *inode;
3185 struct css_set *cg;
3186 struct cgroupfs_root *root;
3187 struct cgroup_subsys *ss;
3188
3189 /* We shouldn't be called by an unregistered subsystem */
3190 BUG_ON(!subsys->active);
3191
3192 /* First figure out what hierarchy and cgroup we're dealing
3193 * with, and pin them so we can drop cgroup_mutex */
3194 mutex_lock(&cgroup_mutex);
3195 again:
3196 root = subsys->root;
3197 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07003198 mutex_unlock(&cgroup_mutex);
3199 return 0;
3200 }
Paul Menage697f4162007-10-18 23:39:34 -07003201
Paul Menage697f4162007-10-18 23:39:34 -07003202 /* Pin the hierarchy */
Li Zefan1404f062009-01-29 14:25:21 -08003203 if (!atomic_inc_not_zero(&root->sb->s_active)) {
Li Zefan7b574b72009-01-04 12:00:45 -08003204 /* We race with the final deactivate_super() */
3205 mutex_unlock(&cgroup_mutex);
3206 return 0;
3207 }
Paul Menage697f4162007-10-18 23:39:34 -07003208
Paul Menage817929e2007-10-18 23:39:36 -07003209 /* Keep the cgroup alive */
Li Zefan1404f062009-01-29 14:25:21 -08003210 task_lock(tsk);
3211 parent = task_cgroup(tsk, subsys->subsys_id);
3212 cg = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07003213 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08003214 task_unlock(tsk);
Li Zefan1404f062009-01-29 14:25:21 -08003215
Paul Menage697f4162007-10-18 23:39:34 -07003216 mutex_unlock(&cgroup_mutex);
3217
3218 /* Now do the VFS work to create a cgroup */
3219 inode = parent->dentry->d_inode;
3220
3221 /* Hold the parent directory mutex across this operation to
3222 * stop anyone else deleting the new cgroup */
3223 mutex_lock(&inode->i_mutex);
3224 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3225 if (IS_ERR(dentry)) {
3226 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08003227 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07003228 PTR_ERR(dentry));
3229 ret = PTR_ERR(dentry);
3230 goto out_release;
3231 }
3232
3233 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08003234 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07003235 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07003236 dput(dentry);
3237 if (ret) {
3238 printk(KERN_INFO
3239 "Failed to create cgroup %s: %d\n", nodename,
3240 ret);
3241 goto out_release;
3242 }
3243
Paul Menage697f4162007-10-18 23:39:34 -07003244 /* The cgroup now exists. Retake cgroup_mutex and check
3245 * that we're still in the same state that we thought we
3246 * were. */
3247 mutex_lock(&cgroup_mutex);
3248 if ((root != subsys->root) ||
3249 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3250 /* Aargh, we raced ... */
3251 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003252 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07003253
Li Zefan1404f062009-01-29 14:25:21 -08003254 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003255 /* The cgroup is still accessible in the VFS, but
3256 * we're not going to try to rmdir() it at this
3257 * point. */
3258 printk(KERN_INFO
3259 "Race in cgroup_clone() - leaking cgroup %s\n",
3260 nodename);
3261 goto again;
3262 }
3263
3264 /* do any required auto-setup */
3265 for_each_subsys(root, ss) {
3266 if (ss->post_clone)
3267 ss->post_clone(ss, child);
3268 }
3269
3270 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08003271 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07003272 mutex_unlock(&cgroup_mutex);
3273
3274 out_release:
3275 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003276
3277 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003278 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003279 mutex_unlock(&cgroup_mutex);
Li Zefan1404f062009-01-29 14:25:21 -08003280 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003281 return ret;
3282}
3283
Li Zefana043e3b2008-02-23 15:24:09 -08003284/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003285 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08003286 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003287 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08003288 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003289 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3290 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07003291 *
3292 * If we are sending in dummytop, then presumably we are creating
3293 * the top cgroup in the subsystem.
3294 *
3295 * Called only by the ns (nsproxy) cgroup.
3296 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003297int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07003298{
3299 int ret;
3300 struct cgroup *target;
3301 int subsys_id;
3302
Paul Menagebd89aab2007-10-18 23:40:44 -07003303 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07003304 return 1;
3305
Paul Menagebd89aab2007-10-18 23:40:44 -07003306 get_first_subsys(cgrp, NULL, &subsys_id);
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003307 target = task_cgroup(task, subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07003308 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3309 cgrp = cgrp->parent;
3310 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07003311 return ret;
3312}
Paul Menage81a6a5c2007-10-18 23:39:38 -07003313
Paul Menagebd89aab2007-10-18 23:40:44 -07003314static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003315{
3316 /* All of these checks rely on RCU to keep the cgroup
3317 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07003318 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3319 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003320 /* Control Group is currently removeable. If it's not
3321 * already queued for a userspace notification, queue
3322 * it now */
3323 int need_schedule_work = 0;
3324 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003325 if (!cgroup_is_removed(cgrp) &&
3326 list_empty(&cgrp->release_list)) {
3327 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003328 need_schedule_work = 1;
3329 }
3330 spin_unlock(&release_list_lock);
3331 if (need_schedule_work)
3332 schedule_work(&release_agent_work);
3333 }
3334}
3335
3336void __css_put(struct cgroup_subsys_state *css)
3337{
Paul Menagebd89aab2007-10-18 23:40:44 -07003338 struct cgroup *cgrp = css->cgroup;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003339 rcu_read_lock();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003340 if (atomic_dec_return(&css->refcnt) == 1) {
3341 if (notify_on_release(cgrp)) {
3342 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3343 check_for_release(cgrp);
3344 }
3345 cgroup_wakeup_rmdir_waiters(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003346 }
3347 rcu_read_unlock();
3348}
3349
3350/*
3351 * Notify userspace when a cgroup is released, by running the
3352 * configured release agent with the name of the cgroup (path
3353 * relative to the root of cgroup file system) as the argument.
3354 *
3355 * Most likely, this user command will try to rmdir this cgroup.
3356 *
3357 * This races with the possibility that some other task will be
3358 * attached to this cgroup before it is removed, or that some other
3359 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3360 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3361 * unused, and this cgroup will be reprieved from its death sentence,
3362 * to continue to serve a useful existence. Next time it's released,
3363 * we will get notified again, if it still has 'notify_on_release' set.
3364 *
3365 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3366 * means only wait until the task is successfully execve()'d. The
3367 * separate release agent task is forked by call_usermodehelper(),
3368 * then control in this thread returns here, without waiting for the
3369 * release agent task. We don't bother to wait because the caller of
3370 * this routine has no use for the exit status of the release agent
3371 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003372 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003373static void cgroup_release_agent(struct work_struct *work)
3374{
3375 BUG_ON(work != &release_agent_work);
3376 mutex_lock(&cgroup_mutex);
3377 spin_lock(&release_list_lock);
3378 while (!list_empty(&release_list)) {
3379 char *argv[3], *envp[3];
3380 int i;
Paul Menagee788e062008-07-25 01:46:59 -07003381 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003382 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003383 struct cgroup,
3384 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003385 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003386 spin_unlock(&release_list_lock);
3387 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07003388 if (!pathbuf)
3389 goto continue_free;
3390 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3391 goto continue_free;
3392 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3393 if (!agentbuf)
3394 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003395
3396 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07003397 argv[i++] = agentbuf;
3398 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003399 argv[i] = NULL;
3400
3401 i = 0;
3402 /* minimal command environment */
3403 envp[i++] = "HOME=/";
3404 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3405 envp[i] = NULL;
3406
3407 /* Drop the lock while we invoke the usermode helper,
3408 * since the exec could involve hitting disk and hence
3409 * be a slow process */
3410 mutex_unlock(&cgroup_mutex);
3411 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003412 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07003413 continue_free:
3414 kfree(pathbuf);
3415 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003416 spin_lock(&release_list_lock);
3417 }
3418 spin_unlock(&release_list_lock);
3419 mutex_unlock(&cgroup_mutex);
3420}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003421
3422static int __init cgroup_disable(char *str)
3423{
3424 int i;
3425 char *token;
3426
3427 while ((token = strsep(&str, ",")) != NULL) {
3428 if (!*token)
3429 continue;
3430
3431 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3432 struct cgroup_subsys *ss = subsys[i];
3433
3434 if (!strcmp(token, ss->name)) {
3435 ss->disabled = 1;
3436 printk(KERN_INFO "Disabling %s control group"
3437 " subsystem\n", ss->name);
3438 break;
3439 }
3440 }
3441 }
3442 return 1;
3443}
3444__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003445
3446/*
3447 * Functons for CSS ID.
3448 */
3449
3450/*
3451 *To get ID other than 0, this should be called when !cgroup_is_removed().
3452 */
3453unsigned short css_id(struct cgroup_subsys_state *css)
3454{
3455 struct css_id *cssid = rcu_dereference(css->id);
3456
3457 if (cssid)
3458 return cssid->id;
3459 return 0;
3460}
3461
3462unsigned short css_depth(struct cgroup_subsys_state *css)
3463{
3464 struct css_id *cssid = rcu_dereference(css->id);
3465
3466 if (cssid)
3467 return cssid->depth;
3468 return 0;
3469}
3470
3471bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07003472 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003473{
3474 struct css_id *child_id = rcu_dereference(child->id);
3475 struct css_id *root_id = rcu_dereference(root->id);
3476
3477 if (!child_id || !root_id || (child_id->depth < root_id->depth))
3478 return false;
3479 return child_id->stack[root_id->depth] == root_id->id;
3480}
3481
3482static void __free_css_id_cb(struct rcu_head *head)
3483{
3484 struct css_id *id;
3485
3486 id = container_of(head, struct css_id, rcu_head);
3487 kfree(id);
3488}
3489
3490void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
3491{
3492 struct css_id *id = css->id;
3493 /* When this is called before css_id initialization, id can be NULL */
3494 if (!id)
3495 return;
3496
3497 BUG_ON(!ss->use_id);
3498
3499 rcu_assign_pointer(id->css, NULL);
3500 rcu_assign_pointer(css->id, NULL);
3501 spin_lock(&ss->id_lock);
3502 idr_remove(&ss->idr, id->id);
3503 spin_unlock(&ss->id_lock);
3504 call_rcu(&id->rcu_head, __free_css_id_cb);
3505}
3506
3507/*
3508 * This is called by init or create(). Then, calls to this function are
3509 * always serialized (By cgroup_mutex() at create()).
3510 */
3511
3512static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
3513{
3514 struct css_id *newid;
3515 int myid, error, size;
3516
3517 BUG_ON(!ss->use_id);
3518
3519 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
3520 newid = kzalloc(size, GFP_KERNEL);
3521 if (!newid)
3522 return ERR_PTR(-ENOMEM);
3523 /* get id */
3524 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
3525 error = -ENOMEM;
3526 goto err_out;
3527 }
3528 spin_lock(&ss->id_lock);
3529 /* Don't use 0. allocates an ID of 1-65535 */
3530 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
3531 spin_unlock(&ss->id_lock);
3532
3533 /* Returns error when there are no free spaces for new ID.*/
3534 if (error) {
3535 error = -ENOSPC;
3536 goto err_out;
3537 }
3538 if (myid > CSS_ID_MAX)
3539 goto remove_idr;
3540
3541 newid->id = myid;
3542 newid->depth = depth;
3543 return newid;
3544remove_idr:
3545 error = -ENOSPC;
3546 spin_lock(&ss->id_lock);
3547 idr_remove(&ss->idr, myid);
3548 spin_unlock(&ss->id_lock);
3549err_out:
3550 kfree(newid);
3551 return ERR_PTR(error);
3552
3553}
3554
3555static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss)
3556{
3557 struct css_id *newid;
3558 struct cgroup_subsys_state *rootcss;
3559
3560 spin_lock_init(&ss->id_lock);
3561 idr_init(&ss->idr);
3562
3563 rootcss = init_css_set.subsys[ss->subsys_id];
3564 newid = get_new_cssid(ss, 0);
3565 if (IS_ERR(newid))
3566 return PTR_ERR(newid);
3567
3568 newid->stack[0] = newid->id;
3569 newid->css = rootcss;
3570 rootcss->id = newid;
3571 return 0;
3572}
3573
3574static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
3575 struct cgroup *child)
3576{
3577 int subsys_id, i, depth = 0;
3578 struct cgroup_subsys_state *parent_css, *child_css;
3579 struct css_id *child_id, *parent_id = NULL;
3580
3581 subsys_id = ss->subsys_id;
3582 parent_css = parent->subsys[subsys_id];
3583 child_css = child->subsys[subsys_id];
3584 depth = css_depth(parent_css) + 1;
3585 parent_id = parent_css->id;
3586
3587 child_id = get_new_cssid(ss, depth);
3588 if (IS_ERR(child_id))
3589 return PTR_ERR(child_id);
3590
3591 for (i = 0; i < depth; i++)
3592 child_id->stack[i] = parent_id->stack[i];
3593 child_id->stack[depth] = child_id->id;
3594 /*
3595 * child_id->css pointer will be set after this cgroup is available
3596 * see cgroup_populate_dir()
3597 */
3598 rcu_assign_pointer(child_css->id, child_id);
3599
3600 return 0;
3601}
3602
3603/**
3604 * css_lookup - lookup css by id
3605 * @ss: cgroup subsys to be looked into.
3606 * @id: the id
3607 *
3608 * Returns pointer to cgroup_subsys_state if there is valid one with id.
3609 * NULL if not. Should be called under rcu_read_lock()
3610 */
3611struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
3612{
3613 struct css_id *cssid = NULL;
3614
3615 BUG_ON(!ss->use_id);
3616 cssid = idr_find(&ss->idr, id);
3617
3618 if (unlikely(!cssid))
3619 return NULL;
3620
3621 return rcu_dereference(cssid->css);
3622}
3623
3624/**
3625 * css_get_next - lookup next cgroup under specified hierarchy.
3626 * @ss: pointer to subsystem
3627 * @id: current position of iteration.
3628 * @root: pointer to css. search tree under this.
3629 * @foundid: position of found object.
3630 *
3631 * Search next css under the specified hierarchy of rootid. Calling under
3632 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
3633 */
3634struct cgroup_subsys_state *
3635css_get_next(struct cgroup_subsys *ss, int id,
3636 struct cgroup_subsys_state *root, int *foundid)
3637{
3638 struct cgroup_subsys_state *ret = NULL;
3639 struct css_id *tmp;
3640 int tmpid;
3641 int rootid = css_id(root);
3642 int depth = css_depth(root);
3643
3644 if (!rootid)
3645 return NULL;
3646
3647 BUG_ON(!ss->use_id);
3648 /* fill start point for scan */
3649 tmpid = id;
3650 while (1) {
3651 /*
3652 * scan next entry from bitmap(tree), tmpid is updated after
3653 * idr_get_next().
3654 */
3655 spin_lock(&ss->id_lock);
3656 tmp = idr_get_next(&ss->idr, &tmpid);
3657 spin_unlock(&ss->id_lock);
3658
3659 if (!tmp)
3660 break;
3661 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
3662 ret = rcu_dereference(tmp->css);
3663 if (ret) {
3664 *foundid = tmpid;
3665 break;
3666 }
3667 }
3668 /* continue to scan from next id */
3669 tmpid = tmpid + 1;
3670 }
3671 return ret;
3672}
3673