blob: a9433f50e53d0670b29ee22aea50d7f840c080ca [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>
Paul Menagec6d57f32009-09-23 15:56:19 -070026#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070027#include <linux/errno.h>
28#include <linux/fs.h>
29#include <linux/kernel.h>
30#include <linux/list.h>
31#include <linux/mm.h>
32#include <linux/mutex.h>
33#include <linux/mount.h>
34#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070035#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070036#include <linux/rcupdate.h>
37#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070038#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070039#include <linux/seq_file.h>
40#include <linux/slab.h>
41#include <linux/magic.h>
42#include <linux/spinlock.h>
43#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070044#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070045#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070046#include <linux/delayacct.h>
47#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070048#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040049#include <linux/namei.h>
Alessio Igor Bogani337eb002009-05-12 15:10:54 +020050#include <linux/smp_lock.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070051#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070052#include <linux/idr.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070053
Paul Menageddbcc7e2007-10-18 23:39:30 -070054#include <asm/atomic.h>
55
Paul Menage81a6a5c2007-10-18 23:39:38 -070056static DEFINE_MUTEX(cgroup_mutex);
57
Paul Menageddbcc7e2007-10-18 23:39:30 -070058/* Generate an array of cgroup subsystem pointers */
59#define SUBSYS(_x) &_x ## _subsys,
60
61static struct cgroup_subsys *subsys[] = {
62#include <linux/cgroup_subsys.h>
63};
64
Paul Menagec6d57f32009-09-23 15:56:19 -070065#define MAX_CGROUP_ROOT_NAMELEN 64
66
Paul Menageddbcc7e2007-10-18 23:39:30 -070067/*
68 * A cgroupfs_root represents the root of a cgroup hierarchy,
69 * and may be associated with a superblock to form an active
70 * hierarchy
71 */
72struct cgroupfs_root {
73 struct super_block *sb;
74
75 /*
76 * The bitmask of subsystems intended to be attached to this
77 * hierarchy
78 */
79 unsigned long subsys_bits;
80
Paul Menage2c6ab6d2009-09-23 15:56:23 -070081 /* Unique id for this hierarchy. */
82 int hierarchy_id;
83
Paul Menageddbcc7e2007-10-18 23:39:30 -070084 /* The bitmask of subsystems currently attached to this hierarchy */
85 unsigned long actual_subsys_bits;
86
87 /* A list running through the attached subsystems */
88 struct list_head subsys_list;
89
90 /* The root cgroup for this hierarchy */
91 struct cgroup top_cgroup;
92
93 /* Tracks how many cgroups are currently defined in hierarchy.*/
94 int number_of_cgroups;
95
Li Zefane5f6a862009-01-07 18:07:41 -080096 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -070097 struct list_head root_list;
98
99 /* Hierarchy-specific flags */
100 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700101
Paul Menagee788e0662008-07-25 01:46:59 -0700102 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700103 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700104
105 /* The name for this hierarchy - may be empty */
106 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700107};
108
Paul Menageddbcc7e2007-10-18 23:39:30 -0700109/*
110 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
111 * subsystems that are otherwise unattached - it never has more than a
112 * single cgroup, and all tasks are part of that cgroup.
113 */
114static struct cgroupfs_root rootnode;
115
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700116/*
117 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
118 * cgroup_subsys->use_id != 0.
119 */
120#define CSS_ID_MAX (65535)
121struct css_id {
122 /*
123 * The css to which this ID points. This pointer is set to valid value
124 * after cgroup is populated. If cgroup is removed, this will be NULL.
125 * This pointer is expected to be RCU-safe because destroy()
126 * is called after synchronize_rcu(). But for safe use, css_is_removed()
127 * css_tryget() should be used for avoiding race.
128 */
129 struct cgroup_subsys_state *css;
130 /*
131 * ID of this css.
132 */
133 unsigned short id;
134 /*
135 * Depth in hierarchy which this ID belongs to.
136 */
137 unsigned short depth;
138 /*
139 * ID is freed by RCU. (and lookup routine is RCU safe.)
140 */
141 struct rcu_head rcu_head;
142 /*
143 * Hierarchy of CSS ID belongs to.
144 */
145 unsigned short stack[0]; /* Array of Length (depth+1) */
146};
147
148
Paul Menageddbcc7e2007-10-18 23:39:30 -0700149/* The list of hierarchy roots */
150
151static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700152static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700153
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700154static DEFINE_IDA(hierarchy_ida);
155static int next_hierarchy_id;
156static DEFINE_SPINLOCK(hierarchy_id_lock);
157
Paul Menageddbcc7e2007-10-18 23:39:30 -0700158/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
159#define dummytop (&rootnode.top_cgroup)
160
161/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800162 * check for fork/exit handlers to call. This avoids us having to do
163 * extra work in the fork/exit path if none of the subsystems need to
164 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700165 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700166static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700167
Paul Menageddbcc7e2007-10-18 23:39:30 -0700168/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700169inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700170{
Paul Menagebd89aab2007-10-18 23:40:44 -0700171 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700172}
173
174/* bits in struct cgroupfs_root flags field */
175enum {
176 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
177};
178
Adrian Bunke9685a02008-02-07 00:13:46 -0800179static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700180{
181 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700182 (1 << CGRP_RELEASABLE) |
183 (1 << CGRP_NOTIFY_ON_RELEASE);
184 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700185}
186
Adrian Bunke9685a02008-02-07 00:13:46 -0800187static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700188{
Paul Menagebd89aab2007-10-18 23:40:44 -0700189 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700190}
191
Paul Menageddbcc7e2007-10-18 23:39:30 -0700192/*
193 * for_each_subsys() allows you to iterate on each subsystem attached to
194 * an active hierarchy
195 */
196#define for_each_subsys(_root, _ss) \
197list_for_each_entry(_ss, &_root->subsys_list, sibling)
198
Li Zefane5f6a862009-01-07 18:07:41 -0800199/* for_each_active_root() allows you to iterate across the active hierarchies */
200#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700201list_for_each_entry(_root, &roots, root_list)
202
Paul Menage81a6a5c2007-10-18 23:39:38 -0700203/* the list of cgroups eligible for automatic release. Protected by
204 * release_list_lock */
205static LIST_HEAD(release_list);
206static DEFINE_SPINLOCK(release_list_lock);
207static void cgroup_release_agent(struct work_struct *work);
208static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700209static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700210
Paul Menage817929e2007-10-18 23:39:36 -0700211/* Link structure for associating css_set objects with cgroups */
212struct cg_cgroup_link {
213 /*
214 * List running through cg_cgroup_links associated with a
215 * cgroup, anchored on cgroup->css_sets
216 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700217 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700218 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700219 /*
220 * List running through cg_cgroup_links pointing at a
221 * single css_set object, anchored on css_set->cg_links
222 */
223 struct list_head cg_link_list;
224 struct css_set *cg;
225};
226
227/* The default css_set - used by init and its children prior to any
228 * hierarchies being mounted. It contains a pointer to the root state
229 * for each subsystem. Also used to anchor the list of css_sets. Not
230 * reference-counted, to improve performance when child cgroups
231 * haven't been created.
232 */
233
234static struct css_set init_css_set;
235static struct cg_cgroup_link init_css_set_link;
236
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700237static int cgroup_subsys_init_idr(struct cgroup_subsys *ss);
238
Paul Menage817929e2007-10-18 23:39:36 -0700239/* css_set_lock protects the list of css_set objects, and the
240 * chain of tasks off each css_set. Nests outside task->alloc_lock
241 * due to cgroup_iter_start() */
242static DEFINE_RWLOCK(css_set_lock);
243static int css_set_count;
244
Paul Menage7717f7b2009-09-23 15:56:22 -0700245/*
246 * hash table for cgroup groups. This improves the performance to find
247 * an existing css_set. This hash doesn't (currently) take into
248 * account cgroups in empty hierarchies.
249 */
Li Zefan472b1052008-04-29 01:00:11 -0700250#define CSS_SET_HASH_BITS 7
251#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
252static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
253
254static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
255{
256 int i;
257 int index;
258 unsigned long tmp = 0UL;
259
260 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
261 tmp += (unsigned long)css[i];
262 tmp = (tmp >> 16) ^ tmp;
263
264 index = hash_long(tmp, CSS_SET_HASH_BITS);
265
266 return &css_set_table[index];
267}
268
Paul Menage817929e2007-10-18 23:39:36 -0700269/* We don't maintain the lists running through each css_set to its
270 * task until after the first call to cgroup_iter_start(). This
271 * reduces the fork()/exit() overhead for people who have cgroups
272 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700273static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700274
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700275static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700276{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700277 struct cg_cgroup_link *link;
278 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700279 /*
280 * Ensure that the refcount doesn't hit zero while any readers
281 * can see it. Similar to atomic_dec_and_lock(), but for an
282 * rwlock
283 */
284 if (atomic_add_unless(&cg->refcount, -1, 1))
285 return;
286 write_lock(&css_set_lock);
287 if (!atomic_dec_and_test(&cg->refcount)) {
288 write_unlock(&css_set_lock);
289 return;
290 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700291
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700292 /* This css_set is dead. unlink it and release cgroup refcounts */
293 hlist_del(&cg->hlist);
294 css_set_count--;
295
296 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
297 cg_link_list) {
298 struct cgroup *cgrp = link->cgrp;
299 list_del(&link->cg_link_list);
300 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700301 if (atomic_dec_and_test(&cgrp->count) &&
302 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700303 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700304 set_bit(CGRP_RELEASABLE, &cgrp->flags);
305 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700306 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700307
308 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700309 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700310
311 write_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700312 kfree(cg);
313}
314
315/*
316 * refcounted get/put for css_set objects
317 */
318static inline void get_css_set(struct css_set *cg)
319{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700320 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700321}
322
323static inline void put_css_set(struct css_set *cg)
324{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700325 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700326}
327
Paul Menage81a6a5c2007-10-18 23:39:38 -0700328static inline void put_css_set_taskexit(struct css_set *cg)
329{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700330 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700331}
332
Paul Menage817929e2007-10-18 23:39:36 -0700333/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700334 * compare_css_sets - helper function for find_existing_css_set().
335 * @cg: candidate css_set being tested
336 * @old_cg: existing css_set for a task
337 * @new_cgrp: cgroup that's being entered by the task
338 * @template: desired set of css pointers in css_set (pre-calculated)
339 *
340 * Returns true if "cg" matches "old_cg" except for the hierarchy
341 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
342 */
343static bool compare_css_sets(struct css_set *cg,
344 struct css_set *old_cg,
345 struct cgroup *new_cgrp,
346 struct cgroup_subsys_state *template[])
347{
348 struct list_head *l1, *l2;
349
350 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
351 /* Not all subsystems matched */
352 return false;
353 }
354
355 /*
356 * Compare cgroup pointers in order to distinguish between
357 * different cgroups in heirarchies with no subsystems. We
358 * could get by with just this check alone (and skip the
359 * memcmp above) but on most setups the memcmp check will
360 * avoid the need for this more expensive check on almost all
361 * candidates.
362 */
363
364 l1 = &cg->cg_links;
365 l2 = &old_cg->cg_links;
366 while (1) {
367 struct cg_cgroup_link *cgl1, *cgl2;
368 struct cgroup *cg1, *cg2;
369
370 l1 = l1->next;
371 l2 = l2->next;
372 /* See if we reached the end - both lists are equal length. */
373 if (l1 == &cg->cg_links) {
374 BUG_ON(l2 != &old_cg->cg_links);
375 break;
376 } else {
377 BUG_ON(l2 == &old_cg->cg_links);
378 }
379 /* Locate the cgroups associated with these links. */
380 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
381 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
382 cg1 = cgl1->cgrp;
383 cg2 = cgl2->cgrp;
384 /* Hierarchies should be linked in the same order. */
385 BUG_ON(cg1->root != cg2->root);
386
387 /*
388 * If this hierarchy is the hierarchy of the cgroup
389 * that's changing, then we need to check that this
390 * css_set points to the new cgroup; if it's any other
391 * hierarchy, then this css_set should point to the
392 * same cgroup as the old css_set.
393 */
394 if (cg1->root == new_cgrp->root) {
395 if (cg1 != new_cgrp)
396 return false;
397 } else {
398 if (cg1 != cg2)
399 return false;
400 }
401 }
402 return true;
403}
404
405/*
Paul Menage817929e2007-10-18 23:39:36 -0700406 * find_existing_css_set() is a helper for
407 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700408 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700409 *
410 * oldcg: the cgroup group that we're using before the cgroup
411 * transition
412 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700413 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700414 *
415 * template: location in which to build the desired set of subsystem
416 * state objects for the new cgroup group
417 */
Paul Menage817929e2007-10-18 23:39:36 -0700418static struct css_set *find_existing_css_set(
419 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700420 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700421 struct cgroup_subsys_state *template[])
422{
423 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700424 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700425 struct hlist_head *hhead;
426 struct hlist_node *node;
427 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700428
429 /* Built the set of subsystem state objects that we want to
430 * see in the new css_set */
431 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800432 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700433 /* Subsystem is in this hierarchy. So we want
434 * the subsystem state from the new
435 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700436 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700437 } else {
438 /* Subsystem is not in this hierarchy, so we
439 * don't want to change the subsystem state */
440 template[i] = oldcg->subsys[i];
441 }
442 }
443
Li Zefan472b1052008-04-29 01:00:11 -0700444 hhead = css_set_hash(template);
445 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700446 if (!compare_css_sets(cg, oldcg, cgrp, template))
447 continue;
448
449 /* This css_set matches what we need */
450 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700451 }
Paul Menage817929e2007-10-18 23:39:36 -0700452
453 /* No existing cgroup group matched */
454 return NULL;
455}
456
Paul Menage817929e2007-10-18 23:39:36 -0700457static void free_cg_links(struct list_head *tmp)
458{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700459 struct cg_cgroup_link *link;
460 struct cg_cgroup_link *saved_link;
461
462 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700463 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700464 kfree(link);
465 }
466}
467
468/*
Li Zefan36553432008-07-29 22:33:19 -0700469 * allocate_cg_links() allocates "count" cg_cgroup_link structures
470 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
471 * success or a negative error
472 */
473static int allocate_cg_links(int count, struct list_head *tmp)
474{
475 struct cg_cgroup_link *link;
476 int i;
477 INIT_LIST_HEAD(tmp);
478 for (i = 0; i < count; i++) {
479 link = kmalloc(sizeof(*link), GFP_KERNEL);
480 if (!link) {
481 free_cg_links(tmp);
482 return -ENOMEM;
483 }
484 list_add(&link->cgrp_link_list, tmp);
485 }
486 return 0;
487}
488
Li Zefanc12f65d2009-01-07 18:07:42 -0800489/**
490 * link_css_set - a helper function to link a css_set to a cgroup
491 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
492 * @cg: the css_set to be linked
493 * @cgrp: the destination cgroup
494 */
495static void link_css_set(struct list_head *tmp_cg_links,
496 struct css_set *cg, struct cgroup *cgrp)
497{
498 struct cg_cgroup_link *link;
499
500 BUG_ON(list_empty(tmp_cg_links));
501 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
502 cgrp_link_list);
503 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700504 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700505 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800506 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700507 /*
508 * Always add links to the tail of the list so that the list
509 * is sorted by order of hierarchy creation
510 */
511 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800512}
513
Li Zefan36553432008-07-29 22:33:19 -0700514/*
Paul Menage817929e2007-10-18 23:39:36 -0700515 * find_css_set() takes an existing cgroup group and a
516 * cgroup object, and returns a css_set object that's
517 * equivalent to the old group, but with the given cgroup
518 * substituted into the appropriate hierarchy. Must be called with
519 * cgroup_mutex held
520 */
Paul Menage817929e2007-10-18 23:39:36 -0700521static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700522 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700523{
524 struct css_set *res;
525 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700526
527 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700528
Li Zefan472b1052008-04-29 01:00:11 -0700529 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700530 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700531
Paul Menage817929e2007-10-18 23:39:36 -0700532 /* First see if we already have a cgroup group that matches
533 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700534 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700535 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700536 if (res)
537 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700538 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700539
540 if (res)
541 return res;
542
543 res = kmalloc(sizeof(*res), GFP_KERNEL);
544 if (!res)
545 return NULL;
546
547 /* Allocate all the cg_cgroup_link objects that we'll need */
548 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
549 kfree(res);
550 return NULL;
551 }
552
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700553 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700554 INIT_LIST_HEAD(&res->cg_links);
555 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700556 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700557
558 /* Copy the set of subsystem state objects generated in
559 * find_existing_css_set() */
560 memcpy(res->subsys, template, sizeof(res->subsys));
561
562 write_lock(&css_set_lock);
563 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700564 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
565 struct cgroup *c = link->cgrp;
566 if (c->root == cgrp->root)
567 c = cgrp;
568 link_css_set(&tmp_cg_links, res, c);
569 }
Paul Menage817929e2007-10-18 23:39:36 -0700570
571 BUG_ON(!list_empty(&tmp_cg_links));
572
Paul Menage817929e2007-10-18 23:39:36 -0700573 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700574
575 /* Add this cgroup group to the hash table */
576 hhead = css_set_hash(res->subsys);
577 hlist_add_head(&res->hlist, hhead);
578
Paul Menage817929e2007-10-18 23:39:36 -0700579 write_unlock(&css_set_lock);
580
581 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700582}
583
Paul Menageddbcc7e2007-10-18 23:39:30 -0700584/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700585 * Return the cgroup for "task" from the given hierarchy. Must be
586 * called with cgroup_mutex held.
587 */
588static struct cgroup *task_cgroup_from_root(struct task_struct *task,
589 struct cgroupfs_root *root)
590{
591 struct css_set *css;
592 struct cgroup *res = NULL;
593
594 BUG_ON(!mutex_is_locked(&cgroup_mutex));
595 read_lock(&css_set_lock);
596 /*
597 * No need to lock the task - since we hold cgroup_mutex the
598 * task can't change groups, so the only thing that can happen
599 * is that it exits and its css is set back to init_css_set.
600 */
601 css = task->cgroups;
602 if (css == &init_css_set) {
603 res = &root->top_cgroup;
604 } else {
605 struct cg_cgroup_link *link;
606 list_for_each_entry(link, &css->cg_links, cg_link_list) {
607 struct cgroup *c = link->cgrp;
608 if (c->root == root) {
609 res = c;
610 break;
611 }
612 }
613 }
614 read_unlock(&css_set_lock);
615 BUG_ON(!res);
616 return res;
617}
618
619/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700620 * There is one global cgroup mutex. We also require taking
621 * task_lock() when dereferencing a task's cgroup subsys pointers.
622 * See "The task_lock() exception", at the end of this comment.
623 *
624 * A task must hold cgroup_mutex to modify cgroups.
625 *
626 * Any task can increment and decrement the count field without lock.
627 * So in general, code holding cgroup_mutex can't rely on the count
628 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800629 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700630 * means that no tasks are currently attached, therefore there is no
631 * way a task attached to that cgroup can fork (the other way to
632 * increment the count). So code holding cgroup_mutex can safely
633 * assume that if the count is zero, it will stay zero. Similarly, if
634 * a task holds cgroup_mutex on a cgroup with zero count, it
635 * knows that the cgroup won't be removed, as cgroup_rmdir()
636 * needs that mutex.
637 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700638 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
639 * (usually) take cgroup_mutex. These are the two most performance
640 * critical pieces of code here. The exception occurs on cgroup_exit(),
641 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
642 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800643 * to the release agent with the name of the cgroup (path relative to
644 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700645 *
646 * A cgroup can only be deleted if both its 'count' of using tasks
647 * is zero, and its list of 'children' cgroups is empty. Since all
648 * tasks in the system use _some_ cgroup, and since there is always at
649 * least one task in the system (init, pid == 1), therefore, top_cgroup
650 * always has either children cgroups and/or using tasks. So we don't
651 * need a special hack to ensure that top_cgroup cannot be deleted.
652 *
653 * The task_lock() exception
654 *
655 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800656 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800657 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700658 * several performance critical places that need to reference
659 * task->cgroup without the expense of grabbing a system global
660 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800661 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700662 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
663 * the task_struct routinely used for such matters.
664 *
665 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800666 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700667 */
668
Paul Menageddbcc7e2007-10-18 23:39:30 -0700669/**
670 * cgroup_lock - lock out any changes to cgroup structures
671 *
672 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700673void cgroup_lock(void)
674{
675 mutex_lock(&cgroup_mutex);
676}
677
678/**
679 * cgroup_unlock - release lock on cgroup changes
680 *
681 * Undo the lock taken in a previous cgroup_lock() call.
682 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700683void cgroup_unlock(void)
684{
685 mutex_unlock(&cgroup_mutex);
686}
687
688/*
689 * A couple of forward declarations required, due to cyclic reference loop:
690 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
691 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
692 * -> cgroup_mkdir.
693 */
694
695static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
696static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700697static int cgroup_populate_dir(struct cgroup *cgrp);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700698static const struct inode_operations cgroup_dir_inode_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700699static struct file_operations proc_cgroupstats_operations;
700
701static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200702 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700703 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700704};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700705
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700706static int alloc_css_id(struct cgroup_subsys *ss,
707 struct cgroup *parent, struct cgroup *child);
708
Paul Menageddbcc7e2007-10-18 23:39:30 -0700709static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
710{
711 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700712
713 if (inode) {
714 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100715 inode->i_uid = current_fsuid();
716 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700717 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
718 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
719 }
720 return inode;
721}
722
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800723/*
724 * Call subsys's pre_destroy handler.
725 * This is called before css refcnt check.
726 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700727static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800728{
729 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700730 int ret = 0;
731
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800732 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700733 if (ss->pre_destroy) {
734 ret = ss->pre_destroy(ss, cgrp);
735 if (ret)
736 break;
737 }
738 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800739}
740
Paul Menagea47295e2009-01-07 18:07:44 -0800741static void free_cgroup_rcu(struct rcu_head *obj)
742{
743 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
744
745 kfree(cgrp);
746}
747
Paul Menageddbcc7e2007-10-18 23:39:30 -0700748static void cgroup_diput(struct dentry *dentry, struct inode *inode)
749{
750 /* is dentry a directory ? if so, kfree() associated cgroup */
751 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700752 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800753 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700754 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700755 /* It's possible for external users to be holding css
756 * reference counts on a cgroup; css_put() needs to
757 * be able to access the cgroup after decrementing
758 * the reference count in order to know if it needs to
759 * queue the cgroup to be handled by the release
760 * agent */
761 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800762
763 mutex_lock(&cgroup_mutex);
764 /*
765 * Release the subsystem state objects.
766 */
Li Zefan75139b82009-01-07 18:07:33 -0800767 for_each_subsys(cgrp->root, ss)
768 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800769
770 cgrp->root->number_of_cgroups--;
771 mutex_unlock(&cgroup_mutex);
772
Paul Menagea47295e2009-01-07 18:07:44 -0800773 /*
774 * Drop the active superblock reference that we took when we
775 * created the cgroup
776 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800777 deactivate_super(cgrp->root->sb);
778
Paul Menagea47295e2009-01-07 18:07:44 -0800779 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700780 }
781 iput(inode);
782}
783
784static void remove_dir(struct dentry *d)
785{
786 struct dentry *parent = dget(d->d_parent);
787
788 d_delete(d);
789 simple_rmdir(parent->d_inode, d);
790 dput(parent);
791}
792
793static void cgroup_clear_directory(struct dentry *dentry)
794{
795 struct list_head *node;
796
797 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
798 spin_lock(&dcache_lock);
799 node = dentry->d_subdirs.next;
800 while (node != &dentry->d_subdirs) {
801 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
802 list_del_init(node);
803 if (d->d_inode) {
804 /* This should never be called on a cgroup
805 * directory with child cgroups */
806 BUG_ON(d->d_inode->i_mode & S_IFDIR);
807 d = dget_locked(d);
808 spin_unlock(&dcache_lock);
809 d_delete(d);
810 simple_unlink(dentry->d_inode, d);
811 dput(d);
812 spin_lock(&dcache_lock);
813 }
814 node = dentry->d_subdirs.next;
815 }
816 spin_unlock(&dcache_lock);
817}
818
819/*
820 * NOTE : the dentry must have been dget()'ed
821 */
822static void cgroup_d_remove_dir(struct dentry *dentry)
823{
824 cgroup_clear_directory(dentry);
825
826 spin_lock(&dcache_lock);
827 list_del_init(&dentry->d_u.d_child);
828 spin_unlock(&dcache_lock);
829 remove_dir(dentry);
830}
831
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700832/*
833 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
834 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
835 * reference to css->refcnt. In general, this refcnt is expected to goes down
836 * to zero, soon.
837 *
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700838 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700839 */
840DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
841
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700842static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700843{
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700844 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700845 wake_up_all(&cgroup_rmdir_waitq);
846}
847
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700848void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
849{
850 css_get(css);
851}
852
853void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
854{
855 cgroup_wakeup_rmdir_waiter(css->cgroup);
856 css_put(css);
857}
858
859
Paul Menageddbcc7e2007-10-18 23:39:30 -0700860static int rebind_subsystems(struct cgroupfs_root *root,
861 unsigned long final_bits)
862{
863 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700864 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700865 int i;
866
867 removed_bits = root->actual_subsys_bits & ~final_bits;
868 added_bits = final_bits & ~root->actual_subsys_bits;
869 /* Check that any added subsystems are currently free */
870 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800871 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700872 struct cgroup_subsys *ss = subsys[i];
873 if (!(bit & added_bits))
874 continue;
875 if (ss->root != &rootnode) {
876 /* Subsystem isn't free */
877 return -EBUSY;
878 }
879 }
880
881 /* Currently we don't handle adding/removing subsystems when
882 * any child cgroups exist. This is theoretically supportable
883 * but involves complex error handling, so it's being left until
884 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800885 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700886 return -EBUSY;
887
888 /* Process each subsystem */
889 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
890 struct cgroup_subsys *ss = subsys[i];
891 unsigned long bit = 1UL << i;
892 if (bit & added_bits) {
893 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700894 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700895 BUG_ON(!dummytop->subsys[i]);
896 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800897 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700898 cgrp->subsys[i] = dummytop->subsys[i];
899 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800900 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800901 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700902 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700903 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800904 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700905 } else if (bit & removed_bits) {
906 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700907 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
908 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800909 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700910 if (ss->bind)
911 ss->bind(ss, dummytop);
912 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700913 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800914 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -0800915 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -0800916 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700917 } else if (bit & final_bits) {
918 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700919 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700920 } else {
921 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700922 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700923 }
924 }
925 root->subsys_bits = root->actual_subsys_bits = final_bits;
926 synchronize_rcu();
927
928 return 0;
929}
930
931static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
932{
933 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
934 struct cgroup_subsys *ss;
935
936 mutex_lock(&cgroup_mutex);
937 for_each_subsys(root, ss)
938 seq_printf(seq, ",%s", ss->name);
939 if (test_bit(ROOT_NOPREFIX, &root->flags))
940 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700941 if (strlen(root->release_agent_path))
942 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menagec6d57f32009-09-23 15:56:19 -0700943 if (strlen(root->name))
944 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700945 mutex_unlock(&cgroup_mutex);
946 return 0;
947}
948
949struct cgroup_sb_opts {
950 unsigned long subsys_bits;
951 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700952 char *release_agent;
Paul Menagec6d57f32009-09-23 15:56:19 -0700953 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700954 /* User explicitly requested empty subsystem */
955 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -0700956
957 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700958
Paul Menageddbcc7e2007-10-18 23:39:30 -0700959};
960
961/* Convert a hierarchy specifier into a bitmask of subsystems and
962 * flags. */
963static int parse_cgroupfs_options(char *data,
964 struct cgroup_sb_opts *opts)
965{
966 char *token, *o = data ?: "all";
Li Zefanf9ab5b52009-06-17 16:26:33 -0700967 unsigned long mask = (unsigned long)-1;
968
969#ifdef CONFIG_CPUSETS
970 mask = ~(1UL << cpuset_subsys_id);
971#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972
Paul Menagec6d57f32009-09-23 15:56:19 -0700973 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -0700974
975 while ((token = strsep(&o, ",")) != NULL) {
976 if (!*token)
977 return -EINVAL;
978 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700979 /* Add all non-disabled subsystems */
980 int i;
981 opts->subsys_bits = 0;
982 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
983 struct cgroup_subsys *ss = subsys[i];
984 if (!ss->disabled)
985 opts->subsys_bits |= 1ul << i;
986 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700987 } else if (!strcmp(token, "none")) {
988 /* Explicitly have no subsystems */
989 opts->none = true;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990 } else if (!strcmp(token, "noprefix")) {
991 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700992 } else if (!strncmp(token, "release_agent=", 14)) {
993 /* Specifying two release agents is forbidden */
994 if (opts->release_agent)
995 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -0700996 opts->release_agent =
997 kstrndup(token + 14, PATH_MAX, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700998 if (!opts->release_agent)
999 return -ENOMEM;
Paul Menagec6d57f32009-09-23 15:56:19 -07001000 } else if (!strncmp(token, "name=", 5)) {
1001 int i;
1002 const char *name = token + 5;
1003 /* Can't specify an empty name */
1004 if (!strlen(name))
1005 return -EINVAL;
1006 /* Must match [\w.-]+ */
1007 for (i = 0; i < strlen(name); i++) {
1008 char c = name[i];
1009 if (isalnum(c))
1010 continue;
1011 if ((c == '.') || (c == '-') || (c == '_'))
1012 continue;
1013 return -EINVAL;
1014 }
1015 /* Specifying two names is forbidden */
1016 if (opts->name)
1017 return -EINVAL;
1018 opts->name = kstrndup(name,
1019 MAX_CGROUP_ROOT_NAMELEN,
1020 GFP_KERNEL);
1021 if (!opts->name)
1022 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001023 } else {
1024 struct cgroup_subsys *ss;
1025 int i;
1026 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1027 ss = subsys[i];
1028 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07001029 if (!ss->disabled)
1030 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031 break;
1032 }
1033 }
1034 if (i == CGROUP_SUBSYS_COUNT)
1035 return -ENOENT;
1036 }
1037 }
1038
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001039 /* Consistency checks */
1040
Li Zefanf9ab5b52009-06-17 16:26:33 -07001041 /*
1042 * Option noprefix was introduced just for backward compatibility
1043 * with the old cpuset, so we allow noprefix only if mounting just
1044 * the cpuset subsystem.
1045 */
1046 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1047 (opts->subsys_bits & mask))
1048 return -EINVAL;
1049
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001050
1051 /* Can't specify "none" and some subsystems */
1052 if (opts->subsys_bits && opts->none)
1053 return -EINVAL;
1054
1055 /*
1056 * We either have to specify by name or by subsystems. (So all
1057 * empty hierarchies must have a name).
1058 */
Paul Menagec6d57f32009-09-23 15:56:19 -07001059 if (!opts->subsys_bits && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060 return -EINVAL;
1061
1062 return 0;
1063}
1064
1065static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1066{
1067 int ret = 0;
1068 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001069 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070 struct cgroup_sb_opts opts;
1071
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001072 lock_kernel();
Paul Menagebd89aab2007-10-18 23:40:44 -07001073 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074 mutex_lock(&cgroup_mutex);
1075
1076 /* See what subsystems are wanted */
1077 ret = parse_cgroupfs_options(data, &opts);
1078 if (ret)
1079 goto out_unlock;
1080
1081 /* Don't allow flags to change at remount */
1082 if (opts.flags != root->flags) {
1083 ret = -EINVAL;
1084 goto out_unlock;
1085 }
1086
Paul Menagec6d57f32009-09-23 15:56:19 -07001087 /* Don't allow name to change at remount */
1088 if (opts.name && strcmp(opts.name, root->name)) {
1089 ret = -EINVAL;
1090 goto out_unlock;
1091 }
1092
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093 ret = rebind_subsystems(root, opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -07001094 if (ret)
1095 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096
1097 /* (re)populate subsystem files */
Li Zefan0670e082009-04-02 16:57:30 -07001098 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099
Paul Menage81a6a5c2007-10-18 23:39:38 -07001100 if (opts.release_agent)
1101 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001103 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001104 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001105 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001106 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001107 unlock_kernel();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108 return ret;
1109}
1110
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001111static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001112 .statfs = simple_statfs,
1113 .drop_inode = generic_delete_inode,
1114 .show_options = cgroup_show_options,
1115 .remount_fs = cgroup_remount,
1116};
1117
Paul Menagecc31edc2008-10-18 20:28:04 -07001118static void init_cgroup_housekeeping(struct cgroup *cgrp)
1119{
1120 INIT_LIST_HEAD(&cgrp->sibling);
1121 INIT_LIST_HEAD(&cgrp->children);
1122 INIT_LIST_HEAD(&cgrp->css_sets);
1123 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum102a7752009-09-23 15:56:26 -07001124 init_rwsem(&(cgrp->tasks.mutex));
1125 init_rwsem(&(cgrp->procs.mutex));
Paul Menagecc31edc2008-10-18 20:28:04 -07001126}
Paul Menagec6d57f32009-09-23 15:56:19 -07001127
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128static void init_cgroup_root(struct cgroupfs_root *root)
1129{
Paul Menagebd89aab2007-10-18 23:40:44 -07001130 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131 INIT_LIST_HEAD(&root->subsys_list);
1132 INIT_LIST_HEAD(&root->root_list);
1133 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001134 cgrp->root = root;
1135 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001136 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137}
1138
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001139static bool init_root_id(struct cgroupfs_root *root)
1140{
1141 int ret = 0;
1142
1143 do {
1144 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1145 return false;
1146 spin_lock(&hierarchy_id_lock);
1147 /* Try to allocate the next unused ID */
1148 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1149 &root->hierarchy_id);
1150 if (ret == -ENOSPC)
1151 /* Try again starting from 0 */
1152 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1153 if (!ret) {
1154 next_hierarchy_id = root->hierarchy_id + 1;
1155 } else if (ret != -EAGAIN) {
1156 /* Can only get here if the 31-bit IDR is full ... */
1157 BUG_ON(ret);
1158 }
1159 spin_unlock(&hierarchy_id_lock);
1160 } while (ret);
1161 return true;
1162}
1163
Paul Menageddbcc7e2007-10-18 23:39:30 -07001164static int cgroup_test_super(struct super_block *sb, void *data)
1165{
Paul Menagec6d57f32009-09-23 15:56:19 -07001166 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001167 struct cgroupfs_root *root = sb->s_fs_info;
1168
Paul Menagec6d57f32009-09-23 15:56:19 -07001169 /* If we asked for a name then it must match */
1170 if (opts->name && strcmp(opts->name, root->name))
1171 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001172
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001173 /*
1174 * If we asked for subsystems (or explicitly for no
1175 * subsystems) then they must match
1176 */
1177 if ((opts->subsys_bits || opts->none)
1178 && (opts->subsys_bits != root->subsys_bits))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001179 return 0;
1180
1181 return 1;
1182}
1183
Paul Menagec6d57f32009-09-23 15:56:19 -07001184static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1185{
1186 struct cgroupfs_root *root;
1187
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001188 if (!opts->subsys_bits && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001189 return NULL;
1190
1191 root = kzalloc(sizeof(*root), GFP_KERNEL);
1192 if (!root)
1193 return ERR_PTR(-ENOMEM);
1194
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001195 if (!init_root_id(root)) {
1196 kfree(root);
1197 return ERR_PTR(-ENOMEM);
1198 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001199 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001200
Paul Menagec6d57f32009-09-23 15:56:19 -07001201 root->subsys_bits = opts->subsys_bits;
1202 root->flags = opts->flags;
1203 if (opts->release_agent)
1204 strcpy(root->release_agent_path, opts->release_agent);
1205 if (opts->name)
1206 strcpy(root->name, opts->name);
1207 return root;
1208}
1209
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001210static void cgroup_drop_root(struct cgroupfs_root *root)
1211{
1212 if (!root)
1213 return;
1214
1215 BUG_ON(!root->hierarchy_id);
1216 spin_lock(&hierarchy_id_lock);
1217 ida_remove(&hierarchy_ida, root->hierarchy_id);
1218 spin_unlock(&hierarchy_id_lock);
1219 kfree(root);
1220}
1221
Paul Menageddbcc7e2007-10-18 23:39:30 -07001222static int cgroup_set_super(struct super_block *sb, void *data)
1223{
1224 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001225 struct cgroup_sb_opts *opts = data;
1226
1227 /* If we don't have a new root, we can't set up a new sb */
1228 if (!opts->new_root)
1229 return -EINVAL;
1230
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001231 BUG_ON(!opts->subsys_bits && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001232
1233 ret = set_anon_super(sb, NULL);
1234 if (ret)
1235 return ret;
1236
Paul Menagec6d57f32009-09-23 15:56:19 -07001237 sb->s_fs_info = opts->new_root;
1238 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001239
1240 sb->s_blocksize = PAGE_CACHE_SIZE;
1241 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1242 sb->s_magic = CGROUP_SUPER_MAGIC;
1243 sb->s_op = &cgroup_ops;
1244
1245 return 0;
1246}
1247
1248static int cgroup_get_rootdir(struct super_block *sb)
1249{
1250 struct inode *inode =
1251 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1252 struct dentry *dentry;
1253
1254 if (!inode)
1255 return -ENOMEM;
1256
Paul Menageddbcc7e2007-10-18 23:39:30 -07001257 inode->i_fop = &simple_dir_operations;
1258 inode->i_op = &cgroup_dir_inode_operations;
1259 /* directories start off with i_nlink == 2 (for "." entry) */
1260 inc_nlink(inode);
1261 dentry = d_alloc_root(inode);
1262 if (!dentry) {
1263 iput(inode);
1264 return -ENOMEM;
1265 }
1266 sb->s_root = dentry;
1267 return 0;
1268}
1269
1270static int cgroup_get_sb(struct file_system_type *fs_type,
1271 int flags, const char *unused_dev_name,
1272 void *data, struct vfsmount *mnt)
1273{
1274 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001275 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001276 int ret = 0;
1277 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001278 struct cgroupfs_root *new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001279
1280 /* First find the desired set of subsystems */
1281 ret = parse_cgroupfs_options(data, &opts);
Paul Menagec6d57f32009-09-23 15:56:19 -07001282 if (ret)
1283 goto out_err;
1284
1285 /*
1286 * Allocate a new cgroup root. We may not need it if we're
1287 * reusing an existing hierarchy.
1288 */
1289 new_root = cgroup_root_from_opts(&opts);
1290 if (IS_ERR(new_root)) {
1291 ret = PTR_ERR(new_root);
1292 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001293 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001294 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001295
Paul Menagec6d57f32009-09-23 15:56:19 -07001296 /* Locate an existing or new sb for this hierarchy */
1297 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001298 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001299 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001300 cgroup_drop_root(opts.new_root);
Paul Menagec6d57f32009-09-23 15:56:19 -07001301 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001302 }
1303
Paul Menagec6d57f32009-09-23 15:56:19 -07001304 root = sb->s_fs_info;
1305 BUG_ON(!root);
1306 if (root == opts.new_root) {
1307 /* We used the new root structure, so this is a new hierarchy */
1308 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001309 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001310 struct inode *inode;
Paul Menagec6d57f32009-09-23 15:56:19 -07001311 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001312 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313
1314 BUG_ON(sb->s_root != NULL);
1315
1316 ret = cgroup_get_rootdir(sb);
1317 if (ret)
1318 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001319 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001320
Paul Menage817929e2007-10-18 23:39:36 -07001321 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322 mutex_lock(&cgroup_mutex);
1323
Paul Menagec6d57f32009-09-23 15:56:19 -07001324 if (strlen(root->name)) {
1325 /* Check for name clashes with existing mounts */
1326 for_each_active_root(existing_root) {
1327 if (!strcmp(existing_root->name, root->name)) {
1328 ret = -EBUSY;
1329 mutex_unlock(&cgroup_mutex);
1330 mutex_unlock(&inode->i_mutex);
1331 goto drop_new_super;
1332 }
1333 }
1334 }
1335
Paul Menage817929e2007-10-18 23:39:36 -07001336 /*
1337 * We're accessing css_set_count without locking
1338 * css_set_lock here, but that's OK - it can only be
1339 * increased by someone holding cgroup_lock, and
1340 * that's us. The worst that can happen is that we
1341 * have some link structures left over
1342 */
1343 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1344 if (ret) {
1345 mutex_unlock(&cgroup_mutex);
1346 mutex_unlock(&inode->i_mutex);
1347 goto drop_new_super;
1348 }
1349
Paul Menageddbcc7e2007-10-18 23:39:30 -07001350 ret = rebind_subsystems(root, root->subsys_bits);
1351 if (ret == -EBUSY) {
1352 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001353 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001354 free_cg_links(&tmp_cg_links);
1355 goto drop_new_super;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001356 }
1357
1358 /* EBUSY should be the only error here */
1359 BUG_ON(ret);
1360
1361 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001362 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001363
Li Zefanc12f65d2009-01-07 18:07:42 -08001364 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365 root->top_cgroup.dentry = sb->s_root;
1366
Paul Menage817929e2007-10-18 23:39:36 -07001367 /* Link the top cgroup in this hierarchy into all
1368 * the css_set objects */
1369 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001370 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1371 struct hlist_head *hhead = &css_set_table[i];
1372 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001373 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001374
Li Zefanc12f65d2009-01-07 18:07:42 -08001375 hlist_for_each_entry(cg, node, hhead, hlist)
1376 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001377 }
Paul Menage817929e2007-10-18 23:39:36 -07001378 write_unlock(&css_set_lock);
1379
1380 free_cg_links(&tmp_cg_links);
1381
Li Zefanc12f65d2009-01-07 18:07:42 -08001382 BUG_ON(!list_empty(&root_cgrp->sibling));
1383 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001384 BUG_ON(root->number_of_cgroups != 1);
1385
Li Zefanc12f65d2009-01-07 18:07:42 -08001386 cgroup_populate_dir(root_cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001387 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001388 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001389 } else {
1390 /*
1391 * We re-used an existing hierarchy - the new root (if
1392 * any) is not needed
1393 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001394 cgroup_drop_root(opts.new_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001395 }
1396
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001397 simple_set_mnt(mnt, sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001398 kfree(opts.release_agent);
1399 kfree(opts.name);
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001400 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401
1402 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001403 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001404 out_err:
1405 kfree(opts.release_agent);
1406 kfree(opts.name);
1407
Paul Menageddbcc7e2007-10-18 23:39:30 -07001408 return ret;
1409}
1410
1411static void cgroup_kill_sb(struct super_block *sb) {
1412 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001413 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001415 struct cg_cgroup_link *link;
1416 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001417
1418 BUG_ON(!root);
1419
1420 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001421 BUG_ON(!list_empty(&cgrp->children));
1422 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001423
1424 mutex_lock(&cgroup_mutex);
1425
1426 /* Rebind all subsystems back to the default hierarchy */
1427 ret = rebind_subsystems(root, 0);
1428 /* Shouldn't be able to fail ... */
1429 BUG_ON(ret);
1430
Paul Menage817929e2007-10-18 23:39:36 -07001431 /*
1432 * Release all the links from css_sets to this hierarchy's
1433 * root cgroup
1434 */
1435 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001436
1437 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1438 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001439 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001440 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001441 kfree(link);
1442 }
1443 write_unlock(&css_set_lock);
1444
Paul Menage839ec542009-01-29 14:25:22 -08001445 if (!list_empty(&root->root_list)) {
1446 list_del(&root->root_list);
1447 root_count--;
1448 }
Li Zefane5f6a862009-01-07 18:07:41 -08001449
Paul Menageddbcc7e2007-10-18 23:39:30 -07001450 mutex_unlock(&cgroup_mutex);
1451
Paul Menageddbcc7e2007-10-18 23:39:30 -07001452 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001453 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454}
1455
1456static struct file_system_type cgroup_fs_type = {
1457 .name = "cgroup",
1458 .get_sb = cgroup_get_sb,
1459 .kill_sb = cgroup_kill_sb,
1460};
1461
Paul Menagebd89aab2007-10-18 23:40:44 -07001462static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001463{
1464 return dentry->d_fsdata;
1465}
1466
1467static inline struct cftype *__d_cft(struct dentry *dentry)
1468{
1469 return dentry->d_fsdata;
1470}
1471
Li Zefana043e3b2008-02-23 15:24:09 -08001472/**
1473 * cgroup_path - generate the path of a cgroup
1474 * @cgrp: the cgroup in question
1475 * @buf: the buffer to write the path into
1476 * @buflen: the length of the buffer
1477 *
Paul Menagea47295e2009-01-07 18:07:44 -08001478 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1479 * reference. Writes path of cgroup into buf. Returns 0 on success,
1480 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001481 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001482int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001483{
1484 char *start;
Paul Menagea47295e2009-01-07 18:07:44 -08001485 struct dentry *dentry = rcu_dereference(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001486
Paul Menagea47295e2009-01-07 18:07:44 -08001487 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001488 /*
1489 * Inactive subsystems have no dentry for their root
1490 * cgroup
1491 */
1492 strcpy(buf, "/");
1493 return 0;
1494 }
1495
1496 start = buf + buflen;
1497
1498 *--start = '\0';
1499 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001500 int len = dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001501 if ((start -= len) < buf)
1502 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001503 memcpy(start, cgrp->dentry->d_name.name, len);
1504 cgrp = cgrp->parent;
1505 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001506 break;
Paul Menagea47295e2009-01-07 18:07:44 -08001507 dentry = rcu_dereference(cgrp->dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001508 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001509 continue;
1510 if (--start < buf)
1511 return -ENAMETOOLONG;
1512 *start = '/';
1513 }
1514 memmove(buf, start, buf + buflen - start);
1515 return 0;
1516}
1517
Li Zefana043e3b2008-02-23 15:24:09 -08001518/**
1519 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1520 * @cgrp: the cgroup the task is attaching to
1521 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001522 *
Li Zefana043e3b2008-02-23 15:24:09 -08001523 * Call holding cgroup_mutex. May take task_lock of
1524 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001525 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001526int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001527{
1528 int retval = 0;
1529 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07001530 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001531 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001532 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001533 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001534
1535 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001536 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001537 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001538 return 0;
1539
1540 for_each_subsys(root, ss) {
1541 if (ss->can_attach) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001542 retval = ss->can_attach(ss, cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001543 if (retval)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001544 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001545 }
1546 }
1547
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001548 task_lock(tsk);
1549 cg = tsk->cgroups;
1550 get_css_set(cg);
1551 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001552 /*
1553 * Locate or allocate a new css_set for this task,
1554 * based on its final set of cgroups
1555 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001556 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001557 put_css_set(cg);
Paul Jacksone18f6312008-02-07 00:13:44 -08001558 if (!newcg)
Paul Menage817929e2007-10-18 23:39:36 -07001559 return -ENOMEM;
Paul Menage817929e2007-10-18 23:39:36 -07001560
Paul Menagebbcb81d2007-10-18 23:39:32 -07001561 task_lock(tsk);
1562 if (tsk->flags & PF_EXITING) {
1563 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001564 put_css_set(newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001565 return -ESRCH;
1566 }
Paul Menage817929e2007-10-18 23:39:36 -07001567 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001568 task_unlock(tsk);
1569
Paul Menage817929e2007-10-18 23:39:36 -07001570 /* Update the css_set linked lists if we're using them */
1571 write_lock(&css_set_lock);
1572 if (!list_empty(&tsk->cg_list)) {
1573 list_del(&tsk->cg_list);
1574 list_add(&tsk->cg_list, &newcg->tasks);
1575 }
1576 write_unlock(&css_set_lock);
1577
Paul Menagebbcb81d2007-10-18 23:39:32 -07001578 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001579 if (ss->attach)
Paul Menagebd89aab2007-10-18 23:40:44 -07001580 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001581 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001582 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001583 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001584 put_css_set(cg);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001585
1586 /*
1587 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1588 * is no longer empty.
1589 */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001590 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001591 return 0;
1592}
1593
1594/*
Paul Menageaf351022008-07-25 01:47:01 -07001595 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1596 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001597 */
Paul Menageaf351022008-07-25 01:47:01 -07001598static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001599{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001600 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001601 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001602 int ret;
1603
Paul Menagebbcb81d2007-10-18 23:39:32 -07001604 if (pid) {
1605 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001606 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001607 if (!tsk || tsk->flags & PF_EXITING) {
1608 rcu_read_unlock();
1609 return -ESRCH;
1610 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001611
David Howellsc69e8d92008-11-14 10:39:19 +11001612 tcred = __task_cred(tsk);
1613 if (cred->euid &&
1614 cred->euid != tcred->uid &&
1615 cred->euid != tcred->suid) {
1616 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001617 return -EACCES;
1618 }
David Howellsc69e8d92008-11-14 10:39:19 +11001619 get_task_struct(tsk);
1620 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001621 } else {
1622 tsk = current;
1623 get_task_struct(tsk);
1624 }
1625
Cliff Wickman956db3c2008-02-07 00:14:43 -08001626 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001627 put_task_struct(tsk);
1628 return ret;
1629}
1630
Paul Menageaf351022008-07-25 01:47:01 -07001631static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1632{
1633 int ret;
1634 if (!cgroup_lock_live_group(cgrp))
1635 return -ENODEV;
1636 ret = attach_task_by_pid(cgrp, pid);
1637 cgroup_unlock();
1638 return ret;
1639}
1640
Paul Menagee788e0662008-07-25 01:46:59 -07001641/**
1642 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1643 * @cgrp: the cgroup to be checked for liveness
1644 *
Paul Menage84eea842008-07-25 01:47:00 -07001645 * On success, returns true; the lock should be later released with
1646 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e0662008-07-25 01:46:59 -07001647 */
Paul Menage84eea842008-07-25 01:47:00 -07001648bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e0662008-07-25 01:46:59 -07001649{
1650 mutex_lock(&cgroup_mutex);
1651 if (cgroup_is_removed(cgrp)) {
1652 mutex_unlock(&cgroup_mutex);
1653 return false;
1654 }
1655 return true;
1656}
1657
1658static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1659 const char *buffer)
1660{
1661 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1662 if (!cgroup_lock_live_group(cgrp))
1663 return -ENODEV;
1664 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001665 cgroup_unlock();
Paul Menagee788e0662008-07-25 01:46:59 -07001666 return 0;
1667}
1668
1669static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1670 struct seq_file *seq)
1671{
1672 if (!cgroup_lock_live_group(cgrp))
1673 return -ENODEV;
1674 seq_puts(seq, cgrp->root->release_agent_path);
1675 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001676 cgroup_unlock();
Paul Menagee788e0662008-07-25 01:46:59 -07001677 return 0;
1678}
1679
Paul Menage84eea842008-07-25 01:47:00 -07001680/* A buffer size big enough for numbers or short strings */
1681#define CGROUP_LOCAL_BUFFER_SIZE 64
1682
Paul Menagee73d2c62008-04-29 01:00:06 -07001683static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001684 struct file *file,
1685 const char __user *userbuf,
1686 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001687{
Paul Menage84eea842008-07-25 01:47:00 -07001688 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001689 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001690 char *end;
1691
1692 if (!nbytes)
1693 return -EINVAL;
1694 if (nbytes >= sizeof(buffer))
1695 return -E2BIG;
1696 if (copy_from_user(buffer, userbuf, nbytes))
1697 return -EFAULT;
1698
1699 buffer[nbytes] = 0; /* nul-terminate */
Paul Menageb7269df2008-04-29 00:59:59 -07001700 strstrip(buffer);
Paul Menagee73d2c62008-04-29 01:00:06 -07001701 if (cft->write_u64) {
1702 u64 val = simple_strtoull(buffer, &end, 0);
1703 if (*end)
1704 return -EINVAL;
1705 retval = cft->write_u64(cgrp, cft, val);
1706 } else {
1707 s64 val = simple_strtoll(buffer, &end, 0);
1708 if (*end)
1709 return -EINVAL;
1710 retval = cft->write_s64(cgrp, cft, val);
1711 }
Paul Menage355e0c42007-10-18 23:39:33 -07001712 if (!retval)
1713 retval = nbytes;
1714 return retval;
1715}
1716
Paul Menagedb3b1492008-07-25 01:46:58 -07001717static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1718 struct file *file,
1719 const char __user *userbuf,
1720 size_t nbytes, loff_t *unused_ppos)
1721{
Paul Menage84eea842008-07-25 01:47:00 -07001722 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001723 int retval = 0;
1724 size_t max_bytes = cft->max_write_len;
1725 char *buffer = local_buffer;
1726
1727 if (!max_bytes)
1728 max_bytes = sizeof(local_buffer) - 1;
1729 if (nbytes >= max_bytes)
1730 return -E2BIG;
1731 /* Allocate a dynamic buffer if we need one */
1732 if (nbytes >= sizeof(local_buffer)) {
1733 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1734 if (buffer == NULL)
1735 return -ENOMEM;
1736 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001737 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1738 retval = -EFAULT;
1739 goto out;
1740 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001741
1742 buffer[nbytes] = 0; /* nul-terminate */
1743 strstrip(buffer);
1744 retval = cft->write_string(cgrp, cft, buffer);
1745 if (!retval)
1746 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001747out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001748 if (buffer != local_buffer)
1749 kfree(buffer);
1750 return retval;
1751}
1752
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1754 size_t nbytes, loff_t *ppos)
1755{
1756 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001757 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758
Li Zefan75139b82009-01-07 18:07:33 -08001759 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001761 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001762 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001763 if (cft->write_u64 || cft->write_s64)
1764 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001765 if (cft->write_string)
1766 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001767 if (cft->trigger) {
1768 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1769 return ret ? ret : nbytes;
1770 }
Paul Menage355e0c42007-10-18 23:39:33 -07001771 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772}
1773
Paul Menagef4c753b2008-04-29 00:59:56 -07001774static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1775 struct file *file,
1776 char __user *buf, size_t nbytes,
1777 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778{
Paul Menage84eea842008-07-25 01:47:00 -07001779 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001780 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1782
1783 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1784}
1785
Paul Menagee73d2c62008-04-29 01:00:06 -07001786static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1787 struct file *file,
1788 char __user *buf, size_t nbytes,
1789 loff_t *ppos)
1790{
Paul Menage84eea842008-07-25 01:47:00 -07001791 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001792 s64 val = cft->read_s64(cgrp, cft);
1793 int len = sprintf(tmp, "%lld\n", (long long) val);
1794
1795 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1796}
1797
Paul Menageddbcc7e2007-10-18 23:39:30 -07001798static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1799 size_t nbytes, loff_t *ppos)
1800{
1801 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001802 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803
Li Zefan75139b82009-01-07 18:07:33 -08001804 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001805 return -ENODEV;
1806
1807 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001808 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001809 if (cft->read_u64)
1810 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001811 if (cft->read_s64)
1812 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813 return -EINVAL;
1814}
1815
Paul Menage91796562008-04-29 01:00:01 -07001816/*
1817 * seqfile ops/methods for returning structured data. Currently just
1818 * supports string->u64 maps, but can be extended in future.
1819 */
1820
1821struct cgroup_seqfile_state {
1822 struct cftype *cft;
1823 struct cgroup *cgroup;
1824};
1825
1826static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1827{
1828 struct seq_file *sf = cb->state;
1829 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1830}
1831
1832static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1833{
1834 struct cgroup_seqfile_state *state = m->private;
1835 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001836 if (cft->read_map) {
1837 struct cgroup_map_cb cb = {
1838 .fill = cgroup_map_add,
1839 .state = m,
1840 };
1841 return cft->read_map(state->cgroup, cft, &cb);
1842 }
1843 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001844}
1845
Adrian Bunk96930a62008-07-25 19:46:21 -07001846static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07001847{
1848 struct seq_file *seq = file->private_data;
1849 kfree(seq->private);
1850 return single_release(inode, file);
1851}
1852
1853static struct file_operations cgroup_seqfile_operations = {
1854 .read = seq_read,
Paul Menagee788e0662008-07-25 01:46:59 -07001855 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07001856 .llseek = seq_lseek,
1857 .release = cgroup_seqfile_release,
1858};
1859
Paul Menageddbcc7e2007-10-18 23:39:30 -07001860static int cgroup_file_open(struct inode *inode, struct file *file)
1861{
1862 int err;
1863 struct cftype *cft;
1864
1865 err = generic_file_open(inode, file);
1866 if (err)
1867 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001868 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08001869
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001870 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001871 struct cgroup_seqfile_state *state =
1872 kzalloc(sizeof(*state), GFP_USER);
1873 if (!state)
1874 return -ENOMEM;
1875 state->cft = cft;
1876 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1877 file->f_op = &cgroup_seqfile_operations;
1878 err = single_open(file, cgroup_seqfile_show, state);
1879 if (err < 0)
1880 kfree(state);
1881 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001882 err = cft->open(inode, file);
1883 else
1884 err = 0;
1885
1886 return err;
1887}
1888
1889static int cgroup_file_release(struct inode *inode, struct file *file)
1890{
1891 struct cftype *cft = __d_cft(file->f_dentry);
1892 if (cft->release)
1893 return cft->release(inode, file);
1894 return 0;
1895}
1896
1897/*
1898 * cgroup_rename - Only allow simple rename of directories in place.
1899 */
1900static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1901 struct inode *new_dir, struct dentry *new_dentry)
1902{
1903 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1904 return -ENOTDIR;
1905 if (new_dentry->d_inode)
1906 return -EEXIST;
1907 if (old_dir != new_dir)
1908 return -EIO;
1909 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1910}
1911
1912static struct file_operations cgroup_file_operations = {
1913 .read = cgroup_file_read,
1914 .write = cgroup_file_write,
1915 .llseek = generic_file_llseek,
1916 .open = cgroup_file_open,
1917 .release = cgroup_file_release,
1918};
1919
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07001920static const struct inode_operations cgroup_dir_inode_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001921 .lookup = simple_lookup,
1922 .mkdir = cgroup_mkdir,
1923 .rmdir = cgroup_rmdir,
1924 .rename = cgroup_rename,
1925};
1926
Li Zefan099fca32009-04-02 16:57:29 -07001927static int cgroup_create_file(struct dentry *dentry, mode_t mode,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001928 struct super_block *sb)
1929{
Al Viro3ba13d12009-02-20 06:02:22 +00001930 static const struct dentry_operations cgroup_dops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001931 .d_iput = cgroup_diput,
1932 };
1933
1934 struct inode *inode;
1935
1936 if (!dentry)
1937 return -ENOENT;
1938 if (dentry->d_inode)
1939 return -EEXIST;
1940
1941 inode = cgroup_new_inode(mode, sb);
1942 if (!inode)
1943 return -ENOMEM;
1944
1945 if (S_ISDIR(mode)) {
1946 inode->i_op = &cgroup_dir_inode_operations;
1947 inode->i_fop = &simple_dir_operations;
1948
1949 /* start off with i_nlink == 2 (for "." entry) */
1950 inc_nlink(inode);
1951
1952 /* start with the directory inode held, so that we can
1953 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07001954 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001955 } else if (S_ISREG(mode)) {
1956 inode->i_size = 0;
1957 inode->i_fop = &cgroup_file_operations;
1958 }
1959 dentry->d_op = &cgroup_dops;
1960 d_instantiate(dentry, inode);
1961 dget(dentry); /* Extra count - pin the dentry in core */
1962 return 0;
1963}
1964
1965/*
Li Zefana043e3b2008-02-23 15:24:09 -08001966 * cgroup_create_dir - create a directory for an object.
1967 * @cgrp: the cgroup we create the directory for. It must have a valid
1968 * ->parent field. And we are going to fill its ->dentry field.
1969 * @dentry: dentry of the new cgroup
1970 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001971 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001972static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07001973 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001974{
1975 struct dentry *parent;
1976 int error = 0;
1977
Paul Menagebd89aab2007-10-18 23:40:44 -07001978 parent = cgrp->parent->dentry;
1979 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001980 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001981 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001982 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08001983 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001984 dget(dentry);
1985 }
1986 dput(dentry);
1987
1988 return error;
1989}
1990
Li Zefan099fca32009-04-02 16:57:29 -07001991/**
1992 * cgroup_file_mode - deduce file mode of a control file
1993 * @cft: the control file in question
1994 *
1995 * returns cft->mode if ->mode is not 0
1996 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1997 * returns S_IRUGO if it has only a read handler
1998 * returns S_IWUSR if it has only a write hander
1999 */
2000static mode_t cgroup_file_mode(const struct cftype *cft)
2001{
2002 mode_t mode = 0;
2003
2004 if (cft->mode)
2005 return cft->mode;
2006
2007 if (cft->read || cft->read_u64 || cft->read_s64 ||
2008 cft->read_map || cft->read_seq_string)
2009 mode |= S_IRUGO;
2010
2011 if (cft->write || cft->write_u64 || cft->write_s64 ||
2012 cft->write_string || cft->trigger)
2013 mode |= S_IWUSR;
2014
2015 return mode;
2016}
2017
Paul Menagebd89aab2007-10-18 23:40:44 -07002018int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002019 struct cgroup_subsys *subsys,
2020 const struct cftype *cft)
2021{
Paul Menagebd89aab2007-10-18 23:40:44 -07002022 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002023 struct dentry *dentry;
2024 int error;
Li Zefan099fca32009-04-02 16:57:29 -07002025 mode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002026
2027 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07002028 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002029 strcpy(name, subsys->name);
2030 strcat(name, ".");
2031 }
2032 strcat(name, cft->name);
2033 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2034 dentry = lookup_one_len(name, dir, strlen(name));
2035 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07002036 mode = cgroup_file_mode(cft);
2037 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07002038 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002039 if (!error)
2040 dentry->d_fsdata = (void *)cft;
2041 dput(dentry);
2042 } else
2043 error = PTR_ERR(dentry);
2044 return error;
2045}
2046
Paul Menagebd89aab2007-10-18 23:40:44 -07002047int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002048 struct cgroup_subsys *subsys,
2049 const struct cftype cft[],
2050 int count)
2051{
2052 int i, err;
2053 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002054 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002055 if (err)
2056 return err;
2057 }
2058 return 0;
2059}
2060
Li Zefana043e3b2008-02-23 15:24:09 -08002061/**
2062 * cgroup_task_count - count the number of tasks in a cgroup.
2063 * @cgrp: the cgroup in question
2064 *
2065 * Return the number of tasks in the cgroup.
2066 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002067int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002068{
2069 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002070 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002071
Paul Menage817929e2007-10-18 23:39:36 -07002072 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002073 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002074 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002075 }
2076 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002077 return count;
2078}
2079
2080/*
Paul Menage817929e2007-10-18 23:39:36 -07002081 * Advance a list_head iterator. The iterator should be positioned at
2082 * the start of a css_set
2083 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002084static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002085 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002086{
2087 struct list_head *l = it->cg_link;
2088 struct cg_cgroup_link *link;
2089 struct css_set *cg;
2090
2091 /* Advance to the next non-empty css_set */
2092 do {
2093 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002094 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002095 it->cg_link = NULL;
2096 return;
2097 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002098 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002099 cg = link->cg;
2100 } while (list_empty(&cg->tasks));
2101 it->cg_link = l;
2102 it->task = cg->tasks.next;
2103}
2104
Cliff Wickman31a7df02008-02-07 00:14:42 -08002105/*
2106 * To reduce the fork() overhead for systems that are not actually
2107 * using their cgroups capability, we don't maintain the lists running
2108 * through each css_set to its tasks until we see the list actually
2109 * used - in other words after the first call to cgroup_iter_start().
2110 *
2111 * The tasklist_lock is not held here, as do_each_thread() and
2112 * while_each_thread() are protected by RCU.
2113 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002114static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002115{
2116 struct task_struct *p, *g;
2117 write_lock(&css_set_lock);
2118 use_task_css_set_links = 1;
2119 do_each_thread(g, p) {
2120 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002121 /*
2122 * We should check if the process is exiting, otherwise
2123 * it will race with cgroup_exit() in that the list
2124 * entry won't be deleted though the process has exited.
2125 */
2126 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002127 list_add(&p->cg_list, &p->cgroups->tasks);
2128 task_unlock(p);
2129 } while_each_thread(g, p);
2130 write_unlock(&css_set_lock);
2131}
2132
Paul Menagebd89aab2007-10-18 23:40:44 -07002133void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002134{
2135 /*
2136 * The first time anyone tries to iterate across a cgroup,
2137 * we need to enable the list linking each css_set to its
2138 * tasks, and fix up all existing tasks.
2139 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002140 if (!use_task_css_set_links)
2141 cgroup_enable_task_cg_lists();
2142
Paul Menage817929e2007-10-18 23:39:36 -07002143 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002144 it->cg_link = &cgrp->css_sets;
2145 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002146}
2147
Paul Menagebd89aab2007-10-18 23:40:44 -07002148struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07002149 struct cgroup_iter *it)
2150{
2151 struct task_struct *res;
2152 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002153 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002154
2155 /* If the iterator cg is NULL, we have no tasks */
2156 if (!it->cg_link)
2157 return NULL;
2158 res = list_entry(l, struct task_struct, cg_list);
2159 /* Advance iterator to find next entry */
2160 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002161 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2162 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07002163 /* We reached the end of this task list - move on to
2164 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07002165 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002166 } else {
2167 it->task = l;
2168 }
2169 return res;
2170}
2171
Paul Menagebd89aab2007-10-18 23:40:44 -07002172void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002173{
2174 read_unlock(&css_set_lock);
2175}
2176
Cliff Wickman31a7df02008-02-07 00:14:42 -08002177static inline int started_after_time(struct task_struct *t1,
2178 struct timespec *time,
2179 struct task_struct *t2)
2180{
2181 int start_diff = timespec_compare(&t1->start_time, time);
2182 if (start_diff > 0) {
2183 return 1;
2184 } else if (start_diff < 0) {
2185 return 0;
2186 } else {
2187 /*
2188 * Arbitrarily, if two processes started at the same
2189 * time, we'll say that the lower pointer value
2190 * started first. Note that t2 may have exited by now
2191 * so this may not be a valid pointer any longer, but
2192 * that's fine - it still serves to distinguish
2193 * between two tasks started (effectively) simultaneously.
2194 */
2195 return t1 > t2;
2196 }
2197}
2198
2199/*
2200 * This function is a callback from heap_insert() and is used to order
2201 * the heap.
2202 * In this case we order the heap in descending task start time.
2203 */
2204static inline int started_after(void *p1, void *p2)
2205{
2206 struct task_struct *t1 = p1;
2207 struct task_struct *t2 = p2;
2208 return started_after_time(t1, &t2->start_time, t2);
2209}
2210
2211/**
2212 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2213 * @scan: struct cgroup_scanner containing arguments for the scan
2214 *
2215 * Arguments include pointers to callback functions test_task() and
2216 * process_task().
2217 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2218 * and if it returns true, call process_task() for it also.
2219 * The test_task pointer may be NULL, meaning always true (select all tasks).
2220 * Effectively duplicates cgroup_iter_{start,next,end}()
2221 * but does not lock css_set_lock for the call to process_task().
2222 * The struct cgroup_scanner may be embedded in any structure of the caller's
2223 * creation.
2224 * It is guaranteed that process_task() will act on every task that
2225 * is a member of the cgroup for the duration of this call. This
2226 * function may or may not call process_task() for tasks that exit
2227 * or move to a different cgroup during the call, or are forked or
2228 * move into the cgroup during the call.
2229 *
2230 * Note that test_task() may be called with locks held, and may in some
2231 * situations be called multiple times for the same task, so it should
2232 * be cheap.
2233 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2234 * pre-allocated and will be used for heap operations (and its "gt" member will
2235 * be overwritten), else a temporary heap will be used (allocation of which
2236 * may cause this function to fail).
2237 */
2238int cgroup_scan_tasks(struct cgroup_scanner *scan)
2239{
2240 int retval, i;
2241 struct cgroup_iter it;
2242 struct task_struct *p, *dropped;
2243 /* Never dereference latest_task, since it's not refcounted */
2244 struct task_struct *latest_task = NULL;
2245 struct ptr_heap tmp_heap;
2246 struct ptr_heap *heap;
2247 struct timespec latest_time = { 0, 0 };
2248
2249 if (scan->heap) {
2250 /* The caller supplied our heap and pre-allocated its memory */
2251 heap = scan->heap;
2252 heap->gt = &started_after;
2253 } else {
2254 /* We need to allocate our own heap memory */
2255 heap = &tmp_heap;
2256 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2257 if (retval)
2258 /* cannot allocate the heap */
2259 return retval;
2260 }
2261
2262 again:
2263 /*
2264 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2265 * to determine which are of interest, and using the scanner's
2266 * "process_task" callback to process any of them that need an update.
2267 * Since we don't want to hold any locks during the task updates,
2268 * gather tasks to be processed in a heap structure.
2269 * The heap is sorted by descending task start time.
2270 * If the statically-sized heap fills up, we overflow tasks that
2271 * started later, and in future iterations only consider tasks that
2272 * started after the latest task in the previous pass. This
2273 * guarantees forward progress and that we don't miss any tasks.
2274 */
2275 heap->size = 0;
2276 cgroup_iter_start(scan->cg, &it);
2277 while ((p = cgroup_iter_next(scan->cg, &it))) {
2278 /*
2279 * Only affect tasks that qualify per the caller's callback,
2280 * if he provided one
2281 */
2282 if (scan->test_task && !scan->test_task(p, scan))
2283 continue;
2284 /*
2285 * Only process tasks that started after the last task
2286 * we processed
2287 */
2288 if (!started_after_time(p, &latest_time, latest_task))
2289 continue;
2290 dropped = heap_insert(heap, p);
2291 if (dropped == NULL) {
2292 /*
2293 * The new task was inserted; the heap wasn't
2294 * previously full
2295 */
2296 get_task_struct(p);
2297 } else if (dropped != p) {
2298 /*
2299 * The new task was inserted, and pushed out a
2300 * different task
2301 */
2302 get_task_struct(p);
2303 put_task_struct(dropped);
2304 }
2305 /*
2306 * Else the new task was newer than anything already in
2307 * the heap and wasn't inserted
2308 */
2309 }
2310 cgroup_iter_end(scan->cg, &it);
2311
2312 if (heap->size) {
2313 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002314 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002315 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002316 latest_time = q->start_time;
2317 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002318 }
2319 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002320 scan->process_task(q, scan);
2321 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002322 }
2323 /*
2324 * If we had to process any tasks at all, scan again
2325 * in case some of them were in the middle of forking
2326 * children that didn't get processed.
2327 * Not the most efficient way to do it, but it avoids
2328 * having to take callback_mutex in the fork path
2329 */
2330 goto again;
2331 }
2332 if (heap == &tmp_heap)
2333 heap_free(&tmp_heap);
2334 return 0;
2335}
2336
Paul Menage817929e2007-10-18 23:39:36 -07002337/*
Ben Blum102a7752009-09-23 15:56:26 -07002338 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002339 *
2340 * Reading this file can return large amounts of data if a cgroup has
2341 * *lots* of attached tasks. So it may need several calls to read(),
2342 * but we cannot guarantee that the information we produce is correct
2343 * unless we produce it entirely atomically.
2344 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002345 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002346
2347/*
Ben Blum102a7752009-09-23 15:56:26 -07002348 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
2349 * If the new stripped list is sufficiently smaller and there's enough memory
2350 * to allocate a new buffer, will let go of the unneeded memory. Returns the
2351 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002352 */
Ben Blum102a7752009-09-23 15:56:26 -07002353/* is the size difference enough that we should re-allocate the array? */
2354#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
2355static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002356{
Ben Blum102a7752009-09-23 15:56:26 -07002357 int src, dest = 1;
2358 pid_t *list = *p;
2359 pid_t *newlist;
2360
2361 /*
2362 * we presume the 0th element is unique, so i starts at 1. trivial
2363 * edge cases first; no work needs to be done for either
2364 */
2365 if (length == 0 || length == 1)
2366 return length;
2367 /* src and dest walk down the list; dest counts unique elements */
2368 for (src = 1; src < length; src++) {
2369 /* find next unique element */
2370 while (list[src] == list[src-1]) {
2371 src++;
2372 if (src == length)
2373 goto after;
2374 }
2375 /* dest always points to where the next unique element goes */
2376 list[dest] = list[src];
2377 dest++;
2378 }
2379after:
2380 /*
2381 * if the length difference is large enough, we want to allocate a
2382 * smaller buffer to save memory. if this fails due to out of memory,
2383 * we'll just stay with what we've got.
2384 */
2385 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
2386 newlist = krealloc(list, dest * sizeof(pid_t), GFP_KERNEL);
2387 if (newlist)
2388 *p = newlist;
2389 }
2390 return dest;
2391}
2392
2393static int cmppid(const void *a, const void *b)
2394{
2395 return *(pid_t *)a - *(pid_t *)b;
2396}
2397
2398/*
2399 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
2400 */
2401static int pidlist_array_load(struct cgroup *cgrp, bool procs)
2402{
2403 pid_t *array;
2404 int length;
2405 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07002406 struct cgroup_iter it;
2407 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07002408 struct cgroup_pidlist *l;
2409
2410 /*
2411 * If cgroup gets more users after we read count, we won't have
2412 * enough space - tough. This race is indistinguishable to the
2413 * caller from the case that the additional cgroup users didn't
2414 * show up until sometime later on.
2415 */
2416 length = cgroup_task_count(cgrp);
2417 array = kmalloc(length * sizeof(pid_t), GFP_KERNEL);
2418 if (!array)
2419 return -ENOMEM;
2420 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07002421 cgroup_iter_start(cgrp, &it);
2422 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07002423 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07002424 break;
Ben Blum102a7752009-09-23 15:56:26 -07002425 /* get tgid or pid for procs or tasks file respectively */
2426 pid = (procs ? task_tgid_vnr(tsk) : task_pid_vnr(tsk));
2427 if (pid > 0) /* make sure to only use valid results */
2428 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07002429 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002430 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07002431 length = n;
2432 /* now sort & (if procs) strip out duplicates */
2433 sort(array, length, sizeof(pid_t), cmppid, NULL);
2434 if (procs) {
2435 length = pidlist_uniq(&array, length);
2436 l = &(cgrp->procs);
2437 } else {
2438 l = &(cgrp->tasks);
2439 }
2440 /* store array in cgroup, freeing old if necessary */
2441 down_write(&l->mutex);
2442 kfree(l->list);
2443 l->list = array;
2444 l->length = length;
2445 l->use_count++;
2446 up_write(&l->mutex);
2447 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002448}
2449
Balbir Singh846c7bb2007-10-18 23:39:44 -07002450/**
Li Zefana043e3b2008-02-23 15:24:09 -08002451 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002452 * @stats: cgroupstats to fill information into
2453 * @dentry: A dentry entry belonging to the cgroup for which stats have
2454 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002455 *
2456 * Build and fill cgroupstats so that taskstats can export it to user
2457 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002458 */
2459int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2460{
2461 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002462 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002463 struct cgroup_iter it;
2464 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002465
Balbir Singh846c7bb2007-10-18 23:39:44 -07002466 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002467 * Validate dentry by checking the superblock operations,
2468 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002469 */
Li Zefan33d283b2008-11-19 15:36:48 -08002470 if (dentry->d_sb->s_op != &cgroup_ops ||
2471 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002472 goto err;
2473
2474 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002475 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002476
Paul Menagebd89aab2007-10-18 23:40:44 -07002477 cgroup_iter_start(cgrp, &it);
2478 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002479 switch (tsk->state) {
2480 case TASK_RUNNING:
2481 stats->nr_running++;
2482 break;
2483 case TASK_INTERRUPTIBLE:
2484 stats->nr_sleeping++;
2485 break;
2486 case TASK_UNINTERRUPTIBLE:
2487 stats->nr_uninterruptible++;
2488 break;
2489 case TASK_STOPPED:
2490 stats->nr_stopped++;
2491 break;
2492 default:
2493 if (delayacct_is_task_waiting_on_io(tsk))
2494 stats->nr_io_wait++;
2495 break;
2496 }
2497 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002498 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002499
Balbir Singh846c7bb2007-10-18 23:39:44 -07002500err:
2501 return ret;
2502}
2503
Paul Menage8f3ff202009-09-23 15:56:25 -07002504
Paul Menagecc31edc2008-10-18 20:28:04 -07002505/*
Ben Blum102a7752009-09-23 15:56:26 -07002506 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07002507 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07002508 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07002509 */
2510
Ben Blum102a7752009-09-23 15:56:26 -07002511static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07002512{
2513 /*
2514 * Initially we receive a position value that corresponds to
2515 * one more than the last pid shown (or 0 on the first call or
2516 * after a seek to the start). Use a binary-search to find the
2517 * next pid to display, if any
2518 */
Ben Blum102a7752009-09-23 15:56:26 -07002519 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07002520 int index = 0, pid = *pos;
2521 int *iter;
2522
Ben Blum102a7752009-09-23 15:56:26 -07002523 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002524 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07002525 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002526
Paul Menagecc31edc2008-10-18 20:28:04 -07002527 while (index < end) {
2528 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07002529 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07002530 index = mid;
2531 break;
Ben Blum102a7752009-09-23 15:56:26 -07002532 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07002533 index = mid + 1;
2534 else
2535 end = mid;
2536 }
2537 }
2538 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07002539 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07002540 return NULL;
2541 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07002542 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07002543 *pos = *iter;
2544 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002545}
2546
Ben Blum102a7752009-09-23 15:56:26 -07002547static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07002548{
Ben Blum102a7752009-09-23 15:56:26 -07002549 struct cgroup_pidlist *l = s->private;
2550 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002551}
2552
Ben Blum102a7752009-09-23 15:56:26 -07002553static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07002554{
Ben Blum102a7752009-09-23 15:56:26 -07002555 struct cgroup_pidlist *l = s->private;
2556 pid_t *p = v;
2557 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07002558 /*
2559 * Advance to the next pid in the array. If this goes off the
2560 * end, we're done
2561 */
2562 p++;
2563 if (p >= end) {
2564 return NULL;
2565 } else {
2566 *pos = *p;
2567 return p;
2568 }
2569}
2570
Ben Blum102a7752009-09-23 15:56:26 -07002571static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07002572{
2573 return seq_printf(s, "%d\n", *(int *)v);
2574}
2575
Ben Blum102a7752009-09-23 15:56:26 -07002576/*
2577 * seq_operations functions for iterating on pidlists through seq_file -
2578 * independent of whether it's tasks or procs
2579 */
2580static const struct seq_operations cgroup_pidlist_seq_operations = {
2581 .start = cgroup_pidlist_start,
2582 .stop = cgroup_pidlist_stop,
2583 .next = cgroup_pidlist_next,
2584 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07002585};
2586
Ben Blum102a7752009-09-23 15:56:26 -07002587static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07002588{
Ben Blum102a7752009-09-23 15:56:26 -07002589 down_write(&l->mutex);
2590 BUG_ON(!l->use_count);
2591 if (!--l->use_count) {
2592 kfree(l->list);
2593 l->list = NULL;
2594 l->length = 0;
Paul Menagecc31edc2008-10-18 20:28:04 -07002595 }
Ben Blum102a7752009-09-23 15:56:26 -07002596 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002597}
2598
Ben Blum102a7752009-09-23 15:56:26 -07002599static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002600{
Ben Blum102a7752009-09-23 15:56:26 -07002601 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002602 if (!(file->f_mode & FMODE_READ))
2603 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07002604 /*
2605 * the seq_file will only be initialized if the file was opened for
2606 * reading; hence we check if it's not null only in that case.
2607 */
2608 l = ((struct seq_file *)file->private_data)->private;
2609 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07002610 return seq_release(inode, file);
2611}
2612
Ben Blum102a7752009-09-23 15:56:26 -07002613static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07002614 .read = seq_read,
2615 .llseek = seq_lseek,
2616 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07002617 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07002618};
2619
2620/*
Ben Blum102a7752009-09-23 15:56:26 -07002621 * The following functions handle opens on a file that displays a pidlist
2622 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
2623 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07002624 */
Ben Blum102a7752009-09-23 15:56:26 -07002625/* helper function for the two below it */
2626static int cgroup_pidlist_open(struct file *file, bool procs)
Paul Menagecc31edc2008-10-18 20:28:04 -07002627{
2628 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum102a7752009-09-23 15:56:26 -07002629 struct cgroup_pidlist *l = (procs ? &cgrp->procs : &cgrp->tasks);
Paul Menagecc31edc2008-10-18 20:28:04 -07002630 int retval;
2631
2632 /* Nothing to do for write-only files */
2633 if (!(file->f_mode & FMODE_READ))
2634 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002635
Ben Blum102a7752009-09-23 15:56:26 -07002636 /* have the array populated */
2637 retval = pidlist_array_load(cgrp, procs);
2638 if (retval)
2639 return retval;
2640 /* configure file information */
2641 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002642
Ben Blum102a7752009-09-23 15:56:26 -07002643 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07002644 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07002645 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07002646 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002647 }
Ben Blum102a7752009-09-23 15:56:26 -07002648 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002649 return 0;
2650}
Ben Blum102a7752009-09-23 15:56:26 -07002651static int cgroup_tasks_open(struct inode *unused, struct file *file)
2652{
2653 return cgroup_pidlist_open(file, false);
2654}
2655static int cgroup_procs_open(struct inode *unused, struct file *file)
2656{
2657 return cgroup_pidlist_open(file, true);
2658}
Paul Menagebbcb81d2007-10-18 23:39:32 -07002659
Paul Menagebd89aab2007-10-18 23:40:44 -07002660static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002661 struct cftype *cft)
2662{
Paul Menagebd89aab2007-10-18 23:40:44 -07002663 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002664}
2665
Paul Menage6379c102008-07-25 01:47:01 -07002666static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2667 struct cftype *cft,
2668 u64 val)
2669{
2670 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2671 if (val)
2672 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2673 else
2674 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2675 return 0;
2676}
2677
Paul Menagebbcb81d2007-10-18 23:39:32 -07002678/*
2679 * for the common functions, 'private' gives the type of file
2680 */
Ben Blum102a7752009-09-23 15:56:26 -07002681/* for hysterical raisins, we can't put this on the older files */
2682#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07002683static struct cftype files[] = {
2684 {
2685 .name = "tasks",
2686 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07002687 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07002688 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07002689 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002690 },
Ben Blum102a7752009-09-23 15:56:26 -07002691 {
2692 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
2693 .open = cgroup_procs_open,
2694 /* .write_u64 = cgroup_procs_write, TODO */
2695 .release = cgroup_pidlist_release,
2696 .mode = S_IRUGO,
2697 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002698 {
2699 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002700 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07002701 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002702 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002703};
2704
2705static struct cftype cft_release_agent = {
2706 .name = "release_agent",
Paul Menagee788e0662008-07-25 01:46:59 -07002707 .read_seq_string = cgroup_release_agent_show,
2708 .write_string = cgroup_release_agent_write,
2709 .max_write_len = PATH_MAX,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002710};
2711
Paul Menagebd89aab2007-10-18 23:40:44 -07002712static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002713{
2714 int err;
2715 struct cgroup_subsys *ss;
2716
2717 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002718 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002719
Paul Menagebd89aab2007-10-18 23:40:44 -07002720 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002721 if (err < 0)
2722 return err;
2723
Paul Menagebd89aab2007-10-18 23:40:44 -07002724 if (cgrp == cgrp->top_cgroup) {
2725 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002726 return err;
2727 }
2728
Paul Menagebd89aab2007-10-18 23:40:44 -07002729 for_each_subsys(cgrp->root, ss) {
2730 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002731 return err;
2732 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002733 /* This cgroup is ready now */
2734 for_each_subsys(cgrp->root, ss) {
2735 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2736 /*
2737 * Update id->css pointer and make this css visible from
2738 * CSS ID functions. This pointer will be dereferened
2739 * from RCU-read-side without locks.
2740 */
2741 if (css->id)
2742 rcu_assign_pointer(css->id->css, css);
2743 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002744
2745 return 0;
2746}
2747
2748static void init_cgroup_css(struct cgroup_subsys_state *css,
2749 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002750 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002751{
Paul Menagebd89aab2007-10-18 23:40:44 -07002752 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08002753 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002754 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002755 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002756 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002757 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002758 BUG_ON(cgrp->subsys[ss->subsys_id]);
2759 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002760}
2761
Paul Menage999cd8a2009-01-07 18:08:36 -08002762static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2763{
2764 /* We need to take each hierarchy_mutex in a consistent order */
2765 int i;
2766
2767 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2768 struct cgroup_subsys *ss = subsys[i];
2769 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08002770 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08002771 }
2772}
2773
2774static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2775{
2776 int i;
2777
2778 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2779 struct cgroup_subsys *ss = subsys[i];
2780 if (ss->root == root)
2781 mutex_unlock(&ss->hierarchy_mutex);
2782 }
2783}
2784
Paul Menageddbcc7e2007-10-18 23:39:30 -07002785/*
Li Zefana043e3b2008-02-23 15:24:09 -08002786 * cgroup_create - create a cgroup
2787 * @parent: cgroup that will be parent of the new cgroup
2788 * @dentry: dentry of the new cgroup
2789 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002790 *
Li Zefana043e3b2008-02-23 15:24:09 -08002791 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002792 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002793static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002794 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002795{
Paul Menagebd89aab2007-10-18 23:40:44 -07002796 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002797 struct cgroupfs_root *root = parent->root;
2798 int err = 0;
2799 struct cgroup_subsys *ss;
2800 struct super_block *sb = root->sb;
2801
Paul Menagebd89aab2007-10-18 23:40:44 -07002802 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2803 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002804 return -ENOMEM;
2805
2806 /* Grab a reference on the superblock so the hierarchy doesn't
2807 * get deleted on unmount if there are child cgroups. This
2808 * can be done outside cgroup_mutex, since the sb can't
2809 * disappear while someone has an open control file on the
2810 * fs */
2811 atomic_inc(&sb->s_active);
2812
2813 mutex_lock(&cgroup_mutex);
2814
Paul Menagecc31edc2008-10-18 20:28:04 -07002815 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002816
Paul Menagebd89aab2007-10-18 23:40:44 -07002817 cgrp->parent = parent;
2818 cgrp->root = parent->root;
2819 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002820
Li Zefanb6abdb02008-03-04 14:28:19 -08002821 if (notify_on_release(parent))
2822 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2823
Paul Menageddbcc7e2007-10-18 23:39:30 -07002824 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002825 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002826 if (IS_ERR(css)) {
2827 err = PTR_ERR(css);
2828 goto err_destroy;
2829 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002830 init_cgroup_css(css, ss, cgrp);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002831 if (ss->use_id)
2832 if (alloc_css_id(ss, parent, cgrp))
2833 goto err_destroy;
2834 /* At error, ->destroy() callback has to free assigned ID. */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002835 }
2836
Paul Menage999cd8a2009-01-07 18:08:36 -08002837 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002838 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08002839 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002840 root->number_of_cgroups++;
2841
Paul Menagebd89aab2007-10-18 23:40:44 -07002842 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002843 if (err < 0)
2844 goto err_remove;
2845
2846 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07002847 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002848
Paul Menagebd89aab2007-10-18 23:40:44 -07002849 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002850 /* If err < 0, we have a half-filled directory - oh well ;) */
2851
2852 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002853 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002854
2855 return 0;
2856
2857 err_remove:
2858
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002859 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002860 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002861 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002862 root->number_of_cgroups--;
2863
2864 err_destroy:
2865
2866 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002867 if (cgrp->subsys[ss->subsys_id])
2868 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002869 }
2870
2871 mutex_unlock(&cgroup_mutex);
2872
2873 /* Release the reference count that we took on the superblock */
2874 deactivate_super(sb);
2875
Paul Menagebd89aab2007-10-18 23:40:44 -07002876 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002877 return err;
2878}
2879
2880static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2881{
2882 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2883
2884 /* the vfs holds inode->i_mutex already */
2885 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2886}
2887
Li Zefan55b6fd02008-07-29 22:33:20 -07002888static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002889{
2890 /* Check the reference count on each subsystem. Since we
2891 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08002892 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07002893 * be no outstanding references, so the subsystem is safe to
2894 * destroy. We scan across all subsystems rather than using
2895 * the per-hierarchy linked list of mounted subsystems since
2896 * we can be called via check_for_release() with no
2897 * synchronization other than RCU, and the subsystem linked
2898 * list isn't RCU-safe */
2899 int i;
2900 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2901 struct cgroup_subsys *ss = subsys[i];
2902 struct cgroup_subsys_state *css;
2903 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07002904 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002905 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07002906 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07002907 /* When called from check_for_release() it's possible
2908 * that by this point the cgroup has been removed
2909 * and the css deleted. But a false-positive doesn't
2910 * matter, since it can only happen if the cgroup
2911 * has been deleted and hence no longer needs the
2912 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08002913 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07002914 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07002915 }
2916 return 0;
2917}
2918
Paul Menagee7c5ec92009-01-07 18:08:38 -08002919/*
2920 * Atomically mark all (or else none) of the cgroup's CSS objects as
2921 * CSS_REMOVED. Return true on success, or false if the cgroup has
2922 * busy subsystems. Call with cgroup_mutex held
2923 */
2924
2925static int cgroup_clear_css_refs(struct cgroup *cgrp)
2926{
2927 struct cgroup_subsys *ss;
2928 unsigned long flags;
2929 bool failed = false;
2930 local_irq_save(flags);
2931 for_each_subsys(cgrp->root, ss) {
2932 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2933 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08002934 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08002935 /* We can only remove a CSS with a refcnt==1 */
2936 refcnt = atomic_read(&css->refcnt);
2937 if (refcnt > 1) {
2938 failed = true;
2939 goto done;
2940 }
2941 BUG_ON(!refcnt);
2942 /*
2943 * Drop the refcnt to 0 while we check other
2944 * subsystems. This will cause any racing
2945 * css_tryget() to spin until we set the
2946 * CSS_REMOVED bits or abort
2947 */
Paul Menage804b3c22009-01-29 14:25:21 -08002948 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
2949 break;
2950 cpu_relax();
2951 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08002952 }
2953 done:
2954 for_each_subsys(cgrp->root, ss) {
2955 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2956 if (failed) {
2957 /*
2958 * Restore old refcnt if we previously managed
2959 * to clear it from 1 to 0
2960 */
2961 if (!atomic_read(&css->refcnt))
2962 atomic_set(&css->refcnt, 1);
2963 } else {
2964 /* Commit the fact that the CSS is removed */
2965 set_bit(CSS_REMOVED, &css->flags);
2966 }
2967 }
2968 local_irq_restore(flags);
2969 return !failed;
2970}
2971
Paul Menageddbcc7e2007-10-18 23:39:30 -07002972static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2973{
Paul Menagebd89aab2007-10-18 23:40:44 -07002974 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002975 struct dentry *d;
2976 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002977 DEFINE_WAIT(wait);
2978 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002979
2980 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002981again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07002982 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002983 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002984 mutex_unlock(&cgroup_mutex);
2985 return -EBUSY;
2986 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002987 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002988 mutex_unlock(&cgroup_mutex);
2989 return -EBUSY;
2990 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002991 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08002992
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002993 /*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002994 * In general, subsystem has no css->refcnt after pre_destroy(). But
2995 * in racy cases, subsystem may have to get css->refcnt after
2996 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
2997 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
2998 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
2999 * and subsystem's reference count handling. Please see css_get/put
3000 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3001 */
3002 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3003
3004 /*
Li Zefana043e3b2008-02-23 15:24:09 -08003005 * Call pre_destroy handlers of subsys. Notify subsystems
3006 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003007 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003008 ret = cgroup_call_pre_destroy(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003009 if (ret) {
3010 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003011 return ret;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003012 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003013
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003014 mutex_lock(&cgroup_mutex);
3015 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003016 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003017 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003018 mutex_unlock(&cgroup_mutex);
3019 return -EBUSY;
3020 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003021 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003022 if (!cgroup_clear_css_refs(cgrp)) {
3023 mutex_unlock(&cgroup_mutex);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003024 /*
3025 * Because someone may call cgroup_wakeup_rmdir_waiter() before
3026 * prepare_to_wait(), we need to check this flag.
3027 */
3028 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
3029 schedule();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003030 finish_wait(&cgroup_rmdir_waitq, &wait);
3031 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3032 if (signal_pending(current))
3033 return -EINTR;
3034 goto again;
3035 }
3036 /* NO css_tryget() can success after here. */
3037 finish_wait(&cgroup_rmdir_waitq, &wait);
3038 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003039
Paul Menage81a6a5c2007-10-18 23:39:38 -07003040 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003041 set_bit(CGRP_REMOVED, &cgrp->flags);
3042 if (!list_empty(&cgrp->release_list))
3043 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003044 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08003045
3046 cgroup_lock_hierarchy(cgrp->root);
3047 /* delete this cgroup from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07003048 list_del(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08003049 cgroup_unlock_hierarchy(cgrp->root);
3050
Paul Menagebd89aab2007-10-18 23:40:44 -07003051 spin_lock(&cgrp->dentry->d_lock);
3052 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003053 spin_unlock(&d->d_lock);
3054
3055 cgroup_d_remove_dir(d);
3056 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003057
Paul Menagebd89aab2007-10-18 23:40:44 -07003058 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003059 check_for_release(parent);
3060
Paul Menageddbcc7e2007-10-18 23:39:30 -07003061 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003062 return 0;
3063}
3064
Li Zefan06a11922008-04-29 01:00:07 -07003065static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003066{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003067 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08003068
3069 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003070
3071 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08003072 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003073 ss->root = &rootnode;
3074 css = ss->create(ss, dummytop);
3075 /* We don't handle early failures gracefully */
3076 BUG_ON(IS_ERR(css));
3077 init_cgroup_css(css, ss, dummytop);
3078
Li Zefane8d55fd2008-04-29 01:00:13 -07003079 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07003080 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07003081 * newly registered, all tasks and hence the
3082 * init_css_set is in the subsystem's top cgroup. */
3083 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07003084
3085 need_forkexit_callback |= ss->fork || ss->exit;
3086
Li Zefane8d55fd2008-04-29 01:00:13 -07003087 /* At system boot, before all subsystems have been
3088 * registered, no tasks have been forked, so we don't
3089 * need to invoke fork callbacks here. */
3090 BUG_ON(!list_empty(&init_task.tasks));
3091
Paul Menage999cd8a2009-01-07 18:08:36 -08003092 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08003093 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003094 ss->active = 1;
3095}
3096
3097/**
Li Zefana043e3b2008-02-23 15:24:09 -08003098 * cgroup_init_early - cgroup initialization at system boot
3099 *
3100 * Initialize cgroups at system boot, and initialize any
3101 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07003102 */
3103int __init cgroup_init_early(void)
3104{
3105 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07003106 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07003107 INIT_LIST_HEAD(&init_css_set.cg_links);
3108 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07003109 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07003110 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003111 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07003112 root_count = 1;
3113 init_task.cgroups = &init_css_set;
3114
3115 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07003116 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07003117 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07003118 &rootnode.top_cgroup.css_sets);
3119 list_add(&init_css_set_link.cg_link_list,
3120 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003121
Li Zefan472b1052008-04-29 01:00:11 -07003122 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
3123 INIT_HLIST_HEAD(&css_set_table[i]);
3124
Paul Menageddbcc7e2007-10-18 23:39:30 -07003125 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3126 struct cgroup_subsys *ss = subsys[i];
3127
3128 BUG_ON(!ss->name);
3129 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
3130 BUG_ON(!ss->create);
3131 BUG_ON(!ss->destroy);
3132 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08003133 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07003134 ss->name, ss->subsys_id);
3135 BUG();
3136 }
3137
3138 if (ss->early_init)
3139 cgroup_init_subsys(ss);
3140 }
3141 return 0;
3142}
3143
3144/**
Li Zefana043e3b2008-02-23 15:24:09 -08003145 * cgroup_init - cgroup initialization
3146 *
3147 * Register cgroup filesystem and /proc file, and initialize
3148 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07003149 */
3150int __init cgroup_init(void)
3151{
3152 int err;
3153 int i;
Li Zefan472b1052008-04-29 01:00:11 -07003154 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07003155
3156 err = bdi_init(&cgroup_backing_dev_info);
3157 if (err)
3158 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003159
3160 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3161 struct cgroup_subsys *ss = subsys[i];
3162 if (!ss->early_init)
3163 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003164 if (ss->use_id)
3165 cgroup_subsys_init_idr(ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003166 }
3167
Li Zefan472b1052008-04-29 01:00:11 -07003168 /* Add init_css_set to the hash table */
3169 hhead = css_set_hash(init_css_set.subsys);
3170 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003171 BUG_ON(!init_root_id(&rootnode));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003172 err = register_filesystem(&cgroup_fs_type);
3173 if (err < 0)
3174 goto out;
3175
Li Zefan46ae2202008-04-29 01:00:08 -07003176 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07003177
Paul Menageddbcc7e2007-10-18 23:39:30 -07003178out:
Paul Menagea4243162007-10-18 23:39:35 -07003179 if (err)
3180 bdi_destroy(&cgroup_backing_dev_info);
3181
Paul Menageddbcc7e2007-10-18 23:39:30 -07003182 return err;
3183}
Paul Menageb4f48b62007-10-18 23:39:33 -07003184
Paul Menagea4243162007-10-18 23:39:35 -07003185/*
3186 * proc_cgroup_show()
3187 * - Print task's cgroup paths into seq_file, one line for each hierarchy
3188 * - Used for /proc/<pid>/cgroup.
3189 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
3190 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003191 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07003192 * anyway. No need to check that tsk->cgroup != NULL, thanks to
3193 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
3194 * cgroup to top_cgroup.
3195 */
3196
3197/* TODO: Use a proper seq_file iterator */
3198static int proc_cgroup_show(struct seq_file *m, void *v)
3199{
3200 struct pid *pid;
3201 struct task_struct *tsk;
3202 char *buf;
3203 int retval;
3204 struct cgroupfs_root *root;
3205
3206 retval = -ENOMEM;
3207 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3208 if (!buf)
3209 goto out;
3210
3211 retval = -ESRCH;
3212 pid = m->private;
3213 tsk = get_pid_task(pid, PIDTYPE_PID);
3214 if (!tsk)
3215 goto out_free;
3216
3217 retval = 0;
3218
3219 mutex_lock(&cgroup_mutex);
3220
Li Zefane5f6a862009-01-07 18:07:41 -08003221 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07003222 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07003223 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07003224 int count = 0;
3225
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003226 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07003227 for_each_subsys(root, ss)
3228 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07003229 if (strlen(root->name))
3230 seq_printf(m, "%sname=%s", count ? "," : "",
3231 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07003232 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07003233 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003234 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07003235 if (retval < 0)
3236 goto out_unlock;
3237 seq_puts(m, buf);
3238 seq_putc(m, '\n');
3239 }
3240
3241out_unlock:
3242 mutex_unlock(&cgroup_mutex);
3243 put_task_struct(tsk);
3244out_free:
3245 kfree(buf);
3246out:
3247 return retval;
3248}
3249
3250static int cgroup_open(struct inode *inode, struct file *file)
3251{
3252 struct pid *pid = PROC_I(inode)->pid;
3253 return single_open(file, proc_cgroup_show, pid);
3254}
3255
3256struct file_operations proc_cgroup_operations = {
3257 .open = cgroup_open,
3258 .read = seq_read,
3259 .llseek = seq_lseek,
3260 .release = single_release,
3261};
3262
3263/* Display information about each subsystem and each hierarchy */
3264static int proc_cgroupstats_show(struct seq_file *m, void *v)
3265{
3266 int i;
Paul Menagea4243162007-10-18 23:39:35 -07003267
Paul Menage8bab8dd2008-04-04 14:29:57 -07003268 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07003269 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07003270 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3271 struct cgroup_subsys *ss = subsys[i];
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003272 seq_printf(m, "%s\t%d\t%d\t%d\n",
3273 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07003274 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07003275 }
3276 mutex_unlock(&cgroup_mutex);
3277 return 0;
3278}
3279
3280static int cgroupstats_open(struct inode *inode, struct file *file)
3281{
Al Viro9dce07f2008-03-29 03:07:28 +00003282 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07003283}
3284
3285static struct file_operations proc_cgroupstats_operations = {
3286 .open = cgroupstats_open,
3287 .read = seq_read,
3288 .llseek = seq_lseek,
3289 .release = single_release,
3290};
3291
Paul Menageb4f48b62007-10-18 23:39:33 -07003292/**
3293 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08003294 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07003295 *
3296 * Description: A task inherits its parent's cgroup at fork().
3297 *
3298 * A pointer to the shared css_set was automatically copied in
3299 * fork.c by dup_task_struct(). However, we ignore that copy, since
3300 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08003301 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07003302 * have already changed current->cgroups, allowing the previously
3303 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07003304 *
3305 * At the point that cgroup_fork() is called, 'current' is the parent
3306 * task, and the passed argument 'child' points to the child task.
3307 */
3308void cgroup_fork(struct task_struct *child)
3309{
Paul Menage817929e2007-10-18 23:39:36 -07003310 task_lock(current);
3311 child->cgroups = current->cgroups;
3312 get_css_set(child->cgroups);
3313 task_unlock(current);
3314 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07003315}
3316
3317/**
Li Zefana043e3b2008-02-23 15:24:09 -08003318 * cgroup_fork_callbacks - run fork callbacks
3319 * @child: the new task
3320 *
3321 * Called on a new task very soon before adding it to the
3322 * tasklist. No need to take any locks since no-one can
3323 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003324 */
3325void cgroup_fork_callbacks(struct task_struct *child)
3326{
3327 if (need_forkexit_callback) {
3328 int i;
3329 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3330 struct cgroup_subsys *ss = subsys[i];
3331 if (ss->fork)
3332 ss->fork(ss, child);
3333 }
3334 }
3335}
3336
3337/**
Li Zefana043e3b2008-02-23 15:24:09 -08003338 * cgroup_post_fork - called on a new task after adding it to the task list
3339 * @child: the task in question
3340 *
3341 * Adds the task to the list running through its css_set if necessary.
3342 * Has to be after the task is visible on the task list in case we race
3343 * with the first call to cgroup_iter_start() - to guarantee that the
3344 * new task ends up on its list.
3345 */
Paul Menage817929e2007-10-18 23:39:36 -07003346void cgroup_post_fork(struct task_struct *child)
3347{
3348 if (use_task_css_set_links) {
3349 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003350 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003351 if (list_empty(&child->cg_list))
3352 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003353 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003354 write_unlock(&css_set_lock);
3355 }
3356}
3357/**
Paul Menageb4f48b62007-10-18 23:39:33 -07003358 * cgroup_exit - detach cgroup from exiting task
3359 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08003360 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07003361 *
3362 * Description: Detach cgroup from @tsk and release it.
3363 *
3364 * Note that cgroups marked notify_on_release force every task in
3365 * them to take the global cgroup_mutex mutex when exiting.
3366 * This could impact scaling on very large systems. Be reluctant to
3367 * use notify_on_release cgroups where very high task exit scaling
3368 * is required on large systems.
3369 *
3370 * the_top_cgroup_hack:
3371 *
3372 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3373 *
3374 * We call cgroup_exit() while the task is still competent to
3375 * handle notify_on_release(), then leave the task attached to the
3376 * root cgroup in each hierarchy for the remainder of its exit.
3377 *
3378 * To do this properly, we would increment the reference count on
3379 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3380 * code we would add a second cgroup function call, to drop that
3381 * reference. This would just create an unnecessary hot spot on
3382 * the top_cgroup reference count, to no avail.
3383 *
3384 * Normally, holding a reference to a cgroup without bumping its
3385 * count is unsafe. The cgroup could go away, or someone could
3386 * attach us to a different cgroup, decrementing the count on
3387 * the first cgroup that we never incremented. But in this case,
3388 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003389 * which wards off any cgroup_attach_task() attempts, or task is a failed
3390 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003391 */
3392void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3393{
3394 int i;
Paul Menage817929e2007-10-18 23:39:36 -07003395 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07003396
3397 if (run_callbacks && need_forkexit_callback) {
3398 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3399 struct cgroup_subsys *ss = subsys[i];
3400 if (ss->exit)
3401 ss->exit(ss, tsk);
3402 }
3403 }
Paul Menage817929e2007-10-18 23:39:36 -07003404
3405 /*
3406 * Unlink from the css_set task list if necessary.
3407 * Optimistically check cg_list before taking
3408 * css_set_lock
3409 */
3410 if (!list_empty(&tsk->cg_list)) {
3411 write_lock(&css_set_lock);
3412 if (!list_empty(&tsk->cg_list))
3413 list_del(&tsk->cg_list);
3414 write_unlock(&css_set_lock);
3415 }
3416
Paul Menageb4f48b62007-10-18 23:39:33 -07003417 /* Reassign the task to the init_css_set. */
3418 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003419 cg = tsk->cgroups;
3420 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07003421 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003422 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003423 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07003424}
Paul Menage697f4162007-10-18 23:39:34 -07003425
3426/**
Li Zefana043e3b2008-02-23 15:24:09 -08003427 * cgroup_clone - clone the cgroup the given subsystem is attached to
3428 * @tsk: the task to be moved
3429 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003430 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08003431 *
3432 * Duplicate the current cgroup in the hierarchy that the given
3433 * subsystem is attached to, and move this task into the new
3434 * child.
Paul Menage697f4162007-10-18 23:39:34 -07003435 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003436int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3437 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07003438{
3439 struct dentry *dentry;
3440 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07003441 struct cgroup *parent, *child;
3442 struct inode *inode;
3443 struct css_set *cg;
3444 struct cgroupfs_root *root;
3445 struct cgroup_subsys *ss;
3446
3447 /* We shouldn't be called by an unregistered subsystem */
3448 BUG_ON(!subsys->active);
3449
3450 /* First figure out what hierarchy and cgroup we're dealing
3451 * with, and pin them so we can drop cgroup_mutex */
3452 mutex_lock(&cgroup_mutex);
3453 again:
3454 root = subsys->root;
3455 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07003456 mutex_unlock(&cgroup_mutex);
3457 return 0;
3458 }
Paul Menage697f4162007-10-18 23:39:34 -07003459
Paul Menage697f4162007-10-18 23:39:34 -07003460 /* Pin the hierarchy */
Li Zefan1404f062009-01-29 14:25:21 -08003461 if (!atomic_inc_not_zero(&root->sb->s_active)) {
Li Zefan7b574b72009-01-04 12:00:45 -08003462 /* We race with the final deactivate_super() */
3463 mutex_unlock(&cgroup_mutex);
3464 return 0;
3465 }
Paul Menage697f4162007-10-18 23:39:34 -07003466
Paul Menage817929e2007-10-18 23:39:36 -07003467 /* Keep the cgroup alive */
Li Zefan1404f062009-01-29 14:25:21 -08003468 task_lock(tsk);
3469 parent = task_cgroup(tsk, subsys->subsys_id);
3470 cg = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07003471 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08003472 task_unlock(tsk);
Li Zefan1404f062009-01-29 14:25:21 -08003473
Paul Menage697f4162007-10-18 23:39:34 -07003474 mutex_unlock(&cgroup_mutex);
3475
3476 /* Now do the VFS work to create a cgroup */
3477 inode = parent->dentry->d_inode;
3478
3479 /* Hold the parent directory mutex across this operation to
3480 * stop anyone else deleting the new cgroup */
3481 mutex_lock(&inode->i_mutex);
3482 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3483 if (IS_ERR(dentry)) {
3484 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08003485 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07003486 PTR_ERR(dentry));
3487 ret = PTR_ERR(dentry);
3488 goto out_release;
3489 }
3490
3491 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08003492 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07003493 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07003494 dput(dentry);
3495 if (ret) {
3496 printk(KERN_INFO
3497 "Failed to create cgroup %s: %d\n", nodename,
3498 ret);
3499 goto out_release;
3500 }
3501
Paul Menage697f4162007-10-18 23:39:34 -07003502 /* The cgroup now exists. Retake cgroup_mutex and check
3503 * that we're still in the same state that we thought we
3504 * were. */
3505 mutex_lock(&cgroup_mutex);
3506 if ((root != subsys->root) ||
3507 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3508 /* Aargh, we raced ... */
3509 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003510 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07003511
Li Zefan1404f062009-01-29 14:25:21 -08003512 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003513 /* The cgroup is still accessible in the VFS, but
3514 * we're not going to try to rmdir() it at this
3515 * point. */
3516 printk(KERN_INFO
3517 "Race in cgroup_clone() - leaking cgroup %s\n",
3518 nodename);
3519 goto again;
3520 }
3521
3522 /* do any required auto-setup */
3523 for_each_subsys(root, ss) {
3524 if (ss->post_clone)
3525 ss->post_clone(ss, child);
3526 }
3527
3528 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08003529 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07003530 mutex_unlock(&cgroup_mutex);
3531
3532 out_release:
3533 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003534
3535 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003536 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003537 mutex_unlock(&cgroup_mutex);
Li Zefan1404f062009-01-29 14:25:21 -08003538 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003539 return ret;
3540}
3541
Li Zefana043e3b2008-02-23 15:24:09 -08003542/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003543 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08003544 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003545 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08003546 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003547 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3548 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07003549 *
3550 * If we are sending in dummytop, then presumably we are creating
3551 * the top cgroup in the subsystem.
3552 *
3553 * Called only by the ns (nsproxy) cgroup.
3554 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003555int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07003556{
3557 int ret;
3558 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07003559
Paul Menagebd89aab2007-10-18 23:40:44 -07003560 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07003561 return 1;
3562
Paul Menage7717f7b2009-09-23 15:56:22 -07003563 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003564 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3565 cgrp = cgrp->parent;
3566 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07003567 return ret;
3568}
Paul Menage81a6a5c2007-10-18 23:39:38 -07003569
Paul Menagebd89aab2007-10-18 23:40:44 -07003570static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003571{
3572 /* All of these checks rely on RCU to keep the cgroup
3573 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07003574 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3575 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003576 /* Control Group is currently removeable. If it's not
3577 * already queued for a userspace notification, queue
3578 * it now */
3579 int need_schedule_work = 0;
3580 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003581 if (!cgroup_is_removed(cgrp) &&
3582 list_empty(&cgrp->release_list)) {
3583 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003584 need_schedule_work = 1;
3585 }
3586 spin_unlock(&release_list_lock);
3587 if (need_schedule_work)
3588 schedule_work(&release_agent_work);
3589 }
3590}
3591
3592void __css_put(struct cgroup_subsys_state *css)
3593{
Paul Menagebd89aab2007-10-18 23:40:44 -07003594 struct cgroup *cgrp = css->cgroup;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003595 rcu_read_lock();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003596 if (atomic_dec_return(&css->refcnt) == 1) {
3597 if (notify_on_release(cgrp)) {
3598 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3599 check_for_release(cgrp);
3600 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003601 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003602 }
3603 rcu_read_unlock();
3604}
3605
3606/*
3607 * Notify userspace when a cgroup is released, by running the
3608 * configured release agent with the name of the cgroup (path
3609 * relative to the root of cgroup file system) as the argument.
3610 *
3611 * Most likely, this user command will try to rmdir this cgroup.
3612 *
3613 * This races with the possibility that some other task will be
3614 * attached to this cgroup before it is removed, or that some other
3615 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3616 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3617 * unused, and this cgroup will be reprieved from its death sentence,
3618 * to continue to serve a useful existence. Next time it's released,
3619 * we will get notified again, if it still has 'notify_on_release' set.
3620 *
3621 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3622 * means only wait until the task is successfully execve()'d. The
3623 * separate release agent task is forked by call_usermodehelper(),
3624 * then control in this thread returns here, without waiting for the
3625 * release agent task. We don't bother to wait because the caller of
3626 * this routine has no use for the exit status of the release agent
3627 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003628 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003629static void cgroup_release_agent(struct work_struct *work)
3630{
3631 BUG_ON(work != &release_agent_work);
3632 mutex_lock(&cgroup_mutex);
3633 spin_lock(&release_list_lock);
3634 while (!list_empty(&release_list)) {
3635 char *argv[3], *envp[3];
3636 int i;
Paul Menagee788e0662008-07-25 01:46:59 -07003637 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003638 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003639 struct cgroup,
3640 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003641 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003642 spin_unlock(&release_list_lock);
3643 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e0662008-07-25 01:46:59 -07003644 if (!pathbuf)
3645 goto continue_free;
3646 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3647 goto continue_free;
3648 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3649 if (!agentbuf)
3650 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003651
3652 i = 0;
Paul Menagee788e0662008-07-25 01:46:59 -07003653 argv[i++] = agentbuf;
3654 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003655 argv[i] = NULL;
3656
3657 i = 0;
3658 /* minimal command environment */
3659 envp[i++] = "HOME=/";
3660 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3661 envp[i] = NULL;
3662
3663 /* Drop the lock while we invoke the usermode helper,
3664 * since the exec could involve hitting disk and hence
3665 * be a slow process */
3666 mutex_unlock(&cgroup_mutex);
3667 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003668 mutex_lock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07003669 continue_free:
3670 kfree(pathbuf);
3671 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003672 spin_lock(&release_list_lock);
3673 }
3674 spin_unlock(&release_list_lock);
3675 mutex_unlock(&cgroup_mutex);
3676}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003677
3678static int __init cgroup_disable(char *str)
3679{
3680 int i;
3681 char *token;
3682
3683 while ((token = strsep(&str, ",")) != NULL) {
3684 if (!*token)
3685 continue;
3686
3687 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3688 struct cgroup_subsys *ss = subsys[i];
3689
3690 if (!strcmp(token, ss->name)) {
3691 ss->disabled = 1;
3692 printk(KERN_INFO "Disabling %s control group"
3693 " subsystem\n", ss->name);
3694 break;
3695 }
3696 }
3697 }
3698 return 1;
3699}
3700__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003701
3702/*
3703 * Functons for CSS ID.
3704 */
3705
3706/*
3707 *To get ID other than 0, this should be called when !cgroup_is_removed().
3708 */
3709unsigned short css_id(struct cgroup_subsys_state *css)
3710{
3711 struct css_id *cssid = rcu_dereference(css->id);
3712
3713 if (cssid)
3714 return cssid->id;
3715 return 0;
3716}
3717
3718unsigned short css_depth(struct cgroup_subsys_state *css)
3719{
3720 struct css_id *cssid = rcu_dereference(css->id);
3721
3722 if (cssid)
3723 return cssid->depth;
3724 return 0;
3725}
3726
3727bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07003728 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003729{
3730 struct css_id *child_id = rcu_dereference(child->id);
3731 struct css_id *root_id = rcu_dereference(root->id);
3732
3733 if (!child_id || !root_id || (child_id->depth < root_id->depth))
3734 return false;
3735 return child_id->stack[root_id->depth] == root_id->id;
3736}
3737
3738static void __free_css_id_cb(struct rcu_head *head)
3739{
3740 struct css_id *id;
3741
3742 id = container_of(head, struct css_id, rcu_head);
3743 kfree(id);
3744}
3745
3746void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
3747{
3748 struct css_id *id = css->id;
3749 /* When this is called before css_id initialization, id can be NULL */
3750 if (!id)
3751 return;
3752
3753 BUG_ON(!ss->use_id);
3754
3755 rcu_assign_pointer(id->css, NULL);
3756 rcu_assign_pointer(css->id, NULL);
3757 spin_lock(&ss->id_lock);
3758 idr_remove(&ss->idr, id->id);
3759 spin_unlock(&ss->id_lock);
3760 call_rcu(&id->rcu_head, __free_css_id_cb);
3761}
3762
3763/*
3764 * This is called by init or create(). Then, calls to this function are
3765 * always serialized (By cgroup_mutex() at create()).
3766 */
3767
3768static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
3769{
3770 struct css_id *newid;
3771 int myid, error, size;
3772
3773 BUG_ON(!ss->use_id);
3774
3775 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
3776 newid = kzalloc(size, GFP_KERNEL);
3777 if (!newid)
3778 return ERR_PTR(-ENOMEM);
3779 /* get id */
3780 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
3781 error = -ENOMEM;
3782 goto err_out;
3783 }
3784 spin_lock(&ss->id_lock);
3785 /* Don't use 0. allocates an ID of 1-65535 */
3786 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
3787 spin_unlock(&ss->id_lock);
3788
3789 /* Returns error when there are no free spaces for new ID.*/
3790 if (error) {
3791 error = -ENOSPC;
3792 goto err_out;
3793 }
3794 if (myid > CSS_ID_MAX)
3795 goto remove_idr;
3796
3797 newid->id = myid;
3798 newid->depth = depth;
3799 return newid;
3800remove_idr:
3801 error = -ENOSPC;
3802 spin_lock(&ss->id_lock);
3803 idr_remove(&ss->idr, myid);
3804 spin_unlock(&ss->id_lock);
3805err_out:
3806 kfree(newid);
3807 return ERR_PTR(error);
3808
3809}
3810
3811static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss)
3812{
3813 struct css_id *newid;
3814 struct cgroup_subsys_state *rootcss;
3815
3816 spin_lock_init(&ss->id_lock);
3817 idr_init(&ss->idr);
3818
3819 rootcss = init_css_set.subsys[ss->subsys_id];
3820 newid = get_new_cssid(ss, 0);
3821 if (IS_ERR(newid))
3822 return PTR_ERR(newid);
3823
3824 newid->stack[0] = newid->id;
3825 newid->css = rootcss;
3826 rootcss->id = newid;
3827 return 0;
3828}
3829
3830static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
3831 struct cgroup *child)
3832{
3833 int subsys_id, i, depth = 0;
3834 struct cgroup_subsys_state *parent_css, *child_css;
3835 struct css_id *child_id, *parent_id = NULL;
3836
3837 subsys_id = ss->subsys_id;
3838 parent_css = parent->subsys[subsys_id];
3839 child_css = child->subsys[subsys_id];
3840 depth = css_depth(parent_css) + 1;
3841 parent_id = parent_css->id;
3842
3843 child_id = get_new_cssid(ss, depth);
3844 if (IS_ERR(child_id))
3845 return PTR_ERR(child_id);
3846
3847 for (i = 0; i < depth; i++)
3848 child_id->stack[i] = parent_id->stack[i];
3849 child_id->stack[depth] = child_id->id;
3850 /*
3851 * child_id->css pointer will be set after this cgroup is available
3852 * see cgroup_populate_dir()
3853 */
3854 rcu_assign_pointer(child_css->id, child_id);
3855
3856 return 0;
3857}
3858
3859/**
3860 * css_lookup - lookup css by id
3861 * @ss: cgroup subsys to be looked into.
3862 * @id: the id
3863 *
3864 * Returns pointer to cgroup_subsys_state if there is valid one with id.
3865 * NULL if not. Should be called under rcu_read_lock()
3866 */
3867struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
3868{
3869 struct css_id *cssid = NULL;
3870
3871 BUG_ON(!ss->use_id);
3872 cssid = idr_find(&ss->idr, id);
3873
3874 if (unlikely(!cssid))
3875 return NULL;
3876
3877 return rcu_dereference(cssid->css);
3878}
3879
3880/**
3881 * css_get_next - lookup next cgroup under specified hierarchy.
3882 * @ss: pointer to subsystem
3883 * @id: current position of iteration.
3884 * @root: pointer to css. search tree under this.
3885 * @foundid: position of found object.
3886 *
3887 * Search next css under the specified hierarchy of rootid. Calling under
3888 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
3889 */
3890struct cgroup_subsys_state *
3891css_get_next(struct cgroup_subsys *ss, int id,
3892 struct cgroup_subsys_state *root, int *foundid)
3893{
3894 struct cgroup_subsys_state *ret = NULL;
3895 struct css_id *tmp;
3896 int tmpid;
3897 int rootid = css_id(root);
3898 int depth = css_depth(root);
3899
3900 if (!rootid)
3901 return NULL;
3902
3903 BUG_ON(!ss->use_id);
3904 /* fill start point for scan */
3905 tmpid = id;
3906 while (1) {
3907 /*
3908 * scan next entry from bitmap(tree), tmpid is updated after
3909 * idr_get_next().
3910 */
3911 spin_lock(&ss->id_lock);
3912 tmp = idr_get_next(&ss->idr, &tmpid);
3913 spin_unlock(&ss->id_lock);
3914
3915 if (!tmp)
3916 break;
3917 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
3918 ret = rcu_dereference(tmp->css);
3919 if (ret) {
3920 *foundid = tmpid;
3921 break;
3922 }
3923 }
3924 /* continue to scan from next id */
3925 tmpid = tmpid + 1;
3926 }
3927 return ret;
3928}
3929
Paul Menagefe693432009-09-23 15:56:20 -07003930#ifdef CONFIG_CGROUP_DEBUG
3931static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
3932 struct cgroup *cont)
3933{
3934 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
3935
3936 if (!css)
3937 return ERR_PTR(-ENOMEM);
3938
3939 return css;
3940}
3941
3942static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
3943{
3944 kfree(cont->subsys[debug_subsys_id]);
3945}
3946
3947static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
3948{
3949 return atomic_read(&cont->count);
3950}
3951
3952static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
3953{
3954 return cgroup_task_count(cont);
3955}
3956
3957static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
3958{
3959 return (u64)(unsigned long)current->cgroups;
3960}
3961
3962static u64 current_css_set_refcount_read(struct cgroup *cont,
3963 struct cftype *cft)
3964{
3965 u64 count;
3966
3967 rcu_read_lock();
3968 count = atomic_read(&current->cgroups->refcount);
3969 rcu_read_unlock();
3970 return count;
3971}
3972
Paul Menage7717f7b2009-09-23 15:56:22 -07003973static int current_css_set_cg_links_read(struct cgroup *cont,
3974 struct cftype *cft,
3975 struct seq_file *seq)
3976{
3977 struct cg_cgroup_link *link;
3978 struct css_set *cg;
3979
3980 read_lock(&css_set_lock);
3981 rcu_read_lock();
3982 cg = rcu_dereference(current->cgroups);
3983 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
3984 struct cgroup *c = link->cgrp;
3985 const char *name;
3986
3987 if (c->dentry)
3988 name = c->dentry->d_name.name;
3989 else
3990 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003991 seq_printf(seq, "Root %d group %s\n",
3992 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07003993 }
3994 rcu_read_unlock();
3995 read_unlock(&css_set_lock);
3996 return 0;
3997}
3998
3999#define MAX_TASKS_SHOWN_PER_CSS 25
4000static int cgroup_css_links_read(struct cgroup *cont,
4001 struct cftype *cft,
4002 struct seq_file *seq)
4003{
4004 struct cg_cgroup_link *link;
4005
4006 read_lock(&css_set_lock);
4007 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
4008 struct css_set *cg = link->cg;
4009 struct task_struct *task;
4010 int count = 0;
4011 seq_printf(seq, "css_set %p\n", cg);
4012 list_for_each_entry(task, &cg->tasks, cg_list) {
4013 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4014 seq_puts(seq, " ...\n");
4015 break;
4016 } else {
4017 seq_printf(seq, " task %d\n",
4018 task_pid_vnr(task));
4019 }
4020 }
4021 }
4022 read_unlock(&css_set_lock);
4023 return 0;
4024}
4025
Paul Menagefe693432009-09-23 15:56:20 -07004026static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
4027{
4028 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
4029}
4030
4031static struct cftype debug_files[] = {
4032 {
4033 .name = "cgroup_refcount",
4034 .read_u64 = cgroup_refcount_read,
4035 },
4036 {
4037 .name = "taskcount",
4038 .read_u64 = debug_taskcount_read,
4039 },
4040
4041 {
4042 .name = "current_css_set",
4043 .read_u64 = current_css_set_read,
4044 },
4045
4046 {
4047 .name = "current_css_set_refcount",
4048 .read_u64 = current_css_set_refcount_read,
4049 },
4050
4051 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004052 .name = "current_css_set_cg_links",
4053 .read_seq_string = current_css_set_cg_links_read,
4054 },
4055
4056 {
4057 .name = "cgroup_css_links",
4058 .read_seq_string = cgroup_css_links_read,
4059 },
4060
4061 {
Paul Menagefe693432009-09-23 15:56:20 -07004062 .name = "releasable",
4063 .read_u64 = releasable_read,
4064 },
4065};
4066
4067static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
4068{
4069 return cgroup_add_files(cont, ss, debug_files,
4070 ARRAY_SIZE(debug_files));
4071}
4072
4073struct cgroup_subsys debug_subsys = {
4074 .name = "debug",
4075 .create = debug_create,
4076 .destroy = debug_destroy,
4077 .populate = debug_populate,
4078 .subsys_id = debug_subsys_id,
4079};
4080#endif /* CONFIG_CGROUP_DEBUG */