blob: ea94984a389532fdc7585b52c3c781dee08e84e8 [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 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
Ingo Molnarc50cc752010-02-25 12:02:13 +010030#include <linux/module.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
33#include <linux/fs.h>
34#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070054#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Alessio Igor Bogani337eb002009-05-12 15:10:54 +020056#include <linux/smp_lock.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070057#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070058#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070059#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080060#include <linux/eventfd.h>
61#include <linux/poll.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070062
Paul Menageddbcc7e2007-10-18 23:39:30 -070063#include <asm/atomic.h>
64
Paul Menage81a6a5c2007-10-18 23:39:38 -070065static DEFINE_MUTEX(cgroup_mutex);
66
Ben Blumaae8aab2010-03-10 15:22:07 -080067/*
68 * Generate an array of cgroup subsystem pointers. At boot time, this is
69 * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
70 * registered after that. The mutable section of this array is protected by
71 * cgroup_mutex.
72 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070073#define SUBSYS(_x) &_x ## _subsys,
Ben Blumaae8aab2010-03-10 15:22:07 -080074static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070075#include <linux/cgroup_subsys.h>
76};
77
Paul Menagec6d57f32009-09-23 15:56:19 -070078#define MAX_CGROUP_ROOT_NAMELEN 64
79
Paul Menageddbcc7e2007-10-18 23:39:30 -070080/*
81 * A cgroupfs_root represents the root of a cgroup hierarchy,
82 * and may be associated with a superblock to form an active
83 * hierarchy
84 */
85struct cgroupfs_root {
86 struct super_block *sb;
87
88 /*
89 * The bitmask of subsystems intended to be attached to this
90 * hierarchy
91 */
92 unsigned long subsys_bits;
93
Paul Menage2c6ab6d2009-09-23 15:56:23 -070094 /* Unique id for this hierarchy. */
95 int hierarchy_id;
96
Paul Menageddbcc7e2007-10-18 23:39:30 -070097 /* The bitmask of subsystems currently attached to this hierarchy */
98 unsigned long actual_subsys_bits;
99
100 /* A list running through the attached subsystems */
101 struct list_head subsys_list;
102
103 /* The root cgroup for this hierarchy */
104 struct cgroup top_cgroup;
105
106 /* Tracks how many cgroups are currently defined in hierarchy.*/
107 int number_of_cgroups;
108
Li Zefane5f6a862009-01-07 18:07:41 -0800109 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700110 struct list_head root_list;
111
112 /* Hierarchy-specific flags */
113 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700114
Paul Menagee788e0662008-07-25 01:46:59 -0700115 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700116 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700117
118 /* The name for this hierarchy - may be empty */
119 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120};
121
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122/*
123 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
124 * subsystems that are otherwise unattached - it never has more than a
125 * single cgroup, and all tasks are part of that cgroup.
126 */
127static struct cgroupfs_root rootnode;
128
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700129/*
130 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
131 * cgroup_subsys->use_id != 0.
132 */
133#define CSS_ID_MAX (65535)
134struct css_id {
135 /*
136 * The css to which this ID points. This pointer is set to valid value
137 * after cgroup is populated. If cgroup is removed, this will be NULL.
138 * This pointer is expected to be RCU-safe because destroy()
139 * is called after synchronize_rcu(). But for safe use, css_is_removed()
140 * css_tryget() should be used for avoiding race.
141 */
142 struct cgroup_subsys_state *css;
143 /*
144 * ID of this css.
145 */
146 unsigned short id;
147 /*
148 * Depth in hierarchy which this ID belongs to.
149 */
150 unsigned short depth;
151 /*
152 * ID is freed by RCU. (and lookup routine is RCU safe.)
153 */
154 struct rcu_head rcu_head;
155 /*
156 * Hierarchy of CSS ID belongs to.
157 */
158 unsigned short stack[0]; /* Array of Length (depth+1) */
159};
160
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800161/*
162 * cgroup_event represents events which userspace want to recieve.
163 */
164struct cgroup_event {
165 /*
166 * Cgroup which the event belongs to.
167 */
168 struct cgroup *cgrp;
169 /*
170 * Control file which the event associated.
171 */
172 struct cftype *cft;
173 /*
174 * eventfd to signal userspace about the event.
175 */
176 struct eventfd_ctx *eventfd;
177 /*
178 * Each of these stored in a list by the cgroup.
179 */
180 struct list_head list;
181 /*
182 * All fields below needed to unregister event when
183 * userspace closes eventfd.
184 */
185 poll_table pt;
186 wait_queue_head_t *wqh;
187 wait_queue_t wait;
188 struct work_struct remove;
189};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700190
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191/* The list of hierarchy roots */
192
193static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700194static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700195
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700196static DEFINE_IDA(hierarchy_ida);
197static int next_hierarchy_id;
198static DEFINE_SPINLOCK(hierarchy_id_lock);
199
Paul Menageddbcc7e2007-10-18 23:39:30 -0700200/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
201#define dummytop (&rootnode.top_cgroup)
202
203/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800204 * check for fork/exit handlers to call. This avoids us having to do
205 * extra work in the fork/exit path if none of the subsystems need to
206 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700207 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700208static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700209
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800210#ifdef CONFIG_PROVE_LOCKING
211int cgroup_lock_is_held(void)
212{
213 return lockdep_is_held(&cgroup_mutex);
214}
215#else /* #ifdef CONFIG_PROVE_LOCKING */
216int cgroup_lock_is_held(void)
217{
218 return mutex_is_locked(&cgroup_mutex);
219}
220#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
221
222EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
223
Paul Menageddbcc7e2007-10-18 23:39:30 -0700224/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700225inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700226{
Paul Menagebd89aab2007-10-18 23:40:44 -0700227 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700228}
229
230/* bits in struct cgroupfs_root flags field */
231enum {
232 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
233};
234
Adrian Bunke9685a02008-02-07 00:13:46 -0800235static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700236{
237 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700238 (1 << CGRP_RELEASABLE) |
239 (1 << CGRP_NOTIFY_ON_RELEASE);
240 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700241}
242
Adrian Bunke9685a02008-02-07 00:13:46 -0800243static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700244{
Paul Menagebd89aab2007-10-18 23:40:44 -0700245 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700246}
247
Paul Menageddbcc7e2007-10-18 23:39:30 -0700248/*
249 * for_each_subsys() allows you to iterate on each subsystem attached to
250 * an active hierarchy
251 */
252#define for_each_subsys(_root, _ss) \
253list_for_each_entry(_ss, &_root->subsys_list, sibling)
254
Li Zefane5f6a862009-01-07 18:07:41 -0800255/* for_each_active_root() allows you to iterate across the active hierarchies */
256#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700257list_for_each_entry(_root, &roots, root_list)
258
Paul Menage81a6a5c2007-10-18 23:39:38 -0700259/* the list of cgroups eligible for automatic release. Protected by
260 * release_list_lock */
261static LIST_HEAD(release_list);
262static DEFINE_SPINLOCK(release_list_lock);
263static void cgroup_release_agent(struct work_struct *work);
264static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700265static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700266
Paul Menage817929e2007-10-18 23:39:36 -0700267/* Link structure for associating css_set objects with cgroups */
268struct cg_cgroup_link {
269 /*
270 * List running through cg_cgroup_links associated with a
271 * cgroup, anchored on cgroup->css_sets
272 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700273 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700274 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700275 /*
276 * List running through cg_cgroup_links pointing at a
277 * single css_set object, anchored on css_set->cg_links
278 */
279 struct list_head cg_link_list;
280 struct css_set *cg;
281};
282
283/* The default css_set - used by init and its children prior to any
284 * hierarchies being mounted. It contains a pointer to the root state
285 * for each subsystem. Also used to anchor the list of css_sets. Not
286 * reference-counted, to improve performance when child cgroups
287 * haven't been created.
288 */
289
290static struct css_set init_css_set;
291static struct cg_cgroup_link init_css_set_link;
292
Ben Blume6a11052010-03-10 15:22:09 -0800293static int cgroup_init_idr(struct cgroup_subsys *ss,
294 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700295
Paul Menage817929e2007-10-18 23:39:36 -0700296/* css_set_lock protects the list of css_set objects, and the
297 * chain of tasks off each css_set. Nests outside task->alloc_lock
298 * due to cgroup_iter_start() */
299static DEFINE_RWLOCK(css_set_lock);
300static int css_set_count;
301
Paul Menage7717f7b2009-09-23 15:56:22 -0700302/*
303 * hash table for cgroup groups. This improves the performance to find
304 * an existing css_set. This hash doesn't (currently) take into
305 * account cgroups in empty hierarchies.
306 */
Li Zefan472b1052008-04-29 01:00:11 -0700307#define CSS_SET_HASH_BITS 7
308#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
309static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
310
311static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
312{
313 int i;
314 int index;
315 unsigned long tmp = 0UL;
316
317 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
318 tmp += (unsigned long)css[i];
319 tmp = (tmp >> 16) ^ tmp;
320
321 index = hash_long(tmp, CSS_SET_HASH_BITS);
322
323 return &css_set_table[index];
324}
325
Ben Blumc3783692009-09-23 15:56:29 -0700326static void free_css_set_rcu(struct rcu_head *obj)
327{
328 struct css_set *cg = container_of(obj, struct css_set, rcu_head);
329 kfree(cg);
330}
331
Paul Menage817929e2007-10-18 23:39:36 -0700332/* We don't maintain the lists running through each css_set to its
333 * task until after the first call to cgroup_iter_start(). This
334 * reduces the fork()/exit() overhead for people who have cgroups
335 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700336static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700337
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700338static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700339{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700340 struct cg_cgroup_link *link;
341 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700342 /*
343 * Ensure that the refcount doesn't hit zero while any readers
344 * can see it. Similar to atomic_dec_and_lock(), but for an
345 * rwlock
346 */
347 if (atomic_add_unless(&cg->refcount, -1, 1))
348 return;
349 write_lock(&css_set_lock);
350 if (!atomic_dec_and_test(&cg->refcount)) {
351 write_unlock(&css_set_lock);
352 return;
353 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700354
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700355 /* This css_set is dead. unlink it and release cgroup refcounts */
356 hlist_del(&cg->hlist);
357 css_set_count--;
358
359 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
360 cg_link_list) {
361 struct cgroup *cgrp = link->cgrp;
362 list_del(&link->cg_link_list);
363 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700364 if (atomic_dec_and_test(&cgrp->count) &&
365 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700366 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700367 set_bit(CGRP_RELEASABLE, &cgrp->flags);
368 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700369 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700370
371 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700372 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700373
374 write_unlock(&css_set_lock);
Ben Blumc3783692009-09-23 15:56:29 -0700375 call_rcu(&cg->rcu_head, free_css_set_rcu);
Paul Menage817929e2007-10-18 23:39:36 -0700376}
377
378/*
379 * refcounted get/put for css_set objects
380 */
381static inline void get_css_set(struct css_set *cg)
382{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700383 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700384}
385
386static inline void put_css_set(struct css_set *cg)
387{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700388 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700389}
390
Paul Menage81a6a5c2007-10-18 23:39:38 -0700391static inline void put_css_set_taskexit(struct css_set *cg)
392{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700393 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700394}
395
Paul Menage817929e2007-10-18 23:39:36 -0700396/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700397 * compare_css_sets - helper function for find_existing_css_set().
398 * @cg: candidate css_set being tested
399 * @old_cg: existing css_set for a task
400 * @new_cgrp: cgroup that's being entered by the task
401 * @template: desired set of css pointers in css_set (pre-calculated)
402 *
403 * Returns true if "cg" matches "old_cg" except for the hierarchy
404 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
405 */
406static bool compare_css_sets(struct css_set *cg,
407 struct css_set *old_cg,
408 struct cgroup *new_cgrp,
409 struct cgroup_subsys_state *template[])
410{
411 struct list_head *l1, *l2;
412
413 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
414 /* Not all subsystems matched */
415 return false;
416 }
417
418 /*
419 * Compare cgroup pointers in order to distinguish between
420 * different cgroups in heirarchies with no subsystems. We
421 * could get by with just this check alone (and skip the
422 * memcmp above) but on most setups the memcmp check will
423 * avoid the need for this more expensive check on almost all
424 * candidates.
425 */
426
427 l1 = &cg->cg_links;
428 l2 = &old_cg->cg_links;
429 while (1) {
430 struct cg_cgroup_link *cgl1, *cgl2;
431 struct cgroup *cg1, *cg2;
432
433 l1 = l1->next;
434 l2 = l2->next;
435 /* See if we reached the end - both lists are equal length. */
436 if (l1 == &cg->cg_links) {
437 BUG_ON(l2 != &old_cg->cg_links);
438 break;
439 } else {
440 BUG_ON(l2 == &old_cg->cg_links);
441 }
442 /* Locate the cgroups associated with these links. */
443 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
444 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
445 cg1 = cgl1->cgrp;
446 cg2 = cgl2->cgrp;
447 /* Hierarchies should be linked in the same order. */
448 BUG_ON(cg1->root != cg2->root);
449
450 /*
451 * If this hierarchy is the hierarchy of the cgroup
452 * that's changing, then we need to check that this
453 * css_set points to the new cgroup; if it's any other
454 * hierarchy, then this css_set should point to the
455 * same cgroup as the old css_set.
456 */
457 if (cg1->root == new_cgrp->root) {
458 if (cg1 != new_cgrp)
459 return false;
460 } else {
461 if (cg1 != cg2)
462 return false;
463 }
464 }
465 return true;
466}
467
468/*
Paul Menage817929e2007-10-18 23:39:36 -0700469 * find_existing_css_set() is a helper for
470 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700471 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700472 *
473 * oldcg: the cgroup group that we're using before the cgroup
474 * transition
475 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700476 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700477 *
478 * template: location in which to build the desired set of subsystem
479 * state objects for the new cgroup group
480 */
Paul Menage817929e2007-10-18 23:39:36 -0700481static struct css_set *find_existing_css_set(
482 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700483 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700484 struct cgroup_subsys_state *template[])
485{
486 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700487 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700488 struct hlist_head *hhead;
489 struct hlist_node *node;
490 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700491
Ben Blumaae8aab2010-03-10 15:22:07 -0800492 /*
493 * Build the set of subsystem state objects that we want to see in the
494 * new css_set. while subsystems can change globally, the entries here
495 * won't change, so no need for locking.
496 */
Paul Menage817929e2007-10-18 23:39:36 -0700497 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800498 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700499 /* Subsystem is in this hierarchy. So we want
500 * the subsystem state from the new
501 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700502 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700503 } else {
504 /* Subsystem is not in this hierarchy, so we
505 * don't want to change the subsystem state */
506 template[i] = oldcg->subsys[i];
507 }
508 }
509
Li Zefan472b1052008-04-29 01:00:11 -0700510 hhead = css_set_hash(template);
511 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700512 if (!compare_css_sets(cg, oldcg, cgrp, template))
513 continue;
514
515 /* This css_set matches what we need */
516 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700517 }
Paul Menage817929e2007-10-18 23:39:36 -0700518
519 /* No existing cgroup group matched */
520 return NULL;
521}
522
Paul Menage817929e2007-10-18 23:39:36 -0700523static void free_cg_links(struct list_head *tmp)
524{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700525 struct cg_cgroup_link *link;
526 struct cg_cgroup_link *saved_link;
527
528 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700529 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700530 kfree(link);
531 }
532}
533
534/*
Li Zefan36553432008-07-29 22:33:19 -0700535 * allocate_cg_links() allocates "count" cg_cgroup_link structures
536 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
537 * success or a negative error
538 */
539static int allocate_cg_links(int count, struct list_head *tmp)
540{
541 struct cg_cgroup_link *link;
542 int i;
543 INIT_LIST_HEAD(tmp);
544 for (i = 0; i < count; i++) {
545 link = kmalloc(sizeof(*link), GFP_KERNEL);
546 if (!link) {
547 free_cg_links(tmp);
548 return -ENOMEM;
549 }
550 list_add(&link->cgrp_link_list, tmp);
551 }
552 return 0;
553}
554
Li Zefanc12f65d2009-01-07 18:07:42 -0800555/**
556 * link_css_set - a helper function to link a css_set to a cgroup
557 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
558 * @cg: the css_set to be linked
559 * @cgrp: the destination cgroup
560 */
561static void link_css_set(struct list_head *tmp_cg_links,
562 struct css_set *cg, struct cgroup *cgrp)
563{
564 struct cg_cgroup_link *link;
565
566 BUG_ON(list_empty(tmp_cg_links));
567 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
568 cgrp_link_list);
569 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700570 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700571 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800572 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700573 /*
574 * Always add links to the tail of the list so that the list
575 * is sorted by order of hierarchy creation
576 */
577 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800578}
579
Li Zefan36553432008-07-29 22:33:19 -0700580/*
Paul Menage817929e2007-10-18 23:39:36 -0700581 * find_css_set() takes an existing cgroup group and a
582 * cgroup object, and returns a css_set object that's
583 * equivalent to the old group, but with the given cgroup
584 * substituted into the appropriate hierarchy. Must be called with
585 * cgroup_mutex held
586 */
Paul Menage817929e2007-10-18 23:39:36 -0700587static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700588 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700589{
590 struct css_set *res;
591 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700592
593 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700594
Li Zefan472b1052008-04-29 01:00:11 -0700595 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700596 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700597
Paul Menage817929e2007-10-18 23:39:36 -0700598 /* First see if we already have a cgroup group that matches
599 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700600 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700601 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700602 if (res)
603 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700604 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700605
606 if (res)
607 return res;
608
609 res = kmalloc(sizeof(*res), GFP_KERNEL);
610 if (!res)
611 return NULL;
612
613 /* Allocate all the cg_cgroup_link objects that we'll need */
614 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
615 kfree(res);
616 return NULL;
617 }
618
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700619 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700620 INIT_LIST_HEAD(&res->cg_links);
621 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700622 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700623
624 /* Copy the set of subsystem state objects generated in
625 * find_existing_css_set() */
626 memcpy(res->subsys, template, sizeof(res->subsys));
627
628 write_lock(&css_set_lock);
629 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700630 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
631 struct cgroup *c = link->cgrp;
632 if (c->root == cgrp->root)
633 c = cgrp;
634 link_css_set(&tmp_cg_links, res, c);
635 }
Paul Menage817929e2007-10-18 23:39:36 -0700636
637 BUG_ON(!list_empty(&tmp_cg_links));
638
Paul Menage817929e2007-10-18 23:39:36 -0700639 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700640
641 /* Add this cgroup group to the hash table */
642 hhead = css_set_hash(res->subsys);
643 hlist_add_head(&res->hlist, hhead);
644
Paul Menage817929e2007-10-18 23:39:36 -0700645 write_unlock(&css_set_lock);
646
647 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700648}
649
Paul Menageddbcc7e2007-10-18 23:39:30 -0700650/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700651 * Return the cgroup for "task" from the given hierarchy. Must be
652 * called with cgroup_mutex held.
653 */
654static struct cgroup *task_cgroup_from_root(struct task_struct *task,
655 struct cgroupfs_root *root)
656{
657 struct css_set *css;
658 struct cgroup *res = NULL;
659
660 BUG_ON(!mutex_is_locked(&cgroup_mutex));
661 read_lock(&css_set_lock);
662 /*
663 * No need to lock the task - since we hold cgroup_mutex the
664 * task can't change groups, so the only thing that can happen
665 * is that it exits and its css is set back to init_css_set.
666 */
667 css = task->cgroups;
668 if (css == &init_css_set) {
669 res = &root->top_cgroup;
670 } else {
671 struct cg_cgroup_link *link;
672 list_for_each_entry(link, &css->cg_links, cg_link_list) {
673 struct cgroup *c = link->cgrp;
674 if (c->root == root) {
675 res = c;
676 break;
677 }
678 }
679 }
680 read_unlock(&css_set_lock);
681 BUG_ON(!res);
682 return res;
683}
684
685/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700686 * There is one global cgroup mutex. We also require taking
687 * task_lock() when dereferencing a task's cgroup subsys pointers.
688 * See "The task_lock() exception", at the end of this comment.
689 *
690 * A task must hold cgroup_mutex to modify cgroups.
691 *
692 * Any task can increment and decrement the count field without lock.
693 * So in general, code holding cgroup_mutex can't rely on the count
694 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800695 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700696 * means that no tasks are currently attached, therefore there is no
697 * way a task attached to that cgroup can fork (the other way to
698 * increment the count). So code holding cgroup_mutex can safely
699 * assume that if the count is zero, it will stay zero. Similarly, if
700 * a task holds cgroup_mutex on a cgroup with zero count, it
701 * knows that the cgroup won't be removed, as cgroup_rmdir()
702 * needs that mutex.
703 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700704 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
705 * (usually) take cgroup_mutex. These are the two most performance
706 * critical pieces of code here. The exception occurs on cgroup_exit(),
707 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
708 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800709 * to the release agent with the name of the cgroup (path relative to
710 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700711 *
712 * A cgroup can only be deleted if both its 'count' of using tasks
713 * is zero, and its list of 'children' cgroups is empty. Since all
714 * tasks in the system use _some_ cgroup, and since there is always at
715 * least one task in the system (init, pid == 1), therefore, top_cgroup
716 * always has either children cgroups and/or using tasks. So we don't
717 * need a special hack to ensure that top_cgroup cannot be deleted.
718 *
719 * The task_lock() exception
720 *
721 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800722 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800723 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700724 * several performance critical places that need to reference
725 * task->cgroup without the expense of grabbing a system global
726 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800727 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700728 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
729 * the task_struct routinely used for such matters.
730 *
731 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800732 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700733 */
734
Paul Menageddbcc7e2007-10-18 23:39:30 -0700735/**
736 * cgroup_lock - lock out any changes to cgroup structures
737 *
738 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700739void cgroup_lock(void)
740{
741 mutex_lock(&cgroup_mutex);
742}
Ben Blum67523c42010-03-10 15:22:11 -0800743EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700744
745/**
746 * cgroup_unlock - release lock on cgroup changes
747 *
748 * Undo the lock taken in a previous cgroup_lock() call.
749 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700750void cgroup_unlock(void)
751{
752 mutex_unlock(&cgroup_mutex);
753}
Ben Blum67523c42010-03-10 15:22:11 -0800754EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700755
756/*
757 * A couple of forward declarations required, due to cyclic reference loop:
758 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
759 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
760 * -> cgroup_mkdir.
761 */
762
763static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
764static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700765static int cgroup_populate_dir(struct cgroup *cgrp);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700766static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700767static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700768
769static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200770 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700771 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700772};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700774static int alloc_css_id(struct cgroup_subsys *ss,
775 struct cgroup *parent, struct cgroup *child);
776
Paul Menageddbcc7e2007-10-18 23:39:30 -0700777static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
778{
779 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700780
781 if (inode) {
782 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100783 inode->i_uid = current_fsuid();
784 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700785 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
786 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
787 }
788 return inode;
789}
790
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800791/*
792 * Call subsys's pre_destroy handler.
793 * This is called before css refcnt check.
794 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700795static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800796{
797 struct cgroup_subsys *ss;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800798 struct cgroup_event *event, *tmp;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700799 int ret = 0;
800
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800801 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700802 if (ss->pre_destroy) {
803 ret = ss->pre_destroy(ss, cgrp);
804 if (ret)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800805 goto out;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700806 }
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800807
808 /*
809 * Unregister events and notify userspace.
810 */
811 spin_lock(&cgrp->event_list_lock);
812 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
813 list_del(&event->list);
814 eventfd_signal(event->eventfd, 1);
815 schedule_work(&event->remove);
816 }
817 spin_unlock(&cgrp->event_list_lock);
818
819out:
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700820 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800821}
822
Paul Menagea47295e2009-01-07 18:07:44 -0800823static void free_cgroup_rcu(struct rcu_head *obj)
824{
825 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
826
827 kfree(cgrp);
828}
829
Paul Menageddbcc7e2007-10-18 23:39:30 -0700830static void cgroup_diput(struct dentry *dentry, struct inode *inode)
831{
832 /* is dentry a directory ? if so, kfree() associated cgroup */
833 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700834 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800835 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700836 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700837 /* It's possible for external users to be holding css
838 * reference counts on a cgroup; css_put() needs to
839 * be able to access the cgroup after decrementing
840 * the reference count in order to know if it needs to
841 * queue the cgroup to be handled by the release
842 * agent */
843 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800844
845 mutex_lock(&cgroup_mutex);
846 /*
847 * Release the subsystem state objects.
848 */
Li Zefan75139b82009-01-07 18:07:33 -0800849 for_each_subsys(cgrp->root, ss)
850 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800851
852 cgrp->root->number_of_cgroups--;
853 mutex_unlock(&cgroup_mutex);
854
Paul Menagea47295e2009-01-07 18:07:44 -0800855 /*
856 * Drop the active superblock reference that we took when we
857 * created the cgroup
858 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800859 deactivate_super(cgrp->root->sb);
860
Ben Blum72a8cb32009-09-23 15:56:27 -0700861 /*
862 * if we're getting rid of the cgroup, refcount should ensure
863 * that there are no pidlists left.
864 */
865 BUG_ON(!list_empty(&cgrp->pidlists));
866
Paul Menagea47295e2009-01-07 18:07:44 -0800867 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700868 }
869 iput(inode);
870}
871
872static void remove_dir(struct dentry *d)
873{
874 struct dentry *parent = dget(d->d_parent);
875
876 d_delete(d);
877 simple_rmdir(parent->d_inode, d);
878 dput(parent);
879}
880
881static void cgroup_clear_directory(struct dentry *dentry)
882{
883 struct list_head *node;
884
885 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
886 spin_lock(&dcache_lock);
887 node = dentry->d_subdirs.next;
888 while (node != &dentry->d_subdirs) {
889 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
890 list_del_init(node);
891 if (d->d_inode) {
892 /* This should never be called on a cgroup
893 * directory with child cgroups */
894 BUG_ON(d->d_inode->i_mode & S_IFDIR);
895 d = dget_locked(d);
896 spin_unlock(&dcache_lock);
897 d_delete(d);
898 simple_unlink(dentry->d_inode, d);
899 dput(d);
900 spin_lock(&dcache_lock);
901 }
902 node = dentry->d_subdirs.next;
903 }
904 spin_unlock(&dcache_lock);
905}
906
907/*
908 * NOTE : the dentry must have been dget()'ed
909 */
910static void cgroup_d_remove_dir(struct dentry *dentry)
911{
912 cgroup_clear_directory(dentry);
913
914 spin_lock(&dcache_lock);
915 list_del_init(&dentry->d_u.d_child);
916 spin_unlock(&dcache_lock);
917 remove_dir(dentry);
918}
919
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700920/*
921 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
922 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
923 * reference to css->refcnt. In general, this refcnt is expected to goes down
924 * to zero, soon.
925 *
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700926 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700927 */
928DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
929
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700930static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700931{
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700932 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700933 wake_up_all(&cgroup_rmdir_waitq);
934}
935
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700936void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
937{
938 css_get(css);
939}
940
941void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
942{
943 cgroup_wakeup_rmdir_waiter(css->cgroup);
944 css_put(css);
945}
946
Ben Blumaae8aab2010-03-10 15:22:07 -0800947/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800948 * Call with cgroup_mutex held. Drops reference counts on modules, including
949 * any duplicate ones that parse_cgroupfs_options took. If this function
950 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800951 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700952static int rebind_subsystems(struct cgroupfs_root *root,
953 unsigned long final_bits)
954{
955 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700956 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700957 int i;
958
Ben Blumaae8aab2010-03-10 15:22:07 -0800959 BUG_ON(!mutex_is_locked(&cgroup_mutex));
960
Paul Menageddbcc7e2007-10-18 23:39:30 -0700961 removed_bits = root->actual_subsys_bits & ~final_bits;
962 added_bits = final_bits & ~root->actual_subsys_bits;
963 /* Check that any added subsystems are currently free */
964 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800965 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700966 struct cgroup_subsys *ss = subsys[i];
967 if (!(bit & added_bits))
968 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -0800969 /*
970 * Nobody should tell us to do a subsys that doesn't exist:
971 * parse_cgroupfs_options should catch that case and refcounts
972 * ensure that subsystems won't disappear once selected.
973 */
974 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700975 if (ss->root != &rootnode) {
976 /* Subsystem isn't free */
977 return -EBUSY;
978 }
979 }
980
981 /* Currently we don't handle adding/removing subsystems when
982 * any child cgroups exist. This is theoretically supportable
983 * but involves complex error handling, so it's being left until
984 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800985 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986 return -EBUSY;
987
988 /* Process each subsystem */
989 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
990 struct cgroup_subsys *ss = subsys[i];
991 unsigned long bit = 1UL << i;
992 if (bit & added_bits) {
993 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -0800994 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -0700995 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700996 BUG_ON(!dummytop->subsys[i]);
997 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800998 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700999 cgrp->subsys[i] = dummytop->subsys[i];
1000 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001001 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001002 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -07001004 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -08001005 mutex_unlock(&ss->hierarchy_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001006 /* refcount was already taken, and we're keeping it */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007 } else if (bit & removed_bits) {
1008 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001009 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001010 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1011 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -08001012 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 if (ss->bind)
1014 ss->bind(ss, dummytop);
1015 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001016 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001017 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001018 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -08001019 mutex_unlock(&ss->hierarchy_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001020 /* subsystem is now free - drop reference on module */
1021 module_put(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001022 } else if (bit & final_bits) {
1023 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001024 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001025 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001026 /*
1027 * a refcount was taken, but we already had one, so
1028 * drop the extra reference.
1029 */
1030 module_put(ss->module);
1031#ifdef CONFIG_MODULE_UNLOAD
1032 BUG_ON(ss->module && !module_refcount(ss->module));
1033#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001034 } else {
1035 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001036 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001037 }
1038 }
1039 root->subsys_bits = root->actual_subsys_bits = final_bits;
1040 synchronize_rcu();
1041
1042 return 0;
1043}
1044
1045static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
1046{
1047 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
1048 struct cgroup_subsys *ss;
1049
1050 mutex_lock(&cgroup_mutex);
1051 for_each_subsys(root, ss)
1052 seq_printf(seq, ",%s", ss->name);
1053 if (test_bit(ROOT_NOPREFIX, &root->flags))
1054 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001055 if (strlen(root->release_agent_path))
1056 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menagec6d57f32009-09-23 15:56:19 -07001057 if (strlen(root->name))
1058 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 mutex_unlock(&cgroup_mutex);
1060 return 0;
1061}
1062
1063struct cgroup_sb_opts {
1064 unsigned long subsys_bits;
1065 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001066 char *release_agent;
Paul Menagec6d57f32009-09-23 15:56:19 -07001067 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001068 /* User explicitly requested empty subsystem */
1069 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001070
1071 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001072
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073};
1074
Ben Blumaae8aab2010-03-10 15:22:07 -08001075/*
1076 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001077 * with cgroup_mutex held to protect the subsys[] array. This function takes
1078 * refcounts on subsystems to be used, unless it returns error, in which case
1079 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001080 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001081static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082{
1083 char *token, *o = data ?: "all";
Li Zefanf9ab5b52009-06-17 16:26:33 -07001084 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001085 int i;
1086 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001087
Ben Blumaae8aab2010-03-10 15:22:07 -08001088 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1089
Li Zefanf9ab5b52009-06-17 16:26:33 -07001090#ifdef CONFIG_CPUSETS
1091 mask = ~(1UL << cpuset_subsys_id);
1092#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093
Paul Menagec6d57f32009-09-23 15:56:19 -07001094 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095
1096 while ((token = strsep(&o, ",")) != NULL) {
1097 if (!*token)
1098 return -EINVAL;
1099 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07001100 /* Add all non-disabled subsystems */
Paul Menage8bab8dd2008-04-04 14:29:57 -07001101 opts->subsys_bits = 0;
1102 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1103 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08001104 if (ss == NULL)
1105 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07001106 if (!ss->disabled)
1107 opts->subsys_bits |= 1ul << i;
1108 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001109 } else if (!strcmp(token, "none")) {
1110 /* Explicitly have no subsystems */
1111 opts->none = true;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001112 } else if (!strcmp(token, "noprefix")) {
1113 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001114 } else if (!strncmp(token, "release_agent=", 14)) {
1115 /* Specifying two release agents is forbidden */
1116 if (opts->release_agent)
1117 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001118 opts->release_agent =
1119 kstrndup(token + 14, PATH_MAX, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001120 if (!opts->release_agent)
1121 return -ENOMEM;
Paul Menagec6d57f32009-09-23 15:56:19 -07001122 } else if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001123 const char *name = token + 5;
1124 /* Can't specify an empty name */
1125 if (!strlen(name))
1126 return -EINVAL;
1127 /* Must match [\w.-]+ */
1128 for (i = 0; i < strlen(name); i++) {
1129 char c = name[i];
1130 if (isalnum(c))
1131 continue;
1132 if ((c == '.') || (c == '-') || (c == '_'))
1133 continue;
1134 return -EINVAL;
1135 }
1136 /* Specifying two names is forbidden */
1137 if (opts->name)
1138 return -EINVAL;
1139 opts->name = kstrndup(name,
1140 MAX_CGROUP_ROOT_NAMELEN,
1141 GFP_KERNEL);
1142 if (!opts->name)
1143 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001144 } else {
1145 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001146 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1147 ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08001148 if (ss == NULL)
1149 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001150 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07001151 if (!ss->disabled)
1152 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001153 break;
1154 }
1155 }
1156 if (i == CGROUP_SUBSYS_COUNT)
1157 return -ENOENT;
1158 }
1159 }
1160
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001161 /* Consistency checks */
1162
Li Zefanf9ab5b52009-06-17 16:26:33 -07001163 /*
1164 * Option noprefix was introduced just for backward compatibility
1165 * with the old cpuset, so we allow noprefix only if mounting just
1166 * the cpuset subsystem.
1167 */
1168 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1169 (opts->subsys_bits & mask))
1170 return -EINVAL;
1171
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001172
1173 /* Can't specify "none" and some subsystems */
1174 if (opts->subsys_bits && opts->none)
1175 return -EINVAL;
1176
1177 /*
1178 * We either have to specify by name or by subsystems. (So all
1179 * empty hierarchies must have a name).
1180 */
Paul Menagec6d57f32009-09-23 15:56:19 -07001181 if (!opts->subsys_bits && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001182 return -EINVAL;
1183
Ben Blumcf5d5942010-03-10 15:22:09 -08001184 /*
1185 * Grab references on all the modules we'll need, so the subsystems
1186 * don't dance around before rebind_subsystems attaches them. This may
1187 * take duplicate reference counts on a subsystem that's already used,
1188 * but rebind_subsystems handles this case.
1189 */
1190 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1191 unsigned long bit = 1UL << i;
1192
1193 if (!(bit & opts->subsys_bits))
1194 continue;
1195 if (!try_module_get(subsys[i]->module)) {
1196 module_pin_failed = true;
1197 break;
1198 }
1199 }
1200 if (module_pin_failed) {
1201 /*
1202 * oops, one of the modules was going away. this means that we
1203 * raced with a module_delete call, and to the user this is
1204 * essentially a "subsystem doesn't exist" case.
1205 */
1206 for (i--; i >= CGROUP_BUILTIN_SUBSYS_COUNT; i--) {
1207 /* drop refcounts only on the ones we took */
1208 unsigned long bit = 1UL << i;
1209
1210 if (!(bit & opts->subsys_bits))
1211 continue;
1212 module_put(subsys[i]->module);
1213 }
1214 return -ENOENT;
1215 }
1216
Paul Menageddbcc7e2007-10-18 23:39:30 -07001217 return 0;
1218}
1219
Ben Blumcf5d5942010-03-10 15:22:09 -08001220static void drop_parsed_module_refcounts(unsigned long subsys_bits)
1221{
1222 int i;
1223 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1224 unsigned long bit = 1UL << i;
1225
1226 if (!(bit & subsys_bits))
1227 continue;
1228 module_put(subsys[i]->module);
1229 }
1230}
1231
Paul Menageddbcc7e2007-10-18 23:39:30 -07001232static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1233{
1234 int ret = 0;
1235 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001236 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001237 struct cgroup_sb_opts opts;
1238
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001239 lock_kernel();
Paul Menagebd89aab2007-10-18 23:40:44 -07001240 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001241 mutex_lock(&cgroup_mutex);
1242
1243 /* See what subsystems are wanted */
1244 ret = parse_cgroupfs_options(data, &opts);
1245 if (ret)
1246 goto out_unlock;
1247
Ben Blumcf5d5942010-03-10 15:22:09 -08001248 /* Don't allow flags or name to change at remount */
1249 if (opts.flags != root->flags ||
1250 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001251 ret = -EINVAL;
Ben Blumcf5d5942010-03-10 15:22:09 -08001252 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menagec6d57f32009-09-23 15:56:19 -07001253 goto out_unlock;
1254 }
1255
Paul Menageddbcc7e2007-10-18 23:39:30 -07001256 ret = rebind_subsystems(root, opts.subsys_bits);
Ben Blumcf5d5942010-03-10 15:22:09 -08001257 if (ret) {
1258 drop_parsed_module_refcounts(opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -07001259 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001260 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001261
1262 /* (re)populate subsystem files */
Li Zefan0670e082009-04-02 16:57:30 -07001263 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001264
Paul Menage81a6a5c2007-10-18 23:39:38 -07001265 if (opts.release_agent)
1266 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001267 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001268 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001269 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001270 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001271 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001272 unlock_kernel();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001273 return ret;
1274}
1275
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001276static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001277 .statfs = simple_statfs,
1278 .drop_inode = generic_delete_inode,
1279 .show_options = cgroup_show_options,
1280 .remount_fs = cgroup_remount,
1281};
1282
Paul Menagecc31edc2008-10-18 20:28:04 -07001283static void init_cgroup_housekeeping(struct cgroup *cgrp)
1284{
1285 INIT_LIST_HEAD(&cgrp->sibling);
1286 INIT_LIST_HEAD(&cgrp->children);
1287 INIT_LIST_HEAD(&cgrp->css_sets);
1288 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001289 INIT_LIST_HEAD(&cgrp->pidlists);
1290 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001291 INIT_LIST_HEAD(&cgrp->event_list);
1292 spin_lock_init(&cgrp->event_list_lock);
Paul Menagecc31edc2008-10-18 20:28:04 -07001293}
Paul Menagec6d57f32009-09-23 15:56:19 -07001294
Paul Menageddbcc7e2007-10-18 23:39:30 -07001295static void init_cgroup_root(struct cgroupfs_root *root)
1296{
Paul Menagebd89aab2007-10-18 23:40:44 -07001297 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001298 INIT_LIST_HEAD(&root->subsys_list);
1299 INIT_LIST_HEAD(&root->root_list);
1300 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001301 cgrp->root = root;
1302 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001303 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304}
1305
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001306static bool init_root_id(struct cgroupfs_root *root)
1307{
1308 int ret = 0;
1309
1310 do {
1311 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1312 return false;
1313 spin_lock(&hierarchy_id_lock);
1314 /* Try to allocate the next unused ID */
1315 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1316 &root->hierarchy_id);
1317 if (ret == -ENOSPC)
1318 /* Try again starting from 0 */
1319 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1320 if (!ret) {
1321 next_hierarchy_id = root->hierarchy_id + 1;
1322 } else if (ret != -EAGAIN) {
1323 /* Can only get here if the 31-bit IDR is full ... */
1324 BUG_ON(ret);
1325 }
1326 spin_unlock(&hierarchy_id_lock);
1327 } while (ret);
1328 return true;
1329}
1330
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331static int cgroup_test_super(struct super_block *sb, void *data)
1332{
Paul Menagec6d57f32009-09-23 15:56:19 -07001333 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001334 struct cgroupfs_root *root = sb->s_fs_info;
1335
Paul Menagec6d57f32009-09-23 15:56:19 -07001336 /* If we asked for a name then it must match */
1337 if (opts->name && strcmp(opts->name, root->name))
1338 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001339
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001340 /*
1341 * If we asked for subsystems (or explicitly for no
1342 * subsystems) then they must match
1343 */
1344 if ((opts->subsys_bits || opts->none)
1345 && (opts->subsys_bits != root->subsys_bits))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346 return 0;
1347
1348 return 1;
1349}
1350
Paul Menagec6d57f32009-09-23 15:56:19 -07001351static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1352{
1353 struct cgroupfs_root *root;
1354
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001355 if (!opts->subsys_bits && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001356 return NULL;
1357
1358 root = kzalloc(sizeof(*root), GFP_KERNEL);
1359 if (!root)
1360 return ERR_PTR(-ENOMEM);
1361
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001362 if (!init_root_id(root)) {
1363 kfree(root);
1364 return ERR_PTR(-ENOMEM);
1365 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001366 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001367
Paul Menagec6d57f32009-09-23 15:56:19 -07001368 root->subsys_bits = opts->subsys_bits;
1369 root->flags = opts->flags;
1370 if (opts->release_agent)
1371 strcpy(root->release_agent_path, opts->release_agent);
1372 if (opts->name)
1373 strcpy(root->name, opts->name);
1374 return root;
1375}
1376
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001377static void cgroup_drop_root(struct cgroupfs_root *root)
1378{
1379 if (!root)
1380 return;
1381
1382 BUG_ON(!root->hierarchy_id);
1383 spin_lock(&hierarchy_id_lock);
1384 ida_remove(&hierarchy_ida, root->hierarchy_id);
1385 spin_unlock(&hierarchy_id_lock);
1386 kfree(root);
1387}
1388
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389static int cgroup_set_super(struct super_block *sb, void *data)
1390{
1391 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001392 struct cgroup_sb_opts *opts = data;
1393
1394 /* If we don't have a new root, we can't set up a new sb */
1395 if (!opts->new_root)
1396 return -EINVAL;
1397
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001398 BUG_ON(!opts->subsys_bits && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001399
1400 ret = set_anon_super(sb, NULL);
1401 if (ret)
1402 return ret;
1403
Paul Menagec6d57f32009-09-23 15:56:19 -07001404 sb->s_fs_info = opts->new_root;
1405 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001406
1407 sb->s_blocksize = PAGE_CACHE_SIZE;
1408 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1409 sb->s_magic = CGROUP_SUPER_MAGIC;
1410 sb->s_op = &cgroup_ops;
1411
1412 return 0;
1413}
1414
1415static int cgroup_get_rootdir(struct super_block *sb)
1416{
1417 struct inode *inode =
1418 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1419 struct dentry *dentry;
1420
1421 if (!inode)
1422 return -ENOMEM;
1423
Paul Menageddbcc7e2007-10-18 23:39:30 -07001424 inode->i_fop = &simple_dir_operations;
1425 inode->i_op = &cgroup_dir_inode_operations;
1426 /* directories start off with i_nlink == 2 (for "." entry) */
1427 inc_nlink(inode);
1428 dentry = d_alloc_root(inode);
1429 if (!dentry) {
1430 iput(inode);
1431 return -ENOMEM;
1432 }
1433 sb->s_root = dentry;
1434 return 0;
1435}
1436
1437static int cgroup_get_sb(struct file_system_type *fs_type,
1438 int flags, const char *unused_dev_name,
1439 void *data, struct vfsmount *mnt)
1440{
1441 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001442 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001443 int ret = 0;
1444 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001445 struct cgroupfs_root *new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001446
1447 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001448 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001449 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001450 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001451 if (ret)
1452 goto out_err;
1453
1454 /*
1455 * Allocate a new cgroup root. We may not need it if we're
1456 * reusing an existing hierarchy.
1457 */
1458 new_root = cgroup_root_from_opts(&opts);
1459 if (IS_ERR(new_root)) {
1460 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001461 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001462 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001463 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001464
Paul Menagec6d57f32009-09-23 15:56:19 -07001465 /* Locate an existing or new sb for this hierarchy */
1466 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001467 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001468 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001469 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001470 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001471 }
1472
Paul Menagec6d57f32009-09-23 15:56:19 -07001473 root = sb->s_fs_info;
1474 BUG_ON(!root);
1475 if (root == opts.new_root) {
1476 /* We used the new root structure, so this is a new hierarchy */
1477 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001478 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001479 struct inode *inode;
Paul Menagec6d57f32009-09-23 15:56:19 -07001480 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001481 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001482
1483 BUG_ON(sb->s_root != NULL);
1484
1485 ret = cgroup_get_rootdir(sb);
1486 if (ret)
1487 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001488 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001489
Paul Menage817929e2007-10-18 23:39:36 -07001490 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001491 mutex_lock(&cgroup_mutex);
1492
Paul Menagec6d57f32009-09-23 15:56:19 -07001493 if (strlen(root->name)) {
1494 /* Check for name clashes with existing mounts */
1495 for_each_active_root(existing_root) {
1496 if (!strcmp(existing_root->name, root->name)) {
1497 ret = -EBUSY;
1498 mutex_unlock(&cgroup_mutex);
1499 mutex_unlock(&inode->i_mutex);
1500 goto drop_new_super;
1501 }
1502 }
1503 }
1504
Paul Menage817929e2007-10-18 23:39:36 -07001505 /*
1506 * We're accessing css_set_count without locking
1507 * css_set_lock here, but that's OK - it can only be
1508 * increased by someone holding cgroup_lock, and
1509 * that's us. The worst that can happen is that we
1510 * have some link structures left over
1511 */
1512 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1513 if (ret) {
1514 mutex_unlock(&cgroup_mutex);
1515 mutex_unlock(&inode->i_mutex);
1516 goto drop_new_super;
1517 }
1518
Paul Menageddbcc7e2007-10-18 23:39:30 -07001519 ret = rebind_subsystems(root, root->subsys_bits);
1520 if (ret == -EBUSY) {
1521 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001522 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001523 free_cg_links(&tmp_cg_links);
1524 goto drop_new_super;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001525 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001526 /*
1527 * There must be no failure case after here, since rebinding
1528 * takes care of subsystems' refcounts, which are explicitly
1529 * dropped in the failure exit path.
1530 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531
1532 /* EBUSY should be the only error here */
1533 BUG_ON(ret);
1534
1535 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001536 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537
Li Zefanc12f65d2009-01-07 18:07:42 -08001538 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539 root->top_cgroup.dentry = sb->s_root;
1540
Paul Menage817929e2007-10-18 23:39:36 -07001541 /* Link the top cgroup in this hierarchy into all
1542 * the css_set objects */
1543 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001544 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1545 struct hlist_head *hhead = &css_set_table[i];
1546 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001547 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001548
Li Zefanc12f65d2009-01-07 18:07:42 -08001549 hlist_for_each_entry(cg, node, hhead, hlist)
1550 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001551 }
Paul Menage817929e2007-10-18 23:39:36 -07001552 write_unlock(&css_set_lock);
1553
1554 free_cg_links(&tmp_cg_links);
1555
Li Zefanc12f65d2009-01-07 18:07:42 -08001556 BUG_ON(!list_empty(&root_cgrp->sibling));
1557 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558 BUG_ON(root->number_of_cgroups != 1);
1559
Li Zefanc12f65d2009-01-07 18:07:42 -08001560 cgroup_populate_dir(root_cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001562 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001563 } else {
1564 /*
1565 * We re-used an existing hierarchy - the new root (if
1566 * any) is not needed
1567 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001568 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001569 /* no subsys rebinding, so refcounts don't change */
1570 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571 }
1572
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001573 simple_set_mnt(mnt, sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001574 kfree(opts.release_agent);
1575 kfree(opts.name);
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001576 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577
1578 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001579 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001580 drop_modules:
1581 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menagec6d57f32009-09-23 15:56:19 -07001582 out_err:
1583 kfree(opts.release_agent);
1584 kfree(opts.name);
1585
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586 return ret;
1587}
1588
1589static void cgroup_kill_sb(struct super_block *sb) {
1590 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001591 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001593 struct cg_cgroup_link *link;
1594 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595
1596 BUG_ON(!root);
1597
1598 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001599 BUG_ON(!list_empty(&cgrp->children));
1600 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001601
1602 mutex_lock(&cgroup_mutex);
1603
1604 /* Rebind all subsystems back to the default hierarchy */
1605 ret = rebind_subsystems(root, 0);
1606 /* Shouldn't be able to fail ... */
1607 BUG_ON(ret);
1608
Paul Menage817929e2007-10-18 23:39:36 -07001609 /*
1610 * Release all the links from css_sets to this hierarchy's
1611 * root cgroup
1612 */
1613 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001614
1615 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1616 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001617 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001618 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001619 kfree(link);
1620 }
1621 write_unlock(&css_set_lock);
1622
Paul Menage839ec542009-01-29 14:25:22 -08001623 if (!list_empty(&root->root_list)) {
1624 list_del(&root->root_list);
1625 root_count--;
1626 }
Li Zefane5f6a862009-01-07 18:07:41 -08001627
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628 mutex_unlock(&cgroup_mutex);
1629
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001631 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632}
1633
1634static struct file_system_type cgroup_fs_type = {
1635 .name = "cgroup",
1636 .get_sb = cgroup_get_sb,
1637 .kill_sb = cgroup_kill_sb,
1638};
1639
Paul Menagebd89aab2007-10-18 23:40:44 -07001640static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001641{
1642 return dentry->d_fsdata;
1643}
1644
1645static inline struct cftype *__d_cft(struct dentry *dentry)
1646{
1647 return dentry->d_fsdata;
1648}
1649
Li Zefana043e3b2008-02-23 15:24:09 -08001650/**
1651 * cgroup_path - generate the path of a cgroup
1652 * @cgrp: the cgroup in question
1653 * @buf: the buffer to write the path into
1654 * @buflen: the length of the buffer
1655 *
Paul Menagea47295e2009-01-07 18:07:44 -08001656 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1657 * reference. Writes path of cgroup into buf. Returns 0 on success,
1658 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001660int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001661{
1662 char *start;
Paul Menagea47295e2009-01-07 18:07:44 -08001663 struct dentry *dentry = rcu_dereference(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664
Paul Menagea47295e2009-01-07 18:07:44 -08001665 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001666 /*
1667 * Inactive subsystems have no dentry for their root
1668 * cgroup
1669 */
1670 strcpy(buf, "/");
1671 return 0;
1672 }
1673
1674 start = buf + buflen;
1675
1676 *--start = '\0';
1677 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001678 int len = dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001679 if ((start -= len) < buf)
1680 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001681 memcpy(start, cgrp->dentry->d_name.name, len);
1682 cgrp = cgrp->parent;
1683 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684 break;
Paul Menagea47295e2009-01-07 18:07:44 -08001685 dentry = rcu_dereference(cgrp->dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001686 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001687 continue;
1688 if (--start < buf)
1689 return -ENAMETOOLONG;
1690 *start = '/';
1691 }
1692 memmove(buf, start, buf + buflen - start);
1693 return 0;
1694}
Ben Blum67523c42010-03-10 15:22:11 -08001695EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696
Li Zefana043e3b2008-02-23 15:24:09 -08001697/**
1698 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1699 * @cgrp: the cgroup the task is attaching to
1700 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001701 *
Li Zefana043e3b2008-02-23 15:24:09 -08001702 * Call holding cgroup_mutex. May take task_lock of
1703 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001704 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001705int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001706{
1707 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001708 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001709 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001710 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001711 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001712 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001713
1714 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001715 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001716 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001717 return 0;
1718
1719 for_each_subsys(root, ss) {
1720 if (ss->can_attach) {
Ben Blumbe367d02009-09-23 15:56:31 -07001721 retval = ss->can_attach(ss, cgrp, tsk, false);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001722 if (retval) {
1723 /*
1724 * Remember on which subsystem the can_attach()
1725 * failed, so that we only call cancel_attach()
1726 * against the subsystems whose can_attach()
1727 * succeeded. (See below)
1728 */
1729 failed_ss = ss;
1730 goto out;
1731 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001732 }
1733 }
1734
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001735 task_lock(tsk);
1736 cg = tsk->cgroups;
1737 get_css_set(cg);
1738 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001739 /*
1740 * Locate or allocate a new css_set for this task,
1741 * based on its final set of cgroups
1742 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001743 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001744 put_css_set(cg);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001745 if (!newcg) {
1746 retval = -ENOMEM;
1747 goto out;
1748 }
Paul Menage817929e2007-10-18 23:39:36 -07001749
Paul Menagebbcb81d2007-10-18 23:39:32 -07001750 task_lock(tsk);
1751 if (tsk->flags & PF_EXITING) {
1752 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001753 put_css_set(newcg);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001754 retval = -ESRCH;
1755 goto out;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001756 }
Paul Menage817929e2007-10-18 23:39:36 -07001757 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001758 task_unlock(tsk);
1759
Paul Menage817929e2007-10-18 23:39:36 -07001760 /* Update the css_set linked lists if we're using them */
1761 write_lock(&css_set_lock);
1762 if (!list_empty(&tsk->cg_list)) {
1763 list_del(&tsk->cg_list);
1764 list_add(&tsk->cg_list, &newcg->tasks);
1765 }
1766 write_unlock(&css_set_lock);
1767
Paul Menagebbcb81d2007-10-18 23:39:32 -07001768 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001769 if (ss->attach)
Ben Blumbe367d02009-09-23 15:56:31 -07001770 ss->attach(ss, cgrp, oldcgrp, tsk, false);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001771 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001772 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001773 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001774 put_css_set(cg);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001775
1776 /*
1777 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1778 * is no longer empty.
1779 */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001780 cgroup_wakeup_rmdir_waiter(cgrp);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001781out:
1782 if (retval) {
1783 for_each_subsys(root, ss) {
1784 if (ss == failed_ss)
1785 /*
1786 * This subsystem was the one that failed the
1787 * can_attach() check earlier, so we don't need
1788 * to call cancel_attach() against it or any
1789 * remaining subsystems.
1790 */
1791 break;
1792 if (ss->cancel_attach)
1793 ss->cancel_attach(ss, cgrp, tsk, false);
1794 }
1795 }
1796 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001797}
1798
1799/*
Paul Menageaf351022008-07-25 01:47:01 -07001800 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1801 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001802 */
Paul Menageaf351022008-07-25 01:47:01 -07001803static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001804{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001805 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001806 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001807 int ret;
1808
Paul Menagebbcb81d2007-10-18 23:39:32 -07001809 if (pid) {
1810 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001811 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001812 if (!tsk || tsk->flags & PF_EXITING) {
1813 rcu_read_unlock();
1814 return -ESRCH;
1815 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001816
David Howellsc69e8d92008-11-14 10:39:19 +11001817 tcred = __task_cred(tsk);
1818 if (cred->euid &&
1819 cred->euid != tcred->uid &&
1820 cred->euid != tcred->suid) {
1821 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001822 return -EACCES;
1823 }
David Howellsc69e8d92008-11-14 10:39:19 +11001824 get_task_struct(tsk);
1825 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001826 } else {
1827 tsk = current;
1828 get_task_struct(tsk);
1829 }
1830
Cliff Wickman956db3c2008-02-07 00:14:43 -08001831 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001832 put_task_struct(tsk);
1833 return ret;
1834}
1835
Paul Menageaf351022008-07-25 01:47:01 -07001836static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1837{
1838 int ret;
1839 if (!cgroup_lock_live_group(cgrp))
1840 return -ENODEV;
1841 ret = attach_task_by_pid(cgrp, pid);
1842 cgroup_unlock();
1843 return ret;
1844}
1845
Paul Menagee788e0662008-07-25 01:46:59 -07001846/**
1847 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1848 * @cgrp: the cgroup to be checked for liveness
1849 *
Paul Menage84eea842008-07-25 01:47:00 -07001850 * On success, returns true; the lock should be later released with
1851 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e0662008-07-25 01:46:59 -07001852 */
Paul Menage84eea842008-07-25 01:47:00 -07001853bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e0662008-07-25 01:46:59 -07001854{
1855 mutex_lock(&cgroup_mutex);
1856 if (cgroup_is_removed(cgrp)) {
1857 mutex_unlock(&cgroup_mutex);
1858 return false;
1859 }
1860 return true;
1861}
Ben Blum67523c42010-03-10 15:22:11 -08001862EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e0662008-07-25 01:46:59 -07001863
1864static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1865 const char *buffer)
1866{
1867 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1868 if (!cgroup_lock_live_group(cgrp))
1869 return -ENODEV;
1870 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001871 cgroup_unlock();
Paul Menagee788e0662008-07-25 01:46:59 -07001872 return 0;
1873}
1874
1875static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1876 struct seq_file *seq)
1877{
1878 if (!cgroup_lock_live_group(cgrp))
1879 return -ENODEV;
1880 seq_puts(seq, cgrp->root->release_agent_path);
1881 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001882 cgroup_unlock();
Paul Menagee788e0662008-07-25 01:46:59 -07001883 return 0;
1884}
1885
Paul Menage84eea842008-07-25 01:47:00 -07001886/* A buffer size big enough for numbers or short strings */
1887#define CGROUP_LOCAL_BUFFER_SIZE 64
1888
Paul Menagee73d2c62008-04-29 01:00:06 -07001889static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001890 struct file *file,
1891 const char __user *userbuf,
1892 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001893{
Paul Menage84eea842008-07-25 01:47:00 -07001894 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001895 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001896 char *end;
1897
1898 if (!nbytes)
1899 return -EINVAL;
1900 if (nbytes >= sizeof(buffer))
1901 return -E2BIG;
1902 if (copy_from_user(buffer, userbuf, nbytes))
1903 return -EFAULT;
1904
1905 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07001906 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001907 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07001908 if (*end)
1909 return -EINVAL;
1910 retval = cft->write_u64(cgrp, cft, val);
1911 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001912 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07001913 if (*end)
1914 return -EINVAL;
1915 retval = cft->write_s64(cgrp, cft, val);
1916 }
Paul Menage355e0c42007-10-18 23:39:33 -07001917 if (!retval)
1918 retval = nbytes;
1919 return retval;
1920}
1921
Paul Menagedb3b1492008-07-25 01:46:58 -07001922static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1923 struct file *file,
1924 const char __user *userbuf,
1925 size_t nbytes, loff_t *unused_ppos)
1926{
Paul Menage84eea842008-07-25 01:47:00 -07001927 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001928 int retval = 0;
1929 size_t max_bytes = cft->max_write_len;
1930 char *buffer = local_buffer;
1931
1932 if (!max_bytes)
1933 max_bytes = sizeof(local_buffer) - 1;
1934 if (nbytes >= max_bytes)
1935 return -E2BIG;
1936 /* Allocate a dynamic buffer if we need one */
1937 if (nbytes >= sizeof(local_buffer)) {
1938 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1939 if (buffer == NULL)
1940 return -ENOMEM;
1941 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001942 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1943 retval = -EFAULT;
1944 goto out;
1945 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001946
1947 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001948 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07001949 if (!retval)
1950 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001951out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001952 if (buffer != local_buffer)
1953 kfree(buffer);
1954 return retval;
1955}
1956
Paul Menageddbcc7e2007-10-18 23:39:30 -07001957static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1958 size_t nbytes, loff_t *ppos)
1959{
1960 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001961 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001962
Li Zefan75139b82009-01-07 18:07:33 -08001963 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001964 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001965 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001966 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001967 if (cft->write_u64 || cft->write_s64)
1968 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001969 if (cft->write_string)
1970 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001971 if (cft->trigger) {
1972 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1973 return ret ? ret : nbytes;
1974 }
Paul Menage355e0c42007-10-18 23:39:33 -07001975 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001976}
1977
Paul Menagef4c753b2008-04-29 00:59:56 -07001978static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1979 struct file *file,
1980 char __user *buf, size_t nbytes,
1981 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001982{
Paul Menage84eea842008-07-25 01:47:00 -07001983 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001984 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001985 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1986
1987 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1988}
1989
Paul Menagee73d2c62008-04-29 01:00:06 -07001990static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1991 struct file *file,
1992 char __user *buf, size_t nbytes,
1993 loff_t *ppos)
1994{
Paul Menage84eea842008-07-25 01:47:00 -07001995 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001996 s64 val = cft->read_s64(cgrp, cft);
1997 int len = sprintf(tmp, "%lld\n", (long long) val);
1998
1999 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2000}
2001
Paul Menageddbcc7e2007-10-18 23:39:30 -07002002static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2003 size_t nbytes, loff_t *ppos)
2004{
2005 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002006 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002007
Li Zefan75139b82009-01-07 18:07:33 -08002008 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002009 return -ENODEV;
2010
2011 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002012 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002013 if (cft->read_u64)
2014 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002015 if (cft->read_s64)
2016 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002017 return -EINVAL;
2018}
2019
Paul Menage91796562008-04-29 01:00:01 -07002020/*
2021 * seqfile ops/methods for returning structured data. Currently just
2022 * supports string->u64 maps, but can be extended in future.
2023 */
2024
2025struct cgroup_seqfile_state {
2026 struct cftype *cft;
2027 struct cgroup *cgroup;
2028};
2029
2030static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2031{
2032 struct seq_file *sf = cb->state;
2033 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2034}
2035
2036static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2037{
2038 struct cgroup_seqfile_state *state = m->private;
2039 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002040 if (cft->read_map) {
2041 struct cgroup_map_cb cb = {
2042 .fill = cgroup_map_add,
2043 .state = m,
2044 };
2045 return cft->read_map(state->cgroup, cft, &cb);
2046 }
2047 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002048}
2049
Adrian Bunk96930a62008-07-25 19:46:21 -07002050static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002051{
2052 struct seq_file *seq = file->private_data;
2053 kfree(seq->private);
2054 return single_release(inode, file);
2055}
2056
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002057static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002058 .read = seq_read,
Paul Menagee788e0662008-07-25 01:46:59 -07002059 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002060 .llseek = seq_lseek,
2061 .release = cgroup_seqfile_release,
2062};
2063
Paul Menageddbcc7e2007-10-18 23:39:30 -07002064static int cgroup_file_open(struct inode *inode, struct file *file)
2065{
2066 int err;
2067 struct cftype *cft;
2068
2069 err = generic_file_open(inode, file);
2070 if (err)
2071 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002072 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002073
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002074 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002075 struct cgroup_seqfile_state *state =
2076 kzalloc(sizeof(*state), GFP_USER);
2077 if (!state)
2078 return -ENOMEM;
2079 state->cft = cft;
2080 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2081 file->f_op = &cgroup_seqfile_operations;
2082 err = single_open(file, cgroup_seqfile_show, state);
2083 if (err < 0)
2084 kfree(state);
2085 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002086 err = cft->open(inode, file);
2087 else
2088 err = 0;
2089
2090 return err;
2091}
2092
2093static int cgroup_file_release(struct inode *inode, struct file *file)
2094{
2095 struct cftype *cft = __d_cft(file->f_dentry);
2096 if (cft->release)
2097 return cft->release(inode, file);
2098 return 0;
2099}
2100
2101/*
2102 * cgroup_rename - Only allow simple rename of directories in place.
2103 */
2104static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2105 struct inode *new_dir, struct dentry *new_dentry)
2106{
2107 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2108 return -ENOTDIR;
2109 if (new_dentry->d_inode)
2110 return -EEXIST;
2111 if (old_dir != new_dir)
2112 return -EIO;
2113 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2114}
2115
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002116static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002117 .read = cgroup_file_read,
2118 .write = cgroup_file_write,
2119 .llseek = generic_file_llseek,
2120 .open = cgroup_file_open,
2121 .release = cgroup_file_release,
2122};
2123
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002124static const struct inode_operations cgroup_dir_inode_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002125 .lookup = simple_lookup,
2126 .mkdir = cgroup_mkdir,
2127 .rmdir = cgroup_rmdir,
2128 .rename = cgroup_rename,
2129};
2130
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002131/*
2132 * Check if a file is a control file
2133 */
2134static inline struct cftype *__file_cft(struct file *file)
2135{
2136 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2137 return ERR_PTR(-EINVAL);
2138 return __d_cft(file->f_dentry);
2139}
2140
Li Zefan099fca32009-04-02 16:57:29 -07002141static int cgroup_create_file(struct dentry *dentry, mode_t mode,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002142 struct super_block *sb)
2143{
Al Viro3ba13d12009-02-20 06:02:22 +00002144 static const struct dentry_operations cgroup_dops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002145 .d_iput = cgroup_diput,
2146 };
2147
2148 struct inode *inode;
2149
2150 if (!dentry)
2151 return -ENOENT;
2152 if (dentry->d_inode)
2153 return -EEXIST;
2154
2155 inode = cgroup_new_inode(mode, sb);
2156 if (!inode)
2157 return -ENOMEM;
2158
2159 if (S_ISDIR(mode)) {
2160 inode->i_op = &cgroup_dir_inode_operations;
2161 inode->i_fop = &simple_dir_operations;
2162
2163 /* start off with i_nlink == 2 (for "." entry) */
2164 inc_nlink(inode);
2165
2166 /* start with the directory inode held, so that we can
2167 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07002168 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002169 } else if (S_ISREG(mode)) {
2170 inode->i_size = 0;
2171 inode->i_fop = &cgroup_file_operations;
2172 }
2173 dentry->d_op = &cgroup_dops;
2174 d_instantiate(dentry, inode);
2175 dget(dentry); /* Extra count - pin the dentry in core */
2176 return 0;
2177}
2178
2179/*
Li Zefana043e3b2008-02-23 15:24:09 -08002180 * cgroup_create_dir - create a directory for an object.
2181 * @cgrp: the cgroup we create the directory for. It must have a valid
2182 * ->parent field. And we are going to fill its ->dentry field.
2183 * @dentry: dentry of the new cgroup
2184 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002185 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002186static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002187 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002188{
2189 struct dentry *parent;
2190 int error = 0;
2191
Paul Menagebd89aab2007-10-18 23:40:44 -07002192 parent = cgrp->parent->dentry;
2193 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002194 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002195 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002196 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08002197 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002198 dget(dentry);
2199 }
2200 dput(dentry);
2201
2202 return error;
2203}
2204
Li Zefan099fca32009-04-02 16:57:29 -07002205/**
2206 * cgroup_file_mode - deduce file mode of a control file
2207 * @cft: the control file in question
2208 *
2209 * returns cft->mode if ->mode is not 0
2210 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2211 * returns S_IRUGO if it has only a read handler
2212 * returns S_IWUSR if it has only a write hander
2213 */
2214static mode_t cgroup_file_mode(const struct cftype *cft)
2215{
2216 mode_t mode = 0;
2217
2218 if (cft->mode)
2219 return cft->mode;
2220
2221 if (cft->read || cft->read_u64 || cft->read_s64 ||
2222 cft->read_map || cft->read_seq_string)
2223 mode |= S_IRUGO;
2224
2225 if (cft->write || cft->write_u64 || cft->write_s64 ||
2226 cft->write_string || cft->trigger)
2227 mode |= S_IWUSR;
2228
2229 return mode;
2230}
2231
Paul Menagebd89aab2007-10-18 23:40:44 -07002232int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002233 struct cgroup_subsys *subsys,
2234 const struct cftype *cft)
2235{
Paul Menagebd89aab2007-10-18 23:40:44 -07002236 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002237 struct dentry *dentry;
2238 int error;
Li Zefan099fca32009-04-02 16:57:29 -07002239 mode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002240
2241 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07002242 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002243 strcpy(name, subsys->name);
2244 strcat(name, ".");
2245 }
2246 strcat(name, cft->name);
2247 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2248 dentry = lookup_one_len(name, dir, strlen(name));
2249 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07002250 mode = cgroup_file_mode(cft);
2251 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07002252 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002253 if (!error)
2254 dentry->d_fsdata = (void *)cft;
2255 dput(dentry);
2256 } else
2257 error = PTR_ERR(dentry);
2258 return error;
2259}
Ben Blume6a11052010-03-10 15:22:09 -08002260EXPORT_SYMBOL_GPL(cgroup_add_file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002261
Paul Menagebd89aab2007-10-18 23:40:44 -07002262int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002263 struct cgroup_subsys *subsys,
2264 const struct cftype cft[],
2265 int count)
2266{
2267 int i, err;
2268 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002269 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002270 if (err)
2271 return err;
2272 }
2273 return 0;
2274}
Ben Blume6a11052010-03-10 15:22:09 -08002275EXPORT_SYMBOL_GPL(cgroup_add_files);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002276
Li Zefana043e3b2008-02-23 15:24:09 -08002277/**
2278 * cgroup_task_count - count the number of tasks in a cgroup.
2279 * @cgrp: the cgroup in question
2280 *
2281 * Return the number of tasks in the cgroup.
2282 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002283int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002284{
2285 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002286 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002287
Paul Menage817929e2007-10-18 23:39:36 -07002288 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002289 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002290 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002291 }
2292 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002293 return count;
2294}
2295
2296/*
Paul Menage817929e2007-10-18 23:39:36 -07002297 * Advance a list_head iterator. The iterator should be positioned at
2298 * the start of a css_set
2299 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002300static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002301 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002302{
2303 struct list_head *l = it->cg_link;
2304 struct cg_cgroup_link *link;
2305 struct css_set *cg;
2306
2307 /* Advance to the next non-empty css_set */
2308 do {
2309 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002310 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002311 it->cg_link = NULL;
2312 return;
2313 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002314 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002315 cg = link->cg;
2316 } while (list_empty(&cg->tasks));
2317 it->cg_link = l;
2318 it->task = cg->tasks.next;
2319}
2320
Cliff Wickman31a7df02008-02-07 00:14:42 -08002321/*
2322 * To reduce the fork() overhead for systems that are not actually
2323 * using their cgroups capability, we don't maintain the lists running
2324 * through each css_set to its tasks until we see the list actually
2325 * used - in other words after the first call to cgroup_iter_start().
2326 *
2327 * The tasklist_lock is not held here, as do_each_thread() and
2328 * while_each_thread() are protected by RCU.
2329 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002330static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002331{
2332 struct task_struct *p, *g;
2333 write_lock(&css_set_lock);
2334 use_task_css_set_links = 1;
2335 do_each_thread(g, p) {
2336 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002337 /*
2338 * We should check if the process is exiting, otherwise
2339 * it will race with cgroup_exit() in that the list
2340 * entry won't be deleted though the process has exited.
2341 */
2342 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002343 list_add(&p->cg_list, &p->cgroups->tasks);
2344 task_unlock(p);
2345 } while_each_thread(g, p);
2346 write_unlock(&css_set_lock);
2347}
2348
Paul Menagebd89aab2007-10-18 23:40:44 -07002349void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002350{
2351 /*
2352 * The first time anyone tries to iterate across a cgroup,
2353 * we need to enable the list linking each css_set to its
2354 * tasks, and fix up all existing tasks.
2355 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002356 if (!use_task_css_set_links)
2357 cgroup_enable_task_cg_lists();
2358
Paul Menage817929e2007-10-18 23:39:36 -07002359 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002360 it->cg_link = &cgrp->css_sets;
2361 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002362}
2363
Paul Menagebd89aab2007-10-18 23:40:44 -07002364struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07002365 struct cgroup_iter *it)
2366{
2367 struct task_struct *res;
2368 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002369 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002370
2371 /* If the iterator cg is NULL, we have no tasks */
2372 if (!it->cg_link)
2373 return NULL;
2374 res = list_entry(l, struct task_struct, cg_list);
2375 /* Advance iterator to find next entry */
2376 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002377 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2378 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07002379 /* We reached the end of this task list - move on to
2380 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07002381 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002382 } else {
2383 it->task = l;
2384 }
2385 return res;
2386}
2387
Paul Menagebd89aab2007-10-18 23:40:44 -07002388void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002389{
2390 read_unlock(&css_set_lock);
2391}
2392
Cliff Wickman31a7df02008-02-07 00:14:42 -08002393static inline int started_after_time(struct task_struct *t1,
2394 struct timespec *time,
2395 struct task_struct *t2)
2396{
2397 int start_diff = timespec_compare(&t1->start_time, time);
2398 if (start_diff > 0) {
2399 return 1;
2400 } else if (start_diff < 0) {
2401 return 0;
2402 } else {
2403 /*
2404 * Arbitrarily, if two processes started at the same
2405 * time, we'll say that the lower pointer value
2406 * started first. Note that t2 may have exited by now
2407 * so this may not be a valid pointer any longer, but
2408 * that's fine - it still serves to distinguish
2409 * between two tasks started (effectively) simultaneously.
2410 */
2411 return t1 > t2;
2412 }
2413}
2414
2415/*
2416 * This function is a callback from heap_insert() and is used to order
2417 * the heap.
2418 * In this case we order the heap in descending task start time.
2419 */
2420static inline int started_after(void *p1, void *p2)
2421{
2422 struct task_struct *t1 = p1;
2423 struct task_struct *t2 = p2;
2424 return started_after_time(t1, &t2->start_time, t2);
2425}
2426
2427/**
2428 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2429 * @scan: struct cgroup_scanner containing arguments for the scan
2430 *
2431 * Arguments include pointers to callback functions test_task() and
2432 * process_task().
2433 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2434 * and if it returns true, call process_task() for it also.
2435 * The test_task pointer may be NULL, meaning always true (select all tasks).
2436 * Effectively duplicates cgroup_iter_{start,next,end}()
2437 * but does not lock css_set_lock for the call to process_task().
2438 * The struct cgroup_scanner may be embedded in any structure of the caller's
2439 * creation.
2440 * It is guaranteed that process_task() will act on every task that
2441 * is a member of the cgroup for the duration of this call. This
2442 * function may or may not call process_task() for tasks that exit
2443 * or move to a different cgroup during the call, or are forked or
2444 * move into the cgroup during the call.
2445 *
2446 * Note that test_task() may be called with locks held, and may in some
2447 * situations be called multiple times for the same task, so it should
2448 * be cheap.
2449 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2450 * pre-allocated and will be used for heap operations (and its "gt" member will
2451 * be overwritten), else a temporary heap will be used (allocation of which
2452 * may cause this function to fail).
2453 */
2454int cgroup_scan_tasks(struct cgroup_scanner *scan)
2455{
2456 int retval, i;
2457 struct cgroup_iter it;
2458 struct task_struct *p, *dropped;
2459 /* Never dereference latest_task, since it's not refcounted */
2460 struct task_struct *latest_task = NULL;
2461 struct ptr_heap tmp_heap;
2462 struct ptr_heap *heap;
2463 struct timespec latest_time = { 0, 0 };
2464
2465 if (scan->heap) {
2466 /* The caller supplied our heap and pre-allocated its memory */
2467 heap = scan->heap;
2468 heap->gt = &started_after;
2469 } else {
2470 /* We need to allocate our own heap memory */
2471 heap = &tmp_heap;
2472 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2473 if (retval)
2474 /* cannot allocate the heap */
2475 return retval;
2476 }
2477
2478 again:
2479 /*
2480 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2481 * to determine which are of interest, and using the scanner's
2482 * "process_task" callback to process any of them that need an update.
2483 * Since we don't want to hold any locks during the task updates,
2484 * gather tasks to be processed in a heap structure.
2485 * The heap is sorted by descending task start time.
2486 * If the statically-sized heap fills up, we overflow tasks that
2487 * started later, and in future iterations only consider tasks that
2488 * started after the latest task in the previous pass. This
2489 * guarantees forward progress and that we don't miss any tasks.
2490 */
2491 heap->size = 0;
2492 cgroup_iter_start(scan->cg, &it);
2493 while ((p = cgroup_iter_next(scan->cg, &it))) {
2494 /*
2495 * Only affect tasks that qualify per the caller's callback,
2496 * if he provided one
2497 */
2498 if (scan->test_task && !scan->test_task(p, scan))
2499 continue;
2500 /*
2501 * Only process tasks that started after the last task
2502 * we processed
2503 */
2504 if (!started_after_time(p, &latest_time, latest_task))
2505 continue;
2506 dropped = heap_insert(heap, p);
2507 if (dropped == NULL) {
2508 /*
2509 * The new task was inserted; the heap wasn't
2510 * previously full
2511 */
2512 get_task_struct(p);
2513 } else if (dropped != p) {
2514 /*
2515 * The new task was inserted, and pushed out a
2516 * different task
2517 */
2518 get_task_struct(p);
2519 put_task_struct(dropped);
2520 }
2521 /*
2522 * Else the new task was newer than anything already in
2523 * the heap and wasn't inserted
2524 */
2525 }
2526 cgroup_iter_end(scan->cg, &it);
2527
2528 if (heap->size) {
2529 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002530 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002531 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002532 latest_time = q->start_time;
2533 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002534 }
2535 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002536 scan->process_task(q, scan);
2537 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002538 }
2539 /*
2540 * If we had to process any tasks at all, scan again
2541 * in case some of them were in the middle of forking
2542 * children that didn't get processed.
2543 * Not the most efficient way to do it, but it avoids
2544 * having to take callback_mutex in the fork path
2545 */
2546 goto again;
2547 }
2548 if (heap == &tmp_heap)
2549 heap_free(&tmp_heap);
2550 return 0;
2551}
2552
Paul Menage817929e2007-10-18 23:39:36 -07002553/*
Ben Blum102a7752009-09-23 15:56:26 -07002554 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002555 *
2556 * Reading this file can return large amounts of data if a cgroup has
2557 * *lots* of attached tasks. So it may need several calls to read(),
2558 * but we cannot guarantee that the information we produce is correct
2559 * unless we produce it entirely atomically.
2560 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002561 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002562
2563/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002564 * The following two functions "fix" the issue where there are more pids
2565 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2566 * TODO: replace with a kernel-wide solution to this problem
2567 */
2568#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2569static void *pidlist_allocate(int count)
2570{
2571 if (PIDLIST_TOO_LARGE(count))
2572 return vmalloc(count * sizeof(pid_t));
2573 else
2574 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2575}
2576static void pidlist_free(void *p)
2577{
2578 if (is_vmalloc_addr(p))
2579 vfree(p);
2580 else
2581 kfree(p);
2582}
2583static void *pidlist_resize(void *p, int newcount)
2584{
2585 void *newlist;
2586 /* note: if new alloc fails, old p will still be valid either way */
2587 if (is_vmalloc_addr(p)) {
2588 newlist = vmalloc(newcount * sizeof(pid_t));
2589 if (!newlist)
2590 return NULL;
2591 memcpy(newlist, p, newcount * sizeof(pid_t));
2592 vfree(p);
2593 } else {
2594 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
2595 }
2596 return newlist;
2597}
2598
2599/*
Ben Blum102a7752009-09-23 15:56:26 -07002600 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
2601 * If the new stripped list is sufficiently smaller and there's enough memory
2602 * to allocate a new buffer, will let go of the unneeded memory. Returns the
2603 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002604 */
Ben Blum102a7752009-09-23 15:56:26 -07002605/* is the size difference enough that we should re-allocate the array? */
2606#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
2607static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002608{
Ben Blum102a7752009-09-23 15:56:26 -07002609 int src, dest = 1;
2610 pid_t *list = *p;
2611 pid_t *newlist;
2612
2613 /*
2614 * we presume the 0th element is unique, so i starts at 1. trivial
2615 * edge cases first; no work needs to be done for either
2616 */
2617 if (length == 0 || length == 1)
2618 return length;
2619 /* src and dest walk down the list; dest counts unique elements */
2620 for (src = 1; src < length; src++) {
2621 /* find next unique element */
2622 while (list[src] == list[src-1]) {
2623 src++;
2624 if (src == length)
2625 goto after;
2626 }
2627 /* dest always points to where the next unique element goes */
2628 list[dest] = list[src];
2629 dest++;
2630 }
2631after:
2632 /*
2633 * if the length difference is large enough, we want to allocate a
2634 * smaller buffer to save memory. if this fails due to out of memory,
2635 * we'll just stay with what we've got.
2636 */
2637 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07002638 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07002639 if (newlist)
2640 *p = newlist;
2641 }
2642 return dest;
2643}
2644
2645static int cmppid(const void *a, const void *b)
2646{
2647 return *(pid_t *)a - *(pid_t *)b;
2648}
2649
2650/*
Ben Blum72a8cb32009-09-23 15:56:27 -07002651 * find the appropriate pidlist for our purpose (given procs vs tasks)
2652 * returns with the lock on that pidlist already held, and takes care
2653 * of the use count, or returns NULL with no locks held if we're out of
2654 * memory.
2655 */
2656static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
2657 enum cgroup_filetype type)
2658{
2659 struct cgroup_pidlist *l;
2660 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08002661 struct pid_namespace *ns = current->nsproxy->pid_ns;
2662
Ben Blum72a8cb32009-09-23 15:56:27 -07002663 /*
2664 * We can't drop the pidlist_mutex before taking the l->mutex in case
2665 * the last ref-holder is trying to remove l from the list at the same
2666 * time. Holding the pidlist_mutex precludes somebody taking whichever
2667 * list we find out from under us - compare release_pid_array().
2668 */
2669 mutex_lock(&cgrp->pidlist_mutex);
2670 list_for_each_entry(l, &cgrp->pidlists, links) {
2671 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07002672 /* make sure l doesn't vanish out from under us */
2673 down_write(&l->mutex);
2674 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07002675 return l;
2676 }
2677 }
2678 /* entry not found; create a new one */
2679 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
2680 if (!l) {
2681 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07002682 return l;
2683 }
2684 init_rwsem(&l->mutex);
2685 down_write(&l->mutex);
2686 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08002687 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07002688 l->use_count = 0; /* don't increment here */
2689 l->list = NULL;
2690 l->owner = cgrp;
2691 list_add(&l->links, &cgrp->pidlists);
2692 mutex_unlock(&cgrp->pidlist_mutex);
2693 return l;
2694}
2695
2696/*
Ben Blum102a7752009-09-23 15:56:26 -07002697 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
2698 */
Ben Blum72a8cb32009-09-23 15:56:27 -07002699static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
2700 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07002701{
2702 pid_t *array;
2703 int length;
2704 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07002705 struct cgroup_iter it;
2706 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07002707 struct cgroup_pidlist *l;
2708
2709 /*
2710 * If cgroup gets more users after we read count, we won't have
2711 * enough space - tough. This race is indistinguishable to the
2712 * caller from the case that the additional cgroup users didn't
2713 * show up until sometime later on.
2714 */
2715 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07002716 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07002717 if (!array)
2718 return -ENOMEM;
2719 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07002720 cgroup_iter_start(cgrp, &it);
2721 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07002722 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07002723 break;
Ben Blum102a7752009-09-23 15:56:26 -07002724 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07002725 if (type == CGROUP_FILE_PROCS)
2726 pid = task_tgid_vnr(tsk);
2727 else
2728 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07002729 if (pid > 0) /* make sure to only use valid results */
2730 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07002731 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002732 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07002733 length = n;
2734 /* now sort & (if procs) strip out duplicates */
2735 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07002736 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07002737 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07002738 l = cgroup_pidlist_find(cgrp, type);
2739 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07002740 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07002741 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07002742 }
Ben Blum72a8cb32009-09-23 15:56:27 -07002743 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07002744 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07002745 l->list = array;
2746 l->length = length;
2747 l->use_count++;
2748 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07002749 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07002750 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002751}
2752
Balbir Singh846c7bb2007-10-18 23:39:44 -07002753/**
Li Zefana043e3b2008-02-23 15:24:09 -08002754 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002755 * @stats: cgroupstats to fill information into
2756 * @dentry: A dentry entry belonging to the cgroup for which stats have
2757 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002758 *
2759 * Build and fill cgroupstats so that taskstats can export it to user
2760 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002761 */
2762int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2763{
2764 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002765 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002766 struct cgroup_iter it;
2767 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002768
Balbir Singh846c7bb2007-10-18 23:39:44 -07002769 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002770 * Validate dentry by checking the superblock operations,
2771 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002772 */
Li Zefan33d283b2008-11-19 15:36:48 -08002773 if (dentry->d_sb->s_op != &cgroup_ops ||
2774 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002775 goto err;
2776
2777 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002778 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002779
Paul Menagebd89aab2007-10-18 23:40:44 -07002780 cgroup_iter_start(cgrp, &it);
2781 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002782 switch (tsk->state) {
2783 case TASK_RUNNING:
2784 stats->nr_running++;
2785 break;
2786 case TASK_INTERRUPTIBLE:
2787 stats->nr_sleeping++;
2788 break;
2789 case TASK_UNINTERRUPTIBLE:
2790 stats->nr_uninterruptible++;
2791 break;
2792 case TASK_STOPPED:
2793 stats->nr_stopped++;
2794 break;
2795 default:
2796 if (delayacct_is_task_waiting_on_io(tsk))
2797 stats->nr_io_wait++;
2798 break;
2799 }
2800 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002801 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002802
Balbir Singh846c7bb2007-10-18 23:39:44 -07002803err:
2804 return ret;
2805}
2806
Paul Menage8f3ff202009-09-23 15:56:25 -07002807
Paul Menagecc31edc2008-10-18 20:28:04 -07002808/*
Ben Blum102a7752009-09-23 15:56:26 -07002809 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07002810 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07002811 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07002812 */
2813
Ben Blum102a7752009-09-23 15:56:26 -07002814static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07002815{
2816 /*
2817 * Initially we receive a position value that corresponds to
2818 * one more than the last pid shown (or 0 on the first call or
2819 * after a seek to the start). Use a binary-search to find the
2820 * next pid to display, if any
2821 */
Ben Blum102a7752009-09-23 15:56:26 -07002822 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07002823 int index = 0, pid = *pos;
2824 int *iter;
2825
Ben Blum102a7752009-09-23 15:56:26 -07002826 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002827 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07002828 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002829
Paul Menagecc31edc2008-10-18 20:28:04 -07002830 while (index < end) {
2831 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07002832 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07002833 index = mid;
2834 break;
Ben Blum102a7752009-09-23 15:56:26 -07002835 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07002836 index = mid + 1;
2837 else
2838 end = mid;
2839 }
2840 }
2841 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07002842 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07002843 return NULL;
2844 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07002845 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07002846 *pos = *iter;
2847 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002848}
2849
Ben Blum102a7752009-09-23 15:56:26 -07002850static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07002851{
Ben Blum102a7752009-09-23 15:56:26 -07002852 struct cgroup_pidlist *l = s->private;
2853 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002854}
2855
Ben Blum102a7752009-09-23 15:56:26 -07002856static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07002857{
Ben Blum102a7752009-09-23 15:56:26 -07002858 struct cgroup_pidlist *l = s->private;
2859 pid_t *p = v;
2860 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07002861 /*
2862 * Advance to the next pid in the array. If this goes off the
2863 * end, we're done
2864 */
2865 p++;
2866 if (p >= end) {
2867 return NULL;
2868 } else {
2869 *pos = *p;
2870 return p;
2871 }
2872}
2873
Ben Blum102a7752009-09-23 15:56:26 -07002874static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07002875{
2876 return seq_printf(s, "%d\n", *(int *)v);
2877}
2878
Ben Blum102a7752009-09-23 15:56:26 -07002879/*
2880 * seq_operations functions for iterating on pidlists through seq_file -
2881 * independent of whether it's tasks or procs
2882 */
2883static const struct seq_operations cgroup_pidlist_seq_operations = {
2884 .start = cgroup_pidlist_start,
2885 .stop = cgroup_pidlist_stop,
2886 .next = cgroup_pidlist_next,
2887 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07002888};
2889
Ben Blum102a7752009-09-23 15:56:26 -07002890static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07002891{
Ben Blum72a8cb32009-09-23 15:56:27 -07002892 /*
2893 * the case where we're the last user of this particular pidlist will
2894 * have us remove it from the cgroup's list, which entails taking the
2895 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
2896 * pidlist_mutex, we have to take pidlist_mutex first.
2897 */
2898 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07002899 down_write(&l->mutex);
2900 BUG_ON(!l->use_count);
2901 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07002902 /* we're the last user if refcount is 0; remove and free */
2903 list_del(&l->links);
2904 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07002905 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07002906 put_pid_ns(l->key.ns);
2907 up_write(&l->mutex);
2908 kfree(l);
2909 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07002910 }
Ben Blum72a8cb32009-09-23 15:56:27 -07002911 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07002912 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002913}
2914
Ben Blum102a7752009-09-23 15:56:26 -07002915static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002916{
Ben Blum102a7752009-09-23 15:56:26 -07002917 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002918 if (!(file->f_mode & FMODE_READ))
2919 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07002920 /*
2921 * the seq_file will only be initialized if the file was opened for
2922 * reading; hence we check if it's not null only in that case.
2923 */
2924 l = ((struct seq_file *)file->private_data)->private;
2925 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07002926 return seq_release(inode, file);
2927}
2928
Ben Blum102a7752009-09-23 15:56:26 -07002929static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07002930 .read = seq_read,
2931 .llseek = seq_lseek,
2932 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07002933 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07002934};
2935
2936/*
Ben Blum102a7752009-09-23 15:56:26 -07002937 * The following functions handle opens on a file that displays a pidlist
2938 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
2939 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07002940 */
Ben Blum102a7752009-09-23 15:56:26 -07002941/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07002942static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07002943{
2944 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07002945 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07002946 int retval;
2947
2948 /* Nothing to do for write-only files */
2949 if (!(file->f_mode & FMODE_READ))
2950 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002951
Ben Blum102a7752009-09-23 15:56:26 -07002952 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07002953 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07002954 if (retval)
2955 return retval;
2956 /* configure file information */
2957 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002958
Ben Blum102a7752009-09-23 15:56:26 -07002959 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07002960 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07002961 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07002962 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002963 }
Ben Blum102a7752009-09-23 15:56:26 -07002964 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002965 return 0;
2966}
Ben Blum102a7752009-09-23 15:56:26 -07002967static int cgroup_tasks_open(struct inode *unused, struct file *file)
2968{
Ben Blum72a8cb32009-09-23 15:56:27 -07002969 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07002970}
2971static int cgroup_procs_open(struct inode *unused, struct file *file)
2972{
Ben Blum72a8cb32009-09-23 15:56:27 -07002973 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07002974}
Paul Menagebbcb81d2007-10-18 23:39:32 -07002975
Paul Menagebd89aab2007-10-18 23:40:44 -07002976static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002977 struct cftype *cft)
2978{
Paul Menagebd89aab2007-10-18 23:40:44 -07002979 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002980}
2981
Paul Menage6379c102008-07-25 01:47:01 -07002982static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2983 struct cftype *cft,
2984 u64 val)
2985{
2986 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2987 if (val)
2988 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2989 else
2990 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2991 return 0;
2992}
2993
Paul Menagebbcb81d2007-10-18 23:39:32 -07002994/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002995 * Unregister event and free resources.
2996 *
2997 * Gets called from workqueue.
2998 */
2999static void cgroup_event_remove(struct work_struct *work)
3000{
3001 struct cgroup_event *event = container_of(work, struct cgroup_event,
3002 remove);
3003 struct cgroup *cgrp = event->cgrp;
3004
3005 /* TODO: check return code */
3006 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3007
3008 eventfd_ctx_put(event->eventfd);
3009 remove_wait_queue(event->wqh, &event->wait);
3010 kfree(event);
3011}
3012
3013/*
3014 * Gets called on POLLHUP on eventfd when user closes it.
3015 *
3016 * Called with wqh->lock held and interrupts disabled.
3017 */
3018static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3019 int sync, void *key)
3020{
3021 struct cgroup_event *event = container_of(wait,
3022 struct cgroup_event, wait);
3023 struct cgroup *cgrp = event->cgrp;
3024 unsigned long flags = (unsigned long)key;
3025
3026 if (flags & POLLHUP) {
3027 spin_lock(&cgrp->event_list_lock);
3028 list_del(&event->list);
3029 spin_unlock(&cgrp->event_list_lock);
3030 /*
3031 * We are in atomic context, but cgroup_event_remove() may
3032 * sleep, so we have to call it in workqueue.
3033 */
3034 schedule_work(&event->remove);
3035 }
3036
3037 return 0;
3038}
3039
3040static void cgroup_event_ptable_queue_proc(struct file *file,
3041 wait_queue_head_t *wqh, poll_table *pt)
3042{
3043 struct cgroup_event *event = container_of(pt,
3044 struct cgroup_event, pt);
3045
3046 event->wqh = wqh;
3047 add_wait_queue(wqh, &event->wait);
3048}
3049
3050/*
3051 * Parse input and register new cgroup event handler.
3052 *
3053 * Input must be in format '<event_fd> <control_fd> <args>'.
3054 * Interpretation of args is defined by control file implementation.
3055 */
3056static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3057 const char *buffer)
3058{
3059 struct cgroup_event *event = NULL;
3060 unsigned int efd, cfd;
3061 struct file *efile = NULL;
3062 struct file *cfile = NULL;
3063 char *endp;
3064 int ret;
3065
3066 efd = simple_strtoul(buffer, &endp, 10);
3067 if (*endp != ' ')
3068 return -EINVAL;
3069 buffer = endp + 1;
3070
3071 cfd = simple_strtoul(buffer, &endp, 10);
3072 if ((*endp != ' ') && (*endp != '\0'))
3073 return -EINVAL;
3074 buffer = endp + 1;
3075
3076 event = kzalloc(sizeof(*event), GFP_KERNEL);
3077 if (!event)
3078 return -ENOMEM;
3079 event->cgrp = cgrp;
3080 INIT_LIST_HEAD(&event->list);
3081 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3082 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3083 INIT_WORK(&event->remove, cgroup_event_remove);
3084
3085 efile = eventfd_fget(efd);
3086 if (IS_ERR(efile)) {
3087 ret = PTR_ERR(efile);
3088 goto fail;
3089 }
3090
3091 event->eventfd = eventfd_ctx_fileget(efile);
3092 if (IS_ERR(event->eventfd)) {
3093 ret = PTR_ERR(event->eventfd);
3094 goto fail;
3095 }
3096
3097 cfile = fget(cfd);
3098 if (!cfile) {
3099 ret = -EBADF;
3100 goto fail;
3101 }
3102
3103 /* the process need read permission on control file */
3104 ret = file_permission(cfile, MAY_READ);
3105 if (ret < 0)
3106 goto fail;
3107
3108 event->cft = __file_cft(cfile);
3109 if (IS_ERR(event->cft)) {
3110 ret = PTR_ERR(event->cft);
3111 goto fail;
3112 }
3113
3114 if (!event->cft->register_event || !event->cft->unregister_event) {
3115 ret = -EINVAL;
3116 goto fail;
3117 }
3118
3119 ret = event->cft->register_event(cgrp, event->cft,
3120 event->eventfd, buffer);
3121 if (ret)
3122 goto fail;
3123
3124 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3125 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3126 ret = 0;
3127 goto fail;
3128 }
3129
3130 spin_lock(&cgrp->event_list_lock);
3131 list_add(&event->list, &cgrp->event_list);
3132 spin_unlock(&cgrp->event_list_lock);
3133
3134 fput(cfile);
3135 fput(efile);
3136
3137 return 0;
3138
3139fail:
3140 if (cfile)
3141 fput(cfile);
3142
3143 if (event && event->eventfd && !IS_ERR(event->eventfd))
3144 eventfd_ctx_put(event->eventfd);
3145
3146 if (!IS_ERR_OR_NULL(efile))
3147 fput(efile);
3148
3149 kfree(event);
3150
3151 return ret;
3152}
3153
3154/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003155 * for the common functions, 'private' gives the type of file
3156 */
Ben Blum102a7752009-09-23 15:56:26 -07003157/* for hysterical raisins, we can't put this on the older files */
3158#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003159static struct cftype files[] = {
3160 {
3161 .name = "tasks",
3162 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003163 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003164 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003165 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003166 },
Ben Blum102a7752009-09-23 15:56:26 -07003167 {
3168 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3169 .open = cgroup_procs_open,
3170 /* .write_u64 = cgroup_procs_write, TODO */
3171 .release = cgroup_pidlist_release,
3172 .mode = S_IRUGO,
3173 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003174 {
3175 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003176 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003177 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003178 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003179 {
3180 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3181 .write_string = cgroup_write_event_control,
3182 .mode = S_IWUGO,
3183 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003184};
3185
3186static struct cftype cft_release_agent = {
3187 .name = "release_agent",
Paul Menagee788e0662008-07-25 01:46:59 -07003188 .read_seq_string = cgroup_release_agent_show,
3189 .write_string = cgroup_release_agent_write,
3190 .max_write_len = PATH_MAX,
Paul Menagebbcb81d2007-10-18 23:39:32 -07003191};
3192
Paul Menagebd89aab2007-10-18 23:40:44 -07003193static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003194{
3195 int err;
3196 struct cgroup_subsys *ss;
3197
3198 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07003199 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003200
Paul Menagebd89aab2007-10-18 23:40:44 -07003201 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07003202 if (err < 0)
3203 return err;
3204
Paul Menagebd89aab2007-10-18 23:40:44 -07003205 if (cgrp == cgrp->top_cgroup) {
3206 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003207 return err;
3208 }
3209
Paul Menagebd89aab2007-10-18 23:40:44 -07003210 for_each_subsys(cgrp->root, ss) {
3211 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003212 return err;
3213 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003214 /* This cgroup is ready now */
3215 for_each_subsys(cgrp->root, ss) {
3216 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3217 /*
3218 * Update id->css pointer and make this css visible from
3219 * CSS ID functions. This pointer will be dereferened
3220 * from RCU-read-side without locks.
3221 */
3222 if (css->id)
3223 rcu_assign_pointer(css->id->css, css);
3224 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003225
3226 return 0;
3227}
3228
3229static void init_cgroup_css(struct cgroup_subsys_state *css,
3230 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07003231 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003232{
Paul Menagebd89aab2007-10-18 23:40:44 -07003233 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08003234 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003235 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003236 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003237 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003238 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07003239 BUG_ON(cgrp->subsys[ss->subsys_id]);
3240 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003241}
3242
Paul Menage999cd8a2009-01-07 18:08:36 -08003243static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
3244{
3245 /* We need to take each hierarchy_mutex in a consistent order */
3246 int i;
3247
Ben Blumaae8aab2010-03-10 15:22:07 -08003248 /*
3249 * No worry about a race with rebind_subsystems that might mess up the
3250 * locking order, since both parties are under cgroup_mutex.
3251 */
Paul Menage999cd8a2009-01-07 18:08:36 -08003252 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3253 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003254 if (ss == NULL)
3255 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08003256 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08003257 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08003258 }
3259}
3260
3261static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
3262{
3263 int i;
3264
3265 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3266 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003267 if (ss == NULL)
3268 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08003269 if (ss->root == root)
3270 mutex_unlock(&ss->hierarchy_mutex);
3271 }
3272}
3273
Paul Menageddbcc7e2007-10-18 23:39:30 -07003274/*
Li Zefana043e3b2008-02-23 15:24:09 -08003275 * cgroup_create - create a cgroup
3276 * @parent: cgroup that will be parent of the new cgroup
3277 * @dentry: dentry of the new cgroup
3278 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07003279 *
Li Zefana043e3b2008-02-23 15:24:09 -08003280 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07003281 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07003282static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07003283 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003284{
Paul Menagebd89aab2007-10-18 23:40:44 -07003285 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003286 struct cgroupfs_root *root = parent->root;
3287 int err = 0;
3288 struct cgroup_subsys *ss;
3289 struct super_block *sb = root->sb;
3290
Paul Menagebd89aab2007-10-18 23:40:44 -07003291 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3292 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003293 return -ENOMEM;
3294
3295 /* Grab a reference on the superblock so the hierarchy doesn't
3296 * get deleted on unmount if there are child cgroups. This
3297 * can be done outside cgroup_mutex, since the sb can't
3298 * disappear while someone has an open control file on the
3299 * fs */
3300 atomic_inc(&sb->s_active);
3301
3302 mutex_lock(&cgroup_mutex);
3303
Paul Menagecc31edc2008-10-18 20:28:04 -07003304 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003305
Paul Menagebd89aab2007-10-18 23:40:44 -07003306 cgrp->parent = parent;
3307 cgrp->root = parent->root;
3308 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003309
Li Zefanb6abdb02008-03-04 14:28:19 -08003310 if (notify_on_release(parent))
3311 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3312
Paul Menageddbcc7e2007-10-18 23:39:30 -07003313 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003314 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003315
Paul Menageddbcc7e2007-10-18 23:39:30 -07003316 if (IS_ERR(css)) {
3317 err = PTR_ERR(css);
3318 goto err_destroy;
3319 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003320 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003321 if (ss->use_id) {
3322 err = alloc_css_id(ss, parent, cgrp);
3323 if (err)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003324 goto err_destroy;
Li Zefan4528fd02010-02-02 13:44:10 -08003325 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003326 /* At error, ->destroy() callback has to free assigned ID. */
Paul Menageddbcc7e2007-10-18 23:39:30 -07003327 }
3328
Paul Menage999cd8a2009-01-07 18:08:36 -08003329 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003330 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08003331 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003332 root->number_of_cgroups++;
3333
Paul Menagebd89aab2007-10-18 23:40:44 -07003334 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003335 if (err < 0)
3336 goto err_remove;
3337
3338 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07003339 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003340
Paul Menagebd89aab2007-10-18 23:40:44 -07003341 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003342 /* If err < 0, we have a half-filled directory - oh well ;) */
3343
3344 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003345 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003346
3347 return 0;
3348
3349 err_remove:
3350
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003351 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003352 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003353 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003354 root->number_of_cgroups--;
3355
3356 err_destroy:
3357
3358 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003359 if (cgrp->subsys[ss->subsys_id])
3360 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003361 }
3362
3363 mutex_unlock(&cgroup_mutex);
3364
3365 /* Release the reference count that we took on the superblock */
3366 deactivate_super(sb);
3367
Paul Menagebd89aab2007-10-18 23:40:44 -07003368 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003369 return err;
3370}
3371
3372static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3373{
3374 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
3375
3376 /* the vfs holds inode->i_mutex already */
3377 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
3378}
3379
Li Zefan55b6fd02008-07-29 22:33:20 -07003380static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003381{
3382 /* Check the reference count on each subsystem. Since we
3383 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08003384 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07003385 * be no outstanding references, so the subsystem is safe to
3386 * destroy. We scan across all subsystems rather than using
3387 * the per-hierarchy linked list of mounted subsystems since
3388 * we can be called via check_for_release() with no
3389 * synchronization other than RCU, and the subsystem linked
3390 * list isn't RCU-safe */
3391 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08003392 /*
3393 * We won't need to lock the subsys array, because the subsystems
3394 * we're concerned about aren't going anywhere since our cgroup root
3395 * has a reference on them.
3396 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003397 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3398 struct cgroup_subsys *ss = subsys[i];
3399 struct cgroup_subsys_state *css;
Ben Blumaae8aab2010-03-10 15:22:07 -08003400 /* Skip subsystems not present or not in this hierarchy */
3401 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003402 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07003403 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07003404 /* When called from check_for_release() it's possible
3405 * that by this point the cgroup has been removed
3406 * and the css deleted. But a false-positive doesn't
3407 * matter, since it can only happen if the cgroup
3408 * has been deleted and hence no longer needs the
3409 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08003410 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07003411 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003412 }
3413 return 0;
3414}
3415
Paul Menagee7c5ec92009-01-07 18:08:38 -08003416/*
3417 * Atomically mark all (or else none) of the cgroup's CSS objects as
3418 * CSS_REMOVED. Return true on success, or false if the cgroup has
3419 * busy subsystems. Call with cgroup_mutex held
3420 */
3421
3422static int cgroup_clear_css_refs(struct cgroup *cgrp)
3423{
3424 struct cgroup_subsys *ss;
3425 unsigned long flags;
3426 bool failed = false;
3427 local_irq_save(flags);
3428 for_each_subsys(cgrp->root, ss) {
3429 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3430 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08003431 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08003432 /* We can only remove a CSS with a refcnt==1 */
3433 refcnt = atomic_read(&css->refcnt);
3434 if (refcnt > 1) {
3435 failed = true;
3436 goto done;
3437 }
3438 BUG_ON(!refcnt);
3439 /*
3440 * Drop the refcnt to 0 while we check other
3441 * subsystems. This will cause any racing
3442 * css_tryget() to spin until we set the
3443 * CSS_REMOVED bits or abort
3444 */
Paul Menage804b3c22009-01-29 14:25:21 -08003445 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
3446 break;
3447 cpu_relax();
3448 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08003449 }
3450 done:
3451 for_each_subsys(cgrp->root, ss) {
3452 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3453 if (failed) {
3454 /*
3455 * Restore old refcnt if we previously managed
3456 * to clear it from 1 to 0
3457 */
3458 if (!atomic_read(&css->refcnt))
3459 atomic_set(&css->refcnt, 1);
3460 } else {
3461 /* Commit the fact that the CSS is removed */
3462 set_bit(CSS_REMOVED, &css->flags);
3463 }
3464 }
3465 local_irq_restore(flags);
3466 return !failed;
3467}
3468
Paul Menageddbcc7e2007-10-18 23:39:30 -07003469static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
3470{
Paul Menagebd89aab2007-10-18 23:40:44 -07003471 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003472 struct dentry *d;
3473 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003474 DEFINE_WAIT(wait);
3475 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003476
3477 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003478again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07003479 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003480 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003481 mutex_unlock(&cgroup_mutex);
3482 return -EBUSY;
3483 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003484 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003485 mutex_unlock(&cgroup_mutex);
3486 return -EBUSY;
3487 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003488 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08003489
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003490 /*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003491 * In general, subsystem has no css->refcnt after pre_destroy(). But
3492 * in racy cases, subsystem may have to get css->refcnt after
3493 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
3494 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
3495 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
3496 * and subsystem's reference count handling. Please see css_get/put
3497 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3498 */
3499 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3500
3501 /*
Li Zefana043e3b2008-02-23 15:24:09 -08003502 * Call pre_destroy handlers of subsys. Notify subsystems
3503 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003504 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003505 ret = cgroup_call_pre_destroy(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003506 if (ret) {
3507 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003508 return ret;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003509 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003510
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003511 mutex_lock(&cgroup_mutex);
3512 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003513 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003514 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003515 mutex_unlock(&cgroup_mutex);
3516 return -EBUSY;
3517 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003518 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003519 if (!cgroup_clear_css_refs(cgrp)) {
3520 mutex_unlock(&cgroup_mutex);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003521 /*
3522 * Because someone may call cgroup_wakeup_rmdir_waiter() before
3523 * prepare_to_wait(), we need to check this flag.
3524 */
3525 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
3526 schedule();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003527 finish_wait(&cgroup_rmdir_waitq, &wait);
3528 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3529 if (signal_pending(current))
3530 return -EINTR;
3531 goto again;
3532 }
3533 /* NO css_tryget() can success after here. */
3534 finish_wait(&cgroup_rmdir_waitq, &wait);
3535 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003536
Paul Menage81a6a5c2007-10-18 23:39:38 -07003537 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003538 set_bit(CGRP_REMOVED, &cgrp->flags);
3539 if (!list_empty(&cgrp->release_list))
3540 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003541 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08003542
3543 cgroup_lock_hierarchy(cgrp->root);
3544 /* delete this cgroup from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07003545 list_del(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08003546 cgroup_unlock_hierarchy(cgrp->root);
3547
Paul Menagebd89aab2007-10-18 23:40:44 -07003548 spin_lock(&cgrp->dentry->d_lock);
3549 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003550 spin_unlock(&d->d_lock);
3551
3552 cgroup_d_remove_dir(d);
3553 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003554
Paul Menagebd89aab2007-10-18 23:40:44 -07003555 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003556 check_for_release(parent);
3557
Paul Menageddbcc7e2007-10-18 23:39:30 -07003558 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003559 return 0;
3560}
3561
Li Zefan06a11922008-04-29 01:00:07 -07003562static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003563{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003564 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08003565
3566 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003567
3568 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08003569 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003570 ss->root = &rootnode;
3571 css = ss->create(ss, dummytop);
3572 /* We don't handle early failures gracefully */
3573 BUG_ON(IS_ERR(css));
3574 init_cgroup_css(css, ss, dummytop);
3575
Li Zefane8d55fd2008-04-29 01:00:13 -07003576 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07003577 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07003578 * newly registered, all tasks and hence the
3579 * init_css_set is in the subsystem's top cgroup. */
3580 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07003581
3582 need_forkexit_callback |= ss->fork || ss->exit;
3583
Li Zefane8d55fd2008-04-29 01:00:13 -07003584 /* At system boot, before all subsystems have been
3585 * registered, no tasks have been forked, so we don't
3586 * need to invoke fork callbacks here. */
3587 BUG_ON(!list_empty(&init_task.tasks));
3588
Paul Menage999cd8a2009-01-07 18:08:36 -08003589 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08003590 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003591 ss->active = 1;
Ben Blume6a11052010-03-10 15:22:09 -08003592
3593 /* this function shouldn't be used with modular subsystems, since they
3594 * need to register a subsys_id, among other things */
3595 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003596}
3597
3598/**
Ben Blume6a11052010-03-10 15:22:09 -08003599 * cgroup_load_subsys: load and register a modular subsystem at runtime
3600 * @ss: the subsystem to load
3601 *
3602 * This function should be called in a modular subsystem's initcall. If the
3603 * subsytem is built as a module, it will be assigned a new subsys_id and set
3604 * up for use. If the subsystem is built-in anyway, work is delegated to the
3605 * simpler cgroup_init_subsys.
3606 */
3607int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
3608{
3609 int i;
3610 struct cgroup_subsys_state *css;
3611
3612 /* check name and function validity */
3613 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
3614 ss->create == NULL || ss->destroy == NULL)
3615 return -EINVAL;
3616
3617 /*
3618 * we don't support callbacks in modular subsystems. this check is
3619 * before the ss->module check for consistency; a subsystem that could
3620 * be a module should still have no callbacks even if the user isn't
3621 * compiling it as one.
3622 */
3623 if (ss->fork || ss->exit)
3624 return -EINVAL;
3625
3626 /*
3627 * an optionally modular subsystem is built-in: we want to do nothing,
3628 * since cgroup_init_subsys will have already taken care of it.
3629 */
3630 if (ss->module == NULL) {
3631 /* a few sanity checks */
3632 BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
3633 BUG_ON(subsys[ss->subsys_id] != ss);
3634 return 0;
3635 }
3636
3637 /*
3638 * need to register a subsys id before anything else - for example,
3639 * init_cgroup_css needs it.
3640 */
3641 mutex_lock(&cgroup_mutex);
3642 /* find the first empty slot in the array */
3643 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
3644 if (subsys[i] == NULL)
3645 break;
3646 }
3647 if (i == CGROUP_SUBSYS_COUNT) {
3648 /* maximum number of subsystems already registered! */
3649 mutex_unlock(&cgroup_mutex);
3650 return -EBUSY;
3651 }
3652 /* assign ourselves the subsys_id */
3653 ss->subsys_id = i;
3654 subsys[i] = ss;
3655
3656 /*
3657 * no ss->create seems to need anything important in the ss struct, so
3658 * this can happen first (i.e. before the rootnode attachment).
3659 */
3660 css = ss->create(ss, dummytop);
3661 if (IS_ERR(css)) {
3662 /* failure case - need to deassign the subsys[] slot. */
3663 subsys[i] = NULL;
3664 mutex_unlock(&cgroup_mutex);
3665 return PTR_ERR(css);
3666 }
3667
3668 list_add(&ss->sibling, &rootnode.subsys_list);
3669 ss->root = &rootnode;
3670
3671 /* our new subsystem will be attached to the dummy hierarchy. */
3672 init_cgroup_css(css, ss, dummytop);
3673 /* init_idr must be after init_cgroup_css because it sets css->id. */
3674 if (ss->use_id) {
3675 int ret = cgroup_init_idr(ss, css);
3676 if (ret) {
3677 dummytop->subsys[ss->subsys_id] = NULL;
3678 ss->destroy(ss, dummytop);
3679 subsys[i] = NULL;
3680 mutex_unlock(&cgroup_mutex);
3681 return ret;
3682 }
3683 }
3684
3685 /*
3686 * Now we need to entangle the css into the existing css_sets. unlike
3687 * in cgroup_init_subsys, there are now multiple css_sets, so each one
3688 * will need a new pointer to it; done by iterating the css_set_table.
3689 * furthermore, modifying the existing css_sets will corrupt the hash
3690 * table state, so each changed css_set will need its hash recomputed.
3691 * this is all done under the css_set_lock.
3692 */
3693 write_lock(&css_set_lock);
3694 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
3695 struct css_set *cg;
3696 struct hlist_node *node, *tmp;
3697 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
3698
3699 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
3700 /* skip entries that we already rehashed */
3701 if (cg->subsys[ss->subsys_id])
3702 continue;
3703 /* remove existing entry */
3704 hlist_del(&cg->hlist);
3705 /* set new value */
3706 cg->subsys[ss->subsys_id] = css;
3707 /* recompute hash and restore entry */
3708 new_bucket = css_set_hash(cg->subsys);
3709 hlist_add_head(&cg->hlist, new_bucket);
3710 }
3711 }
3712 write_unlock(&css_set_lock);
3713
3714 mutex_init(&ss->hierarchy_mutex);
3715 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
3716 ss->active = 1;
3717
Ben Blume6a11052010-03-10 15:22:09 -08003718 /* success! */
3719 mutex_unlock(&cgroup_mutex);
3720 return 0;
3721}
3722EXPORT_SYMBOL_GPL(cgroup_load_subsys);
3723
3724/**
Ben Blumcf5d5942010-03-10 15:22:09 -08003725 * cgroup_unload_subsys: unload a modular subsystem
3726 * @ss: the subsystem to unload
3727 *
3728 * This function should be called in a modular subsystem's exitcall. When this
3729 * function is invoked, the refcount on the subsystem's module will be 0, so
3730 * the subsystem will not be attached to any hierarchy.
3731 */
3732void cgroup_unload_subsys(struct cgroup_subsys *ss)
3733{
3734 struct cg_cgroup_link *link;
3735 struct hlist_head *hhead;
3736
3737 BUG_ON(ss->module == NULL);
3738
3739 /*
3740 * we shouldn't be called if the subsystem is in use, and the use of
3741 * try_module_get in parse_cgroupfs_options should ensure that it
3742 * doesn't start being used while we're killing it off.
3743 */
3744 BUG_ON(ss->root != &rootnode);
3745
3746 mutex_lock(&cgroup_mutex);
3747 /* deassign the subsys_id */
3748 BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT);
3749 subsys[ss->subsys_id] = NULL;
3750
3751 /* remove subsystem from rootnode's list of subsystems */
3752 list_del(&ss->sibling);
3753
3754 /*
3755 * disentangle the css from all css_sets attached to the dummytop. as
3756 * in loading, we need to pay our respects to the hashtable gods.
3757 */
3758 write_lock(&css_set_lock);
3759 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
3760 struct css_set *cg = link->cg;
3761
3762 hlist_del(&cg->hlist);
3763 BUG_ON(!cg->subsys[ss->subsys_id]);
3764 cg->subsys[ss->subsys_id] = NULL;
3765 hhead = css_set_hash(cg->subsys);
3766 hlist_add_head(&cg->hlist, hhead);
3767 }
3768 write_unlock(&css_set_lock);
3769
3770 /*
3771 * remove subsystem's css from the dummytop and free it - need to free
3772 * before marking as null because ss->destroy needs the cgrp->subsys
3773 * pointer to find their state. note that this also takes care of
3774 * freeing the css_id.
3775 */
3776 ss->destroy(ss, dummytop);
3777 dummytop->subsys[ss->subsys_id] = NULL;
3778
3779 mutex_unlock(&cgroup_mutex);
3780}
3781EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
3782
3783/**
Li Zefana043e3b2008-02-23 15:24:09 -08003784 * cgroup_init_early - cgroup initialization at system boot
3785 *
3786 * Initialize cgroups at system boot, and initialize any
3787 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07003788 */
3789int __init cgroup_init_early(void)
3790{
3791 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07003792 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07003793 INIT_LIST_HEAD(&init_css_set.cg_links);
3794 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07003795 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07003796 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003797 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07003798 root_count = 1;
3799 init_task.cgroups = &init_css_set;
3800
3801 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07003802 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07003803 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07003804 &rootnode.top_cgroup.css_sets);
3805 list_add(&init_css_set_link.cg_link_list,
3806 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003807
Li Zefan472b1052008-04-29 01:00:11 -07003808 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
3809 INIT_HLIST_HEAD(&css_set_table[i]);
3810
Ben Blumaae8aab2010-03-10 15:22:07 -08003811 /* at bootup time, we don't worry about modular subsystems */
3812 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003813 struct cgroup_subsys *ss = subsys[i];
3814
3815 BUG_ON(!ss->name);
3816 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
3817 BUG_ON(!ss->create);
3818 BUG_ON(!ss->destroy);
3819 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08003820 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07003821 ss->name, ss->subsys_id);
3822 BUG();
3823 }
3824
3825 if (ss->early_init)
3826 cgroup_init_subsys(ss);
3827 }
3828 return 0;
3829}
3830
3831/**
Li Zefana043e3b2008-02-23 15:24:09 -08003832 * cgroup_init - cgroup initialization
3833 *
3834 * Register cgroup filesystem and /proc file, and initialize
3835 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07003836 */
3837int __init cgroup_init(void)
3838{
3839 int err;
3840 int i;
Li Zefan472b1052008-04-29 01:00:11 -07003841 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07003842
3843 err = bdi_init(&cgroup_backing_dev_info);
3844 if (err)
3845 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003846
Ben Blumaae8aab2010-03-10 15:22:07 -08003847 /* at bootup time, we don't worry about modular subsystems */
3848 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003849 struct cgroup_subsys *ss = subsys[i];
3850 if (!ss->early_init)
3851 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003852 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08003853 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003854 }
3855
Li Zefan472b1052008-04-29 01:00:11 -07003856 /* Add init_css_set to the hash table */
3857 hhead = css_set_hash(init_css_set.subsys);
3858 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003859 BUG_ON(!init_root_id(&rootnode));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003860 err = register_filesystem(&cgroup_fs_type);
3861 if (err < 0)
3862 goto out;
3863
Li Zefan46ae2202008-04-29 01:00:08 -07003864 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07003865
Paul Menageddbcc7e2007-10-18 23:39:30 -07003866out:
Paul Menagea4243162007-10-18 23:39:35 -07003867 if (err)
3868 bdi_destroy(&cgroup_backing_dev_info);
3869
Paul Menageddbcc7e2007-10-18 23:39:30 -07003870 return err;
3871}
Paul Menageb4f48b62007-10-18 23:39:33 -07003872
Paul Menagea4243162007-10-18 23:39:35 -07003873/*
3874 * proc_cgroup_show()
3875 * - Print task's cgroup paths into seq_file, one line for each hierarchy
3876 * - Used for /proc/<pid>/cgroup.
3877 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
3878 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003879 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07003880 * anyway. No need to check that tsk->cgroup != NULL, thanks to
3881 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
3882 * cgroup to top_cgroup.
3883 */
3884
3885/* TODO: Use a proper seq_file iterator */
3886static int proc_cgroup_show(struct seq_file *m, void *v)
3887{
3888 struct pid *pid;
3889 struct task_struct *tsk;
3890 char *buf;
3891 int retval;
3892 struct cgroupfs_root *root;
3893
3894 retval = -ENOMEM;
3895 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3896 if (!buf)
3897 goto out;
3898
3899 retval = -ESRCH;
3900 pid = m->private;
3901 tsk = get_pid_task(pid, PIDTYPE_PID);
3902 if (!tsk)
3903 goto out_free;
3904
3905 retval = 0;
3906
3907 mutex_lock(&cgroup_mutex);
3908
Li Zefane5f6a862009-01-07 18:07:41 -08003909 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07003910 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07003911 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07003912 int count = 0;
3913
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003914 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07003915 for_each_subsys(root, ss)
3916 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07003917 if (strlen(root->name))
3918 seq_printf(m, "%sname=%s", count ? "," : "",
3919 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07003920 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07003921 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003922 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07003923 if (retval < 0)
3924 goto out_unlock;
3925 seq_puts(m, buf);
3926 seq_putc(m, '\n');
3927 }
3928
3929out_unlock:
3930 mutex_unlock(&cgroup_mutex);
3931 put_task_struct(tsk);
3932out_free:
3933 kfree(buf);
3934out:
3935 return retval;
3936}
3937
3938static int cgroup_open(struct inode *inode, struct file *file)
3939{
3940 struct pid *pid = PROC_I(inode)->pid;
3941 return single_open(file, proc_cgroup_show, pid);
3942}
3943
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003944const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07003945 .open = cgroup_open,
3946 .read = seq_read,
3947 .llseek = seq_lseek,
3948 .release = single_release,
3949};
3950
3951/* Display information about each subsystem and each hierarchy */
3952static int proc_cgroupstats_show(struct seq_file *m, void *v)
3953{
3954 int i;
Paul Menagea4243162007-10-18 23:39:35 -07003955
Paul Menage8bab8dd2008-04-04 14:29:57 -07003956 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08003957 /*
3958 * ideally we don't want subsystems moving around while we do this.
3959 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
3960 * subsys/hierarchy state.
3961 */
Paul Menagea4243162007-10-18 23:39:35 -07003962 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07003963 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3964 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003965 if (ss == NULL)
3966 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003967 seq_printf(m, "%s\t%d\t%d\t%d\n",
3968 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07003969 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07003970 }
3971 mutex_unlock(&cgroup_mutex);
3972 return 0;
3973}
3974
3975static int cgroupstats_open(struct inode *inode, struct file *file)
3976{
Al Viro9dce07f2008-03-29 03:07:28 +00003977 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07003978}
3979
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003980static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07003981 .open = cgroupstats_open,
3982 .read = seq_read,
3983 .llseek = seq_lseek,
3984 .release = single_release,
3985};
3986
Paul Menageb4f48b62007-10-18 23:39:33 -07003987/**
3988 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08003989 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07003990 *
3991 * Description: A task inherits its parent's cgroup at fork().
3992 *
3993 * A pointer to the shared css_set was automatically copied in
3994 * fork.c by dup_task_struct(). However, we ignore that copy, since
3995 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08003996 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07003997 * have already changed current->cgroups, allowing the previously
3998 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07003999 *
4000 * At the point that cgroup_fork() is called, 'current' is the parent
4001 * task, and the passed argument 'child' points to the child task.
4002 */
4003void cgroup_fork(struct task_struct *child)
4004{
Paul Menage817929e2007-10-18 23:39:36 -07004005 task_lock(current);
4006 child->cgroups = current->cgroups;
4007 get_css_set(child->cgroups);
4008 task_unlock(current);
4009 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004010}
4011
4012/**
Li Zefana043e3b2008-02-23 15:24:09 -08004013 * cgroup_fork_callbacks - run fork callbacks
4014 * @child: the new task
4015 *
4016 * Called on a new task very soon before adding it to the
4017 * tasklist. No need to take any locks since no-one can
4018 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004019 */
4020void cgroup_fork_callbacks(struct task_struct *child)
4021{
4022 if (need_forkexit_callback) {
4023 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08004024 /*
4025 * forkexit callbacks are only supported for builtin
4026 * subsystems, and the builtin section of the subsys array is
4027 * immutable, so we don't need to lock the subsys array here.
4028 */
4029 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageb4f48b62007-10-18 23:39:33 -07004030 struct cgroup_subsys *ss = subsys[i];
4031 if (ss->fork)
4032 ss->fork(ss, child);
4033 }
4034 }
4035}
4036
4037/**
Li Zefana043e3b2008-02-23 15:24:09 -08004038 * cgroup_post_fork - called on a new task after adding it to the task list
4039 * @child: the task in question
4040 *
4041 * Adds the task to the list running through its css_set if necessary.
4042 * Has to be after the task is visible on the task list in case we race
4043 * with the first call to cgroup_iter_start() - to guarantee that the
4044 * new task ends up on its list.
4045 */
Paul Menage817929e2007-10-18 23:39:36 -07004046void cgroup_post_fork(struct task_struct *child)
4047{
4048 if (use_task_css_set_links) {
4049 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08004050 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004051 if (list_empty(&child->cg_list))
4052 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08004053 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004054 write_unlock(&css_set_lock);
4055 }
4056}
4057/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004058 * cgroup_exit - detach cgroup from exiting task
4059 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004060 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004061 *
4062 * Description: Detach cgroup from @tsk and release it.
4063 *
4064 * Note that cgroups marked notify_on_release force every task in
4065 * them to take the global cgroup_mutex mutex when exiting.
4066 * This could impact scaling on very large systems. Be reluctant to
4067 * use notify_on_release cgroups where very high task exit scaling
4068 * is required on large systems.
4069 *
4070 * the_top_cgroup_hack:
4071 *
4072 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4073 *
4074 * We call cgroup_exit() while the task is still competent to
4075 * handle notify_on_release(), then leave the task attached to the
4076 * root cgroup in each hierarchy for the remainder of its exit.
4077 *
4078 * To do this properly, we would increment the reference count on
4079 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4080 * code we would add a second cgroup function call, to drop that
4081 * reference. This would just create an unnecessary hot spot on
4082 * the top_cgroup reference count, to no avail.
4083 *
4084 * Normally, holding a reference to a cgroup without bumping its
4085 * count is unsafe. The cgroup could go away, or someone could
4086 * attach us to a different cgroup, decrementing the count on
4087 * the first cgroup that we never incremented. But in this case,
4088 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004089 * which wards off any cgroup_attach_task() attempts, or task is a failed
4090 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004091 */
4092void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4093{
4094 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004095 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07004096
4097 if (run_callbacks && need_forkexit_callback) {
Ben Blumaae8aab2010-03-10 15:22:07 -08004098 /*
4099 * modular subsystems can't use callbacks, so no need to lock
4100 * the subsys array
4101 */
4102 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageb4f48b62007-10-18 23:39:33 -07004103 struct cgroup_subsys *ss = subsys[i];
4104 if (ss->exit)
4105 ss->exit(ss, tsk);
4106 }
4107 }
Paul Menage817929e2007-10-18 23:39:36 -07004108
4109 /*
4110 * Unlink from the css_set task list if necessary.
4111 * Optimistically check cg_list before taking
4112 * css_set_lock
4113 */
4114 if (!list_empty(&tsk->cg_list)) {
4115 write_lock(&css_set_lock);
4116 if (!list_empty(&tsk->cg_list))
4117 list_del(&tsk->cg_list);
4118 write_unlock(&css_set_lock);
4119 }
4120
Paul Menageb4f48b62007-10-18 23:39:33 -07004121 /* Reassign the task to the init_css_set. */
4122 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004123 cg = tsk->cgroups;
4124 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07004125 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004126 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004127 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004128}
Paul Menage697f4162007-10-18 23:39:34 -07004129
4130/**
Li Zefana043e3b2008-02-23 15:24:09 -08004131 * cgroup_clone - clone the cgroup the given subsystem is attached to
4132 * @tsk: the task to be moved
4133 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07004134 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08004135 *
4136 * Duplicate the current cgroup in the hierarchy that the given
4137 * subsystem is attached to, and move this task into the new
4138 * child.
Paul Menage697f4162007-10-18 23:39:34 -07004139 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07004140int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
4141 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07004142{
4143 struct dentry *dentry;
4144 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07004145 struct cgroup *parent, *child;
4146 struct inode *inode;
4147 struct css_set *cg;
4148 struct cgroupfs_root *root;
4149 struct cgroup_subsys *ss;
4150
4151 /* We shouldn't be called by an unregistered subsystem */
4152 BUG_ON(!subsys->active);
4153
4154 /* First figure out what hierarchy and cgroup we're dealing
4155 * with, and pin them so we can drop cgroup_mutex */
4156 mutex_lock(&cgroup_mutex);
4157 again:
4158 root = subsys->root;
4159 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07004160 mutex_unlock(&cgroup_mutex);
4161 return 0;
4162 }
Paul Menage697f4162007-10-18 23:39:34 -07004163
Paul Menage697f4162007-10-18 23:39:34 -07004164 /* Pin the hierarchy */
Li Zefan1404f062009-01-29 14:25:21 -08004165 if (!atomic_inc_not_zero(&root->sb->s_active)) {
Li Zefan7b574b72009-01-04 12:00:45 -08004166 /* We race with the final deactivate_super() */
4167 mutex_unlock(&cgroup_mutex);
4168 return 0;
4169 }
Paul Menage697f4162007-10-18 23:39:34 -07004170
Paul Menage817929e2007-10-18 23:39:36 -07004171 /* Keep the cgroup alive */
Li Zefan1404f062009-01-29 14:25:21 -08004172 task_lock(tsk);
4173 parent = task_cgroup(tsk, subsys->subsys_id);
4174 cg = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07004175 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08004176 task_unlock(tsk);
Li Zefan1404f062009-01-29 14:25:21 -08004177
Paul Menage697f4162007-10-18 23:39:34 -07004178 mutex_unlock(&cgroup_mutex);
4179
4180 /* Now do the VFS work to create a cgroup */
4181 inode = parent->dentry->d_inode;
4182
4183 /* Hold the parent directory mutex across this operation to
4184 * stop anyone else deleting the new cgroup */
4185 mutex_lock(&inode->i_mutex);
4186 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
4187 if (IS_ERR(dentry)) {
4188 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08004189 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07004190 PTR_ERR(dentry));
4191 ret = PTR_ERR(dentry);
4192 goto out_release;
4193 }
4194
4195 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08004196 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07004197 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07004198 dput(dentry);
4199 if (ret) {
4200 printk(KERN_INFO
4201 "Failed to create cgroup %s: %d\n", nodename,
4202 ret);
4203 goto out_release;
4204 }
4205
Paul Menage697f4162007-10-18 23:39:34 -07004206 /* The cgroup now exists. Retake cgroup_mutex and check
4207 * that we're still in the same state that we thought we
4208 * were. */
4209 mutex_lock(&cgroup_mutex);
4210 if ((root != subsys->root) ||
4211 (parent != task_cgroup(tsk, subsys->subsys_id))) {
4212 /* Aargh, we raced ... */
4213 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07004214 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07004215
Li Zefan1404f062009-01-29 14:25:21 -08004216 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07004217 /* The cgroup is still accessible in the VFS, but
4218 * we're not going to try to rmdir() it at this
4219 * point. */
4220 printk(KERN_INFO
4221 "Race in cgroup_clone() - leaking cgroup %s\n",
4222 nodename);
4223 goto again;
4224 }
4225
4226 /* do any required auto-setup */
4227 for_each_subsys(root, ss) {
4228 if (ss->post_clone)
4229 ss->post_clone(ss, child);
4230 }
4231
4232 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08004233 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07004234 mutex_unlock(&cgroup_mutex);
4235
4236 out_release:
4237 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004238
4239 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07004240 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004241 mutex_unlock(&cgroup_mutex);
Li Zefan1404f062009-01-29 14:25:21 -08004242 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07004243 return ret;
4244}
4245
Li Zefana043e3b2008-02-23 15:24:09 -08004246/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004247 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08004248 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004249 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08004250 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004251 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4252 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07004253 *
4254 * If we are sending in dummytop, then presumably we are creating
4255 * the top cgroup in the subsystem.
4256 *
4257 * Called only by the ns (nsproxy) cgroup.
4258 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004259int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07004260{
4261 int ret;
4262 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07004263
Paul Menagebd89aab2007-10-18 23:40:44 -07004264 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07004265 return 1;
4266
Paul Menage7717f7b2009-09-23 15:56:22 -07004267 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004268 while (cgrp != target && cgrp!= cgrp->top_cgroup)
4269 cgrp = cgrp->parent;
4270 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07004271 return ret;
4272}
Paul Menage81a6a5c2007-10-18 23:39:38 -07004273
Paul Menagebd89aab2007-10-18 23:40:44 -07004274static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004275{
4276 /* All of these checks rely on RCU to keep the cgroup
4277 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07004278 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
4279 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004280 /* Control Group is currently removeable. If it's not
4281 * already queued for a userspace notification, queue
4282 * it now */
4283 int need_schedule_work = 0;
4284 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004285 if (!cgroup_is_removed(cgrp) &&
4286 list_empty(&cgrp->release_list)) {
4287 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004288 need_schedule_work = 1;
4289 }
4290 spin_unlock(&release_list_lock);
4291 if (need_schedule_work)
4292 schedule_work(&release_agent_work);
4293 }
4294}
4295
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004296/* Caller must verify that the css is not for root cgroup */
4297void __css_put(struct cgroup_subsys_state *css, int count)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004298{
Paul Menagebd89aab2007-10-18 23:40:44 -07004299 struct cgroup *cgrp = css->cgroup;
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004300 int val;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004301 rcu_read_lock();
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004302 val = atomic_sub_return(count, &css->refcnt);
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004303 if (val == 1) {
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004304 if (notify_on_release(cgrp)) {
4305 set_bit(CGRP_RELEASABLE, &cgrp->flags);
4306 check_for_release(cgrp);
4307 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004308 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004309 }
4310 rcu_read_unlock();
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004311 WARN_ON_ONCE(val < 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004312}
Ben Blum67523c42010-03-10 15:22:11 -08004313EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004314
4315/*
4316 * Notify userspace when a cgroup is released, by running the
4317 * configured release agent with the name of the cgroup (path
4318 * relative to the root of cgroup file system) as the argument.
4319 *
4320 * Most likely, this user command will try to rmdir this cgroup.
4321 *
4322 * This races with the possibility that some other task will be
4323 * attached to this cgroup before it is removed, or that some other
4324 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4325 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4326 * unused, and this cgroup will be reprieved from its death sentence,
4327 * to continue to serve a useful existence. Next time it's released,
4328 * we will get notified again, if it still has 'notify_on_release' set.
4329 *
4330 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4331 * means only wait until the task is successfully execve()'d. The
4332 * separate release agent task is forked by call_usermodehelper(),
4333 * then control in this thread returns here, without waiting for the
4334 * release agent task. We don't bother to wait because the caller of
4335 * this routine has no use for the exit status of the release agent
4336 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004337 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004338static void cgroup_release_agent(struct work_struct *work)
4339{
4340 BUG_ON(work != &release_agent_work);
4341 mutex_lock(&cgroup_mutex);
4342 spin_lock(&release_list_lock);
4343 while (!list_empty(&release_list)) {
4344 char *argv[3], *envp[3];
4345 int i;
Paul Menagee788e0662008-07-25 01:46:59 -07004346 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004347 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004348 struct cgroup,
4349 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004350 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004351 spin_unlock(&release_list_lock);
4352 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e0662008-07-25 01:46:59 -07004353 if (!pathbuf)
4354 goto continue_free;
4355 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
4356 goto continue_free;
4357 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4358 if (!agentbuf)
4359 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004360
4361 i = 0;
Paul Menagee788e0662008-07-25 01:46:59 -07004362 argv[i++] = agentbuf;
4363 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004364 argv[i] = NULL;
4365
4366 i = 0;
4367 /* minimal command environment */
4368 envp[i++] = "HOME=/";
4369 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4370 envp[i] = NULL;
4371
4372 /* Drop the lock while we invoke the usermode helper,
4373 * since the exec could involve hitting disk and hence
4374 * be a slow process */
4375 mutex_unlock(&cgroup_mutex);
4376 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004377 mutex_lock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07004378 continue_free:
4379 kfree(pathbuf);
4380 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004381 spin_lock(&release_list_lock);
4382 }
4383 spin_unlock(&release_list_lock);
4384 mutex_unlock(&cgroup_mutex);
4385}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004386
4387static int __init cgroup_disable(char *str)
4388{
4389 int i;
4390 char *token;
4391
4392 while ((token = strsep(&str, ",")) != NULL) {
4393 if (!*token)
4394 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08004395 /*
4396 * cgroup_disable, being at boot time, can't know about module
4397 * subsystems, so we don't worry about them.
4398 */
4399 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004400 struct cgroup_subsys *ss = subsys[i];
4401
4402 if (!strcmp(token, ss->name)) {
4403 ss->disabled = 1;
4404 printk(KERN_INFO "Disabling %s control group"
4405 " subsystem\n", ss->name);
4406 break;
4407 }
4408 }
4409 }
4410 return 1;
4411}
4412__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004413
4414/*
4415 * Functons for CSS ID.
4416 */
4417
4418/*
4419 *To get ID other than 0, this should be called when !cgroup_is_removed().
4420 */
4421unsigned short css_id(struct cgroup_subsys_state *css)
4422{
4423 struct css_id *cssid = rcu_dereference(css->id);
4424
4425 if (cssid)
4426 return cssid->id;
4427 return 0;
4428}
Ben Blum67523c42010-03-10 15:22:11 -08004429EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004430
4431unsigned short css_depth(struct cgroup_subsys_state *css)
4432{
4433 struct css_id *cssid = rcu_dereference(css->id);
4434
4435 if (cssid)
4436 return cssid->depth;
4437 return 0;
4438}
Ben Blum67523c42010-03-10 15:22:11 -08004439EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004440
4441bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07004442 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004443{
4444 struct css_id *child_id = rcu_dereference(child->id);
4445 struct css_id *root_id = rcu_dereference(root->id);
4446
4447 if (!child_id || !root_id || (child_id->depth < root_id->depth))
4448 return false;
4449 return child_id->stack[root_id->depth] == root_id->id;
4450}
4451
4452static void __free_css_id_cb(struct rcu_head *head)
4453{
4454 struct css_id *id;
4455
4456 id = container_of(head, struct css_id, rcu_head);
4457 kfree(id);
4458}
4459
4460void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
4461{
4462 struct css_id *id = css->id;
4463 /* When this is called before css_id initialization, id can be NULL */
4464 if (!id)
4465 return;
4466
4467 BUG_ON(!ss->use_id);
4468
4469 rcu_assign_pointer(id->css, NULL);
4470 rcu_assign_pointer(css->id, NULL);
4471 spin_lock(&ss->id_lock);
4472 idr_remove(&ss->idr, id->id);
4473 spin_unlock(&ss->id_lock);
4474 call_rcu(&id->rcu_head, __free_css_id_cb);
4475}
Ben Blum67523c42010-03-10 15:22:11 -08004476EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004477
4478/*
4479 * This is called by init or create(). Then, calls to this function are
4480 * always serialized (By cgroup_mutex() at create()).
4481 */
4482
4483static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
4484{
4485 struct css_id *newid;
4486 int myid, error, size;
4487
4488 BUG_ON(!ss->use_id);
4489
4490 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
4491 newid = kzalloc(size, GFP_KERNEL);
4492 if (!newid)
4493 return ERR_PTR(-ENOMEM);
4494 /* get id */
4495 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
4496 error = -ENOMEM;
4497 goto err_out;
4498 }
4499 spin_lock(&ss->id_lock);
4500 /* Don't use 0. allocates an ID of 1-65535 */
4501 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
4502 spin_unlock(&ss->id_lock);
4503
4504 /* Returns error when there are no free spaces for new ID.*/
4505 if (error) {
4506 error = -ENOSPC;
4507 goto err_out;
4508 }
4509 if (myid > CSS_ID_MAX)
4510 goto remove_idr;
4511
4512 newid->id = myid;
4513 newid->depth = depth;
4514 return newid;
4515remove_idr:
4516 error = -ENOSPC;
4517 spin_lock(&ss->id_lock);
4518 idr_remove(&ss->idr, myid);
4519 spin_unlock(&ss->id_lock);
4520err_out:
4521 kfree(newid);
4522 return ERR_PTR(error);
4523
4524}
4525
Ben Blume6a11052010-03-10 15:22:09 -08004526static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
4527 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004528{
4529 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004530
4531 spin_lock_init(&ss->id_lock);
4532 idr_init(&ss->idr);
4533
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004534 newid = get_new_cssid(ss, 0);
4535 if (IS_ERR(newid))
4536 return PTR_ERR(newid);
4537
4538 newid->stack[0] = newid->id;
4539 newid->css = rootcss;
4540 rootcss->id = newid;
4541 return 0;
4542}
4543
4544static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
4545 struct cgroup *child)
4546{
4547 int subsys_id, i, depth = 0;
4548 struct cgroup_subsys_state *parent_css, *child_css;
4549 struct css_id *child_id, *parent_id = NULL;
4550
4551 subsys_id = ss->subsys_id;
4552 parent_css = parent->subsys[subsys_id];
4553 child_css = child->subsys[subsys_id];
4554 depth = css_depth(parent_css) + 1;
4555 parent_id = parent_css->id;
4556
4557 child_id = get_new_cssid(ss, depth);
4558 if (IS_ERR(child_id))
4559 return PTR_ERR(child_id);
4560
4561 for (i = 0; i < depth; i++)
4562 child_id->stack[i] = parent_id->stack[i];
4563 child_id->stack[depth] = child_id->id;
4564 /*
4565 * child_id->css pointer will be set after this cgroup is available
4566 * see cgroup_populate_dir()
4567 */
4568 rcu_assign_pointer(child_css->id, child_id);
4569
4570 return 0;
4571}
4572
4573/**
4574 * css_lookup - lookup css by id
4575 * @ss: cgroup subsys to be looked into.
4576 * @id: the id
4577 *
4578 * Returns pointer to cgroup_subsys_state if there is valid one with id.
4579 * NULL if not. Should be called under rcu_read_lock()
4580 */
4581struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
4582{
4583 struct css_id *cssid = NULL;
4584
4585 BUG_ON(!ss->use_id);
4586 cssid = idr_find(&ss->idr, id);
4587
4588 if (unlikely(!cssid))
4589 return NULL;
4590
4591 return rcu_dereference(cssid->css);
4592}
Ben Blum67523c42010-03-10 15:22:11 -08004593EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004594
4595/**
4596 * css_get_next - lookup next cgroup under specified hierarchy.
4597 * @ss: pointer to subsystem
4598 * @id: current position of iteration.
4599 * @root: pointer to css. search tree under this.
4600 * @foundid: position of found object.
4601 *
4602 * Search next css under the specified hierarchy of rootid. Calling under
4603 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
4604 */
4605struct cgroup_subsys_state *
4606css_get_next(struct cgroup_subsys *ss, int id,
4607 struct cgroup_subsys_state *root, int *foundid)
4608{
4609 struct cgroup_subsys_state *ret = NULL;
4610 struct css_id *tmp;
4611 int tmpid;
4612 int rootid = css_id(root);
4613 int depth = css_depth(root);
4614
4615 if (!rootid)
4616 return NULL;
4617
4618 BUG_ON(!ss->use_id);
4619 /* fill start point for scan */
4620 tmpid = id;
4621 while (1) {
4622 /*
4623 * scan next entry from bitmap(tree), tmpid is updated after
4624 * idr_get_next().
4625 */
4626 spin_lock(&ss->id_lock);
4627 tmp = idr_get_next(&ss->idr, &tmpid);
4628 spin_unlock(&ss->id_lock);
4629
4630 if (!tmp)
4631 break;
4632 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
4633 ret = rcu_dereference(tmp->css);
4634 if (ret) {
4635 *foundid = tmpid;
4636 break;
4637 }
4638 }
4639 /* continue to scan from next id */
4640 tmpid = tmpid + 1;
4641 }
4642 return ret;
4643}
4644
Paul Menagefe693432009-09-23 15:56:20 -07004645#ifdef CONFIG_CGROUP_DEBUG
4646static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
4647 struct cgroup *cont)
4648{
4649 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4650
4651 if (!css)
4652 return ERR_PTR(-ENOMEM);
4653
4654 return css;
4655}
4656
4657static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
4658{
4659 kfree(cont->subsys[debug_subsys_id]);
4660}
4661
4662static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
4663{
4664 return atomic_read(&cont->count);
4665}
4666
4667static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
4668{
4669 return cgroup_task_count(cont);
4670}
4671
4672static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
4673{
4674 return (u64)(unsigned long)current->cgroups;
4675}
4676
4677static u64 current_css_set_refcount_read(struct cgroup *cont,
4678 struct cftype *cft)
4679{
4680 u64 count;
4681
4682 rcu_read_lock();
4683 count = atomic_read(&current->cgroups->refcount);
4684 rcu_read_unlock();
4685 return count;
4686}
4687
Paul Menage7717f7b2009-09-23 15:56:22 -07004688static int current_css_set_cg_links_read(struct cgroup *cont,
4689 struct cftype *cft,
4690 struct seq_file *seq)
4691{
4692 struct cg_cgroup_link *link;
4693 struct css_set *cg;
4694
4695 read_lock(&css_set_lock);
4696 rcu_read_lock();
4697 cg = rcu_dereference(current->cgroups);
4698 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
4699 struct cgroup *c = link->cgrp;
4700 const char *name;
4701
4702 if (c->dentry)
4703 name = c->dentry->d_name.name;
4704 else
4705 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004706 seq_printf(seq, "Root %d group %s\n",
4707 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07004708 }
4709 rcu_read_unlock();
4710 read_unlock(&css_set_lock);
4711 return 0;
4712}
4713
4714#define MAX_TASKS_SHOWN_PER_CSS 25
4715static int cgroup_css_links_read(struct cgroup *cont,
4716 struct cftype *cft,
4717 struct seq_file *seq)
4718{
4719 struct cg_cgroup_link *link;
4720
4721 read_lock(&css_set_lock);
4722 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
4723 struct css_set *cg = link->cg;
4724 struct task_struct *task;
4725 int count = 0;
4726 seq_printf(seq, "css_set %p\n", cg);
4727 list_for_each_entry(task, &cg->tasks, cg_list) {
4728 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4729 seq_puts(seq, " ...\n");
4730 break;
4731 } else {
4732 seq_printf(seq, " task %d\n",
4733 task_pid_vnr(task));
4734 }
4735 }
4736 }
4737 read_unlock(&css_set_lock);
4738 return 0;
4739}
4740
Paul Menagefe693432009-09-23 15:56:20 -07004741static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
4742{
4743 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
4744}
4745
4746static struct cftype debug_files[] = {
4747 {
4748 .name = "cgroup_refcount",
4749 .read_u64 = cgroup_refcount_read,
4750 },
4751 {
4752 .name = "taskcount",
4753 .read_u64 = debug_taskcount_read,
4754 },
4755
4756 {
4757 .name = "current_css_set",
4758 .read_u64 = current_css_set_read,
4759 },
4760
4761 {
4762 .name = "current_css_set_refcount",
4763 .read_u64 = current_css_set_refcount_read,
4764 },
4765
4766 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004767 .name = "current_css_set_cg_links",
4768 .read_seq_string = current_css_set_cg_links_read,
4769 },
4770
4771 {
4772 .name = "cgroup_css_links",
4773 .read_seq_string = cgroup_css_links_read,
4774 },
4775
4776 {
Paul Menagefe693432009-09-23 15:56:20 -07004777 .name = "releasable",
4778 .read_u64 = releasable_read,
4779 },
4780};
4781
4782static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
4783{
4784 return cgroup_add_files(cont, ss, debug_files,
4785 ARRAY_SIZE(debug_files));
4786}
4787
4788struct cgroup_subsys debug_subsys = {
4789 .name = "debug",
4790 .create = debug_create,
4791 .destroy = debug_destroy,
4792 .populate = debug_populate,
4793 .subsys_id = debug_subsys_id,
4794};
4795#endif /* CONFIG_CGROUP_DEBUG */