blob: 3737a682cdf52229ee78efa5c637164ba60dcc28 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
11 *
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
14 *
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
19 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
23 */
24
25#include <linux/cgroup.h>
26#include <linux/errno.h>
27#include <linux/fs.h>
28#include <linux/kernel.h>
29#include <linux/list.h>
30#include <linux/mm.h>
31#include <linux/mutex.h>
32#include <linux/mount.h>
33#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070034#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/rcupdate.h>
36#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070037#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070038#include <linux/seq_file.h>
39#include <linux/slab.h>
40#include <linux/magic.h>
41#include <linux/spinlock.h>
42#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070043#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070044#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070045#include <linux/delayacct.h>
46#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070047#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040048#include <linux/namei.h>
Alessio Igor Bogani337eb002009-05-12 15:10:54 +020049#include <linux/smp_lock.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070050
Paul Menageddbcc7e2007-10-18 23:39:30 -070051#include <asm/atomic.h>
52
Paul Menage81a6a5c2007-10-18 23:39:38 -070053static DEFINE_MUTEX(cgroup_mutex);
54
Paul Menageddbcc7e2007-10-18 23:39:30 -070055/* Generate an array of cgroup subsystem pointers */
56#define SUBSYS(_x) &_x ## _subsys,
57
58static struct cgroup_subsys *subsys[] = {
59#include <linux/cgroup_subsys.h>
60};
61
62/*
63 * A cgroupfs_root represents the root of a cgroup hierarchy,
64 * and may be associated with a superblock to form an active
65 * hierarchy
66 */
67struct cgroupfs_root {
68 struct super_block *sb;
69
70 /*
71 * The bitmask of subsystems intended to be attached to this
72 * hierarchy
73 */
74 unsigned long subsys_bits;
75
76 /* The bitmask of subsystems currently attached to this hierarchy */
77 unsigned long actual_subsys_bits;
78
79 /* A list running through the attached subsystems */
80 struct list_head subsys_list;
81
82 /* The root cgroup for this hierarchy */
83 struct cgroup top_cgroup;
84
85 /* Tracks how many cgroups are currently defined in hierarchy.*/
86 int number_of_cgroups;
87
Li Zefane5f6a862009-01-07 18:07:41 -080088 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -070089 struct list_head root_list;
90
91 /* Hierarchy-specific flags */
92 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -070093
Paul Menagee788e062008-07-25 01:46:59 -070094 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -070095 char release_agent_path[PATH_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -070096};
97
Paul Menageddbcc7e2007-10-18 23:39:30 -070098/*
99 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
100 * subsystems that are otherwise unattached - it never has more than a
101 * single cgroup, and all tasks are part of that cgroup.
102 */
103static struct cgroupfs_root rootnode;
104
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700105/*
106 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
107 * cgroup_subsys->use_id != 0.
108 */
109#define CSS_ID_MAX (65535)
110struct css_id {
111 /*
112 * The css to which this ID points. This pointer is set to valid value
113 * after cgroup is populated. If cgroup is removed, this will be NULL.
114 * This pointer is expected to be RCU-safe because destroy()
115 * is called after synchronize_rcu(). But for safe use, css_is_removed()
116 * css_tryget() should be used for avoiding race.
117 */
118 struct cgroup_subsys_state *css;
119 /*
120 * ID of this css.
121 */
122 unsigned short id;
123 /*
124 * Depth in hierarchy which this ID belongs to.
125 */
126 unsigned short depth;
127 /*
128 * ID is freed by RCU. (and lookup routine is RCU safe.)
129 */
130 struct rcu_head rcu_head;
131 /*
132 * Hierarchy of CSS ID belongs to.
133 */
134 unsigned short stack[0]; /* Array of Length (depth+1) */
135};
136
137
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138/* The list of hierarchy roots */
139
140static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700141static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700142
143/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
144#define dummytop (&rootnode.top_cgroup)
145
146/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800147 * check for fork/exit handlers to call. This avoids us having to do
148 * extra work in the fork/exit path if none of the subsystems need to
149 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700150 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700151static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700152
Paul Menageddbcc7e2007-10-18 23:39:30 -0700153/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700154inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700155{
Paul Menagebd89aab2007-10-18 23:40:44 -0700156 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700157}
158
159/* bits in struct cgroupfs_root flags field */
160enum {
161 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
162};
163
Adrian Bunke9685a02008-02-07 00:13:46 -0800164static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700165{
166 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700167 (1 << CGRP_RELEASABLE) |
168 (1 << CGRP_NOTIFY_ON_RELEASE);
169 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700170}
171
Adrian Bunke9685a02008-02-07 00:13:46 -0800172static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700173{
Paul Menagebd89aab2007-10-18 23:40:44 -0700174 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700175}
176
Paul Menageddbcc7e2007-10-18 23:39:30 -0700177/*
178 * for_each_subsys() allows you to iterate on each subsystem attached to
179 * an active hierarchy
180 */
181#define for_each_subsys(_root, _ss) \
182list_for_each_entry(_ss, &_root->subsys_list, sibling)
183
Li Zefane5f6a862009-01-07 18:07:41 -0800184/* for_each_active_root() allows you to iterate across the active hierarchies */
185#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700186list_for_each_entry(_root, &roots, root_list)
187
Paul Menage81a6a5c2007-10-18 23:39:38 -0700188/* the list of cgroups eligible for automatic release. Protected by
189 * release_list_lock */
190static LIST_HEAD(release_list);
191static DEFINE_SPINLOCK(release_list_lock);
192static void cgroup_release_agent(struct work_struct *work);
193static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700194static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700195
Paul Menage817929e2007-10-18 23:39:36 -0700196/* Link structure for associating css_set objects with cgroups */
197struct cg_cgroup_link {
198 /*
199 * List running through cg_cgroup_links associated with a
200 * cgroup, anchored on cgroup->css_sets
201 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700202 struct list_head cgrp_link_list;
Paul Menage817929e2007-10-18 23:39:36 -0700203 /*
204 * List running through cg_cgroup_links pointing at a
205 * single css_set object, anchored on css_set->cg_links
206 */
207 struct list_head cg_link_list;
208 struct css_set *cg;
209};
210
211/* The default css_set - used by init and its children prior to any
212 * hierarchies being mounted. It contains a pointer to the root state
213 * for each subsystem. Also used to anchor the list of css_sets. Not
214 * reference-counted, to improve performance when child cgroups
215 * haven't been created.
216 */
217
218static struct css_set init_css_set;
219static struct cg_cgroup_link init_css_set_link;
220
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700221static int cgroup_subsys_init_idr(struct cgroup_subsys *ss);
222
Paul Menage817929e2007-10-18 23:39:36 -0700223/* css_set_lock protects the list of css_set objects, and the
224 * chain of tasks off each css_set. Nests outside task->alloc_lock
225 * due to cgroup_iter_start() */
226static DEFINE_RWLOCK(css_set_lock);
227static int css_set_count;
228
Li Zefan472b1052008-04-29 01:00:11 -0700229/* hash table for cgroup groups. This improves the performance to
230 * find an existing css_set */
231#define CSS_SET_HASH_BITS 7
232#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
233static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
234
235static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
236{
237 int i;
238 int index;
239 unsigned long tmp = 0UL;
240
241 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
242 tmp += (unsigned long)css[i];
243 tmp = (tmp >> 16) ^ tmp;
244
245 index = hash_long(tmp, CSS_SET_HASH_BITS);
246
247 return &css_set_table[index];
248}
249
Paul Menage817929e2007-10-18 23:39:36 -0700250/* We don't maintain the lists running through each css_set to its
251 * task until after the first call to cgroup_iter_start(). This
252 * reduces the fork()/exit() overhead for people who have cgroups
253 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700254static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700255
256/* When we create or destroy a css_set, the operation simply
257 * takes/releases a reference count on all the cgroups referenced
258 * by subsystems in this css_set. This can end up multiple-counting
259 * some cgroups, but that's OK - the ref-count is just a
260 * busy/not-busy indicator; ensuring that we only count each cgroup
261 * once would require taking a global lock to ensure that no
Paul Menageb4f48b62007-10-18 23:39:33 -0700262 * subsystems moved between hierarchies while we were doing so.
263 *
264 * Possible TODO: decide at boot time based on the number of
265 * registered subsystems and the number of CPUs or NUMA nodes whether
266 * it's better for performance to ref-count every subsystem, or to
267 * take a global lock and only add one ref count to each hierarchy.
268 */
Paul Menageb4f48b62007-10-18 23:39:33 -0700269
Paul Menage817929e2007-10-18 23:39:36 -0700270/*
271 * unlink a css_set from the list and free it
272 */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700273static void unlink_css_set(struct css_set *cg)
Paul Menageb4f48b62007-10-18 23:39:33 -0700274{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700275 struct cg_cgroup_link *link;
276 struct cg_cgroup_link *saved_link;
277
Li Zefan472b1052008-04-29 01:00:11 -0700278 hlist_del(&cg->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700279 css_set_count--;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700280
281 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
282 cg_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -0700283 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700284 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700285 kfree(link);
286 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700287}
288
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700289static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700290{
291 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700292 /*
293 * Ensure that the refcount doesn't hit zero while any readers
294 * can see it. Similar to atomic_dec_and_lock(), but for an
295 * rwlock
296 */
297 if (atomic_add_unless(&cg->refcount, -1, 1))
298 return;
299 write_lock(&css_set_lock);
300 if (!atomic_dec_and_test(&cg->refcount)) {
301 write_unlock(&css_set_lock);
302 return;
303 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700304 unlink_css_set(cg);
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700305 write_unlock(&css_set_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700306
307 rcu_read_lock();
308 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagea47295e2009-01-07 18:07:44 -0800309 struct cgroup *cgrp = rcu_dereference(cg->subsys[i]->cgroup);
Paul Menagebd89aab2007-10-18 23:40:44 -0700310 if (atomic_dec_and_test(&cgrp->count) &&
311 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700312 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700313 set_bit(CGRP_RELEASABLE, &cgrp->flags);
314 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700315 }
316 }
317 rcu_read_unlock();
Paul Menage817929e2007-10-18 23:39:36 -0700318 kfree(cg);
319}
320
321/*
322 * refcounted get/put for css_set objects
323 */
324static inline void get_css_set(struct css_set *cg)
325{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700326 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700327}
328
329static inline void put_css_set(struct css_set *cg)
330{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700331 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700332}
333
Paul Menage81a6a5c2007-10-18 23:39:38 -0700334static inline void put_css_set_taskexit(struct css_set *cg)
335{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700336 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700337}
338
Paul Menage817929e2007-10-18 23:39:36 -0700339/*
340 * find_existing_css_set() is a helper for
341 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700342 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700343 *
344 * oldcg: the cgroup group that we're using before the cgroup
345 * transition
346 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700347 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700348 *
349 * template: location in which to build the desired set of subsystem
350 * state objects for the new cgroup group
351 */
Paul Menage817929e2007-10-18 23:39:36 -0700352static struct css_set *find_existing_css_set(
353 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700354 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700355 struct cgroup_subsys_state *template[])
356{
357 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700358 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700359 struct hlist_head *hhead;
360 struct hlist_node *node;
361 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700362
363 /* Built the set of subsystem state objects that we want to
364 * see in the new css_set */
365 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800366 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700367 /* Subsystem is in this hierarchy. So we want
368 * the subsystem state from the new
369 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700370 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700371 } else {
372 /* Subsystem is not in this hierarchy, so we
373 * don't want to change the subsystem state */
374 template[i] = oldcg->subsys[i];
375 }
376 }
377
Li Zefan472b1052008-04-29 01:00:11 -0700378 hhead = css_set_hash(template);
379 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage817929e2007-10-18 23:39:36 -0700380 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
381 /* All subsystems matched */
382 return cg;
383 }
Li Zefan472b1052008-04-29 01:00:11 -0700384 }
Paul Menage817929e2007-10-18 23:39:36 -0700385
386 /* No existing cgroup group matched */
387 return NULL;
388}
389
Paul Menage817929e2007-10-18 23:39:36 -0700390static void free_cg_links(struct list_head *tmp)
391{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700392 struct cg_cgroup_link *link;
393 struct cg_cgroup_link *saved_link;
394
395 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700396 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700397 kfree(link);
398 }
399}
400
401/*
Li Zefan36553432008-07-29 22:33:19 -0700402 * allocate_cg_links() allocates "count" cg_cgroup_link structures
403 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
404 * success or a negative error
405 */
406static int allocate_cg_links(int count, struct list_head *tmp)
407{
408 struct cg_cgroup_link *link;
409 int i;
410 INIT_LIST_HEAD(tmp);
411 for (i = 0; i < count; i++) {
412 link = kmalloc(sizeof(*link), GFP_KERNEL);
413 if (!link) {
414 free_cg_links(tmp);
415 return -ENOMEM;
416 }
417 list_add(&link->cgrp_link_list, tmp);
418 }
419 return 0;
420}
421
Li Zefanc12f65d2009-01-07 18:07:42 -0800422/**
423 * link_css_set - a helper function to link a css_set to a cgroup
424 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
425 * @cg: the css_set to be linked
426 * @cgrp: the destination cgroup
427 */
428static void link_css_set(struct list_head *tmp_cg_links,
429 struct css_set *cg, struct cgroup *cgrp)
430{
431 struct cg_cgroup_link *link;
432
433 BUG_ON(list_empty(tmp_cg_links));
434 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
435 cgrp_link_list);
436 link->cg = cg;
437 list_move(&link->cgrp_link_list, &cgrp->css_sets);
438 list_add(&link->cg_link_list, &cg->cg_links);
439}
440
Li Zefan36553432008-07-29 22:33:19 -0700441/*
Paul Menage817929e2007-10-18 23:39:36 -0700442 * find_css_set() takes an existing cgroup group and a
443 * cgroup object, and returns a css_set object that's
444 * equivalent to the old group, but with the given cgroup
445 * substituted into the appropriate hierarchy. Must be called with
446 * cgroup_mutex held
447 */
Paul Menage817929e2007-10-18 23:39:36 -0700448static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700449 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700450{
451 struct css_set *res;
452 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
453 int i;
454
455 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700456
Li Zefan472b1052008-04-29 01:00:11 -0700457 struct hlist_head *hhead;
458
Paul Menage817929e2007-10-18 23:39:36 -0700459 /* First see if we already have a cgroup group that matches
460 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700461 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700462 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700463 if (res)
464 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700465 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700466
467 if (res)
468 return res;
469
470 res = kmalloc(sizeof(*res), GFP_KERNEL);
471 if (!res)
472 return NULL;
473
474 /* Allocate all the cg_cgroup_link objects that we'll need */
475 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
476 kfree(res);
477 return NULL;
478 }
479
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700480 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700481 INIT_LIST_HEAD(&res->cg_links);
482 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700483 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700484
485 /* Copy the set of subsystem state objects generated in
486 * find_existing_css_set() */
487 memcpy(res->subsys, template, sizeof(res->subsys));
488
489 write_lock(&css_set_lock);
490 /* Add reference counts and links from the new css_set. */
491 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700492 struct cgroup *cgrp = res->subsys[i]->cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700493 struct cgroup_subsys *ss = subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -0700494 atomic_inc(&cgrp->count);
Paul Menage817929e2007-10-18 23:39:36 -0700495 /*
496 * We want to add a link once per cgroup, so we
497 * only do it for the first subsystem in each
498 * hierarchy
499 */
Li Zefanc12f65d2009-01-07 18:07:42 -0800500 if (ss->root->subsys_list.next == &ss->sibling)
501 link_css_set(&tmp_cg_links, res, cgrp);
Paul Menage817929e2007-10-18 23:39:36 -0700502 }
Li Zefanc12f65d2009-01-07 18:07:42 -0800503 if (list_empty(&rootnode.subsys_list))
504 link_css_set(&tmp_cg_links, res, dummytop);
Paul Menage817929e2007-10-18 23:39:36 -0700505
506 BUG_ON(!list_empty(&tmp_cg_links));
507
Paul Menage817929e2007-10-18 23:39:36 -0700508 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700509
510 /* Add this cgroup group to the hash table */
511 hhead = css_set_hash(res->subsys);
512 hlist_add_head(&res->hlist, hhead);
513
Paul Menage817929e2007-10-18 23:39:36 -0700514 write_unlock(&css_set_lock);
515
516 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700517}
518
Paul Menageddbcc7e2007-10-18 23:39:30 -0700519/*
520 * There is one global cgroup mutex. We also require taking
521 * task_lock() when dereferencing a task's cgroup subsys pointers.
522 * See "The task_lock() exception", at the end of this comment.
523 *
524 * A task must hold cgroup_mutex to modify cgroups.
525 *
526 * Any task can increment and decrement the count field without lock.
527 * So in general, code holding cgroup_mutex can't rely on the count
528 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800529 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700530 * means that no tasks are currently attached, therefore there is no
531 * way a task attached to that cgroup can fork (the other way to
532 * increment the count). So code holding cgroup_mutex can safely
533 * assume that if the count is zero, it will stay zero. Similarly, if
534 * a task holds cgroup_mutex on a cgroup with zero count, it
535 * knows that the cgroup won't be removed, as cgroup_rmdir()
536 * needs that mutex.
537 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700538 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
539 * (usually) take cgroup_mutex. These are the two most performance
540 * critical pieces of code here. The exception occurs on cgroup_exit(),
541 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
542 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800543 * to the release agent with the name of the cgroup (path relative to
544 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700545 *
546 * A cgroup can only be deleted if both its 'count' of using tasks
547 * is zero, and its list of 'children' cgroups is empty. Since all
548 * tasks in the system use _some_ cgroup, and since there is always at
549 * least one task in the system (init, pid == 1), therefore, top_cgroup
550 * always has either children cgroups and/or using tasks. So we don't
551 * need a special hack to ensure that top_cgroup cannot be deleted.
552 *
553 * The task_lock() exception
554 *
555 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800556 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800557 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700558 * several performance critical places that need to reference
559 * task->cgroup without the expense of grabbing a system global
560 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800561 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700562 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
563 * the task_struct routinely used for such matters.
564 *
565 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800566 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700567 */
568
Paul Menageddbcc7e2007-10-18 23:39:30 -0700569/**
570 * cgroup_lock - lock out any changes to cgroup structures
571 *
572 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700573void cgroup_lock(void)
574{
575 mutex_lock(&cgroup_mutex);
576}
577
578/**
579 * cgroup_unlock - release lock on cgroup changes
580 *
581 * Undo the lock taken in a previous cgroup_lock() call.
582 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700583void cgroup_unlock(void)
584{
585 mutex_unlock(&cgroup_mutex);
586}
587
588/*
589 * A couple of forward declarations required, due to cyclic reference loop:
590 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
591 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
592 * -> cgroup_mkdir.
593 */
594
595static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
596static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700597static int cgroup_populate_dir(struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700598static struct inode_operations cgroup_dir_inode_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700599static struct file_operations proc_cgroupstats_operations;
600
601static struct backing_dev_info cgroup_backing_dev_info = {
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700602 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700603};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700604
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700605static int alloc_css_id(struct cgroup_subsys *ss,
606 struct cgroup *parent, struct cgroup *child);
607
Paul Menageddbcc7e2007-10-18 23:39:30 -0700608static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
609{
610 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700611
612 if (inode) {
613 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100614 inode->i_uid = current_fsuid();
615 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700616 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
617 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
618 }
619 return inode;
620}
621
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800622/*
623 * Call subsys's pre_destroy handler.
624 * This is called before css refcnt check.
625 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700626static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800627{
628 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700629 int ret = 0;
630
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800631 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700632 if (ss->pre_destroy) {
633 ret = ss->pre_destroy(ss, cgrp);
634 if (ret)
635 break;
636 }
637 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800638}
639
Paul Menagea47295e2009-01-07 18:07:44 -0800640static void free_cgroup_rcu(struct rcu_head *obj)
641{
642 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
643
644 kfree(cgrp);
645}
646
Paul Menageddbcc7e2007-10-18 23:39:30 -0700647static void cgroup_diput(struct dentry *dentry, struct inode *inode)
648{
649 /* is dentry a directory ? if so, kfree() associated cgroup */
650 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700651 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800652 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700653 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700654 /* It's possible for external users to be holding css
655 * reference counts on a cgroup; css_put() needs to
656 * be able to access the cgroup after decrementing
657 * the reference count in order to know if it needs to
658 * queue the cgroup to be handled by the release
659 * agent */
660 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800661
662 mutex_lock(&cgroup_mutex);
663 /*
664 * Release the subsystem state objects.
665 */
Li Zefan75139b82009-01-07 18:07:33 -0800666 for_each_subsys(cgrp->root, ss)
667 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800668
669 cgrp->root->number_of_cgroups--;
670 mutex_unlock(&cgroup_mutex);
671
Paul Menagea47295e2009-01-07 18:07:44 -0800672 /*
673 * Drop the active superblock reference that we took when we
674 * created the cgroup
675 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800676 deactivate_super(cgrp->root->sb);
677
Paul Menagea47295e2009-01-07 18:07:44 -0800678 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700679 }
680 iput(inode);
681}
682
683static void remove_dir(struct dentry *d)
684{
685 struct dentry *parent = dget(d->d_parent);
686
687 d_delete(d);
688 simple_rmdir(parent->d_inode, d);
689 dput(parent);
690}
691
692static void cgroup_clear_directory(struct dentry *dentry)
693{
694 struct list_head *node;
695
696 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
697 spin_lock(&dcache_lock);
698 node = dentry->d_subdirs.next;
699 while (node != &dentry->d_subdirs) {
700 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
701 list_del_init(node);
702 if (d->d_inode) {
703 /* This should never be called on a cgroup
704 * directory with child cgroups */
705 BUG_ON(d->d_inode->i_mode & S_IFDIR);
706 d = dget_locked(d);
707 spin_unlock(&dcache_lock);
708 d_delete(d);
709 simple_unlink(dentry->d_inode, d);
710 dput(d);
711 spin_lock(&dcache_lock);
712 }
713 node = dentry->d_subdirs.next;
714 }
715 spin_unlock(&dcache_lock);
716}
717
718/*
719 * NOTE : the dentry must have been dget()'ed
720 */
721static void cgroup_d_remove_dir(struct dentry *dentry)
722{
723 cgroup_clear_directory(dentry);
724
725 spin_lock(&dcache_lock);
726 list_del_init(&dentry->d_u.d_child);
727 spin_unlock(&dcache_lock);
728 remove_dir(dentry);
729}
730
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700731/*
732 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
733 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
734 * reference to css->refcnt. In general, this refcnt is expected to goes down
735 * to zero, soon.
736 *
737 * CGRP_WAIT_ON_RMDIR flag is modified under cgroup's inode->i_mutex;
738 */
739DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
740
741static void cgroup_wakeup_rmdir_waiters(const struct cgroup *cgrp)
742{
743 if (unlikely(test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
744 wake_up_all(&cgroup_rmdir_waitq);
745}
746
Paul Menageddbcc7e2007-10-18 23:39:30 -0700747static int rebind_subsystems(struct cgroupfs_root *root,
748 unsigned long final_bits)
749{
750 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700751 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700752 int i;
753
754 removed_bits = root->actual_subsys_bits & ~final_bits;
755 added_bits = final_bits & ~root->actual_subsys_bits;
756 /* Check that any added subsystems are currently free */
757 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800758 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700759 struct cgroup_subsys *ss = subsys[i];
760 if (!(bit & added_bits))
761 continue;
762 if (ss->root != &rootnode) {
763 /* Subsystem isn't free */
764 return -EBUSY;
765 }
766 }
767
768 /* Currently we don't handle adding/removing subsystems when
769 * any child cgroups exist. This is theoretically supportable
770 * but involves complex error handling, so it's being left until
771 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800772 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773 return -EBUSY;
774
775 /* Process each subsystem */
776 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
777 struct cgroup_subsys *ss = subsys[i];
778 unsigned long bit = 1UL << i;
779 if (bit & added_bits) {
780 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700781 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700782 BUG_ON(!dummytop->subsys[i]);
783 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800784 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700785 cgrp->subsys[i] = dummytop->subsys[i];
786 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800787 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800788 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700789 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700790 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800791 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792 } else if (bit & removed_bits) {
793 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700794 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
795 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800796 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700797 if (ss->bind)
798 ss->bind(ss, dummytop);
799 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700800 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800801 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -0800802 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -0800803 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700804 } else if (bit & final_bits) {
805 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700806 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700807 } else {
808 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700809 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700810 }
811 }
812 root->subsys_bits = root->actual_subsys_bits = final_bits;
813 synchronize_rcu();
814
815 return 0;
816}
817
818static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
819{
820 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
821 struct cgroup_subsys *ss;
822
823 mutex_lock(&cgroup_mutex);
824 for_each_subsys(root, ss)
825 seq_printf(seq, ",%s", ss->name);
826 if (test_bit(ROOT_NOPREFIX, &root->flags))
827 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700828 if (strlen(root->release_agent_path))
829 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700830 mutex_unlock(&cgroup_mutex);
831 return 0;
832}
833
834struct cgroup_sb_opts {
835 unsigned long subsys_bits;
836 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700837 char *release_agent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700838};
839
840/* Convert a hierarchy specifier into a bitmask of subsystems and
841 * flags. */
842static int parse_cgroupfs_options(char *data,
843 struct cgroup_sb_opts *opts)
844{
845 char *token, *o = data ?: "all";
Li Zefanf9ab5b52009-06-17 16:26:33 -0700846 unsigned long mask = (unsigned long)-1;
847
848#ifdef CONFIG_CPUSETS
849 mask = ~(1UL << cpuset_subsys_id);
850#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -0700851
852 opts->subsys_bits = 0;
853 opts->flags = 0;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700854 opts->release_agent = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700855
856 while ((token = strsep(&o, ",")) != NULL) {
857 if (!*token)
858 return -EINVAL;
859 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700860 /* Add all non-disabled subsystems */
861 int i;
862 opts->subsys_bits = 0;
863 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
864 struct cgroup_subsys *ss = subsys[i];
865 if (!ss->disabled)
866 opts->subsys_bits |= 1ul << i;
867 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700868 } else if (!strcmp(token, "noprefix")) {
869 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700870 } else if (!strncmp(token, "release_agent=", 14)) {
871 /* Specifying two release agents is forbidden */
872 if (opts->release_agent)
873 return -EINVAL;
874 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
875 if (!opts->release_agent)
876 return -ENOMEM;
877 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
878 opts->release_agent[PATH_MAX - 1] = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700879 } else {
880 struct cgroup_subsys *ss;
881 int i;
882 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
883 ss = subsys[i];
884 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700885 if (!ss->disabled)
886 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700887 break;
888 }
889 }
890 if (i == CGROUP_SUBSYS_COUNT)
891 return -ENOENT;
892 }
893 }
894
Li Zefanf9ab5b52009-06-17 16:26:33 -0700895 /*
896 * Option noprefix was introduced just for backward compatibility
897 * with the old cpuset, so we allow noprefix only if mounting just
898 * the cpuset subsystem.
899 */
900 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
901 (opts->subsys_bits & mask))
902 return -EINVAL;
903
Paul Menageddbcc7e2007-10-18 23:39:30 -0700904 /* We can't have an empty hierarchy */
905 if (!opts->subsys_bits)
906 return -EINVAL;
907
908 return 0;
909}
910
911static int cgroup_remount(struct super_block *sb, int *flags, char *data)
912{
913 int ret = 0;
914 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -0700915 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700916 struct cgroup_sb_opts opts;
917
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200918 lock_kernel();
Paul Menagebd89aab2007-10-18 23:40:44 -0700919 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700920 mutex_lock(&cgroup_mutex);
921
922 /* See what subsystems are wanted */
923 ret = parse_cgroupfs_options(data, &opts);
924 if (ret)
925 goto out_unlock;
926
927 /* Don't allow flags to change at remount */
928 if (opts.flags != root->flags) {
929 ret = -EINVAL;
930 goto out_unlock;
931 }
932
933 ret = rebind_subsystems(root, opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -0700934 if (ret)
935 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700936
937 /* (re)populate subsystem files */
Li Zefan0670e082009-04-02 16:57:30 -0700938 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700939
Paul Menage81a6a5c2007-10-18 23:39:38 -0700940 if (opts.release_agent)
941 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700942 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -0700943 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700944 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700945 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200946 unlock_kernel();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700947 return ret;
948}
949
950static struct super_operations cgroup_ops = {
951 .statfs = simple_statfs,
952 .drop_inode = generic_delete_inode,
953 .show_options = cgroup_show_options,
954 .remount_fs = cgroup_remount,
955};
956
Paul Menagecc31edc2008-10-18 20:28:04 -0700957static void init_cgroup_housekeeping(struct cgroup *cgrp)
958{
959 INIT_LIST_HEAD(&cgrp->sibling);
960 INIT_LIST_HEAD(&cgrp->children);
961 INIT_LIST_HEAD(&cgrp->css_sets);
962 INIT_LIST_HEAD(&cgrp->release_list);
963 init_rwsem(&cgrp->pids_mutex);
964}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700965static void init_cgroup_root(struct cgroupfs_root *root)
966{
Paul Menagebd89aab2007-10-18 23:40:44 -0700967 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968 INIT_LIST_HEAD(&root->subsys_list);
969 INIT_LIST_HEAD(&root->root_list);
970 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -0700971 cgrp->root = root;
972 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -0700973 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700974}
975
976static int cgroup_test_super(struct super_block *sb, void *data)
977{
978 struct cgroupfs_root *new = data;
979 struct cgroupfs_root *root = sb->s_fs_info;
980
981 /* First check subsystems */
982 if (new->subsys_bits != root->subsys_bits)
983 return 0;
984
985 /* Next check flags */
986 if (new->flags != root->flags)
987 return 0;
988
989 return 1;
990}
991
992static int cgroup_set_super(struct super_block *sb, void *data)
993{
994 int ret;
995 struct cgroupfs_root *root = data;
996
997 ret = set_anon_super(sb, NULL);
998 if (ret)
999 return ret;
1000
1001 sb->s_fs_info = root;
1002 root->sb = sb;
1003
1004 sb->s_blocksize = PAGE_CACHE_SIZE;
1005 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1006 sb->s_magic = CGROUP_SUPER_MAGIC;
1007 sb->s_op = &cgroup_ops;
1008
1009 return 0;
1010}
1011
1012static int cgroup_get_rootdir(struct super_block *sb)
1013{
1014 struct inode *inode =
1015 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1016 struct dentry *dentry;
1017
1018 if (!inode)
1019 return -ENOMEM;
1020
Paul Menageddbcc7e2007-10-18 23:39:30 -07001021 inode->i_fop = &simple_dir_operations;
1022 inode->i_op = &cgroup_dir_inode_operations;
1023 /* directories start off with i_nlink == 2 (for "." entry) */
1024 inc_nlink(inode);
1025 dentry = d_alloc_root(inode);
1026 if (!dentry) {
1027 iput(inode);
1028 return -ENOMEM;
1029 }
1030 sb->s_root = dentry;
1031 return 0;
1032}
1033
1034static int cgroup_get_sb(struct file_system_type *fs_type,
1035 int flags, const char *unused_dev_name,
1036 void *data, struct vfsmount *mnt)
1037{
1038 struct cgroup_sb_opts opts;
1039 int ret = 0;
1040 struct super_block *sb;
1041 struct cgroupfs_root *root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001042 struct list_head tmp_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043
1044 /* First find the desired set of subsystems */
1045 ret = parse_cgroupfs_options(data, &opts);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001046 if (ret) {
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001047 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048 return ret;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001049 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001050
1051 root = kzalloc(sizeof(*root), GFP_KERNEL);
Li Zefanf7770732008-02-23 15:24:10 -08001052 if (!root) {
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001053 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054 return -ENOMEM;
Li Zefanf7770732008-02-23 15:24:10 -08001055 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056
1057 init_cgroup_root(root);
1058 root->subsys_bits = opts.subsys_bits;
1059 root->flags = opts.flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001060 if (opts.release_agent) {
1061 strcpy(root->release_agent_path, opts.release_agent);
1062 kfree(opts.release_agent);
1063 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001064
1065 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
1066
1067 if (IS_ERR(sb)) {
1068 kfree(root);
1069 return PTR_ERR(sb);
1070 }
1071
1072 if (sb->s_fs_info != root) {
1073 /* Reusing an existing superblock */
1074 BUG_ON(sb->s_root == NULL);
1075 kfree(root);
1076 root = NULL;
1077 } else {
1078 /* New superblock */
Li Zefanc12f65d2009-01-07 18:07:42 -08001079 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001080 struct inode *inode;
Li Zefan28fd5df2008-04-29 01:00:13 -07001081 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082
1083 BUG_ON(sb->s_root != NULL);
1084
1085 ret = cgroup_get_rootdir(sb);
1086 if (ret)
1087 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001088 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089
Paul Menage817929e2007-10-18 23:39:36 -07001090 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091 mutex_lock(&cgroup_mutex);
1092
Paul Menage817929e2007-10-18 23:39:36 -07001093 /*
1094 * We're accessing css_set_count without locking
1095 * css_set_lock here, but that's OK - it can only be
1096 * increased by someone holding cgroup_lock, and
1097 * that's us. The worst that can happen is that we
1098 * have some link structures left over
1099 */
1100 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1101 if (ret) {
1102 mutex_unlock(&cgroup_mutex);
1103 mutex_unlock(&inode->i_mutex);
1104 goto drop_new_super;
1105 }
1106
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 ret = rebind_subsystems(root, root->subsys_bits);
1108 if (ret == -EBUSY) {
1109 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001110 mutex_unlock(&inode->i_mutex);
Li Zefan20ca9b32008-12-23 13:57:14 -08001111 goto free_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001112 }
1113
1114 /* EBUSY should be the only error here */
1115 BUG_ON(ret);
1116
1117 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001118 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001119
Li Zefanc12f65d2009-01-07 18:07:42 -08001120 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001121 root->top_cgroup.dentry = sb->s_root;
1122
Paul Menage817929e2007-10-18 23:39:36 -07001123 /* Link the top cgroup in this hierarchy into all
1124 * the css_set objects */
1125 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001126 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1127 struct hlist_head *hhead = &css_set_table[i];
1128 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001129 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001130
Li Zefanc12f65d2009-01-07 18:07:42 -08001131 hlist_for_each_entry(cg, node, hhead, hlist)
1132 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001133 }
Paul Menage817929e2007-10-18 23:39:36 -07001134 write_unlock(&css_set_lock);
1135
1136 free_cg_links(&tmp_cg_links);
1137
Li Zefanc12f65d2009-01-07 18:07:42 -08001138 BUG_ON(!list_empty(&root_cgrp->sibling));
1139 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001140 BUG_ON(root->number_of_cgroups != 1);
1141
Li Zefanc12f65d2009-01-07 18:07:42 -08001142 cgroup_populate_dir(root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001143 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001144 mutex_unlock(&cgroup_mutex);
1145 }
1146
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001147 simple_set_mnt(mnt, sb);
1148 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001149
Li Zefan20ca9b32008-12-23 13:57:14 -08001150 free_cg_links:
1151 free_cg_links(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001152 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001153 deactivate_locked_super(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001154 return ret;
1155}
1156
1157static void cgroup_kill_sb(struct super_block *sb) {
1158 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001159 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001160 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001161 struct cg_cgroup_link *link;
1162 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001163
1164 BUG_ON(!root);
1165
1166 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001167 BUG_ON(!list_empty(&cgrp->children));
1168 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001169
1170 mutex_lock(&cgroup_mutex);
1171
1172 /* Rebind all subsystems back to the default hierarchy */
1173 ret = rebind_subsystems(root, 0);
1174 /* Shouldn't be able to fail ... */
1175 BUG_ON(ret);
1176
Paul Menage817929e2007-10-18 23:39:36 -07001177 /*
1178 * Release all the links from css_sets to this hierarchy's
1179 * root cgroup
1180 */
1181 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001182
1183 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1184 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001185 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001186 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001187 kfree(link);
1188 }
1189 write_unlock(&css_set_lock);
1190
Paul Menage839ec542009-01-29 14:25:22 -08001191 if (!list_empty(&root->root_list)) {
1192 list_del(&root->root_list);
1193 root_count--;
1194 }
Li Zefane5f6a862009-01-07 18:07:41 -08001195
Paul Menageddbcc7e2007-10-18 23:39:30 -07001196 mutex_unlock(&cgroup_mutex);
1197
Paul Menageddbcc7e2007-10-18 23:39:30 -07001198 kill_litter_super(sb);
Li Zefan67e055d2009-02-18 14:48:20 -08001199 kfree(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001200}
1201
1202static struct file_system_type cgroup_fs_type = {
1203 .name = "cgroup",
1204 .get_sb = cgroup_get_sb,
1205 .kill_sb = cgroup_kill_sb,
1206};
1207
Paul Menagebd89aab2007-10-18 23:40:44 -07001208static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001209{
1210 return dentry->d_fsdata;
1211}
1212
1213static inline struct cftype *__d_cft(struct dentry *dentry)
1214{
1215 return dentry->d_fsdata;
1216}
1217
Li Zefana043e3b2008-02-23 15:24:09 -08001218/**
1219 * cgroup_path - generate the path of a cgroup
1220 * @cgrp: the cgroup in question
1221 * @buf: the buffer to write the path into
1222 * @buflen: the length of the buffer
1223 *
Paul Menagea47295e2009-01-07 18:07:44 -08001224 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1225 * reference. Writes path of cgroup into buf. Returns 0 on success,
1226 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001227 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001228int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001229{
1230 char *start;
Paul Menagea47295e2009-01-07 18:07:44 -08001231 struct dentry *dentry = rcu_dereference(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001232
Paul Menagea47295e2009-01-07 18:07:44 -08001233 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001234 /*
1235 * Inactive subsystems have no dentry for their root
1236 * cgroup
1237 */
1238 strcpy(buf, "/");
1239 return 0;
1240 }
1241
1242 start = buf + buflen;
1243
1244 *--start = '\0';
1245 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001246 int len = dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001247 if ((start -= len) < buf)
1248 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001249 memcpy(start, cgrp->dentry->d_name.name, len);
1250 cgrp = cgrp->parent;
1251 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001252 break;
Paul Menagea47295e2009-01-07 18:07:44 -08001253 dentry = rcu_dereference(cgrp->dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001254 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001255 continue;
1256 if (--start < buf)
1257 return -ENAMETOOLONG;
1258 *start = '/';
1259 }
1260 memmove(buf, start, buf + buflen - start);
1261 return 0;
1262}
1263
Paul Menagebbcb81d2007-10-18 23:39:32 -07001264/*
1265 * Return the first subsystem attached to a cgroup's hierarchy, and
1266 * its subsystem id.
1267 */
1268
Paul Menagebd89aab2007-10-18 23:40:44 -07001269static void get_first_subsys(const struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001270 struct cgroup_subsys_state **css, int *subsys_id)
1271{
Paul Menagebd89aab2007-10-18 23:40:44 -07001272 const struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001273 const struct cgroup_subsys *test_ss;
1274 BUG_ON(list_empty(&root->subsys_list));
1275 test_ss = list_entry(root->subsys_list.next,
1276 struct cgroup_subsys, sibling);
1277 if (css) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001278 *css = cgrp->subsys[test_ss->subsys_id];
Paul Menagebbcb81d2007-10-18 23:39:32 -07001279 BUG_ON(!*css);
1280 }
1281 if (subsys_id)
1282 *subsys_id = test_ss->subsys_id;
1283}
1284
Li Zefana043e3b2008-02-23 15:24:09 -08001285/**
1286 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1287 * @cgrp: the cgroup the task is attaching to
1288 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001289 *
Li Zefana043e3b2008-02-23 15:24:09 -08001290 * Call holding cgroup_mutex. May take task_lock of
1291 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001292 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001293int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001294{
1295 int retval = 0;
1296 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07001297 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001298 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001299 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001300 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001301 int subsys_id;
1302
Paul Menagebd89aab2007-10-18 23:40:44 -07001303 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001304
1305 /* Nothing to do if the task is already in that cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -07001306 oldcgrp = task_cgroup(tsk, subsys_id);
1307 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001308 return 0;
1309
1310 for_each_subsys(root, ss) {
1311 if (ss->can_attach) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001312 retval = ss->can_attach(ss, cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001313 if (retval)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001314 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001315 }
1316 }
1317
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001318 task_lock(tsk);
1319 cg = tsk->cgroups;
1320 get_css_set(cg);
1321 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001322 /*
1323 * Locate or allocate a new css_set for this task,
1324 * based on its final set of cgroups
1325 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001326 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001327 put_css_set(cg);
Paul Jacksone18f6312008-02-07 00:13:44 -08001328 if (!newcg)
Paul Menage817929e2007-10-18 23:39:36 -07001329 return -ENOMEM;
Paul Menage817929e2007-10-18 23:39:36 -07001330
Paul Menagebbcb81d2007-10-18 23:39:32 -07001331 task_lock(tsk);
1332 if (tsk->flags & PF_EXITING) {
1333 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001334 put_css_set(newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001335 return -ESRCH;
1336 }
Paul Menage817929e2007-10-18 23:39:36 -07001337 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001338 task_unlock(tsk);
1339
Paul Menage817929e2007-10-18 23:39:36 -07001340 /* Update the css_set linked lists if we're using them */
1341 write_lock(&css_set_lock);
1342 if (!list_empty(&tsk->cg_list)) {
1343 list_del(&tsk->cg_list);
1344 list_add(&tsk->cg_list, &newcg->tasks);
1345 }
1346 write_unlock(&css_set_lock);
1347
Paul Menagebbcb81d2007-10-18 23:39:32 -07001348 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001349 if (ss->attach)
Paul Menagebd89aab2007-10-18 23:40:44 -07001350 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001351 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001352 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001353 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001354 put_css_set(cg);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001355
1356 /*
1357 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1358 * is no longer empty.
1359 */
1360 cgroup_wakeup_rmdir_waiters(cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001361 return 0;
1362}
1363
1364/*
Paul Menageaf351022008-07-25 01:47:01 -07001365 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1366 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001367 */
Paul Menageaf351022008-07-25 01:47:01 -07001368static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001369{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001370 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001371 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001372 int ret;
1373
Paul Menagebbcb81d2007-10-18 23:39:32 -07001374 if (pid) {
1375 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001376 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001377 if (!tsk || tsk->flags & PF_EXITING) {
1378 rcu_read_unlock();
1379 return -ESRCH;
1380 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001381
David Howellsc69e8d92008-11-14 10:39:19 +11001382 tcred = __task_cred(tsk);
1383 if (cred->euid &&
1384 cred->euid != tcred->uid &&
1385 cred->euid != tcred->suid) {
1386 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001387 return -EACCES;
1388 }
David Howellsc69e8d92008-11-14 10:39:19 +11001389 get_task_struct(tsk);
1390 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001391 } else {
1392 tsk = current;
1393 get_task_struct(tsk);
1394 }
1395
Cliff Wickman956db3c2008-02-07 00:14:43 -08001396 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001397 put_task_struct(tsk);
1398 return ret;
1399}
1400
Paul Menageaf351022008-07-25 01:47:01 -07001401static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1402{
1403 int ret;
1404 if (!cgroup_lock_live_group(cgrp))
1405 return -ENODEV;
1406 ret = attach_task_by_pid(cgrp, pid);
1407 cgroup_unlock();
1408 return ret;
1409}
1410
Paul Menageddbcc7e2007-10-18 23:39:30 -07001411/* The various types of files and directories in a cgroup file system */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001412enum cgroup_filetype {
1413 FILE_ROOT,
1414 FILE_DIR,
1415 FILE_TASKLIST,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001416 FILE_NOTIFY_ON_RELEASE,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001417 FILE_RELEASE_AGENT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001418};
1419
Paul Menagee788e062008-07-25 01:46:59 -07001420/**
1421 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1422 * @cgrp: the cgroup to be checked for liveness
1423 *
Paul Menage84eea842008-07-25 01:47:00 -07001424 * On success, returns true; the lock should be later released with
1425 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07001426 */
Paul Menage84eea842008-07-25 01:47:00 -07001427bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07001428{
1429 mutex_lock(&cgroup_mutex);
1430 if (cgroup_is_removed(cgrp)) {
1431 mutex_unlock(&cgroup_mutex);
1432 return false;
1433 }
1434 return true;
1435}
1436
1437static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1438 const char *buffer)
1439{
1440 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1441 if (!cgroup_lock_live_group(cgrp))
1442 return -ENODEV;
1443 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001444 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001445 return 0;
1446}
1447
1448static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1449 struct seq_file *seq)
1450{
1451 if (!cgroup_lock_live_group(cgrp))
1452 return -ENODEV;
1453 seq_puts(seq, cgrp->root->release_agent_path);
1454 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001455 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001456 return 0;
1457}
1458
Paul Menage84eea842008-07-25 01:47:00 -07001459/* A buffer size big enough for numbers or short strings */
1460#define CGROUP_LOCAL_BUFFER_SIZE 64
1461
Paul Menagee73d2c62008-04-29 01:00:06 -07001462static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001463 struct file *file,
1464 const char __user *userbuf,
1465 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001466{
Paul Menage84eea842008-07-25 01:47:00 -07001467 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001468 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001469 char *end;
1470
1471 if (!nbytes)
1472 return -EINVAL;
1473 if (nbytes >= sizeof(buffer))
1474 return -E2BIG;
1475 if (copy_from_user(buffer, userbuf, nbytes))
1476 return -EFAULT;
1477
1478 buffer[nbytes] = 0; /* nul-terminate */
Paul Menageb7269df2008-04-29 00:59:59 -07001479 strstrip(buffer);
Paul Menagee73d2c62008-04-29 01:00:06 -07001480 if (cft->write_u64) {
1481 u64 val = simple_strtoull(buffer, &end, 0);
1482 if (*end)
1483 return -EINVAL;
1484 retval = cft->write_u64(cgrp, cft, val);
1485 } else {
1486 s64 val = simple_strtoll(buffer, &end, 0);
1487 if (*end)
1488 return -EINVAL;
1489 retval = cft->write_s64(cgrp, cft, val);
1490 }
Paul Menage355e0c42007-10-18 23:39:33 -07001491 if (!retval)
1492 retval = nbytes;
1493 return retval;
1494}
1495
Paul Menagedb3b1492008-07-25 01:46:58 -07001496static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1497 struct file *file,
1498 const char __user *userbuf,
1499 size_t nbytes, loff_t *unused_ppos)
1500{
Paul Menage84eea842008-07-25 01:47:00 -07001501 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001502 int retval = 0;
1503 size_t max_bytes = cft->max_write_len;
1504 char *buffer = local_buffer;
1505
1506 if (!max_bytes)
1507 max_bytes = sizeof(local_buffer) - 1;
1508 if (nbytes >= max_bytes)
1509 return -E2BIG;
1510 /* Allocate a dynamic buffer if we need one */
1511 if (nbytes >= sizeof(local_buffer)) {
1512 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1513 if (buffer == NULL)
1514 return -ENOMEM;
1515 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001516 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1517 retval = -EFAULT;
1518 goto out;
1519 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001520
1521 buffer[nbytes] = 0; /* nul-terminate */
1522 strstrip(buffer);
1523 retval = cft->write_string(cgrp, cft, buffer);
1524 if (!retval)
1525 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001526out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001527 if (buffer != local_buffer)
1528 kfree(buffer);
1529 return retval;
1530}
1531
Paul Menageddbcc7e2007-10-18 23:39:30 -07001532static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1533 size_t nbytes, loff_t *ppos)
1534{
1535 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001536 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537
Li Zefan75139b82009-01-07 18:07:33 -08001538 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001540 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001541 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001542 if (cft->write_u64 || cft->write_s64)
1543 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001544 if (cft->write_string)
1545 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001546 if (cft->trigger) {
1547 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1548 return ret ? ret : nbytes;
1549 }
Paul Menage355e0c42007-10-18 23:39:33 -07001550 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551}
1552
Paul Menagef4c753b2008-04-29 00:59:56 -07001553static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1554 struct file *file,
1555 char __user *buf, size_t nbytes,
1556 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557{
Paul Menage84eea842008-07-25 01:47:00 -07001558 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001559 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1561
1562 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1563}
1564
Paul Menagee73d2c62008-04-29 01:00:06 -07001565static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1566 struct file *file,
1567 char __user *buf, size_t nbytes,
1568 loff_t *ppos)
1569{
Paul Menage84eea842008-07-25 01:47:00 -07001570 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001571 s64 val = cft->read_s64(cgrp, cft);
1572 int len = sprintf(tmp, "%lld\n", (long long) val);
1573
1574 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1575}
1576
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1578 size_t nbytes, loff_t *ppos)
1579{
1580 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001581 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582
Li Zefan75139b82009-01-07 18:07:33 -08001583 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584 return -ENODEV;
1585
1586 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001587 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001588 if (cft->read_u64)
1589 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001590 if (cft->read_s64)
1591 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592 return -EINVAL;
1593}
1594
Paul Menage91796562008-04-29 01:00:01 -07001595/*
1596 * seqfile ops/methods for returning structured data. Currently just
1597 * supports string->u64 maps, but can be extended in future.
1598 */
1599
1600struct cgroup_seqfile_state {
1601 struct cftype *cft;
1602 struct cgroup *cgroup;
1603};
1604
1605static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1606{
1607 struct seq_file *sf = cb->state;
1608 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1609}
1610
1611static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1612{
1613 struct cgroup_seqfile_state *state = m->private;
1614 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001615 if (cft->read_map) {
1616 struct cgroup_map_cb cb = {
1617 .fill = cgroup_map_add,
1618 .state = m,
1619 };
1620 return cft->read_map(state->cgroup, cft, &cb);
1621 }
1622 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001623}
1624
Adrian Bunk96930a62008-07-25 19:46:21 -07001625static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07001626{
1627 struct seq_file *seq = file->private_data;
1628 kfree(seq->private);
1629 return single_release(inode, file);
1630}
1631
1632static struct file_operations cgroup_seqfile_operations = {
1633 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07001634 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07001635 .llseek = seq_lseek,
1636 .release = cgroup_seqfile_release,
1637};
1638
Paul Menageddbcc7e2007-10-18 23:39:30 -07001639static int cgroup_file_open(struct inode *inode, struct file *file)
1640{
1641 int err;
1642 struct cftype *cft;
1643
1644 err = generic_file_open(inode, file);
1645 if (err)
1646 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08001648
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001649 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001650 struct cgroup_seqfile_state *state =
1651 kzalloc(sizeof(*state), GFP_USER);
1652 if (!state)
1653 return -ENOMEM;
1654 state->cft = cft;
1655 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1656 file->f_op = &cgroup_seqfile_operations;
1657 err = single_open(file, cgroup_seqfile_show, state);
1658 if (err < 0)
1659 kfree(state);
1660 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001661 err = cft->open(inode, file);
1662 else
1663 err = 0;
1664
1665 return err;
1666}
1667
1668static int cgroup_file_release(struct inode *inode, struct file *file)
1669{
1670 struct cftype *cft = __d_cft(file->f_dentry);
1671 if (cft->release)
1672 return cft->release(inode, file);
1673 return 0;
1674}
1675
1676/*
1677 * cgroup_rename - Only allow simple rename of directories in place.
1678 */
1679static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1680 struct inode *new_dir, struct dentry *new_dentry)
1681{
1682 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1683 return -ENOTDIR;
1684 if (new_dentry->d_inode)
1685 return -EEXIST;
1686 if (old_dir != new_dir)
1687 return -EIO;
1688 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1689}
1690
1691static struct file_operations cgroup_file_operations = {
1692 .read = cgroup_file_read,
1693 .write = cgroup_file_write,
1694 .llseek = generic_file_llseek,
1695 .open = cgroup_file_open,
1696 .release = cgroup_file_release,
1697};
1698
1699static struct inode_operations cgroup_dir_inode_operations = {
1700 .lookup = simple_lookup,
1701 .mkdir = cgroup_mkdir,
1702 .rmdir = cgroup_rmdir,
1703 .rename = cgroup_rename,
1704};
1705
Li Zefan099fca32009-04-02 16:57:29 -07001706static int cgroup_create_file(struct dentry *dentry, mode_t mode,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001707 struct super_block *sb)
1708{
Al Viro3ba13d12009-02-20 06:02:22 +00001709 static const struct dentry_operations cgroup_dops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710 .d_iput = cgroup_diput,
1711 };
1712
1713 struct inode *inode;
1714
1715 if (!dentry)
1716 return -ENOENT;
1717 if (dentry->d_inode)
1718 return -EEXIST;
1719
1720 inode = cgroup_new_inode(mode, sb);
1721 if (!inode)
1722 return -ENOMEM;
1723
1724 if (S_ISDIR(mode)) {
1725 inode->i_op = &cgroup_dir_inode_operations;
1726 inode->i_fop = &simple_dir_operations;
1727
1728 /* start off with i_nlink == 2 (for "." entry) */
1729 inc_nlink(inode);
1730
1731 /* start with the directory inode held, so that we can
1732 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07001733 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001734 } else if (S_ISREG(mode)) {
1735 inode->i_size = 0;
1736 inode->i_fop = &cgroup_file_operations;
1737 }
1738 dentry->d_op = &cgroup_dops;
1739 d_instantiate(dentry, inode);
1740 dget(dentry); /* Extra count - pin the dentry in core */
1741 return 0;
1742}
1743
1744/*
Li Zefana043e3b2008-02-23 15:24:09 -08001745 * cgroup_create_dir - create a directory for an object.
1746 * @cgrp: the cgroup we create the directory for. It must have a valid
1747 * ->parent field. And we are going to fill its ->dentry field.
1748 * @dentry: dentry of the new cgroup
1749 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001750 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001751static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07001752 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753{
1754 struct dentry *parent;
1755 int error = 0;
1756
Paul Menagebd89aab2007-10-18 23:40:44 -07001757 parent = cgrp->parent->dentry;
1758 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001760 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08001762 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001763 dget(dentry);
1764 }
1765 dput(dentry);
1766
1767 return error;
1768}
1769
Li Zefan099fca32009-04-02 16:57:29 -07001770/**
1771 * cgroup_file_mode - deduce file mode of a control file
1772 * @cft: the control file in question
1773 *
1774 * returns cft->mode if ->mode is not 0
1775 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1776 * returns S_IRUGO if it has only a read handler
1777 * returns S_IWUSR if it has only a write hander
1778 */
1779static mode_t cgroup_file_mode(const struct cftype *cft)
1780{
1781 mode_t mode = 0;
1782
1783 if (cft->mode)
1784 return cft->mode;
1785
1786 if (cft->read || cft->read_u64 || cft->read_s64 ||
1787 cft->read_map || cft->read_seq_string)
1788 mode |= S_IRUGO;
1789
1790 if (cft->write || cft->write_u64 || cft->write_s64 ||
1791 cft->write_string || cft->trigger)
1792 mode |= S_IWUSR;
1793
1794 return mode;
1795}
1796
Paul Menagebd89aab2007-10-18 23:40:44 -07001797int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001798 struct cgroup_subsys *subsys,
1799 const struct cftype *cft)
1800{
Paul Menagebd89aab2007-10-18 23:40:44 -07001801 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001802 struct dentry *dentry;
1803 int error;
Li Zefan099fca32009-04-02 16:57:29 -07001804 mode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001805
1806 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07001807 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001808 strcpy(name, subsys->name);
1809 strcat(name, ".");
1810 }
1811 strcat(name, cft->name);
1812 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1813 dentry = lookup_one_len(name, dir, strlen(name));
1814 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07001815 mode = cgroup_file_mode(cft);
1816 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07001817 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001818 if (!error)
1819 dentry->d_fsdata = (void *)cft;
1820 dput(dentry);
1821 } else
1822 error = PTR_ERR(dentry);
1823 return error;
1824}
1825
Paul Menagebd89aab2007-10-18 23:40:44 -07001826int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001827 struct cgroup_subsys *subsys,
1828 const struct cftype cft[],
1829 int count)
1830{
1831 int i, err;
1832 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001833 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001834 if (err)
1835 return err;
1836 }
1837 return 0;
1838}
1839
Li Zefana043e3b2008-02-23 15:24:09 -08001840/**
1841 * cgroup_task_count - count the number of tasks in a cgroup.
1842 * @cgrp: the cgroup in question
1843 *
1844 * Return the number of tasks in the cgroup.
1845 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001846int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001847{
1848 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001849 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001850
Paul Menage817929e2007-10-18 23:39:36 -07001851 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001852 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07001853 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07001854 }
1855 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001856 return count;
1857}
1858
1859/*
Paul Menage817929e2007-10-18 23:39:36 -07001860 * Advance a list_head iterator. The iterator should be positioned at
1861 * the start of a css_set
1862 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001863static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001864 struct cgroup_iter *it)
1865{
1866 struct list_head *l = it->cg_link;
1867 struct cg_cgroup_link *link;
1868 struct css_set *cg;
1869
1870 /* Advance to the next non-empty css_set */
1871 do {
1872 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07001873 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001874 it->cg_link = NULL;
1875 return;
1876 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001877 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001878 cg = link->cg;
1879 } while (list_empty(&cg->tasks));
1880 it->cg_link = l;
1881 it->task = cg->tasks.next;
1882}
1883
Cliff Wickman31a7df02008-02-07 00:14:42 -08001884/*
1885 * To reduce the fork() overhead for systems that are not actually
1886 * using their cgroups capability, we don't maintain the lists running
1887 * through each css_set to its tasks until we see the list actually
1888 * used - in other words after the first call to cgroup_iter_start().
1889 *
1890 * The tasklist_lock is not held here, as do_each_thread() and
1891 * while_each_thread() are protected by RCU.
1892 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07001893static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08001894{
1895 struct task_struct *p, *g;
1896 write_lock(&css_set_lock);
1897 use_task_css_set_links = 1;
1898 do_each_thread(g, p) {
1899 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08001900 /*
1901 * We should check if the process is exiting, otherwise
1902 * it will race with cgroup_exit() in that the list
1903 * entry won't be deleted though the process has exited.
1904 */
1905 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08001906 list_add(&p->cg_list, &p->cgroups->tasks);
1907 task_unlock(p);
1908 } while_each_thread(g, p);
1909 write_unlock(&css_set_lock);
1910}
1911
Paul Menagebd89aab2007-10-18 23:40:44 -07001912void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001913{
1914 /*
1915 * The first time anyone tries to iterate across a cgroup,
1916 * we need to enable the list linking each css_set to its
1917 * tasks, and fix up all existing tasks.
1918 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08001919 if (!use_task_css_set_links)
1920 cgroup_enable_task_cg_lists();
1921
Paul Menage817929e2007-10-18 23:39:36 -07001922 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001923 it->cg_link = &cgrp->css_sets;
1924 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001925}
1926
Paul Menagebd89aab2007-10-18 23:40:44 -07001927struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001928 struct cgroup_iter *it)
1929{
1930 struct task_struct *res;
1931 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001932 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07001933
1934 /* If the iterator cg is NULL, we have no tasks */
1935 if (!it->cg_link)
1936 return NULL;
1937 res = list_entry(l, struct task_struct, cg_list);
1938 /* Advance iterator to find next entry */
1939 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001940 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
1941 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07001942 /* We reached the end of this task list - move on to
1943 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07001944 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001945 } else {
1946 it->task = l;
1947 }
1948 return res;
1949}
1950
Paul Menagebd89aab2007-10-18 23:40:44 -07001951void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001952{
1953 read_unlock(&css_set_lock);
1954}
1955
Cliff Wickman31a7df02008-02-07 00:14:42 -08001956static inline int started_after_time(struct task_struct *t1,
1957 struct timespec *time,
1958 struct task_struct *t2)
1959{
1960 int start_diff = timespec_compare(&t1->start_time, time);
1961 if (start_diff > 0) {
1962 return 1;
1963 } else if (start_diff < 0) {
1964 return 0;
1965 } else {
1966 /*
1967 * Arbitrarily, if two processes started at the same
1968 * time, we'll say that the lower pointer value
1969 * started first. Note that t2 may have exited by now
1970 * so this may not be a valid pointer any longer, but
1971 * that's fine - it still serves to distinguish
1972 * between two tasks started (effectively) simultaneously.
1973 */
1974 return t1 > t2;
1975 }
1976}
1977
1978/*
1979 * This function is a callback from heap_insert() and is used to order
1980 * the heap.
1981 * In this case we order the heap in descending task start time.
1982 */
1983static inline int started_after(void *p1, void *p2)
1984{
1985 struct task_struct *t1 = p1;
1986 struct task_struct *t2 = p2;
1987 return started_after_time(t1, &t2->start_time, t2);
1988}
1989
1990/**
1991 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1992 * @scan: struct cgroup_scanner containing arguments for the scan
1993 *
1994 * Arguments include pointers to callback functions test_task() and
1995 * process_task().
1996 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1997 * and if it returns true, call process_task() for it also.
1998 * The test_task pointer may be NULL, meaning always true (select all tasks).
1999 * Effectively duplicates cgroup_iter_{start,next,end}()
2000 * but does not lock css_set_lock for the call to process_task().
2001 * The struct cgroup_scanner may be embedded in any structure of the caller's
2002 * creation.
2003 * It is guaranteed that process_task() will act on every task that
2004 * is a member of the cgroup for the duration of this call. This
2005 * function may or may not call process_task() for tasks that exit
2006 * or move to a different cgroup during the call, or are forked or
2007 * move into the cgroup during the call.
2008 *
2009 * Note that test_task() may be called with locks held, and may in some
2010 * situations be called multiple times for the same task, so it should
2011 * be cheap.
2012 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2013 * pre-allocated and will be used for heap operations (and its "gt" member will
2014 * be overwritten), else a temporary heap will be used (allocation of which
2015 * may cause this function to fail).
2016 */
2017int cgroup_scan_tasks(struct cgroup_scanner *scan)
2018{
2019 int retval, i;
2020 struct cgroup_iter it;
2021 struct task_struct *p, *dropped;
2022 /* Never dereference latest_task, since it's not refcounted */
2023 struct task_struct *latest_task = NULL;
2024 struct ptr_heap tmp_heap;
2025 struct ptr_heap *heap;
2026 struct timespec latest_time = { 0, 0 };
2027
2028 if (scan->heap) {
2029 /* The caller supplied our heap and pre-allocated its memory */
2030 heap = scan->heap;
2031 heap->gt = &started_after;
2032 } else {
2033 /* We need to allocate our own heap memory */
2034 heap = &tmp_heap;
2035 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2036 if (retval)
2037 /* cannot allocate the heap */
2038 return retval;
2039 }
2040
2041 again:
2042 /*
2043 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2044 * to determine which are of interest, and using the scanner's
2045 * "process_task" callback to process any of them that need an update.
2046 * Since we don't want to hold any locks during the task updates,
2047 * gather tasks to be processed in a heap structure.
2048 * The heap is sorted by descending task start time.
2049 * If the statically-sized heap fills up, we overflow tasks that
2050 * started later, and in future iterations only consider tasks that
2051 * started after the latest task in the previous pass. This
2052 * guarantees forward progress and that we don't miss any tasks.
2053 */
2054 heap->size = 0;
2055 cgroup_iter_start(scan->cg, &it);
2056 while ((p = cgroup_iter_next(scan->cg, &it))) {
2057 /*
2058 * Only affect tasks that qualify per the caller's callback,
2059 * if he provided one
2060 */
2061 if (scan->test_task && !scan->test_task(p, scan))
2062 continue;
2063 /*
2064 * Only process tasks that started after the last task
2065 * we processed
2066 */
2067 if (!started_after_time(p, &latest_time, latest_task))
2068 continue;
2069 dropped = heap_insert(heap, p);
2070 if (dropped == NULL) {
2071 /*
2072 * The new task was inserted; the heap wasn't
2073 * previously full
2074 */
2075 get_task_struct(p);
2076 } else if (dropped != p) {
2077 /*
2078 * The new task was inserted, and pushed out a
2079 * different task
2080 */
2081 get_task_struct(p);
2082 put_task_struct(dropped);
2083 }
2084 /*
2085 * Else the new task was newer than anything already in
2086 * the heap and wasn't inserted
2087 */
2088 }
2089 cgroup_iter_end(scan->cg, &it);
2090
2091 if (heap->size) {
2092 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002093 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002094 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002095 latest_time = q->start_time;
2096 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002097 }
2098 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002099 scan->process_task(q, scan);
2100 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002101 }
2102 /*
2103 * If we had to process any tasks at all, scan again
2104 * in case some of them were in the middle of forking
2105 * children that didn't get processed.
2106 * Not the most efficient way to do it, but it avoids
2107 * having to take callback_mutex in the fork path
2108 */
2109 goto again;
2110 }
2111 if (heap == &tmp_heap)
2112 heap_free(&tmp_heap);
2113 return 0;
2114}
2115
Paul Menage817929e2007-10-18 23:39:36 -07002116/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07002117 * Stuff for reading the 'tasks' file.
2118 *
2119 * Reading this file can return large amounts of data if a cgroup has
2120 * *lots* of attached tasks. So it may need several calls to read(),
2121 * but we cannot guarantee that the information we produce is correct
2122 * unless we produce it entirely atomically.
2123 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002124 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002125
2126/*
2127 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
Paul Menagebd89aab2007-10-18 23:40:44 -07002128 * 'cgrp'. Return actual number of pids loaded. No need to
Paul Menagebbcb81d2007-10-18 23:39:32 -07002129 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2130 * read section, so the css_set can't go away, and is
2131 * immutable after creation.
2132 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002133static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002134{
Gowrishankar Me7b80bb2009-01-07 18:07:43 -08002135 int n = 0, pid;
Paul Menage817929e2007-10-18 23:39:36 -07002136 struct cgroup_iter it;
2137 struct task_struct *tsk;
Paul Menagebd89aab2007-10-18 23:40:44 -07002138 cgroup_iter_start(cgrp, &it);
2139 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Paul Menage817929e2007-10-18 23:39:36 -07002140 if (unlikely(n == npids))
2141 break;
Gowrishankar Me7b80bb2009-01-07 18:07:43 -08002142 pid = task_pid_vnr(tsk);
2143 if (pid > 0)
2144 pidarray[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07002145 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002146 cgroup_iter_end(cgrp, &it);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002147 return n;
2148}
2149
Balbir Singh846c7bb2007-10-18 23:39:44 -07002150/**
Li Zefana043e3b2008-02-23 15:24:09 -08002151 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002152 * @stats: cgroupstats to fill information into
2153 * @dentry: A dentry entry belonging to the cgroup for which stats have
2154 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002155 *
2156 * Build and fill cgroupstats so that taskstats can export it to user
2157 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002158 */
2159int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2160{
2161 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002162 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002163 struct cgroup_iter it;
2164 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002165
Balbir Singh846c7bb2007-10-18 23:39:44 -07002166 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002167 * Validate dentry by checking the superblock operations,
2168 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002169 */
Li Zefan33d283b2008-11-19 15:36:48 -08002170 if (dentry->d_sb->s_op != &cgroup_ops ||
2171 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002172 goto err;
2173
2174 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002175 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002176
Paul Menagebd89aab2007-10-18 23:40:44 -07002177 cgroup_iter_start(cgrp, &it);
2178 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002179 switch (tsk->state) {
2180 case TASK_RUNNING:
2181 stats->nr_running++;
2182 break;
2183 case TASK_INTERRUPTIBLE:
2184 stats->nr_sleeping++;
2185 break;
2186 case TASK_UNINTERRUPTIBLE:
2187 stats->nr_uninterruptible++;
2188 break;
2189 case TASK_STOPPED:
2190 stats->nr_stopped++;
2191 break;
2192 default:
2193 if (delayacct_is_task_waiting_on_io(tsk))
2194 stats->nr_io_wait++;
2195 break;
2196 }
2197 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002198 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002199
Balbir Singh846c7bb2007-10-18 23:39:44 -07002200err:
2201 return ret;
2202}
2203
Paul Menagebbcb81d2007-10-18 23:39:32 -07002204static int cmppid(const void *a, const void *b)
2205{
2206 return *(pid_t *)a - *(pid_t *)b;
2207}
2208
Paul Menagebbcb81d2007-10-18 23:39:32 -07002209
Paul Menagecc31edc2008-10-18 20:28:04 -07002210/*
2211 * seq_file methods for the "tasks" file. The seq_file position is the
2212 * next pid to display; the seq_file iterator is a pointer to the pid
2213 * in the cgroup->tasks_pids array.
2214 */
2215
2216static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
2217{
2218 /*
2219 * Initially we receive a position value that corresponds to
2220 * one more than the last pid shown (or 0 on the first call or
2221 * after a seek to the start). Use a binary-search to find the
2222 * next pid to display, if any
2223 */
2224 struct cgroup *cgrp = s->private;
2225 int index = 0, pid = *pos;
2226 int *iter;
2227
2228 down_read(&cgrp->pids_mutex);
2229 if (pid) {
2230 int end = cgrp->pids_length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002231
Paul Menagecc31edc2008-10-18 20:28:04 -07002232 while (index < end) {
2233 int mid = (index + end) / 2;
2234 if (cgrp->tasks_pids[mid] == pid) {
2235 index = mid;
2236 break;
2237 } else if (cgrp->tasks_pids[mid] <= pid)
2238 index = mid + 1;
2239 else
2240 end = mid;
2241 }
2242 }
2243 /* If we're off the end of the array, we're done */
2244 if (index >= cgrp->pids_length)
2245 return NULL;
2246 /* Update the abstract position to be the actual pid that we found */
2247 iter = cgrp->tasks_pids + index;
2248 *pos = *iter;
2249 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002250}
2251
Paul Menagecc31edc2008-10-18 20:28:04 -07002252static void cgroup_tasks_stop(struct seq_file *s, void *v)
2253{
2254 struct cgroup *cgrp = s->private;
2255 up_read(&cgrp->pids_mutex);
2256}
2257
2258static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
2259{
2260 struct cgroup *cgrp = s->private;
2261 int *p = v;
2262 int *end = cgrp->tasks_pids + cgrp->pids_length;
2263
2264 /*
2265 * Advance to the next pid in the array. If this goes off the
2266 * end, we're done
2267 */
2268 p++;
2269 if (p >= end) {
2270 return NULL;
2271 } else {
2272 *pos = *p;
2273 return p;
2274 }
2275}
2276
2277static int cgroup_tasks_show(struct seq_file *s, void *v)
2278{
2279 return seq_printf(s, "%d\n", *(int *)v);
2280}
2281
2282static struct seq_operations cgroup_tasks_seq_operations = {
2283 .start = cgroup_tasks_start,
2284 .stop = cgroup_tasks_stop,
2285 .next = cgroup_tasks_next,
2286 .show = cgroup_tasks_show,
2287};
2288
2289static void release_cgroup_pid_array(struct cgroup *cgrp)
2290{
2291 down_write(&cgrp->pids_mutex);
2292 BUG_ON(!cgrp->pids_use_count);
2293 if (!--cgrp->pids_use_count) {
2294 kfree(cgrp->tasks_pids);
2295 cgrp->tasks_pids = NULL;
2296 cgrp->pids_length = 0;
2297 }
2298 up_write(&cgrp->pids_mutex);
2299}
2300
2301static int cgroup_tasks_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002302{
Paul Menagebd89aab2007-10-18 23:40:44 -07002303 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002304
2305 if (!(file->f_mode & FMODE_READ))
2306 return 0;
2307
Paul Menagecc31edc2008-10-18 20:28:04 -07002308 release_cgroup_pid_array(cgrp);
2309 return seq_release(inode, file);
2310}
2311
2312static struct file_operations cgroup_tasks_operations = {
2313 .read = seq_read,
2314 .llseek = seq_lseek,
2315 .write = cgroup_file_write,
2316 .release = cgroup_tasks_release,
2317};
2318
2319/*
2320 * Handle an open on 'tasks' file. Prepare an array containing the
2321 * process id's of tasks currently attached to the cgroup being opened.
2322 */
2323
2324static int cgroup_tasks_open(struct inode *unused, struct file *file)
2325{
2326 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2327 pid_t *pidarray;
2328 int npids;
2329 int retval;
2330
2331 /* Nothing to do for write-only files */
2332 if (!(file->f_mode & FMODE_READ))
2333 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002334
2335 /*
2336 * If cgroup gets more users after we read count, we won't have
2337 * enough space - tough. This race is indistinguishable to the
2338 * caller from the case that the additional cgroup users didn't
2339 * show up until sometime later on.
2340 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002341 npids = cgroup_task_count(cgrp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002342 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2343 if (!pidarray)
2344 return -ENOMEM;
2345 npids = pid_array_load(pidarray, npids, cgrp);
2346 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002347
Paul Menagecc31edc2008-10-18 20:28:04 -07002348 /*
2349 * Store the array in the cgroup, freeing the old
2350 * array if necessary
2351 */
2352 down_write(&cgrp->pids_mutex);
2353 kfree(cgrp->tasks_pids);
2354 cgrp->tasks_pids = pidarray;
2355 cgrp->pids_length = npids;
2356 cgrp->pids_use_count++;
2357 up_write(&cgrp->pids_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002358
Paul Menagecc31edc2008-10-18 20:28:04 -07002359 file->f_op = &cgroup_tasks_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002360
Paul Menagecc31edc2008-10-18 20:28:04 -07002361 retval = seq_open(file, &cgroup_tasks_seq_operations);
2362 if (retval) {
2363 release_cgroup_pid_array(cgrp);
2364 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002365 }
Paul Menagecc31edc2008-10-18 20:28:04 -07002366 ((struct seq_file *)file->private_data)->private = cgrp;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002367 return 0;
2368}
2369
Paul Menagebd89aab2007-10-18 23:40:44 -07002370static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002371 struct cftype *cft)
2372{
Paul Menagebd89aab2007-10-18 23:40:44 -07002373 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002374}
2375
Paul Menage6379c102008-07-25 01:47:01 -07002376static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2377 struct cftype *cft,
2378 u64 val)
2379{
2380 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2381 if (val)
2382 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2383 else
2384 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2385 return 0;
2386}
2387
Paul Menagebbcb81d2007-10-18 23:39:32 -07002388/*
2389 * for the common functions, 'private' gives the type of file
2390 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07002391static struct cftype files[] = {
2392 {
2393 .name = "tasks",
2394 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07002395 .write_u64 = cgroup_tasks_write,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002396 .release = cgroup_tasks_release,
2397 .private = FILE_TASKLIST,
Li Zefan099fca32009-04-02 16:57:29 -07002398 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002399 },
2400
2401 {
2402 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002403 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07002404 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002405 .private = FILE_NOTIFY_ON_RELEASE,
2406 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002407};
2408
2409static struct cftype cft_release_agent = {
2410 .name = "release_agent",
Paul Menagee788e062008-07-25 01:46:59 -07002411 .read_seq_string = cgroup_release_agent_show,
2412 .write_string = cgroup_release_agent_write,
2413 .max_write_len = PATH_MAX,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002414 .private = FILE_RELEASE_AGENT,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002415};
2416
Paul Menagebd89aab2007-10-18 23:40:44 -07002417static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002418{
2419 int err;
2420 struct cgroup_subsys *ss;
2421
2422 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002423 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002424
Paul Menagebd89aab2007-10-18 23:40:44 -07002425 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002426 if (err < 0)
2427 return err;
2428
Paul Menagebd89aab2007-10-18 23:40:44 -07002429 if (cgrp == cgrp->top_cgroup) {
2430 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002431 return err;
2432 }
2433
Paul Menagebd89aab2007-10-18 23:40:44 -07002434 for_each_subsys(cgrp->root, ss) {
2435 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002436 return err;
2437 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002438 /* This cgroup is ready now */
2439 for_each_subsys(cgrp->root, ss) {
2440 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2441 /*
2442 * Update id->css pointer and make this css visible from
2443 * CSS ID functions. This pointer will be dereferened
2444 * from RCU-read-side without locks.
2445 */
2446 if (css->id)
2447 rcu_assign_pointer(css->id->css, css);
2448 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002449
2450 return 0;
2451}
2452
2453static void init_cgroup_css(struct cgroup_subsys_state *css,
2454 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002455 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002456{
Paul Menagebd89aab2007-10-18 23:40:44 -07002457 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08002458 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002459 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002460 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002461 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002462 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002463 BUG_ON(cgrp->subsys[ss->subsys_id]);
2464 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002465}
2466
Paul Menage999cd8a2009-01-07 18:08:36 -08002467static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2468{
2469 /* We need to take each hierarchy_mutex in a consistent order */
2470 int i;
2471
2472 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2473 struct cgroup_subsys *ss = subsys[i];
2474 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08002475 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08002476 }
2477}
2478
2479static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2480{
2481 int i;
2482
2483 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2484 struct cgroup_subsys *ss = subsys[i];
2485 if (ss->root == root)
2486 mutex_unlock(&ss->hierarchy_mutex);
2487 }
2488}
2489
Paul Menageddbcc7e2007-10-18 23:39:30 -07002490/*
Li Zefana043e3b2008-02-23 15:24:09 -08002491 * cgroup_create - create a cgroup
2492 * @parent: cgroup that will be parent of the new cgroup
2493 * @dentry: dentry of the new cgroup
2494 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002495 *
Li Zefana043e3b2008-02-23 15:24:09 -08002496 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002497 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002498static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002499 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002500{
Paul Menagebd89aab2007-10-18 23:40:44 -07002501 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002502 struct cgroupfs_root *root = parent->root;
2503 int err = 0;
2504 struct cgroup_subsys *ss;
2505 struct super_block *sb = root->sb;
2506
Paul Menagebd89aab2007-10-18 23:40:44 -07002507 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2508 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002509 return -ENOMEM;
2510
2511 /* Grab a reference on the superblock so the hierarchy doesn't
2512 * get deleted on unmount if there are child cgroups. This
2513 * can be done outside cgroup_mutex, since the sb can't
2514 * disappear while someone has an open control file on the
2515 * fs */
2516 atomic_inc(&sb->s_active);
2517
2518 mutex_lock(&cgroup_mutex);
2519
Paul Menagecc31edc2008-10-18 20:28:04 -07002520 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002521
Paul Menagebd89aab2007-10-18 23:40:44 -07002522 cgrp->parent = parent;
2523 cgrp->root = parent->root;
2524 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002525
Li Zefanb6abdb02008-03-04 14:28:19 -08002526 if (notify_on_release(parent))
2527 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2528
Paul Menageddbcc7e2007-10-18 23:39:30 -07002529 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002530 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002531 if (IS_ERR(css)) {
2532 err = PTR_ERR(css);
2533 goto err_destroy;
2534 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002535 init_cgroup_css(css, ss, cgrp);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002536 if (ss->use_id)
2537 if (alloc_css_id(ss, parent, cgrp))
2538 goto err_destroy;
2539 /* At error, ->destroy() callback has to free assigned ID. */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002540 }
2541
Paul Menage999cd8a2009-01-07 18:08:36 -08002542 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002543 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08002544 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002545 root->number_of_cgroups++;
2546
Paul Menagebd89aab2007-10-18 23:40:44 -07002547 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002548 if (err < 0)
2549 goto err_remove;
2550
2551 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07002552 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002553
Paul Menagebd89aab2007-10-18 23:40:44 -07002554 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002555 /* If err < 0, we have a half-filled directory - oh well ;) */
2556
2557 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002558 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002559
2560 return 0;
2561
2562 err_remove:
2563
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002564 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002565 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002566 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002567 root->number_of_cgroups--;
2568
2569 err_destroy:
2570
2571 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002572 if (cgrp->subsys[ss->subsys_id])
2573 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002574 }
2575
2576 mutex_unlock(&cgroup_mutex);
2577
2578 /* Release the reference count that we took on the superblock */
2579 deactivate_super(sb);
2580
Paul Menagebd89aab2007-10-18 23:40:44 -07002581 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002582 return err;
2583}
2584
2585static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2586{
2587 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2588
2589 /* the vfs holds inode->i_mutex already */
2590 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2591}
2592
Li Zefan55b6fd02008-07-29 22:33:20 -07002593static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002594{
2595 /* Check the reference count on each subsystem. Since we
2596 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08002597 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07002598 * be no outstanding references, so the subsystem is safe to
2599 * destroy. We scan across all subsystems rather than using
2600 * the per-hierarchy linked list of mounted subsystems since
2601 * we can be called via check_for_release() with no
2602 * synchronization other than RCU, and the subsystem linked
2603 * list isn't RCU-safe */
2604 int i;
2605 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2606 struct cgroup_subsys *ss = subsys[i];
2607 struct cgroup_subsys_state *css;
2608 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07002609 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002610 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07002611 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07002612 /* When called from check_for_release() it's possible
2613 * that by this point the cgroup has been removed
2614 * and the css deleted. But a false-positive doesn't
2615 * matter, since it can only happen if the cgroup
2616 * has been deleted and hence no longer needs the
2617 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08002618 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07002619 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07002620 }
2621 return 0;
2622}
2623
Paul Menagee7c5ec92009-01-07 18:08:38 -08002624/*
2625 * Atomically mark all (or else none) of the cgroup's CSS objects as
2626 * CSS_REMOVED. Return true on success, or false if the cgroup has
2627 * busy subsystems. Call with cgroup_mutex held
2628 */
2629
2630static int cgroup_clear_css_refs(struct cgroup *cgrp)
2631{
2632 struct cgroup_subsys *ss;
2633 unsigned long flags;
2634 bool failed = false;
2635 local_irq_save(flags);
2636 for_each_subsys(cgrp->root, ss) {
2637 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2638 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08002639 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08002640 /* We can only remove a CSS with a refcnt==1 */
2641 refcnt = atomic_read(&css->refcnt);
2642 if (refcnt > 1) {
2643 failed = true;
2644 goto done;
2645 }
2646 BUG_ON(!refcnt);
2647 /*
2648 * Drop the refcnt to 0 while we check other
2649 * subsystems. This will cause any racing
2650 * css_tryget() to spin until we set the
2651 * CSS_REMOVED bits or abort
2652 */
Paul Menage804b3c22009-01-29 14:25:21 -08002653 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
2654 break;
2655 cpu_relax();
2656 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08002657 }
2658 done:
2659 for_each_subsys(cgrp->root, ss) {
2660 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2661 if (failed) {
2662 /*
2663 * Restore old refcnt if we previously managed
2664 * to clear it from 1 to 0
2665 */
2666 if (!atomic_read(&css->refcnt))
2667 atomic_set(&css->refcnt, 1);
2668 } else {
2669 /* Commit the fact that the CSS is removed */
2670 set_bit(CSS_REMOVED, &css->flags);
2671 }
2672 }
2673 local_irq_restore(flags);
2674 return !failed;
2675}
2676
Paul Menageddbcc7e2007-10-18 23:39:30 -07002677static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2678{
Paul Menagebd89aab2007-10-18 23:40:44 -07002679 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002680 struct dentry *d;
2681 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002682 DEFINE_WAIT(wait);
2683 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002684
2685 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002686again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07002687 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002688 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002689 mutex_unlock(&cgroup_mutex);
2690 return -EBUSY;
2691 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002692 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002693 mutex_unlock(&cgroup_mutex);
2694 return -EBUSY;
2695 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002696 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08002697
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002698 /*
Li Zefana043e3b2008-02-23 15:24:09 -08002699 * Call pre_destroy handlers of subsys. Notify subsystems
2700 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002701 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002702 ret = cgroup_call_pre_destroy(cgrp);
2703 if (ret)
2704 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002705
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002706 mutex_lock(&cgroup_mutex);
2707 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002708 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002709 mutex_unlock(&cgroup_mutex);
2710 return -EBUSY;
2711 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002712 /*
2713 * css_put/get is provided for subsys to grab refcnt to css. In typical
2714 * case, subsystem has no reference after pre_destroy(). But, under
2715 * hierarchy management, some *temporal* refcnt can be hold.
2716 * To avoid returning -EBUSY to a user, waitqueue is used. If subsys
2717 * is really busy, it should return -EBUSY at pre_destroy(). wake_up
2718 * is called when css_put() is called and refcnt goes down to 0.
2719 */
2720 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2721 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
2722
2723 if (!cgroup_clear_css_refs(cgrp)) {
2724 mutex_unlock(&cgroup_mutex);
2725 schedule();
2726 finish_wait(&cgroup_rmdir_waitq, &wait);
2727 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2728 if (signal_pending(current))
2729 return -EINTR;
2730 goto again;
2731 }
2732 /* NO css_tryget() can success after here. */
2733 finish_wait(&cgroup_rmdir_waitq, &wait);
2734 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002735
Paul Menage81a6a5c2007-10-18 23:39:38 -07002736 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002737 set_bit(CGRP_REMOVED, &cgrp->flags);
2738 if (!list_empty(&cgrp->release_list))
2739 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002740 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08002741
2742 cgroup_lock_hierarchy(cgrp->root);
2743 /* delete this cgroup from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07002744 list_del(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08002745 cgroup_unlock_hierarchy(cgrp->root);
2746
Paul Menagebd89aab2007-10-18 23:40:44 -07002747 spin_lock(&cgrp->dentry->d_lock);
2748 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002749 spin_unlock(&d->d_lock);
2750
2751 cgroup_d_remove_dir(d);
2752 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002753
Paul Menagebd89aab2007-10-18 23:40:44 -07002754 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002755 check_for_release(parent);
2756
Paul Menageddbcc7e2007-10-18 23:39:30 -07002757 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002758 return 0;
2759}
2760
Li Zefan06a11922008-04-29 01:00:07 -07002761static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002762{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002763 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08002764
2765 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002766
2767 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08002768 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002769 ss->root = &rootnode;
2770 css = ss->create(ss, dummytop);
2771 /* We don't handle early failures gracefully */
2772 BUG_ON(IS_ERR(css));
2773 init_cgroup_css(css, ss, dummytop);
2774
Li Zefane8d55fd2008-04-29 01:00:13 -07002775 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07002776 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07002777 * newly registered, all tasks and hence the
2778 * init_css_set is in the subsystem's top cgroup. */
2779 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07002780
2781 need_forkexit_callback |= ss->fork || ss->exit;
2782
Li Zefane8d55fd2008-04-29 01:00:13 -07002783 /* At system boot, before all subsystems have been
2784 * registered, no tasks have been forked, so we don't
2785 * need to invoke fork callbacks here. */
2786 BUG_ON(!list_empty(&init_task.tasks));
2787
Paul Menage999cd8a2009-01-07 18:08:36 -08002788 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08002789 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002790 ss->active = 1;
2791}
2792
2793/**
Li Zefana043e3b2008-02-23 15:24:09 -08002794 * cgroup_init_early - cgroup initialization at system boot
2795 *
2796 * Initialize cgroups at system boot, and initialize any
2797 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002798 */
2799int __init cgroup_init_early(void)
2800{
2801 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002802 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07002803 INIT_LIST_HEAD(&init_css_set.cg_links);
2804 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07002805 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07002806 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002807 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07002808 root_count = 1;
2809 init_task.cgroups = &init_css_set;
2810
2811 init_css_set_link.cg = &init_css_set;
Paul Menagebd89aab2007-10-18 23:40:44 -07002812 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07002813 &rootnode.top_cgroup.css_sets);
2814 list_add(&init_css_set_link.cg_link_list,
2815 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002816
Li Zefan472b1052008-04-29 01:00:11 -07002817 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2818 INIT_HLIST_HEAD(&css_set_table[i]);
2819
Paul Menageddbcc7e2007-10-18 23:39:30 -07002820 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2821 struct cgroup_subsys *ss = subsys[i];
2822
2823 BUG_ON(!ss->name);
2824 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2825 BUG_ON(!ss->create);
2826 BUG_ON(!ss->destroy);
2827 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08002828 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07002829 ss->name, ss->subsys_id);
2830 BUG();
2831 }
2832
2833 if (ss->early_init)
2834 cgroup_init_subsys(ss);
2835 }
2836 return 0;
2837}
2838
2839/**
Li Zefana043e3b2008-02-23 15:24:09 -08002840 * cgroup_init - cgroup initialization
2841 *
2842 * Register cgroup filesystem and /proc file, and initialize
2843 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002844 */
2845int __init cgroup_init(void)
2846{
2847 int err;
2848 int i;
Li Zefan472b1052008-04-29 01:00:11 -07002849 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07002850
2851 err = bdi_init(&cgroup_backing_dev_info);
2852 if (err)
2853 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002854
2855 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2856 struct cgroup_subsys *ss = subsys[i];
2857 if (!ss->early_init)
2858 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002859 if (ss->use_id)
2860 cgroup_subsys_init_idr(ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002861 }
2862
Li Zefan472b1052008-04-29 01:00:11 -07002863 /* Add init_css_set to the hash table */
2864 hhead = css_set_hash(init_css_set.subsys);
2865 hlist_add_head(&init_css_set.hlist, hhead);
2866
Paul Menageddbcc7e2007-10-18 23:39:30 -07002867 err = register_filesystem(&cgroup_fs_type);
2868 if (err < 0)
2869 goto out;
2870
Li Zefan46ae2202008-04-29 01:00:08 -07002871 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07002872
Paul Menageddbcc7e2007-10-18 23:39:30 -07002873out:
Paul Menagea4243162007-10-18 23:39:35 -07002874 if (err)
2875 bdi_destroy(&cgroup_backing_dev_info);
2876
Paul Menageddbcc7e2007-10-18 23:39:30 -07002877 return err;
2878}
Paul Menageb4f48b62007-10-18 23:39:33 -07002879
Paul Menagea4243162007-10-18 23:39:35 -07002880/*
2881 * proc_cgroup_show()
2882 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2883 * - Used for /proc/<pid>/cgroup.
2884 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2885 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002886 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07002887 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2888 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2889 * cgroup to top_cgroup.
2890 */
2891
2892/* TODO: Use a proper seq_file iterator */
2893static int proc_cgroup_show(struct seq_file *m, void *v)
2894{
2895 struct pid *pid;
2896 struct task_struct *tsk;
2897 char *buf;
2898 int retval;
2899 struct cgroupfs_root *root;
2900
2901 retval = -ENOMEM;
2902 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2903 if (!buf)
2904 goto out;
2905
2906 retval = -ESRCH;
2907 pid = m->private;
2908 tsk = get_pid_task(pid, PIDTYPE_PID);
2909 if (!tsk)
2910 goto out_free;
2911
2912 retval = 0;
2913
2914 mutex_lock(&cgroup_mutex);
2915
Li Zefane5f6a862009-01-07 18:07:41 -08002916 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07002917 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07002918 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07002919 int subsys_id;
2920 int count = 0;
2921
Paul Menageb6c30062008-04-10 21:29:16 -07002922 seq_printf(m, "%lu:", root->subsys_bits);
Paul Menagea4243162007-10-18 23:39:35 -07002923 for_each_subsys(root, ss)
2924 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2925 seq_putc(m, ':');
2926 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07002927 cgrp = task_cgroup(tsk, subsys_id);
2928 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07002929 if (retval < 0)
2930 goto out_unlock;
2931 seq_puts(m, buf);
2932 seq_putc(m, '\n');
2933 }
2934
2935out_unlock:
2936 mutex_unlock(&cgroup_mutex);
2937 put_task_struct(tsk);
2938out_free:
2939 kfree(buf);
2940out:
2941 return retval;
2942}
2943
2944static int cgroup_open(struct inode *inode, struct file *file)
2945{
2946 struct pid *pid = PROC_I(inode)->pid;
2947 return single_open(file, proc_cgroup_show, pid);
2948}
2949
2950struct file_operations proc_cgroup_operations = {
2951 .open = cgroup_open,
2952 .read = seq_read,
2953 .llseek = seq_lseek,
2954 .release = single_release,
2955};
2956
2957/* Display information about each subsystem and each hierarchy */
2958static int proc_cgroupstats_show(struct seq_file *m, void *v)
2959{
2960 int i;
Paul Menagea4243162007-10-18 23:39:35 -07002961
Paul Menage8bab8dd2008-04-04 14:29:57 -07002962 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07002963 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07002964 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2965 struct cgroup_subsys *ss = subsys[i];
Paul Menage8bab8dd2008-04-04 14:29:57 -07002966 seq_printf(m, "%s\t%lu\t%d\t%d\n",
Paul Menage817929e2007-10-18 23:39:36 -07002967 ss->name, ss->root->subsys_bits,
Paul Menage8bab8dd2008-04-04 14:29:57 -07002968 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07002969 }
2970 mutex_unlock(&cgroup_mutex);
2971 return 0;
2972}
2973
2974static int cgroupstats_open(struct inode *inode, struct file *file)
2975{
Al Viro9dce07f2008-03-29 03:07:28 +00002976 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07002977}
2978
2979static struct file_operations proc_cgroupstats_operations = {
2980 .open = cgroupstats_open,
2981 .read = seq_read,
2982 .llseek = seq_lseek,
2983 .release = single_release,
2984};
2985
Paul Menageb4f48b62007-10-18 23:39:33 -07002986/**
2987 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08002988 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07002989 *
2990 * Description: A task inherits its parent's cgroup at fork().
2991 *
2992 * A pointer to the shared css_set was automatically copied in
2993 * fork.c by dup_task_struct(). However, we ignore that copy, since
2994 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08002995 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07002996 * have already changed current->cgroups, allowing the previously
2997 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07002998 *
2999 * At the point that cgroup_fork() is called, 'current' is the parent
3000 * task, and the passed argument 'child' points to the child task.
3001 */
3002void cgroup_fork(struct task_struct *child)
3003{
Paul Menage817929e2007-10-18 23:39:36 -07003004 task_lock(current);
3005 child->cgroups = current->cgroups;
3006 get_css_set(child->cgroups);
3007 task_unlock(current);
3008 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07003009}
3010
3011/**
Li Zefana043e3b2008-02-23 15:24:09 -08003012 * cgroup_fork_callbacks - run fork callbacks
3013 * @child: the new task
3014 *
3015 * Called on a new task very soon before adding it to the
3016 * tasklist. No need to take any locks since no-one can
3017 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003018 */
3019void cgroup_fork_callbacks(struct task_struct *child)
3020{
3021 if (need_forkexit_callback) {
3022 int i;
3023 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3024 struct cgroup_subsys *ss = subsys[i];
3025 if (ss->fork)
3026 ss->fork(ss, child);
3027 }
3028 }
3029}
3030
3031/**
Li Zefana043e3b2008-02-23 15:24:09 -08003032 * cgroup_post_fork - called on a new task after adding it to the task list
3033 * @child: the task in question
3034 *
3035 * Adds the task to the list running through its css_set if necessary.
3036 * Has to be after the task is visible on the task list in case we race
3037 * with the first call to cgroup_iter_start() - to guarantee that the
3038 * new task ends up on its list.
3039 */
Paul Menage817929e2007-10-18 23:39:36 -07003040void cgroup_post_fork(struct task_struct *child)
3041{
3042 if (use_task_css_set_links) {
3043 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003044 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003045 if (list_empty(&child->cg_list))
3046 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003047 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003048 write_unlock(&css_set_lock);
3049 }
3050}
3051/**
Paul Menageb4f48b62007-10-18 23:39:33 -07003052 * cgroup_exit - detach cgroup from exiting task
3053 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08003054 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07003055 *
3056 * Description: Detach cgroup from @tsk and release it.
3057 *
3058 * Note that cgroups marked notify_on_release force every task in
3059 * them to take the global cgroup_mutex mutex when exiting.
3060 * This could impact scaling on very large systems. Be reluctant to
3061 * use notify_on_release cgroups where very high task exit scaling
3062 * is required on large systems.
3063 *
3064 * the_top_cgroup_hack:
3065 *
3066 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3067 *
3068 * We call cgroup_exit() while the task is still competent to
3069 * handle notify_on_release(), then leave the task attached to the
3070 * root cgroup in each hierarchy for the remainder of its exit.
3071 *
3072 * To do this properly, we would increment the reference count on
3073 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3074 * code we would add a second cgroup function call, to drop that
3075 * reference. This would just create an unnecessary hot spot on
3076 * the top_cgroup reference count, to no avail.
3077 *
3078 * Normally, holding a reference to a cgroup without bumping its
3079 * count is unsafe. The cgroup could go away, or someone could
3080 * attach us to a different cgroup, decrementing the count on
3081 * the first cgroup that we never incremented. But in this case,
3082 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003083 * which wards off any cgroup_attach_task() attempts, or task is a failed
3084 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003085 */
3086void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3087{
3088 int i;
Paul Menage817929e2007-10-18 23:39:36 -07003089 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07003090
3091 if (run_callbacks && need_forkexit_callback) {
3092 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3093 struct cgroup_subsys *ss = subsys[i];
3094 if (ss->exit)
3095 ss->exit(ss, tsk);
3096 }
3097 }
Paul Menage817929e2007-10-18 23:39:36 -07003098
3099 /*
3100 * Unlink from the css_set task list if necessary.
3101 * Optimistically check cg_list before taking
3102 * css_set_lock
3103 */
3104 if (!list_empty(&tsk->cg_list)) {
3105 write_lock(&css_set_lock);
3106 if (!list_empty(&tsk->cg_list))
3107 list_del(&tsk->cg_list);
3108 write_unlock(&css_set_lock);
3109 }
3110
Paul Menageb4f48b62007-10-18 23:39:33 -07003111 /* Reassign the task to the init_css_set. */
3112 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003113 cg = tsk->cgroups;
3114 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07003115 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003116 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003117 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07003118}
Paul Menage697f4162007-10-18 23:39:34 -07003119
3120/**
Li Zefana043e3b2008-02-23 15:24:09 -08003121 * cgroup_clone - clone the cgroup the given subsystem is attached to
3122 * @tsk: the task to be moved
3123 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003124 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08003125 *
3126 * Duplicate the current cgroup in the hierarchy that the given
3127 * subsystem is attached to, and move this task into the new
3128 * child.
Paul Menage697f4162007-10-18 23:39:34 -07003129 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003130int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3131 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07003132{
3133 struct dentry *dentry;
3134 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07003135 struct cgroup *parent, *child;
3136 struct inode *inode;
3137 struct css_set *cg;
3138 struct cgroupfs_root *root;
3139 struct cgroup_subsys *ss;
3140
3141 /* We shouldn't be called by an unregistered subsystem */
3142 BUG_ON(!subsys->active);
3143
3144 /* First figure out what hierarchy and cgroup we're dealing
3145 * with, and pin them so we can drop cgroup_mutex */
3146 mutex_lock(&cgroup_mutex);
3147 again:
3148 root = subsys->root;
3149 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07003150 mutex_unlock(&cgroup_mutex);
3151 return 0;
3152 }
Paul Menage697f4162007-10-18 23:39:34 -07003153
Paul Menage697f4162007-10-18 23:39:34 -07003154 /* Pin the hierarchy */
Li Zefan1404f062009-01-29 14:25:21 -08003155 if (!atomic_inc_not_zero(&root->sb->s_active)) {
Li Zefan7b574b72009-01-04 12:00:45 -08003156 /* We race with the final deactivate_super() */
3157 mutex_unlock(&cgroup_mutex);
3158 return 0;
3159 }
Paul Menage697f4162007-10-18 23:39:34 -07003160
Paul Menage817929e2007-10-18 23:39:36 -07003161 /* Keep the cgroup alive */
Li Zefan1404f062009-01-29 14:25:21 -08003162 task_lock(tsk);
3163 parent = task_cgroup(tsk, subsys->subsys_id);
3164 cg = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07003165 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08003166 task_unlock(tsk);
Li Zefan1404f062009-01-29 14:25:21 -08003167
Paul Menage697f4162007-10-18 23:39:34 -07003168 mutex_unlock(&cgroup_mutex);
3169
3170 /* Now do the VFS work to create a cgroup */
3171 inode = parent->dentry->d_inode;
3172
3173 /* Hold the parent directory mutex across this operation to
3174 * stop anyone else deleting the new cgroup */
3175 mutex_lock(&inode->i_mutex);
3176 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3177 if (IS_ERR(dentry)) {
3178 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08003179 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07003180 PTR_ERR(dentry));
3181 ret = PTR_ERR(dentry);
3182 goto out_release;
3183 }
3184
3185 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08003186 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07003187 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07003188 dput(dentry);
3189 if (ret) {
3190 printk(KERN_INFO
3191 "Failed to create cgroup %s: %d\n", nodename,
3192 ret);
3193 goto out_release;
3194 }
3195
Paul Menage697f4162007-10-18 23:39:34 -07003196 /* The cgroup now exists. Retake cgroup_mutex and check
3197 * that we're still in the same state that we thought we
3198 * were. */
3199 mutex_lock(&cgroup_mutex);
3200 if ((root != subsys->root) ||
3201 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3202 /* Aargh, we raced ... */
3203 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003204 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07003205
Li Zefan1404f062009-01-29 14:25:21 -08003206 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003207 /* The cgroup is still accessible in the VFS, but
3208 * we're not going to try to rmdir() it at this
3209 * point. */
3210 printk(KERN_INFO
3211 "Race in cgroup_clone() - leaking cgroup %s\n",
3212 nodename);
3213 goto again;
3214 }
3215
3216 /* do any required auto-setup */
3217 for_each_subsys(root, ss) {
3218 if (ss->post_clone)
3219 ss->post_clone(ss, child);
3220 }
3221
3222 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08003223 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07003224 mutex_unlock(&cgroup_mutex);
3225
3226 out_release:
3227 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003228
3229 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003230 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003231 mutex_unlock(&cgroup_mutex);
Li Zefan1404f062009-01-29 14:25:21 -08003232 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003233 return ret;
3234}
3235
Li Zefana043e3b2008-02-23 15:24:09 -08003236/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003237 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08003238 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003239 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08003240 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003241 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3242 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07003243 *
3244 * If we are sending in dummytop, then presumably we are creating
3245 * the top cgroup in the subsystem.
3246 *
3247 * Called only by the ns (nsproxy) cgroup.
3248 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003249int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07003250{
3251 int ret;
3252 struct cgroup *target;
3253 int subsys_id;
3254
Paul Menagebd89aab2007-10-18 23:40:44 -07003255 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07003256 return 1;
3257
Paul Menagebd89aab2007-10-18 23:40:44 -07003258 get_first_subsys(cgrp, NULL, &subsys_id);
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003259 target = task_cgroup(task, subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07003260 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3261 cgrp = cgrp->parent;
3262 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07003263 return ret;
3264}
Paul Menage81a6a5c2007-10-18 23:39:38 -07003265
Paul Menagebd89aab2007-10-18 23:40:44 -07003266static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003267{
3268 /* All of these checks rely on RCU to keep the cgroup
3269 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07003270 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3271 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003272 /* Control Group is currently removeable. If it's not
3273 * already queued for a userspace notification, queue
3274 * it now */
3275 int need_schedule_work = 0;
3276 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003277 if (!cgroup_is_removed(cgrp) &&
3278 list_empty(&cgrp->release_list)) {
3279 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003280 need_schedule_work = 1;
3281 }
3282 spin_unlock(&release_list_lock);
3283 if (need_schedule_work)
3284 schedule_work(&release_agent_work);
3285 }
3286}
3287
3288void __css_put(struct cgroup_subsys_state *css)
3289{
Paul Menagebd89aab2007-10-18 23:40:44 -07003290 struct cgroup *cgrp = css->cgroup;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003291 rcu_read_lock();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003292 if (atomic_dec_return(&css->refcnt) == 1) {
3293 if (notify_on_release(cgrp)) {
3294 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3295 check_for_release(cgrp);
3296 }
3297 cgroup_wakeup_rmdir_waiters(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003298 }
3299 rcu_read_unlock();
3300}
3301
3302/*
3303 * Notify userspace when a cgroup is released, by running the
3304 * configured release agent with the name of the cgroup (path
3305 * relative to the root of cgroup file system) as the argument.
3306 *
3307 * Most likely, this user command will try to rmdir this cgroup.
3308 *
3309 * This races with the possibility that some other task will be
3310 * attached to this cgroup before it is removed, or that some other
3311 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3312 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3313 * unused, and this cgroup will be reprieved from its death sentence,
3314 * to continue to serve a useful existence. Next time it's released,
3315 * we will get notified again, if it still has 'notify_on_release' set.
3316 *
3317 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3318 * means only wait until the task is successfully execve()'d. The
3319 * separate release agent task is forked by call_usermodehelper(),
3320 * then control in this thread returns here, without waiting for the
3321 * release agent task. We don't bother to wait because the caller of
3322 * this routine has no use for the exit status of the release agent
3323 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003324 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003325static void cgroup_release_agent(struct work_struct *work)
3326{
3327 BUG_ON(work != &release_agent_work);
3328 mutex_lock(&cgroup_mutex);
3329 spin_lock(&release_list_lock);
3330 while (!list_empty(&release_list)) {
3331 char *argv[3], *envp[3];
3332 int i;
Paul Menagee788e062008-07-25 01:46:59 -07003333 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003334 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003335 struct cgroup,
3336 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003337 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003338 spin_unlock(&release_list_lock);
3339 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07003340 if (!pathbuf)
3341 goto continue_free;
3342 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3343 goto continue_free;
3344 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3345 if (!agentbuf)
3346 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003347
3348 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07003349 argv[i++] = agentbuf;
3350 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003351 argv[i] = NULL;
3352
3353 i = 0;
3354 /* minimal command environment */
3355 envp[i++] = "HOME=/";
3356 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3357 envp[i] = NULL;
3358
3359 /* Drop the lock while we invoke the usermode helper,
3360 * since the exec could involve hitting disk and hence
3361 * be a slow process */
3362 mutex_unlock(&cgroup_mutex);
3363 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003364 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07003365 continue_free:
3366 kfree(pathbuf);
3367 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003368 spin_lock(&release_list_lock);
3369 }
3370 spin_unlock(&release_list_lock);
3371 mutex_unlock(&cgroup_mutex);
3372}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003373
3374static int __init cgroup_disable(char *str)
3375{
3376 int i;
3377 char *token;
3378
3379 while ((token = strsep(&str, ",")) != NULL) {
3380 if (!*token)
3381 continue;
3382
3383 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3384 struct cgroup_subsys *ss = subsys[i];
3385
3386 if (!strcmp(token, ss->name)) {
3387 ss->disabled = 1;
3388 printk(KERN_INFO "Disabling %s control group"
3389 " subsystem\n", ss->name);
3390 break;
3391 }
3392 }
3393 }
3394 return 1;
3395}
3396__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003397
3398/*
3399 * Functons for CSS ID.
3400 */
3401
3402/*
3403 *To get ID other than 0, this should be called when !cgroup_is_removed().
3404 */
3405unsigned short css_id(struct cgroup_subsys_state *css)
3406{
3407 struct css_id *cssid = rcu_dereference(css->id);
3408
3409 if (cssid)
3410 return cssid->id;
3411 return 0;
3412}
3413
3414unsigned short css_depth(struct cgroup_subsys_state *css)
3415{
3416 struct css_id *cssid = rcu_dereference(css->id);
3417
3418 if (cssid)
3419 return cssid->depth;
3420 return 0;
3421}
3422
3423bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07003424 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003425{
3426 struct css_id *child_id = rcu_dereference(child->id);
3427 struct css_id *root_id = rcu_dereference(root->id);
3428
3429 if (!child_id || !root_id || (child_id->depth < root_id->depth))
3430 return false;
3431 return child_id->stack[root_id->depth] == root_id->id;
3432}
3433
3434static void __free_css_id_cb(struct rcu_head *head)
3435{
3436 struct css_id *id;
3437
3438 id = container_of(head, struct css_id, rcu_head);
3439 kfree(id);
3440}
3441
3442void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
3443{
3444 struct css_id *id = css->id;
3445 /* When this is called before css_id initialization, id can be NULL */
3446 if (!id)
3447 return;
3448
3449 BUG_ON(!ss->use_id);
3450
3451 rcu_assign_pointer(id->css, NULL);
3452 rcu_assign_pointer(css->id, NULL);
3453 spin_lock(&ss->id_lock);
3454 idr_remove(&ss->idr, id->id);
3455 spin_unlock(&ss->id_lock);
3456 call_rcu(&id->rcu_head, __free_css_id_cb);
3457}
3458
3459/*
3460 * This is called by init or create(). Then, calls to this function are
3461 * always serialized (By cgroup_mutex() at create()).
3462 */
3463
3464static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
3465{
3466 struct css_id *newid;
3467 int myid, error, size;
3468
3469 BUG_ON(!ss->use_id);
3470
3471 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
3472 newid = kzalloc(size, GFP_KERNEL);
3473 if (!newid)
3474 return ERR_PTR(-ENOMEM);
3475 /* get id */
3476 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
3477 error = -ENOMEM;
3478 goto err_out;
3479 }
3480 spin_lock(&ss->id_lock);
3481 /* Don't use 0. allocates an ID of 1-65535 */
3482 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
3483 spin_unlock(&ss->id_lock);
3484
3485 /* Returns error when there are no free spaces for new ID.*/
3486 if (error) {
3487 error = -ENOSPC;
3488 goto err_out;
3489 }
3490 if (myid > CSS_ID_MAX)
3491 goto remove_idr;
3492
3493 newid->id = myid;
3494 newid->depth = depth;
3495 return newid;
3496remove_idr:
3497 error = -ENOSPC;
3498 spin_lock(&ss->id_lock);
3499 idr_remove(&ss->idr, myid);
3500 spin_unlock(&ss->id_lock);
3501err_out:
3502 kfree(newid);
3503 return ERR_PTR(error);
3504
3505}
3506
3507static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss)
3508{
3509 struct css_id *newid;
3510 struct cgroup_subsys_state *rootcss;
3511
3512 spin_lock_init(&ss->id_lock);
3513 idr_init(&ss->idr);
3514
3515 rootcss = init_css_set.subsys[ss->subsys_id];
3516 newid = get_new_cssid(ss, 0);
3517 if (IS_ERR(newid))
3518 return PTR_ERR(newid);
3519
3520 newid->stack[0] = newid->id;
3521 newid->css = rootcss;
3522 rootcss->id = newid;
3523 return 0;
3524}
3525
3526static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
3527 struct cgroup *child)
3528{
3529 int subsys_id, i, depth = 0;
3530 struct cgroup_subsys_state *parent_css, *child_css;
3531 struct css_id *child_id, *parent_id = NULL;
3532
3533 subsys_id = ss->subsys_id;
3534 parent_css = parent->subsys[subsys_id];
3535 child_css = child->subsys[subsys_id];
3536 depth = css_depth(parent_css) + 1;
3537 parent_id = parent_css->id;
3538
3539 child_id = get_new_cssid(ss, depth);
3540 if (IS_ERR(child_id))
3541 return PTR_ERR(child_id);
3542
3543 for (i = 0; i < depth; i++)
3544 child_id->stack[i] = parent_id->stack[i];
3545 child_id->stack[depth] = child_id->id;
3546 /*
3547 * child_id->css pointer will be set after this cgroup is available
3548 * see cgroup_populate_dir()
3549 */
3550 rcu_assign_pointer(child_css->id, child_id);
3551
3552 return 0;
3553}
3554
3555/**
3556 * css_lookup - lookup css by id
3557 * @ss: cgroup subsys to be looked into.
3558 * @id: the id
3559 *
3560 * Returns pointer to cgroup_subsys_state if there is valid one with id.
3561 * NULL if not. Should be called under rcu_read_lock()
3562 */
3563struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
3564{
3565 struct css_id *cssid = NULL;
3566
3567 BUG_ON(!ss->use_id);
3568 cssid = idr_find(&ss->idr, id);
3569
3570 if (unlikely(!cssid))
3571 return NULL;
3572
3573 return rcu_dereference(cssid->css);
3574}
3575
3576/**
3577 * css_get_next - lookup next cgroup under specified hierarchy.
3578 * @ss: pointer to subsystem
3579 * @id: current position of iteration.
3580 * @root: pointer to css. search tree under this.
3581 * @foundid: position of found object.
3582 *
3583 * Search next css under the specified hierarchy of rootid. Calling under
3584 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
3585 */
3586struct cgroup_subsys_state *
3587css_get_next(struct cgroup_subsys *ss, int id,
3588 struct cgroup_subsys_state *root, int *foundid)
3589{
3590 struct cgroup_subsys_state *ret = NULL;
3591 struct css_id *tmp;
3592 int tmpid;
3593 int rootid = css_id(root);
3594 int depth = css_depth(root);
3595
3596 if (!rootid)
3597 return NULL;
3598
3599 BUG_ON(!ss->use_id);
3600 /* fill start point for scan */
3601 tmpid = id;
3602 while (1) {
3603 /*
3604 * scan next entry from bitmap(tree), tmpid is updated after
3605 * idr_get_next().
3606 */
3607 spin_lock(&ss->id_lock);
3608 tmp = idr_get_next(&ss->idr, &tmpid);
3609 spin_unlock(&ss->id_lock);
3610
3611 if (!tmp)
3612 break;
3613 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
3614 ret = rcu_dereference(tmp->css);
3615 if (ret) {
3616 *foundid = tmpid;
3617 break;
3618 }
3619 }
3620 /* continue to scan from next id */
3621 tmpid = tmpid + 1;
3622 }
3623 return ret;
3624}
3625