blob: 21bba77223509d9bd5242c19998eb50be21db3c5 [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 Sharma600634972011-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
Tejun Heob0ca5a82012-04-01 12:09:54 -0700130 /* All cgroups on this root, cgroup_mutex protected */
131 struct list_head allcg_list;
132
Paul Menageddbcc7e2007-10-18 23:39:30 -0700133 /* Hierarchy-specific flags */
134 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700135
Paul Menagee788e062008-07-25 01:46:59 -0700136 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700137 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700138
139 /* The name for this hierarchy - may be empty */
140 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700141};
142
Paul Menageddbcc7e2007-10-18 23:39:30 -0700143/*
144 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
145 * subsystems that are otherwise unattached - it never has more than a
146 * single cgroup, and all tasks are part of that cgroup.
147 */
148static struct cgroupfs_root rootnode;
149
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700150/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700151 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
152 */
153struct cfent {
154 struct list_head node;
155 struct dentry *dentry;
156 struct cftype *type;
157};
158
159/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700160 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
161 * cgroup_subsys->use_id != 0.
162 */
163#define CSS_ID_MAX (65535)
164struct css_id {
165 /*
166 * The css to which this ID points. This pointer is set to valid value
167 * after cgroup is populated. If cgroup is removed, this will be NULL.
168 * This pointer is expected to be RCU-safe because destroy()
169 * is called after synchronize_rcu(). But for safe use, css_is_removed()
170 * css_tryget() should be used for avoiding race.
171 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100172 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700173 /*
174 * ID of this css.
175 */
176 unsigned short id;
177 /*
178 * Depth in hierarchy which this ID belongs to.
179 */
180 unsigned short depth;
181 /*
182 * ID is freed by RCU. (and lookup routine is RCU safe.)
183 */
184 struct rcu_head rcu_head;
185 /*
186 * Hierarchy of CSS ID belongs to.
187 */
188 unsigned short stack[0]; /* Array of Length (depth+1) */
189};
190
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800191/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300192 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800193 */
194struct cgroup_event {
195 /*
196 * Cgroup which the event belongs to.
197 */
198 struct cgroup *cgrp;
199 /*
200 * Control file which the event associated.
201 */
202 struct cftype *cft;
203 /*
204 * eventfd to signal userspace about the event.
205 */
206 struct eventfd_ctx *eventfd;
207 /*
208 * Each of these stored in a list by the cgroup.
209 */
210 struct list_head list;
211 /*
212 * All fields below needed to unregister event when
213 * userspace closes eventfd.
214 */
215 poll_table pt;
216 wait_queue_head_t *wqh;
217 wait_queue_t wait;
218 struct work_struct remove;
219};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700220
Paul Menageddbcc7e2007-10-18 23:39:30 -0700221/* The list of hierarchy roots */
222
223static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700224static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700226static DEFINE_IDA(hierarchy_ida);
227static int next_hierarchy_id;
228static DEFINE_SPINLOCK(hierarchy_id_lock);
229
Paul Menageddbcc7e2007-10-18 23:39:30 -0700230/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
231#define dummytop (&rootnode.top_cgroup)
232
233/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800234 * check for fork/exit handlers to call. This avoids us having to do
235 * extra work in the fork/exit path if none of the subsystems need to
236 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700237 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700238static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700239
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800240#ifdef CONFIG_PROVE_LOCKING
241int cgroup_lock_is_held(void)
242{
243 return lockdep_is_held(&cgroup_mutex);
244}
245#else /* #ifdef CONFIG_PROVE_LOCKING */
246int cgroup_lock_is_held(void)
247{
248 return mutex_is_locked(&cgroup_mutex);
249}
250#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
251
252EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
253
Paul Menageddbcc7e2007-10-18 23:39:30 -0700254/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700255inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700256{
Paul Menagebd89aab2007-10-18 23:40:44 -0700257 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700258}
259
260/* bits in struct cgroupfs_root flags field */
261enum {
262 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
263};
264
Adrian Bunke9685a02008-02-07 00:13:46 -0800265static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700266{
267 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700268 (1 << CGRP_RELEASABLE) |
269 (1 << CGRP_NOTIFY_ON_RELEASE);
270 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700271}
272
Adrian Bunke9685a02008-02-07 00:13:46 -0800273static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700274{
Paul Menagebd89aab2007-10-18 23:40:44 -0700275 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700276}
277
Daniel Lezcano97978e62010-10-27 15:33:35 -0700278static int clone_children(const struct cgroup *cgrp)
279{
280 return test_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
281}
282
Paul Menageddbcc7e2007-10-18 23:39:30 -0700283/*
284 * for_each_subsys() allows you to iterate on each subsystem attached to
285 * an active hierarchy
286 */
287#define for_each_subsys(_root, _ss) \
288list_for_each_entry(_ss, &_root->subsys_list, sibling)
289
Li Zefane5f6a862009-01-07 18:07:41 -0800290/* for_each_active_root() allows you to iterate across the active hierarchies */
291#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700292list_for_each_entry(_root, &roots, root_list)
293
Tejun Heof6ea9372012-04-01 12:09:55 -0700294static inline struct cgroup *__d_cgrp(struct dentry *dentry)
295{
296 return dentry->d_fsdata;
297}
298
Tejun Heo05ef1d72012-04-01 12:09:56 -0700299static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700300{
301 return dentry->d_fsdata;
302}
303
Tejun Heo05ef1d72012-04-01 12:09:56 -0700304static inline struct cftype *__d_cft(struct dentry *dentry)
305{
306 return __d_cfe(dentry)->type;
307}
308
Paul Menage81a6a5c2007-10-18 23:39:38 -0700309/* the list of cgroups eligible for automatic release. Protected by
310 * release_list_lock */
311static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200312static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700313static void cgroup_release_agent(struct work_struct *work);
314static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700315static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700316
Paul Menage817929e2007-10-18 23:39:36 -0700317/* Link structure for associating css_set objects with cgroups */
318struct cg_cgroup_link {
319 /*
320 * List running through cg_cgroup_links associated with a
321 * cgroup, anchored on cgroup->css_sets
322 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700323 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700324 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700325 /*
326 * List running through cg_cgroup_links pointing at a
327 * single css_set object, anchored on css_set->cg_links
328 */
329 struct list_head cg_link_list;
330 struct css_set *cg;
331};
332
333/* The default css_set - used by init and its children prior to any
334 * hierarchies being mounted. It contains a pointer to the root state
335 * for each subsystem. Also used to anchor the list of css_sets. Not
336 * reference-counted, to improve performance when child cgroups
337 * haven't been created.
338 */
339
340static struct css_set init_css_set;
341static struct cg_cgroup_link init_css_set_link;
342
Ben Blume6a11052010-03-10 15:22:09 -0800343static int cgroup_init_idr(struct cgroup_subsys *ss,
344 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700345
Paul Menage817929e2007-10-18 23:39:36 -0700346/* css_set_lock protects the list of css_set objects, and the
347 * chain of tasks off each css_set. Nests outside task->alloc_lock
348 * due to cgroup_iter_start() */
349static DEFINE_RWLOCK(css_set_lock);
350static int css_set_count;
351
Paul Menage7717f7b2009-09-23 15:56:22 -0700352/*
353 * hash table for cgroup groups. This improves the performance to find
354 * an existing css_set. This hash doesn't (currently) take into
355 * account cgroups in empty hierarchies.
356 */
Li Zefan472b1052008-04-29 01:00:11 -0700357#define CSS_SET_HASH_BITS 7
358#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
359static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
360
361static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
362{
363 int i;
364 int index;
365 unsigned long tmp = 0UL;
366
367 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
368 tmp += (unsigned long)css[i];
369 tmp = (tmp >> 16) ^ tmp;
370
371 index = hash_long(tmp, CSS_SET_HASH_BITS);
372
373 return &css_set_table[index];
374}
375
Paul Menage817929e2007-10-18 23:39:36 -0700376/* We don't maintain the lists running through each css_set to its
377 * task until after the first call to cgroup_iter_start(). This
378 * reduces the fork()/exit() overhead for people who have cgroups
379 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700380static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700381
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700382static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700383{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700384 struct cg_cgroup_link *link;
385 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700386 /*
387 * Ensure that the refcount doesn't hit zero while any readers
388 * can see it. Similar to atomic_dec_and_lock(), but for an
389 * rwlock
390 */
391 if (atomic_add_unless(&cg->refcount, -1, 1))
392 return;
393 write_lock(&css_set_lock);
394 if (!atomic_dec_and_test(&cg->refcount)) {
395 write_unlock(&css_set_lock);
396 return;
397 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700398
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700399 /* This css_set is dead. unlink it and release cgroup refcounts */
400 hlist_del(&cg->hlist);
401 css_set_count--;
402
403 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
404 cg_link_list) {
405 struct cgroup *cgrp = link->cgrp;
406 list_del(&link->cg_link_list);
407 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700408 if (atomic_dec_and_test(&cgrp->count) &&
409 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700410 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700411 set_bit(CGRP_RELEASABLE, &cgrp->flags);
412 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700413 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700414
415 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700416 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700417
418 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800419 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700420}
421
422/*
423 * refcounted get/put for css_set objects
424 */
425static inline void get_css_set(struct css_set *cg)
426{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700427 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700428}
429
430static inline void put_css_set(struct css_set *cg)
431{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700432 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700433}
434
Paul Menage81a6a5c2007-10-18 23:39:38 -0700435static inline void put_css_set_taskexit(struct css_set *cg)
436{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700437 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700438}
439
Paul Menage817929e2007-10-18 23:39:36 -0700440/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700441 * compare_css_sets - helper function for find_existing_css_set().
442 * @cg: candidate css_set being tested
443 * @old_cg: existing css_set for a task
444 * @new_cgrp: cgroup that's being entered by the task
445 * @template: desired set of css pointers in css_set (pre-calculated)
446 *
447 * Returns true if "cg" matches "old_cg" except for the hierarchy
448 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
449 */
450static bool compare_css_sets(struct css_set *cg,
451 struct css_set *old_cg,
452 struct cgroup *new_cgrp,
453 struct cgroup_subsys_state *template[])
454{
455 struct list_head *l1, *l2;
456
457 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
458 /* Not all subsystems matched */
459 return false;
460 }
461
462 /*
463 * Compare cgroup pointers in order to distinguish between
464 * different cgroups in heirarchies with no subsystems. We
465 * could get by with just this check alone (and skip the
466 * memcmp above) but on most setups the memcmp check will
467 * avoid the need for this more expensive check on almost all
468 * candidates.
469 */
470
471 l1 = &cg->cg_links;
472 l2 = &old_cg->cg_links;
473 while (1) {
474 struct cg_cgroup_link *cgl1, *cgl2;
475 struct cgroup *cg1, *cg2;
476
477 l1 = l1->next;
478 l2 = l2->next;
479 /* See if we reached the end - both lists are equal length. */
480 if (l1 == &cg->cg_links) {
481 BUG_ON(l2 != &old_cg->cg_links);
482 break;
483 } else {
484 BUG_ON(l2 == &old_cg->cg_links);
485 }
486 /* Locate the cgroups associated with these links. */
487 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
488 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
489 cg1 = cgl1->cgrp;
490 cg2 = cgl2->cgrp;
491 /* Hierarchies should be linked in the same order. */
492 BUG_ON(cg1->root != cg2->root);
493
494 /*
495 * If this hierarchy is the hierarchy of the cgroup
496 * that's changing, then we need to check that this
497 * css_set points to the new cgroup; if it's any other
498 * hierarchy, then this css_set should point to the
499 * same cgroup as the old css_set.
500 */
501 if (cg1->root == new_cgrp->root) {
502 if (cg1 != new_cgrp)
503 return false;
504 } else {
505 if (cg1 != cg2)
506 return false;
507 }
508 }
509 return true;
510}
511
512/*
Paul Menage817929e2007-10-18 23:39:36 -0700513 * find_existing_css_set() is a helper for
514 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700515 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700516 *
517 * oldcg: the cgroup group that we're using before the cgroup
518 * transition
519 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700520 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700521 *
522 * template: location in which to build the desired set of subsystem
523 * state objects for the new cgroup group
524 */
Paul Menage817929e2007-10-18 23:39:36 -0700525static struct css_set *find_existing_css_set(
526 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700527 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700528 struct cgroup_subsys_state *template[])
529{
530 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700531 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700532 struct hlist_head *hhead;
533 struct hlist_node *node;
534 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700535
Ben Blumaae8aab2010-03-10 15:22:07 -0800536 /*
537 * Build the set of subsystem state objects that we want to see in the
538 * new css_set. while subsystems can change globally, the entries here
539 * won't change, so no need for locking.
540 */
Paul Menage817929e2007-10-18 23:39:36 -0700541 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800542 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700543 /* Subsystem is in this hierarchy. So we want
544 * the subsystem state from the new
545 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700546 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700547 } else {
548 /* Subsystem is not in this hierarchy, so we
549 * don't want to change the subsystem state */
550 template[i] = oldcg->subsys[i];
551 }
552 }
553
Li Zefan472b1052008-04-29 01:00:11 -0700554 hhead = css_set_hash(template);
555 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700556 if (!compare_css_sets(cg, oldcg, cgrp, template))
557 continue;
558
559 /* This css_set matches what we need */
560 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700561 }
Paul Menage817929e2007-10-18 23:39:36 -0700562
563 /* No existing cgroup group matched */
564 return NULL;
565}
566
Paul Menage817929e2007-10-18 23:39:36 -0700567static void free_cg_links(struct list_head *tmp)
568{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700569 struct cg_cgroup_link *link;
570 struct cg_cgroup_link *saved_link;
571
572 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700573 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700574 kfree(link);
575 }
576}
577
578/*
Li Zefan36553432008-07-29 22:33:19 -0700579 * allocate_cg_links() allocates "count" cg_cgroup_link structures
580 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
581 * success or a negative error
582 */
583static int allocate_cg_links(int count, struct list_head *tmp)
584{
585 struct cg_cgroup_link *link;
586 int i;
587 INIT_LIST_HEAD(tmp);
588 for (i = 0; i < count; i++) {
589 link = kmalloc(sizeof(*link), GFP_KERNEL);
590 if (!link) {
591 free_cg_links(tmp);
592 return -ENOMEM;
593 }
594 list_add(&link->cgrp_link_list, tmp);
595 }
596 return 0;
597}
598
Li Zefanc12f65d2009-01-07 18:07:42 -0800599/**
600 * link_css_set - a helper function to link a css_set to a cgroup
601 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
602 * @cg: the css_set to be linked
603 * @cgrp: the destination cgroup
604 */
605static void link_css_set(struct list_head *tmp_cg_links,
606 struct css_set *cg, struct cgroup *cgrp)
607{
608 struct cg_cgroup_link *link;
609
610 BUG_ON(list_empty(tmp_cg_links));
611 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
612 cgrp_link_list);
613 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700614 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700615 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800616 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700617 /*
618 * Always add links to the tail of the list so that the list
619 * is sorted by order of hierarchy creation
620 */
621 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800622}
623
Li Zefan36553432008-07-29 22:33:19 -0700624/*
Paul Menage817929e2007-10-18 23:39:36 -0700625 * find_css_set() takes an existing cgroup group and a
626 * cgroup object, and returns a css_set object that's
627 * equivalent to the old group, but with the given cgroup
628 * substituted into the appropriate hierarchy. Must be called with
629 * cgroup_mutex held
630 */
Paul Menage817929e2007-10-18 23:39:36 -0700631static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700632 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700633{
634 struct css_set *res;
635 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700636
637 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700638
Li Zefan472b1052008-04-29 01:00:11 -0700639 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700640 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700641
Paul Menage817929e2007-10-18 23:39:36 -0700642 /* First see if we already have a cgroup group that matches
643 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700644 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700645 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700646 if (res)
647 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700648 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700649
650 if (res)
651 return res;
652
653 res = kmalloc(sizeof(*res), GFP_KERNEL);
654 if (!res)
655 return NULL;
656
657 /* Allocate all the cg_cgroup_link objects that we'll need */
658 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
659 kfree(res);
660 return NULL;
661 }
662
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700663 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700664 INIT_LIST_HEAD(&res->cg_links);
665 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700666 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700667
668 /* Copy the set of subsystem state objects generated in
669 * find_existing_css_set() */
670 memcpy(res->subsys, template, sizeof(res->subsys));
671
672 write_lock(&css_set_lock);
673 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700674 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
675 struct cgroup *c = link->cgrp;
676 if (c->root == cgrp->root)
677 c = cgrp;
678 link_css_set(&tmp_cg_links, res, c);
679 }
Paul Menage817929e2007-10-18 23:39:36 -0700680
681 BUG_ON(!list_empty(&tmp_cg_links));
682
Paul Menage817929e2007-10-18 23:39:36 -0700683 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700684
685 /* Add this cgroup group to the hash table */
686 hhead = css_set_hash(res->subsys);
687 hlist_add_head(&res->hlist, hhead);
688
Paul Menage817929e2007-10-18 23:39:36 -0700689 write_unlock(&css_set_lock);
690
691 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700692}
693
Paul Menageddbcc7e2007-10-18 23:39:30 -0700694/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700695 * Return the cgroup for "task" from the given hierarchy. Must be
696 * called with cgroup_mutex held.
697 */
698static struct cgroup *task_cgroup_from_root(struct task_struct *task,
699 struct cgroupfs_root *root)
700{
701 struct css_set *css;
702 struct cgroup *res = NULL;
703
704 BUG_ON(!mutex_is_locked(&cgroup_mutex));
705 read_lock(&css_set_lock);
706 /*
707 * No need to lock the task - since we hold cgroup_mutex the
708 * task can't change groups, so the only thing that can happen
709 * is that it exits and its css is set back to init_css_set.
710 */
711 css = task->cgroups;
712 if (css == &init_css_set) {
713 res = &root->top_cgroup;
714 } else {
715 struct cg_cgroup_link *link;
716 list_for_each_entry(link, &css->cg_links, cg_link_list) {
717 struct cgroup *c = link->cgrp;
718 if (c->root == root) {
719 res = c;
720 break;
721 }
722 }
723 }
724 read_unlock(&css_set_lock);
725 BUG_ON(!res);
726 return res;
727}
728
729/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700730 * There is one global cgroup mutex. We also require taking
731 * task_lock() when dereferencing a task's cgroup subsys pointers.
732 * See "The task_lock() exception", at the end of this comment.
733 *
734 * A task must hold cgroup_mutex to modify cgroups.
735 *
736 * Any task can increment and decrement the count field without lock.
737 * So in general, code holding cgroup_mutex can't rely on the count
738 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800739 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700740 * means that no tasks are currently attached, therefore there is no
741 * way a task attached to that cgroup can fork (the other way to
742 * increment the count). So code holding cgroup_mutex can safely
743 * assume that if the count is zero, it will stay zero. Similarly, if
744 * a task holds cgroup_mutex on a cgroup with zero count, it
745 * knows that the cgroup won't be removed, as cgroup_rmdir()
746 * needs that mutex.
747 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700748 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
749 * (usually) take cgroup_mutex. These are the two most performance
750 * critical pieces of code here. The exception occurs on cgroup_exit(),
751 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
752 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800753 * to the release agent with the name of the cgroup (path relative to
754 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700755 *
756 * A cgroup can only be deleted if both its 'count' of using tasks
757 * is zero, and its list of 'children' cgroups is empty. Since all
758 * tasks in the system use _some_ cgroup, and since there is always at
759 * least one task in the system (init, pid == 1), therefore, top_cgroup
760 * always has either children cgroups and/or using tasks. So we don't
761 * need a special hack to ensure that top_cgroup cannot be deleted.
762 *
763 * The task_lock() exception
764 *
765 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800766 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800767 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700768 * several performance critical places that need to reference
769 * task->cgroup without the expense of grabbing a system global
770 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800771 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700772 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
773 * the task_struct routinely used for such matters.
774 *
775 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800776 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700777 */
778
Paul Menageddbcc7e2007-10-18 23:39:30 -0700779/**
780 * cgroup_lock - lock out any changes to cgroup structures
781 *
782 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700783void cgroup_lock(void)
784{
785 mutex_lock(&cgroup_mutex);
786}
Ben Blum67523c42010-03-10 15:22:11 -0800787EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788
789/**
790 * cgroup_unlock - release lock on cgroup changes
791 *
792 * Undo the lock taken in a previous cgroup_lock() call.
793 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700794void cgroup_unlock(void)
795{
796 mutex_unlock(&cgroup_mutex);
797}
Ben Blum67523c42010-03-10 15:22:11 -0800798EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799
800/*
801 * A couple of forward declarations required, due to cyclic reference loop:
802 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
803 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
804 * -> cgroup_mkdir.
805 */
806
Al Viro18bb1db2011-07-26 01:41:39 -0400807static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viroc72a04e2011-01-14 05:31:45 +0000808static struct dentry *cgroup_lookup(struct inode *, struct dentry *, struct nameidata *);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700810static int cgroup_populate_dir(struct cgroup *cgrp);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700811static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700812static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700813
814static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200815 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700816 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700817};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700818
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700819static int alloc_css_id(struct cgroup_subsys *ss,
820 struct cgroup *parent, struct cgroup *child);
821
Al Viroa5e7ed32011-07-26 01:55:55 -0400822static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700823{
824 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700825
826 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400827 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700828 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100829 inode->i_uid = current_fsuid();
830 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700831 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
832 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
833 }
834 return inode;
835}
836
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800837/*
838 * Call subsys's pre_destroy handler.
839 * This is called before css refcnt check.
840 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700841static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800842{
843 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700844 int ret = 0;
845
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800846 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700847 if (ss->pre_destroy) {
Li Zefan761b3ef2012-01-31 13:47:36 +0800848 ret = ss->pre_destroy(cgrp);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700849 if (ret)
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -0800850 break;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700851 }
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800852
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700853 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800854}
855
Paul Menageddbcc7e2007-10-18 23:39:30 -0700856static void cgroup_diput(struct dentry *dentry, struct inode *inode)
857{
858 /* is dentry a directory ? if so, kfree() associated cgroup */
859 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700860 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800861 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700862 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700863 /* It's possible for external users to be holding css
864 * reference counts on a cgroup; css_put() needs to
865 * be able to access the cgroup after decrementing
866 * the reference count in order to know if it needs to
867 * queue the cgroup to be handled by the release
868 * agent */
869 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800870
871 mutex_lock(&cgroup_mutex);
872 /*
873 * Release the subsystem state objects.
874 */
Li Zefan75139b82009-01-07 18:07:33 -0800875 for_each_subsys(cgrp->root, ss)
Li Zefan761b3ef2012-01-31 13:47:36 +0800876 ss->destroy(cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800877
878 cgrp->root->number_of_cgroups--;
879 mutex_unlock(&cgroup_mutex);
880
Paul Menagea47295e2009-01-07 18:07:44 -0800881 /*
882 * Drop the active superblock reference that we took when we
883 * created the cgroup
884 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800885 deactivate_super(cgrp->root->sb);
886
Ben Blum72a8cb32009-09-23 15:56:27 -0700887 /*
888 * if we're getting rid of the cgroup, refcount should ensure
889 * that there are no pidlists left.
890 */
891 BUG_ON(!list_empty(&cgrp->pidlists));
892
Lai Jiangshanf2da1c42011-03-15 17:55:16 +0800893 kfree_rcu(cgrp, rcu_head);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700894 } else {
895 struct cfent *cfe = __d_cfe(dentry);
896 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
897
898 WARN_ONCE(!list_empty(&cfe->node) &&
899 cgrp != &cgrp->root->top_cgroup,
900 "cfe still linked for %s\n", cfe->type->name);
901 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700902 }
903 iput(inode);
904}
905
Al Viroc72a04e2011-01-14 05:31:45 +0000906static int cgroup_delete(const struct dentry *d)
907{
908 return 1;
909}
910
Paul Menageddbcc7e2007-10-18 23:39:30 -0700911static void remove_dir(struct dentry *d)
912{
913 struct dentry *parent = dget(d->d_parent);
914
915 d_delete(d);
916 simple_rmdir(parent->d_inode, d);
917 dput(parent);
918}
919
Tejun Heo05ef1d72012-04-01 12:09:56 -0700920static int cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700921{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700922 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700923
Tejun Heo05ef1d72012-04-01 12:09:56 -0700924 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
925 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100926
Tejun Heo05ef1d72012-04-01 12:09:56 -0700927 list_for_each_entry(cfe, &cgrp->files, node) {
928 struct dentry *d = cfe->dentry;
929
930 if (cft && cfe->type != cft)
931 continue;
932
933 dget(d);
934 d_delete(d);
935 simple_unlink(d->d_inode, d);
936 list_del_init(&cfe->node);
937 dput(d);
938
939 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700940 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700941 return -ENOENT;
942}
943
944static void cgroup_clear_directory(struct dentry *dir)
945{
946 struct cgroup *cgrp = __d_cgrp(dir);
947
948 while (!list_empty(&cgrp->files))
949 cgroup_rm_file(cgrp, NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700950}
951
952/*
953 * NOTE : the dentry must have been dget()'ed
954 */
955static void cgroup_d_remove_dir(struct dentry *dentry)
956{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100957 struct dentry *parent;
958
Paul Menageddbcc7e2007-10-18 23:39:30 -0700959 cgroup_clear_directory(dentry);
960
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100961 parent = dentry->d_parent;
962 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800963 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700964 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100965 spin_unlock(&dentry->d_lock);
966 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700967 remove_dir(dentry);
968}
969
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700970/*
971 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
972 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
973 * reference to css->refcnt. In general, this refcnt is expected to goes down
974 * to zero, soon.
975 *
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700976 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700977 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +0200978static DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700979
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700980static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700981{
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700982 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700983 wake_up_all(&cgroup_rmdir_waitq);
984}
985
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700986void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
987{
988 css_get(css);
989}
990
991void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
992{
993 cgroup_wakeup_rmdir_waiter(css->cgroup);
994 css_put(css);
995}
996
Ben Blumaae8aab2010-03-10 15:22:07 -0800997/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800998 * Call with cgroup_mutex held. Drops reference counts on modules, including
999 * any duplicate ones that parse_cgroupfs_options took. If this function
1000 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001001 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002static int rebind_subsystems(struct cgroupfs_root *root,
1003 unsigned long final_bits)
1004{
1005 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -07001006 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007 int i;
1008
Ben Blumaae8aab2010-03-10 15:22:07 -08001009 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001010 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001011
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 removed_bits = root->actual_subsys_bits & ~final_bits;
1013 added_bits = final_bits & ~root->actual_subsys_bits;
1014 /* Check that any added subsystems are currently free */
1015 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001016 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 struct cgroup_subsys *ss = subsys[i];
1018 if (!(bit & added_bits))
1019 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001020 /*
1021 * Nobody should tell us to do a subsys that doesn't exist:
1022 * parse_cgroupfs_options should catch that case and refcounts
1023 * ensure that subsystems won't disappear once selected.
1024 */
1025 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026 if (ss->root != &rootnode) {
1027 /* Subsystem isn't free */
1028 return -EBUSY;
1029 }
1030 }
1031
1032 /* Currently we don't handle adding/removing subsystems when
1033 * any child cgroups exist. This is theoretically supportable
1034 * but involves complex error handling, so it's being left until
1035 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001036 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001037 return -EBUSY;
1038
1039 /* Process each subsystem */
1040 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1041 struct cgroup_subsys *ss = subsys[i];
1042 unsigned long bit = 1UL << i;
1043 if (bit & added_bits) {
1044 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001045 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001046 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047 BUG_ON(!dummytop->subsys[i]);
1048 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -08001049 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001050 cgrp->subsys[i] = dummytop->subsys[i];
1051 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001052 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001053 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001055 ss->bind(cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -08001056 mutex_unlock(&ss->hierarchy_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001057 /* refcount was already taken, and we're keeping it */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 } else if (bit & removed_bits) {
1059 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001060 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001061 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1062 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -08001063 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001064 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001065 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001067 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001068 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001069 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -08001070 mutex_unlock(&ss->hierarchy_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001071 /* subsystem is now free - drop reference on module */
1072 module_put(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073 } else if (bit & final_bits) {
1074 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001075 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001076 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001077 /*
1078 * a refcount was taken, but we already had one, so
1079 * drop the extra reference.
1080 */
1081 module_put(ss->module);
1082#ifdef CONFIG_MODULE_UNLOAD
1083 BUG_ON(ss->module && !module_refcount(ss->module));
1084#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085 } else {
1086 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001087 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 }
1089 }
1090 root->subsys_bits = root->actual_subsys_bits = final_bits;
1091 synchronize_rcu();
1092
1093 return 0;
1094}
1095
Al Viro34c80b12011-12-08 21:32:45 -05001096static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097{
Al Viro34c80b12011-12-08 21:32:45 -05001098 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099 struct cgroup_subsys *ss;
1100
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001101 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102 for_each_subsys(root, ss)
1103 seq_printf(seq, ",%s", ss->name);
1104 if (test_bit(ROOT_NOPREFIX, &root->flags))
1105 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001106 if (strlen(root->release_agent_path))
1107 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001108 if (clone_children(&root->top_cgroup))
1109 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001110 if (strlen(root->name))
1111 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001112 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001113 return 0;
1114}
1115
1116struct cgroup_sb_opts {
1117 unsigned long subsys_bits;
1118 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001119 char *release_agent;
Daniel Lezcano97978e62010-10-27 15:33:35 -07001120 bool clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001121 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001122 /* User explicitly requested empty subsystem */
1123 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001124
1125 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001126
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127};
1128
Ben Blumaae8aab2010-03-10 15:22:07 -08001129/*
1130 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001131 * with cgroup_mutex held to protect the subsys[] array. This function takes
1132 * refcounts on subsystems to be used, unless it returns error, in which case
1133 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001134 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001135static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001137 char *token, *o = data;
1138 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001139 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001140 int i;
1141 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001142
Ben Blumaae8aab2010-03-10 15:22:07 -08001143 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1144
Li Zefanf9ab5b52009-06-17 16:26:33 -07001145#ifdef CONFIG_CPUSETS
1146 mask = ~(1UL << cpuset_subsys_id);
1147#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001148
Paul Menagec6d57f32009-09-23 15:56:19 -07001149 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001150
1151 while ((token = strsep(&o, ",")) != NULL) {
1152 if (!*token)
1153 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001154 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001155 /* Explicitly have no subsystems */
1156 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001157 continue;
1158 }
1159 if (!strcmp(token, "all")) {
1160 /* Mutually exclusive option 'all' + subsystem name */
1161 if (one_ss)
1162 return -EINVAL;
1163 all_ss = true;
1164 continue;
1165 }
1166 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001167 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001168 continue;
1169 }
1170 if (!strcmp(token, "clone_children")) {
Daniel Lezcano97978e62010-10-27 15:33:35 -07001171 opts->clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001172 continue;
1173 }
1174 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001175 /* Specifying two release agents is forbidden */
1176 if (opts->release_agent)
1177 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001178 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001179 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001180 if (!opts->release_agent)
1181 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001182 continue;
1183 }
1184 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001185 const char *name = token + 5;
1186 /* Can't specify an empty name */
1187 if (!strlen(name))
1188 return -EINVAL;
1189 /* Must match [\w.-]+ */
1190 for (i = 0; i < strlen(name); i++) {
1191 char c = name[i];
1192 if (isalnum(c))
1193 continue;
1194 if ((c == '.') || (c == '-') || (c == '_'))
1195 continue;
1196 return -EINVAL;
1197 }
1198 /* Specifying two names is forbidden */
1199 if (opts->name)
1200 return -EINVAL;
1201 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001202 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001203 GFP_KERNEL);
1204 if (!opts->name)
1205 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001206
1207 continue;
1208 }
1209
1210 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1211 struct cgroup_subsys *ss = subsys[i];
1212 if (ss == NULL)
1213 continue;
1214 if (strcmp(token, ss->name))
1215 continue;
1216 if (ss->disabled)
1217 continue;
1218
1219 /* Mutually exclusive option 'all' + subsystem name */
1220 if (all_ss)
1221 return -EINVAL;
1222 set_bit(i, &opts->subsys_bits);
1223 one_ss = true;
1224
1225 break;
1226 }
1227 if (i == CGROUP_SUBSYS_COUNT)
1228 return -ENOENT;
1229 }
1230
1231 /*
1232 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001233 * otherwise if 'none', 'name=' and a subsystem name options
1234 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001235 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001236 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001237 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1238 struct cgroup_subsys *ss = subsys[i];
1239 if (ss == NULL)
1240 continue;
1241 if (ss->disabled)
1242 continue;
1243 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001244 }
1245 }
1246
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001247 /* Consistency checks */
1248
Li Zefanf9ab5b52009-06-17 16:26:33 -07001249 /*
1250 * Option noprefix was introduced just for backward compatibility
1251 * with the old cpuset, so we allow noprefix only if mounting just
1252 * the cpuset subsystem.
1253 */
1254 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1255 (opts->subsys_bits & mask))
1256 return -EINVAL;
1257
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001258
1259 /* Can't specify "none" and some subsystems */
1260 if (opts->subsys_bits && opts->none)
1261 return -EINVAL;
1262
1263 /*
1264 * We either have to specify by name or by subsystems. (So all
1265 * empty hierarchies must have a name).
1266 */
Paul Menagec6d57f32009-09-23 15:56:19 -07001267 if (!opts->subsys_bits && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001268 return -EINVAL;
1269
Ben Blumcf5d5942010-03-10 15:22:09 -08001270 /*
1271 * Grab references on all the modules we'll need, so the subsystems
1272 * don't dance around before rebind_subsystems attaches them. This may
1273 * take duplicate reference counts on a subsystem that's already used,
1274 * but rebind_subsystems handles this case.
1275 */
1276 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1277 unsigned long bit = 1UL << i;
1278
1279 if (!(bit & opts->subsys_bits))
1280 continue;
1281 if (!try_module_get(subsys[i]->module)) {
1282 module_pin_failed = true;
1283 break;
1284 }
1285 }
1286 if (module_pin_failed) {
1287 /*
1288 * oops, one of the modules was going away. this means that we
1289 * raced with a module_delete call, and to the user this is
1290 * essentially a "subsystem doesn't exist" case.
1291 */
1292 for (i--; i >= CGROUP_BUILTIN_SUBSYS_COUNT; i--) {
1293 /* drop refcounts only on the ones we took */
1294 unsigned long bit = 1UL << i;
1295
1296 if (!(bit & opts->subsys_bits))
1297 continue;
1298 module_put(subsys[i]->module);
1299 }
1300 return -ENOENT;
1301 }
1302
Paul Menageddbcc7e2007-10-18 23:39:30 -07001303 return 0;
1304}
1305
Ben Blumcf5d5942010-03-10 15:22:09 -08001306static void drop_parsed_module_refcounts(unsigned long subsys_bits)
1307{
1308 int i;
1309 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1310 unsigned long bit = 1UL << i;
1311
1312 if (!(bit & subsys_bits))
1313 continue;
1314 module_put(subsys[i]->module);
1315 }
1316}
1317
Paul Menageddbcc7e2007-10-18 23:39:30 -07001318static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1319{
1320 int ret = 0;
1321 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001322 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323 struct cgroup_sb_opts opts;
1324
Paul Menagebd89aab2007-10-18 23:40:44 -07001325 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001327 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328
1329 /* See what subsystems are wanted */
1330 ret = parse_cgroupfs_options(data, &opts);
1331 if (ret)
1332 goto out_unlock;
1333
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001334 /* See feature-removal-schedule.txt */
1335 if (opts.subsys_bits != root->actual_subsys_bits || opts.release_agent)
1336 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1337 task_tgid_nr(current), current->comm);
1338
Ben Blumcf5d5942010-03-10 15:22:09 -08001339 /* Don't allow flags or name to change at remount */
1340 if (opts.flags != root->flags ||
1341 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342 ret = -EINVAL;
Ben Blumcf5d5942010-03-10 15:22:09 -08001343 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menagec6d57f32009-09-23 15:56:19 -07001344 goto out_unlock;
1345 }
1346
Paul Menageddbcc7e2007-10-18 23:39:30 -07001347 ret = rebind_subsystems(root, opts.subsys_bits);
Ben Blumcf5d5942010-03-10 15:22:09 -08001348 if (ret) {
1349 drop_parsed_module_refcounts(opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -07001350 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001351 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001352
Tejun Heoff4c8d52012-04-01 12:09:54 -07001353 /* clear out any existing files and repopulate subsystem files */
1354 cgroup_clear_directory(cgrp->dentry);
Li Zefan0670e082009-04-02 16:57:30 -07001355 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001356
Paul Menage81a6a5c2007-10-18 23:39:38 -07001357 if (opts.release_agent)
1358 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001359 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001360 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001361 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001362 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001363 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001364 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365 return ret;
1366}
1367
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001368static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001369 .statfs = simple_statfs,
1370 .drop_inode = generic_delete_inode,
1371 .show_options = cgroup_show_options,
1372 .remount_fs = cgroup_remount,
1373};
1374
Paul Menagecc31edc2008-10-18 20:28:04 -07001375static void init_cgroup_housekeeping(struct cgroup *cgrp)
1376{
1377 INIT_LIST_HEAD(&cgrp->sibling);
1378 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001379 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001380 INIT_LIST_HEAD(&cgrp->css_sets);
1381 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001382 INIT_LIST_HEAD(&cgrp->pidlists);
1383 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001384 INIT_LIST_HEAD(&cgrp->event_list);
1385 spin_lock_init(&cgrp->event_list_lock);
Paul Menagecc31edc2008-10-18 20:28:04 -07001386}
Paul Menagec6d57f32009-09-23 15:56:19 -07001387
Paul Menageddbcc7e2007-10-18 23:39:30 -07001388static void init_cgroup_root(struct cgroupfs_root *root)
1389{
Paul Menagebd89aab2007-10-18 23:40:44 -07001390 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001391
Paul Menageddbcc7e2007-10-18 23:39:30 -07001392 INIT_LIST_HEAD(&root->subsys_list);
1393 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001394 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001395 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001396 cgrp->root = root;
1397 cgrp->top_cgroup = cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001398 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menagecc31edc2008-10-18 20:28:04 -07001399 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001400}
1401
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001402static bool init_root_id(struct cgroupfs_root *root)
1403{
1404 int ret = 0;
1405
1406 do {
1407 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1408 return false;
1409 spin_lock(&hierarchy_id_lock);
1410 /* Try to allocate the next unused ID */
1411 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1412 &root->hierarchy_id);
1413 if (ret == -ENOSPC)
1414 /* Try again starting from 0 */
1415 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1416 if (!ret) {
1417 next_hierarchy_id = root->hierarchy_id + 1;
1418 } else if (ret != -EAGAIN) {
1419 /* Can only get here if the 31-bit IDR is full ... */
1420 BUG_ON(ret);
1421 }
1422 spin_unlock(&hierarchy_id_lock);
1423 } while (ret);
1424 return true;
1425}
1426
Paul Menageddbcc7e2007-10-18 23:39:30 -07001427static int cgroup_test_super(struct super_block *sb, void *data)
1428{
Paul Menagec6d57f32009-09-23 15:56:19 -07001429 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001430 struct cgroupfs_root *root = sb->s_fs_info;
1431
Paul Menagec6d57f32009-09-23 15:56:19 -07001432 /* If we asked for a name then it must match */
1433 if (opts->name && strcmp(opts->name, root->name))
1434 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001435
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001436 /*
1437 * If we asked for subsystems (or explicitly for no
1438 * subsystems) then they must match
1439 */
1440 if ((opts->subsys_bits || opts->none)
1441 && (opts->subsys_bits != root->subsys_bits))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442 return 0;
1443
1444 return 1;
1445}
1446
Paul Menagec6d57f32009-09-23 15:56:19 -07001447static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1448{
1449 struct cgroupfs_root *root;
1450
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001451 if (!opts->subsys_bits && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001452 return NULL;
1453
1454 root = kzalloc(sizeof(*root), GFP_KERNEL);
1455 if (!root)
1456 return ERR_PTR(-ENOMEM);
1457
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001458 if (!init_root_id(root)) {
1459 kfree(root);
1460 return ERR_PTR(-ENOMEM);
1461 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001462 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001463
Paul Menagec6d57f32009-09-23 15:56:19 -07001464 root->subsys_bits = opts->subsys_bits;
1465 root->flags = opts->flags;
1466 if (opts->release_agent)
1467 strcpy(root->release_agent_path, opts->release_agent);
1468 if (opts->name)
1469 strcpy(root->name, opts->name);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001470 if (opts->clone_children)
1471 set_bit(CGRP_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001472 return root;
1473}
1474
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001475static void cgroup_drop_root(struct cgroupfs_root *root)
1476{
1477 if (!root)
1478 return;
1479
1480 BUG_ON(!root->hierarchy_id);
1481 spin_lock(&hierarchy_id_lock);
1482 ida_remove(&hierarchy_ida, root->hierarchy_id);
1483 spin_unlock(&hierarchy_id_lock);
1484 kfree(root);
1485}
1486
Paul Menageddbcc7e2007-10-18 23:39:30 -07001487static int cgroup_set_super(struct super_block *sb, void *data)
1488{
1489 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001490 struct cgroup_sb_opts *opts = data;
1491
1492 /* If we don't have a new root, we can't set up a new sb */
1493 if (!opts->new_root)
1494 return -EINVAL;
1495
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001496 BUG_ON(!opts->subsys_bits && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001497
1498 ret = set_anon_super(sb, NULL);
1499 if (ret)
1500 return ret;
1501
Paul Menagec6d57f32009-09-23 15:56:19 -07001502 sb->s_fs_info = opts->new_root;
1503 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001504
1505 sb->s_blocksize = PAGE_CACHE_SIZE;
1506 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1507 sb->s_magic = CGROUP_SUPER_MAGIC;
1508 sb->s_op = &cgroup_ops;
1509
1510 return 0;
1511}
1512
1513static int cgroup_get_rootdir(struct super_block *sb)
1514{
Al Viro0df6a632010-12-21 13:29:29 -05001515 static const struct dentry_operations cgroup_dops = {
1516 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001517 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001518 };
1519
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520 struct inode *inode =
1521 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001522
1523 if (!inode)
1524 return -ENOMEM;
1525
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526 inode->i_fop = &simple_dir_operations;
1527 inode->i_op = &cgroup_dir_inode_operations;
1528 /* directories start off with i_nlink == 2 (for "." entry) */
1529 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001530 sb->s_root = d_make_root(inode);
1531 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001532 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001533 /* for everything else we want ->d_op set */
1534 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001535 return 0;
1536}
1537
Al Virof7e83572010-07-26 13:23:11 +04001538static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001540 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541{
1542 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001543 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001544 int ret = 0;
1545 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001546 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001547 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548
1549 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001550 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001552 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001553 if (ret)
1554 goto out_err;
1555
1556 /*
1557 * Allocate a new cgroup root. We may not need it if we're
1558 * reusing an existing hierarchy.
1559 */
1560 new_root = cgroup_root_from_opts(&opts);
1561 if (IS_ERR(new_root)) {
1562 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001563 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001564 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001565 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566
Paul Menagec6d57f32009-09-23 15:56:19 -07001567 /* Locate an existing or new sb for this hierarchy */
1568 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001570 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001571 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001572 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573 }
1574
Paul Menagec6d57f32009-09-23 15:56:19 -07001575 root = sb->s_fs_info;
1576 BUG_ON(!root);
1577 if (root == opts.new_root) {
1578 /* We used the new root structure, so this is a new hierarchy */
1579 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001580 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001581 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001582 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001583 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584
1585 BUG_ON(sb->s_root != NULL);
1586
1587 ret = cgroup_get_rootdir(sb);
1588 if (ret)
1589 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001590 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001591
Paul Menage817929e2007-10-18 23:39:36 -07001592 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001593 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001594 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001596 /* Check for name clashes with existing mounts */
1597 ret = -EBUSY;
1598 if (strlen(root->name))
1599 for_each_active_root(existing_root)
1600 if (!strcmp(existing_root->name, root->name))
1601 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001602
Paul Menage817929e2007-10-18 23:39:36 -07001603 /*
1604 * We're accessing css_set_count without locking
1605 * css_set_lock here, but that's OK - it can only be
1606 * increased by someone holding cgroup_lock, and
1607 * that's us. The worst that can happen is that we
1608 * have some link structures left over
1609 */
1610 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001611 if (ret)
1612 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001613
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614 ret = rebind_subsystems(root, root->subsys_bits);
1615 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001616 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001617 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001618 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001619 /*
1620 * There must be no failure case after here, since rebinding
1621 * takes care of subsystems' refcounts, which are explicitly
1622 * dropped in the failure exit path.
1623 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624
1625 /* EBUSY should be the only error here */
1626 BUG_ON(ret);
1627
1628 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001629 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630
Li Zefanc12f65d2009-01-07 18:07:42 -08001631 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632 root->top_cgroup.dentry = sb->s_root;
1633
Paul Menage817929e2007-10-18 23:39:36 -07001634 /* Link the top cgroup in this hierarchy into all
1635 * the css_set objects */
1636 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001637 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1638 struct hlist_head *hhead = &css_set_table[i];
1639 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001640 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001641
Li Zefanc12f65d2009-01-07 18:07:42 -08001642 hlist_for_each_entry(cg, node, hhead, hlist)
1643 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001644 }
Paul Menage817929e2007-10-18 23:39:36 -07001645 write_unlock(&css_set_lock);
1646
1647 free_cg_links(&tmp_cg_links);
1648
Li Zefanc12f65d2009-01-07 18:07:42 -08001649 BUG_ON(!list_empty(&root_cgrp->sibling));
1650 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001651 BUG_ON(root->number_of_cgroups != 1);
1652
eparis@redhat2ce97382011-06-02 21:20:51 +10001653 cred = override_creds(&init_cred);
Li Zefanc12f65d2009-01-07 18:07:42 -08001654 cgroup_populate_dir(root_cgrp);
eparis@redhat2ce97382011-06-02 21:20:51 +10001655 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001656 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001658 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001659 } else {
1660 /*
1661 * We re-used an existing hierarchy - the new root (if
1662 * any) is not needed
1663 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001664 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001665 /* no subsys rebinding, so refcounts don't change */
1666 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001667 }
1668
Paul Menagec6d57f32009-09-23 15:56:19 -07001669 kfree(opts.release_agent);
1670 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001671 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001673 unlock_drop:
1674 mutex_unlock(&cgroup_root_mutex);
1675 mutex_unlock(&cgroup_mutex);
1676 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001677 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001678 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001679 drop_modules:
1680 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menagec6d57f32009-09-23 15:56:19 -07001681 out_err:
1682 kfree(opts.release_agent);
1683 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001684 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001685}
1686
1687static void cgroup_kill_sb(struct super_block *sb) {
1688 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001689 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001690 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001691 struct cg_cgroup_link *link;
1692 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693
1694 BUG_ON(!root);
1695
1696 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001697 BUG_ON(!list_empty(&cgrp->children));
1698 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001699
1700 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001701 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001702
1703 /* Rebind all subsystems back to the default hierarchy */
1704 ret = rebind_subsystems(root, 0);
1705 /* Shouldn't be able to fail ... */
1706 BUG_ON(ret);
1707
Paul Menage817929e2007-10-18 23:39:36 -07001708 /*
1709 * Release all the links from css_sets to this hierarchy's
1710 * root cgroup
1711 */
1712 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001713
1714 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1715 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001716 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001717 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001718 kfree(link);
1719 }
1720 write_unlock(&css_set_lock);
1721
Paul Menage839ec542009-01-29 14:25:22 -08001722 if (!list_empty(&root->root_list)) {
1723 list_del(&root->root_list);
1724 root_count--;
1725 }
Li Zefane5f6a862009-01-07 18:07:41 -08001726
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001727 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001728 mutex_unlock(&cgroup_mutex);
1729
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001731 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732}
1733
1734static struct file_system_type cgroup_fs_type = {
1735 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001736 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001737 .kill_sb = cgroup_kill_sb,
1738};
1739
Greg KH676db4a2010-08-05 13:53:35 -07001740static struct kobject *cgroup_kobj;
1741
Li Zefana043e3b2008-02-23 15:24:09 -08001742/**
1743 * cgroup_path - generate the path of a cgroup
1744 * @cgrp: the cgroup in question
1745 * @buf: the buffer to write the path into
1746 * @buflen: the length of the buffer
1747 *
Paul Menagea47295e2009-01-07 18:07:44 -08001748 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1749 * reference. Writes path of cgroup into buf. Returns 0 on success,
1750 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001751 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001752int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753{
1754 char *start;
Li Zefan9a9686b2010-04-22 17:29:24 +08001755 struct dentry *dentry = rcu_dereference_check(cgrp->dentry,
Li Zefan9a9686b2010-04-22 17:29:24 +08001756 cgroup_lock_is_held());
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757
Paul Menagea47295e2009-01-07 18:07:44 -08001758 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759 /*
1760 * Inactive subsystems have no dentry for their root
1761 * cgroup
1762 */
1763 strcpy(buf, "/");
1764 return 0;
1765 }
1766
1767 start = buf + buflen;
1768
1769 *--start = '\0';
1770 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001771 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001772
Paul Menageddbcc7e2007-10-18 23:39:30 -07001773 if ((start -= len) < buf)
1774 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001775 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001776 cgrp = cgrp->parent;
1777 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001779
1780 dentry = rcu_dereference_check(cgrp->dentry,
Li Zefan9a9686b2010-04-22 17:29:24 +08001781 cgroup_lock_is_held());
Paul Menagebd89aab2007-10-18 23:40:44 -07001782 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001783 continue;
1784 if (--start < buf)
1785 return -ENAMETOOLONG;
1786 *start = '/';
1787 }
1788 memmove(buf, start, buf + buflen - start);
1789 return 0;
1790}
Ben Blum67523c42010-03-10 15:22:11 -08001791EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792
Ben Blum74a11662011-05-26 16:25:20 -07001793/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001794 * Control Group taskset
1795 */
Tejun Heo134d3372011-12-12 18:12:21 -08001796struct task_and_cgroup {
1797 struct task_struct *task;
1798 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001799 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001800};
1801
Tejun Heo2f7ee562011-12-12 18:12:21 -08001802struct cgroup_taskset {
1803 struct task_and_cgroup single;
1804 struct flex_array *tc_array;
1805 int tc_array_len;
1806 int idx;
1807 struct cgroup *cur_cgrp;
1808};
1809
1810/**
1811 * cgroup_taskset_first - reset taskset and return the first task
1812 * @tset: taskset of interest
1813 *
1814 * @tset iteration is initialized and the first task is returned.
1815 */
1816struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1817{
1818 if (tset->tc_array) {
1819 tset->idx = 0;
1820 return cgroup_taskset_next(tset);
1821 } else {
1822 tset->cur_cgrp = tset->single.cgrp;
1823 return tset->single.task;
1824 }
1825}
1826EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1827
1828/**
1829 * cgroup_taskset_next - iterate to the next task in taskset
1830 * @tset: taskset of interest
1831 *
1832 * Return the next task in @tset. Iteration must have been initialized
1833 * with cgroup_taskset_first().
1834 */
1835struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1836{
1837 struct task_and_cgroup *tc;
1838
1839 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1840 return NULL;
1841
1842 tc = flex_array_get(tset->tc_array, tset->idx++);
1843 tset->cur_cgrp = tc->cgrp;
1844 return tc->task;
1845}
1846EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1847
1848/**
1849 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1850 * @tset: taskset of interest
1851 *
1852 * Return the cgroup for the current (last returned) task of @tset. This
1853 * function must be preceded by either cgroup_taskset_first() or
1854 * cgroup_taskset_next().
1855 */
1856struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1857{
1858 return tset->cur_cgrp;
1859}
1860EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1861
1862/**
1863 * cgroup_taskset_size - return the number of tasks in taskset
1864 * @tset: taskset of interest
1865 */
1866int cgroup_taskset_size(struct cgroup_taskset *tset)
1867{
1868 return tset->tc_array ? tset->tc_array_len : 1;
1869}
1870EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1871
1872
Ben Blum74a11662011-05-26 16:25:20 -07001873/*
1874 * cgroup_task_migrate - move a task from one cgroup to another.
1875 *
1876 * 'guarantee' is set if the caller promises that a new css_set for the task
1877 * will already exist. If not set, this function might sleep, and can fail with
Tejun Heocd3d0952011-12-12 18:12:21 -08001878 * -ENOMEM. Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001879 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001880static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1881 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001882{
1883 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001884
1885 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001886 * We are synchronized through threadgroup_lock() against PF_EXITING
1887 * setting such that we can't race against cgroup_exit() changing the
1888 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001889 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001890 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001891 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001892
Ben Blum74a11662011-05-26 16:25:20 -07001893 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001894 rcu_assign_pointer(tsk->cgroups, newcg);
1895 task_unlock(tsk);
1896
1897 /* Update the css_set linked lists if we're using them */
1898 write_lock(&css_set_lock);
1899 if (!list_empty(&tsk->cg_list))
1900 list_move(&tsk->cg_list, &newcg->tasks);
1901 write_unlock(&css_set_lock);
1902
1903 /*
1904 * We just gained a reference on oldcg by taking it from the task. As
1905 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1906 * it here; it will be freed under RCU.
1907 */
1908 put_css_set(oldcg);
1909
1910 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Ben Blum74a11662011-05-26 16:25:20 -07001911}
1912
Li Zefana043e3b2008-02-23 15:24:09 -08001913/**
1914 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1915 * @cgrp: the cgroup the task is attaching to
1916 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001917 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001918 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1919 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001920 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001921int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001922{
Tejun Heo8f121912012-03-29 22:03:33 -07001923 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001924 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001925 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001926 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001927 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001928 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001929
Tejun Heocd3d0952011-12-12 18:12:21 -08001930 /* @tsk either already exited or can't exit until the end */
1931 if (tsk->flags & PF_EXITING)
1932 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001933
1934 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001935 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001936 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001937 return 0;
1938
Tejun Heo2f7ee562011-12-12 18:12:21 -08001939 tset.single.task = tsk;
1940 tset.single.cgrp = oldcgrp;
1941
Paul Menagebbcb81d2007-10-18 23:39:32 -07001942 for_each_subsys(root, ss) {
1943 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08001944 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001945 if (retval) {
1946 /*
1947 * Remember on which subsystem the can_attach()
1948 * failed, so that we only call cancel_attach()
1949 * against the subsystems whose can_attach()
1950 * succeeded. (See below)
1951 */
1952 failed_ss = ss;
1953 goto out;
1954 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001955 }
1956 }
1957
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001958 newcg = find_css_set(tsk->cgroups, cgrp);
1959 if (!newcg) {
1960 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001961 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001962 }
1963
1964 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001965
Paul Menagebbcb81d2007-10-18 23:39:32 -07001966 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001967 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001968 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001969 }
Ben Blum74a11662011-05-26 16:25:20 -07001970
Paul Menagebbcb81d2007-10-18 23:39:32 -07001971 synchronize_rcu();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001972
1973 /*
1974 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1975 * is no longer empty.
1976 */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001977 cgroup_wakeup_rmdir_waiter(cgrp);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001978out:
1979 if (retval) {
1980 for_each_subsys(root, ss) {
1981 if (ss == failed_ss)
1982 /*
1983 * This subsystem was the one that failed the
1984 * can_attach() check earlier, so we don't need
1985 * to call cancel_attach() against it or any
1986 * remaining subsystems.
1987 */
1988 break;
1989 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001990 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001991 }
1992 }
1993 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001994}
1995
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001996/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001997 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1998 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001999 * @tsk: the task to be attached
2000 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002001int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002002{
2003 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002004 int retval = 0;
2005
2006 cgroup_lock();
2007 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002008 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2009
2010 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002011 if (retval)
2012 break;
2013 }
2014 cgroup_unlock();
2015
2016 return retval;
2017}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002018EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002019
Ben Blum74a11662011-05-26 16:25:20 -07002020/**
2021 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2022 * @cgrp: the cgroup to attach to
2023 * @leader: the threadgroup leader task_struct of the group to be attached
2024 *
Tejun Heo257058a2011-12-12 18:12:21 -08002025 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2026 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002027 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002028static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002029{
2030 int retval, i, group_size;
2031 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002032 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002033 struct cgroupfs_root *root = cgrp->root;
2034 /* threadgroup list cursor and array */
2035 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002036 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002037 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002038 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002039
2040 /*
2041 * step 0: in order to do expensive, possibly blocking operations for
2042 * every thread, we cannot iterate the thread group list, since it needs
2043 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002044 * group - group_rwsem prevents new threads from appearing, and if
2045 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002046 */
2047 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002048 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002049 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002050 if (!group)
2051 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002052 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2053 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2054 if (retval)
2055 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002056
Ben Blum74a11662011-05-26 16:25:20 -07002057 tsk = leader;
2058 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002059 /*
2060 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2061 * already PF_EXITING could be freed from underneath us unless we
2062 * take an rcu_read_lock.
2063 */
2064 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002065 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002066 struct task_and_cgroup ent;
2067
Tejun Heocd3d0952011-12-12 18:12:21 -08002068 /* @tsk either already exited or can't exit until the end */
2069 if (tsk->flags & PF_EXITING)
2070 continue;
2071
Ben Blum74a11662011-05-26 16:25:20 -07002072 /* as per above, nr_threads may decrease, but not increase. */
2073 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002074 ent.task = tsk;
2075 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002076 /* nothing to do if this task is already in the cgroup */
2077 if (ent.cgrp == cgrp)
2078 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002079 /*
2080 * saying GFP_ATOMIC has no effect here because we did prealloc
2081 * earlier, but it's good form to communicate our expectations.
2082 */
Tejun Heo134d3372011-12-12 18:12:21 -08002083 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002084 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002085 i++;
2086 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002087 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002088 /* remember the number of threads in the array for later. */
2089 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002090 tset.tc_array = group;
2091 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002092
Tejun Heo134d3372011-12-12 18:12:21 -08002093 /* methods shouldn't be called if no task is actually migrating */
2094 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002095 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002096 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002097
Ben Blum74a11662011-05-26 16:25:20 -07002098 /*
2099 * step 1: check that we can legitimately attach to the cgroup.
2100 */
2101 for_each_subsys(root, ss) {
2102 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002103 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002104 if (retval) {
2105 failed_ss = ss;
2106 goto out_cancel_attach;
2107 }
2108 }
Ben Blum74a11662011-05-26 16:25:20 -07002109 }
2110
2111 /*
2112 * step 2: make sure css_sets exist for all threads to be migrated.
2113 * we use find_css_set, which allocates a new one if necessary.
2114 */
Ben Blum74a11662011-05-26 16:25:20 -07002115 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002116 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002117 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2118 if (!tc->cg) {
2119 retval = -ENOMEM;
2120 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002121 }
2122 }
2123
2124 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002125 * step 3: now that we're guaranteed success wrt the css_sets,
2126 * proceed to move all tasks to the new cgroup. There are no
2127 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002128 */
Ben Blum74a11662011-05-26 16:25:20 -07002129 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002130 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002131 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002132 }
2133 /* nothing is sensitive to fork() after this point. */
2134
2135 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002136 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002137 */
2138 for_each_subsys(root, ss) {
2139 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002140 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002141 }
2142
2143 /*
2144 * step 5: success! and cleanup
2145 */
2146 synchronize_rcu();
2147 cgroup_wakeup_rmdir_waiter(cgrp);
2148 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002149out_put_css_set_refs:
2150 if (retval) {
2151 for (i = 0; i < group_size; i++) {
2152 tc = flex_array_get(group, i);
2153 if (!tc->cg)
2154 break;
2155 put_css_set(tc->cg);
2156 }
Ben Blum74a11662011-05-26 16:25:20 -07002157 }
2158out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002159 if (retval) {
2160 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002161 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002162 break;
Ben Blum74a11662011-05-26 16:25:20 -07002163 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002164 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002165 }
2166 }
Ben Blum74a11662011-05-26 16:25:20 -07002167out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002168 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002169 return retval;
2170}
2171
2172/*
2173 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002174 * function to attach either it or all tasks in its threadgroup. Will lock
2175 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002176 */
2177static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002178{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002179 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002180 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002181 int ret;
2182
Ben Blum74a11662011-05-26 16:25:20 -07002183 if (!cgroup_lock_live_group(cgrp))
2184 return -ENODEV;
2185
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002186retry_find_task:
2187 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002188 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002189 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002190 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002191 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002192 ret= -ESRCH;
2193 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002194 }
Ben Blum74a11662011-05-26 16:25:20 -07002195 /*
2196 * even if we're attaching all tasks in the thread group, we
2197 * only need to check permissions on one of them.
2198 */
David Howellsc69e8d92008-11-14 10:39:19 +11002199 tcred = __task_cred(tsk);
2200 if (cred->euid &&
2201 cred->euid != tcred->uid &&
2202 cred->euid != tcred->suid) {
2203 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002204 ret = -EACCES;
2205 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002206 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002207 } else
2208 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002209
2210 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002211 tsk = tsk->group_leader;
2212 get_task_struct(tsk);
2213 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002214
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002215 threadgroup_lock(tsk);
2216 if (threadgroup) {
2217 if (!thread_group_leader(tsk)) {
2218 /*
2219 * a race with de_thread from another thread's exec()
2220 * may strip us of our leadership, if this happens,
2221 * there is no choice but to throw this task away and
2222 * try again; this is
2223 * "double-double-toil-and-trouble-check locking".
2224 */
2225 threadgroup_unlock(tsk);
2226 put_task_struct(tsk);
2227 goto retry_find_task;
2228 }
2229 ret = cgroup_attach_proc(cgrp, tsk);
2230 } else
2231 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002232 threadgroup_unlock(tsk);
2233
Paul Menagebbcb81d2007-10-18 23:39:32 -07002234 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002235out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002236 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002237 return ret;
2238}
2239
Paul Menageaf351022008-07-25 01:47:01 -07002240static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2241{
Ben Blum74a11662011-05-26 16:25:20 -07002242 return attach_task_by_pid(cgrp, pid, false);
2243}
2244
2245static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2246{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002247 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002248}
2249
Paul Menagee788e062008-07-25 01:46:59 -07002250/**
2251 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2252 * @cgrp: the cgroup to be checked for liveness
2253 *
Paul Menage84eea842008-07-25 01:47:00 -07002254 * On success, returns true; the lock should be later released with
2255 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002256 */
Paul Menage84eea842008-07-25 01:47:00 -07002257bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002258{
2259 mutex_lock(&cgroup_mutex);
2260 if (cgroup_is_removed(cgrp)) {
2261 mutex_unlock(&cgroup_mutex);
2262 return false;
2263 }
2264 return true;
2265}
Ben Blum67523c42010-03-10 15:22:11 -08002266EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002267
2268static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2269 const char *buffer)
2270{
2271 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002272 if (strlen(buffer) >= PATH_MAX)
2273 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002274 if (!cgroup_lock_live_group(cgrp))
2275 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002276 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002277 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002278 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002279 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002280 return 0;
2281}
2282
2283static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2284 struct seq_file *seq)
2285{
2286 if (!cgroup_lock_live_group(cgrp))
2287 return -ENODEV;
2288 seq_puts(seq, cgrp->root->release_agent_path);
2289 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002290 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002291 return 0;
2292}
2293
Paul Menage84eea842008-07-25 01:47:00 -07002294/* A buffer size big enough for numbers or short strings */
2295#define CGROUP_LOCAL_BUFFER_SIZE 64
2296
Paul Menagee73d2c62008-04-29 01:00:06 -07002297static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002298 struct file *file,
2299 const char __user *userbuf,
2300 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002301{
Paul Menage84eea842008-07-25 01:47:00 -07002302 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002303 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002304 char *end;
2305
2306 if (!nbytes)
2307 return -EINVAL;
2308 if (nbytes >= sizeof(buffer))
2309 return -E2BIG;
2310 if (copy_from_user(buffer, userbuf, nbytes))
2311 return -EFAULT;
2312
2313 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002314 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002315 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002316 if (*end)
2317 return -EINVAL;
2318 retval = cft->write_u64(cgrp, cft, val);
2319 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002320 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002321 if (*end)
2322 return -EINVAL;
2323 retval = cft->write_s64(cgrp, cft, val);
2324 }
Paul Menage355e0c42007-10-18 23:39:33 -07002325 if (!retval)
2326 retval = nbytes;
2327 return retval;
2328}
2329
Paul Menagedb3b1492008-07-25 01:46:58 -07002330static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2331 struct file *file,
2332 const char __user *userbuf,
2333 size_t nbytes, loff_t *unused_ppos)
2334{
Paul Menage84eea842008-07-25 01:47:00 -07002335 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002336 int retval = 0;
2337 size_t max_bytes = cft->max_write_len;
2338 char *buffer = local_buffer;
2339
2340 if (!max_bytes)
2341 max_bytes = sizeof(local_buffer) - 1;
2342 if (nbytes >= max_bytes)
2343 return -E2BIG;
2344 /* Allocate a dynamic buffer if we need one */
2345 if (nbytes >= sizeof(local_buffer)) {
2346 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2347 if (buffer == NULL)
2348 return -ENOMEM;
2349 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002350 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2351 retval = -EFAULT;
2352 goto out;
2353 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002354
2355 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002356 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002357 if (!retval)
2358 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002359out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002360 if (buffer != local_buffer)
2361 kfree(buffer);
2362 return retval;
2363}
2364
Paul Menageddbcc7e2007-10-18 23:39:30 -07002365static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2366 size_t nbytes, loff_t *ppos)
2367{
2368 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002369 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002370
Li Zefan75139b82009-01-07 18:07:33 -08002371 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002372 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002373 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002374 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002375 if (cft->write_u64 || cft->write_s64)
2376 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002377 if (cft->write_string)
2378 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002379 if (cft->trigger) {
2380 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2381 return ret ? ret : nbytes;
2382 }
Paul Menage355e0c42007-10-18 23:39:33 -07002383 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002384}
2385
Paul Menagef4c753b2008-04-29 00:59:56 -07002386static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2387 struct file *file,
2388 char __user *buf, size_t nbytes,
2389 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390{
Paul Menage84eea842008-07-25 01:47:00 -07002391 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002392 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002393 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2394
2395 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2396}
2397
Paul Menagee73d2c62008-04-29 01:00:06 -07002398static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2399 struct file *file,
2400 char __user *buf, size_t nbytes,
2401 loff_t *ppos)
2402{
Paul Menage84eea842008-07-25 01:47:00 -07002403 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002404 s64 val = cft->read_s64(cgrp, cft);
2405 int len = sprintf(tmp, "%lld\n", (long long) val);
2406
2407 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2408}
2409
Paul Menageddbcc7e2007-10-18 23:39:30 -07002410static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2411 size_t nbytes, loff_t *ppos)
2412{
2413 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002414 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002415
Li Zefan75139b82009-01-07 18:07:33 -08002416 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002417 return -ENODEV;
2418
2419 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002420 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002421 if (cft->read_u64)
2422 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002423 if (cft->read_s64)
2424 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002425 return -EINVAL;
2426}
2427
Paul Menage91796562008-04-29 01:00:01 -07002428/*
2429 * seqfile ops/methods for returning structured data. Currently just
2430 * supports string->u64 maps, but can be extended in future.
2431 */
2432
2433struct cgroup_seqfile_state {
2434 struct cftype *cft;
2435 struct cgroup *cgroup;
2436};
2437
2438static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2439{
2440 struct seq_file *sf = cb->state;
2441 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2442}
2443
2444static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2445{
2446 struct cgroup_seqfile_state *state = m->private;
2447 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002448 if (cft->read_map) {
2449 struct cgroup_map_cb cb = {
2450 .fill = cgroup_map_add,
2451 .state = m,
2452 };
2453 return cft->read_map(state->cgroup, cft, &cb);
2454 }
2455 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002456}
2457
Adrian Bunk96930a62008-07-25 19:46:21 -07002458static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002459{
2460 struct seq_file *seq = file->private_data;
2461 kfree(seq->private);
2462 return single_release(inode, file);
2463}
2464
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002465static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002466 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002467 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002468 .llseek = seq_lseek,
2469 .release = cgroup_seqfile_release,
2470};
2471
Paul Menageddbcc7e2007-10-18 23:39:30 -07002472static int cgroup_file_open(struct inode *inode, struct file *file)
2473{
2474 int err;
2475 struct cftype *cft;
2476
2477 err = generic_file_open(inode, file);
2478 if (err)
2479 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002480 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002481
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002482 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002483 struct cgroup_seqfile_state *state =
2484 kzalloc(sizeof(*state), GFP_USER);
2485 if (!state)
2486 return -ENOMEM;
2487 state->cft = cft;
2488 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2489 file->f_op = &cgroup_seqfile_operations;
2490 err = single_open(file, cgroup_seqfile_show, state);
2491 if (err < 0)
2492 kfree(state);
2493 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002494 err = cft->open(inode, file);
2495 else
2496 err = 0;
2497
2498 return err;
2499}
2500
2501static int cgroup_file_release(struct inode *inode, struct file *file)
2502{
2503 struct cftype *cft = __d_cft(file->f_dentry);
2504 if (cft->release)
2505 return cft->release(inode, file);
2506 return 0;
2507}
2508
2509/*
2510 * cgroup_rename - Only allow simple rename of directories in place.
2511 */
2512static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2513 struct inode *new_dir, struct dentry *new_dentry)
2514{
2515 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2516 return -ENOTDIR;
2517 if (new_dentry->d_inode)
2518 return -EEXIST;
2519 if (old_dir != new_dir)
2520 return -EIO;
2521 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2522}
2523
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002524static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002525 .read = cgroup_file_read,
2526 .write = cgroup_file_write,
2527 .llseek = generic_file_llseek,
2528 .open = cgroup_file_open,
2529 .release = cgroup_file_release,
2530};
2531
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002532static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002533 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002534 .mkdir = cgroup_mkdir,
2535 .rmdir = cgroup_rmdir,
2536 .rename = cgroup_rename,
2537};
2538
Al Viroc72a04e2011-01-14 05:31:45 +00002539static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2540{
2541 if (dentry->d_name.len > NAME_MAX)
2542 return ERR_PTR(-ENAMETOOLONG);
2543 d_add(dentry, NULL);
2544 return NULL;
2545}
2546
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002547/*
2548 * Check if a file is a control file
2549 */
2550static inline struct cftype *__file_cft(struct file *file)
2551{
2552 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2553 return ERR_PTR(-EINVAL);
2554 return __d_cft(file->f_dentry);
2555}
2556
Al Viroa5e7ed32011-07-26 01:55:55 -04002557static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002558 struct super_block *sb)
2559{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002560 struct inode *inode;
2561
2562 if (!dentry)
2563 return -ENOENT;
2564 if (dentry->d_inode)
2565 return -EEXIST;
2566
2567 inode = cgroup_new_inode(mode, sb);
2568 if (!inode)
2569 return -ENOMEM;
2570
2571 if (S_ISDIR(mode)) {
2572 inode->i_op = &cgroup_dir_inode_operations;
2573 inode->i_fop = &simple_dir_operations;
2574
2575 /* start off with i_nlink == 2 (for "." entry) */
2576 inc_nlink(inode);
2577
2578 /* start with the directory inode held, so that we can
2579 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07002580 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002581 } else if (S_ISREG(mode)) {
2582 inode->i_size = 0;
2583 inode->i_fop = &cgroup_file_operations;
2584 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002585 d_instantiate(dentry, inode);
2586 dget(dentry); /* Extra count - pin the dentry in core */
2587 return 0;
2588}
2589
2590/*
Li Zefana043e3b2008-02-23 15:24:09 -08002591 * cgroup_create_dir - create a directory for an object.
2592 * @cgrp: the cgroup we create the directory for. It must have a valid
2593 * ->parent field. And we are going to fill its ->dentry field.
2594 * @dentry: dentry of the new cgroup
2595 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002596 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002597static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04002598 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002599{
2600 struct dentry *parent;
2601 int error = 0;
2602
Paul Menagebd89aab2007-10-18 23:40:44 -07002603 parent = cgrp->parent->dentry;
2604 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002605 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002606 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002607 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08002608 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002609 dget(dentry);
2610 }
2611 dput(dentry);
2612
2613 return error;
2614}
2615
Li Zefan099fca32009-04-02 16:57:29 -07002616/**
2617 * cgroup_file_mode - deduce file mode of a control file
2618 * @cft: the control file in question
2619 *
2620 * returns cft->mode if ->mode is not 0
2621 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2622 * returns S_IRUGO if it has only a read handler
2623 * returns S_IWUSR if it has only a write hander
2624 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002625static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002626{
Al Viroa5e7ed32011-07-26 01:55:55 -04002627 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002628
2629 if (cft->mode)
2630 return cft->mode;
2631
2632 if (cft->read || cft->read_u64 || cft->read_s64 ||
2633 cft->read_map || cft->read_seq_string)
2634 mode |= S_IRUGO;
2635
2636 if (cft->write || cft->write_u64 || cft->write_s64 ||
2637 cft->write_string || cft->trigger)
2638 mode |= S_IWUSR;
2639
2640 return mode;
2641}
2642
Tejun Heodb0416b2012-04-01 12:09:55 -07002643static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
2644 const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002645{
Paul Menagebd89aab2007-10-18 23:40:44 -07002646 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002647 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002648 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002649 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002650 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002651 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002652 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002653
2654 /* does @cft->flags tell us to skip creation on @cgrp? */
2655 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2656 return 0;
2657 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2658 return 0;
2659
Paul Menagebd89aab2007-10-18 23:40:44 -07002660 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002661 strcpy(name, subsys->name);
2662 strcat(name, ".");
2663 }
2664 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002665
Paul Menageddbcc7e2007-10-18 23:39:30 -07002666 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002667
2668 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2669 if (!cfe)
2670 return -ENOMEM;
2671
Paul Menageddbcc7e2007-10-18 23:39:30 -07002672 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002673 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002675 goto out;
2676 }
2677
2678 mode = cgroup_file_mode(cft);
2679 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2680 if (!error) {
2681 cfe->type = (void *)cft;
2682 cfe->dentry = dentry;
2683 dentry->d_fsdata = cfe;
2684 list_add_tail(&cfe->node, &parent->files);
2685 cfe = NULL;
2686 }
2687 dput(dentry);
2688out:
2689 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002690 return error;
2691}
2692
Tejun Heo79578622012-04-01 12:09:56 -07002693static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
2694 const struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002695{
Tejun Heodb0416b2012-04-01 12:09:55 -07002696 const struct cftype *cft;
2697 int err, ret = 0;
2698
2699 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Tejun Heo79578622012-04-01 12:09:56 -07002700 if (is_add)
2701 err = cgroup_add_file(cgrp, subsys, cft);
2702 else
2703 err = cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002704 if (err) {
Tejun Heo79578622012-04-01 12:09:56 -07002705 pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n",
2706 is_add ? "add" : "remove", cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002707 ret = err;
2708 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002709 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002710 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002711}
2712
Tejun Heo8e3f6542012-04-01 12:09:55 -07002713static DEFINE_MUTEX(cgroup_cft_mutex);
2714
2715static void cgroup_cfts_prepare(void)
2716 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2717{
2718 /*
2719 * Thanks to the entanglement with vfs inode locking, we can't walk
2720 * the existing cgroups under cgroup_mutex and create files.
2721 * Instead, we increment reference on all cgroups and build list of
2722 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2723 * exclusive access to the field.
2724 */
2725 mutex_lock(&cgroup_cft_mutex);
2726 mutex_lock(&cgroup_mutex);
2727}
2728
2729static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Tejun Heo79578622012-04-01 12:09:56 -07002730 const struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002731 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2732{
2733 LIST_HEAD(pending);
2734 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002735
2736 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2737 if (cfts && ss->root != &rootnode) {
2738 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2739 dget(cgrp->dentry);
2740 list_add_tail(&cgrp->cft_q_node, &pending);
2741 }
2742 }
2743
2744 mutex_unlock(&cgroup_mutex);
2745
2746 /*
2747 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2748 * files for all cgroups which were created before.
2749 */
2750 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2751 struct inode *inode = cgrp->dentry->d_inode;
2752
2753 mutex_lock(&inode->i_mutex);
2754 mutex_lock(&cgroup_mutex);
2755 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002756 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002757 mutex_unlock(&cgroup_mutex);
2758 mutex_unlock(&inode->i_mutex);
2759
2760 list_del_init(&cgrp->cft_q_node);
2761 dput(cgrp->dentry);
2762 }
2763
2764 mutex_unlock(&cgroup_cft_mutex);
2765}
2766
2767/**
2768 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2769 * @ss: target cgroup subsystem
2770 * @cfts: zero-length name terminated array of cftypes
2771 *
2772 * Register @cfts to @ss. Files described by @cfts are created for all
2773 * existing cgroups to which @ss is attached and all future cgroups will
2774 * have them too. This function can be called anytime whether @ss is
2775 * attached or not.
2776 *
2777 * Returns 0 on successful registration, -errno on failure. Note that this
2778 * function currently returns 0 as long as @cfts registration is successful
2779 * even if some file creation attempts on existing cgroups fail.
2780 */
2781int cgroup_add_cftypes(struct cgroup_subsys *ss, const struct cftype *cfts)
2782{
2783 struct cftype_set *set;
2784
2785 set = kzalloc(sizeof(*set), GFP_KERNEL);
2786 if (!set)
2787 return -ENOMEM;
2788
2789 cgroup_cfts_prepare();
2790 set->cfts = cfts;
2791 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002792 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002793
2794 return 0;
2795}
2796EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2797
Li Zefana043e3b2008-02-23 15:24:09 -08002798/**
Tejun Heo79578622012-04-01 12:09:56 -07002799 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2800 * @ss: target cgroup subsystem
2801 * @cfts: zero-length name terminated array of cftypes
2802 *
2803 * Unregister @cfts from @ss. Files described by @cfts are removed from
2804 * all existing cgroups to which @ss is attached and all future cgroups
2805 * won't have them either. This function can be called anytime whether @ss
2806 * is attached or not.
2807 *
2808 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2809 * registered with @ss.
2810 */
2811int cgroup_rm_cftypes(struct cgroup_subsys *ss, const struct cftype *cfts)
2812{
2813 struct cftype_set *set;
2814
2815 cgroup_cfts_prepare();
2816
2817 list_for_each_entry(set, &ss->cftsets, node) {
2818 if (set->cfts == cfts) {
2819 list_del_init(&set->node);
2820 cgroup_cfts_commit(ss, cfts, false);
2821 return 0;
2822 }
2823 }
2824
2825 cgroup_cfts_commit(ss, NULL, false);
2826 return -ENOENT;
2827}
2828
2829/**
Li Zefana043e3b2008-02-23 15:24:09 -08002830 * cgroup_task_count - count the number of tasks in a cgroup.
2831 * @cgrp: the cgroup in question
2832 *
2833 * Return the number of tasks in the cgroup.
2834 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002835int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002836{
2837 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002838 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002839
Paul Menage817929e2007-10-18 23:39:36 -07002840 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002841 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002842 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002843 }
2844 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002845 return count;
2846}
2847
2848/*
Paul Menage817929e2007-10-18 23:39:36 -07002849 * Advance a list_head iterator. The iterator should be positioned at
2850 * the start of a css_set
2851 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002852static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002853 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002854{
2855 struct list_head *l = it->cg_link;
2856 struct cg_cgroup_link *link;
2857 struct css_set *cg;
2858
2859 /* Advance to the next non-empty css_set */
2860 do {
2861 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002862 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002863 it->cg_link = NULL;
2864 return;
2865 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002866 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002867 cg = link->cg;
2868 } while (list_empty(&cg->tasks));
2869 it->cg_link = l;
2870 it->task = cg->tasks.next;
2871}
2872
Cliff Wickman31a7df02008-02-07 00:14:42 -08002873/*
2874 * To reduce the fork() overhead for systems that are not actually
2875 * using their cgroups capability, we don't maintain the lists running
2876 * through each css_set to its tasks until we see the list actually
2877 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002878 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002879static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002880{
2881 struct task_struct *p, *g;
2882 write_lock(&css_set_lock);
2883 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002884 /*
2885 * We need tasklist_lock because RCU is not safe against
2886 * while_each_thread(). Besides, a forking task that has passed
2887 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2888 * is not guaranteed to have its child immediately visible in the
2889 * tasklist if we walk through it with RCU.
2890 */
2891 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002892 do_each_thread(g, p) {
2893 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002894 /*
2895 * We should check if the process is exiting, otherwise
2896 * it will race with cgroup_exit() in that the list
2897 * entry won't be deleted though the process has exited.
2898 */
2899 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002900 list_add(&p->cg_list, &p->cgroups->tasks);
2901 task_unlock(p);
2902 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002903 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002904 write_unlock(&css_set_lock);
2905}
2906
Paul Menagebd89aab2007-10-18 23:40:44 -07002907void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002908 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002909{
2910 /*
2911 * The first time anyone tries to iterate across a cgroup,
2912 * we need to enable the list linking each css_set to its
2913 * tasks, and fix up all existing tasks.
2914 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002915 if (!use_task_css_set_links)
2916 cgroup_enable_task_cg_lists();
2917
Paul Menage817929e2007-10-18 23:39:36 -07002918 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002919 it->cg_link = &cgrp->css_sets;
2920 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002921}
2922
Paul Menagebd89aab2007-10-18 23:40:44 -07002923struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07002924 struct cgroup_iter *it)
2925{
2926 struct task_struct *res;
2927 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002928 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002929
2930 /* If the iterator cg is NULL, we have no tasks */
2931 if (!it->cg_link)
2932 return NULL;
2933 res = list_entry(l, struct task_struct, cg_list);
2934 /* Advance iterator to find next entry */
2935 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002936 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2937 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07002938 /* We reached the end of this task list - move on to
2939 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07002940 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002941 } else {
2942 it->task = l;
2943 }
2944 return res;
2945}
2946
Paul Menagebd89aab2007-10-18 23:40:44 -07002947void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002948 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002949{
2950 read_unlock(&css_set_lock);
2951}
2952
Cliff Wickman31a7df02008-02-07 00:14:42 -08002953static inline int started_after_time(struct task_struct *t1,
2954 struct timespec *time,
2955 struct task_struct *t2)
2956{
2957 int start_diff = timespec_compare(&t1->start_time, time);
2958 if (start_diff > 0) {
2959 return 1;
2960 } else if (start_diff < 0) {
2961 return 0;
2962 } else {
2963 /*
2964 * Arbitrarily, if two processes started at the same
2965 * time, we'll say that the lower pointer value
2966 * started first. Note that t2 may have exited by now
2967 * so this may not be a valid pointer any longer, but
2968 * that's fine - it still serves to distinguish
2969 * between two tasks started (effectively) simultaneously.
2970 */
2971 return t1 > t2;
2972 }
2973}
2974
2975/*
2976 * This function is a callback from heap_insert() and is used to order
2977 * the heap.
2978 * In this case we order the heap in descending task start time.
2979 */
2980static inline int started_after(void *p1, void *p2)
2981{
2982 struct task_struct *t1 = p1;
2983 struct task_struct *t2 = p2;
2984 return started_after_time(t1, &t2->start_time, t2);
2985}
2986
2987/**
2988 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2989 * @scan: struct cgroup_scanner containing arguments for the scan
2990 *
2991 * Arguments include pointers to callback functions test_task() and
2992 * process_task().
2993 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2994 * and if it returns true, call process_task() for it also.
2995 * The test_task pointer may be NULL, meaning always true (select all tasks).
2996 * Effectively duplicates cgroup_iter_{start,next,end}()
2997 * but does not lock css_set_lock for the call to process_task().
2998 * The struct cgroup_scanner may be embedded in any structure of the caller's
2999 * creation.
3000 * It is guaranteed that process_task() will act on every task that
3001 * is a member of the cgroup for the duration of this call. This
3002 * function may or may not call process_task() for tasks that exit
3003 * or move to a different cgroup during the call, or are forked or
3004 * move into the cgroup during the call.
3005 *
3006 * Note that test_task() may be called with locks held, and may in some
3007 * situations be called multiple times for the same task, so it should
3008 * be cheap.
3009 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3010 * pre-allocated and will be used for heap operations (and its "gt" member will
3011 * be overwritten), else a temporary heap will be used (allocation of which
3012 * may cause this function to fail).
3013 */
3014int cgroup_scan_tasks(struct cgroup_scanner *scan)
3015{
3016 int retval, i;
3017 struct cgroup_iter it;
3018 struct task_struct *p, *dropped;
3019 /* Never dereference latest_task, since it's not refcounted */
3020 struct task_struct *latest_task = NULL;
3021 struct ptr_heap tmp_heap;
3022 struct ptr_heap *heap;
3023 struct timespec latest_time = { 0, 0 };
3024
3025 if (scan->heap) {
3026 /* The caller supplied our heap and pre-allocated its memory */
3027 heap = scan->heap;
3028 heap->gt = &started_after;
3029 } else {
3030 /* We need to allocate our own heap memory */
3031 heap = &tmp_heap;
3032 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3033 if (retval)
3034 /* cannot allocate the heap */
3035 return retval;
3036 }
3037
3038 again:
3039 /*
3040 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3041 * to determine which are of interest, and using the scanner's
3042 * "process_task" callback to process any of them that need an update.
3043 * Since we don't want to hold any locks during the task updates,
3044 * gather tasks to be processed in a heap structure.
3045 * The heap is sorted by descending task start time.
3046 * If the statically-sized heap fills up, we overflow tasks that
3047 * started later, and in future iterations only consider tasks that
3048 * started after the latest task in the previous pass. This
3049 * guarantees forward progress and that we don't miss any tasks.
3050 */
3051 heap->size = 0;
3052 cgroup_iter_start(scan->cg, &it);
3053 while ((p = cgroup_iter_next(scan->cg, &it))) {
3054 /*
3055 * Only affect tasks that qualify per the caller's callback,
3056 * if he provided one
3057 */
3058 if (scan->test_task && !scan->test_task(p, scan))
3059 continue;
3060 /*
3061 * Only process tasks that started after the last task
3062 * we processed
3063 */
3064 if (!started_after_time(p, &latest_time, latest_task))
3065 continue;
3066 dropped = heap_insert(heap, p);
3067 if (dropped == NULL) {
3068 /*
3069 * The new task was inserted; the heap wasn't
3070 * previously full
3071 */
3072 get_task_struct(p);
3073 } else if (dropped != p) {
3074 /*
3075 * The new task was inserted, and pushed out a
3076 * different task
3077 */
3078 get_task_struct(p);
3079 put_task_struct(dropped);
3080 }
3081 /*
3082 * Else the new task was newer than anything already in
3083 * the heap and wasn't inserted
3084 */
3085 }
3086 cgroup_iter_end(scan->cg, &it);
3087
3088 if (heap->size) {
3089 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003090 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003091 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003092 latest_time = q->start_time;
3093 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003094 }
3095 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003096 scan->process_task(q, scan);
3097 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003098 }
3099 /*
3100 * If we had to process any tasks at all, scan again
3101 * in case some of them were in the middle of forking
3102 * children that didn't get processed.
3103 * Not the most efficient way to do it, but it avoids
3104 * having to take callback_mutex in the fork path
3105 */
3106 goto again;
3107 }
3108 if (heap == &tmp_heap)
3109 heap_free(&tmp_heap);
3110 return 0;
3111}
3112
Paul Menage817929e2007-10-18 23:39:36 -07003113/*
Ben Blum102a7752009-09-23 15:56:26 -07003114 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003115 *
3116 * Reading this file can return large amounts of data if a cgroup has
3117 * *lots* of attached tasks. So it may need several calls to read(),
3118 * but we cannot guarantee that the information we produce is correct
3119 * unless we produce it entirely atomically.
3120 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003121 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003122
Li Zefan24528252012-01-20 11:58:43 +08003123/* which pidlist file are we talking about? */
3124enum cgroup_filetype {
3125 CGROUP_FILE_PROCS,
3126 CGROUP_FILE_TASKS,
3127};
3128
3129/*
3130 * A pidlist is a list of pids that virtually represents the contents of one
3131 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3132 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3133 * to the cgroup.
3134 */
3135struct cgroup_pidlist {
3136 /*
3137 * used to find which pidlist is wanted. doesn't change as long as
3138 * this particular list stays in the list.
3139 */
3140 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3141 /* array of xids */
3142 pid_t *list;
3143 /* how many elements the above list has */
3144 int length;
3145 /* how many files are using the current array */
3146 int use_count;
3147 /* each of these stored in a list by its cgroup */
3148 struct list_head links;
3149 /* pointer to the cgroup we belong to, for list removal purposes */
3150 struct cgroup *owner;
3151 /* protects the other fields */
3152 struct rw_semaphore mutex;
3153};
3154
Paul Menagebbcb81d2007-10-18 23:39:32 -07003155/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003156 * The following two functions "fix" the issue where there are more pids
3157 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3158 * TODO: replace with a kernel-wide solution to this problem
3159 */
3160#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3161static void *pidlist_allocate(int count)
3162{
3163 if (PIDLIST_TOO_LARGE(count))
3164 return vmalloc(count * sizeof(pid_t));
3165 else
3166 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3167}
3168static void pidlist_free(void *p)
3169{
3170 if (is_vmalloc_addr(p))
3171 vfree(p);
3172 else
3173 kfree(p);
3174}
3175static void *pidlist_resize(void *p, int newcount)
3176{
3177 void *newlist;
3178 /* note: if new alloc fails, old p will still be valid either way */
3179 if (is_vmalloc_addr(p)) {
3180 newlist = vmalloc(newcount * sizeof(pid_t));
3181 if (!newlist)
3182 return NULL;
3183 memcpy(newlist, p, newcount * sizeof(pid_t));
3184 vfree(p);
3185 } else {
3186 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3187 }
3188 return newlist;
3189}
3190
3191/*
Ben Blum102a7752009-09-23 15:56:26 -07003192 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3193 * If the new stripped list is sufficiently smaller and there's enough memory
3194 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3195 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003196 */
Ben Blum102a7752009-09-23 15:56:26 -07003197/* is the size difference enough that we should re-allocate the array? */
3198#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3199static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003200{
Ben Blum102a7752009-09-23 15:56:26 -07003201 int src, dest = 1;
3202 pid_t *list = *p;
3203 pid_t *newlist;
3204
3205 /*
3206 * we presume the 0th element is unique, so i starts at 1. trivial
3207 * edge cases first; no work needs to be done for either
3208 */
3209 if (length == 0 || length == 1)
3210 return length;
3211 /* src and dest walk down the list; dest counts unique elements */
3212 for (src = 1; src < length; src++) {
3213 /* find next unique element */
3214 while (list[src] == list[src-1]) {
3215 src++;
3216 if (src == length)
3217 goto after;
3218 }
3219 /* dest always points to where the next unique element goes */
3220 list[dest] = list[src];
3221 dest++;
3222 }
3223after:
3224 /*
3225 * if the length difference is large enough, we want to allocate a
3226 * smaller buffer to save memory. if this fails due to out of memory,
3227 * we'll just stay with what we've got.
3228 */
3229 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003230 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003231 if (newlist)
3232 *p = newlist;
3233 }
3234 return dest;
3235}
3236
3237static int cmppid(const void *a, const void *b)
3238{
3239 return *(pid_t *)a - *(pid_t *)b;
3240}
3241
3242/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003243 * find the appropriate pidlist for our purpose (given procs vs tasks)
3244 * returns with the lock on that pidlist already held, and takes care
3245 * of the use count, or returns NULL with no locks held if we're out of
3246 * memory.
3247 */
3248static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3249 enum cgroup_filetype type)
3250{
3251 struct cgroup_pidlist *l;
3252 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08003253 struct pid_namespace *ns = current->nsproxy->pid_ns;
3254
Ben Blum72a8cb32009-09-23 15:56:27 -07003255 /*
3256 * We can't drop the pidlist_mutex before taking the l->mutex in case
3257 * the last ref-holder is trying to remove l from the list at the same
3258 * time. Holding the pidlist_mutex precludes somebody taking whichever
3259 * list we find out from under us - compare release_pid_array().
3260 */
3261 mutex_lock(&cgrp->pidlist_mutex);
3262 list_for_each_entry(l, &cgrp->pidlists, links) {
3263 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003264 /* make sure l doesn't vanish out from under us */
3265 down_write(&l->mutex);
3266 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003267 return l;
3268 }
3269 }
3270 /* entry not found; create a new one */
3271 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3272 if (!l) {
3273 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003274 return l;
3275 }
3276 init_rwsem(&l->mutex);
3277 down_write(&l->mutex);
3278 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003279 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003280 l->use_count = 0; /* don't increment here */
3281 l->list = NULL;
3282 l->owner = cgrp;
3283 list_add(&l->links, &cgrp->pidlists);
3284 mutex_unlock(&cgrp->pidlist_mutex);
3285 return l;
3286}
3287
3288/*
Ben Blum102a7752009-09-23 15:56:26 -07003289 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3290 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003291static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3292 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003293{
3294 pid_t *array;
3295 int length;
3296 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003297 struct cgroup_iter it;
3298 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003299 struct cgroup_pidlist *l;
3300
3301 /*
3302 * If cgroup gets more users after we read count, we won't have
3303 * enough space - tough. This race is indistinguishable to the
3304 * caller from the case that the additional cgroup users didn't
3305 * show up until sometime later on.
3306 */
3307 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003308 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003309 if (!array)
3310 return -ENOMEM;
3311 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003312 cgroup_iter_start(cgrp, &it);
3313 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003314 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003315 break;
Ben Blum102a7752009-09-23 15:56:26 -07003316 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003317 if (type == CGROUP_FILE_PROCS)
3318 pid = task_tgid_vnr(tsk);
3319 else
3320 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003321 if (pid > 0) /* make sure to only use valid results */
3322 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003323 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003324 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003325 length = n;
3326 /* now sort & (if procs) strip out duplicates */
3327 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003328 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003329 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003330 l = cgroup_pidlist_find(cgrp, type);
3331 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003332 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003333 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003334 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003335 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003336 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003337 l->list = array;
3338 l->length = length;
3339 l->use_count++;
3340 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003341 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003342 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003343}
3344
Balbir Singh846c7bb2007-10-18 23:39:44 -07003345/**
Li Zefana043e3b2008-02-23 15:24:09 -08003346 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003347 * @stats: cgroupstats to fill information into
3348 * @dentry: A dentry entry belonging to the cgroup for which stats have
3349 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003350 *
3351 * Build and fill cgroupstats so that taskstats can export it to user
3352 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003353 */
3354int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3355{
3356 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003357 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003358 struct cgroup_iter it;
3359 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003360
Balbir Singh846c7bb2007-10-18 23:39:44 -07003361 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003362 * Validate dentry by checking the superblock operations,
3363 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003364 */
Li Zefan33d283b2008-11-19 15:36:48 -08003365 if (dentry->d_sb->s_op != &cgroup_ops ||
3366 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003367 goto err;
3368
3369 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003370 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003371
Paul Menagebd89aab2007-10-18 23:40:44 -07003372 cgroup_iter_start(cgrp, &it);
3373 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003374 switch (tsk->state) {
3375 case TASK_RUNNING:
3376 stats->nr_running++;
3377 break;
3378 case TASK_INTERRUPTIBLE:
3379 stats->nr_sleeping++;
3380 break;
3381 case TASK_UNINTERRUPTIBLE:
3382 stats->nr_uninterruptible++;
3383 break;
3384 case TASK_STOPPED:
3385 stats->nr_stopped++;
3386 break;
3387 default:
3388 if (delayacct_is_task_waiting_on_io(tsk))
3389 stats->nr_io_wait++;
3390 break;
3391 }
3392 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003393 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003394
Balbir Singh846c7bb2007-10-18 23:39:44 -07003395err:
3396 return ret;
3397}
3398
Paul Menage8f3ff202009-09-23 15:56:25 -07003399
Paul Menagecc31edc2008-10-18 20:28:04 -07003400/*
Ben Blum102a7752009-09-23 15:56:26 -07003401 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003402 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003403 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003404 */
3405
Ben Blum102a7752009-09-23 15:56:26 -07003406static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003407{
3408 /*
3409 * Initially we receive a position value that corresponds to
3410 * one more than the last pid shown (or 0 on the first call or
3411 * after a seek to the start). Use a binary-search to find the
3412 * next pid to display, if any
3413 */
Ben Blum102a7752009-09-23 15:56:26 -07003414 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003415 int index = 0, pid = *pos;
3416 int *iter;
3417
Ben Blum102a7752009-09-23 15:56:26 -07003418 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003419 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003420 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003421
Paul Menagecc31edc2008-10-18 20:28:04 -07003422 while (index < end) {
3423 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003424 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003425 index = mid;
3426 break;
Ben Blum102a7752009-09-23 15:56:26 -07003427 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003428 index = mid + 1;
3429 else
3430 end = mid;
3431 }
3432 }
3433 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003434 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003435 return NULL;
3436 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003437 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003438 *pos = *iter;
3439 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003440}
3441
Ben Blum102a7752009-09-23 15:56:26 -07003442static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003443{
Ben Blum102a7752009-09-23 15:56:26 -07003444 struct cgroup_pidlist *l = s->private;
3445 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003446}
3447
Ben Blum102a7752009-09-23 15:56:26 -07003448static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003449{
Ben Blum102a7752009-09-23 15:56:26 -07003450 struct cgroup_pidlist *l = s->private;
3451 pid_t *p = v;
3452 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003453 /*
3454 * Advance to the next pid in the array. If this goes off the
3455 * end, we're done
3456 */
3457 p++;
3458 if (p >= end) {
3459 return NULL;
3460 } else {
3461 *pos = *p;
3462 return p;
3463 }
3464}
3465
Ben Blum102a7752009-09-23 15:56:26 -07003466static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003467{
3468 return seq_printf(s, "%d\n", *(int *)v);
3469}
3470
Ben Blum102a7752009-09-23 15:56:26 -07003471/*
3472 * seq_operations functions for iterating on pidlists through seq_file -
3473 * independent of whether it's tasks or procs
3474 */
3475static const struct seq_operations cgroup_pidlist_seq_operations = {
3476 .start = cgroup_pidlist_start,
3477 .stop = cgroup_pidlist_stop,
3478 .next = cgroup_pidlist_next,
3479 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003480};
3481
Ben Blum102a7752009-09-23 15:56:26 -07003482static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003483{
Ben Blum72a8cb32009-09-23 15:56:27 -07003484 /*
3485 * the case where we're the last user of this particular pidlist will
3486 * have us remove it from the cgroup's list, which entails taking the
3487 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3488 * pidlist_mutex, we have to take pidlist_mutex first.
3489 */
3490 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003491 down_write(&l->mutex);
3492 BUG_ON(!l->use_count);
3493 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003494 /* we're the last user if refcount is 0; remove and free */
3495 list_del(&l->links);
3496 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003497 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003498 put_pid_ns(l->key.ns);
3499 up_write(&l->mutex);
3500 kfree(l);
3501 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003502 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003503 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003504 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003505}
3506
Ben Blum102a7752009-09-23 15:56:26 -07003507static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003508{
Ben Blum102a7752009-09-23 15:56:26 -07003509 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003510 if (!(file->f_mode & FMODE_READ))
3511 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003512 /*
3513 * the seq_file will only be initialized if the file was opened for
3514 * reading; hence we check if it's not null only in that case.
3515 */
3516 l = ((struct seq_file *)file->private_data)->private;
3517 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003518 return seq_release(inode, file);
3519}
3520
Ben Blum102a7752009-09-23 15:56:26 -07003521static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003522 .read = seq_read,
3523 .llseek = seq_lseek,
3524 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003525 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003526};
3527
3528/*
Ben Blum102a7752009-09-23 15:56:26 -07003529 * The following functions handle opens on a file that displays a pidlist
3530 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3531 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003532 */
Ben Blum102a7752009-09-23 15:56:26 -07003533/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003534static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003535{
3536 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003537 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003538 int retval;
3539
3540 /* Nothing to do for write-only files */
3541 if (!(file->f_mode & FMODE_READ))
3542 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003543
Ben Blum102a7752009-09-23 15:56:26 -07003544 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003545 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003546 if (retval)
3547 return retval;
3548 /* configure file information */
3549 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003550
Ben Blum102a7752009-09-23 15:56:26 -07003551 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003552 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003553 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003554 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003555 }
Ben Blum102a7752009-09-23 15:56:26 -07003556 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003557 return 0;
3558}
Ben Blum102a7752009-09-23 15:56:26 -07003559static int cgroup_tasks_open(struct inode *unused, struct file *file)
3560{
Ben Blum72a8cb32009-09-23 15:56:27 -07003561 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003562}
3563static int cgroup_procs_open(struct inode *unused, struct file *file)
3564{
Ben Blum72a8cb32009-09-23 15:56:27 -07003565 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003566}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003567
Paul Menagebd89aab2007-10-18 23:40:44 -07003568static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003569 struct cftype *cft)
3570{
Paul Menagebd89aab2007-10-18 23:40:44 -07003571 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003572}
3573
Paul Menage6379c102008-07-25 01:47:01 -07003574static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3575 struct cftype *cft,
3576 u64 val)
3577{
3578 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3579 if (val)
3580 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3581 else
3582 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3583 return 0;
3584}
3585
Paul Menagebbcb81d2007-10-18 23:39:32 -07003586/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003587 * Unregister event and free resources.
3588 *
3589 * Gets called from workqueue.
3590 */
3591static void cgroup_event_remove(struct work_struct *work)
3592{
3593 struct cgroup_event *event = container_of(work, struct cgroup_event,
3594 remove);
3595 struct cgroup *cgrp = event->cgrp;
3596
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003597 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3598
3599 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003600 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003601 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003602}
3603
3604/*
3605 * Gets called on POLLHUP on eventfd when user closes it.
3606 *
3607 * Called with wqh->lock held and interrupts disabled.
3608 */
3609static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3610 int sync, void *key)
3611{
3612 struct cgroup_event *event = container_of(wait,
3613 struct cgroup_event, wait);
3614 struct cgroup *cgrp = event->cgrp;
3615 unsigned long flags = (unsigned long)key;
3616
3617 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003618 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003619 spin_lock(&cgrp->event_list_lock);
3620 list_del(&event->list);
3621 spin_unlock(&cgrp->event_list_lock);
3622 /*
3623 * We are in atomic context, but cgroup_event_remove() may
3624 * sleep, so we have to call it in workqueue.
3625 */
3626 schedule_work(&event->remove);
3627 }
3628
3629 return 0;
3630}
3631
3632static void cgroup_event_ptable_queue_proc(struct file *file,
3633 wait_queue_head_t *wqh, poll_table *pt)
3634{
3635 struct cgroup_event *event = container_of(pt,
3636 struct cgroup_event, pt);
3637
3638 event->wqh = wqh;
3639 add_wait_queue(wqh, &event->wait);
3640}
3641
3642/*
3643 * Parse input and register new cgroup event handler.
3644 *
3645 * Input must be in format '<event_fd> <control_fd> <args>'.
3646 * Interpretation of args is defined by control file implementation.
3647 */
3648static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3649 const char *buffer)
3650{
3651 struct cgroup_event *event = NULL;
3652 unsigned int efd, cfd;
3653 struct file *efile = NULL;
3654 struct file *cfile = NULL;
3655 char *endp;
3656 int ret;
3657
3658 efd = simple_strtoul(buffer, &endp, 10);
3659 if (*endp != ' ')
3660 return -EINVAL;
3661 buffer = endp + 1;
3662
3663 cfd = simple_strtoul(buffer, &endp, 10);
3664 if ((*endp != ' ') && (*endp != '\0'))
3665 return -EINVAL;
3666 buffer = endp + 1;
3667
3668 event = kzalloc(sizeof(*event), GFP_KERNEL);
3669 if (!event)
3670 return -ENOMEM;
3671 event->cgrp = cgrp;
3672 INIT_LIST_HEAD(&event->list);
3673 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3674 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3675 INIT_WORK(&event->remove, cgroup_event_remove);
3676
3677 efile = eventfd_fget(efd);
3678 if (IS_ERR(efile)) {
3679 ret = PTR_ERR(efile);
3680 goto fail;
3681 }
3682
3683 event->eventfd = eventfd_ctx_fileget(efile);
3684 if (IS_ERR(event->eventfd)) {
3685 ret = PTR_ERR(event->eventfd);
3686 goto fail;
3687 }
3688
3689 cfile = fget(cfd);
3690 if (!cfile) {
3691 ret = -EBADF;
3692 goto fail;
3693 }
3694
3695 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003696 /* AV: shouldn't we check that it's been opened for read instead? */
3697 ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003698 if (ret < 0)
3699 goto fail;
3700
3701 event->cft = __file_cft(cfile);
3702 if (IS_ERR(event->cft)) {
3703 ret = PTR_ERR(event->cft);
3704 goto fail;
3705 }
3706
3707 if (!event->cft->register_event || !event->cft->unregister_event) {
3708 ret = -EINVAL;
3709 goto fail;
3710 }
3711
3712 ret = event->cft->register_event(cgrp, event->cft,
3713 event->eventfd, buffer);
3714 if (ret)
3715 goto fail;
3716
3717 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3718 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3719 ret = 0;
3720 goto fail;
3721 }
3722
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003723 /*
3724 * Events should be removed after rmdir of cgroup directory, but before
3725 * destroying subsystem state objects. Let's take reference to cgroup
3726 * directory dentry to do that.
3727 */
3728 dget(cgrp->dentry);
3729
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003730 spin_lock(&cgrp->event_list_lock);
3731 list_add(&event->list, &cgrp->event_list);
3732 spin_unlock(&cgrp->event_list_lock);
3733
3734 fput(cfile);
3735 fput(efile);
3736
3737 return 0;
3738
3739fail:
3740 if (cfile)
3741 fput(cfile);
3742
3743 if (event && event->eventfd && !IS_ERR(event->eventfd))
3744 eventfd_ctx_put(event->eventfd);
3745
3746 if (!IS_ERR_OR_NULL(efile))
3747 fput(efile);
3748
3749 kfree(event);
3750
3751 return ret;
3752}
3753
Daniel Lezcano97978e62010-10-27 15:33:35 -07003754static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3755 struct cftype *cft)
3756{
3757 return clone_children(cgrp);
3758}
3759
3760static int cgroup_clone_children_write(struct cgroup *cgrp,
3761 struct cftype *cft,
3762 u64 val)
3763{
3764 if (val)
3765 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3766 else
3767 clear_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3768 return 0;
3769}
3770
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003771/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003772 * for the common functions, 'private' gives the type of file
3773 */
Ben Blum102a7752009-09-23 15:56:26 -07003774/* for hysterical raisins, we can't put this on the older files */
3775#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003776static struct cftype files[] = {
3777 {
3778 .name = "tasks",
3779 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003780 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003781 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003782 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003783 },
Ben Blum102a7752009-09-23 15:56:26 -07003784 {
3785 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3786 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003787 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003788 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003789 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003790 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003791 {
3792 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003793 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003794 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003795 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003796 {
3797 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3798 .write_string = cgroup_write_event_control,
3799 .mode = S_IWUGO,
3800 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003801 {
3802 .name = "cgroup.clone_children",
3803 .read_u64 = cgroup_clone_children_read,
3804 .write_u64 = cgroup_clone_children_write,
3805 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003806 {
3807 .name = "release_agent",
3808 .flags = CFTYPE_ONLY_ON_ROOT,
3809 .read_seq_string = cgroup_release_agent_show,
3810 .write_string = cgroup_release_agent_write,
3811 .max_write_len = PATH_MAX,
3812 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003813 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003814};
3815
Paul Menagebd89aab2007-10-18 23:40:44 -07003816static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003817{
3818 int err;
3819 struct cgroup_subsys *ss;
3820
Tejun Heo79578622012-04-01 12:09:56 -07003821 err = cgroup_addrm_files(cgrp, NULL, files, true);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003822 if (err < 0)
3823 return err;
3824
Tejun Heo8e3f6542012-04-01 12:09:55 -07003825 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07003826 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003827 struct cftype_set *set;
3828
Paul Menagebd89aab2007-10-18 23:40:44 -07003829 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003830 return err;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003831
Tejun Heodb0416b2012-04-01 12:09:55 -07003832 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07003833 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003834 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07003835
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003836 /* This cgroup is ready now */
3837 for_each_subsys(cgrp->root, ss) {
3838 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3839 /*
3840 * Update id->css pointer and make this css visible from
3841 * CSS ID functions. This pointer will be dereferened
3842 * from RCU-read-side without locks.
3843 */
3844 if (css->id)
3845 rcu_assign_pointer(css->id->css, css);
3846 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003847
3848 return 0;
3849}
3850
3851static void init_cgroup_css(struct cgroup_subsys_state *css,
3852 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07003853 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003854{
Paul Menagebd89aab2007-10-18 23:40:44 -07003855 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08003856 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003857 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003858 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003859 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003860 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07003861 BUG_ON(cgrp->subsys[ss->subsys_id]);
3862 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003863}
3864
Paul Menage999cd8a2009-01-07 18:08:36 -08003865static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
3866{
3867 /* We need to take each hierarchy_mutex in a consistent order */
3868 int i;
3869
Ben Blumaae8aab2010-03-10 15:22:07 -08003870 /*
3871 * No worry about a race with rebind_subsystems that might mess up the
3872 * locking order, since both parties are under cgroup_mutex.
3873 */
Paul Menage999cd8a2009-01-07 18:08:36 -08003874 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3875 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003876 if (ss == NULL)
3877 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08003878 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08003879 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08003880 }
3881}
3882
3883static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
3884{
3885 int i;
3886
3887 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3888 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003889 if (ss == NULL)
3890 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08003891 if (ss->root == root)
3892 mutex_unlock(&ss->hierarchy_mutex);
3893 }
3894}
3895
Paul Menageddbcc7e2007-10-18 23:39:30 -07003896/*
Li Zefana043e3b2008-02-23 15:24:09 -08003897 * cgroup_create - create a cgroup
3898 * @parent: cgroup that will be parent of the new cgroup
3899 * @dentry: dentry of the new cgroup
3900 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07003901 *
Li Zefana043e3b2008-02-23 15:24:09 -08003902 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07003903 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07003904static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04003905 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003906{
Paul Menagebd89aab2007-10-18 23:40:44 -07003907 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003908 struct cgroupfs_root *root = parent->root;
3909 int err = 0;
3910 struct cgroup_subsys *ss;
3911 struct super_block *sb = root->sb;
3912
Paul Menagebd89aab2007-10-18 23:40:44 -07003913 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3914 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003915 return -ENOMEM;
3916
3917 /* Grab a reference on the superblock so the hierarchy doesn't
3918 * get deleted on unmount if there are child cgroups. This
3919 * can be done outside cgroup_mutex, since the sb can't
3920 * disappear while someone has an open control file on the
3921 * fs */
3922 atomic_inc(&sb->s_active);
3923
3924 mutex_lock(&cgroup_mutex);
3925
Paul Menagecc31edc2008-10-18 20:28:04 -07003926 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003927
Paul Menagebd89aab2007-10-18 23:40:44 -07003928 cgrp->parent = parent;
3929 cgrp->root = parent->root;
3930 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003931
Li Zefanb6abdb02008-03-04 14:28:19 -08003932 if (notify_on_release(parent))
3933 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3934
Daniel Lezcano97978e62010-10-27 15:33:35 -07003935 if (clone_children(parent))
3936 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3937
Paul Menageddbcc7e2007-10-18 23:39:30 -07003938 for_each_subsys(root, ss) {
Li Zefan761b3ef2012-01-31 13:47:36 +08003939 struct cgroup_subsys_state *css = ss->create(cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003940
Paul Menageddbcc7e2007-10-18 23:39:30 -07003941 if (IS_ERR(css)) {
3942 err = PTR_ERR(css);
3943 goto err_destroy;
3944 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003945 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003946 if (ss->use_id) {
3947 err = alloc_css_id(ss, parent, cgrp);
3948 if (err)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003949 goto err_destroy;
Li Zefan4528fd02010-02-02 13:44:10 -08003950 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003951 /* At error, ->destroy() callback has to free assigned ID. */
Daniel Lezcano97978e62010-10-27 15:33:35 -07003952 if (clone_children(parent) && ss->post_clone)
Li Zefan761b3ef2012-01-31 13:47:36 +08003953 ss->post_clone(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003954 }
3955
Paul Menage999cd8a2009-01-07 18:08:36 -08003956 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003957 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08003958 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003959 root->number_of_cgroups++;
3960
Paul Menagebd89aab2007-10-18 23:40:44 -07003961 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003962 if (err < 0)
3963 goto err_remove;
3964
3965 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07003966 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003967
Tejun Heob0ca5a82012-04-01 12:09:54 -07003968 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
3969
Paul Menagebd89aab2007-10-18 23:40:44 -07003970 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003971 /* If err < 0, we have a half-filled directory - oh well ;) */
3972
3973 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003974 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003975
3976 return 0;
3977
3978 err_remove:
3979
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003980 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003981 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003982 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003983 root->number_of_cgroups--;
3984
3985 err_destroy:
3986
3987 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003988 if (cgrp->subsys[ss->subsys_id])
Li Zefan761b3ef2012-01-31 13:47:36 +08003989 ss->destroy(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003990 }
3991
3992 mutex_unlock(&cgroup_mutex);
3993
3994 /* Release the reference count that we took on the superblock */
3995 deactivate_super(sb);
3996
Paul Menagebd89aab2007-10-18 23:40:44 -07003997 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003998 return err;
3999}
4000
Al Viro18bb1db2011-07-26 01:41:39 -04004001static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004002{
4003 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4004
4005 /* the vfs holds inode->i_mutex already */
4006 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4007}
4008
Li Zefan55b6fd02008-07-29 22:33:20 -07004009static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004010{
4011 /* Check the reference count on each subsystem. Since we
4012 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08004013 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07004014 * be no outstanding references, so the subsystem is safe to
4015 * destroy. We scan across all subsystems rather than using
4016 * the per-hierarchy linked list of mounted subsystems since
4017 * we can be called via check_for_release() with no
4018 * synchronization other than RCU, and the subsystem linked
4019 * list isn't RCU-safe */
4020 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08004021 /*
4022 * We won't need to lock the subsys array, because the subsystems
4023 * we're concerned about aren't going anywhere since our cgroup root
4024 * has a reference on them.
4025 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004026 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4027 struct cgroup_subsys *ss = subsys[i];
4028 struct cgroup_subsys_state *css;
Ben Blumaae8aab2010-03-10 15:22:07 -08004029 /* Skip subsystems not present or not in this hierarchy */
4030 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004031 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07004032 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07004033 /* When called from check_for_release() it's possible
4034 * that by this point the cgroup has been removed
4035 * and the css deleted. But a false-positive doesn't
4036 * matter, since it can only happen if the cgroup
4037 * has been deleted and hence no longer needs the
4038 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08004039 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07004040 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004041 }
4042 return 0;
4043}
4044
Paul Menagee7c5ec92009-01-07 18:08:38 -08004045/*
4046 * Atomically mark all (or else none) of the cgroup's CSS objects as
4047 * CSS_REMOVED. Return true on success, or false if the cgroup has
4048 * busy subsystems. Call with cgroup_mutex held
4049 */
4050
4051static int cgroup_clear_css_refs(struct cgroup *cgrp)
4052{
4053 struct cgroup_subsys *ss;
4054 unsigned long flags;
4055 bool failed = false;
4056 local_irq_save(flags);
4057 for_each_subsys(cgrp->root, ss) {
4058 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4059 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08004060 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08004061 /* We can only remove a CSS with a refcnt==1 */
4062 refcnt = atomic_read(&css->refcnt);
4063 if (refcnt > 1) {
4064 failed = true;
4065 goto done;
4066 }
4067 BUG_ON(!refcnt);
4068 /*
4069 * Drop the refcnt to 0 while we check other
4070 * subsystems. This will cause any racing
4071 * css_tryget() to spin until we set the
4072 * CSS_REMOVED bits or abort
4073 */
Paul Menage804b3c22009-01-29 14:25:21 -08004074 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
4075 break;
4076 cpu_relax();
4077 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08004078 }
4079 done:
4080 for_each_subsys(cgrp->root, ss) {
4081 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4082 if (failed) {
4083 /*
4084 * Restore old refcnt if we previously managed
4085 * to clear it from 1 to 0
4086 */
4087 if (!atomic_read(&css->refcnt))
4088 atomic_set(&css->refcnt, 1);
4089 } else {
4090 /* Commit the fact that the CSS is removed */
4091 set_bit(CSS_REMOVED, &css->flags);
4092 }
4093 }
4094 local_irq_restore(flags);
4095 return !failed;
4096}
4097
Paul Menageddbcc7e2007-10-18 23:39:30 -07004098static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4099{
Paul Menagebd89aab2007-10-18 23:40:44 -07004100 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004101 struct dentry *d;
4102 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004103 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004104 struct cgroup_event *event, *tmp;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004105 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004106
4107 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004108again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004109 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004110 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004111 mutex_unlock(&cgroup_mutex);
4112 return -EBUSY;
4113 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004114 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004115 mutex_unlock(&cgroup_mutex);
4116 return -EBUSY;
4117 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08004118 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08004119
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08004120 /*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004121 * In general, subsystem has no css->refcnt after pre_destroy(). But
4122 * in racy cases, subsystem may have to get css->refcnt after
4123 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
4124 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
4125 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
4126 * and subsystem's reference count handling. Please see css_get/put
4127 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
4128 */
4129 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4130
4131 /*
Li Zefana043e3b2008-02-23 15:24:09 -08004132 * Call pre_destroy handlers of subsys. Notify subsystems
4133 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08004134 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004135 ret = cgroup_call_pre_destroy(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004136 if (ret) {
4137 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004138 return ret;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004139 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004140
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08004141 mutex_lock(&cgroup_mutex);
4142 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004143 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004144 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004145 mutex_unlock(&cgroup_mutex);
4146 return -EBUSY;
4147 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004148 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004149 if (!cgroup_clear_css_refs(cgrp)) {
4150 mutex_unlock(&cgroup_mutex);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004151 /*
4152 * Because someone may call cgroup_wakeup_rmdir_waiter() before
4153 * prepare_to_wait(), we need to check this flag.
4154 */
4155 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
4156 schedule();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004157 finish_wait(&cgroup_rmdir_waitq, &wait);
4158 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4159 if (signal_pending(current))
4160 return -EINTR;
4161 goto again;
4162 }
4163 /* NO css_tryget() can success after here. */
4164 finish_wait(&cgroup_rmdir_waitq, &wait);
4165 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004166
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004167 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004168 set_bit(CGRP_REMOVED, &cgrp->flags);
4169 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004170 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004171 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004172
4173 cgroup_lock_hierarchy(cgrp->root);
4174 /* delete this cgroup from parent->children */
Phil Carmody8d258792011-03-22 16:30:13 -07004175 list_del_init(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08004176 cgroup_unlock_hierarchy(cgrp->root);
4177
Tejun Heob0ca5a82012-04-01 12:09:54 -07004178 list_del_init(&cgrp->allcg_node);
4179
Paul Menagebd89aab2007-10-18 23:40:44 -07004180 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004181
4182 cgroup_d_remove_dir(d);
4183 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004184
Paul Menagebd89aab2007-10-18 23:40:44 -07004185 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004186 check_for_release(parent);
4187
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004188 /*
4189 * Unregister events and notify userspace.
4190 * Notify userspace about cgroup removing only after rmdir of cgroup
4191 * directory to avoid race between userspace and kernelspace
4192 */
4193 spin_lock(&cgrp->event_list_lock);
4194 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4195 list_del(&event->list);
4196 remove_wait_queue(event->wqh, &event->wait);
4197 eventfd_signal(event->eventfd, 1);
4198 schedule_work(&event->remove);
4199 }
4200 spin_unlock(&cgrp->event_list_lock);
4201
Paul Menageddbcc7e2007-10-18 23:39:30 -07004202 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004203 return 0;
4204}
4205
Tejun Heo8e3f6542012-04-01 12:09:55 -07004206static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4207{
4208 INIT_LIST_HEAD(&ss->cftsets);
4209
4210 /*
4211 * base_cftset is embedded in subsys itself, no need to worry about
4212 * deregistration.
4213 */
4214 if (ss->base_cftypes) {
4215 ss->base_cftset.cfts = ss->base_cftypes;
4216 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4217 }
4218}
4219
Li Zefan06a11922008-04-29 01:00:07 -07004220static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004221{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004222 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004223
4224 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004225
Tejun Heo8e3f6542012-04-01 12:09:55 -07004226 /* init base cftset */
4227 cgroup_init_cftsets(ss);
4228
Paul Menageddbcc7e2007-10-18 23:39:30 -07004229 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004230 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004231 ss->root = &rootnode;
Li Zefan761b3ef2012-01-31 13:47:36 +08004232 css = ss->create(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004233 /* We don't handle early failures gracefully */
4234 BUG_ON(IS_ERR(css));
4235 init_cgroup_css(css, ss, dummytop);
4236
Li Zefane8d55fd2008-04-29 01:00:13 -07004237 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004238 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004239 * newly registered, all tasks and hence the
4240 * init_css_set is in the subsystem's top cgroup. */
4241 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004242
4243 need_forkexit_callback |= ss->fork || ss->exit;
4244
Li Zefane8d55fd2008-04-29 01:00:13 -07004245 /* At system boot, before all subsystems have been
4246 * registered, no tasks have been forked, so we don't
4247 * need to invoke fork callbacks here. */
4248 BUG_ON(!list_empty(&init_task.tasks));
4249
Paul Menage999cd8a2009-01-07 18:08:36 -08004250 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08004251 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004252 ss->active = 1;
Ben Blume6a11052010-03-10 15:22:09 -08004253
4254 /* this function shouldn't be used with modular subsystems, since they
4255 * need to register a subsys_id, among other things */
4256 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004257}
4258
4259/**
Ben Blume6a11052010-03-10 15:22:09 -08004260 * cgroup_load_subsys: load and register a modular subsystem at runtime
4261 * @ss: the subsystem to load
4262 *
4263 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004264 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004265 * up for use. If the subsystem is built-in anyway, work is delegated to the
4266 * simpler cgroup_init_subsys.
4267 */
4268int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4269{
4270 int i;
4271 struct cgroup_subsys_state *css;
4272
4273 /* check name and function validity */
4274 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4275 ss->create == NULL || ss->destroy == NULL)
4276 return -EINVAL;
4277
4278 /*
4279 * we don't support callbacks in modular subsystems. this check is
4280 * before the ss->module check for consistency; a subsystem that could
4281 * be a module should still have no callbacks even if the user isn't
4282 * compiling it as one.
4283 */
4284 if (ss->fork || ss->exit)
4285 return -EINVAL;
4286
4287 /*
4288 * an optionally modular subsystem is built-in: we want to do nothing,
4289 * since cgroup_init_subsys will have already taken care of it.
4290 */
4291 if (ss->module == NULL) {
4292 /* a few sanity checks */
4293 BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
4294 BUG_ON(subsys[ss->subsys_id] != ss);
4295 return 0;
4296 }
4297
Tejun Heo8e3f6542012-04-01 12:09:55 -07004298 /* init base cftset */
4299 cgroup_init_cftsets(ss);
4300
Ben Blume6a11052010-03-10 15:22:09 -08004301 /*
4302 * need to register a subsys id before anything else - for example,
4303 * init_cgroup_css needs it.
4304 */
4305 mutex_lock(&cgroup_mutex);
4306 /* find the first empty slot in the array */
4307 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
4308 if (subsys[i] == NULL)
4309 break;
4310 }
4311 if (i == CGROUP_SUBSYS_COUNT) {
4312 /* maximum number of subsystems already registered! */
4313 mutex_unlock(&cgroup_mutex);
4314 return -EBUSY;
4315 }
4316 /* assign ourselves the subsys_id */
4317 ss->subsys_id = i;
4318 subsys[i] = ss;
4319
4320 /*
4321 * no ss->create seems to need anything important in the ss struct, so
4322 * this can happen first (i.e. before the rootnode attachment).
4323 */
Li Zefan761b3ef2012-01-31 13:47:36 +08004324 css = ss->create(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004325 if (IS_ERR(css)) {
4326 /* failure case - need to deassign the subsys[] slot. */
4327 subsys[i] = NULL;
4328 mutex_unlock(&cgroup_mutex);
4329 return PTR_ERR(css);
4330 }
4331
4332 list_add(&ss->sibling, &rootnode.subsys_list);
4333 ss->root = &rootnode;
4334
4335 /* our new subsystem will be attached to the dummy hierarchy. */
4336 init_cgroup_css(css, ss, dummytop);
4337 /* init_idr must be after init_cgroup_css because it sets css->id. */
4338 if (ss->use_id) {
4339 int ret = cgroup_init_idr(ss, css);
4340 if (ret) {
4341 dummytop->subsys[ss->subsys_id] = NULL;
Li Zefan761b3ef2012-01-31 13:47:36 +08004342 ss->destroy(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004343 subsys[i] = NULL;
4344 mutex_unlock(&cgroup_mutex);
4345 return ret;
4346 }
4347 }
4348
4349 /*
4350 * Now we need to entangle the css into the existing css_sets. unlike
4351 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4352 * will need a new pointer to it; done by iterating the css_set_table.
4353 * furthermore, modifying the existing css_sets will corrupt the hash
4354 * table state, so each changed css_set will need its hash recomputed.
4355 * this is all done under the css_set_lock.
4356 */
4357 write_lock(&css_set_lock);
4358 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4359 struct css_set *cg;
4360 struct hlist_node *node, *tmp;
4361 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4362
4363 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4364 /* skip entries that we already rehashed */
4365 if (cg->subsys[ss->subsys_id])
4366 continue;
4367 /* remove existing entry */
4368 hlist_del(&cg->hlist);
4369 /* set new value */
4370 cg->subsys[ss->subsys_id] = css;
4371 /* recompute hash and restore entry */
4372 new_bucket = css_set_hash(cg->subsys);
4373 hlist_add_head(&cg->hlist, new_bucket);
4374 }
4375 }
4376 write_unlock(&css_set_lock);
4377
4378 mutex_init(&ss->hierarchy_mutex);
4379 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
4380 ss->active = 1;
4381
Ben Blume6a11052010-03-10 15:22:09 -08004382 /* success! */
4383 mutex_unlock(&cgroup_mutex);
4384 return 0;
4385}
4386EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4387
4388/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004389 * cgroup_unload_subsys: unload a modular subsystem
4390 * @ss: the subsystem to unload
4391 *
4392 * This function should be called in a modular subsystem's exitcall. When this
4393 * function is invoked, the refcount on the subsystem's module will be 0, so
4394 * the subsystem will not be attached to any hierarchy.
4395 */
4396void cgroup_unload_subsys(struct cgroup_subsys *ss)
4397{
4398 struct cg_cgroup_link *link;
4399 struct hlist_head *hhead;
4400
4401 BUG_ON(ss->module == NULL);
4402
4403 /*
4404 * we shouldn't be called if the subsystem is in use, and the use of
4405 * try_module_get in parse_cgroupfs_options should ensure that it
4406 * doesn't start being used while we're killing it off.
4407 */
4408 BUG_ON(ss->root != &rootnode);
4409
4410 mutex_lock(&cgroup_mutex);
4411 /* deassign the subsys_id */
4412 BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT);
4413 subsys[ss->subsys_id] = NULL;
4414
4415 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004416 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004417
4418 /*
4419 * disentangle the css from all css_sets attached to the dummytop. as
4420 * in loading, we need to pay our respects to the hashtable gods.
4421 */
4422 write_lock(&css_set_lock);
4423 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4424 struct css_set *cg = link->cg;
4425
4426 hlist_del(&cg->hlist);
4427 BUG_ON(!cg->subsys[ss->subsys_id]);
4428 cg->subsys[ss->subsys_id] = NULL;
4429 hhead = css_set_hash(cg->subsys);
4430 hlist_add_head(&cg->hlist, hhead);
4431 }
4432 write_unlock(&css_set_lock);
4433
4434 /*
4435 * remove subsystem's css from the dummytop and free it - need to free
4436 * before marking as null because ss->destroy needs the cgrp->subsys
4437 * pointer to find their state. note that this also takes care of
4438 * freeing the css_id.
4439 */
Li Zefan761b3ef2012-01-31 13:47:36 +08004440 ss->destroy(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004441 dummytop->subsys[ss->subsys_id] = NULL;
4442
4443 mutex_unlock(&cgroup_mutex);
4444}
4445EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4446
4447/**
Li Zefana043e3b2008-02-23 15:24:09 -08004448 * cgroup_init_early - cgroup initialization at system boot
4449 *
4450 * Initialize cgroups at system boot, and initialize any
4451 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004452 */
4453int __init cgroup_init_early(void)
4454{
4455 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004456 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004457 INIT_LIST_HEAD(&init_css_set.cg_links);
4458 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004459 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004460 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004461 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004462 root_count = 1;
4463 init_task.cgroups = &init_css_set;
4464
4465 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004466 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004467 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004468 &rootnode.top_cgroup.css_sets);
4469 list_add(&init_css_set_link.cg_link_list,
4470 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004471
Li Zefan472b1052008-04-29 01:00:11 -07004472 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4473 INIT_HLIST_HEAD(&css_set_table[i]);
4474
Ben Blumaae8aab2010-03-10 15:22:07 -08004475 /* at bootup time, we don't worry about modular subsystems */
4476 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004477 struct cgroup_subsys *ss = subsys[i];
4478
4479 BUG_ON(!ss->name);
4480 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
4481 BUG_ON(!ss->create);
4482 BUG_ON(!ss->destroy);
4483 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004484 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004485 ss->name, ss->subsys_id);
4486 BUG();
4487 }
4488
4489 if (ss->early_init)
4490 cgroup_init_subsys(ss);
4491 }
4492 return 0;
4493}
4494
4495/**
Li Zefana043e3b2008-02-23 15:24:09 -08004496 * cgroup_init - cgroup initialization
4497 *
4498 * Register cgroup filesystem and /proc file, and initialize
4499 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004500 */
4501int __init cgroup_init(void)
4502{
4503 int err;
4504 int i;
Li Zefan472b1052008-04-29 01:00:11 -07004505 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07004506
4507 err = bdi_init(&cgroup_backing_dev_info);
4508 if (err)
4509 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004510
Ben Blumaae8aab2010-03-10 15:22:07 -08004511 /* at bootup time, we don't worry about modular subsystems */
4512 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004513 struct cgroup_subsys *ss = subsys[i];
4514 if (!ss->early_init)
4515 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004516 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004517 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004518 }
4519
Li Zefan472b1052008-04-29 01:00:11 -07004520 /* Add init_css_set to the hash table */
4521 hhead = css_set_hash(init_css_set.subsys);
4522 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004523 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004524
4525 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4526 if (!cgroup_kobj) {
4527 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004528 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004529 }
4530
4531 err = register_filesystem(&cgroup_fs_type);
4532 if (err < 0) {
4533 kobject_put(cgroup_kobj);
4534 goto out;
4535 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004536
Li Zefan46ae2202008-04-29 01:00:08 -07004537 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004538
Paul Menageddbcc7e2007-10-18 23:39:30 -07004539out:
Paul Menagea4243162007-10-18 23:39:35 -07004540 if (err)
4541 bdi_destroy(&cgroup_backing_dev_info);
4542
Paul Menageddbcc7e2007-10-18 23:39:30 -07004543 return err;
4544}
Paul Menageb4f48b62007-10-18 23:39:33 -07004545
Paul Menagea4243162007-10-18 23:39:35 -07004546/*
4547 * proc_cgroup_show()
4548 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4549 * - Used for /proc/<pid>/cgroup.
4550 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4551 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004552 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004553 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4554 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4555 * cgroup to top_cgroup.
4556 */
4557
4558/* TODO: Use a proper seq_file iterator */
4559static int proc_cgroup_show(struct seq_file *m, void *v)
4560{
4561 struct pid *pid;
4562 struct task_struct *tsk;
4563 char *buf;
4564 int retval;
4565 struct cgroupfs_root *root;
4566
4567 retval = -ENOMEM;
4568 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4569 if (!buf)
4570 goto out;
4571
4572 retval = -ESRCH;
4573 pid = m->private;
4574 tsk = get_pid_task(pid, PIDTYPE_PID);
4575 if (!tsk)
4576 goto out_free;
4577
4578 retval = 0;
4579
4580 mutex_lock(&cgroup_mutex);
4581
Li Zefane5f6a862009-01-07 18:07:41 -08004582 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004583 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004584 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004585 int count = 0;
4586
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004587 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004588 for_each_subsys(root, ss)
4589 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004590 if (strlen(root->name))
4591 seq_printf(m, "%sname=%s", count ? "," : "",
4592 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004593 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004594 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004595 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004596 if (retval < 0)
4597 goto out_unlock;
4598 seq_puts(m, buf);
4599 seq_putc(m, '\n');
4600 }
4601
4602out_unlock:
4603 mutex_unlock(&cgroup_mutex);
4604 put_task_struct(tsk);
4605out_free:
4606 kfree(buf);
4607out:
4608 return retval;
4609}
4610
4611static int cgroup_open(struct inode *inode, struct file *file)
4612{
4613 struct pid *pid = PROC_I(inode)->pid;
4614 return single_open(file, proc_cgroup_show, pid);
4615}
4616
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004617const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004618 .open = cgroup_open,
4619 .read = seq_read,
4620 .llseek = seq_lseek,
4621 .release = single_release,
4622};
4623
4624/* Display information about each subsystem and each hierarchy */
4625static int proc_cgroupstats_show(struct seq_file *m, void *v)
4626{
4627 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004628
Paul Menage8bab8dd2008-04-04 14:29:57 -07004629 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004630 /*
4631 * ideally we don't want subsystems moving around while we do this.
4632 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4633 * subsys/hierarchy state.
4634 */
Paul Menagea4243162007-10-18 23:39:35 -07004635 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004636 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4637 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004638 if (ss == NULL)
4639 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004640 seq_printf(m, "%s\t%d\t%d\t%d\n",
4641 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004642 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004643 }
4644 mutex_unlock(&cgroup_mutex);
4645 return 0;
4646}
4647
4648static int cgroupstats_open(struct inode *inode, struct file *file)
4649{
Al Viro9dce07f2008-03-29 03:07:28 +00004650 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004651}
4652
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004653static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004654 .open = cgroupstats_open,
4655 .read = seq_read,
4656 .llseek = seq_lseek,
4657 .release = single_release,
4658};
4659
Paul Menageb4f48b62007-10-18 23:39:33 -07004660/**
4661 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004662 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004663 *
4664 * Description: A task inherits its parent's cgroup at fork().
4665 *
4666 * A pointer to the shared css_set was automatically copied in
4667 * fork.c by dup_task_struct(). However, we ignore that copy, since
Frederic Weisbecker7e381b0e2011-12-21 20:03:19 +01004668 * it was not made under the protection of RCU, cgroup_mutex or
4669 * threadgroup_change_begin(), so it might no longer be a valid
4670 * cgroup pointer. cgroup_attach_task() might have already changed
4671 * current->cgroups, allowing the previously referenced cgroup
4672 * group to be removed and freed.
4673 *
4674 * Outside the pointer validity we also need to process the css_set
4675 * inheritance between threadgoup_change_begin() and
4676 * threadgoup_change_end(), this way there is no leak in any process
4677 * wide migration performed by cgroup_attach_proc() that could otherwise
4678 * miss a thread because it is too early or too late in the fork stage.
Paul Menageb4f48b62007-10-18 23:39:33 -07004679 *
4680 * At the point that cgroup_fork() is called, 'current' is the parent
4681 * task, and the passed argument 'child' points to the child task.
4682 */
4683void cgroup_fork(struct task_struct *child)
4684{
Frederic Weisbecker7e381b0e2011-12-21 20:03:19 +01004685 /*
4686 * We don't need to task_lock() current because current->cgroups
4687 * can't be changed concurrently here. The parent obviously hasn't
4688 * exited and called cgroup_exit(), and we are synchronized against
4689 * cgroup migration through threadgroup_change_begin().
4690 */
Paul Menage817929e2007-10-18 23:39:36 -07004691 child->cgroups = current->cgroups;
4692 get_css_set(child->cgroups);
Paul Menage817929e2007-10-18 23:39:36 -07004693 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004694}
4695
4696/**
Li Zefana043e3b2008-02-23 15:24:09 -08004697 * cgroup_fork_callbacks - run fork callbacks
4698 * @child: the new task
4699 *
4700 * Called on a new task very soon before adding it to the
4701 * tasklist. No need to take any locks since no-one can
4702 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004703 */
4704void cgroup_fork_callbacks(struct task_struct *child)
4705{
4706 if (need_forkexit_callback) {
4707 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08004708 /*
4709 * forkexit callbacks are only supported for builtin
4710 * subsystems, and the builtin section of the subsys array is
4711 * immutable, so we don't need to lock the subsys array here.
4712 */
4713 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageb4f48b62007-10-18 23:39:33 -07004714 struct cgroup_subsys *ss = subsys[i];
4715 if (ss->fork)
Li Zefan761b3ef2012-01-31 13:47:36 +08004716 ss->fork(child);
Paul Menageb4f48b62007-10-18 23:39:33 -07004717 }
4718 }
4719}
4720
4721/**
Li Zefana043e3b2008-02-23 15:24:09 -08004722 * cgroup_post_fork - called on a new task after adding it to the task list
4723 * @child: the task in question
4724 *
4725 * Adds the task to the list running through its css_set if necessary.
4726 * Has to be after the task is visible on the task list in case we race
4727 * with the first call to cgroup_iter_start() - to guarantee that the
4728 * new task ends up on its list.
4729 */
Paul Menage817929e2007-10-18 23:39:36 -07004730void cgroup_post_fork(struct task_struct *child)
4731{
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004732 /*
4733 * use_task_css_set_links is set to 1 before we walk the tasklist
4734 * under the tasklist_lock and we read it here after we added the child
4735 * to the tasklist under the tasklist_lock as well. If the child wasn't
4736 * yet in the tasklist when we walked through it from
4737 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4738 * should be visible now due to the paired locking and barriers implied
4739 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4740 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4741 * lock on fork.
4742 */
Paul Menage817929e2007-10-18 23:39:36 -07004743 if (use_task_css_set_links) {
4744 write_lock(&css_set_lock);
Frederic Weisbecker7e3aa302011-12-23 04:25:23 +01004745 if (list_empty(&child->cg_list)) {
4746 /*
4747 * It's safe to use child->cgroups without task_lock()
4748 * here because we are protected through
4749 * threadgroup_change_begin() against concurrent
4750 * css_set change in cgroup_task_migrate(). Also
4751 * the task can't exit at that point until
4752 * wake_up_new_task() is called, so we are protected
4753 * against cgroup_exit() setting child->cgroup to
4754 * init_css_set.
4755 */
Paul Menage817929e2007-10-18 23:39:36 -07004756 list_add(&child->cg_list, &child->cgroups->tasks);
Frederic Weisbecker7e3aa302011-12-23 04:25:23 +01004757 }
Paul Menage817929e2007-10-18 23:39:36 -07004758 write_unlock(&css_set_lock);
4759 }
4760}
4761/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004762 * cgroup_exit - detach cgroup from exiting task
4763 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004764 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004765 *
4766 * Description: Detach cgroup from @tsk and release it.
4767 *
4768 * Note that cgroups marked notify_on_release force every task in
4769 * them to take the global cgroup_mutex mutex when exiting.
4770 * This could impact scaling on very large systems. Be reluctant to
4771 * use notify_on_release cgroups where very high task exit scaling
4772 * is required on large systems.
4773 *
4774 * the_top_cgroup_hack:
4775 *
4776 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4777 *
4778 * We call cgroup_exit() while the task is still competent to
4779 * handle notify_on_release(), then leave the task attached to the
4780 * root cgroup in each hierarchy for the remainder of its exit.
4781 *
4782 * To do this properly, we would increment the reference count on
4783 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4784 * code we would add a second cgroup function call, to drop that
4785 * reference. This would just create an unnecessary hot spot on
4786 * the top_cgroup reference count, to no avail.
4787 *
4788 * Normally, holding a reference to a cgroup without bumping its
4789 * count is unsafe. The cgroup could go away, or someone could
4790 * attach us to a different cgroup, decrementing the count on
4791 * the first cgroup that we never incremented. But in this case,
4792 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004793 * which wards off any cgroup_attach_task() attempts, or task is a failed
4794 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004795 */
4796void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4797{
Paul Menage817929e2007-10-18 23:39:36 -07004798 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004799 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004800
4801 /*
4802 * Unlink from the css_set task list if necessary.
4803 * Optimistically check cg_list before taking
4804 * css_set_lock
4805 */
4806 if (!list_empty(&tsk->cg_list)) {
4807 write_lock(&css_set_lock);
4808 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004809 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004810 write_unlock(&css_set_lock);
4811 }
4812
Paul Menageb4f48b62007-10-18 23:39:33 -07004813 /* Reassign the task to the init_css_set. */
4814 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004815 cg = tsk->cgroups;
4816 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004817
4818 if (run_callbacks && need_forkexit_callback) {
4819 /*
4820 * modular subsystems can't use callbacks, so no need to lock
4821 * the subsys array
4822 */
4823 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
4824 struct cgroup_subsys *ss = subsys[i];
4825 if (ss->exit) {
4826 struct cgroup *old_cgrp =
4827 rcu_dereference_raw(cg->subsys[i])->cgroup;
4828 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004829 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004830 }
4831 }
4832 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004833 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004834
Paul Menage817929e2007-10-18 23:39:36 -07004835 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004836 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004837}
Paul Menage697f4162007-10-18 23:39:34 -07004838
4839/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004840 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08004841 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004842 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08004843 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004844 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4845 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07004846 *
4847 * If we are sending in dummytop, then presumably we are creating
4848 * the top cgroup in the subsystem.
4849 *
4850 * Called only by the ns (nsproxy) cgroup.
4851 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004852int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07004853{
4854 int ret;
4855 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07004856
Paul Menagebd89aab2007-10-18 23:40:44 -07004857 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07004858 return 1;
4859
Paul Menage7717f7b2009-09-23 15:56:22 -07004860 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004861 while (cgrp != target && cgrp!= cgrp->top_cgroup)
4862 cgrp = cgrp->parent;
4863 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07004864 return ret;
4865}
Paul Menage81a6a5c2007-10-18 23:39:38 -07004866
Paul Menagebd89aab2007-10-18 23:40:44 -07004867static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004868{
4869 /* All of these checks rely on RCU to keep the cgroup
4870 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07004871 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
4872 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004873 /* Control Group is currently removeable. If it's not
4874 * already queued for a userspace notification, queue
4875 * it now */
4876 int need_schedule_work = 0;
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004877 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004878 if (!cgroup_is_removed(cgrp) &&
4879 list_empty(&cgrp->release_list)) {
4880 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004881 need_schedule_work = 1;
4882 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004883 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004884 if (need_schedule_work)
4885 schedule_work(&release_agent_work);
4886 }
4887}
4888
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004889/* Caller must verify that the css is not for root cgroup */
4890void __css_put(struct cgroup_subsys_state *css, int count)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004891{
Paul Menagebd89aab2007-10-18 23:40:44 -07004892 struct cgroup *cgrp = css->cgroup;
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004893 int val;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004894 rcu_read_lock();
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004895 val = atomic_sub_return(count, &css->refcnt);
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004896 if (val == 1) {
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004897 if (notify_on_release(cgrp)) {
4898 set_bit(CGRP_RELEASABLE, &cgrp->flags);
4899 check_for_release(cgrp);
4900 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004901 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004902 }
4903 rcu_read_unlock();
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004904 WARN_ON_ONCE(val < 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004905}
Ben Blum67523c42010-03-10 15:22:11 -08004906EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004907
4908/*
4909 * Notify userspace when a cgroup is released, by running the
4910 * configured release agent with the name of the cgroup (path
4911 * relative to the root of cgroup file system) as the argument.
4912 *
4913 * Most likely, this user command will try to rmdir this cgroup.
4914 *
4915 * This races with the possibility that some other task will be
4916 * attached to this cgroup before it is removed, or that some other
4917 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4918 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4919 * unused, and this cgroup will be reprieved from its death sentence,
4920 * to continue to serve a useful existence. Next time it's released,
4921 * we will get notified again, if it still has 'notify_on_release' set.
4922 *
4923 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4924 * means only wait until the task is successfully execve()'d. The
4925 * separate release agent task is forked by call_usermodehelper(),
4926 * then control in this thread returns here, without waiting for the
4927 * release agent task. We don't bother to wait because the caller of
4928 * this routine has no use for the exit status of the release agent
4929 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004930 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004931static void cgroup_release_agent(struct work_struct *work)
4932{
4933 BUG_ON(work != &release_agent_work);
4934 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004935 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004936 while (!list_empty(&release_list)) {
4937 char *argv[3], *envp[3];
4938 int i;
Paul Menagee788e062008-07-25 01:46:59 -07004939 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004940 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004941 struct cgroup,
4942 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004943 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004944 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004945 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004946 if (!pathbuf)
4947 goto continue_free;
4948 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
4949 goto continue_free;
4950 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4951 if (!agentbuf)
4952 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004953
4954 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004955 argv[i++] = agentbuf;
4956 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004957 argv[i] = NULL;
4958
4959 i = 0;
4960 /* minimal command environment */
4961 envp[i++] = "HOME=/";
4962 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4963 envp[i] = NULL;
4964
4965 /* Drop the lock while we invoke the usermode helper,
4966 * since the exec could involve hitting disk and hence
4967 * be a slow process */
4968 mutex_unlock(&cgroup_mutex);
4969 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004970 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004971 continue_free:
4972 kfree(pathbuf);
4973 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004974 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004975 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004976 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004977 mutex_unlock(&cgroup_mutex);
4978}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004979
4980static int __init cgroup_disable(char *str)
4981{
4982 int i;
4983 char *token;
4984
4985 while ((token = strsep(&str, ",")) != NULL) {
4986 if (!*token)
4987 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08004988 /*
4989 * cgroup_disable, being at boot time, can't know about module
4990 * subsystems, so we don't worry about them.
4991 */
4992 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004993 struct cgroup_subsys *ss = subsys[i];
4994
4995 if (!strcmp(token, ss->name)) {
4996 ss->disabled = 1;
4997 printk(KERN_INFO "Disabling %s control group"
4998 " subsystem\n", ss->name);
4999 break;
5000 }
5001 }
5002 }
5003 return 1;
5004}
5005__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005006
5007/*
5008 * Functons for CSS ID.
5009 */
5010
5011/*
5012 *To get ID other than 0, this should be called when !cgroup_is_removed().
5013 */
5014unsigned short css_id(struct cgroup_subsys_state *css)
5015{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005016 struct css_id *cssid;
5017
5018 /*
5019 * This css_id() can return correct value when somone has refcnt
5020 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5021 * it's unchanged until freed.
5022 */
Michal Hockod8bf4ca2011-07-08 14:39:41 +02005023 cssid = rcu_dereference_check(css->id, atomic_read(&css->refcnt));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005024
5025 if (cssid)
5026 return cssid->id;
5027 return 0;
5028}
Ben Blum67523c42010-03-10 15:22:11 -08005029EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005030
5031unsigned short css_depth(struct cgroup_subsys_state *css)
5032{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005033 struct css_id *cssid;
5034
Michal Hockod8bf4ca2011-07-08 14:39:41 +02005035 cssid = rcu_dereference_check(css->id, atomic_read(&css->refcnt));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005036
5037 if (cssid)
5038 return cssid->depth;
5039 return 0;
5040}
Ben Blum67523c42010-03-10 15:22:11 -08005041EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005042
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005043/**
5044 * css_is_ancestor - test "root" css is an ancestor of "child"
5045 * @child: the css to be tested.
5046 * @root: the css supporsed to be an ancestor of the child.
5047 *
5048 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
5049 * this function reads css->id, this use rcu_dereference() and rcu_read_lock().
5050 * But, considering usual usage, the csses should be valid objects after test.
5051 * Assuming that the caller will do some action to the child if this returns
5052 * returns true, the caller must take "child";s reference count.
5053 * If "child" is valid object and this returns true, "root" is valid, too.
5054 */
5055
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005056bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005057 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005058{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005059 struct css_id *child_id;
5060 struct css_id *root_id;
5061 bool ret = true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005062
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005063 rcu_read_lock();
5064 child_id = rcu_dereference(child->id);
5065 root_id = rcu_dereference(root->id);
5066 if (!child_id
5067 || !root_id
5068 || (child_id->depth < root_id->depth)
5069 || (child_id->stack[root_id->depth] != root_id->id))
5070 ret = false;
5071 rcu_read_unlock();
5072 return ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005073}
5074
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005075void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5076{
5077 struct css_id *id = css->id;
5078 /* When this is called before css_id initialization, id can be NULL */
5079 if (!id)
5080 return;
5081
5082 BUG_ON(!ss->use_id);
5083
5084 rcu_assign_pointer(id->css, NULL);
5085 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005086 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005087 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005088 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005089 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005090}
Ben Blum67523c42010-03-10 15:22:11 -08005091EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005092
5093/*
5094 * This is called by init or create(). Then, calls to this function are
5095 * always serialized (By cgroup_mutex() at create()).
5096 */
5097
5098static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5099{
5100 struct css_id *newid;
5101 int myid, error, size;
5102
5103 BUG_ON(!ss->use_id);
5104
5105 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5106 newid = kzalloc(size, GFP_KERNEL);
5107 if (!newid)
5108 return ERR_PTR(-ENOMEM);
5109 /* get id */
5110 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
5111 error = -ENOMEM;
5112 goto err_out;
5113 }
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005114 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005115 /* Don't use 0. allocates an ID of 1-65535 */
5116 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005117 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005118
5119 /* Returns error when there are no free spaces for new ID.*/
5120 if (error) {
5121 error = -ENOSPC;
5122 goto err_out;
5123 }
5124 if (myid > CSS_ID_MAX)
5125 goto remove_idr;
5126
5127 newid->id = myid;
5128 newid->depth = depth;
5129 return newid;
5130remove_idr:
5131 error = -ENOSPC;
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005132 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005133 idr_remove(&ss->idr, myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005134 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005135err_out:
5136 kfree(newid);
5137 return ERR_PTR(error);
5138
5139}
5140
Ben Blume6a11052010-03-10 15:22:09 -08005141static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5142 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005143{
5144 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005145
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005146 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005147 idr_init(&ss->idr);
5148
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005149 newid = get_new_cssid(ss, 0);
5150 if (IS_ERR(newid))
5151 return PTR_ERR(newid);
5152
5153 newid->stack[0] = newid->id;
5154 newid->css = rootcss;
5155 rootcss->id = newid;
5156 return 0;
5157}
5158
5159static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5160 struct cgroup *child)
5161{
5162 int subsys_id, i, depth = 0;
5163 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005164 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005165
5166 subsys_id = ss->subsys_id;
5167 parent_css = parent->subsys[subsys_id];
5168 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005169 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005170 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005171
5172 child_id = get_new_cssid(ss, depth);
5173 if (IS_ERR(child_id))
5174 return PTR_ERR(child_id);
5175
5176 for (i = 0; i < depth; i++)
5177 child_id->stack[i] = parent_id->stack[i];
5178 child_id->stack[depth] = child_id->id;
5179 /*
5180 * child_id->css pointer will be set after this cgroup is available
5181 * see cgroup_populate_dir()
5182 */
5183 rcu_assign_pointer(child_css->id, child_id);
5184
5185 return 0;
5186}
5187
5188/**
5189 * css_lookup - lookup css by id
5190 * @ss: cgroup subsys to be looked into.
5191 * @id: the id
5192 *
5193 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5194 * NULL if not. Should be called under rcu_read_lock()
5195 */
5196struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5197{
5198 struct css_id *cssid = NULL;
5199
5200 BUG_ON(!ss->use_id);
5201 cssid = idr_find(&ss->idr, id);
5202
5203 if (unlikely(!cssid))
5204 return NULL;
5205
5206 return rcu_dereference(cssid->css);
5207}
Ben Blum67523c42010-03-10 15:22:11 -08005208EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005209
5210/**
5211 * css_get_next - lookup next cgroup under specified hierarchy.
5212 * @ss: pointer to subsystem
5213 * @id: current position of iteration.
5214 * @root: pointer to css. search tree under this.
5215 * @foundid: position of found object.
5216 *
5217 * Search next css under the specified hierarchy of rootid. Calling under
5218 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5219 */
5220struct cgroup_subsys_state *
5221css_get_next(struct cgroup_subsys *ss, int id,
5222 struct cgroup_subsys_state *root, int *foundid)
5223{
5224 struct cgroup_subsys_state *ret = NULL;
5225 struct css_id *tmp;
5226 int tmpid;
5227 int rootid = css_id(root);
5228 int depth = css_depth(root);
5229
5230 if (!rootid)
5231 return NULL;
5232
5233 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005234 WARN_ON_ONCE(!rcu_read_lock_held());
5235
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005236 /* fill start point for scan */
5237 tmpid = id;
5238 while (1) {
5239 /*
5240 * scan next entry from bitmap(tree), tmpid is updated after
5241 * idr_get_next().
5242 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005243 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005244 if (!tmp)
5245 break;
5246 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5247 ret = rcu_dereference(tmp->css);
5248 if (ret) {
5249 *foundid = tmpid;
5250 break;
5251 }
5252 }
5253 /* continue to scan from next id */
5254 tmpid = tmpid + 1;
5255 }
5256 return ret;
5257}
5258
Stephane Eraniane5d13672011-02-14 11:20:01 +02005259/*
5260 * get corresponding css from file open on cgroupfs directory
5261 */
5262struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5263{
5264 struct cgroup *cgrp;
5265 struct inode *inode;
5266 struct cgroup_subsys_state *css;
5267
5268 inode = f->f_dentry->d_inode;
5269 /* check in cgroup filesystem dir */
5270 if (inode->i_op != &cgroup_dir_inode_operations)
5271 return ERR_PTR(-EBADF);
5272
5273 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5274 return ERR_PTR(-EINVAL);
5275
5276 /* get cgroup */
5277 cgrp = __d_cgrp(f->f_dentry);
5278 css = cgrp->subsys[id];
5279 return css ? css : ERR_PTR(-ENOENT);
5280}
5281
Paul Menagefe693432009-09-23 15:56:20 -07005282#ifdef CONFIG_CGROUP_DEBUG
Li Zefan761b3ef2012-01-31 13:47:36 +08005283static struct cgroup_subsys_state *debug_create(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005284{
5285 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5286
5287 if (!css)
5288 return ERR_PTR(-ENOMEM);
5289
5290 return css;
5291}
5292
Li Zefan761b3ef2012-01-31 13:47:36 +08005293static void debug_destroy(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005294{
5295 kfree(cont->subsys[debug_subsys_id]);
5296}
5297
5298static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5299{
5300 return atomic_read(&cont->count);
5301}
5302
5303static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5304{
5305 return cgroup_task_count(cont);
5306}
5307
5308static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5309{
5310 return (u64)(unsigned long)current->cgroups;
5311}
5312
5313static u64 current_css_set_refcount_read(struct cgroup *cont,
5314 struct cftype *cft)
5315{
5316 u64 count;
5317
5318 rcu_read_lock();
5319 count = atomic_read(&current->cgroups->refcount);
5320 rcu_read_unlock();
5321 return count;
5322}
5323
Paul Menage7717f7b2009-09-23 15:56:22 -07005324static int current_css_set_cg_links_read(struct cgroup *cont,
5325 struct cftype *cft,
5326 struct seq_file *seq)
5327{
5328 struct cg_cgroup_link *link;
5329 struct css_set *cg;
5330
5331 read_lock(&css_set_lock);
5332 rcu_read_lock();
5333 cg = rcu_dereference(current->cgroups);
5334 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5335 struct cgroup *c = link->cgrp;
5336 const char *name;
5337
5338 if (c->dentry)
5339 name = c->dentry->d_name.name;
5340 else
5341 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005342 seq_printf(seq, "Root %d group %s\n",
5343 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005344 }
5345 rcu_read_unlock();
5346 read_unlock(&css_set_lock);
5347 return 0;
5348}
5349
5350#define MAX_TASKS_SHOWN_PER_CSS 25
5351static int cgroup_css_links_read(struct cgroup *cont,
5352 struct cftype *cft,
5353 struct seq_file *seq)
5354{
5355 struct cg_cgroup_link *link;
5356
5357 read_lock(&css_set_lock);
5358 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5359 struct css_set *cg = link->cg;
5360 struct task_struct *task;
5361 int count = 0;
5362 seq_printf(seq, "css_set %p\n", cg);
5363 list_for_each_entry(task, &cg->tasks, cg_list) {
5364 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5365 seq_puts(seq, " ...\n");
5366 break;
5367 } else {
5368 seq_printf(seq, " task %d\n",
5369 task_pid_vnr(task));
5370 }
5371 }
5372 }
5373 read_unlock(&css_set_lock);
5374 return 0;
5375}
5376
Paul Menagefe693432009-09-23 15:56:20 -07005377static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5378{
5379 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5380}
5381
5382static struct cftype debug_files[] = {
5383 {
5384 .name = "cgroup_refcount",
5385 .read_u64 = cgroup_refcount_read,
5386 },
5387 {
5388 .name = "taskcount",
5389 .read_u64 = debug_taskcount_read,
5390 },
5391
5392 {
5393 .name = "current_css_set",
5394 .read_u64 = current_css_set_read,
5395 },
5396
5397 {
5398 .name = "current_css_set_refcount",
5399 .read_u64 = current_css_set_refcount_read,
5400 },
5401
5402 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005403 .name = "current_css_set_cg_links",
5404 .read_seq_string = current_css_set_cg_links_read,
5405 },
5406
5407 {
5408 .name = "cgroup_css_links",
5409 .read_seq_string = cgroup_css_links_read,
5410 },
5411
5412 {
Paul Menagefe693432009-09-23 15:56:20 -07005413 .name = "releasable",
5414 .read_u64 = releasable_read,
5415 },
Paul Menagefe693432009-09-23 15:56:20 -07005416
Tejun Heo4baf6e32012-04-01 12:09:55 -07005417 { } /* terminate */
5418};
Paul Menagefe693432009-09-23 15:56:20 -07005419
5420struct cgroup_subsys debug_subsys = {
5421 .name = "debug",
5422 .create = debug_create,
5423 .destroy = debug_destroy,
Paul Menagefe693432009-09-23 15:56:20 -07005424 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005425 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005426};
5427#endif /* CONFIG_CGROUP_DEBUG */