blob: 0de816098bfc78451632b1a218f05d81a237def0 [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>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.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>
eparis@redhat2ce97382011-06-02 21:20:51 +100034#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/kernel.h>
36#include <linux/list.h>
37#include <linux/mm.h>
38#include <linux/mutex.h>
39#include <linux/mount.h>
40#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070041#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070042#include <linux/rcupdate.h>
43#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070044#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/seq_file.h>
46#include <linux/slab.h>
47#include <linux/magic.h>
48#include <linux/spinlock.h>
49#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080052#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070053#include <linux/delayacct.h>
54#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070055#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040056#include <linux/namei.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>
Ben Blumd8466872011-05-26 16:25:21 -070062#include <linux/flex_array.h> /* used in cgroup_attach_proc */
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma60063492011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heoe25e2cb2011-12-12 18:12:21 -080066/*
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
69 *
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
75 *
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
78 *
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
80 * breaks it.
81 */
Paul Menage81a6a5c2007-10-18 23:39:38 -070082static DEFINE_MUTEX(cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -080083static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070084
Ben Blumaae8aab2010-03-10 15:22:07 -080085/*
86 * Generate an array of cgroup subsystem pointers. At boot time, this is
87 * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
88 * registered after that. The mutable section of this array is protected by
89 * cgroup_mutex.
90 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070091#define SUBSYS(_x) &_x ## _subsys,
Ben Blumaae8aab2010-03-10 15:22:07 -080092static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070093#include <linux/cgroup_subsys.h>
94};
95
Paul Menagec6d57f32009-09-23 15:56:19 -070096#define MAX_CGROUP_ROOT_NAMELEN 64
97
Paul Menageddbcc7e2007-10-18 23:39:30 -070098/*
99 * A cgroupfs_root represents the root of a cgroup hierarchy,
100 * and may be associated with a superblock to form an active
101 * hierarchy
102 */
103struct cgroupfs_root {
104 struct super_block *sb;
105
106 /*
107 * The bitmask of subsystems intended to be attached to this
108 * hierarchy
109 */
110 unsigned long subsys_bits;
111
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700112 /* Unique id for this hierarchy. */
113 int hierarchy_id;
114
Paul Menageddbcc7e2007-10-18 23:39:30 -0700115 /* The bitmask of subsystems currently attached to this hierarchy */
116 unsigned long actual_subsys_bits;
117
118 /* A list running through the attached subsystems */
119 struct list_head subsys_list;
120
121 /* The root cgroup for this hierarchy */
122 struct cgroup top_cgroup;
123
124 /* Tracks how many cgroups are currently defined in hierarchy.*/
125 int number_of_cgroups;
126
Li Zefane5f6a862009-01-07 18:07:41 -0800127 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700128 struct list_head root_list;
129
130 /* Hierarchy-specific flags */
131 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700132
Paul Menagee788e062008-07-25 01:46:59 -0700133 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700134 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700135
136 /* The name for this hierarchy - may be empty */
137 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138};
139
Paul Menageddbcc7e2007-10-18 23:39:30 -0700140/*
141 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
142 * subsystems that are otherwise unattached - it never has more than a
143 * single cgroup, and all tasks are part of that cgroup.
144 */
145static struct cgroupfs_root rootnode;
146
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700147/*
148 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
149 * cgroup_subsys->use_id != 0.
150 */
151#define CSS_ID_MAX (65535)
152struct css_id {
153 /*
154 * The css to which this ID points. This pointer is set to valid value
155 * after cgroup is populated. If cgroup is removed, this will be NULL.
156 * This pointer is expected to be RCU-safe because destroy()
157 * is called after synchronize_rcu(). But for safe use, css_is_removed()
158 * css_tryget() should be used for avoiding race.
159 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100160 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700161 /*
162 * ID of this css.
163 */
164 unsigned short id;
165 /*
166 * Depth in hierarchy which this ID belongs to.
167 */
168 unsigned short depth;
169 /*
170 * ID is freed by RCU. (and lookup routine is RCU safe.)
171 */
172 struct rcu_head rcu_head;
173 /*
174 * Hierarchy of CSS ID belongs to.
175 */
176 unsigned short stack[0]; /* Array of Length (depth+1) */
177};
178
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800179/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300180 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800181 */
182struct cgroup_event {
183 /*
184 * Cgroup which the event belongs to.
185 */
186 struct cgroup *cgrp;
187 /*
188 * Control file which the event associated.
189 */
190 struct cftype *cft;
191 /*
192 * eventfd to signal userspace about the event.
193 */
194 struct eventfd_ctx *eventfd;
195 /*
196 * Each of these stored in a list by the cgroup.
197 */
198 struct list_head list;
199 /*
200 * All fields below needed to unregister event when
201 * userspace closes eventfd.
202 */
203 poll_table pt;
204 wait_queue_head_t *wqh;
205 wait_queue_t wait;
206 struct work_struct remove;
207};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700208
Paul Menageddbcc7e2007-10-18 23:39:30 -0700209/* The list of hierarchy roots */
210
211static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700212static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700213
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700214static DEFINE_IDA(hierarchy_ida);
215static int next_hierarchy_id;
216static DEFINE_SPINLOCK(hierarchy_id_lock);
217
Paul Menageddbcc7e2007-10-18 23:39:30 -0700218/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
219#define dummytop (&rootnode.top_cgroup)
220
221/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800222 * check for fork/exit handlers to call. This avoids us having to do
223 * extra work in the fork/exit path if none of the subsystems need to
224 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700226static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700227
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800228#ifdef CONFIG_PROVE_LOCKING
229int cgroup_lock_is_held(void)
230{
231 return lockdep_is_held(&cgroup_mutex);
232}
233#else /* #ifdef CONFIG_PROVE_LOCKING */
234int cgroup_lock_is_held(void)
235{
236 return mutex_is_locked(&cgroup_mutex);
237}
238#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
239
240EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
241
Paul Menageddbcc7e2007-10-18 23:39:30 -0700242/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700243inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700244{
Paul Menagebd89aab2007-10-18 23:40:44 -0700245 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700246}
247
248/* bits in struct cgroupfs_root flags field */
249enum {
250 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
251};
252
Adrian Bunke9685a02008-02-07 00:13:46 -0800253static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700254{
255 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700256 (1 << CGRP_RELEASABLE) |
257 (1 << CGRP_NOTIFY_ON_RELEASE);
258 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700259}
260
Adrian Bunke9685a02008-02-07 00:13:46 -0800261static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700262{
Paul Menagebd89aab2007-10-18 23:40:44 -0700263 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700264}
265
Daniel Lezcano97978e62010-10-27 15:33:35 -0700266static int clone_children(const struct cgroup *cgrp)
267{
268 return test_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
269}
270
Paul Menageddbcc7e2007-10-18 23:39:30 -0700271/*
272 * for_each_subsys() allows you to iterate on each subsystem attached to
273 * an active hierarchy
274 */
275#define for_each_subsys(_root, _ss) \
276list_for_each_entry(_ss, &_root->subsys_list, sibling)
277
Li Zefane5f6a862009-01-07 18:07:41 -0800278/* for_each_active_root() allows you to iterate across the active hierarchies */
279#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700280list_for_each_entry(_root, &roots, root_list)
281
Paul Menage81a6a5c2007-10-18 23:39:38 -0700282/* the list of cgroups eligible for automatic release. Protected by
283 * release_list_lock */
284static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200285static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700286static void cgroup_release_agent(struct work_struct *work);
287static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700288static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700289
Paul Menage817929e2007-10-18 23:39:36 -0700290/* Link structure for associating css_set objects with cgroups */
291struct cg_cgroup_link {
292 /*
293 * List running through cg_cgroup_links associated with a
294 * cgroup, anchored on cgroup->css_sets
295 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700296 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700297 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700298 /*
299 * List running through cg_cgroup_links pointing at a
300 * single css_set object, anchored on css_set->cg_links
301 */
302 struct list_head cg_link_list;
303 struct css_set *cg;
304};
305
306/* The default css_set - used by init and its children prior to any
307 * hierarchies being mounted. It contains a pointer to the root state
308 * for each subsystem. Also used to anchor the list of css_sets. Not
309 * reference-counted, to improve performance when child cgroups
310 * haven't been created.
311 */
312
313static struct css_set init_css_set;
314static struct cg_cgroup_link init_css_set_link;
315
Ben Blume6a11052010-03-10 15:22:09 -0800316static int cgroup_init_idr(struct cgroup_subsys *ss,
317 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700318
Paul Menage817929e2007-10-18 23:39:36 -0700319/* css_set_lock protects the list of css_set objects, and the
320 * chain of tasks off each css_set. Nests outside task->alloc_lock
321 * due to cgroup_iter_start() */
322static DEFINE_RWLOCK(css_set_lock);
323static int css_set_count;
324
Paul Menage7717f7b2009-09-23 15:56:22 -0700325/*
326 * hash table for cgroup groups. This improves the performance to find
327 * an existing css_set. This hash doesn't (currently) take into
328 * account cgroups in empty hierarchies.
329 */
Li Zefan472b1052008-04-29 01:00:11 -0700330#define CSS_SET_HASH_BITS 7
331#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
332static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
333
334static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
335{
336 int i;
337 int index;
338 unsigned long tmp = 0UL;
339
340 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
341 tmp += (unsigned long)css[i];
342 tmp = (tmp >> 16) ^ tmp;
343
344 index = hash_long(tmp, CSS_SET_HASH_BITS);
345
346 return &css_set_table[index];
347}
348
Paul Menage817929e2007-10-18 23:39:36 -0700349/* We don't maintain the lists running through each css_set to its
350 * task until after the first call to cgroup_iter_start(). This
351 * reduces the fork()/exit() overhead for people who have cgroups
352 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700353static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700354
Colin Crossc0f6fa82010-11-23 21:37:03 -0800355/*
356 * refcounted get/put for css_set objects
357 */
358static inline void get_css_set(struct css_set *cg)
359{
360 atomic_inc(&cg->refcount);
361}
362
363static void put_css_set(struct css_set *cg)
Paul Menageb4f48b62007-10-18 23:39:33 -0700364{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700365 struct cg_cgroup_link *link;
366 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700367 /*
368 * Ensure that the refcount doesn't hit zero while any readers
369 * can see it. Similar to atomic_dec_and_lock(), but for an
370 * rwlock
371 */
372 if (atomic_add_unless(&cg->refcount, -1, 1))
373 return;
374 write_lock(&css_set_lock);
375 if (!atomic_dec_and_test(&cg->refcount)) {
376 write_unlock(&css_set_lock);
377 return;
378 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700379
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700380 /* This css_set is dead. unlink it and release cgroup refcounts */
381 hlist_del(&cg->hlist);
382 css_set_count--;
383
384 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
385 cg_link_list) {
386 struct cgroup *cgrp = link->cgrp;
387 list_del(&link->cg_link_list);
388 list_del(&link->cgrp_link_list);
Colin Crossc0f6fa82010-11-23 21:37:03 -0800389 if (atomic_dec_and_test(&cgrp->count))
Paul Menagebd89aab2007-10-18 23:40:44 -0700390 check_for_release(cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700391
392 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700393 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700394
395 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800396 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700397}
398
399/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700400 * compare_css_sets - helper function for find_existing_css_set().
401 * @cg: candidate css_set being tested
402 * @old_cg: existing css_set for a task
403 * @new_cgrp: cgroup that's being entered by the task
404 * @template: desired set of css pointers in css_set (pre-calculated)
405 *
406 * Returns true if "cg" matches "old_cg" except for the hierarchy
407 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
408 */
409static bool compare_css_sets(struct css_set *cg,
410 struct css_set *old_cg,
411 struct cgroup *new_cgrp,
412 struct cgroup_subsys_state *template[])
413{
414 struct list_head *l1, *l2;
415
416 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
417 /* Not all subsystems matched */
418 return false;
419 }
420
421 /*
422 * Compare cgroup pointers in order to distinguish between
423 * different cgroups in heirarchies with no subsystems. We
424 * could get by with just this check alone (and skip the
425 * memcmp above) but on most setups the memcmp check will
426 * avoid the need for this more expensive check on almost all
427 * candidates.
428 */
429
430 l1 = &cg->cg_links;
431 l2 = &old_cg->cg_links;
432 while (1) {
433 struct cg_cgroup_link *cgl1, *cgl2;
434 struct cgroup *cg1, *cg2;
435
436 l1 = l1->next;
437 l2 = l2->next;
438 /* See if we reached the end - both lists are equal length. */
439 if (l1 == &cg->cg_links) {
440 BUG_ON(l2 != &old_cg->cg_links);
441 break;
442 } else {
443 BUG_ON(l2 == &old_cg->cg_links);
444 }
445 /* Locate the cgroups associated with these links. */
446 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
447 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
448 cg1 = cgl1->cgrp;
449 cg2 = cgl2->cgrp;
450 /* Hierarchies should be linked in the same order. */
451 BUG_ON(cg1->root != cg2->root);
452
453 /*
454 * If this hierarchy is the hierarchy of the cgroup
455 * that's changing, then we need to check that this
456 * css_set points to the new cgroup; if it's any other
457 * hierarchy, then this css_set should point to the
458 * same cgroup as the old css_set.
459 */
460 if (cg1->root == new_cgrp->root) {
461 if (cg1 != new_cgrp)
462 return false;
463 } else {
464 if (cg1 != cg2)
465 return false;
466 }
467 }
468 return true;
469}
470
471/*
Paul Menage817929e2007-10-18 23:39:36 -0700472 * find_existing_css_set() is a helper for
473 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700474 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700475 *
476 * oldcg: the cgroup group that we're using before the cgroup
477 * transition
478 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700479 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700480 *
481 * template: location in which to build the desired set of subsystem
482 * state objects for the new cgroup group
483 */
Paul Menage817929e2007-10-18 23:39:36 -0700484static struct css_set *find_existing_css_set(
485 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700486 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700487 struct cgroup_subsys_state *template[])
488{
489 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700490 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700491 struct hlist_head *hhead;
492 struct hlist_node *node;
493 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700494
Ben Blumaae8aab2010-03-10 15:22:07 -0800495 /*
496 * Build the set of subsystem state objects that we want to see in the
497 * new css_set. while subsystems can change globally, the entries here
498 * won't change, so no need for locking.
499 */
Paul Menage817929e2007-10-18 23:39:36 -0700500 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800501 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700502 /* Subsystem is in this hierarchy. So we want
503 * the subsystem state from the new
504 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700505 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700506 } else {
507 /* Subsystem is not in this hierarchy, so we
508 * don't want to change the subsystem state */
509 template[i] = oldcg->subsys[i];
510 }
511 }
512
Li Zefan472b1052008-04-29 01:00:11 -0700513 hhead = css_set_hash(template);
514 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700515 if (!compare_css_sets(cg, oldcg, cgrp, template))
516 continue;
517
518 /* This css_set matches what we need */
519 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700520 }
Paul Menage817929e2007-10-18 23:39:36 -0700521
522 /* No existing cgroup group matched */
523 return NULL;
524}
525
Paul Menage817929e2007-10-18 23:39:36 -0700526static void free_cg_links(struct list_head *tmp)
527{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700528 struct cg_cgroup_link *link;
529 struct cg_cgroup_link *saved_link;
530
531 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700532 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700533 kfree(link);
534 }
535}
536
537/*
Li Zefan36553432008-07-29 22:33:19 -0700538 * allocate_cg_links() allocates "count" cg_cgroup_link structures
539 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
540 * success or a negative error
541 */
542static int allocate_cg_links(int count, struct list_head *tmp)
543{
544 struct cg_cgroup_link *link;
545 int i;
546 INIT_LIST_HEAD(tmp);
547 for (i = 0; i < count; i++) {
548 link = kmalloc(sizeof(*link), GFP_KERNEL);
549 if (!link) {
550 free_cg_links(tmp);
551 return -ENOMEM;
552 }
553 list_add(&link->cgrp_link_list, tmp);
554 }
555 return 0;
556}
557
Li Zefanc12f65d2009-01-07 18:07:42 -0800558/**
559 * link_css_set - a helper function to link a css_set to a cgroup
560 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
561 * @cg: the css_set to be linked
562 * @cgrp: the destination cgroup
563 */
564static void link_css_set(struct list_head *tmp_cg_links,
565 struct css_set *cg, struct cgroup *cgrp)
566{
567 struct cg_cgroup_link *link;
568
569 BUG_ON(list_empty(tmp_cg_links));
570 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
571 cgrp_link_list);
572 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700573 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700574 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800575 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700576 /*
577 * Always add links to the tail of the list so that the list
578 * is sorted by order of hierarchy creation
579 */
580 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800581}
582
Li Zefan36553432008-07-29 22:33:19 -0700583/*
Paul Menage817929e2007-10-18 23:39:36 -0700584 * find_css_set() takes an existing cgroup group and a
585 * cgroup object, and returns a css_set object that's
586 * equivalent to the old group, but with the given cgroup
587 * substituted into the appropriate hierarchy. Must be called with
588 * cgroup_mutex held
589 */
Paul Menage817929e2007-10-18 23:39:36 -0700590static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700591 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700592{
593 struct css_set *res;
594 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700595
596 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700597
Li Zefan472b1052008-04-29 01:00:11 -0700598 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700599 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700600
Paul Menage817929e2007-10-18 23:39:36 -0700601 /* First see if we already have a cgroup group that matches
602 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700603 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700604 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700605 if (res)
606 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700607 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700608
609 if (res)
610 return res;
611
612 res = kmalloc(sizeof(*res), GFP_KERNEL);
613 if (!res)
614 return NULL;
615
616 /* Allocate all the cg_cgroup_link objects that we'll need */
617 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
618 kfree(res);
619 return NULL;
620 }
621
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700622 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700623 INIT_LIST_HEAD(&res->cg_links);
624 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700625 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700626
627 /* Copy the set of subsystem state objects generated in
628 * find_existing_css_set() */
629 memcpy(res->subsys, template, sizeof(res->subsys));
630
631 write_lock(&css_set_lock);
632 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700633 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
634 struct cgroup *c = link->cgrp;
635 if (c->root == cgrp->root)
636 c = cgrp;
637 link_css_set(&tmp_cg_links, res, c);
638 }
Paul Menage817929e2007-10-18 23:39:36 -0700639
640 BUG_ON(!list_empty(&tmp_cg_links));
641
Paul Menage817929e2007-10-18 23:39:36 -0700642 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700643
644 /* Add this cgroup group to the hash table */
645 hhead = css_set_hash(res->subsys);
646 hlist_add_head(&res->hlist, hhead);
647
Paul Menage817929e2007-10-18 23:39:36 -0700648 write_unlock(&css_set_lock);
649
650 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700651}
652
Paul Menageddbcc7e2007-10-18 23:39:30 -0700653/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700654 * Return the cgroup for "task" from the given hierarchy. Must be
655 * called with cgroup_mutex held.
656 */
657static struct cgroup *task_cgroup_from_root(struct task_struct *task,
658 struct cgroupfs_root *root)
659{
660 struct css_set *css;
661 struct cgroup *res = NULL;
662
663 BUG_ON(!mutex_is_locked(&cgroup_mutex));
664 read_lock(&css_set_lock);
665 /*
666 * No need to lock the task - since we hold cgroup_mutex the
667 * task can't change groups, so the only thing that can happen
668 * is that it exits and its css is set back to init_css_set.
669 */
670 css = task->cgroups;
671 if (css == &init_css_set) {
672 res = &root->top_cgroup;
673 } else {
674 struct cg_cgroup_link *link;
675 list_for_each_entry(link, &css->cg_links, cg_link_list) {
676 struct cgroup *c = link->cgrp;
677 if (c->root == root) {
678 res = c;
679 break;
680 }
681 }
682 }
683 read_unlock(&css_set_lock);
684 BUG_ON(!res);
685 return res;
686}
687
688/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700689 * There is one global cgroup mutex. We also require taking
690 * task_lock() when dereferencing a task's cgroup subsys pointers.
691 * See "The task_lock() exception", at the end of this comment.
692 *
693 * A task must hold cgroup_mutex to modify cgroups.
694 *
695 * Any task can increment and decrement the count field without lock.
696 * So in general, code holding cgroup_mutex can't rely on the count
697 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800698 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700699 * means that no tasks are currently attached, therefore there is no
700 * way a task attached to that cgroup can fork (the other way to
701 * increment the count). So code holding cgroup_mutex can safely
702 * assume that if the count is zero, it will stay zero. Similarly, if
703 * a task holds cgroup_mutex on a cgroup with zero count, it
704 * knows that the cgroup won't be removed, as cgroup_rmdir()
705 * needs that mutex.
706 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700707 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
708 * (usually) take cgroup_mutex. These are the two most performance
709 * critical pieces of code here. The exception occurs on cgroup_exit(),
710 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
711 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800712 * to the release agent with the name of the cgroup (path relative to
713 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700714 *
715 * A cgroup can only be deleted if both its 'count' of using tasks
716 * is zero, and its list of 'children' cgroups is empty. Since all
717 * tasks in the system use _some_ cgroup, and since there is always at
718 * least one task in the system (init, pid == 1), therefore, top_cgroup
719 * always has either children cgroups and/or using tasks. So we don't
720 * need a special hack to ensure that top_cgroup cannot be deleted.
721 *
722 * The task_lock() exception
723 *
724 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800725 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800726 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700727 * several performance critical places that need to reference
728 * task->cgroup without the expense of grabbing a system global
729 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800730 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700731 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
732 * the task_struct routinely used for such matters.
733 *
734 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800735 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700736 */
737
Paul Menageddbcc7e2007-10-18 23:39:30 -0700738/**
739 * cgroup_lock - lock out any changes to cgroup structures
740 *
741 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700742void cgroup_lock(void)
743{
744 mutex_lock(&cgroup_mutex);
745}
Ben Blum67523c42010-03-10 15:22:11 -0800746EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700747
748/**
749 * cgroup_unlock - release lock on cgroup changes
750 *
751 * Undo the lock taken in a previous cgroup_lock() call.
752 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700753void cgroup_unlock(void)
754{
755 mutex_unlock(&cgroup_mutex);
756}
Ben Blum67523c42010-03-10 15:22:11 -0800757EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700758
759/*
760 * A couple of forward declarations required, due to cyclic reference loop:
761 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
762 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
763 * -> cgroup_mkdir.
764 */
765
Al Viro18bb1db2011-07-26 01:41:39 -0400766static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viroc72a04e2011-01-14 05:31:45 +0000767static struct dentry *cgroup_lookup(struct inode *, struct dentry *, struct nameidata *);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700768static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700769static int cgroup_populate_dir(struct cgroup *cgrp);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700770static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700771static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700772
773static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200774 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700775 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700776};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700777
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700778static int alloc_css_id(struct cgroup_subsys *ss,
779 struct cgroup *parent, struct cgroup *child);
780
Al Viroa5e7ed32011-07-26 01:55:55 -0400781static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700782{
783 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700784
785 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400786 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700787 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100788 inode->i_uid = current_fsuid();
789 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700790 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
791 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
792 }
793 return inode;
794}
795
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800796/*
797 * Call subsys's pre_destroy handler.
798 * This is called before css refcnt check.
799 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700800static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800801{
802 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700803 int ret = 0;
804
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800805 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700806 if (ss->pre_destroy) {
Li Zefan761b3ef2012-01-31 13:47:36 +0800807 ret = ss->pre_destroy(cgrp);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700808 if (ret)
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -0800809 break;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700810 }
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800811
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700812 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800813}
814
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815static void cgroup_diput(struct dentry *dentry, struct inode *inode)
816{
817 /* is dentry a directory ? if so, kfree() associated cgroup */
818 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700819 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800820 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700821 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700822 /* It's possible for external users to be holding css
823 * reference counts on a cgroup; css_put() needs to
824 * be able to access the cgroup after decrementing
825 * the reference count in order to know if it needs to
826 * queue the cgroup to be handled by the release
827 * agent */
828 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800829
830 mutex_lock(&cgroup_mutex);
831 /*
832 * Release the subsystem state objects.
833 */
Li Zefan75139b82009-01-07 18:07:33 -0800834 for_each_subsys(cgrp->root, ss)
Li Zefan761b3ef2012-01-31 13:47:36 +0800835 ss->destroy(cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800836
837 cgrp->root->number_of_cgroups--;
838 mutex_unlock(&cgroup_mutex);
839
Paul Menagea47295e2009-01-07 18:07:44 -0800840 /*
841 * Drop the active superblock reference that we took when we
842 * created the cgroup
843 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800844 deactivate_super(cgrp->root->sb);
845
Ben Blum72a8cb32009-09-23 15:56:27 -0700846 /*
847 * if we're getting rid of the cgroup, refcount should ensure
848 * that there are no pidlists left.
849 */
850 BUG_ON(!list_empty(&cgrp->pidlists));
851
Lai Jiangshanf2da1c42011-03-15 17:55:16 +0800852 kfree_rcu(cgrp, rcu_head);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700853 }
854 iput(inode);
855}
856
Al Viroc72a04e2011-01-14 05:31:45 +0000857static int cgroup_delete(const struct dentry *d)
858{
859 return 1;
860}
861
Paul Menageddbcc7e2007-10-18 23:39:30 -0700862static void remove_dir(struct dentry *d)
863{
864 struct dentry *parent = dget(d->d_parent);
865
866 d_delete(d);
867 simple_rmdir(parent->d_inode, d);
868 dput(parent);
869}
870
871static void cgroup_clear_directory(struct dentry *dentry)
872{
873 struct list_head *node;
874
875 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100876 spin_lock(&dentry->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700877 node = dentry->d_subdirs.next;
878 while (node != &dentry->d_subdirs) {
879 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100880
881 spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700882 list_del_init(node);
883 if (d->d_inode) {
884 /* This should never be called on a cgroup
885 * directory with child cgroups */
886 BUG_ON(d->d_inode->i_mode & S_IFDIR);
Nick Piggindc0474b2011-01-07 17:49:43 +1100887 dget_dlock(d);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100888 spin_unlock(&d->d_lock);
889 spin_unlock(&dentry->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700890 d_delete(d);
891 simple_unlink(dentry->d_inode, d);
892 dput(d);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100893 spin_lock(&dentry->d_lock);
894 } else
895 spin_unlock(&d->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700896 node = dentry->d_subdirs.next;
897 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100898 spin_unlock(&dentry->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700899}
900
901/*
902 * NOTE : the dentry must have been dget()'ed
903 */
904static void cgroup_d_remove_dir(struct dentry *dentry)
905{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100906 struct dentry *parent;
907
Paul Menageddbcc7e2007-10-18 23:39:30 -0700908 cgroup_clear_directory(dentry);
909
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100910 parent = dentry->d_parent;
911 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800912 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700913 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100914 spin_unlock(&dentry->d_lock);
915 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700916 remove_dir(dentry);
917}
918
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700919/*
920 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
921 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
922 * reference to css->refcnt. In general, this refcnt is expected to goes down
923 * to zero, soon.
924 *
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700925 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700926 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +0200927static DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700928
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700929static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700930{
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700931 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700932 wake_up_all(&cgroup_rmdir_waitq);
933}
934
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700935void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
936{
937 css_get(css);
938}
939
940void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
941{
942 cgroup_wakeup_rmdir_waiter(css->cgroup);
943 css_put(css);
944}
945
Ben Blumaae8aab2010-03-10 15:22:07 -0800946/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800947 * Call with cgroup_mutex held. Drops reference counts on modules, including
948 * any duplicate ones that parse_cgroupfs_options took. If this function
949 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800950 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700951static int rebind_subsystems(struct cgroupfs_root *root,
952 unsigned long final_bits)
953{
954 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700955 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700956 int i;
957
Ben Blumaae8aab2010-03-10 15:22:07 -0800958 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800959 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800960
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)
Li Zefan761b3ef2012-01-31 13:47:36 +08001004 ss->bind(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)
Li Zefan761b3ef2012-01-31 13:47:36 +08001014 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015 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
Al Viro34c80b12011-12-08 21:32:45 -05001045static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046{
Al Viro34c80b12011-12-08 21:32:45 -05001047 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048 struct cgroup_subsys *ss;
1049
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001050 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 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);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001057 if (clone_children(&root->top_cgroup))
1058 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001059 if (strlen(root->name))
1060 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001061 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001062 return 0;
1063}
1064
1065struct cgroup_sb_opts {
1066 unsigned long subsys_bits;
1067 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001068 char *release_agent;
Daniel Lezcano97978e62010-10-27 15:33:35 -07001069 bool clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001070 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001071 /* User explicitly requested empty subsystem */
1072 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001073
1074 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001075
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076};
1077
Ben Blumaae8aab2010-03-10 15:22:07 -08001078/*
1079 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001080 * with cgroup_mutex held to protect the subsys[] array. This function takes
1081 * refcounts on subsystems to be used, unless it returns error, in which case
1082 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001083 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001084static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001086 char *token, *o = data;
1087 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001088 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001089 int i;
1090 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001091
Ben Blumaae8aab2010-03-10 15:22:07 -08001092 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1093
Li Zefanf9ab5b52009-06-17 16:26:33 -07001094#ifdef CONFIG_CPUSETS
1095 mask = ~(1UL << cpuset_subsys_id);
1096#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097
Paul Menagec6d57f32009-09-23 15:56:19 -07001098 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099
1100 while ((token = strsep(&o, ",")) != NULL) {
1101 if (!*token)
1102 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001103 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001104 /* Explicitly have no subsystems */
1105 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001106 continue;
1107 }
1108 if (!strcmp(token, "all")) {
1109 /* Mutually exclusive option 'all' + subsystem name */
1110 if (one_ss)
1111 return -EINVAL;
1112 all_ss = true;
1113 continue;
1114 }
1115 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001116 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001117 continue;
1118 }
1119 if (!strcmp(token, "clone_children")) {
Daniel Lezcano97978e62010-10-27 15:33:35 -07001120 opts->clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001121 continue;
1122 }
1123 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001124 /* Specifying two release agents is forbidden */
1125 if (opts->release_agent)
1126 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001127 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001128 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001129 if (!opts->release_agent)
1130 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001131 continue;
1132 }
1133 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001134 const char *name = token + 5;
1135 /* Can't specify an empty name */
1136 if (!strlen(name))
1137 return -EINVAL;
1138 /* Must match [\w.-]+ */
1139 for (i = 0; i < strlen(name); i++) {
1140 char c = name[i];
1141 if (isalnum(c))
1142 continue;
1143 if ((c == '.') || (c == '-') || (c == '_'))
1144 continue;
1145 return -EINVAL;
1146 }
1147 /* Specifying two names is forbidden */
1148 if (opts->name)
1149 return -EINVAL;
1150 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001151 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001152 GFP_KERNEL);
1153 if (!opts->name)
1154 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001155
1156 continue;
1157 }
1158
1159 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1160 struct cgroup_subsys *ss = subsys[i];
1161 if (ss == NULL)
1162 continue;
1163 if (strcmp(token, ss->name))
1164 continue;
1165 if (ss->disabled)
1166 continue;
1167
1168 /* Mutually exclusive option 'all' + subsystem name */
1169 if (all_ss)
1170 return -EINVAL;
1171 set_bit(i, &opts->subsys_bits);
1172 one_ss = true;
1173
1174 break;
1175 }
1176 if (i == CGROUP_SUBSYS_COUNT)
1177 return -ENOENT;
1178 }
1179
1180 /*
1181 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001182 * otherwise if 'none', 'name=' and a subsystem name options
1183 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001184 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001185 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001186 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1187 struct cgroup_subsys *ss = subsys[i];
1188 if (ss == NULL)
1189 continue;
1190 if (ss->disabled)
1191 continue;
1192 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001193 }
1194 }
1195
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001196 /* Consistency checks */
1197
Li Zefanf9ab5b52009-06-17 16:26:33 -07001198 /*
1199 * Option noprefix was introduced just for backward compatibility
1200 * with the old cpuset, so we allow noprefix only if mounting just
1201 * the cpuset subsystem.
1202 */
1203 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1204 (opts->subsys_bits & mask))
1205 return -EINVAL;
1206
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001207
1208 /* Can't specify "none" and some subsystems */
1209 if (opts->subsys_bits && opts->none)
1210 return -EINVAL;
1211
1212 /*
1213 * We either have to specify by name or by subsystems. (So all
1214 * empty hierarchies must have a name).
1215 */
Paul Menagec6d57f32009-09-23 15:56:19 -07001216 if (!opts->subsys_bits && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001217 return -EINVAL;
1218
Ben Blumcf5d5942010-03-10 15:22:09 -08001219 /*
1220 * Grab references on all the modules we'll need, so the subsystems
1221 * don't dance around before rebind_subsystems attaches them. This may
1222 * take duplicate reference counts on a subsystem that's already used,
1223 * but rebind_subsystems handles this case.
1224 */
1225 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1226 unsigned long bit = 1UL << i;
1227
1228 if (!(bit & opts->subsys_bits))
1229 continue;
1230 if (!try_module_get(subsys[i]->module)) {
1231 module_pin_failed = true;
1232 break;
1233 }
1234 }
1235 if (module_pin_failed) {
1236 /*
1237 * oops, one of the modules was going away. this means that we
1238 * raced with a module_delete call, and to the user this is
1239 * essentially a "subsystem doesn't exist" case.
1240 */
1241 for (i--; i >= CGROUP_BUILTIN_SUBSYS_COUNT; i--) {
1242 /* drop refcounts only on the ones we took */
1243 unsigned long bit = 1UL << i;
1244
1245 if (!(bit & opts->subsys_bits))
1246 continue;
1247 module_put(subsys[i]->module);
1248 }
1249 return -ENOENT;
1250 }
1251
Paul Menageddbcc7e2007-10-18 23:39:30 -07001252 return 0;
1253}
1254
Ben Blumcf5d5942010-03-10 15:22:09 -08001255static void drop_parsed_module_refcounts(unsigned long subsys_bits)
1256{
1257 int i;
1258 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1259 unsigned long bit = 1UL << i;
1260
1261 if (!(bit & subsys_bits))
1262 continue;
1263 module_put(subsys[i]->module);
1264 }
1265}
1266
Paul Menageddbcc7e2007-10-18 23:39:30 -07001267static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1268{
1269 int ret = 0;
1270 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001271 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001272 struct cgroup_sb_opts opts;
1273
Paul Menagebd89aab2007-10-18 23:40:44 -07001274 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001275 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001276 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001277
1278 /* See what subsystems are wanted */
1279 ret = parse_cgroupfs_options(data, &opts);
1280 if (ret)
1281 goto out_unlock;
1282
Ben Blumcf5d5942010-03-10 15:22:09 -08001283 /* Don't allow flags or name to change at remount */
1284 if (opts.flags != root->flags ||
1285 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001286 ret = -EINVAL;
Ben Blumcf5d5942010-03-10 15:22:09 -08001287 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menagec6d57f32009-09-23 15:56:19 -07001288 goto out_unlock;
1289 }
1290
Paul Menageddbcc7e2007-10-18 23:39:30 -07001291 ret = rebind_subsystems(root, opts.subsys_bits);
Ben Blumcf5d5942010-03-10 15:22:09 -08001292 if (ret) {
1293 drop_parsed_module_refcounts(opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -07001294 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001295 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001296
1297 /* (re)populate subsystem files */
Li Zefan0670e082009-04-02 16:57:30 -07001298 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001299
Paul Menage81a6a5c2007-10-18 23:39:38 -07001300 if (opts.release_agent)
1301 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001302 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001303 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001304 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001305 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001306 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001307 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001308 return ret;
1309}
1310
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001311static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001312 .statfs = simple_statfs,
1313 .drop_inode = generic_delete_inode,
1314 .show_options = cgroup_show_options,
1315 .remount_fs = cgroup_remount,
1316};
1317
Paul Menagecc31edc2008-10-18 20:28:04 -07001318static void init_cgroup_housekeeping(struct cgroup *cgrp)
1319{
1320 INIT_LIST_HEAD(&cgrp->sibling);
1321 INIT_LIST_HEAD(&cgrp->children);
1322 INIT_LIST_HEAD(&cgrp->css_sets);
1323 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001324 INIT_LIST_HEAD(&cgrp->pidlists);
1325 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001326 INIT_LIST_HEAD(&cgrp->event_list);
1327 spin_lock_init(&cgrp->event_list_lock);
Paul Menagecc31edc2008-10-18 20:28:04 -07001328}
Paul Menagec6d57f32009-09-23 15:56:19 -07001329
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330static void init_cgroup_root(struct cgroupfs_root *root)
1331{
Paul Menagebd89aab2007-10-18 23:40:44 -07001332 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333 INIT_LIST_HEAD(&root->subsys_list);
1334 INIT_LIST_HEAD(&root->root_list);
1335 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001336 cgrp->root = root;
1337 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001338 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001339}
1340
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001341static bool init_root_id(struct cgroupfs_root *root)
1342{
1343 int ret = 0;
1344
1345 do {
1346 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1347 return false;
1348 spin_lock(&hierarchy_id_lock);
1349 /* Try to allocate the next unused ID */
1350 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1351 &root->hierarchy_id);
1352 if (ret == -ENOSPC)
1353 /* Try again starting from 0 */
1354 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1355 if (!ret) {
1356 next_hierarchy_id = root->hierarchy_id + 1;
1357 } else if (ret != -EAGAIN) {
1358 /* Can only get here if the 31-bit IDR is full ... */
1359 BUG_ON(ret);
1360 }
1361 spin_unlock(&hierarchy_id_lock);
1362 } while (ret);
1363 return true;
1364}
1365
Paul Menageddbcc7e2007-10-18 23:39:30 -07001366static int cgroup_test_super(struct super_block *sb, void *data)
1367{
Paul Menagec6d57f32009-09-23 15:56:19 -07001368 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001369 struct cgroupfs_root *root = sb->s_fs_info;
1370
Paul Menagec6d57f32009-09-23 15:56:19 -07001371 /* If we asked for a name then it must match */
1372 if (opts->name && strcmp(opts->name, root->name))
1373 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001375 /*
1376 * If we asked for subsystems (or explicitly for no
1377 * subsystems) then they must match
1378 */
1379 if ((opts->subsys_bits || opts->none)
1380 && (opts->subsys_bits != root->subsys_bits))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001381 return 0;
1382
1383 return 1;
1384}
1385
Paul Menagec6d57f32009-09-23 15:56:19 -07001386static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1387{
1388 struct cgroupfs_root *root;
1389
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001390 if (!opts->subsys_bits && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001391 return NULL;
1392
1393 root = kzalloc(sizeof(*root), GFP_KERNEL);
1394 if (!root)
1395 return ERR_PTR(-ENOMEM);
1396
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001397 if (!init_root_id(root)) {
1398 kfree(root);
1399 return ERR_PTR(-ENOMEM);
1400 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001401 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001402
Paul Menagec6d57f32009-09-23 15:56:19 -07001403 root->subsys_bits = opts->subsys_bits;
1404 root->flags = opts->flags;
1405 if (opts->release_agent)
1406 strcpy(root->release_agent_path, opts->release_agent);
1407 if (opts->name)
1408 strcpy(root->name, opts->name);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001409 if (opts->clone_children)
1410 set_bit(CGRP_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001411 return root;
1412}
1413
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001414static void cgroup_drop_root(struct cgroupfs_root *root)
1415{
1416 if (!root)
1417 return;
1418
1419 BUG_ON(!root->hierarchy_id);
1420 spin_lock(&hierarchy_id_lock);
1421 ida_remove(&hierarchy_ida, root->hierarchy_id);
1422 spin_unlock(&hierarchy_id_lock);
1423 kfree(root);
1424}
1425
Paul Menageddbcc7e2007-10-18 23:39:30 -07001426static int cgroup_set_super(struct super_block *sb, void *data)
1427{
1428 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001429 struct cgroup_sb_opts *opts = data;
1430
1431 /* If we don't have a new root, we can't set up a new sb */
1432 if (!opts->new_root)
1433 return -EINVAL;
1434
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001435 BUG_ON(!opts->subsys_bits && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001436
1437 ret = set_anon_super(sb, NULL);
1438 if (ret)
1439 return ret;
1440
Paul Menagec6d57f32009-09-23 15:56:19 -07001441 sb->s_fs_info = opts->new_root;
1442 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001443
1444 sb->s_blocksize = PAGE_CACHE_SIZE;
1445 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1446 sb->s_magic = CGROUP_SUPER_MAGIC;
1447 sb->s_op = &cgroup_ops;
1448
1449 return 0;
1450}
1451
1452static int cgroup_get_rootdir(struct super_block *sb)
1453{
Al Viro0df6a632010-12-21 13:29:29 -05001454 static const struct dentry_operations cgroup_dops = {
1455 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001456 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001457 };
1458
Paul Menageddbcc7e2007-10-18 23:39:30 -07001459 struct inode *inode =
1460 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461
1462 if (!inode)
1463 return -ENOMEM;
1464
Paul Menageddbcc7e2007-10-18 23:39:30 -07001465 inode->i_fop = &simple_dir_operations;
1466 inode->i_op = &cgroup_dir_inode_operations;
1467 /* directories start off with i_nlink == 2 (for "." entry) */
1468 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001469 sb->s_root = d_make_root(inode);
1470 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001471 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001472 /* for everything else we want ->d_op set */
1473 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001474 return 0;
1475}
1476
Al Virof7e83572010-07-26 13:23:11 +04001477static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001479 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001480{
1481 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001483 int ret = 0;
1484 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001485 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001486 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001487
1488 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001489 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001490 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001491 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001492 if (ret)
1493 goto out_err;
1494
1495 /*
1496 * Allocate a new cgroup root. We may not need it if we're
1497 * reusing an existing hierarchy.
1498 */
1499 new_root = cgroup_root_from_opts(&opts);
1500 if (IS_ERR(new_root)) {
1501 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001502 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001503 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001504 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001505
Paul Menagec6d57f32009-09-23 15:56:19 -07001506 /* Locate an existing or new sb for this hierarchy */
1507 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001509 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001510 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001511 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512 }
1513
Paul Menagec6d57f32009-09-23 15:56:19 -07001514 root = sb->s_fs_info;
1515 BUG_ON(!root);
1516 if (root == opts.new_root) {
1517 /* We used the new root structure, so this is a new hierarchy */
1518 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001519 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001520 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001521 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001522 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001523
1524 BUG_ON(sb->s_root != NULL);
1525
1526 ret = cgroup_get_rootdir(sb);
1527 if (ret)
1528 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001529 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001530
Paul Menage817929e2007-10-18 23:39:36 -07001531 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001532 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001533 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001534
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001535 /* Check for name clashes with existing mounts */
1536 ret = -EBUSY;
1537 if (strlen(root->name))
1538 for_each_active_root(existing_root)
1539 if (!strcmp(existing_root->name, root->name))
1540 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001541
Paul Menage817929e2007-10-18 23:39:36 -07001542 /*
1543 * We're accessing css_set_count without locking
1544 * css_set_lock here, but that's OK - it can only be
1545 * increased by someone holding cgroup_lock, and
1546 * that's us. The worst that can happen is that we
1547 * have some link structures left over
1548 */
1549 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001550 if (ret)
1551 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001552
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553 ret = rebind_subsystems(root, root->subsys_bits);
1554 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001555 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001556 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001558 /*
1559 * There must be no failure case after here, since rebinding
1560 * takes care of subsystems' refcounts, which are explicitly
1561 * dropped in the failure exit path.
1562 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001563
1564 /* EBUSY should be the only error here */
1565 BUG_ON(ret);
1566
1567 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001568 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569
Li Zefanc12f65d2009-01-07 18:07:42 -08001570 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571 root->top_cgroup.dentry = sb->s_root;
1572
Paul Menage817929e2007-10-18 23:39:36 -07001573 /* Link the top cgroup in this hierarchy into all
1574 * the css_set objects */
1575 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001576 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1577 struct hlist_head *hhead = &css_set_table[i];
1578 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001579 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001580
Li Zefanc12f65d2009-01-07 18:07:42 -08001581 hlist_for_each_entry(cg, node, hhead, hlist)
1582 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001583 }
Paul Menage817929e2007-10-18 23:39:36 -07001584 write_unlock(&css_set_lock);
1585
1586 free_cg_links(&tmp_cg_links);
1587
Li Zefanc12f65d2009-01-07 18:07:42 -08001588 BUG_ON(!list_empty(&root_cgrp->sibling));
1589 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001590 BUG_ON(root->number_of_cgroups != 1);
1591
eparis@redhat2ce97382011-06-02 21:20:51 +10001592 cred = override_creds(&init_cred);
Li Zefanc12f65d2009-01-07 18:07:42 -08001593 cgroup_populate_dir(root_cgrp);
eparis@redhat2ce97382011-06-02 21:20:51 +10001594 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001595 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001596 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001597 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001598 } else {
1599 /*
1600 * We re-used an existing hierarchy - the new root (if
1601 * any) is not needed
1602 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001603 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001604 /* no subsys rebinding, so refcounts don't change */
1605 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001606 }
1607
Paul Menagec6d57f32009-09-23 15:56:19 -07001608 kfree(opts.release_agent);
1609 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001610 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001611
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001612 unlock_drop:
1613 mutex_unlock(&cgroup_root_mutex);
1614 mutex_unlock(&cgroup_mutex);
1615 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001616 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001617 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001618 drop_modules:
1619 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menagec6d57f32009-09-23 15:56:19 -07001620 out_err:
1621 kfree(opts.release_agent);
1622 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001623 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624}
1625
1626static void cgroup_kill_sb(struct super_block *sb) {
1627 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001628 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001629 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001630 struct cg_cgroup_link *link;
1631 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632
1633 BUG_ON(!root);
1634
1635 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001636 BUG_ON(!list_empty(&cgrp->children));
1637 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001638
1639 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001640 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001641
1642 /* Rebind all subsystems back to the default hierarchy */
1643 ret = rebind_subsystems(root, 0);
1644 /* Shouldn't be able to fail ... */
1645 BUG_ON(ret);
1646
Paul Menage817929e2007-10-18 23:39:36 -07001647 /*
1648 * Release all the links from css_sets to this hierarchy's
1649 * root cgroup
1650 */
1651 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001652
1653 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1654 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001655 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001656 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001657 kfree(link);
1658 }
1659 write_unlock(&css_set_lock);
1660
Paul Menage839ec542009-01-29 14:25:22 -08001661 if (!list_empty(&root->root_list)) {
1662 list_del(&root->root_list);
1663 root_count--;
1664 }
Li Zefane5f6a862009-01-07 18:07:41 -08001665
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001666 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001667 mutex_unlock(&cgroup_mutex);
1668
Paul Menageddbcc7e2007-10-18 23:39:30 -07001669 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001670 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001671}
1672
1673static struct file_system_type cgroup_fs_type = {
1674 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001675 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 .kill_sb = cgroup_kill_sb,
1677};
1678
Greg KH676db4a2010-08-05 13:53:35 -07001679static struct kobject *cgroup_kobj;
1680
Paul Menagebd89aab2007-10-18 23:40:44 -07001681static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682{
1683 return dentry->d_fsdata;
1684}
1685
1686static inline struct cftype *__d_cft(struct dentry *dentry)
1687{
1688 return dentry->d_fsdata;
1689}
1690
Li Zefana043e3b2008-02-23 15:24:09 -08001691/**
1692 * cgroup_path - generate the path of a cgroup
1693 * @cgrp: the cgroup in question
1694 * @buf: the buffer to write the path into
1695 * @buflen: the length of the buffer
1696 *
Paul Menagea47295e2009-01-07 18:07:44 -08001697 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1698 * reference. Writes path of cgroup into buf. Returns 0 on success,
1699 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001701int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001702{
1703 char *start;
Li Zefan9a9686b2010-04-22 17:29:24 +08001704 struct dentry *dentry = rcu_dereference_check(cgrp->dentry,
Li Zefan9a9686b2010-04-22 17:29:24 +08001705 cgroup_lock_is_held());
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706
Paul Menagea47295e2009-01-07 18:07:44 -08001707 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708 /*
1709 * Inactive subsystems have no dentry for their root
1710 * cgroup
1711 */
1712 strcpy(buf, "/");
1713 return 0;
1714 }
1715
1716 start = buf + buflen;
1717
1718 *--start = '\0';
1719 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001720 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001721
Paul Menageddbcc7e2007-10-18 23:39:30 -07001722 if ((start -= len) < buf)
1723 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001724 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001725 cgrp = cgrp->parent;
1726 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001728
1729 dentry = rcu_dereference_check(cgrp->dentry,
Li Zefan9a9686b2010-04-22 17:29:24 +08001730 cgroup_lock_is_held());
Paul Menagebd89aab2007-10-18 23:40:44 -07001731 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732 continue;
1733 if (--start < buf)
1734 return -ENAMETOOLONG;
1735 *start = '/';
1736 }
1737 memmove(buf, start, buf + buflen - start);
1738 return 0;
1739}
Ben Blum67523c42010-03-10 15:22:11 -08001740EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001741
Ben Blum74a11662011-05-26 16:25:20 -07001742/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001743 * Control Group taskset
1744 */
Tejun Heo134d3372011-12-12 18:12:21 -08001745struct task_and_cgroup {
1746 struct task_struct *task;
1747 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001748 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001749};
1750
Tejun Heo2f7ee562011-12-12 18:12:21 -08001751struct cgroup_taskset {
1752 struct task_and_cgroup single;
1753 struct flex_array *tc_array;
1754 int tc_array_len;
1755 int idx;
1756 struct cgroup *cur_cgrp;
1757};
1758
1759/**
1760 * cgroup_taskset_first - reset taskset and return the first task
1761 * @tset: taskset of interest
1762 *
1763 * @tset iteration is initialized and the first task is returned.
1764 */
1765struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1766{
1767 if (tset->tc_array) {
1768 tset->idx = 0;
1769 return cgroup_taskset_next(tset);
1770 } else {
1771 tset->cur_cgrp = tset->single.cgrp;
1772 return tset->single.task;
1773 }
1774}
1775EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1776
1777/**
1778 * cgroup_taskset_next - iterate to the next task in taskset
1779 * @tset: taskset of interest
1780 *
1781 * Return the next task in @tset. Iteration must have been initialized
1782 * with cgroup_taskset_first().
1783 */
1784struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1785{
1786 struct task_and_cgroup *tc;
1787
1788 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1789 return NULL;
1790
1791 tc = flex_array_get(tset->tc_array, tset->idx++);
1792 tset->cur_cgrp = tc->cgrp;
1793 return tc->task;
1794}
1795EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1796
1797/**
1798 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1799 * @tset: taskset of interest
1800 *
1801 * Return the cgroup for the current (last returned) task of @tset. This
1802 * function must be preceded by either cgroup_taskset_first() or
1803 * cgroup_taskset_next().
1804 */
1805struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1806{
1807 return tset->cur_cgrp;
1808}
1809EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1810
1811/**
1812 * cgroup_taskset_size - return the number of tasks in taskset
1813 * @tset: taskset of interest
1814 */
1815int cgroup_taskset_size(struct cgroup_taskset *tset)
1816{
1817 return tset->tc_array ? tset->tc_array_len : 1;
1818}
1819EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1820
1821
Ben Blum74a11662011-05-26 16:25:20 -07001822/*
1823 * cgroup_task_migrate - move a task from one cgroup to another.
1824 *
1825 * 'guarantee' is set if the caller promises that a new css_set for the task
1826 * will already exist. If not set, this function might sleep, and can fail with
Tejun Heocd3d0952011-12-12 18:12:21 -08001827 * -ENOMEM. Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001828 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001829static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1830 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001831{
1832 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001833
1834 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001835 * We are synchronized through threadgroup_lock() against PF_EXITING
1836 * setting such that we can't race against cgroup_exit() changing the
1837 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001838 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001839 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001840 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001841
Ben Blum74a11662011-05-26 16:25:20 -07001842 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001843 rcu_assign_pointer(tsk->cgroups, newcg);
1844 task_unlock(tsk);
1845
1846 /* Update the css_set linked lists if we're using them */
1847 write_lock(&css_set_lock);
1848 if (!list_empty(&tsk->cg_list))
1849 list_move(&tsk->cg_list, &newcg->tasks);
1850 write_unlock(&css_set_lock);
1851
1852 /*
1853 * We just gained a reference on oldcg by taking it from the task. As
1854 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1855 * it here; it will be freed under RCU.
1856 */
1857 put_css_set(oldcg);
1858
1859 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Ben Blum74a11662011-05-26 16:25:20 -07001860}
1861
Li Zefana043e3b2008-02-23 15:24:09 -08001862/**
1863 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1864 * @cgrp: the cgroup the task is attaching to
1865 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001866 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001867 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1868 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001869 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001870int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001871{
Tejun Heo8f121912012-03-29 22:03:33 -07001872 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001873 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001874 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001875 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001876 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001877 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001878
Tejun Heocd3d0952011-12-12 18:12:21 -08001879 /* @tsk either already exited or can't exit until the end */
1880 if (tsk->flags & PF_EXITING)
1881 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001882
1883 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001884 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001885 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001886 return 0;
1887
Tejun Heo2f7ee562011-12-12 18:12:21 -08001888 tset.single.task = tsk;
1889 tset.single.cgrp = oldcgrp;
1890
Paul Menagebbcb81d2007-10-18 23:39:32 -07001891 for_each_subsys(root, ss) {
1892 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08001893 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001894 if (retval) {
1895 /*
1896 * Remember on which subsystem the can_attach()
1897 * failed, so that we only call cancel_attach()
1898 * against the subsystems whose can_attach()
1899 * succeeded. (See below)
1900 */
1901 failed_ss = ss;
1902 goto out;
1903 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001904 }
1905 }
1906
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001907 newcg = find_css_set(tsk->cgroups, cgrp);
1908 if (!newcg) {
1909 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001910 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001911 }
1912
1913 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001914
Paul Menagebbcb81d2007-10-18 23:39:32 -07001915 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001916 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001917 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001918 }
Ben Blum74a11662011-05-26 16:25:20 -07001919
Colin Crossc0f6fa82010-11-23 21:37:03 -08001920 set_bit(CGRP_RELEASABLE, &cgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001921 synchronize_rcu();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001922
1923 /*
1924 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1925 * is no longer empty.
1926 */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001927 cgroup_wakeup_rmdir_waiter(cgrp);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001928out:
1929 if (retval) {
1930 for_each_subsys(root, ss) {
1931 if (ss == failed_ss)
1932 /*
1933 * This subsystem was the one that failed the
1934 * can_attach() check earlier, so we don't need
1935 * to call cancel_attach() against it or any
1936 * remaining subsystems.
1937 */
1938 break;
1939 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001940 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001941 }
1942 }
1943 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001944}
1945
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001946/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001947 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1948 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001949 * @tsk: the task to be attached
1950 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001951int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001952{
1953 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001954 int retval = 0;
1955
1956 cgroup_lock();
1957 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001958 struct cgroup *from_cg = task_cgroup_from_root(from, root);
1959
1960 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001961 if (retval)
1962 break;
1963 }
1964 cgroup_unlock();
1965
1966 return retval;
1967}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001968EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001969
Ben Blum74a11662011-05-26 16:25:20 -07001970/**
1971 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
1972 * @cgrp: the cgroup to attach to
1973 * @leader: the threadgroup leader task_struct of the group to be attached
1974 *
Tejun Heo257058a2011-12-12 18:12:21 -08001975 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
1976 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001977 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02001978static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07001979{
1980 int retval, i, group_size;
1981 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001982 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07001983 struct cgroupfs_root *root = cgrp->root;
1984 /* threadgroup list cursor and array */
1985 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001986 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001987 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001988 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001989
1990 /*
1991 * step 0: in order to do expensive, possibly blocking operations for
1992 * every thread, we cannot iterate the thread group list, since it needs
1993 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001994 * group - group_rwsem prevents new threads from appearing, and if
1995 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001996 */
1997 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07001998 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001999 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002000 if (!group)
2001 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002002 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2003 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2004 if (retval)
2005 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002006
Ben Blum74a11662011-05-26 16:25:20 -07002007 tsk = leader;
2008 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002009 /*
2010 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2011 * already PF_EXITING could be freed from underneath us unless we
2012 * take an rcu_read_lock.
2013 */
2014 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002015 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002016 struct task_and_cgroup ent;
2017
Tejun Heocd3d0952011-12-12 18:12:21 -08002018 /* @tsk either already exited or can't exit until the end */
2019 if (tsk->flags & PF_EXITING)
2020 continue;
2021
Ben Blum74a11662011-05-26 16:25:20 -07002022 /* as per above, nr_threads may decrease, but not increase. */
2023 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002024 ent.task = tsk;
2025 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002026 /* nothing to do if this task is already in the cgroup */
2027 if (ent.cgrp == cgrp)
2028 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002029 /*
2030 * saying GFP_ATOMIC has no effect here because we did prealloc
2031 * earlier, but it's good form to communicate our expectations.
2032 */
Tejun Heo134d3372011-12-12 18:12:21 -08002033 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002034 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002035 i++;
2036 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002037 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002038 /* remember the number of threads in the array for later. */
2039 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002040 tset.tc_array = group;
2041 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002042
Tejun Heo134d3372011-12-12 18:12:21 -08002043 /* methods shouldn't be called if no task is actually migrating */
2044 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002045 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002046 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002047
Ben Blum74a11662011-05-26 16:25:20 -07002048 /*
2049 * step 1: check that we can legitimately attach to the cgroup.
2050 */
2051 for_each_subsys(root, ss) {
2052 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002053 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002054 if (retval) {
2055 failed_ss = ss;
2056 goto out_cancel_attach;
2057 }
2058 }
Ben Blum74a11662011-05-26 16:25:20 -07002059 }
2060
2061 /*
2062 * step 2: make sure css_sets exist for all threads to be migrated.
2063 * we use find_css_set, which allocates a new one if necessary.
2064 */
Ben Blum74a11662011-05-26 16:25:20 -07002065 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002066 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002067 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2068 if (!tc->cg) {
2069 retval = -ENOMEM;
2070 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002071 }
2072 }
2073
2074 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002075 * step 3: now that we're guaranteed success wrt the css_sets,
2076 * proceed to move all tasks to the new cgroup. There are no
2077 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002078 */
Ben Blum74a11662011-05-26 16:25:20 -07002079 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002080 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002081 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002082 }
2083 /* nothing is sensitive to fork() after this point. */
2084
2085 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002086 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002087 */
2088 for_each_subsys(root, ss) {
2089 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002090 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002091 }
2092
2093 /*
2094 * step 5: success! and cleanup
2095 */
2096 synchronize_rcu();
2097 cgroup_wakeup_rmdir_waiter(cgrp);
2098 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002099out_put_css_set_refs:
2100 if (retval) {
2101 for (i = 0; i < group_size; i++) {
2102 tc = flex_array_get(group, i);
2103 if (!tc->cg)
2104 break;
2105 put_css_set(tc->cg);
2106 }
Ben Blum74a11662011-05-26 16:25:20 -07002107 }
2108out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002109 if (retval) {
2110 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002111 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002112 break;
Ben Blum74a11662011-05-26 16:25:20 -07002113 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002114 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002115 }
2116 }
Ben Blum74a11662011-05-26 16:25:20 -07002117out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002118 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002119 return retval;
2120}
2121
2122/*
2123 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002124 * function to attach either it or all tasks in its threadgroup. Will lock
2125 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002126 */
2127static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002128{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002129 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002130 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002131 int ret;
2132
Ben Blum74a11662011-05-26 16:25:20 -07002133 if (!cgroup_lock_live_group(cgrp))
2134 return -ENODEV;
2135
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002136retry_find_task:
2137 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002138 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002139 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002140 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002141 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002142 ret= -ESRCH;
2143 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002144 }
Ben Blum74a11662011-05-26 16:25:20 -07002145 /*
2146 * even if we're attaching all tasks in the thread group, we
2147 * only need to check permissions on one of them.
2148 */
David Howellsc69e8d92008-11-14 10:39:19 +11002149 tcred = __task_cred(tsk);
2150 if (cred->euid &&
2151 cred->euid != tcred->uid &&
2152 cred->euid != tcred->suid) {
2153 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002154 ret = -EACCES;
2155 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002156 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002157 } else
2158 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002159
2160 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002161 tsk = tsk->group_leader;
2162 get_task_struct(tsk);
2163 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002164
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002165 threadgroup_lock(tsk);
2166 if (threadgroup) {
2167 if (!thread_group_leader(tsk)) {
2168 /*
2169 * a race with de_thread from another thread's exec()
2170 * may strip us of our leadership, if this happens,
2171 * there is no choice but to throw this task away and
2172 * try again; this is
2173 * "double-double-toil-and-trouble-check locking".
2174 */
2175 threadgroup_unlock(tsk);
2176 put_task_struct(tsk);
2177 goto retry_find_task;
2178 }
2179 ret = cgroup_attach_proc(cgrp, tsk);
2180 } else
2181 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002182 threadgroup_unlock(tsk);
2183
Paul Menagebbcb81d2007-10-18 23:39:32 -07002184 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002185out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002186 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002187 return ret;
2188}
2189
Paul Menageaf351022008-07-25 01:47:01 -07002190static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2191{
Ben Blum74a11662011-05-26 16:25:20 -07002192 return attach_task_by_pid(cgrp, pid, false);
2193}
2194
2195static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2196{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002197 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002198}
2199
Paul Menagee788e062008-07-25 01:46:59 -07002200/**
2201 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2202 * @cgrp: the cgroup to be checked for liveness
2203 *
Paul Menage84eea842008-07-25 01:47:00 -07002204 * On success, returns true; the lock should be later released with
2205 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002206 */
Paul Menage84eea842008-07-25 01:47:00 -07002207bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002208{
2209 mutex_lock(&cgroup_mutex);
2210 if (cgroup_is_removed(cgrp)) {
2211 mutex_unlock(&cgroup_mutex);
2212 return false;
2213 }
2214 return true;
2215}
Ben Blum67523c42010-03-10 15:22:11 -08002216EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002217
2218static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2219 const char *buffer)
2220{
2221 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002222 if (strlen(buffer) >= PATH_MAX)
2223 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002224 if (!cgroup_lock_live_group(cgrp))
2225 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002226 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002227 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002228 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002229 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002230 return 0;
2231}
2232
2233static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2234 struct seq_file *seq)
2235{
2236 if (!cgroup_lock_live_group(cgrp))
2237 return -ENODEV;
2238 seq_puts(seq, cgrp->root->release_agent_path);
2239 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002240 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002241 return 0;
2242}
2243
Paul Menage84eea842008-07-25 01:47:00 -07002244/* A buffer size big enough for numbers or short strings */
2245#define CGROUP_LOCAL_BUFFER_SIZE 64
2246
Paul Menagee73d2c62008-04-29 01:00:06 -07002247static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002248 struct file *file,
2249 const char __user *userbuf,
2250 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002251{
Paul Menage84eea842008-07-25 01:47:00 -07002252 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002253 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002254 char *end;
2255
2256 if (!nbytes)
2257 return -EINVAL;
2258 if (nbytes >= sizeof(buffer))
2259 return -E2BIG;
2260 if (copy_from_user(buffer, userbuf, nbytes))
2261 return -EFAULT;
2262
2263 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002264 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002265 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002266 if (*end)
2267 return -EINVAL;
2268 retval = cft->write_u64(cgrp, cft, val);
2269 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002270 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002271 if (*end)
2272 return -EINVAL;
2273 retval = cft->write_s64(cgrp, cft, val);
2274 }
Paul Menage355e0c42007-10-18 23:39:33 -07002275 if (!retval)
2276 retval = nbytes;
2277 return retval;
2278}
2279
Paul Menagedb3b1492008-07-25 01:46:58 -07002280static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2281 struct file *file,
2282 const char __user *userbuf,
2283 size_t nbytes, loff_t *unused_ppos)
2284{
Paul Menage84eea842008-07-25 01:47:00 -07002285 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002286 int retval = 0;
2287 size_t max_bytes = cft->max_write_len;
2288 char *buffer = local_buffer;
2289
2290 if (!max_bytes)
2291 max_bytes = sizeof(local_buffer) - 1;
2292 if (nbytes >= max_bytes)
2293 return -E2BIG;
2294 /* Allocate a dynamic buffer if we need one */
2295 if (nbytes >= sizeof(local_buffer)) {
2296 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2297 if (buffer == NULL)
2298 return -ENOMEM;
2299 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002300 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2301 retval = -EFAULT;
2302 goto out;
2303 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002304
2305 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002306 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002307 if (!retval)
2308 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002309out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002310 if (buffer != local_buffer)
2311 kfree(buffer);
2312 return retval;
2313}
2314
Paul Menageddbcc7e2007-10-18 23:39:30 -07002315static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2316 size_t nbytes, loff_t *ppos)
2317{
2318 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002319 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002320
Li Zefan75139b82009-01-07 18:07:33 -08002321 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002322 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002323 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002324 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002325 if (cft->write_u64 || cft->write_s64)
2326 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002327 if (cft->write_string)
2328 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002329 if (cft->trigger) {
2330 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2331 return ret ? ret : nbytes;
2332 }
Paul Menage355e0c42007-10-18 23:39:33 -07002333 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002334}
2335
Paul Menagef4c753b2008-04-29 00:59:56 -07002336static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2337 struct file *file,
2338 char __user *buf, size_t nbytes,
2339 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002340{
Paul Menage84eea842008-07-25 01:47:00 -07002341 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002342 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002343 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2344
2345 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2346}
2347
Paul Menagee73d2c62008-04-29 01:00:06 -07002348static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2349 struct file *file,
2350 char __user *buf, size_t nbytes,
2351 loff_t *ppos)
2352{
Paul Menage84eea842008-07-25 01:47:00 -07002353 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002354 s64 val = cft->read_s64(cgrp, cft);
2355 int len = sprintf(tmp, "%lld\n", (long long) val);
2356
2357 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2358}
2359
Paul Menageddbcc7e2007-10-18 23:39:30 -07002360static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2361 size_t nbytes, loff_t *ppos)
2362{
2363 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002364 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002365
Li Zefan75139b82009-01-07 18:07:33 -08002366 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002367 return -ENODEV;
2368
2369 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002370 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002371 if (cft->read_u64)
2372 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002373 if (cft->read_s64)
2374 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002375 return -EINVAL;
2376}
2377
Paul Menage91796562008-04-29 01:00:01 -07002378/*
2379 * seqfile ops/methods for returning structured data. Currently just
2380 * supports string->u64 maps, but can be extended in future.
2381 */
2382
2383struct cgroup_seqfile_state {
2384 struct cftype *cft;
2385 struct cgroup *cgroup;
2386};
2387
2388static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2389{
2390 struct seq_file *sf = cb->state;
2391 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2392}
2393
2394static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2395{
2396 struct cgroup_seqfile_state *state = m->private;
2397 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002398 if (cft->read_map) {
2399 struct cgroup_map_cb cb = {
2400 .fill = cgroup_map_add,
2401 .state = m,
2402 };
2403 return cft->read_map(state->cgroup, cft, &cb);
2404 }
2405 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002406}
2407
Adrian Bunk96930a62008-07-25 19:46:21 -07002408static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002409{
2410 struct seq_file *seq = file->private_data;
2411 kfree(seq->private);
2412 return single_release(inode, file);
2413}
2414
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002415static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002416 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002417 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002418 .llseek = seq_lseek,
2419 .release = cgroup_seqfile_release,
2420};
2421
Paul Menageddbcc7e2007-10-18 23:39:30 -07002422static int cgroup_file_open(struct inode *inode, struct file *file)
2423{
2424 int err;
2425 struct cftype *cft;
2426
2427 err = generic_file_open(inode, file);
2428 if (err)
2429 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002430 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002431
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002432 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002433 struct cgroup_seqfile_state *state =
2434 kzalloc(sizeof(*state), GFP_USER);
2435 if (!state)
2436 return -ENOMEM;
2437 state->cft = cft;
2438 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2439 file->f_op = &cgroup_seqfile_operations;
2440 err = single_open(file, cgroup_seqfile_show, state);
2441 if (err < 0)
2442 kfree(state);
2443 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002444 err = cft->open(inode, file);
2445 else
2446 err = 0;
2447
2448 return err;
2449}
2450
2451static int cgroup_file_release(struct inode *inode, struct file *file)
2452{
2453 struct cftype *cft = __d_cft(file->f_dentry);
2454 if (cft->release)
2455 return cft->release(inode, file);
2456 return 0;
2457}
2458
2459/*
2460 * cgroup_rename - Only allow simple rename of directories in place.
2461 */
2462static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2463 struct inode *new_dir, struct dentry *new_dentry)
2464{
2465 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2466 return -ENOTDIR;
2467 if (new_dentry->d_inode)
2468 return -EEXIST;
2469 if (old_dir != new_dir)
2470 return -EIO;
2471 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2472}
2473
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002474static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002475 .read = cgroup_file_read,
2476 .write = cgroup_file_write,
2477 .llseek = generic_file_llseek,
2478 .open = cgroup_file_open,
2479 .release = cgroup_file_release,
2480};
2481
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002482static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002483 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002484 .mkdir = cgroup_mkdir,
2485 .rmdir = cgroup_rmdir,
2486 .rename = cgroup_rename,
2487};
2488
Al Viroc72a04e2011-01-14 05:31:45 +00002489static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2490{
2491 if (dentry->d_name.len > NAME_MAX)
2492 return ERR_PTR(-ENAMETOOLONG);
2493 d_add(dentry, NULL);
2494 return NULL;
2495}
2496
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002497/*
2498 * Check if a file is a control file
2499 */
2500static inline struct cftype *__file_cft(struct file *file)
2501{
2502 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2503 return ERR_PTR(-EINVAL);
2504 return __d_cft(file->f_dentry);
2505}
2506
Al Viroa5e7ed32011-07-26 01:55:55 -04002507static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002508 struct super_block *sb)
2509{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002510 struct inode *inode;
2511
2512 if (!dentry)
2513 return -ENOENT;
2514 if (dentry->d_inode)
2515 return -EEXIST;
2516
2517 inode = cgroup_new_inode(mode, sb);
2518 if (!inode)
2519 return -ENOMEM;
2520
2521 if (S_ISDIR(mode)) {
2522 inode->i_op = &cgroup_dir_inode_operations;
2523 inode->i_fop = &simple_dir_operations;
2524
2525 /* start off with i_nlink == 2 (for "." entry) */
2526 inc_nlink(inode);
2527
2528 /* start with the directory inode held, so that we can
2529 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07002530 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002531 } else if (S_ISREG(mode)) {
2532 inode->i_size = 0;
2533 inode->i_fop = &cgroup_file_operations;
2534 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002535 d_instantiate(dentry, inode);
2536 dget(dentry); /* Extra count - pin the dentry in core */
2537 return 0;
2538}
2539
2540/*
Li Zefana043e3b2008-02-23 15:24:09 -08002541 * cgroup_create_dir - create a directory for an object.
2542 * @cgrp: the cgroup we create the directory for. It must have a valid
2543 * ->parent field. And we are going to fill its ->dentry field.
2544 * @dentry: dentry of the new cgroup
2545 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002546 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002547static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04002548 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002549{
2550 struct dentry *parent;
2551 int error = 0;
2552
Paul Menagebd89aab2007-10-18 23:40:44 -07002553 parent = cgrp->parent->dentry;
2554 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002555 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002556 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002557 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08002558 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002559 dget(dentry);
2560 }
2561 dput(dentry);
2562
2563 return error;
2564}
2565
Li Zefan099fca32009-04-02 16:57:29 -07002566/**
2567 * cgroup_file_mode - deduce file mode of a control file
2568 * @cft: the control file in question
2569 *
2570 * returns cft->mode if ->mode is not 0
2571 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2572 * returns S_IRUGO if it has only a read handler
2573 * returns S_IWUSR if it has only a write hander
2574 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002575static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002576{
Al Viroa5e7ed32011-07-26 01:55:55 -04002577 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002578
2579 if (cft->mode)
2580 return cft->mode;
2581
2582 if (cft->read || cft->read_u64 || cft->read_s64 ||
2583 cft->read_map || cft->read_seq_string)
2584 mode |= S_IRUGO;
2585
2586 if (cft->write || cft->write_u64 || cft->write_s64 ||
2587 cft->write_string || cft->trigger)
2588 mode |= S_IWUSR;
2589
2590 return mode;
2591}
2592
Paul Menagebd89aab2007-10-18 23:40:44 -07002593int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002594 struct cgroup_subsys *subsys,
2595 const struct cftype *cft)
2596{
Paul Menagebd89aab2007-10-18 23:40:44 -07002597 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002598 struct dentry *dentry;
2599 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002600 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002601
2602 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07002603 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002604 strcpy(name, subsys->name);
2605 strcat(name, ".");
2606 }
2607 strcat(name, cft->name);
2608 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2609 dentry = lookup_one_len(name, dir, strlen(name));
2610 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07002611 mode = cgroup_file_mode(cft);
2612 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07002613 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002614 if (!error)
2615 dentry->d_fsdata = (void *)cft;
2616 dput(dentry);
2617 } else
2618 error = PTR_ERR(dentry);
2619 return error;
2620}
Ben Blume6a11052010-03-10 15:22:09 -08002621EXPORT_SYMBOL_GPL(cgroup_add_file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002622
Paul Menagebd89aab2007-10-18 23:40:44 -07002623int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002624 struct cgroup_subsys *subsys,
2625 const struct cftype cft[],
2626 int count)
2627{
2628 int i, err;
2629 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002630 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002631 if (err)
2632 return err;
2633 }
2634 return 0;
2635}
Ben Blume6a11052010-03-10 15:22:09 -08002636EXPORT_SYMBOL_GPL(cgroup_add_files);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002637
Li Zefana043e3b2008-02-23 15:24:09 -08002638/**
2639 * cgroup_task_count - count the number of tasks in a cgroup.
2640 * @cgrp: the cgroup in question
2641 *
2642 * Return the number of tasks in the cgroup.
2643 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002644int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002645{
2646 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002647 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002648
Paul Menage817929e2007-10-18 23:39:36 -07002649 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002650 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002651 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002652 }
2653 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002654 return count;
2655}
2656
2657/*
Paul Menage817929e2007-10-18 23:39:36 -07002658 * Advance a list_head iterator. The iterator should be positioned at
2659 * the start of a css_set
2660 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002661static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002662 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002663{
2664 struct list_head *l = it->cg_link;
2665 struct cg_cgroup_link *link;
2666 struct css_set *cg;
2667
2668 /* Advance to the next non-empty css_set */
2669 do {
2670 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002671 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002672 it->cg_link = NULL;
2673 return;
2674 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002675 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002676 cg = link->cg;
2677 } while (list_empty(&cg->tasks));
2678 it->cg_link = l;
2679 it->task = cg->tasks.next;
2680}
2681
Cliff Wickman31a7df02008-02-07 00:14:42 -08002682/*
2683 * To reduce the fork() overhead for systems that are not actually
2684 * using their cgroups capability, we don't maintain the lists running
2685 * through each css_set to its tasks until we see the list actually
2686 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002687 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002688static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002689{
2690 struct task_struct *p, *g;
2691 write_lock(&css_set_lock);
2692 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002693 /*
2694 * We need tasklist_lock because RCU is not safe against
2695 * while_each_thread(). Besides, a forking task that has passed
2696 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2697 * is not guaranteed to have its child immediately visible in the
2698 * tasklist if we walk through it with RCU.
2699 */
2700 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002701 do_each_thread(g, p) {
2702 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002703 /*
2704 * We should check if the process is exiting, otherwise
2705 * it will race with cgroup_exit() in that the list
2706 * entry won't be deleted though the process has exited.
2707 */
2708 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002709 list_add(&p->cg_list, &p->cgroups->tasks);
2710 task_unlock(p);
2711 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002712 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002713 write_unlock(&css_set_lock);
2714}
2715
Paul Menagebd89aab2007-10-18 23:40:44 -07002716void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002717 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002718{
2719 /*
2720 * The first time anyone tries to iterate across a cgroup,
2721 * we need to enable the list linking each css_set to its
2722 * tasks, and fix up all existing tasks.
2723 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002724 if (!use_task_css_set_links)
2725 cgroup_enable_task_cg_lists();
2726
Paul Menage817929e2007-10-18 23:39:36 -07002727 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002728 it->cg_link = &cgrp->css_sets;
2729 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002730}
2731
Paul Menagebd89aab2007-10-18 23:40:44 -07002732struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07002733 struct cgroup_iter *it)
2734{
2735 struct task_struct *res;
2736 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002737 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002738
2739 /* If the iterator cg is NULL, we have no tasks */
2740 if (!it->cg_link)
2741 return NULL;
2742 res = list_entry(l, struct task_struct, cg_list);
2743 /* Advance iterator to find next entry */
2744 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002745 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2746 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07002747 /* We reached the end of this task list - move on to
2748 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07002749 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002750 } else {
2751 it->task = l;
2752 }
2753 return res;
2754}
2755
Paul Menagebd89aab2007-10-18 23:40:44 -07002756void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002757 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002758{
2759 read_unlock(&css_set_lock);
2760}
2761
Cliff Wickman31a7df02008-02-07 00:14:42 -08002762static inline int started_after_time(struct task_struct *t1,
2763 struct timespec *time,
2764 struct task_struct *t2)
2765{
2766 int start_diff = timespec_compare(&t1->start_time, time);
2767 if (start_diff > 0) {
2768 return 1;
2769 } else if (start_diff < 0) {
2770 return 0;
2771 } else {
2772 /*
2773 * Arbitrarily, if two processes started at the same
2774 * time, we'll say that the lower pointer value
2775 * started first. Note that t2 may have exited by now
2776 * so this may not be a valid pointer any longer, but
2777 * that's fine - it still serves to distinguish
2778 * between two tasks started (effectively) simultaneously.
2779 */
2780 return t1 > t2;
2781 }
2782}
2783
2784/*
2785 * This function is a callback from heap_insert() and is used to order
2786 * the heap.
2787 * In this case we order the heap in descending task start time.
2788 */
2789static inline int started_after(void *p1, void *p2)
2790{
2791 struct task_struct *t1 = p1;
2792 struct task_struct *t2 = p2;
2793 return started_after_time(t1, &t2->start_time, t2);
2794}
2795
2796/**
2797 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2798 * @scan: struct cgroup_scanner containing arguments for the scan
2799 *
2800 * Arguments include pointers to callback functions test_task() and
2801 * process_task().
2802 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2803 * and if it returns true, call process_task() for it also.
2804 * The test_task pointer may be NULL, meaning always true (select all tasks).
2805 * Effectively duplicates cgroup_iter_{start,next,end}()
2806 * but does not lock css_set_lock for the call to process_task().
2807 * The struct cgroup_scanner may be embedded in any structure of the caller's
2808 * creation.
2809 * It is guaranteed that process_task() will act on every task that
2810 * is a member of the cgroup for the duration of this call. This
2811 * function may or may not call process_task() for tasks that exit
2812 * or move to a different cgroup during the call, or are forked or
2813 * move into the cgroup during the call.
2814 *
2815 * Note that test_task() may be called with locks held, and may in some
2816 * situations be called multiple times for the same task, so it should
2817 * be cheap.
2818 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2819 * pre-allocated and will be used for heap operations (and its "gt" member will
2820 * be overwritten), else a temporary heap will be used (allocation of which
2821 * may cause this function to fail).
2822 */
2823int cgroup_scan_tasks(struct cgroup_scanner *scan)
2824{
2825 int retval, i;
2826 struct cgroup_iter it;
2827 struct task_struct *p, *dropped;
2828 /* Never dereference latest_task, since it's not refcounted */
2829 struct task_struct *latest_task = NULL;
2830 struct ptr_heap tmp_heap;
2831 struct ptr_heap *heap;
2832 struct timespec latest_time = { 0, 0 };
2833
2834 if (scan->heap) {
2835 /* The caller supplied our heap and pre-allocated its memory */
2836 heap = scan->heap;
2837 heap->gt = &started_after;
2838 } else {
2839 /* We need to allocate our own heap memory */
2840 heap = &tmp_heap;
2841 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2842 if (retval)
2843 /* cannot allocate the heap */
2844 return retval;
2845 }
2846
2847 again:
2848 /*
2849 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2850 * to determine which are of interest, and using the scanner's
2851 * "process_task" callback to process any of them that need an update.
2852 * Since we don't want to hold any locks during the task updates,
2853 * gather tasks to be processed in a heap structure.
2854 * The heap is sorted by descending task start time.
2855 * If the statically-sized heap fills up, we overflow tasks that
2856 * started later, and in future iterations only consider tasks that
2857 * started after the latest task in the previous pass. This
2858 * guarantees forward progress and that we don't miss any tasks.
2859 */
2860 heap->size = 0;
2861 cgroup_iter_start(scan->cg, &it);
2862 while ((p = cgroup_iter_next(scan->cg, &it))) {
2863 /*
2864 * Only affect tasks that qualify per the caller's callback,
2865 * if he provided one
2866 */
2867 if (scan->test_task && !scan->test_task(p, scan))
2868 continue;
2869 /*
2870 * Only process tasks that started after the last task
2871 * we processed
2872 */
2873 if (!started_after_time(p, &latest_time, latest_task))
2874 continue;
2875 dropped = heap_insert(heap, p);
2876 if (dropped == NULL) {
2877 /*
2878 * The new task was inserted; the heap wasn't
2879 * previously full
2880 */
2881 get_task_struct(p);
2882 } else if (dropped != p) {
2883 /*
2884 * The new task was inserted, and pushed out a
2885 * different task
2886 */
2887 get_task_struct(p);
2888 put_task_struct(dropped);
2889 }
2890 /*
2891 * Else the new task was newer than anything already in
2892 * the heap and wasn't inserted
2893 */
2894 }
2895 cgroup_iter_end(scan->cg, &it);
2896
2897 if (heap->size) {
2898 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002899 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002900 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002901 latest_time = q->start_time;
2902 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002903 }
2904 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002905 scan->process_task(q, scan);
2906 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002907 }
2908 /*
2909 * If we had to process any tasks at all, scan again
2910 * in case some of them were in the middle of forking
2911 * children that didn't get processed.
2912 * Not the most efficient way to do it, but it avoids
2913 * having to take callback_mutex in the fork path
2914 */
2915 goto again;
2916 }
2917 if (heap == &tmp_heap)
2918 heap_free(&tmp_heap);
2919 return 0;
2920}
2921
Paul Menage817929e2007-10-18 23:39:36 -07002922/*
Ben Blum102a7752009-09-23 15:56:26 -07002923 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002924 *
2925 * Reading this file can return large amounts of data if a cgroup has
2926 * *lots* of attached tasks. So it may need several calls to read(),
2927 * but we cannot guarantee that the information we produce is correct
2928 * unless we produce it entirely atomically.
2929 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002930 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002931
Li Zefan24528252012-01-20 11:58:43 +08002932/* which pidlist file are we talking about? */
2933enum cgroup_filetype {
2934 CGROUP_FILE_PROCS,
2935 CGROUP_FILE_TASKS,
2936};
2937
2938/*
2939 * A pidlist is a list of pids that virtually represents the contents of one
2940 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
2941 * a pair (one each for procs, tasks) for each pid namespace that's relevant
2942 * to the cgroup.
2943 */
2944struct cgroup_pidlist {
2945 /*
2946 * used to find which pidlist is wanted. doesn't change as long as
2947 * this particular list stays in the list.
2948 */
2949 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
2950 /* array of xids */
2951 pid_t *list;
2952 /* how many elements the above list has */
2953 int length;
2954 /* how many files are using the current array */
2955 int use_count;
2956 /* each of these stored in a list by its cgroup */
2957 struct list_head links;
2958 /* pointer to the cgroup we belong to, for list removal purposes */
2959 struct cgroup *owner;
2960 /* protects the other fields */
2961 struct rw_semaphore mutex;
2962};
2963
Paul Menagebbcb81d2007-10-18 23:39:32 -07002964/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002965 * The following two functions "fix" the issue where there are more pids
2966 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2967 * TODO: replace with a kernel-wide solution to this problem
2968 */
2969#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2970static void *pidlist_allocate(int count)
2971{
2972 if (PIDLIST_TOO_LARGE(count))
2973 return vmalloc(count * sizeof(pid_t));
2974 else
2975 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2976}
2977static void pidlist_free(void *p)
2978{
2979 if (is_vmalloc_addr(p))
2980 vfree(p);
2981 else
2982 kfree(p);
2983}
2984static void *pidlist_resize(void *p, int newcount)
2985{
2986 void *newlist;
2987 /* note: if new alloc fails, old p will still be valid either way */
2988 if (is_vmalloc_addr(p)) {
2989 newlist = vmalloc(newcount * sizeof(pid_t));
2990 if (!newlist)
2991 return NULL;
2992 memcpy(newlist, p, newcount * sizeof(pid_t));
2993 vfree(p);
2994 } else {
2995 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
2996 }
2997 return newlist;
2998}
2999
3000/*
Ben Blum102a7752009-09-23 15:56:26 -07003001 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3002 * If the new stripped list is sufficiently smaller and there's enough memory
3003 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3004 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003005 */
Ben Blum102a7752009-09-23 15:56:26 -07003006/* is the size difference enough that we should re-allocate the array? */
3007#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3008static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003009{
Ben Blum102a7752009-09-23 15:56:26 -07003010 int src, dest = 1;
3011 pid_t *list = *p;
3012 pid_t *newlist;
3013
3014 /*
3015 * we presume the 0th element is unique, so i starts at 1. trivial
3016 * edge cases first; no work needs to be done for either
3017 */
3018 if (length == 0 || length == 1)
3019 return length;
3020 /* src and dest walk down the list; dest counts unique elements */
3021 for (src = 1; src < length; src++) {
3022 /* find next unique element */
3023 while (list[src] == list[src-1]) {
3024 src++;
3025 if (src == length)
3026 goto after;
3027 }
3028 /* dest always points to where the next unique element goes */
3029 list[dest] = list[src];
3030 dest++;
3031 }
3032after:
3033 /*
3034 * if the length difference is large enough, we want to allocate a
3035 * smaller buffer to save memory. if this fails due to out of memory,
3036 * we'll just stay with what we've got.
3037 */
3038 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003039 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003040 if (newlist)
3041 *p = newlist;
3042 }
3043 return dest;
3044}
3045
3046static int cmppid(const void *a, const void *b)
3047{
3048 return *(pid_t *)a - *(pid_t *)b;
3049}
3050
3051/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003052 * find the appropriate pidlist for our purpose (given procs vs tasks)
3053 * returns with the lock on that pidlist already held, and takes care
3054 * of the use count, or returns NULL with no locks held if we're out of
3055 * memory.
3056 */
3057static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3058 enum cgroup_filetype type)
3059{
3060 struct cgroup_pidlist *l;
3061 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08003062 struct pid_namespace *ns = current->nsproxy->pid_ns;
3063
Ben Blum72a8cb32009-09-23 15:56:27 -07003064 /*
3065 * We can't drop the pidlist_mutex before taking the l->mutex in case
3066 * the last ref-holder is trying to remove l from the list at the same
3067 * time. Holding the pidlist_mutex precludes somebody taking whichever
3068 * list we find out from under us - compare release_pid_array().
3069 */
3070 mutex_lock(&cgrp->pidlist_mutex);
3071 list_for_each_entry(l, &cgrp->pidlists, links) {
3072 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003073 /* make sure l doesn't vanish out from under us */
3074 down_write(&l->mutex);
3075 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003076 return l;
3077 }
3078 }
3079 /* entry not found; create a new one */
3080 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3081 if (!l) {
3082 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003083 return l;
3084 }
3085 init_rwsem(&l->mutex);
3086 down_write(&l->mutex);
3087 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003088 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003089 l->use_count = 0; /* don't increment here */
3090 l->list = NULL;
3091 l->owner = cgrp;
3092 list_add(&l->links, &cgrp->pidlists);
3093 mutex_unlock(&cgrp->pidlist_mutex);
3094 return l;
3095}
3096
3097/*
Ben Blum102a7752009-09-23 15:56:26 -07003098 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3099 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003100static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3101 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003102{
3103 pid_t *array;
3104 int length;
3105 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003106 struct cgroup_iter it;
3107 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003108 struct cgroup_pidlist *l;
3109
3110 /*
3111 * If cgroup gets more users after we read count, we won't have
3112 * enough space - tough. This race is indistinguishable to the
3113 * caller from the case that the additional cgroup users didn't
3114 * show up until sometime later on.
3115 */
3116 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003117 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003118 if (!array)
3119 return -ENOMEM;
3120 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003121 cgroup_iter_start(cgrp, &it);
3122 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003123 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003124 break;
Ben Blum102a7752009-09-23 15:56:26 -07003125 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003126 if (type == CGROUP_FILE_PROCS)
3127 pid = task_tgid_vnr(tsk);
3128 else
3129 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003130 if (pid > 0) /* make sure to only use valid results */
3131 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003132 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003133 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003134 length = n;
3135 /* now sort & (if procs) strip out duplicates */
3136 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003137 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003138 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003139 l = cgroup_pidlist_find(cgrp, type);
3140 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003141 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003142 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003143 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003144 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003145 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003146 l->list = array;
3147 l->length = length;
3148 l->use_count++;
3149 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003150 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003151 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003152}
3153
Balbir Singh846c7bb2007-10-18 23:39:44 -07003154/**
Li Zefana043e3b2008-02-23 15:24:09 -08003155 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003156 * @stats: cgroupstats to fill information into
3157 * @dentry: A dentry entry belonging to the cgroup for which stats have
3158 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003159 *
3160 * Build and fill cgroupstats so that taskstats can export it to user
3161 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003162 */
3163int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3164{
3165 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003166 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003167 struct cgroup_iter it;
3168 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003169
Balbir Singh846c7bb2007-10-18 23:39:44 -07003170 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003171 * Validate dentry by checking the superblock operations,
3172 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003173 */
Li Zefan33d283b2008-11-19 15:36:48 -08003174 if (dentry->d_sb->s_op != &cgroup_ops ||
3175 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003176 goto err;
3177
3178 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003179 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003180
Paul Menagebd89aab2007-10-18 23:40:44 -07003181 cgroup_iter_start(cgrp, &it);
3182 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003183 switch (tsk->state) {
3184 case TASK_RUNNING:
3185 stats->nr_running++;
3186 break;
3187 case TASK_INTERRUPTIBLE:
3188 stats->nr_sleeping++;
3189 break;
3190 case TASK_UNINTERRUPTIBLE:
3191 stats->nr_uninterruptible++;
3192 break;
3193 case TASK_STOPPED:
3194 stats->nr_stopped++;
3195 break;
3196 default:
3197 if (delayacct_is_task_waiting_on_io(tsk))
3198 stats->nr_io_wait++;
3199 break;
3200 }
3201 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003202 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003203
Balbir Singh846c7bb2007-10-18 23:39:44 -07003204err:
3205 return ret;
3206}
3207
Paul Menage8f3ff202009-09-23 15:56:25 -07003208
Paul Menagecc31edc2008-10-18 20:28:04 -07003209/*
Ben Blum102a7752009-09-23 15:56:26 -07003210 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003211 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003212 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003213 */
3214
Ben Blum102a7752009-09-23 15:56:26 -07003215static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003216{
3217 /*
3218 * Initially we receive a position value that corresponds to
3219 * one more than the last pid shown (or 0 on the first call or
3220 * after a seek to the start). Use a binary-search to find the
3221 * next pid to display, if any
3222 */
Ben Blum102a7752009-09-23 15:56:26 -07003223 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003224 int index = 0, pid = *pos;
3225 int *iter;
3226
Ben Blum102a7752009-09-23 15:56:26 -07003227 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003228 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003229 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003230
Paul Menagecc31edc2008-10-18 20:28:04 -07003231 while (index < end) {
3232 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003233 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003234 index = mid;
3235 break;
Ben Blum102a7752009-09-23 15:56:26 -07003236 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003237 index = mid + 1;
3238 else
3239 end = mid;
3240 }
3241 }
3242 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003243 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003244 return NULL;
3245 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003246 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003247 *pos = *iter;
3248 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003249}
3250
Ben Blum102a7752009-09-23 15:56:26 -07003251static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003252{
Ben Blum102a7752009-09-23 15:56:26 -07003253 struct cgroup_pidlist *l = s->private;
3254 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003255}
3256
Ben Blum102a7752009-09-23 15:56:26 -07003257static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003258{
Ben Blum102a7752009-09-23 15:56:26 -07003259 struct cgroup_pidlist *l = s->private;
3260 pid_t *p = v;
3261 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003262 /*
3263 * Advance to the next pid in the array. If this goes off the
3264 * end, we're done
3265 */
3266 p++;
3267 if (p >= end) {
3268 return NULL;
3269 } else {
3270 *pos = *p;
3271 return p;
3272 }
3273}
3274
Ben Blum102a7752009-09-23 15:56:26 -07003275static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003276{
3277 return seq_printf(s, "%d\n", *(int *)v);
3278}
3279
Ben Blum102a7752009-09-23 15:56:26 -07003280/*
3281 * seq_operations functions for iterating on pidlists through seq_file -
3282 * independent of whether it's tasks or procs
3283 */
3284static const struct seq_operations cgroup_pidlist_seq_operations = {
3285 .start = cgroup_pidlist_start,
3286 .stop = cgroup_pidlist_stop,
3287 .next = cgroup_pidlist_next,
3288 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003289};
3290
Ben Blum102a7752009-09-23 15:56:26 -07003291static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003292{
Ben Blum72a8cb32009-09-23 15:56:27 -07003293 /*
3294 * the case where we're the last user of this particular pidlist will
3295 * have us remove it from the cgroup's list, which entails taking the
3296 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3297 * pidlist_mutex, we have to take pidlist_mutex first.
3298 */
3299 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003300 down_write(&l->mutex);
3301 BUG_ON(!l->use_count);
3302 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003303 /* we're the last user if refcount is 0; remove and free */
3304 list_del(&l->links);
3305 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003306 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003307 put_pid_ns(l->key.ns);
3308 up_write(&l->mutex);
3309 kfree(l);
3310 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003311 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003312 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003313 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003314}
3315
Ben Blum102a7752009-09-23 15:56:26 -07003316static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003317{
Ben Blum102a7752009-09-23 15:56:26 -07003318 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003319 if (!(file->f_mode & FMODE_READ))
3320 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003321 /*
3322 * the seq_file will only be initialized if the file was opened for
3323 * reading; hence we check if it's not null only in that case.
3324 */
3325 l = ((struct seq_file *)file->private_data)->private;
3326 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003327 return seq_release(inode, file);
3328}
3329
Ben Blum102a7752009-09-23 15:56:26 -07003330static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003331 .read = seq_read,
3332 .llseek = seq_lseek,
3333 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003334 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003335};
3336
3337/*
Ben Blum102a7752009-09-23 15:56:26 -07003338 * The following functions handle opens on a file that displays a pidlist
3339 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3340 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003341 */
Ben Blum102a7752009-09-23 15:56:26 -07003342/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003343static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003344{
3345 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003346 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003347 int retval;
3348
3349 /* Nothing to do for write-only files */
3350 if (!(file->f_mode & FMODE_READ))
3351 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003352
Ben Blum102a7752009-09-23 15:56:26 -07003353 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003354 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003355 if (retval)
3356 return retval;
3357 /* configure file information */
3358 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003359
Ben Blum102a7752009-09-23 15:56:26 -07003360 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003361 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003362 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003363 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003364 }
Ben Blum102a7752009-09-23 15:56:26 -07003365 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003366 return 0;
3367}
Ben Blum102a7752009-09-23 15:56:26 -07003368static int cgroup_tasks_open(struct inode *unused, struct file *file)
3369{
Ben Blum72a8cb32009-09-23 15:56:27 -07003370 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003371}
3372static int cgroup_procs_open(struct inode *unused, struct file *file)
3373{
Ben Blum72a8cb32009-09-23 15:56:27 -07003374 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003375}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003376
Paul Menagebd89aab2007-10-18 23:40:44 -07003377static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003378 struct cftype *cft)
3379{
Paul Menagebd89aab2007-10-18 23:40:44 -07003380 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003381}
3382
Paul Menage6379c102008-07-25 01:47:01 -07003383static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3384 struct cftype *cft,
3385 u64 val)
3386{
3387 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3388 if (val)
3389 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3390 else
3391 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3392 return 0;
3393}
3394
Paul Menagebbcb81d2007-10-18 23:39:32 -07003395/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003396 * Unregister event and free resources.
3397 *
3398 * Gets called from workqueue.
3399 */
3400static void cgroup_event_remove(struct work_struct *work)
3401{
3402 struct cgroup_event *event = container_of(work, struct cgroup_event,
3403 remove);
3404 struct cgroup *cgrp = event->cgrp;
3405
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003406 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3407
3408 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003409 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003410 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003411}
3412
3413/*
3414 * Gets called on POLLHUP on eventfd when user closes it.
3415 *
3416 * Called with wqh->lock held and interrupts disabled.
3417 */
3418static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3419 int sync, void *key)
3420{
3421 struct cgroup_event *event = container_of(wait,
3422 struct cgroup_event, wait);
3423 struct cgroup *cgrp = event->cgrp;
3424 unsigned long flags = (unsigned long)key;
3425
3426 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003427 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003428 spin_lock(&cgrp->event_list_lock);
3429 list_del(&event->list);
3430 spin_unlock(&cgrp->event_list_lock);
3431 /*
3432 * We are in atomic context, but cgroup_event_remove() may
3433 * sleep, so we have to call it in workqueue.
3434 */
3435 schedule_work(&event->remove);
3436 }
3437
3438 return 0;
3439}
3440
3441static void cgroup_event_ptable_queue_proc(struct file *file,
3442 wait_queue_head_t *wqh, poll_table *pt)
3443{
3444 struct cgroup_event *event = container_of(pt,
3445 struct cgroup_event, pt);
3446
3447 event->wqh = wqh;
3448 add_wait_queue(wqh, &event->wait);
3449}
3450
3451/*
3452 * Parse input and register new cgroup event handler.
3453 *
3454 * Input must be in format '<event_fd> <control_fd> <args>'.
3455 * Interpretation of args is defined by control file implementation.
3456 */
3457static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3458 const char *buffer)
3459{
3460 struct cgroup_event *event = NULL;
3461 unsigned int efd, cfd;
3462 struct file *efile = NULL;
3463 struct file *cfile = NULL;
3464 char *endp;
3465 int ret;
3466
3467 efd = simple_strtoul(buffer, &endp, 10);
3468 if (*endp != ' ')
3469 return -EINVAL;
3470 buffer = endp + 1;
3471
3472 cfd = simple_strtoul(buffer, &endp, 10);
3473 if ((*endp != ' ') && (*endp != '\0'))
3474 return -EINVAL;
3475 buffer = endp + 1;
3476
3477 event = kzalloc(sizeof(*event), GFP_KERNEL);
3478 if (!event)
3479 return -ENOMEM;
3480 event->cgrp = cgrp;
3481 INIT_LIST_HEAD(&event->list);
3482 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3483 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3484 INIT_WORK(&event->remove, cgroup_event_remove);
3485
3486 efile = eventfd_fget(efd);
3487 if (IS_ERR(efile)) {
3488 ret = PTR_ERR(efile);
3489 goto fail;
3490 }
3491
3492 event->eventfd = eventfd_ctx_fileget(efile);
3493 if (IS_ERR(event->eventfd)) {
3494 ret = PTR_ERR(event->eventfd);
3495 goto fail;
3496 }
3497
3498 cfile = fget(cfd);
3499 if (!cfile) {
3500 ret = -EBADF;
3501 goto fail;
3502 }
3503
3504 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003505 /* AV: shouldn't we check that it's been opened for read instead? */
3506 ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003507 if (ret < 0)
3508 goto fail;
3509
3510 event->cft = __file_cft(cfile);
3511 if (IS_ERR(event->cft)) {
3512 ret = PTR_ERR(event->cft);
3513 goto fail;
3514 }
3515
3516 if (!event->cft->register_event || !event->cft->unregister_event) {
3517 ret = -EINVAL;
3518 goto fail;
3519 }
3520
3521 ret = event->cft->register_event(cgrp, event->cft,
3522 event->eventfd, buffer);
3523 if (ret)
3524 goto fail;
3525
3526 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3527 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3528 ret = 0;
3529 goto fail;
3530 }
3531
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003532 /*
3533 * Events should be removed after rmdir of cgroup directory, but before
3534 * destroying subsystem state objects. Let's take reference to cgroup
3535 * directory dentry to do that.
3536 */
3537 dget(cgrp->dentry);
3538
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003539 spin_lock(&cgrp->event_list_lock);
3540 list_add(&event->list, &cgrp->event_list);
3541 spin_unlock(&cgrp->event_list_lock);
3542
3543 fput(cfile);
3544 fput(efile);
3545
3546 return 0;
3547
3548fail:
3549 if (cfile)
3550 fput(cfile);
3551
3552 if (event && event->eventfd && !IS_ERR(event->eventfd))
3553 eventfd_ctx_put(event->eventfd);
3554
3555 if (!IS_ERR_OR_NULL(efile))
3556 fput(efile);
3557
3558 kfree(event);
3559
3560 return ret;
3561}
3562
Daniel Lezcano97978e62010-10-27 15:33:35 -07003563static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3564 struct cftype *cft)
3565{
3566 return clone_children(cgrp);
3567}
3568
3569static int cgroup_clone_children_write(struct cgroup *cgrp,
3570 struct cftype *cft,
3571 u64 val)
3572{
3573 if (val)
3574 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3575 else
3576 clear_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3577 return 0;
3578}
3579
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003580/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003581 * for the common functions, 'private' gives the type of file
3582 */
Ben Blum102a7752009-09-23 15:56:26 -07003583/* for hysterical raisins, we can't put this on the older files */
3584#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003585static struct cftype files[] = {
3586 {
3587 .name = "tasks",
3588 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003589 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003590 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003591 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003592 },
Ben Blum102a7752009-09-23 15:56:26 -07003593 {
3594 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3595 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003596 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003597 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003598 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003599 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003600 {
3601 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003602 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003603 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003604 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003605 {
3606 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3607 .write_string = cgroup_write_event_control,
3608 .mode = S_IWUGO,
3609 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003610 {
3611 .name = "cgroup.clone_children",
3612 .read_u64 = cgroup_clone_children_read,
3613 .write_u64 = cgroup_clone_children_write,
3614 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003615};
3616
3617static struct cftype cft_release_agent = {
3618 .name = "release_agent",
Paul Menagee788e062008-07-25 01:46:59 -07003619 .read_seq_string = cgroup_release_agent_show,
3620 .write_string = cgroup_release_agent_write,
3621 .max_write_len = PATH_MAX,
Paul Menagebbcb81d2007-10-18 23:39:32 -07003622};
3623
Paul Menagebd89aab2007-10-18 23:40:44 -07003624static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003625{
3626 int err;
3627 struct cgroup_subsys *ss;
3628
3629 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07003630 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003631
Paul Menagebd89aab2007-10-18 23:40:44 -07003632 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07003633 if (err < 0)
3634 return err;
3635
Paul Menagebd89aab2007-10-18 23:40:44 -07003636 if (cgrp == cgrp->top_cgroup) {
3637 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003638 return err;
3639 }
3640
Paul Menagebd89aab2007-10-18 23:40:44 -07003641 for_each_subsys(cgrp->root, ss) {
3642 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003643 return err;
3644 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003645 /* This cgroup is ready now */
3646 for_each_subsys(cgrp->root, ss) {
3647 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3648 /*
3649 * Update id->css pointer and make this css visible from
3650 * CSS ID functions. This pointer will be dereferened
3651 * from RCU-read-side without locks.
3652 */
3653 if (css->id)
3654 rcu_assign_pointer(css->id->css, css);
3655 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003656
3657 return 0;
3658}
3659
3660static void init_cgroup_css(struct cgroup_subsys_state *css,
3661 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07003662 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003663{
Paul Menagebd89aab2007-10-18 23:40:44 -07003664 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08003665 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003666 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003667 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003668 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003669 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07003670 BUG_ON(cgrp->subsys[ss->subsys_id]);
3671 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003672}
3673
Paul Menage999cd8a2009-01-07 18:08:36 -08003674static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
3675{
3676 /* We need to take each hierarchy_mutex in a consistent order */
3677 int i;
3678
Ben Blumaae8aab2010-03-10 15:22:07 -08003679 /*
3680 * No worry about a race with rebind_subsystems that might mess up the
3681 * locking order, since both parties are under cgroup_mutex.
3682 */
Paul Menage999cd8a2009-01-07 18:08:36 -08003683 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3684 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003685 if (ss == NULL)
3686 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08003687 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08003688 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08003689 }
3690}
3691
3692static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
3693{
3694 int i;
3695
3696 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3697 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003698 if (ss == NULL)
3699 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08003700 if (ss->root == root)
3701 mutex_unlock(&ss->hierarchy_mutex);
3702 }
3703}
3704
Paul Menageddbcc7e2007-10-18 23:39:30 -07003705/*
Li Zefana043e3b2008-02-23 15:24:09 -08003706 * cgroup_create - create a cgroup
3707 * @parent: cgroup that will be parent of the new cgroup
3708 * @dentry: dentry of the new cgroup
3709 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07003710 *
Li Zefana043e3b2008-02-23 15:24:09 -08003711 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07003712 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07003713static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04003714 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003715{
Paul Menagebd89aab2007-10-18 23:40:44 -07003716 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003717 struct cgroupfs_root *root = parent->root;
3718 int err = 0;
3719 struct cgroup_subsys *ss;
3720 struct super_block *sb = root->sb;
3721
Paul Menagebd89aab2007-10-18 23:40:44 -07003722 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3723 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003724 return -ENOMEM;
3725
3726 /* Grab a reference on the superblock so the hierarchy doesn't
3727 * get deleted on unmount if there are child cgroups. This
3728 * can be done outside cgroup_mutex, since the sb can't
3729 * disappear while someone has an open control file on the
3730 * fs */
3731 atomic_inc(&sb->s_active);
3732
3733 mutex_lock(&cgroup_mutex);
3734
Paul Menagecc31edc2008-10-18 20:28:04 -07003735 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003736
Paul Menagebd89aab2007-10-18 23:40:44 -07003737 cgrp->parent = parent;
3738 cgrp->root = parent->root;
3739 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003740
Li Zefanb6abdb02008-03-04 14:28:19 -08003741 if (notify_on_release(parent))
3742 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3743
Daniel Lezcano97978e62010-10-27 15:33:35 -07003744 if (clone_children(parent))
3745 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3746
Paul Menageddbcc7e2007-10-18 23:39:30 -07003747 for_each_subsys(root, ss) {
Li Zefan761b3ef2012-01-31 13:47:36 +08003748 struct cgroup_subsys_state *css = ss->create(cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003749
Paul Menageddbcc7e2007-10-18 23:39:30 -07003750 if (IS_ERR(css)) {
3751 err = PTR_ERR(css);
3752 goto err_destroy;
3753 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003754 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003755 if (ss->use_id) {
3756 err = alloc_css_id(ss, parent, cgrp);
3757 if (err)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003758 goto err_destroy;
Li Zefan4528fd02010-02-02 13:44:10 -08003759 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003760 /* At error, ->destroy() callback has to free assigned ID. */
Daniel Lezcano97978e62010-10-27 15:33:35 -07003761 if (clone_children(parent) && ss->post_clone)
Li Zefan761b3ef2012-01-31 13:47:36 +08003762 ss->post_clone(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003763 }
3764
Paul Menage999cd8a2009-01-07 18:08:36 -08003765 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003766 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08003767 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003768 root->number_of_cgroups++;
3769
Paul Menagebd89aab2007-10-18 23:40:44 -07003770 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003771 if (err < 0)
3772 goto err_remove;
3773
Colin Crossc0f6fa82010-11-23 21:37:03 -08003774 set_bit(CGRP_RELEASABLE, &parent->flags);
3775
Paul Menageddbcc7e2007-10-18 23:39:30 -07003776 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07003777 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003778
Paul Menagebd89aab2007-10-18 23:40:44 -07003779 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003780 /* If err < 0, we have a half-filled directory - oh well ;) */
3781
3782 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003783 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003784
3785 return 0;
3786
3787 err_remove:
3788
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003789 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003790 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003791 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003792 root->number_of_cgroups--;
3793
3794 err_destroy:
3795
3796 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003797 if (cgrp->subsys[ss->subsys_id])
Li Zefan761b3ef2012-01-31 13:47:36 +08003798 ss->destroy(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003799 }
3800
3801 mutex_unlock(&cgroup_mutex);
3802
3803 /* Release the reference count that we took on the superblock */
3804 deactivate_super(sb);
3805
Paul Menagebd89aab2007-10-18 23:40:44 -07003806 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003807 return err;
3808}
3809
Al Viro18bb1db2011-07-26 01:41:39 -04003810static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003811{
3812 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
3813
3814 /* the vfs holds inode->i_mutex already */
3815 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
3816}
3817
Li Zefan55b6fd02008-07-29 22:33:20 -07003818static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003819{
3820 /* Check the reference count on each subsystem. Since we
3821 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08003822 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07003823 * be no outstanding references, so the subsystem is safe to
3824 * destroy. We scan across all subsystems rather than using
3825 * the per-hierarchy linked list of mounted subsystems since
3826 * we can be called via check_for_release() with no
3827 * synchronization other than RCU, and the subsystem linked
3828 * list isn't RCU-safe */
3829 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08003830 /*
3831 * We won't need to lock the subsys array, because the subsystems
3832 * we're concerned about aren't going anywhere since our cgroup root
3833 * has a reference on them.
3834 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003835 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3836 struct cgroup_subsys *ss = subsys[i];
3837 struct cgroup_subsys_state *css;
Ben Blumaae8aab2010-03-10 15:22:07 -08003838 /* Skip subsystems not present or not in this hierarchy */
3839 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003840 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07003841 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07003842 /* When called from check_for_release() it's possible
3843 * that by this point the cgroup has been removed
3844 * and the css deleted. But a false-positive doesn't
3845 * matter, since it can only happen if the cgroup
3846 * has been deleted and hence no longer needs the
3847 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08003848 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07003849 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003850 }
3851 return 0;
3852}
3853
Paul Menagee7c5ec92009-01-07 18:08:38 -08003854/*
3855 * Atomically mark all (or else none) of the cgroup's CSS objects as
3856 * CSS_REMOVED. Return true on success, or false if the cgroup has
3857 * busy subsystems. Call with cgroup_mutex held
3858 */
3859
3860static int cgroup_clear_css_refs(struct cgroup *cgrp)
3861{
3862 struct cgroup_subsys *ss;
3863 unsigned long flags;
3864 bool failed = false;
3865 local_irq_save(flags);
3866 for_each_subsys(cgrp->root, ss) {
3867 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3868 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08003869 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08003870 /* We can only remove a CSS with a refcnt==1 */
3871 refcnt = atomic_read(&css->refcnt);
3872 if (refcnt > 1) {
3873 failed = true;
3874 goto done;
3875 }
3876 BUG_ON(!refcnt);
3877 /*
3878 * Drop the refcnt to 0 while we check other
3879 * subsystems. This will cause any racing
3880 * css_tryget() to spin until we set the
3881 * CSS_REMOVED bits or abort
3882 */
Paul Menage804b3c22009-01-29 14:25:21 -08003883 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
3884 break;
3885 cpu_relax();
3886 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08003887 }
3888 done:
3889 for_each_subsys(cgrp->root, ss) {
3890 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3891 if (failed) {
3892 /*
3893 * Restore old refcnt if we previously managed
3894 * to clear it from 1 to 0
3895 */
3896 if (!atomic_read(&css->refcnt))
3897 atomic_set(&css->refcnt, 1);
3898 } else {
3899 /* Commit the fact that the CSS is removed */
3900 set_bit(CSS_REMOVED, &css->flags);
3901 }
3902 }
3903 local_irq_restore(flags);
3904 return !failed;
3905}
3906
Paul Menageddbcc7e2007-10-18 23:39:30 -07003907static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
3908{
Paul Menagebd89aab2007-10-18 23:40:44 -07003909 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003910 struct dentry *d;
3911 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003912 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08003913 struct cgroup_event *event, *tmp;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003914 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003915
3916 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003917again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07003918 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003919 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003920 mutex_unlock(&cgroup_mutex);
3921 return -EBUSY;
3922 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003923 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003924 mutex_unlock(&cgroup_mutex);
3925 return -EBUSY;
3926 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003927 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08003928
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003929 /*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003930 * In general, subsystem has no css->refcnt after pre_destroy(). But
3931 * in racy cases, subsystem may have to get css->refcnt after
3932 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
3933 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
3934 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
3935 * and subsystem's reference count handling. Please see css_get/put
3936 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3937 */
3938 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3939
3940 /*
Li Zefana043e3b2008-02-23 15:24:09 -08003941 * Call pre_destroy handlers of subsys. Notify subsystems
3942 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003943 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003944 ret = cgroup_call_pre_destroy(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003945 if (ret) {
3946 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003947 return ret;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003948 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003949
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003950 mutex_lock(&cgroup_mutex);
3951 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003952 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003953 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003954 mutex_unlock(&cgroup_mutex);
3955 return -EBUSY;
3956 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003957 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003958 if (!cgroup_clear_css_refs(cgrp)) {
3959 mutex_unlock(&cgroup_mutex);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003960 /*
3961 * Because someone may call cgroup_wakeup_rmdir_waiter() before
3962 * prepare_to_wait(), we need to check this flag.
3963 */
3964 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
3965 schedule();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003966 finish_wait(&cgroup_rmdir_waitq, &wait);
3967 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3968 if (signal_pending(current))
3969 return -EINTR;
3970 goto again;
3971 }
3972 /* NO css_tryget() can success after here. */
3973 finish_wait(&cgroup_rmdir_waitq, &wait);
3974 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003975
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02003976 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003977 set_bit(CGRP_REMOVED, &cgrp->flags);
3978 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07003979 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02003980 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08003981
3982 cgroup_lock_hierarchy(cgrp->root);
3983 /* delete this cgroup from parent->children */
Phil Carmody8d258792011-03-22 16:30:13 -07003984 list_del_init(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08003985 cgroup_unlock_hierarchy(cgrp->root);
3986
Paul Menagebd89aab2007-10-18 23:40:44 -07003987 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003988
3989 cgroup_d_remove_dir(d);
3990 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003991
Paul Menage81a6a5c2007-10-18 23:39:38 -07003992 check_for_release(parent);
3993
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08003994 /*
3995 * Unregister events and notify userspace.
3996 * Notify userspace about cgroup removing only after rmdir of cgroup
3997 * directory to avoid race between userspace and kernelspace
3998 */
3999 spin_lock(&cgrp->event_list_lock);
4000 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4001 list_del(&event->list);
4002 remove_wait_queue(event->wqh, &event->wait);
4003 eventfd_signal(event->eventfd, 1);
4004 schedule_work(&event->remove);
4005 }
4006 spin_unlock(&cgrp->event_list_lock);
4007
Paul Menageddbcc7e2007-10-18 23:39:30 -07004008 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004009 return 0;
4010}
4011
Li Zefan06a11922008-04-29 01:00:07 -07004012static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004013{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004014 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004015
4016 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004017
4018 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004019 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004020 ss->root = &rootnode;
Li Zefan761b3ef2012-01-31 13:47:36 +08004021 css = ss->create(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004022 /* We don't handle early failures gracefully */
4023 BUG_ON(IS_ERR(css));
4024 init_cgroup_css(css, ss, dummytop);
4025
Li Zefane8d55fd2008-04-29 01:00:13 -07004026 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004027 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004028 * newly registered, all tasks and hence the
4029 * init_css_set is in the subsystem's top cgroup. */
4030 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004031
4032 need_forkexit_callback |= ss->fork || ss->exit;
4033
Li Zefane8d55fd2008-04-29 01:00:13 -07004034 /* At system boot, before all subsystems have been
4035 * registered, no tasks have been forked, so we don't
4036 * need to invoke fork callbacks here. */
4037 BUG_ON(!list_empty(&init_task.tasks));
4038
Paul Menage999cd8a2009-01-07 18:08:36 -08004039 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08004040 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004041 ss->active = 1;
Ben Blume6a11052010-03-10 15:22:09 -08004042
4043 /* this function shouldn't be used with modular subsystems, since they
4044 * need to register a subsys_id, among other things */
4045 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004046}
4047
4048/**
Ben Blume6a11052010-03-10 15:22:09 -08004049 * cgroup_load_subsys: load and register a modular subsystem at runtime
4050 * @ss: the subsystem to load
4051 *
4052 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004053 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004054 * up for use. If the subsystem is built-in anyway, work is delegated to the
4055 * simpler cgroup_init_subsys.
4056 */
4057int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4058{
4059 int i;
4060 struct cgroup_subsys_state *css;
4061
4062 /* check name and function validity */
4063 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4064 ss->create == NULL || ss->destroy == NULL)
4065 return -EINVAL;
4066
4067 /*
4068 * we don't support callbacks in modular subsystems. this check is
4069 * before the ss->module check for consistency; a subsystem that could
4070 * be a module should still have no callbacks even if the user isn't
4071 * compiling it as one.
4072 */
4073 if (ss->fork || ss->exit)
4074 return -EINVAL;
4075
4076 /*
4077 * an optionally modular subsystem is built-in: we want to do nothing,
4078 * since cgroup_init_subsys will have already taken care of it.
4079 */
4080 if (ss->module == NULL) {
4081 /* a few sanity checks */
4082 BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
4083 BUG_ON(subsys[ss->subsys_id] != ss);
4084 return 0;
4085 }
4086
4087 /*
4088 * need to register a subsys id before anything else - for example,
4089 * init_cgroup_css needs it.
4090 */
4091 mutex_lock(&cgroup_mutex);
4092 /* find the first empty slot in the array */
4093 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
4094 if (subsys[i] == NULL)
4095 break;
4096 }
4097 if (i == CGROUP_SUBSYS_COUNT) {
4098 /* maximum number of subsystems already registered! */
4099 mutex_unlock(&cgroup_mutex);
4100 return -EBUSY;
4101 }
4102 /* assign ourselves the subsys_id */
4103 ss->subsys_id = i;
4104 subsys[i] = ss;
4105
4106 /*
4107 * no ss->create seems to need anything important in the ss struct, so
4108 * this can happen first (i.e. before the rootnode attachment).
4109 */
Li Zefan761b3ef2012-01-31 13:47:36 +08004110 css = ss->create(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004111 if (IS_ERR(css)) {
4112 /* failure case - need to deassign the subsys[] slot. */
4113 subsys[i] = NULL;
4114 mutex_unlock(&cgroup_mutex);
4115 return PTR_ERR(css);
4116 }
4117
4118 list_add(&ss->sibling, &rootnode.subsys_list);
4119 ss->root = &rootnode;
4120
4121 /* our new subsystem will be attached to the dummy hierarchy. */
4122 init_cgroup_css(css, ss, dummytop);
4123 /* init_idr must be after init_cgroup_css because it sets css->id. */
4124 if (ss->use_id) {
4125 int ret = cgroup_init_idr(ss, css);
4126 if (ret) {
4127 dummytop->subsys[ss->subsys_id] = NULL;
Li Zefan761b3ef2012-01-31 13:47:36 +08004128 ss->destroy(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004129 subsys[i] = NULL;
4130 mutex_unlock(&cgroup_mutex);
4131 return ret;
4132 }
4133 }
4134
4135 /*
4136 * Now we need to entangle the css into the existing css_sets. unlike
4137 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4138 * will need a new pointer to it; done by iterating the css_set_table.
4139 * furthermore, modifying the existing css_sets will corrupt the hash
4140 * table state, so each changed css_set will need its hash recomputed.
4141 * this is all done under the css_set_lock.
4142 */
4143 write_lock(&css_set_lock);
4144 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4145 struct css_set *cg;
4146 struct hlist_node *node, *tmp;
4147 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4148
4149 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4150 /* skip entries that we already rehashed */
4151 if (cg->subsys[ss->subsys_id])
4152 continue;
4153 /* remove existing entry */
4154 hlist_del(&cg->hlist);
4155 /* set new value */
4156 cg->subsys[ss->subsys_id] = css;
4157 /* recompute hash and restore entry */
4158 new_bucket = css_set_hash(cg->subsys);
4159 hlist_add_head(&cg->hlist, new_bucket);
4160 }
4161 }
4162 write_unlock(&css_set_lock);
4163
4164 mutex_init(&ss->hierarchy_mutex);
4165 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
4166 ss->active = 1;
4167
Ben Blume6a11052010-03-10 15:22:09 -08004168 /* success! */
4169 mutex_unlock(&cgroup_mutex);
4170 return 0;
4171}
4172EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4173
4174/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004175 * cgroup_unload_subsys: unload a modular subsystem
4176 * @ss: the subsystem to unload
4177 *
4178 * This function should be called in a modular subsystem's exitcall. When this
4179 * function is invoked, the refcount on the subsystem's module will be 0, so
4180 * the subsystem will not be attached to any hierarchy.
4181 */
4182void cgroup_unload_subsys(struct cgroup_subsys *ss)
4183{
4184 struct cg_cgroup_link *link;
4185 struct hlist_head *hhead;
4186
4187 BUG_ON(ss->module == NULL);
4188
4189 /*
4190 * we shouldn't be called if the subsystem is in use, and the use of
4191 * try_module_get in parse_cgroupfs_options should ensure that it
4192 * doesn't start being used while we're killing it off.
4193 */
4194 BUG_ON(ss->root != &rootnode);
4195
4196 mutex_lock(&cgroup_mutex);
4197 /* deassign the subsys_id */
4198 BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT);
4199 subsys[ss->subsys_id] = NULL;
4200
4201 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004202 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004203
4204 /*
4205 * disentangle the css from all css_sets attached to the dummytop. as
4206 * in loading, we need to pay our respects to the hashtable gods.
4207 */
4208 write_lock(&css_set_lock);
4209 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4210 struct css_set *cg = link->cg;
4211
4212 hlist_del(&cg->hlist);
4213 BUG_ON(!cg->subsys[ss->subsys_id]);
4214 cg->subsys[ss->subsys_id] = NULL;
4215 hhead = css_set_hash(cg->subsys);
4216 hlist_add_head(&cg->hlist, hhead);
4217 }
4218 write_unlock(&css_set_lock);
4219
4220 /*
4221 * remove subsystem's css from the dummytop and free it - need to free
4222 * before marking as null because ss->destroy needs the cgrp->subsys
4223 * pointer to find their state. note that this also takes care of
4224 * freeing the css_id.
4225 */
Li Zefan761b3ef2012-01-31 13:47:36 +08004226 ss->destroy(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004227 dummytop->subsys[ss->subsys_id] = NULL;
4228
4229 mutex_unlock(&cgroup_mutex);
4230}
4231EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4232
4233/**
Li Zefana043e3b2008-02-23 15:24:09 -08004234 * cgroup_init_early - cgroup initialization at system boot
4235 *
4236 * Initialize cgroups at system boot, and initialize any
4237 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004238 */
4239int __init cgroup_init_early(void)
4240{
4241 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004242 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004243 INIT_LIST_HEAD(&init_css_set.cg_links);
4244 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004245 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004246 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004247 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004248 root_count = 1;
4249 init_task.cgroups = &init_css_set;
4250
4251 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004252 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004253 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004254 &rootnode.top_cgroup.css_sets);
4255 list_add(&init_css_set_link.cg_link_list,
4256 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004257
Li Zefan472b1052008-04-29 01:00:11 -07004258 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4259 INIT_HLIST_HEAD(&css_set_table[i]);
4260
Ben Blumaae8aab2010-03-10 15:22:07 -08004261 /* at bootup time, we don't worry about modular subsystems */
4262 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004263 struct cgroup_subsys *ss = subsys[i];
4264
4265 BUG_ON(!ss->name);
4266 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
4267 BUG_ON(!ss->create);
4268 BUG_ON(!ss->destroy);
4269 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004270 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004271 ss->name, ss->subsys_id);
4272 BUG();
4273 }
4274
4275 if (ss->early_init)
4276 cgroup_init_subsys(ss);
4277 }
4278 return 0;
4279}
4280
4281/**
Li Zefana043e3b2008-02-23 15:24:09 -08004282 * cgroup_init - cgroup initialization
4283 *
4284 * Register cgroup filesystem and /proc file, and initialize
4285 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004286 */
4287int __init cgroup_init(void)
4288{
4289 int err;
4290 int i;
Li Zefan472b1052008-04-29 01:00:11 -07004291 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07004292
4293 err = bdi_init(&cgroup_backing_dev_info);
4294 if (err)
4295 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004296
Ben Blumaae8aab2010-03-10 15:22:07 -08004297 /* at bootup time, we don't worry about modular subsystems */
4298 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004299 struct cgroup_subsys *ss = subsys[i];
4300 if (!ss->early_init)
4301 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004302 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004303 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004304 }
4305
Li Zefan472b1052008-04-29 01:00:11 -07004306 /* Add init_css_set to the hash table */
4307 hhead = css_set_hash(init_css_set.subsys);
4308 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004309 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004310
4311 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4312 if (!cgroup_kobj) {
4313 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004314 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004315 }
4316
4317 err = register_filesystem(&cgroup_fs_type);
4318 if (err < 0) {
4319 kobject_put(cgroup_kobj);
4320 goto out;
4321 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004322
Li Zefan46ae2202008-04-29 01:00:08 -07004323 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004324
Paul Menageddbcc7e2007-10-18 23:39:30 -07004325out:
Paul Menagea4243162007-10-18 23:39:35 -07004326 if (err)
4327 bdi_destroy(&cgroup_backing_dev_info);
4328
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329 return err;
4330}
Paul Menageb4f48b62007-10-18 23:39:33 -07004331
Paul Menagea4243162007-10-18 23:39:35 -07004332/*
4333 * proc_cgroup_show()
4334 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4335 * - Used for /proc/<pid>/cgroup.
4336 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4337 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004338 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004339 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4340 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4341 * cgroup to top_cgroup.
4342 */
4343
4344/* TODO: Use a proper seq_file iterator */
4345static int proc_cgroup_show(struct seq_file *m, void *v)
4346{
4347 struct pid *pid;
4348 struct task_struct *tsk;
4349 char *buf;
4350 int retval;
4351 struct cgroupfs_root *root;
4352
4353 retval = -ENOMEM;
4354 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4355 if (!buf)
4356 goto out;
4357
4358 retval = -ESRCH;
4359 pid = m->private;
4360 tsk = get_pid_task(pid, PIDTYPE_PID);
4361 if (!tsk)
4362 goto out_free;
4363
4364 retval = 0;
4365
4366 mutex_lock(&cgroup_mutex);
4367
Li Zefane5f6a862009-01-07 18:07:41 -08004368 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004369 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004370 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004371 int count = 0;
4372
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004373 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004374 for_each_subsys(root, ss)
4375 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004376 if (strlen(root->name))
4377 seq_printf(m, "%sname=%s", count ? "," : "",
4378 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004379 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004380 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004381 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004382 if (retval < 0)
4383 goto out_unlock;
4384 seq_puts(m, buf);
4385 seq_putc(m, '\n');
4386 }
4387
4388out_unlock:
4389 mutex_unlock(&cgroup_mutex);
4390 put_task_struct(tsk);
4391out_free:
4392 kfree(buf);
4393out:
4394 return retval;
4395}
4396
4397static int cgroup_open(struct inode *inode, struct file *file)
4398{
4399 struct pid *pid = PROC_I(inode)->pid;
4400 return single_open(file, proc_cgroup_show, pid);
4401}
4402
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004403const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004404 .open = cgroup_open,
4405 .read = seq_read,
4406 .llseek = seq_lseek,
4407 .release = single_release,
4408};
4409
4410/* Display information about each subsystem and each hierarchy */
4411static int proc_cgroupstats_show(struct seq_file *m, void *v)
4412{
4413 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004414
Paul Menage8bab8dd2008-04-04 14:29:57 -07004415 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004416 /*
4417 * ideally we don't want subsystems moving around while we do this.
4418 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4419 * subsys/hierarchy state.
4420 */
Paul Menagea4243162007-10-18 23:39:35 -07004421 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004422 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4423 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004424 if (ss == NULL)
4425 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004426 seq_printf(m, "%s\t%d\t%d\t%d\n",
4427 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004428 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004429 }
4430 mutex_unlock(&cgroup_mutex);
4431 return 0;
4432}
4433
4434static int cgroupstats_open(struct inode *inode, struct file *file)
4435{
Al Viro9dce07f12008-03-29 03:07:28 +00004436 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004437}
4438
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004439static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004440 .open = cgroupstats_open,
4441 .read = seq_read,
4442 .llseek = seq_lseek,
4443 .release = single_release,
4444};
4445
Paul Menageb4f48b62007-10-18 23:39:33 -07004446/**
4447 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004448 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004449 *
4450 * Description: A task inherits its parent's cgroup at fork().
4451 *
4452 * A pointer to the shared css_set was automatically copied in
4453 * fork.c by dup_task_struct(). However, we ignore that copy, since
Frederic Weisbecker7e381b0e2011-12-21 20:03:19 +01004454 * it was not made under the protection of RCU, cgroup_mutex or
4455 * threadgroup_change_begin(), so it might no longer be a valid
4456 * cgroup pointer. cgroup_attach_task() might have already changed
4457 * current->cgroups, allowing the previously referenced cgroup
4458 * group to be removed and freed.
4459 *
4460 * Outside the pointer validity we also need to process the css_set
4461 * inheritance between threadgoup_change_begin() and
4462 * threadgoup_change_end(), this way there is no leak in any process
4463 * wide migration performed by cgroup_attach_proc() that could otherwise
4464 * miss a thread because it is too early or too late in the fork stage.
Paul Menageb4f48b62007-10-18 23:39:33 -07004465 *
4466 * At the point that cgroup_fork() is called, 'current' is the parent
4467 * task, and the passed argument 'child' points to the child task.
4468 */
4469void cgroup_fork(struct task_struct *child)
4470{
Frederic Weisbecker7e381b0e2011-12-21 20:03:19 +01004471 /*
4472 * We don't need to task_lock() current because current->cgroups
4473 * can't be changed concurrently here. The parent obviously hasn't
4474 * exited and called cgroup_exit(), and we are synchronized against
4475 * cgroup migration through threadgroup_change_begin().
4476 */
Paul Menage817929e2007-10-18 23:39:36 -07004477 child->cgroups = current->cgroups;
4478 get_css_set(child->cgroups);
Paul Menage817929e2007-10-18 23:39:36 -07004479 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004480}
4481
4482/**
Li Zefana043e3b2008-02-23 15:24:09 -08004483 * cgroup_fork_callbacks - run fork callbacks
4484 * @child: the new task
4485 *
4486 * Called on a new task very soon before adding it to the
4487 * tasklist. No need to take any locks since no-one can
4488 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004489 */
4490void cgroup_fork_callbacks(struct task_struct *child)
4491{
4492 if (need_forkexit_callback) {
4493 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08004494 /*
4495 * forkexit callbacks are only supported for builtin
4496 * subsystems, and the builtin section of the subsys array is
4497 * immutable, so we don't need to lock the subsys array here.
4498 */
4499 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageb4f48b62007-10-18 23:39:33 -07004500 struct cgroup_subsys *ss = subsys[i];
4501 if (ss->fork)
Li Zefan761b3ef2012-01-31 13:47:36 +08004502 ss->fork(child);
Paul Menageb4f48b62007-10-18 23:39:33 -07004503 }
4504 }
4505}
4506
4507/**
Li Zefana043e3b2008-02-23 15:24:09 -08004508 * cgroup_post_fork - called on a new task after adding it to the task list
4509 * @child: the task in question
4510 *
4511 * Adds the task to the list running through its css_set if necessary.
4512 * Has to be after the task is visible on the task list in case we race
4513 * with the first call to cgroup_iter_start() - to guarantee that the
4514 * new task ends up on its list.
4515 */
Paul Menage817929e2007-10-18 23:39:36 -07004516void cgroup_post_fork(struct task_struct *child)
4517{
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004518 /*
4519 * use_task_css_set_links is set to 1 before we walk the tasklist
4520 * under the tasklist_lock and we read it here after we added the child
4521 * to the tasklist under the tasklist_lock as well. If the child wasn't
4522 * yet in the tasklist when we walked through it from
4523 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4524 * should be visible now due to the paired locking and barriers implied
4525 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4526 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4527 * lock on fork.
4528 */
Paul Menage817929e2007-10-18 23:39:36 -07004529 if (use_task_css_set_links) {
4530 write_lock(&css_set_lock);
Frederic Weisbecker7e3aa302011-12-23 04:25:23 +01004531 if (list_empty(&child->cg_list)) {
4532 /*
4533 * It's safe to use child->cgroups without task_lock()
4534 * here because we are protected through
4535 * threadgroup_change_begin() against concurrent
4536 * css_set change in cgroup_task_migrate(). Also
4537 * the task can't exit at that point until
4538 * wake_up_new_task() is called, so we are protected
4539 * against cgroup_exit() setting child->cgroup to
4540 * init_css_set.
4541 */
Paul Menage817929e2007-10-18 23:39:36 -07004542 list_add(&child->cg_list, &child->cgroups->tasks);
Frederic Weisbecker7e3aa302011-12-23 04:25:23 +01004543 }
Paul Menage817929e2007-10-18 23:39:36 -07004544 write_unlock(&css_set_lock);
4545 }
4546}
4547/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004548 * cgroup_exit - detach cgroup from exiting task
4549 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004550 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004551 *
4552 * Description: Detach cgroup from @tsk and release it.
4553 *
4554 * Note that cgroups marked notify_on_release force every task in
4555 * them to take the global cgroup_mutex mutex when exiting.
4556 * This could impact scaling on very large systems. Be reluctant to
4557 * use notify_on_release cgroups where very high task exit scaling
4558 * is required on large systems.
4559 *
4560 * the_top_cgroup_hack:
4561 *
4562 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4563 *
4564 * We call cgroup_exit() while the task is still competent to
4565 * handle notify_on_release(), then leave the task attached to the
4566 * root cgroup in each hierarchy for the remainder of its exit.
4567 *
4568 * To do this properly, we would increment the reference count on
4569 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4570 * code we would add a second cgroup function call, to drop that
4571 * reference. This would just create an unnecessary hot spot on
4572 * the top_cgroup reference count, to no avail.
4573 *
4574 * Normally, holding a reference to a cgroup without bumping its
4575 * count is unsafe. The cgroup could go away, or someone could
4576 * attach us to a different cgroup, decrementing the count on
4577 * the first cgroup that we never incremented. But in this case,
4578 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004579 * which wards off any cgroup_attach_task() attempts, or task is a failed
4580 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004581 */
4582void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4583{
Paul Menage817929e2007-10-18 23:39:36 -07004584 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004585 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004586
4587 /*
4588 * Unlink from the css_set task list if necessary.
4589 * Optimistically check cg_list before taking
4590 * css_set_lock
4591 */
4592 if (!list_empty(&tsk->cg_list)) {
4593 write_lock(&css_set_lock);
4594 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004595 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004596 write_unlock(&css_set_lock);
4597 }
4598
Paul Menageb4f48b62007-10-18 23:39:33 -07004599 /* Reassign the task to the init_css_set. */
4600 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004601 cg = tsk->cgroups;
4602 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004603
4604 if (run_callbacks && need_forkexit_callback) {
4605 /*
4606 * modular subsystems can't use callbacks, so no need to lock
4607 * the subsys array
4608 */
4609 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
4610 struct cgroup_subsys *ss = subsys[i];
4611 if (ss->exit) {
4612 struct cgroup *old_cgrp =
4613 rcu_dereference_raw(cg->subsys[i])->cgroup;
4614 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004615 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004616 }
4617 }
4618 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004619 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004620
Paul Menage817929e2007-10-18 23:39:36 -07004621 if (cg)
Colin Crossc0f6fa82010-11-23 21:37:03 -08004622 put_css_set(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004623}
Paul Menage697f4162007-10-18 23:39:34 -07004624
4625/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004626 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08004627 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004628 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08004629 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004630 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4631 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07004632 *
4633 * If we are sending in dummytop, then presumably we are creating
4634 * the top cgroup in the subsystem.
4635 *
4636 * Called only by the ns (nsproxy) cgroup.
4637 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004638int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07004639{
4640 int ret;
4641 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07004642
Paul Menagebd89aab2007-10-18 23:40:44 -07004643 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07004644 return 1;
4645
Paul Menage7717f7b2009-09-23 15:56:22 -07004646 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004647 while (cgrp != target && cgrp!= cgrp->top_cgroup)
4648 cgrp = cgrp->parent;
4649 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07004650 return ret;
4651}
Paul Menage81a6a5c2007-10-18 23:39:38 -07004652
Paul Menagebd89aab2007-10-18 23:40:44 -07004653static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004654{
4655 /* All of these checks rely on RCU to keep the cgroup
4656 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07004657 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
4658 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004659 /* Control Group is currently removeable. If it's not
4660 * already queued for a userspace notification, queue
4661 * it now */
4662 int need_schedule_work = 0;
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004663 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004664 if (!cgroup_is_removed(cgrp) &&
4665 list_empty(&cgrp->release_list)) {
4666 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004667 need_schedule_work = 1;
4668 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004669 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004670 if (need_schedule_work)
4671 schedule_work(&release_agent_work);
4672 }
4673}
4674
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004675/* Caller must verify that the css is not for root cgroup */
Colin Crossc0f6fa82010-11-23 21:37:03 -08004676void __css_get(struct cgroup_subsys_state *css, int count)
4677{
4678 atomic_add(count, &css->refcnt);
4679 set_bit(CGRP_RELEASABLE, &css->cgroup->flags);
4680}
4681EXPORT_SYMBOL_GPL(__css_get);
4682
4683/* Caller must verify that the css is not for root cgroup */
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004684void __css_put(struct cgroup_subsys_state *css, int count)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004685{
Paul Menagebd89aab2007-10-18 23:40:44 -07004686 struct cgroup *cgrp = css->cgroup;
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004687 int val;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004688 rcu_read_lock();
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004689 val = atomic_sub_return(count, &css->refcnt);
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004690 if (val == 1) {
Colin Crossc0f6fa82010-11-23 21:37:03 -08004691 check_for_release(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004692 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004693 }
4694 rcu_read_unlock();
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004695 WARN_ON_ONCE(val < 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004696}
Ben Blum67523c42010-03-10 15:22:11 -08004697EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004698
4699/*
4700 * Notify userspace when a cgroup is released, by running the
4701 * configured release agent with the name of the cgroup (path
4702 * relative to the root of cgroup file system) as the argument.
4703 *
4704 * Most likely, this user command will try to rmdir this cgroup.
4705 *
4706 * This races with the possibility that some other task will be
4707 * attached to this cgroup before it is removed, or that some other
4708 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4709 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4710 * unused, and this cgroup will be reprieved from its death sentence,
4711 * to continue to serve a useful existence. Next time it's released,
4712 * we will get notified again, if it still has 'notify_on_release' set.
4713 *
4714 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4715 * means only wait until the task is successfully execve()'d. The
4716 * separate release agent task is forked by call_usermodehelper(),
4717 * then control in this thread returns here, without waiting for the
4718 * release agent task. We don't bother to wait because the caller of
4719 * this routine has no use for the exit status of the release agent
4720 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004721 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004722static void cgroup_release_agent(struct work_struct *work)
4723{
4724 BUG_ON(work != &release_agent_work);
4725 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004726 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004727 while (!list_empty(&release_list)) {
4728 char *argv[3], *envp[3];
4729 int i;
Paul Menagee788e062008-07-25 01:46:59 -07004730 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004731 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004732 struct cgroup,
4733 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004734 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004735 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004736 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004737 if (!pathbuf)
4738 goto continue_free;
4739 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
4740 goto continue_free;
4741 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4742 if (!agentbuf)
4743 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004744
4745 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004746 argv[i++] = agentbuf;
4747 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004748 argv[i] = NULL;
4749
4750 i = 0;
4751 /* minimal command environment */
4752 envp[i++] = "HOME=/";
4753 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4754 envp[i] = NULL;
4755
4756 /* Drop the lock while we invoke the usermode helper,
4757 * since the exec could involve hitting disk and hence
4758 * be a slow process */
4759 mutex_unlock(&cgroup_mutex);
4760 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004761 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004762 continue_free:
4763 kfree(pathbuf);
4764 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004765 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004766 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004767 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004768 mutex_unlock(&cgroup_mutex);
4769}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004770
4771static int __init cgroup_disable(char *str)
4772{
4773 int i;
4774 char *token;
4775
4776 while ((token = strsep(&str, ",")) != NULL) {
4777 if (!*token)
4778 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08004779 /*
4780 * cgroup_disable, being at boot time, can't know about module
4781 * subsystems, so we don't worry about them.
4782 */
4783 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004784 struct cgroup_subsys *ss = subsys[i];
4785
4786 if (!strcmp(token, ss->name)) {
4787 ss->disabled = 1;
4788 printk(KERN_INFO "Disabling %s control group"
4789 " subsystem\n", ss->name);
4790 break;
4791 }
4792 }
4793 }
4794 return 1;
4795}
4796__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004797
4798/*
4799 * Functons for CSS ID.
4800 */
4801
4802/*
4803 *To get ID other than 0, this should be called when !cgroup_is_removed().
4804 */
4805unsigned short css_id(struct cgroup_subsys_state *css)
4806{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004807 struct css_id *cssid;
4808
4809 /*
4810 * This css_id() can return correct value when somone has refcnt
4811 * on this or this is under rcu_read_lock(). Once css->id is allocated,
4812 * it's unchanged until freed.
4813 */
Michal Hockod8bf4ca2011-07-08 14:39:41 +02004814 cssid = rcu_dereference_check(css->id, atomic_read(&css->refcnt));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004815
4816 if (cssid)
4817 return cssid->id;
4818 return 0;
4819}
Ben Blum67523c42010-03-10 15:22:11 -08004820EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004821
4822unsigned short css_depth(struct cgroup_subsys_state *css)
4823{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004824 struct css_id *cssid;
4825
Michal Hockod8bf4ca2011-07-08 14:39:41 +02004826 cssid = rcu_dereference_check(css->id, atomic_read(&css->refcnt));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004827
4828 if (cssid)
4829 return cssid->depth;
4830 return 0;
4831}
Ben Blum67523c42010-03-10 15:22:11 -08004832EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004833
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07004834/**
4835 * css_is_ancestor - test "root" css is an ancestor of "child"
4836 * @child: the css to be tested.
4837 * @root: the css supporsed to be an ancestor of the child.
4838 *
4839 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
4840 * this function reads css->id, this use rcu_dereference() and rcu_read_lock().
4841 * But, considering usual usage, the csses should be valid objects after test.
4842 * Assuming that the caller will do some action to the child if this returns
4843 * returns true, the caller must take "child";s reference count.
4844 * If "child" is valid object and this returns true, "root" is valid, too.
4845 */
4846
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004847bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07004848 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004849{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07004850 struct css_id *child_id;
4851 struct css_id *root_id;
4852 bool ret = true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004853
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07004854 rcu_read_lock();
4855 child_id = rcu_dereference(child->id);
4856 root_id = rcu_dereference(root->id);
4857 if (!child_id
4858 || !root_id
4859 || (child_id->depth < root_id->depth)
4860 || (child_id->stack[root_id->depth] != root_id->id))
4861 ret = false;
4862 rcu_read_unlock();
4863 return ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004864}
4865
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004866void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
4867{
4868 struct css_id *id = css->id;
4869 /* When this is called before css_id initialization, id can be NULL */
4870 if (!id)
4871 return;
4872
4873 BUG_ON(!ss->use_id);
4874
4875 rcu_assign_pointer(id->css, NULL);
4876 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07004877 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004878 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07004879 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08004880 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004881}
Ben Blum67523c42010-03-10 15:22:11 -08004882EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004883
4884/*
4885 * This is called by init or create(). Then, calls to this function are
4886 * always serialized (By cgroup_mutex() at create()).
4887 */
4888
4889static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
4890{
4891 struct css_id *newid;
4892 int myid, error, size;
4893
4894 BUG_ON(!ss->use_id);
4895
4896 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
4897 newid = kzalloc(size, GFP_KERNEL);
4898 if (!newid)
4899 return ERR_PTR(-ENOMEM);
4900 /* get id */
4901 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
4902 error = -ENOMEM;
4903 goto err_out;
4904 }
Hugh Dickins42aee6c2012-03-21 16:34:21 -07004905 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004906 /* Don't use 0. allocates an ID of 1-65535 */
4907 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07004908 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004909
4910 /* Returns error when there are no free spaces for new ID.*/
4911 if (error) {
4912 error = -ENOSPC;
4913 goto err_out;
4914 }
4915 if (myid > CSS_ID_MAX)
4916 goto remove_idr;
4917
4918 newid->id = myid;
4919 newid->depth = depth;
4920 return newid;
4921remove_idr:
4922 error = -ENOSPC;
Hugh Dickins42aee6c2012-03-21 16:34:21 -07004923 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004924 idr_remove(&ss->idr, myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07004925 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004926err_out:
4927 kfree(newid);
4928 return ERR_PTR(error);
4929
4930}
4931
Ben Blume6a11052010-03-10 15:22:09 -08004932static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
4933 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004934{
4935 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004936
Hugh Dickins42aee6c2012-03-21 16:34:21 -07004937 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004938 idr_init(&ss->idr);
4939
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004940 newid = get_new_cssid(ss, 0);
4941 if (IS_ERR(newid))
4942 return PTR_ERR(newid);
4943
4944 newid->stack[0] = newid->id;
4945 newid->css = rootcss;
4946 rootcss->id = newid;
4947 return 0;
4948}
4949
4950static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
4951 struct cgroup *child)
4952{
4953 int subsys_id, i, depth = 0;
4954 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08004955 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004956
4957 subsys_id = ss->subsys_id;
4958 parent_css = parent->subsys[subsys_id];
4959 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004960 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07004961 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004962
4963 child_id = get_new_cssid(ss, depth);
4964 if (IS_ERR(child_id))
4965 return PTR_ERR(child_id);
4966
4967 for (i = 0; i < depth; i++)
4968 child_id->stack[i] = parent_id->stack[i];
4969 child_id->stack[depth] = child_id->id;
4970 /*
4971 * child_id->css pointer will be set after this cgroup is available
4972 * see cgroup_populate_dir()
4973 */
4974 rcu_assign_pointer(child_css->id, child_id);
4975
4976 return 0;
4977}
4978
4979/**
4980 * css_lookup - lookup css by id
4981 * @ss: cgroup subsys to be looked into.
4982 * @id: the id
4983 *
4984 * Returns pointer to cgroup_subsys_state if there is valid one with id.
4985 * NULL if not. Should be called under rcu_read_lock()
4986 */
4987struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
4988{
4989 struct css_id *cssid = NULL;
4990
4991 BUG_ON(!ss->use_id);
4992 cssid = idr_find(&ss->idr, id);
4993
4994 if (unlikely(!cssid))
4995 return NULL;
4996
4997 return rcu_dereference(cssid->css);
4998}
Ben Blum67523c42010-03-10 15:22:11 -08004999EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005000
5001/**
5002 * css_get_next - lookup next cgroup under specified hierarchy.
5003 * @ss: pointer to subsystem
5004 * @id: current position of iteration.
5005 * @root: pointer to css. search tree under this.
5006 * @foundid: position of found object.
5007 *
5008 * Search next css under the specified hierarchy of rootid. Calling under
5009 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5010 */
5011struct cgroup_subsys_state *
5012css_get_next(struct cgroup_subsys *ss, int id,
5013 struct cgroup_subsys_state *root, int *foundid)
5014{
5015 struct cgroup_subsys_state *ret = NULL;
5016 struct css_id *tmp;
5017 int tmpid;
5018 int rootid = css_id(root);
5019 int depth = css_depth(root);
5020
5021 if (!rootid)
5022 return NULL;
5023
5024 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005025 WARN_ON_ONCE(!rcu_read_lock_held());
5026
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005027 /* fill start point for scan */
5028 tmpid = id;
5029 while (1) {
5030 /*
5031 * scan next entry from bitmap(tree), tmpid is updated after
5032 * idr_get_next().
5033 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005034 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005035 if (!tmp)
5036 break;
5037 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5038 ret = rcu_dereference(tmp->css);
5039 if (ret) {
5040 *foundid = tmpid;
5041 break;
5042 }
5043 }
5044 /* continue to scan from next id */
5045 tmpid = tmpid + 1;
5046 }
5047 return ret;
5048}
5049
Stephane Eraniane5d13672011-02-14 11:20:01 +02005050/*
5051 * get corresponding css from file open on cgroupfs directory
5052 */
5053struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5054{
5055 struct cgroup *cgrp;
5056 struct inode *inode;
5057 struct cgroup_subsys_state *css;
5058
5059 inode = f->f_dentry->d_inode;
5060 /* check in cgroup filesystem dir */
5061 if (inode->i_op != &cgroup_dir_inode_operations)
5062 return ERR_PTR(-EBADF);
5063
5064 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5065 return ERR_PTR(-EINVAL);
5066
5067 /* get cgroup */
5068 cgrp = __d_cgrp(f->f_dentry);
5069 css = cgrp->subsys[id];
5070 return css ? css : ERR_PTR(-ENOENT);
5071}
5072
Paul Menagefe693432009-09-23 15:56:20 -07005073#ifdef CONFIG_CGROUP_DEBUG
Li Zefan761b3ef2012-01-31 13:47:36 +08005074static struct cgroup_subsys_state *debug_create(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005075{
5076 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5077
5078 if (!css)
5079 return ERR_PTR(-ENOMEM);
5080
5081 return css;
5082}
5083
Li Zefan761b3ef2012-01-31 13:47:36 +08005084static void debug_destroy(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005085{
5086 kfree(cont->subsys[debug_subsys_id]);
5087}
5088
5089static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5090{
5091 return atomic_read(&cont->count);
5092}
5093
5094static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5095{
5096 return cgroup_task_count(cont);
5097}
5098
5099static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5100{
5101 return (u64)(unsigned long)current->cgroups;
5102}
5103
5104static u64 current_css_set_refcount_read(struct cgroup *cont,
5105 struct cftype *cft)
5106{
5107 u64 count;
5108
5109 rcu_read_lock();
5110 count = atomic_read(&current->cgroups->refcount);
5111 rcu_read_unlock();
5112 return count;
5113}
5114
Paul Menage7717f7b2009-09-23 15:56:22 -07005115static int current_css_set_cg_links_read(struct cgroup *cont,
5116 struct cftype *cft,
5117 struct seq_file *seq)
5118{
5119 struct cg_cgroup_link *link;
5120 struct css_set *cg;
5121
5122 read_lock(&css_set_lock);
5123 rcu_read_lock();
5124 cg = rcu_dereference(current->cgroups);
5125 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5126 struct cgroup *c = link->cgrp;
5127 const char *name;
5128
5129 if (c->dentry)
5130 name = c->dentry->d_name.name;
5131 else
5132 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005133 seq_printf(seq, "Root %d group %s\n",
5134 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005135 }
5136 rcu_read_unlock();
5137 read_unlock(&css_set_lock);
5138 return 0;
5139}
5140
5141#define MAX_TASKS_SHOWN_PER_CSS 25
5142static int cgroup_css_links_read(struct cgroup *cont,
5143 struct cftype *cft,
5144 struct seq_file *seq)
5145{
5146 struct cg_cgroup_link *link;
5147
5148 read_lock(&css_set_lock);
5149 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5150 struct css_set *cg = link->cg;
5151 struct task_struct *task;
5152 int count = 0;
5153 seq_printf(seq, "css_set %p\n", cg);
5154 list_for_each_entry(task, &cg->tasks, cg_list) {
5155 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5156 seq_puts(seq, " ...\n");
5157 break;
5158 } else {
5159 seq_printf(seq, " task %d\n",
5160 task_pid_vnr(task));
5161 }
5162 }
5163 }
5164 read_unlock(&css_set_lock);
5165 return 0;
5166}
5167
Paul Menagefe693432009-09-23 15:56:20 -07005168static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5169{
5170 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5171}
5172
5173static struct cftype debug_files[] = {
5174 {
5175 .name = "cgroup_refcount",
5176 .read_u64 = cgroup_refcount_read,
5177 },
5178 {
5179 .name = "taskcount",
5180 .read_u64 = debug_taskcount_read,
5181 },
5182
5183 {
5184 .name = "current_css_set",
5185 .read_u64 = current_css_set_read,
5186 },
5187
5188 {
5189 .name = "current_css_set_refcount",
5190 .read_u64 = current_css_set_refcount_read,
5191 },
5192
5193 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005194 .name = "current_css_set_cg_links",
5195 .read_seq_string = current_css_set_cg_links_read,
5196 },
5197
5198 {
5199 .name = "cgroup_css_links",
5200 .read_seq_string = cgroup_css_links_read,
5201 },
5202
5203 {
Paul Menagefe693432009-09-23 15:56:20 -07005204 .name = "releasable",
5205 .read_u64 = releasable_read,
5206 },
5207};
5208
5209static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
5210{
5211 return cgroup_add_files(cont, ss, debug_files,
5212 ARRAY_SIZE(debug_files));
5213}
5214
5215struct cgroup_subsys debug_subsys = {
5216 .name = "debug",
5217 .create = debug_create,
5218 .destroy = debug_destroy,
5219 .populate = debug_populate,
5220 .subsys_id = debug_subsys_id,
5221};
5222#endif /* CONFIG_CGROUP_DEBUG */