blob: 2895880e6800465d82e055f447558e06c1e8722c [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 */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020063#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070064
Arun Sharma600634972011-07-26 16:09:06 -070065#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070066
Tejun Heo28b4c272012-04-01 12:09:56 -070067/* css deactivation bias, makes css->refcnt negative to deny new trygets */
68#define CSS_DEACT_BIAS INT_MIN
69
Tejun Heoe25e2cb2011-12-12 18:12:21 -080070/*
71 * cgroup_mutex is the master lock. Any modification to cgroup or its
72 * hierarchy must be performed while holding it.
73 *
74 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
75 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
76 * release_agent_path and so on. Modifying requires both cgroup_mutex and
77 * cgroup_root_mutex. Readers can acquire either of the two. This is to
78 * break the following locking order cycle.
79 *
80 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
81 * B. namespace_sem -> cgroup_mutex
82 *
83 * B happens only through cgroup_show_options() and using cgroup_root_mutex
84 * breaks it.
85 */
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -080087static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070088
Ben Blumaae8aab2010-03-10 15:22:07 -080089/*
90 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020091 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080092 * registered after that. The mutable section of this array is protected by
93 * cgroup_mutex.
94 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020095#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020096#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -080097static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070098#include <linux/cgroup_subsys.h>
99};
100
Paul Menagec6d57f32009-09-23 15:56:19 -0700101#define MAX_CGROUP_ROOT_NAMELEN 64
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
104 * A cgroupfs_root represents the root of a cgroup hierarchy,
105 * and may be associated with a superblock to form an active
106 * hierarchy
107 */
108struct cgroupfs_root {
109 struct super_block *sb;
110
111 /*
112 * The bitmask of subsystems intended to be attached to this
113 * hierarchy
114 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400115 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700116
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700117 /* Unique id for this hierarchy. */
118 int hierarchy_id;
119
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120 /* The bitmask of subsystems currently attached to this hierarchy */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400121 unsigned long actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122
123 /* A list running through the attached subsystems */
124 struct list_head subsys_list;
125
126 /* The root cgroup for this hierarchy */
127 struct cgroup top_cgroup;
128
129 /* Tracks how many cgroups are currently defined in hierarchy.*/
130 int number_of_cgroups;
131
Li Zefane5f6a862009-01-07 18:07:41 -0800132 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700133 struct list_head root_list;
134
Tejun Heob0ca5a82012-04-01 12:09:54 -0700135 /* All cgroups on this root, cgroup_mutex protected */
136 struct list_head allcg_list;
137
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138 /* Hierarchy-specific flags */
139 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700140
Paul Menagee788e062008-07-25 01:46:59 -0700141 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700142 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700143
144 /* The name for this hierarchy - may be empty */
145 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700146};
147
Paul Menageddbcc7e2007-10-18 23:39:30 -0700148/*
149 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
150 * subsystems that are otherwise unattached - it never has more than a
151 * single cgroup, and all tasks are part of that cgroup.
152 */
153static struct cgroupfs_root rootnode;
154
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700155/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700156 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
157 */
158struct cfent {
159 struct list_head node;
160 struct dentry *dentry;
161 struct cftype *type;
162};
163
164/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700165 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
166 * cgroup_subsys->use_id != 0.
167 */
168#define CSS_ID_MAX (65535)
169struct css_id {
170 /*
171 * The css to which this ID points. This pointer is set to valid value
172 * after cgroup is populated. If cgroup is removed, this will be NULL.
173 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800174 * is called after synchronize_rcu(). But for safe use, css_tryget()
175 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700176 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100177 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700178 /*
179 * ID of this css.
180 */
181 unsigned short id;
182 /*
183 * Depth in hierarchy which this ID belongs to.
184 */
185 unsigned short depth;
186 /*
187 * ID is freed by RCU. (and lookup routine is RCU safe.)
188 */
189 struct rcu_head rcu_head;
190 /*
191 * Hierarchy of CSS ID belongs to.
192 */
193 unsigned short stack[0]; /* Array of Length (depth+1) */
194};
195
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800196/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300197 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800198 */
199struct cgroup_event {
200 /*
201 * Cgroup which the event belongs to.
202 */
203 struct cgroup *cgrp;
204 /*
205 * Control file which the event associated.
206 */
207 struct cftype *cft;
208 /*
209 * eventfd to signal userspace about the event.
210 */
211 struct eventfd_ctx *eventfd;
212 /*
213 * Each of these stored in a list by the cgroup.
214 */
215 struct list_head list;
216 /*
217 * All fields below needed to unregister event when
218 * userspace closes eventfd.
219 */
220 poll_table pt;
221 wait_queue_head_t *wqh;
222 wait_queue_t wait;
223 struct work_struct remove;
224};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700225
Paul Menageddbcc7e2007-10-18 23:39:30 -0700226/* The list of hierarchy roots */
227
228static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700229static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700230
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700231static DEFINE_IDA(hierarchy_ida);
232static int next_hierarchy_id;
233static DEFINE_SPINLOCK(hierarchy_id_lock);
234
Paul Menageddbcc7e2007-10-18 23:39:30 -0700235/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
236#define dummytop (&rootnode.top_cgroup)
237
238/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800239 * check for fork/exit handlers to call. This avoids us having to do
240 * extra work in the fork/exit path if none of the subsystems need to
241 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700242 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700243static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700244
Tejun Heo42809dd2012-11-19 08:13:37 -0800245static int cgroup_destroy_locked(struct cgroup *cgrp);
246
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800247#ifdef CONFIG_PROVE_LOCKING
248int cgroup_lock_is_held(void)
249{
250 return lockdep_is_held(&cgroup_mutex);
251}
252#else /* #ifdef CONFIG_PROVE_LOCKING */
253int cgroup_lock_is_held(void)
254{
255 return mutex_is_locked(&cgroup_mutex);
256}
257#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
258
259EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
260
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700261static int css_unbias_refcnt(int refcnt)
262{
263 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
264}
265
Tejun Heo28b4c272012-04-01 12:09:56 -0700266/* the current nr of refs, always >= 0 whether @css is deactivated or not */
267static int css_refcnt(struct cgroup_subsys_state *css)
268{
269 int v = atomic_read(&css->refcnt);
270
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700271 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700272}
273
Paul Menageddbcc7e2007-10-18 23:39:30 -0700274/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700275inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700276{
Paul Menagebd89aab2007-10-18 23:40:44 -0700277 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700278}
279
280/* bits in struct cgroupfs_root flags field */
281enum {
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400282 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
283 ROOT_XATTR, /* supports extended attributes */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700284};
285
Adrian Bunke9685a02008-02-07 00:13:46 -0800286static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700287{
288 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700289 (1 << CGRP_RELEASABLE) |
290 (1 << CGRP_NOTIFY_ON_RELEASE);
291 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700292}
293
Adrian Bunke9685a02008-02-07 00:13:46 -0800294static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700295{
Paul Menagebd89aab2007-10-18 23:40:44 -0700296 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700297}
298
Paul Menageddbcc7e2007-10-18 23:39:30 -0700299/*
300 * for_each_subsys() allows you to iterate on each subsystem attached to
301 * an active hierarchy
302 */
303#define for_each_subsys(_root, _ss) \
304list_for_each_entry(_ss, &_root->subsys_list, sibling)
305
Li Zefane5f6a862009-01-07 18:07:41 -0800306/* for_each_active_root() allows you to iterate across the active hierarchies */
307#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700308list_for_each_entry(_root, &roots, root_list)
309
Tejun Heof6ea9372012-04-01 12:09:55 -0700310static inline struct cgroup *__d_cgrp(struct dentry *dentry)
311{
312 return dentry->d_fsdata;
313}
314
Tejun Heo05ef1d72012-04-01 12:09:56 -0700315static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700316{
317 return dentry->d_fsdata;
318}
319
Tejun Heo05ef1d72012-04-01 12:09:56 -0700320static inline struct cftype *__d_cft(struct dentry *dentry)
321{
322 return __d_cfe(dentry)->type;
323}
324
Paul Menage81a6a5c2007-10-18 23:39:38 -0700325/* the list of cgroups eligible for automatic release. Protected by
326 * release_list_lock */
327static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200328static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700329static void cgroup_release_agent(struct work_struct *work);
330static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700331static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700332
Paul Menage817929e2007-10-18 23:39:36 -0700333/* Link structure for associating css_set objects with cgroups */
334struct cg_cgroup_link {
335 /*
336 * List running through cg_cgroup_links associated with a
337 * cgroup, anchored on cgroup->css_sets
338 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700339 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700340 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700341 /*
342 * List running through cg_cgroup_links pointing at a
343 * single css_set object, anchored on css_set->cg_links
344 */
345 struct list_head cg_link_list;
346 struct css_set *cg;
347};
348
349/* The default css_set - used by init and its children prior to any
350 * hierarchies being mounted. It contains a pointer to the root state
351 * for each subsystem. Also used to anchor the list of css_sets. Not
352 * reference-counted, to improve performance when child cgroups
353 * haven't been created.
354 */
355
356static struct css_set init_css_set;
357static struct cg_cgroup_link init_css_set_link;
358
Ben Blume6a11052010-03-10 15:22:09 -0800359static int cgroup_init_idr(struct cgroup_subsys *ss,
360 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700361
Paul Menage817929e2007-10-18 23:39:36 -0700362/* css_set_lock protects the list of css_set objects, and the
363 * chain of tasks off each css_set. Nests outside task->alloc_lock
364 * due to cgroup_iter_start() */
365static DEFINE_RWLOCK(css_set_lock);
366static int css_set_count;
367
Paul Menage7717f7b2009-09-23 15:56:22 -0700368/*
369 * hash table for cgroup groups. This improves the performance to find
370 * an existing css_set. This hash doesn't (currently) take into
371 * account cgroups in empty hierarchies.
372 */
Li Zefan472b1052008-04-29 01:00:11 -0700373#define CSS_SET_HASH_BITS 7
374#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
375static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
376
377static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
378{
379 int i;
380 int index;
381 unsigned long tmp = 0UL;
382
383 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
384 tmp += (unsigned long)css[i];
385 tmp = (tmp >> 16) ^ tmp;
386
387 index = hash_long(tmp, CSS_SET_HASH_BITS);
388
389 return &css_set_table[index];
390}
391
Paul Menage817929e2007-10-18 23:39:36 -0700392/* We don't maintain the lists running through each css_set to its
393 * task until after the first call to cgroup_iter_start(). This
394 * reduces the fork()/exit() overhead for people who have cgroups
395 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700396static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700397
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700398static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700399{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700400 struct cg_cgroup_link *link;
401 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700402 /*
403 * Ensure that the refcount doesn't hit zero while any readers
404 * can see it. Similar to atomic_dec_and_lock(), but for an
405 * rwlock
406 */
407 if (atomic_add_unless(&cg->refcount, -1, 1))
408 return;
409 write_lock(&css_set_lock);
410 if (!atomic_dec_and_test(&cg->refcount)) {
411 write_unlock(&css_set_lock);
412 return;
413 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700414
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700415 /* This css_set is dead. unlink it and release cgroup refcounts */
416 hlist_del(&cg->hlist);
417 css_set_count--;
418
419 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
420 cg_link_list) {
421 struct cgroup *cgrp = link->cgrp;
422 list_del(&link->cg_link_list);
423 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700424 if (atomic_dec_and_test(&cgrp->count) &&
425 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700426 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700427 set_bit(CGRP_RELEASABLE, &cgrp->flags);
428 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700429 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700430
431 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700433
434 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800435 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700436}
437
438/*
439 * refcounted get/put for css_set objects
440 */
441static inline void get_css_set(struct css_set *cg)
442{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700443 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700444}
445
446static inline void put_css_set(struct css_set *cg)
447{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700448 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700449}
450
Paul Menage81a6a5c2007-10-18 23:39:38 -0700451static inline void put_css_set_taskexit(struct css_set *cg)
452{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700453 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700454}
455
Paul Menage817929e2007-10-18 23:39:36 -0700456/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700457 * compare_css_sets - helper function for find_existing_css_set().
458 * @cg: candidate css_set being tested
459 * @old_cg: existing css_set for a task
460 * @new_cgrp: cgroup that's being entered by the task
461 * @template: desired set of css pointers in css_set (pre-calculated)
462 *
463 * Returns true if "cg" matches "old_cg" except for the hierarchy
464 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
465 */
466static bool compare_css_sets(struct css_set *cg,
467 struct css_set *old_cg,
468 struct cgroup *new_cgrp,
469 struct cgroup_subsys_state *template[])
470{
471 struct list_head *l1, *l2;
472
473 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
474 /* Not all subsystems matched */
475 return false;
476 }
477
478 /*
479 * Compare cgroup pointers in order to distinguish between
480 * different cgroups in heirarchies with no subsystems. We
481 * could get by with just this check alone (and skip the
482 * memcmp above) but on most setups the memcmp check will
483 * avoid the need for this more expensive check on almost all
484 * candidates.
485 */
486
487 l1 = &cg->cg_links;
488 l2 = &old_cg->cg_links;
489 while (1) {
490 struct cg_cgroup_link *cgl1, *cgl2;
491 struct cgroup *cg1, *cg2;
492
493 l1 = l1->next;
494 l2 = l2->next;
495 /* See if we reached the end - both lists are equal length. */
496 if (l1 == &cg->cg_links) {
497 BUG_ON(l2 != &old_cg->cg_links);
498 break;
499 } else {
500 BUG_ON(l2 == &old_cg->cg_links);
501 }
502 /* Locate the cgroups associated with these links. */
503 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
504 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
505 cg1 = cgl1->cgrp;
506 cg2 = cgl2->cgrp;
507 /* Hierarchies should be linked in the same order. */
508 BUG_ON(cg1->root != cg2->root);
509
510 /*
511 * If this hierarchy is the hierarchy of the cgroup
512 * that's changing, then we need to check that this
513 * css_set points to the new cgroup; if it's any other
514 * hierarchy, then this css_set should point to the
515 * same cgroup as the old css_set.
516 */
517 if (cg1->root == new_cgrp->root) {
518 if (cg1 != new_cgrp)
519 return false;
520 } else {
521 if (cg1 != cg2)
522 return false;
523 }
524 }
525 return true;
526}
527
528/*
Paul Menage817929e2007-10-18 23:39:36 -0700529 * find_existing_css_set() is a helper for
530 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700531 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700532 *
533 * oldcg: the cgroup group that we're using before the cgroup
534 * transition
535 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700536 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700537 *
538 * template: location in which to build the desired set of subsystem
539 * state objects for the new cgroup group
540 */
Paul Menage817929e2007-10-18 23:39:36 -0700541static struct css_set *find_existing_css_set(
542 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700543 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700544 struct cgroup_subsys_state *template[])
545{
546 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700547 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700548 struct hlist_head *hhead;
549 struct hlist_node *node;
550 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700551
Ben Blumaae8aab2010-03-10 15:22:07 -0800552 /*
553 * Build the set of subsystem state objects that we want to see in the
554 * new css_set. while subsystems can change globally, the entries here
555 * won't change, so no need for locking.
556 */
Paul Menage817929e2007-10-18 23:39:36 -0700557 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400558 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700559 /* Subsystem is in this hierarchy. So we want
560 * the subsystem state from the new
561 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700562 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700563 } else {
564 /* Subsystem is not in this hierarchy, so we
565 * don't want to change the subsystem state */
566 template[i] = oldcg->subsys[i];
567 }
568 }
569
Li Zefan472b1052008-04-29 01:00:11 -0700570 hhead = css_set_hash(template);
571 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700572 if (!compare_css_sets(cg, oldcg, cgrp, template))
573 continue;
574
575 /* This css_set matches what we need */
576 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700577 }
Paul Menage817929e2007-10-18 23:39:36 -0700578
579 /* No existing cgroup group matched */
580 return NULL;
581}
582
Paul Menage817929e2007-10-18 23:39:36 -0700583static void free_cg_links(struct list_head *tmp)
584{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700585 struct cg_cgroup_link *link;
586 struct cg_cgroup_link *saved_link;
587
588 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700589 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700590 kfree(link);
591 }
592}
593
594/*
Li Zefan36553432008-07-29 22:33:19 -0700595 * allocate_cg_links() allocates "count" cg_cgroup_link structures
596 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
597 * success or a negative error
598 */
599static int allocate_cg_links(int count, struct list_head *tmp)
600{
601 struct cg_cgroup_link *link;
602 int i;
603 INIT_LIST_HEAD(tmp);
604 for (i = 0; i < count; i++) {
605 link = kmalloc(sizeof(*link), GFP_KERNEL);
606 if (!link) {
607 free_cg_links(tmp);
608 return -ENOMEM;
609 }
610 list_add(&link->cgrp_link_list, tmp);
611 }
612 return 0;
613}
614
Li Zefanc12f65d2009-01-07 18:07:42 -0800615/**
616 * link_css_set - a helper function to link a css_set to a cgroup
617 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
618 * @cg: the css_set to be linked
619 * @cgrp: the destination cgroup
620 */
621static void link_css_set(struct list_head *tmp_cg_links,
622 struct css_set *cg, struct cgroup *cgrp)
623{
624 struct cg_cgroup_link *link;
625
626 BUG_ON(list_empty(tmp_cg_links));
627 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
628 cgrp_link_list);
629 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700630 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700631 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800632 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700633 /*
634 * Always add links to the tail of the list so that the list
635 * is sorted by order of hierarchy creation
636 */
637 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800638}
639
Li Zefan36553432008-07-29 22:33:19 -0700640/*
Paul Menage817929e2007-10-18 23:39:36 -0700641 * find_css_set() takes an existing cgroup group and a
642 * cgroup object, and returns a css_set object that's
643 * equivalent to the old group, but with the given cgroup
644 * substituted into the appropriate hierarchy. Must be called with
645 * cgroup_mutex held
646 */
Paul Menage817929e2007-10-18 23:39:36 -0700647static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700648 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700649{
650 struct css_set *res;
651 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700652
653 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700654
Li Zefan472b1052008-04-29 01:00:11 -0700655 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700656 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700657
Paul Menage817929e2007-10-18 23:39:36 -0700658 /* First see if we already have a cgroup group that matches
659 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700660 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700661 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700662 if (res)
663 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700664 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700665
666 if (res)
667 return res;
668
669 res = kmalloc(sizeof(*res), GFP_KERNEL);
670 if (!res)
671 return NULL;
672
673 /* Allocate all the cg_cgroup_link objects that we'll need */
674 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
675 kfree(res);
676 return NULL;
677 }
678
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700679 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700680 INIT_LIST_HEAD(&res->cg_links);
681 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700682 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700683
684 /* Copy the set of subsystem state objects generated in
685 * find_existing_css_set() */
686 memcpy(res->subsys, template, sizeof(res->subsys));
687
688 write_lock(&css_set_lock);
689 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700690 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
691 struct cgroup *c = link->cgrp;
692 if (c->root == cgrp->root)
693 c = cgrp;
694 link_css_set(&tmp_cg_links, res, c);
695 }
Paul Menage817929e2007-10-18 23:39:36 -0700696
697 BUG_ON(!list_empty(&tmp_cg_links));
698
Paul Menage817929e2007-10-18 23:39:36 -0700699 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700700
701 /* Add this cgroup group to the hash table */
702 hhead = css_set_hash(res->subsys);
703 hlist_add_head(&res->hlist, hhead);
704
Paul Menage817929e2007-10-18 23:39:36 -0700705 write_unlock(&css_set_lock);
706
707 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700708}
709
Paul Menageddbcc7e2007-10-18 23:39:30 -0700710/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700711 * Return the cgroup for "task" from the given hierarchy. Must be
712 * called with cgroup_mutex held.
713 */
714static struct cgroup *task_cgroup_from_root(struct task_struct *task,
715 struct cgroupfs_root *root)
716{
717 struct css_set *css;
718 struct cgroup *res = NULL;
719
720 BUG_ON(!mutex_is_locked(&cgroup_mutex));
721 read_lock(&css_set_lock);
722 /*
723 * No need to lock the task - since we hold cgroup_mutex the
724 * task can't change groups, so the only thing that can happen
725 * is that it exits and its css is set back to init_css_set.
726 */
727 css = task->cgroups;
728 if (css == &init_css_set) {
729 res = &root->top_cgroup;
730 } else {
731 struct cg_cgroup_link *link;
732 list_for_each_entry(link, &css->cg_links, cg_link_list) {
733 struct cgroup *c = link->cgrp;
734 if (c->root == root) {
735 res = c;
736 break;
737 }
738 }
739 }
740 read_unlock(&css_set_lock);
741 BUG_ON(!res);
742 return res;
743}
744
745/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700746 * There is one global cgroup mutex. We also require taking
747 * task_lock() when dereferencing a task's cgroup subsys pointers.
748 * See "The task_lock() exception", at the end of this comment.
749 *
750 * A task must hold cgroup_mutex to modify cgroups.
751 *
752 * Any task can increment and decrement the count field without lock.
753 * So in general, code holding cgroup_mutex can't rely on the count
754 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800755 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700756 * means that no tasks are currently attached, therefore there is no
757 * way a task attached to that cgroup can fork (the other way to
758 * increment the count). So code holding cgroup_mutex can safely
759 * assume that if the count is zero, it will stay zero. Similarly, if
760 * a task holds cgroup_mutex on a cgroup with zero count, it
761 * knows that the cgroup won't be removed, as cgroup_rmdir()
762 * needs that mutex.
763 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700764 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
765 * (usually) take cgroup_mutex. These are the two most performance
766 * critical pieces of code here. The exception occurs on cgroup_exit(),
767 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
768 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800769 * to the release agent with the name of the cgroup (path relative to
770 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700771 *
772 * A cgroup can only be deleted if both its 'count' of using tasks
773 * is zero, and its list of 'children' cgroups is empty. Since all
774 * tasks in the system use _some_ cgroup, and since there is always at
775 * least one task in the system (init, pid == 1), therefore, top_cgroup
776 * always has either children cgroups and/or using tasks. So we don't
777 * need a special hack to ensure that top_cgroup cannot be deleted.
778 *
779 * The task_lock() exception
780 *
781 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800782 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800783 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700784 * several performance critical places that need to reference
785 * task->cgroup without the expense of grabbing a system global
786 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800787 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
789 * the task_struct routinely used for such matters.
790 *
791 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800792 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793 */
794
Paul Menageddbcc7e2007-10-18 23:39:30 -0700795/**
796 * cgroup_lock - lock out any changes to cgroup structures
797 *
798 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799void cgroup_lock(void)
800{
801 mutex_lock(&cgroup_mutex);
802}
Ben Blum67523c42010-03-10 15:22:11 -0800803EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700804
805/**
806 * cgroup_unlock - release lock on cgroup changes
807 *
808 * Undo the lock taken in a previous cgroup_lock() call.
809 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700810void cgroup_unlock(void)
811{
812 mutex_unlock(&cgroup_mutex);
813}
Ben Blum67523c42010-03-10 15:22:11 -0800814EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815
816/*
817 * A couple of forward declarations required, due to cyclic reference loop:
818 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
819 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
820 * -> cgroup_mkdir.
821 */
822
Al Viro18bb1db2011-07-26 01:41:39 -0400823static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400824static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700825static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400826static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
827 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700828static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700829static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700830
831static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200832 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700833 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700834};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700835
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700836static int alloc_css_id(struct cgroup_subsys *ss,
837 struct cgroup *parent, struct cgroup *child);
838
Al Viroa5e7ed32011-07-26 01:55:55 -0400839static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700840{
841 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700842
843 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400844 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700845 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100846 inode->i_uid = current_fsuid();
847 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700848 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
849 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
850 }
851 return inode;
852}
853
854static void cgroup_diput(struct dentry *dentry, struct inode *inode)
855{
856 /* is dentry a directory ? if so, kfree() associated cgroup */
857 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700858 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800859 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700860 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700861 /* It's possible for external users to be holding css
862 * reference counts on a cgroup; css_put() needs to
863 * be able to access the cgroup after decrementing
864 * the reference count in order to know if it needs to
865 * queue the cgroup to be handled by the release
866 * agent */
867 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800868
869 mutex_lock(&cgroup_mutex);
870 /*
871 * Release the subsystem state objects.
872 */
Li Zefan75139b82009-01-07 18:07:33 -0800873 for_each_subsys(cgrp->root, ss)
Tejun Heo92fb9742012-11-19 08:13:38 -0800874 ss->css_free(cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800875
876 cgrp->root->number_of_cgroups--;
877 mutex_unlock(&cgroup_mutex);
878
Paul Menagea47295e2009-01-07 18:07:44 -0800879 /*
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700880 * Drop the active superblock reference that we took when we
881 * created the cgroup
Paul Menagea47295e2009-01-07 18:07:44 -0800882 */
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700883 deactivate_super(cgrp->root->sb);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800884
Ben Blum72a8cb32009-09-23 15:56:27 -0700885 /*
886 * if we're getting rid of the cgroup, refcount should ensure
887 * that there are no pidlists left.
888 */
889 BUG_ON(!list_empty(&cgrp->pidlists));
890
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400891 simple_xattrs_free(&cgrp->xattrs);
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;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400897 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700898
899 WARN_ONCE(!list_empty(&cfe->node) &&
900 cgrp != &cgrp->root->top_cgroup,
901 "cfe still linked for %s\n", cfe->type->name);
902 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400903 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700904 }
905 iput(inode);
906}
907
Al Viroc72a04e2011-01-14 05:31:45 +0000908static int cgroup_delete(const struct dentry *d)
909{
910 return 1;
911}
912
Paul Menageddbcc7e2007-10-18 23:39:30 -0700913static void remove_dir(struct dentry *d)
914{
915 struct dentry *parent = dget(d->d_parent);
916
917 d_delete(d);
918 simple_rmdir(parent->d_inode, d);
919 dput(parent);
920}
921
Tejun Heo05ef1d72012-04-01 12:09:56 -0700922static int cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700923{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700924 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700925
Tejun Heo05ef1d72012-04-01 12:09:56 -0700926 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
927 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100928
Tejun Heo05ef1d72012-04-01 12:09:56 -0700929 list_for_each_entry(cfe, &cgrp->files, node) {
930 struct dentry *d = cfe->dentry;
931
932 if (cft && cfe->type != cft)
933 continue;
934
935 dget(d);
936 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700937 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700938 list_del_init(&cfe->node);
939 dput(d);
940
941 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700942 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700943 return -ENOENT;
944}
945
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400946/**
947 * cgroup_clear_directory - selective removal of base and subsystem files
948 * @dir: directory containing the files
949 * @base_files: true if the base files should be removed
950 * @subsys_mask: mask of the subsystem ids whose files should be removed
951 */
952static void cgroup_clear_directory(struct dentry *dir, bool base_files,
953 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700954{
955 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400956 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700957
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400958 for_each_subsys(cgrp->root, ss) {
959 struct cftype_set *set;
960 if (!test_bit(ss->subsys_id, &subsys_mask))
961 continue;
962 list_for_each_entry(set, &ss->cftsets, node)
963 cgroup_rm_file(cgrp, set->cfts);
964 }
965 if (base_files) {
966 while (!list_empty(&cgrp->files))
967 cgroup_rm_file(cgrp, NULL);
968 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700969}
970
971/*
972 * NOTE : the dentry must have been dget()'ed
973 */
974static void cgroup_d_remove_dir(struct dentry *dentry)
975{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100976 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400977 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100978
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400979 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100981 parent = dentry->d_parent;
982 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800983 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100985 spin_unlock(&dentry->d_lock);
986 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987 remove_dir(dentry);
988}
989
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700990/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800991 * Call with cgroup_mutex held. Drops reference counts on modules, including
992 * any duplicate ones that parse_cgroupfs_options took. If this function
993 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800994 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400996 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400998 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -0700999 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000 int i;
1001
Ben Blumaae8aab2010-03-10 15:22:07 -08001002 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001003 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001004
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001005 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1006 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007 /* Check that any added subsystems are currently free */
1008 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001009 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001011 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001013 /*
1014 * Nobody should tell us to do a subsys that doesn't exist:
1015 * parse_cgroupfs_options should catch that case and refcounts
1016 * ensure that subsystems won't disappear once selected.
1017 */
1018 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001019 if (ss->root != &rootnode) {
1020 /* Subsystem isn't free */
1021 return -EBUSY;
1022 }
1023 }
1024
1025 /* Currently we don't handle adding/removing subsystems when
1026 * any child cgroups exist. This is theoretically supportable
1027 * but involves complex error handling, so it's being left until
1028 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001029 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030 return -EBUSY;
1031
1032 /* Process each subsystem */
1033 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1034 struct cgroup_subsys *ss = subsys[i];
1035 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001036 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001037 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001038 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001039 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001040 BUG_ON(!dummytop->subsys[i]);
1041 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001042 cgrp->subsys[i] = dummytop->subsys[i];
1043 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001044 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001045 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001047 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001048 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001049 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001050 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001051 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001052 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1053 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001055 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001057 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001058 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001059 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001060 /* subsystem is now free - drop reference on module */
1061 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001062 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001064 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001065 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001066 /*
1067 * a refcount was taken, but we already had one, so
1068 * drop the extra reference.
1069 */
1070 module_put(ss->module);
1071#ifdef CONFIG_MODULE_UNLOAD
1072 BUG_ON(ss->module && !module_refcount(ss->module));
1073#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074 } else {
1075 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001076 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077 }
1078 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001079 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080 synchronize_rcu();
1081
1082 return 0;
1083}
1084
Al Viro34c80b12011-12-08 21:32:45 -05001085static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086{
Al Viro34c80b12011-12-08 21:32:45 -05001087 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 struct cgroup_subsys *ss;
1089
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001090 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091 for_each_subsys(root, ss)
1092 seq_printf(seq, ",%s", ss->name);
1093 if (test_bit(ROOT_NOPREFIX, &root->flags))
1094 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001095 if (test_bit(ROOT_XATTR, &root->flags))
1096 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001097 if (strlen(root->release_agent_path))
1098 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001099 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001100 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001101 if (strlen(root->name))
1102 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001103 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104 return 0;
1105}
1106
1107struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001108 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001110 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001111 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001112 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001113 /* User explicitly requested empty subsystem */
1114 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001115
1116 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001117
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118};
1119
Ben Blumaae8aab2010-03-10 15:22:07 -08001120/*
1121 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001122 * with cgroup_mutex held to protect the subsys[] array. This function takes
1123 * refcounts on subsystems to be used, unless it returns error, in which case
1124 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001125 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001126static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001128 char *token, *o = data;
1129 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001130 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001131 int i;
1132 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001133
Ben Blumaae8aab2010-03-10 15:22:07 -08001134 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1135
Li Zefanf9ab5b52009-06-17 16:26:33 -07001136#ifdef CONFIG_CPUSETS
1137 mask = ~(1UL << cpuset_subsys_id);
1138#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001139
Paul Menagec6d57f32009-09-23 15:56:19 -07001140 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001141
1142 while ((token = strsep(&o, ",")) != NULL) {
1143 if (!*token)
1144 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001145 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001146 /* Explicitly have no subsystems */
1147 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001148 continue;
1149 }
1150 if (!strcmp(token, "all")) {
1151 /* Mutually exclusive option 'all' + subsystem name */
1152 if (one_ss)
1153 return -EINVAL;
1154 all_ss = true;
1155 continue;
1156 }
1157 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001158 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001159 continue;
1160 }
1161 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001162 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001163 continue;
1164 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001165 if (!strcmp(token, "xattr")) {
1166 set_bit(ROOT_XATTR, &opts->flags);
1167 continue;
1168 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001169 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001170 /* Specifying two release agents is forbidden */
1171 if (opts->release_agent)
1172 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001173 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001174 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001175 if (!opts->release_agent)
1176 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001177 continue;
1178 }
1179 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001180 const char *name = token + 5;
1181 /* Can't specify an empty name */
1182 if (!strlen(name))
1183 return -EINVAL;
1184 /* Must match [\w.-]+ */
1185 for (i = 0; i < strlen(name); i++) {
1186 char c = name[i];
1187 if (isalnum(c))
1188 continue;
1189 if ((c == '.') || (c == '-') || (c == '_'))
1190 continue;
1191 return -EINVAL;
1192 }
1193 /* Specifying two names is forbidden */
1194 if (opts->name)
1195 return -EINVAL;
1196 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001197 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001198 GFP_KERNEL);
1199 if (!opts->name)
1200 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001201
1202 continue;
1203 }
1204
1205 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1206 struct cgroup_subsys *ss = subsys[i];
1207 if (ss == NULL)
1208 continue;
1209 if (strcmp(token, ss->name))
1210 continue;
1211 if (ss->disabled)
1212 continue;
1213
1214 /* Mutually exclusive option 'all' + subsystem name */
1215 if (all_ss)
1216 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001217 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001218 one_ss = true;
1219
1220 break;
1221 }
1222 if (i == CGROUP_SUBSYS_COUNT)
1223 return -ENOENT;
1224 }
1225
1226 /*
1227 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001228 * otherwise if 'none', 'name=' and a subsystem name options
1229 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001230 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001231 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001232 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1233 struct cgroup_subsys *ss = subsys[i];
1234 if (ss == NULL)
1235 continue;
1236 if (ss->disabled)
1237 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001238 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001239 }
1240 }
1241
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001242 /* Consistency checks */
1243
Li Zefanf9ab5b52009-06-17 16:26:33 -07001244 /*
1245 * Option noprefix was introduced just for backward compatibility
1246 * with the old cpuset, so we allow noprefix only if mounting just
1247 * the cpuset subsystem.
1248 */
1249 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001250 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001251 return -EINVAL;
1252
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001253
1254 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001255 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001256 return -EINVAL;
1257
1258 /*
1259 * We either have to specify by name or by subsystems. (So all
1260 * empty hierarchies must have a name).
1261 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001262 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001263 return -EINVAL;
1264
Ben Blumcf5d5942010-03-10 15:22:09 -08001265 /*
1266 * Grab references on all the modules we'll need, so the subsystems
1267 * don't dance around before rebind_subsystems attaches them. This may
1268 * take duplicate reference counts on a subsystem that's already used,
1269 * but rebind_subsystems handles this case.
1270 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001271 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001272 unsigned long bit = 1UL << i;
1273
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001274 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001275 continue;
1276 if (!try_module_get(subsys[i]->module)) {
1277 module_pin_failed = true;
1278 break;
1279 }
1280 }
1281 if (module_pin_failed) {
1282 /*
1283 * oops, one of the modules was going away. this means that we
1284 * raced with a module_delete call, and to the user this is
1285 * essentially a "subsystem doesn't exist" case.
1286 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001287 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001288 /* drop refcounts only on the ones we took */
1289 unsigned long bit = 1UL << i;
1290
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001291 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001292 continue;
1293 module_put(subsys[i]->module);
1294 }
1295 return -ENOENT;
1296 }
1297
Paul Menageddbcc7e2007-10-18 23:39:30 -07001298 return 0;
1299}
1300
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001301static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001302{
1303 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001304 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001305 unsigned long bit = 1UL << i;
1306
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001307 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001308 continue;
1309 module_put(subsys[i]->module);
1310 }
1311}
1312
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1314{
1315 int ret = 0;
1316 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001317 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001318 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001319 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001320
Paul Menagebd89aab2007-10-18 23:40:44 -07001321 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001323 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324
1325 /* See what subsystems are wanted */
1326 ret = parse_cgroupfs_options(data, &opts);
1327 if (ret)
1328 goto out_unlock;
1329
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001330 /* See feature-removal-schedule.txt */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001331 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001332 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1333 task_tgid_nr(current), current->comm);
1334
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001335 added_mask = opts.subsys_mask & ~root->subsys_mask;
1336 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001337
Ben Blumcf5d5942010-03-10 15:22:09 -08001338 /* Don't allow flags or name to change at remount */
1339 if (opts.flags != root->flags ||
1340 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001341 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001342 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001343 goto out_unlock;
1344 }
1345
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001346 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001347 if (ret) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001348 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001349 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001350 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001351
Tejun Heoff4c8d52012-04-01 12:09:54 -07001352 /* clear out any existing files and repopulate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001353 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001354 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001355 cgroup_populate_dir(cgrp, false, added_mask);
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);
Tejun Heo22430762012-11-19 08:13:35 -08001381 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001382 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001383 INIT_LIST_HEAD(&cgrp->pidlists);
1384 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001385 INIT_LIST_HEAD(&cgrp->event_list);
1386 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001387 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001388}
Paul Menagec6d57f32009-09-23 15:56:19 -07001389
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390static void init_cgroup_root(struct cgroupfs_root *root)
1391{
Paul Menagebd89aab2007-10-18 23:40:44 -07001392 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001393
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394 INIT_LIST_HEAD(&root->subsys_list);
1395 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001396 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001397 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001398 cgrp->root = root;
1399 cgrp->top_cgroup = cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001400 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menagecc31edc2008-10-18 20:28:04 -07001401 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001402}
1403
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001404static bool init_root_id(struct cgroupfs_root *root)
1405{
1406 int ret = 0;
1407
1408 do {
1409 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1410 return false;
1411 spin_lock(&hierarchy_id_lock);
1412 /* Try to allocate the next unused ID */
1413 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1414 &root->hierarchy_id);
1415 if (ret == -ENOSPC)
1416 /* Try again starting from 0 */
1417 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1418 if (!ret) {
1419 next_hierarchy_id = root->hierarchy_id + 1;
1420 } else if (ret != -EAGAIN) {
1421 /* Can only get here if the 31-bit IDR is full ... */
1422 BUG_ON(ret);
1423 }
1424 spin_unlock(&hierarchy_id_lock);
1425 } while (ret);
1426 return true;
1427}
1428
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429static int cgroup_test_super(struct super_block *sb, void *data)
1430{
Paul Menagec6d57f32009-09-23 15:56:19 -07001431 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001432 struct cgroupfs_root *root = sb->s_fs_info;
1433
Paul Menagec6d57f32009-09-23 15:56:19 -07001434 /* If we asked for a name then it must match */
1435 if (opts->name && strcmp(opts->name, root->name))
1436 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001437
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001438 /*
1439 * If we asked for subsystems (or explicitly for no
1440 * subsystems) then they must match
1441 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001442 if ((opts->subsys_mask || opts->none)
1443 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001444 return 0;
1445
1446 return 1;
1447}
1448
Paul Menagec6d57f32009-09-23 15:56:19 -07001449static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1450{
1451 struct cgroupfs_root *root;
1452
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001453 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001454 return NULL;
1455
1456 root = kzalloc(sizeof(*root), GFP_KERNEL);
1457 if (!root)
1458 return ERR_PTR(-ENOMEM);
1459
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001460 if (!init_root_id(root)) {
1461 kfree(root);
1462 return ERR_PTR(-ENOMEM);
1463 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001464 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001465
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001466 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001467 root->flags = opts->flags;
1468 if (opts->release_agent)
1469 strcpy(root->release_agent_path, opts->release_agent);
1470 if (opts->name)
1471 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001472 if (opts->cpuset_clone_children)
1473 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001474 return root;
1475}
1476
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001477static void cgroup_drop_root(struct cgroupfs_root *root)
1478{
1479 if (!root)
1480 return;
1481
1482 BUG_ON(!root->hierarchy_id);
1483 spin_lock(&hierarchy_id_lock);
1484 ida_remove(&hierarchy_ida, root->hierarchy_id);
1485 spin_unlock(&hierarchy_id_lock);
1486 kfree(root);
1487}
1488
Paul Menageddbcc7e2007-10-18 23:39:30 -07001489static int cgroup_set_super(struct super_block *sb, void *data)
1490{
1491 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001492 struct cgroup_sb_opts *opts = data;
1493
1494 /* If we don't have a new root, we can't set up a new sb */
1495 if (!opts->new_root)
1496 return -EINVAL;
1497
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001498 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001499
1500 ret = set_anon_super(sb, NULL);
1501 if (ret)
1502 return ret;
1503
Paul Menagec6d57f32009-09-23 15:56:19 -07001504 sb->s_fs_info = opts->new_root;
1505 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001506
1507 sb->s_blocksize = PAGE_CACHE_SIZE;
1508 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1509 sb->s_magic = CGROUP_SUPER_MAGIC;
1510 sb->s_op = &cgroup_ops;
1511
1512 return 0;
1513}
1514
1515static int cgroup_get_rootdir(struct super_block *sb)
1516{
Al Viro0df6a632010-12-21 13:29:29 -05001517 static const struct dentry_operations cgroup_dops = {
1518 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001519 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001520 };
1521
Paul Menageddbcc7e2007-10-18 23:39:30 -07001522 struct inode *inode =
1523 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001524
1525 if (!inode)
1526 return -ENOMEM;
1527
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528 inode->i_fop = &simple_dir_operations;
1529 inode->i_op = &cgroup_dir_inode_operations;
1530 /* directories start off with i_nlink == 2 (for "." entry) */
1531 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001532 sb->s_root = d_make_root(inode);
1533 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001534 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001535 /* for everything else we want ->d_op set */
1536 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537 return 0;
1538}
1539
Al Virof7e83572010-07-26 13:23:11 +04001540static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001542 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543{
1544 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001545 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 int ret = 0;
1547 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001548 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001549 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001550
1551 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001552 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001554 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001555 if (ret)
1556 goto out_err;
1557
1558 /*
1559 * Allocate a new cgroup root. We may not need it if we're
1560 * reusing an existing hierarchy.
1561 */
1562 new_root = cgroup_root_from_opts(&opts);
1563 if (IS_ERR(new_root)) {
1564 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001565 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001566 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001567 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568
Paul Menagec6d57f32009-09-23 15:56:19 -07001569 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001570 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001572 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001573 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001574 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575 }
1576
Paul Menagec6d57f32009-09-23 15:56:19 -07001577 root = sb->s_fs_info;
1578 BUG_ON(!root);
1579 if (root == opts.new_root) {
1580 /* We used the new root structure, so this is a new hierarchy */
1581 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001582 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001583 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001584 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001585 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586
1587 BUG_ON(sb->s_root != NULL);
1588
1589 ret = cgroup_get_rootdir(sb);
1590 if (ret)
1591 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001592 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001593
Paul Menage817929e2007-10-18 23:39:36 -07001594 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001596 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001597
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001598 /* Check for name clashes with existing mounts */
1599 ret = -EBUSY;
1600 if (strlen(root->name))
1601 for_each_active_root(existing_root)
1602 if (!strcmp(existing_root->name, root->name))
1603 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001604
Paul Menage817929e2007-10-18 23:39:36 -07001605 /*
1606 * We're accessing css_set_count without locking
1607 * css_set_lock here, but that's OK - it can only be
1608 * increased by someone holding cgroup_lock, and
1609 * that's us. The worst that can happen is that we
1610 * have some link structures left over
1611 */
1612 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001613 if (ret)
1614 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001615
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001616 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001617 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001618 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001619 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001620 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001621 /*
1622 * There must be no failure case after here, since rebinding
1623 * takes care of subsystems' refcounts, which are explicitly
1624 * dropped in the failure exit path.
1625 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626
1627 /* EBUSY should be the only error here */
1628 BUG_ON(ret);
1629
1630 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001631 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632
Li Zefanc12f65d2009-01-07 18:07:42 -08001633 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001634 root->top_cgroup.dentry = sb->s_root;
1635
Paul Menage817929e2007-10-18 23:39:36 -07001636 /* Link the top cgroup in this hierarchy into all
1637 * the css_set objects */
1638 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001639 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1640 struct hlist_head *hhead = &css_set_table[i];
1641 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001642 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001643
Li Zefanc12f65d2009-01-07 18:07:42 -08001644 hlist_for_each_entry(cg, node, hhead, hlist)
1645 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001646 }
Paul Menage817929e2007-10-18 23:39:36 -07001647 write_unlock(&css_set_lock);
1648
1649 free_cg_links(&tmp_cg_links);
1650
Li Zefanc12f65d2009-01-07 18:07:42 -08001651 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652 BUG_ON(root->number_of_cgroups != 1);
1653
eparis@redhat2ce97382011-06-02 21:20:51 +10001654 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001655 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001656 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001657 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001658 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001659 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001660 } else {
1661 /*
1662 * We re-used an existing hierarchy - the new root (if
1663 * any) is not needed
1664 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001665 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001666 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001667 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001668 }
1669
Paul Menagec6d57f32009-09-23 15:56:19 -07001670 kfree(opts.release_agent);
1671 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001672 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001674 unlock_drop:
1675 mutex_unlock(&cgroup_root_mutex);
1676 mutex_unlock(&cgroup_mutex);
1677 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001678 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001679 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001680 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001681 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001682 out_err:
1683 kfree(opts.release_agent);
1684 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001685 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001686}
1687
1688static void cgroup_kill_sb(struct super_block *sb) {
1689 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001690 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001691 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001692 struct cg_cgroup_link *link;
1693 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001694
1695 BUG_ON(!root);
1696
1697 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001698 BUG_ON(!list_empty(&cgrp->children));
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
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001730 simple_xattrs_free(&cgrp->xattrs);
1731
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001733 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001734}
1735
1736static struct file_system_type cgroup_fs_type = {
1737 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001738 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739 .kill_sb = cgroup_kill_sb,
1740};
1741
Greg KH676db4a2010-08-05 13:53:35 -07001742static struct kobject *cgroup_kobj;
1743
Li Zefana043e3b2008-02-23 15:24:09 -08001744/**
1745 * cgroup_path - generate the path of a cgroup
1746 * @cgrp: the cgroup in question
1747 * @buf: the buffer to write the path into
1748 * @buflen: the length of the buffer
1749 *
Paul Menagea47295e2009-01-07 18:07:44 -08001750 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1751 * reference. Writes path of cgroup into buf. Returns 0 on success,
1752 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001754int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001755{
Tejun Heofebfcef2012-11-19 08:13:36 -08001756 struct dentry *dentry = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001758
1759 rcu_lockdep_assert(rcu_read_lock_held() || cgroup_lock_is_held(),
1760 "cgroup_path() called without proper locking");
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761
Paul Menagea47295e2009-01-07 18:07:44 -08001762 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001763 /*
1764 * Inactive subsystems have no dentry for their root
1765 * cgroup
1766 */
1767 strcpy(buf, "/");
1768 return 0;
1769 }
1770
Tao Ma316eb662012-11-08 21:36:38 +08001771 start = buf + buflen - 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772
Tao Ma316eb662012-11-08 21:36:38 +08001773 *start = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001775 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001776
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777 if ((start -= len) < buf)
1778 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001779 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001780 cgrp = cgrp->parent;
1781 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001783
Tejun Heofebfcef2012-11-19 08:13:36 -08001784 dentry = cgrp->dentry;
Paul Menagebd89aab2007-10-18 23:40:44 -07001785 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786 continue;
1787 if (--start < buf)
1788 return -ENAMETOOLONG;
1789 *start = '/';
1790 }
1791 memmove(buf, start, buf + buflen - start);
1792 return 0;
1793}
Ben Blum67523c42010-03-10 15:22:11 -08001794EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795
Ben Blum74a11662011-05-26 16:25:20 -07001796/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001797 * Control Group taskset
1798 */
Tejun Heo134d3372011-12-12 18:12:21 -08001799struct task_and_cgroup {
1800 struct task_struct *task;
1801 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001802 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001803};
1804
Tejun Heo2f7ee562011-12-12 18:12:21 -08001805struct cgroup_taskset {
1806 struct task_and_cgroup single;
1807 struct flex_array *tc_array;
1808 int tc_array_len;
1809 int idx;
1810 struct cgroup *cur_cgrp;
1811};
1812
1813/**
1814 * cgroup_taskset_first - reset taskset and return the first task
1815 * @tset: taskset of interest
1816 *
1817 * @tset iteration is initialized and the first task is returned.
1818 */
1819struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1820{
1821 if (tset->tc_array) {
1822 tset->idx = 0;
1823 return cgroup_taskset_next(tset);
1824 } else {
1825 tset->cur_cgrp = tset->single.cgrp;
1826 return tset->single.task;
1827 }
1828}
1829EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1830
1831/**
1832 * cgroup_taskset_next - iterate to the next task in taskset
1833 * @tset: taskset of interest
1834 *
1835 * Return the next task in @tset. Iteration must have been initialized
1836 * with cgroup_taskset_first().
1837 */
1838struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1839{
1840 struct task_and_cgroup *tc;
1841
1842 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1843 return NULL;
1844
1845 tc = flex_array_get(tset->tc_array, tset->idx++);
1846 tset->cur_cgrp = tc->cgrp;
1847 return tc->task;
1848}
1849EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1850
1851/**
1852 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1853 * @tset: taskset of interest
1854 *
1855 * Return the cgroup for the current (last returned) task of @tset. This
1856 * function must be preceded by either cgroup_taskset_first() or
1857 * cgroup_taskset_next().
1858 */
1859struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1860{
1861 return tset->cur_cgrp;
1862}
1863EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1864
1865/**
1866 * cgroup_taskset_size - return the number of tasks in taskset
1867 * @tset: taskset of interest
1868 */
1869int cgroup_taskset_size(struct cgroup_taskset *tset)
1870{
1871 return tset->tc_array ? tset->tc_array_len : 1;
1872}
1873EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1874
1875
Ben Blum74a11662011-05-26 16:25:20 -07001876/*
1877 * cgroup_task_migrate - move a task from one cgroup to another.
1878 *
1879 * 'guarantee' is set if the caller promises that a new css_set for the task
1880 * will already exist. If not set, this function might sleep, and can fail with
Tejun Heocd3d0952011-12-12 18:12:21 -08001881 * -ENOMEM. Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001882 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001883static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1884 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001885{
1886 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001887
1888 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001889 * We are synchronized through threadgroup_lock() against PF_EXITING
1890 * setting such that we can't race against cgroup_exit() changing the
1891 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001892 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001893 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001894 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001895
Ben Blum74a11662011-05-26 16:25:20 -07001896 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001897 rcu_assign_pointer(tsk->cgroups, newcg);
1898 task_unlock(tsk);
1899
1900 /* Update the css_set linked lists if we're using them */
1901 write_lock(&css_set_lock);
1902 if (!list_empty(&tsk->cg_list))
1903 list_move(&tsk->cg_list, &newcg->tasks);
1904 write_unlock(&css_set_lock);
1905
1906 /*
1907 * We just gained a reference on oldcg by taking it from the task. As
1908 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1909 * it here; it will be freed under RCU.
1910 */
Ben Blum74a11662011-05-26 16:25:20 -07001911 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001912 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001913}
1914
Li Zefana043e3b2008-02-23 15:24:09 -08001915/**
1916 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1917 * @cgrp: the cgroup the task is attaching to
1918 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001919 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001920 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1921 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001922 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001923int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001924{
Tejun Heo8f121912012-03-29 22:03:33 -07001925 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001926 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001927 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001928 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001929 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001930 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001931
Tejun Heocd3d0952011-12-12 18:12:21 -08001932 /* @tsk either already exited or can't exit until the end */
1933 if (tsk->flags & PF_EXITING)
1934 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001935
1936 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001937 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001938 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001939 return 0;
1940
Tejun Heo2f7ee562011-12-12 18:12:21 -08001941 tset.single.task = tsk;
1942 tset.single.cgrp = oldcgrp;
1943
Paul Menagebbcb81d2007-10-18 23:39:32 -07001944 for_each_subsys(root, ss) {
1945 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08001946 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001947 if (retval) {
1948 /*
1949 * Remember on which subsystem the can_attach()
1950 * failed, so that we only call cancel_attach()
1951 * against the subsystems whose can_attach()
1952 * succeeded. (See below)
1953 */
1954 failed_ss = ss;
1955 goto out;
1956 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001957 }
1958 }
1959
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001960 newcg = find_css_set(tsk->cgroups, cgrp);
1961 if (!newcg) {
1962 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001963 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001964 }
1965
1966 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001967
Paul Menagebbcb81d2007-10-18 23:39:32 -07001968 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001969 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001970 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001971 }
Ben Blum74a11662011-05-26 16:25:20 -07001972
Paul Menagebbcb81d2007-10-18 23:39:32 -07001973 synchronize_rcu();
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001974out:
1975 if (retval) {
1976 for_each_subsys(root, ss) {
1977 if (ss == failed_ss)
1978 /*
1979 * This subsystem was the one that failed the
1980 * can_attach() check earlier, so we don't need
1981 * to call cancel_attach() against it or any
1982 * remaining subsystems.
1983 */
1984 break;
1985 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001986 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001987 }
1988 }
1989 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001990}
1991
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001992/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001993 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1994 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001995 * @tsk: the task to be attached
1996 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001997int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001998{
1999 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002000 int retval = 0;
2001
2002 cgroup_lock();
2003 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002004 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2005
2006 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002007 if (retval)
2008 break;
2009 }
2010 cgroup_unlock();
2011
2012 return retval;
2013}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002014EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002015
Ben Blum74a11662011-05-26 16:25:20 -07002016/**
2017 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2018 * @cgrp: the cgroup to attach to
2019 * @leader: the threadgroup leader task_struct of the group to be attached
2020 *
Tejun Heo257058a2011-12-12 18:12:21 -08002021 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2022 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002023 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002024static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002025{
2026 int retval, i, group_size;
2027 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002028 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002029 struct cgroupfs_root *root = cgrp->root;
2030 /* threadgroup list cursor and array */
2031 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002032 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002033 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002034 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002035
2036 /*
2037 * step 0: in order to do expensive, possibly blocking operations for
2038 * every thread, we cannot iterate the thread group list, since it needs
2039 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002040 * group - group_rwsem prevents new threads from appearing, and if
2041 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002042 */
2043 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002044 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002045 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002046 if (!group)
2047 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002048 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2049 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2050 if (retval)
2051 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002052
Ben Blum74a11662011-05-26 16:25:20 -07002053 tsk = leader;
2054 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002055 /*
2056 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2057 * already PF_EXITING could be freed from underneath us unless we
2058 * take an rcu_read_lock.
2059 */
2060 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002061 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002062 struct task_and_cgroup ent;
2063
Tejun Heocd3d0952011-12-12 18:12:21 -08002064 /* @tsk either already exited or can't exit until the end */
2065 if (tsk->flags & PF_EXITING)
2066 continue;
2067
Ben Blum74a11662011-05-26 16:25:20 -07002068 /* as per above, nr_threads may decrease, but not increase. */
2069 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002070 ent.task = tsk;
2071 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002072 /* nothing to do if this task is already in the cgroup */
2073 if (ent.cgrp == cgrp)
2074 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002075 /*
2076 * saying GFP_ATOMIC has no effect here because we did prealloc
2077 * earlier, but it's good form to communicate our expectations.
2078 */
Tejun Heo134d3372011-12-12 18:12:21 -08002079 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002080 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002081 i++;
2082 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002083 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002084 /* remember the number of threads in the array for later. */
2085 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002086 tset.tc_array = group;
2087 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002088
Tejun Heo134d3372011-12-12 18:12:21 -08002089 /* methods shouldn't be called if no task is actually migrating */
2090 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002091 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002092 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002093
Ben Blum74a11662011-05-26 16:25:20 -07002094 /*
2095 * step 1: check that we can legitimately attach to the cgroup.
2096 */
2097 for_each_subsys(root, ss) {
2098 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002099 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002100 if (retval) {
2101 failed_ss = ss;
2102 goto out_cancel_attach;
2103 }
2104 }
Ben Blum74a11662011-05-26 16:25:20 -07002105 }
2106
2107 /*
2108 * step 2: make sure css_sets exist for all threads to be migrated.
2109 * we use find_css_set, which allocates a new one if necessary.
2110 */
Ben Blum74a11662011-05-26 16:25:20 -07002111 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002112 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002113 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2114 if (!tc->cg) {
2115 retval = -ENOMEM;
2116 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002117 }
2118 }
2119
2120 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002121 * step 3: now that we're guaranteed success wrt the css_sets,
2122 * proceed to move all tasks to the new cgroup. There are no
2123 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002124 */
Ben Blum74a11662011-05-26 16:25:20 -07002125 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002126 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002127 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002128 }
2129 /* nothing is sensitive to fork() after this point. */
2130
2131 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002132 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002133 */
2134 for_each_subsys(root, ss) {
2135 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002136 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002137 }
2138
2139 /*
2140 * step 5: success! and cleanup
2141 */
2142 synchronize_rcu();
Ben Blum74a11662011-05-26 16:25:20 -07002143 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002144out_put_css_set_refs:
2145 if (retval) {
2146 for (i = 0; i < group_size; i++) {
2147 tc = flex_array_get(group, i);
2148 if (!tc->cg)
2149 break;
2150 put_css_set(tc->cg);
2151 }
Ben Blum74a11662011-05-26 16:25:20 -07002152 }
2153out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002154 if (retval) {
2155 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002156 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002157 break;
Ben Blum74a11662011-05-26 16:25:20 -07002158 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002159 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002160 }
2161 }
Ben Blum74a11662011-05-26 16:25:20 -07002162out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002163 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002164 return retval;
2165}
2166
2167/*
2168 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002169 * function to attach either it or all tasks in its threadgroup. Will lock
2170 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002171 */
2172static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002173{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002174 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002175 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002176 int ret;
2177
Ben Blum74a11662011-05-26 16:25:20 -07002178 if (!cgroup_lock_live_group(cgrp))
2179 return -ENODEV;
2180
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002181retry_find_task:
2182 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002183 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002184 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002185 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002186 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002187 ret= -ESRCH;
2188 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002189 }
Ben Blum74a11662011-05-26 16:25:20 -07002190 /*
2191 * even if we're attaching all tasks in the thread group, we
2192 * only need to check permissions on one of them.
2193 */
David Howellsc69e8d92008-11-14 10:39:19 +11002194 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002195 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2196 !uid_eq(cred->euid, tcred->uid) &&
2197 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002198 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002199 ret = -EACCES;
2200 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002201 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002202 } else
2203 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002204
2205 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002206 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002207
2208 /*
2209 * Workqueue threads may acquire PF_THREAD_BOUND and become
2210 * trapped in a cpuset, or RT worker may be born in a cgroup
2211 * with no rt_runtime allocated. Just say no.
2212 */
2213 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2214 ret = -EINVAL;
2215 rcu_read_unlock();
2216 goto out_unlock_cgroup;
2217 }
2218
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002219 get_task_struct(tsk);
2220 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002221
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002222 threadgroup_lock(tsk);
2223 if (threadgroup) {
2224 if (!thread_group_leader(tsk)) {
2225 /*
2226 * a race with de_thread from another thread's exec()
2227 * may strip us of our leadership, if this happens,
2228 * there is no choice but to throw this task away and
2229 * try again; this is
2230 * "double-double-toil-and-trouble-check locking".
2231 */
2232 threadgroup_unlock(tsk);
2233 put_task_struct(tsk);
2234 goto retry_find_task;
2235 }
2236 ret = cgroup_attach_proc(cgrp, tsk);
2237 } else
2238 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002239 threadgroup_unlock(tsk);
2240
Paul Menagebbcb81d2007-10-18 23:39:32 -07002241 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002242out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002243 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002244 return ret;
2245}
2246
Paul Menageaf351022008-07-25 01:47:01 -07002247static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2248{
Ben Blum74a11662011-05-26 16:25:20 -07002249 return attach_task_by_pid(cgrp, pid, false);
2250}
2251
2252static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2253{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002254 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002255}
2256
Paul Menagee788e062008-07-25 01:46:59 -07002257/**
2258 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2259 * @cgrp: the cgroup to be checked for liveness
2260 *
Paul Menage84eea842008-07-25 01:47:00 -07002261 * On success, returns true; the lock should be later released with
2262 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002263 */
Paul Menage84eea842008-07-25 01:47:00 -07002264bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002265{
2266 mutex_lock(&cgroup_mutex);
2267 if (cgroup_is_removed(cgrp)) {
2268 mutex_unlock(&cgroup_mutex);
2269 return false;
2270 }
2271 return true;
2272}
Ben Blum67523c42010-03-10 15:22:11 -08002273EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002274
2275static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2276 const char *buffer)
2277{
2278 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002279 if (strlen(buffer) >= PATH_MAX)
2280 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002281 if (!cgroup_lock_live_group(cgrp))
2282 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002283 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002284 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002285 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002286 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002287 return 0;
2288}
2289
2290static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2291 struct seq_file *seq)
2292{
2293 if (!cgroup_lock_live_group(cgrp))
2294 return -ENODEV;
2295 seq_puts(seq, cgrp->root->release_agent_path);
2296 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002297 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002298 return 0;
2299}
2300
Paul Menage84eea842008-07-25 01:47:00 -07002301/* A buffer size big enough for numbers or short strings */
2302#define CGROUP_LOCAL_BUFFER_SIZE 64
2303
Paul Menagee73d2c62008-04-29 01:00:06 -07002304static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002305 struct file *file,
2306 const char __user *userbuf,
2307 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002308{
Paul Menage84eea842008-07-25 01:47:00 -07002309 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002310 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002311 char *end;
2312
2313 if (!nbytes)
2314 return -EINVAL;
2315 if (nbytes >= sizeof(buffer))
2316 return -E2BIG;
2317 if (copy_from_user(buffer, userbuf, nbytes))
2318 return -EFAULT;
2319
2320 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002321 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002322 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002323 if (*end)
2324 return -EINVAL;
2325 retval = cft->write_u64(cgrp, cft, val);
2326 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002327 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002328 if (*end)
2329 return -EINVAL;
2330 retval = cft->write_s64(cgrp, cft, val);
2331 }
Paul Menage355e0c42007-10-18 23:39:33 -07002332 if (!retval)
2333 retval = nbytes;
2334 return retval;
2335}
2336
Paul Menagedb3b1492008-07-25 01:46:58 -07002337static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2338 struct file *file,
2339 const char __user *userbuf,
2340 size_t nbytes, loff_t *unused_ppos)
2341{
Paul Menage84eea842008-07-25 01:47:00 -07002342 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002343 int retval = 0;
2344 size_t max_bytes = cft->max_write_len;
2345 char *buffer = local_buffer;
2346
2347 if (!max_bytes)
2348 max_bytes = sizeof(local_buffer) - 1;
2349 if (nbytes >= max_bytes)
2350 return -E2BIG;
2351 /* Allocate a dynamic buffer if we need one */
2352 if (nbytes >= sizeof(local_buffer)) {
2353 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2354 if (buffer == NULL)
2355 return -ENOMEM;
2356 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002357 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2358 retval = -EFAULT;
2359 goto out;
2360 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002361
2362 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002363 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002364 if (!retval)
2365 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002366out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002367 if (buffer != local_buffer)
2368 kfree(buffer);
2369 return retval;
2370}
2371
Paul Menageddbcc7e2007-10-18 23:39:30 -07002372static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2373 size_t nbytes, loff_t *ppos)
2374{
2375 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002376 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377
Li Zefan75139b82009-01-07 18:07:33 -08002378 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002379 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002380 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002381 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002382 if (cft->write_u64 || cft->write_s64)
2383 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002384 if (cft->write_string)
2385 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002386 if (cft->trigger) {
2387 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2388 return ret ? ret : nbytes;
2389 }
Paul Menage355e0c42007-10-18 23:39:33 -07002390 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002391}
2392
Paul Menagef4c753b2008-04-29 00:59:56 -07002393static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2394 struct file *file,
2395 char __user *buf, size_t nbytes,
2396 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002397{
Paul Menage84eea842008-07-25 01:47:00 -07002398 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002399 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002400 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2401
2402 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2403}
2404
Paul Menagee73d2c62008-04-29 01:00:06 -07002405static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2406 struct file *file,
2407 char __user *buf, size_t nbytes,
2408 loff_t *ppos)
2409{
Paul Menage84eea842008-07-25 01:47:00 -07002410 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002411 s64 val = cft->read_s64(cgrp, cft);
2412 int len = sprintf(tmp, "%lld\n", (long long) val);
2413
2414 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2415}
2416
Paul Menageddbcc7e2007-10-18 23:39:30 -07002417static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2418 size_t nbytes, loff_t *ppos)
2419{
2420 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002421 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002422
Li Zefan75139b82009-01-07 18:07:33 -08002423 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002424 return -ENODEV;
2425
2426 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002427 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002428 if (cft->read_u64)
2429 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002430 if (cft->read_s64)
2431 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432 return -EINVAL;
2433}
2434
Paul Menage91796562008-04-29 01:00:01 -07002435/*
2436 * seqfile ops/methods for returning structured data. Currently just
2437 * supports string->u64 maps, but can be extended in future.
2438 */
2439
2440struct cgroup_seqfile_state {
2441 struct cftype *cft;
2442 struct cgroup *cgroup;
2443};
2444
2445static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2446{
2447 struct seq_file *sf = cb->state;
2448 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2449}
2450
2451static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2452{
2453 struct cgroup_seqfile_state *state = m->private;
2454 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002455 if (cft->read_map) {
2456 struct cgroup_map_cb cb = {
2457 .fill = cgroup_map_add,
2458 .state = m,
2459 };
2460 return cft->read_map(state->cgroup, cft, &cb);
2461 }
2462 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002463}
2464
Adrian Bunk96930a62008-07-25 19:46:21 -07002465static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002466{
2467 struct seq_file *seq = file->private_data;
2468 kfree(seq->private);
2469 return single_release(inode, file);
2470}
2471
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002472static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002473 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002474 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002475 .llseek = seq_lseek,
2476 .release = cgroup_seqfile_release,
2477};
2478
Paul Menageddbcc7e2007-10-18 23:39:30 -07002479static int cgroup_file_open(struct inode *inode, struct file *file)
2480{
2481 int err;
2482 struct cftype *cft;
2483
2484 err = generic_file_open(inode, file);
2485 if (err)
2486 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002487 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002488
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002489 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002490 struct cgroup_seqfile_state *state =
2491 kzalloc(sizeof(*state), GFP_USER);
2492 if (!state)
2493 return -ENOMEM;
2494 state->cft = cft;
2495 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2496 file->f_op = &cgroup_seqfile_operations;
2497 err = single_open(file, cgroup_seqfile_show, state);
2498 if (err < 0)
2499 kfree(state);
2500 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002501 err = cft->open(inode, file);
2502 else
2503 err = 0;
2504
2505 return err;
2506}
2507
2508static int cgroup_file_release(struct inode *inode, struct file *file)
2509{
2510 struct cftype *cft = __d_cft(file->f_dentry);
2511 if (cft->release)
2512 return cft->release(inode, file);
2513 return 0;
2514}
2515
2516/*
2517 * cgroup_rename - Only allow simple rename of directories in place.
2518 */
2519static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2520 struct inode *new_dir, struct dentry *new_dentry)
2521{
2522 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2523 return -ENOTDIR;
2524 if (new_dentry->d_inode)
2525 return -EEXIST;
2526 if (old_dir != new_dir)
2527 return -EIO;
2528 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2529}
2530
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002531static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2532{
2533 if (S_ISDIR(dentry->d_inode->i_mode))
2534 return &__d_cgrp(dentry)->xattrs;
2535 else
2536 return &__d_cft(dentry)->xattrs;
2537}
2538
2539static inline int xattr_enabled(struct dentry *dentry)
2540{
2541 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2542 return test_bit(ROOT_XATTR, &root->flags);
2543}
2544
2545static bool is_valid_xattr(const char *name)
2546{
2547 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2548 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2549 return true;
2550 return false;
2551}
2552
2553static int cgroup_setxattr(struct dentry *dentry, const char *name,
2554 const void *val, size_t size, int flags)
2555{
2556 if (!xattr_enabled(dentry))
2557 return -EOPNOTSUPP;
2558 if (!is_valid_xattr(name))
2559 return -EINVAL;
2560 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2561}
2562
2563static int cgroup_removexattr(struct dentry *dentry, const char *name)
2564{
2565 if (!xattr_enabled(dentry))
2566 return -EOPNOTSUPP;
2567 if (!is_valid_xattr(name))
2568 return -EINVAL;
2569 return simple_xattr_remove(__d_xattrs(dentry), name);
2570}
2571
2572static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2573 void *buf, size_t size)
2574{
2575 if (!xattr_enabled(dentry))
2576 return -EOPNOTSUPP;
2577 if (!is_valid_xattr(name))
2578 return -EINVAL;
2579 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2580}
2581
2582static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2583{
2584 if (!xattr_enabled(dentry))
2585 return -EOPNOTSUPP;
2586 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2587}
2588
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002589static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002590 .read = cgroup_file_read,
2591 .write = cgroup_file_write,
2592 .llseek = generic_file_llseek,
2593 .open = cgroup_file_open,
2594 .release = cgroup_file_release,
2595};
2596
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002597static const struct inode_operations cgroup_file_inode_operations = {
2598 .setxattr = cgroup_setxattr,
2599 .getxattr = cgroup_getxattr,
2600 .listxattr = cgroup_listxattr,
2601 .removexattr = cgroup_removexattr,
2602};
2603
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002604static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002605 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002606 .mkdir = cgroup_mkdir,
2607 .rmdir = cgroup_rmdir,
2608 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002609 .setxattr = cgroup_setxattr,
2610 .getxattr = cgroup_getxattr,
2611 .listxattr = cgroup_listxattr,
2612 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002613};
2614
Al Viro00cd8dd2012-06-10 17:13:09 -04002615static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002616{
2617 if (dentry->d_name.len > NAME_MAX)
2618 return ERR_PTR(-ENAMETOOLONG);
2619 d_add(dentry, NULL);
2620 return NULL;
2621}
2622
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002623/*
2624 * Check if a file is a control file
2625 */
2626static inline struct cftype *__file_cft(struct file *file)
2627{
2628 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2629 return ERR_PTR(-EINVAL);
2630 return __d_cft(file->f_dentry);
2631}
2632
Al Viroa5e7ed32011-07-26 01:55:55 -04002633static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002634 struct super_block *sb)
2635{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002636 struct inode *inode;
2637
2638 if (!dentry)
2639 return -ENOENT;
2640 if (dentry->d_inode)
2641 return -EEXIST;
2642
2643 inode = cgroup_new_inode(mode, sb);
2644 if (!inode)
2645 return -ENOMEM;
2646
2647 if (S_ISDIR(mode)) {
2648 inode->i_op = &cgroup_dir_inode_operations;
2649 inode->i_fop = &simple_dir_operations;
2650
2651 /* start off with i_nlink == 2 (for "." entry) */
2652 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002653 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002654
Tejun Heob8a2df62012-11-19 08:13:37 -08002655 /*
2656 * Control reaches here with cgroup_mutex held.
2657 * @inode->i_mutex should nest outside cgroup_mutex but we
2658 * want to populate it immediately without releasing
2659 * cgroup_mutex. As @inode isn't visible to anyone else
2660 * yet, trylock will always succeed without affecting
2661 * lockdep checks.
2662 */
2663 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002664 } else if (S_ISREG(mode)) {
2665 inode->i_size = 0;
2666 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002667 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002668 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002669 d_instantiate(dentry, inode);
2670 dget(dentry); /* Extra count - pin the dentry in core */
2671 return 0;
2672}
2673
Li Zefan099fca32009-04-02 16:57:29 -07002674/**
2675 * cgroup_file_mode - deduce file mode of a control file
2676 * @cft: the control file in question
2677 *
2678 * returns cft->mode if ->mode is not 0
2679 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2680 * returns S_IRUGO if it has only a read handler
2681 * returns S_IWUSR if it has only a write hander
2682 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002683static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002684{
Al Viroa5e7ed32011-07-26 01:55:55 -04002685 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002686
2687 if (cft->mode)
2688 return cft->mode;
2689
2690 if (cft->read || cft->read_u64 || cft->read_s64 ||
2691 cft->read_map || cft->read_seq_string)
2692 mode |= S_IRUGO;
2693
2694 if (cft->write || cft->write_u64 || cft->write_s64 ||
2695 cft->write_string || cft->trigger)
2696 mode |= S_IWUSR;
2697
2698 return mode;
2699}
2700
Tejun Heodb0416b2012-04-01 12:09:55 -07002701static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002702 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002703{
Paul Menagebd89aab2007-10-18 23:40:44 -07002704 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002705 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002706 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002707 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002708 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002709 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002710 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002711
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002712 simple_xattrs_init(&cft->xattrs);
2713
Tejun Heo8e3f6542012-04-01 12:09:55 -07002714 /* does @cft->flags tell us to skip creation on @cgrp? */
2715 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2716 return 0;
2717 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2718 return 0;
2719
Paul Menagebd89aab2007-10-18 23:40:44 -07002720 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721 strcpy(name, subsys->name);
2722 strcat(name, ".");
2723 }
2724 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002725
Paul Menageddbcc7e2007-10-18 23:39:30 -07002726 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002727
2728 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2729 if (!cfe)
2730 return -ENOMEM;
2731
Paul Menageddbcc7e2007-10-18 23:39:30 -07002732 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002733 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002734 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002735 goto out;
2736 }
2737
2738 mode = cgroup_file_mode(cft);
2739 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2740 if (!error) {
2741 cfe->type = (void *)cft;
2742 cfe->dentry = dentry;
2743 dentry->d_fsdata = cfe;
2744 list_add_tail(&cfe->node, &parent->files);
2745 cfe = NULL;
2746 }
2747 dput(dentry);
2748out:
2749 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002750 return error;
2751}
2752
Tejun Heo79578622012-04-01 12:09:56 -07002753static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002754 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002755{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002756 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002757 int err, ret = 0;
2758
2759 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Tejun Heo79578622012-04-01 12:09:56 -07002760 if (is_add)
2761 err = cgroup_add_file(cgrp, subsys, cft);
2762 else
2763 err = cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002764 if (err) {
Tejun Heo79578622012-04-01 12:09:56 -07002765 pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n",
2766 is_add ? "add" : "remove", cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002767 ret = err;
2768 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002769 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002770 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002771}
2772
Tejun Heo8e3f6542012-04-01 12:09:55 -07002773static DEFINE_MUTEX(cgroup_cft_mutex);
2774
2775static void cgroup_cfts_prepare(void)
2776 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2777{
2778 /*
2779 * Thanks to the entanglement with vfs inode locking, we can't walk
2780 * the existing cgroups under cgroup_mutex and create files.
2781 * Instead, we increment reference on all cgroups and build list of
2782 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2783 * exclusive access to the field.
2784 */
2785 mutex_lock(&cgroup_cft_mutex);
2786 mutex_lock(&cgroup_mutex);
2787}
2788
2789static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002790 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002791 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2792{
2793 LIST_HEAD(pending);
2794 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002795
2796 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2797 if (cfts && ss->root != &rootnode) {
2798 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2799 dget(cgrp->dentry);
2800 list_add_tail(&cgrp->cft_q_node, &pending);
2801 }
2802 }
2803
2804 mutex_unlock(&cgroup_mutex);
2805
2806 /*
2807 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2808 * files for all cgroups which were created before.
2809 */
2810 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2811 struct inode *inode = cgrp->dentry->d_inode;
2812
2813 mutex_lock(&inode->i_mutex);
2814 mutex_lock(&cgroup_mutex);
2815 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002816 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002817 mutex_unlock(&cgroup_mutex);
2818 mutex_unlock(&inode->i_mutex);
2819
2820 list_del_init(&cgrp->cft_q_node);
2821 dput(cgrp->dentry);
2822 }
2823
2824 mutex_unlock(&cgroup_cft_mutex);
2825}
2826
2827/**
2828 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2829 * @ss: target cgroup subsystem
2830 * @cfts: zero-length name terminated array of cftypes
2831 *
2832 * Register @cfts to @ss. Files described by @cfts are created for all
2833 * existing cgroups to which @ss is attached and all future cgroups will
2834 * have them too. This function can be called anytime whether @ss is
2835 * attached or not.
2836 *
2837 * Returns 0 on successful registration, -errno on failure. Note that this
2838 * function currently returns 0 as long as @cfts registration is successful
2839 * even if some file creation attempts on existing cgroups fail.
2840 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002841int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002842{
2843 struct cftype_set *set;
2844
2845 set = kzalloc(sizeof(*set), GFP_KERNEL);
2846 if (!set)
2847 return -ENOMEM;
2848
2849 cgroup_cfts_prepare();
2850 set->cfts = cfts;
2851 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002852 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002853
2854 return 0;
2855}
2856EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2857
Li Zefana043e3b2008-02-23 15:24:09 -08002858/**
Tejun Heo79578622012-04-01 12:09:56 -07002859 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2860 * @ss: target cgroup subsystem
2861 * @cfts: zero-length name terminated array of cftypes
2862 *
2863 * Unregister @cfts from @ss. Files described by @cfts are removed from
2864 * all existing cgroups to which @ss is attached and all future cgroups
2865 * won't have them either. This function can be called anytime whether @ss
2866 * is attached or not.
2867 *
2868 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2869 * registered with @ss.
2870 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002871int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002872{
2873 struct cftype_set *set;
2874
2875 cgroup_cfts_prepare();
2876
2877 list_for_each_entry(set, &ss->cftsets, node) {
2878 if (set->cfts == cfts) {
2879 list_del_init(&set->node);
2880 cgroup_cfts_commit(ss, cfts, false);
2881 return 0;
2882 }
2883 }
2884
2885 cgroup_cfts_commit(ss, NULL, false);
2886 return -ENOENT;
2887}
2888
2889/**
Li Zefana043e3b2008-02-23 15:24:09 -08002890 * cgroup_task_count - count the number of tasks in a cgroup.
2891 * @cgrp: the cgroup in question
2892 *
2893 * Return the number of tasks in the cgroup.
2894 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002895int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002896{
2897 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002898 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002899
Paul Menage817929e2007-10-18 23:39:36 -07002900 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002901 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002902 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002903 }
2904 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002905 return count;
2906}
2907
2908/*
Paul Menage817929e2007-10-18 23:39:36 -07002909 * Advance a list_head iterator. The iterator should be positioned at
2910 * the start of a css_set
2911 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002912static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002913 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002914{
2915 struct list_head *l = it->cg_link;
2916 struct cg_cgroup_link *link;
2917 struct css_set *cg;
2918
2919 /* Advance to the next non-empty css_set */
2920 do {
2921 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002922 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002923 it->cg_link = NULL;
2924 return;
2925 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002926 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002927 cg = link->cg;
2928 } while (list_empty(&cg->tasks));
2929 it->cg_link = l;
2930 it->task = cg->tasks.next;
2931}
2932
Cliff Wickman31a7df02008-02-07 00:14:42 -08002933/*
2934 * To reduce the fork() overhead for systems that are not actually
2935 * using their cgroups capability, we don't maintain the lists running
2936 * through each css_set to its tasks until we see the list actually
2937 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002938 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002939static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002940{
2941 struct task_struct *p, *g;
2942 write_lock(&css_set_lock);
2943 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002944 /*
2945 * We need tasklist_lock because RCU is not safe against
2946 * while_each_thread(). Besides, a forking task that has passed
2947 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2948 * is not guaranteed to have its child immediately visible in the
2949 * tasklist if we walk through it with RCU.
2950 */
2951 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002952 do_each_thread(g, p) {
2953 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002954 /*
2955 * We should check if the process is exiting, otherwise
2956 * it will race with cgroup_exit() in that the list
2957 * entry won't be deleted though the process has exited.
2958 */
2959 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002960 list_add(&p->cg_list, &p->cgroups->tasks);
2961 task_unlock(p);
2962 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002963 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002964 write_unlock(&css_set_lock);
2965}
2966
Tejun Heo574bd9f2012-11-09 09:12:29 -08002967/**
2968 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2969 * @pos: the current position (%NULL to initiate traversal)
2970 * @cgroup: cgroup whose descendants to walk
2971 *
2972 * To be used by cgroup_for_each_descendant_pre(). Find the next
2973 * descendant to visit for pre-order traversal of @cgroup's descendants.
2974 */
2975struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2976 struct cgroup *cgroup)
2977{
2978 struct cgroup *next;
2979
2980 WARN_ON_ONCE(!rcu_read_lock_held());
2981
2982 /* if first iteration, pretend we just visited @cgroup */
2983 if (!pos) {
2984 if (list_empty(&cgroup->children))
2985 return NULL;
2986 pos = cgroup;
2987 }
2988
2989 /* visit the first child if exists */
2990 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2991 if (next)
2992 return next;
2993
2994 /* no child, visit my or the closest ancestor's next sibling */
2995 do {
2996 next = list_entry_rcu(pos->sibling.next, struct cgroup,
2997 sibling);
2998 if (&next->sibling != &pos->parent->children)
2999 return next;
3000
3001 pos = pos->parent;
3002 } while (pos != cgroup);
3003
3004 return NULL;
3005}
3006EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3007
3008static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3009{
3010 struct cgroup *last;
3011
3012 do {
3013 last = pos;
3014 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3015 sibling);
3016 } while (pos);
3017
3018 return last;
3019}
3020
3021/**
3022 * cgroup_next_descendant_post - find the next descendant for post-order walk
3023 * @pos: the current position (%NULL to initiate traversal)
3024 * @cgroup: cgroup whose descendants to walk
3025 *
3026 * To be used by cgroup_for_each_descendant_post(). Find the next
3027 * descendant to visit for post-order traversal of @cgroup's descendants.
3028 */
3029struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3030 struct cgroup *cgroup)
3031{
3032 struct cgroup *next;
3033
3034 WARN_ON_ONCE(!rcu_read_lock_held());
3035
3036 /* if first iteration, visit the leftmost descendant */
3037 if (!pos) {
3038 next = cgroup_leftmost_descendant(cgroup);
3039 return next != cgroup ? next : NULL;
3040 }
3041
3042 /* if there's an unvisited sibling, visit its leftmost descendant */
3043 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3044 if (&next->sibling != &pos->parent->children)
3045 return cgroup_leftmost_descendant(next);
3046
3047 /* no sibling left, visit parent */
3048 next = pos->parent;
3049 return next != cgroup ? next : NULL;
3050}
3051EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3052
Paul Menagebd89aab2007-10-18 23:40:44 -07003053void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003054 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003055{
3056 /*
3057 * The first time anyone tries to iterate across a cgroup,
3058 * we need to enable the list linking each css_set to its
3059 * tasks, and fix up all existing tasks.
3060 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003061 if (!use_task_css_set_links)
3062 cgroup_enable_task_cg_lists();
3063
Paul Menage817929e2007-10-18 23:39:36 -07003064 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003065 it->cg_link = &cgrp->css_sets;
3066 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003067}
3068
Paul Menagebd89aab2007-10-18 23:40:44 -07003069struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003070 struct cgroup_iter *it)
3071{
3072 struct task_struct *res;
3073 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003074 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003075
3076 /* If the iterator cg is NULL, we have no tasks */
3077 if (!it->cg_link)
3078 return NULL;
3079 res = list_entry(l, struct task_struct, cg_list);
3080 /* Advance iterator to find next entry */
3081 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003082 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3083 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003084 /* We reached the end of this task list - move on to
3085 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003086 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003087 } else {
3088 it->task = l;
3089 }
3090 return res;
3091}
3092
Paul Menagebd89aab2007-10-18 23:40:44 -07003093void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003094 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003095{
3096 read_unlock(&css_set_lock);
3097}
3098
Cliff Wickman31a7df02008-02-07 00:14:42 -08003099static inline int started_after_time(struct task_struct *t1,
3100 struct timespec *time,
3101 struct task_struct *t2)
3102{
3103 int start_diff = timespec_compare(&t1->start_time, time);
3104 if (start_diff > 0) {
3105 return 1;
3106 } else if (start_diff < 0) {
3107 return 0;
3108 } else {
3109 /*
3110 * Arbitrarily, if two processes started at the same
3111 * time, we'll say that the lower pointer value
3112 * started first. Note that t2 may have exited by now
3113 * so this may not be a valid pointer any longer, but
3114 * that's fine - it still serves to distinguish
3115 * between two tasks started (effectively) simultaneously.
3116 */
3117 return t1 > t2;
3118 }
3119}
3120
3121/*
3122 * This function is a callback from heap_insert() and is used to order
3123 * the heap.
3124 * In this case we order the heap in descending task start time.
3125 */
3126static inline int started_after(void *p1, void *p2)
3127{
3128 struct task_struct *t1 = p1;
3129 struct task_struct *t2 = p2;
3130 return started_after_time(t1, &t2->start_time, t2);
3131}
3132
3133/**
3134 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3135 * @scan: struct cgroup_scanner containing arguments for the scan
3136 *
3137 * Arguments include pointers to callback functions test_task() and
3138 * process_task().
3139 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3140 * and if it returns true, call process_task() for it also.
3141 * The test_task pointer may be NULL, meaning always true (select all tasks).
3142 * Effectively duplicates cgroup_iter_{start,next,end}()
3143 * but does not lock css_set_lock for the call to process_task().
3144 * The struct cgroup_scanner may be embedded in any structure of the caller's
3145 * creation.
3146 * It is guaranteed that process_task() will act on every task that
3147 * is a member of the cgroup for the duration of this call. This
3148 * function may or may not call process_task() for tasks that exit
3149 * or move to a different cgroup during the call, or are forked or
3150 * move into the cgroup during the call.
3151 *
3152 * Note that test_task() may be called with locks held, and may in some
3153 * situations be called multiple times for the same task, so it should
3154 * be cheap.
3155 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3156 * pre-allocated and will be used for heap operations (and its "gt" member will
3157 * be overwritten), else a temporary heap will be used (allocation of which
3158 * may cause this function to fail).
3159 */
3160int cgroup_scan_tasks(struct cgroup_scanner *scan)
3161{
3162 int retval, i;
3163 struct cgroup_iter it;
3164 struct task_struct *p, *dropped;
3165 /* Never dereference latest_task, since it's not refcounted */
3166 struct task_struct *latest_task = NULL;
3167 struct ptr_heap tmp_heap;
3168 struct ptr_heap *heap;
3169 struct timespec latest_time = { 0, 0 };
3170
3171 if (scan->heap) {
3172 /* The caller supplied our heap and pre-allocated its memory */
3173 heap = scan->heap;
3174 heap->gt = &started_after;
3175 } else {
3176 /* We need to allocate our own heap memory */
3177 heap = &tmp_heap;
3178 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3179 if (retval)
3180 /* cannot allocate the heap */
3181 return retval;
3182 }
3183
3184 again:
3185 /*
3186 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3187 * to determine which are of interest, and using the scanner's
3188 * "process_task" callback to process any of them that need an update.
3189 * Since we don't want to hold any locks during the task updates,
3190 * gather tasks to be processed in a heap structure.
3191 * The heap is sorted by descending task start time.
3192 * If the statically-sized heap fills up, we overflow tasks that
3193 * started later, and in future iterations only consider tasks that
3194 * started after the latest task in the previous pass. This
3195 * guarantees forward progress and that we don't miss any tasks.
3196 */
3197 heap->size = 0;
3198 cgroup_iter_start(scan->cg, &it);
3199 while ((p = cgroup_iter_next(scan->cg, &it))) {
3200 /*
3201 * Only affect tasks that qualify per the caller's callback,
3202 * if he provided one
3203 */
3204 if (scan->test_task && !scan->test_task(p, scan))
3205 continue;
3206 /*
3207 * Only process tasks that started after the last task
3208 * we processed
3209 */
3210 if (!started_after_time(p, &latest_time, latest_task))
3211 continue;
3212 dropped = heap_insert(heap, p);
3213 if (dropped == NULL) {
3214 /*
3215 * The new task was inserted; the heap wasn't
3216 * previously full
3217 */
3218 get_task_struct(p);
3219 } else if (dropped != p) {
3220 /*
3221 * The new task was inserted, and pushed out a
3222 * different task
3223 */
3224 get_task_struct(p);
3225 put_task_struct(dropped);
3226 }
3227 /*
3228 * Else the new task was newer than anything already in
3229 * the heap and wasn't inserted
3230 */
3231 }
3232 cgroup_iter_end(scan->cg, &it);
3233
3234 if (heap->size) {
3235 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003236 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003237 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003238 latest_time = q->start_time;
3239 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003240 }
3241 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003242 scan->process_task(q, scan);
3243 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003244 }
3245 /*
3246 * If we had to process any tasks at all, scan again
3247 * in case some of them were in the middle of forking
3248 * children that didn't get processed.
3249 * Not the most efficient way to do it, but it avoids
3250 * having to take callback_mutex in the fork path
3251 */
3252 goto again;
3253 }
3254 if (heap == &tmp_heap)
3255 heap_free(&tmp_heap);
3256 return 0;
3257}
3258
Paul Menage817929e2007-10-18 23:39:36 -07003259/*
Ben Blum102a7752009-09-23 15:56:26 -07003260 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003261 *
3262 * Reading this file can return large amounts of data if a cgroup has
3263 * *lots* of attached tasks. So it may need several calls to read(),
3264 * but we cannot guarantee that the information we produce is correct
3265 * unless we produce it entirely atomically.
3266 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003267 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003268
Li Zefan24528252012-01-20 11:58:43 +08003269/* which pidlist file are we talking about? */
3270enum cgroup_filetype {
3271 CGROUP_FILE_PROCS,
3272 CGROUP_FILE_TASKS,
3273};
3274
3275/*
3276 * A pidlist is a list of pids that virtually represents the contents of one
3277 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3278 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3279 * to the cgroup.
3280 */
3281struct cgroup_pidlist {
3282 /*
3283 * used to find which pidlist is wanted. doesn't change as long as
3284 * this particular list stays in the list.
3285 */
3286 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3287 /* array of xids */
3288 pid_t *list;
3289 /* how many elements the above list has */
3290 int length;
3291 /* how many files are using the current array */
3292 int use_count;
3293 /* each of these stored in a list by its cgroup */
3294 struct list_head links;
3295 /* pointer to the cgroup we belong to, for list removal purposes */
3296 struct cgroup *owner;
3297 /* protects the other fields */
3298 struct rw_semaphore mutex;
3299};
3300
Paul Menagebbcb81d2007-10-18 23:39:32 -07003301/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003302 * The following two functions "fix" the issue where there are more pids
3303 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3304 * TODO: replace with a kernel-wide solution to this problem
3305 */
3306#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3307static void *pidlist_allocate(int count)
3308{
3309 if (PIDLIST_TOO_LARGE(count))
3310 return vmalloc(count * sizeof(pid_t));
3311 else
3312 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3313}
3314static void pidlist_free(void *p)
3315{
3316 if (is_vmalloc_addr(p))
3317 vfree(p);
3318 else
3319 kfree(p);
3320}
3321static void *pidlist_resize(void *p, int newcount)
3322{
3323 void *newlist;
3324 /* note: if new alloc fails, old p will still be valid either way */
3325 if (is_vmalloc_addr(p)) {
3326 newlist = vmalloc(newcount * sizeof(pid_t));
3327 if (!newlist)
3328 return NULL;
3329 memcpy(newlist, p, newcount * sizeof(pid_t));
3330 vfree(p);
3331 } else {
3332 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3333 }
3334 return newlist;
3335}
3336
3337/*
Ben Blum102a7752009-09-23 15:56:26 -07003338 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3339 * If the new stripped list is sufficiently smaller and there's enough memory
3340 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3341 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003342 */
Ben Blum102a7752009-09-23 15:56:26 -07003343/* is the size difference enough that we should re-allocate the array? */
3344#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3345static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003346{
Ben Blum102a7752009-09-23 15:56:26 -07003347 int src, dest = 1;
3348 pid_t *list = *p;
3349 pid_t *newlist;
3350
3351 /*
3352 * we presume the 0th element is unique, so i starts at 1. trivial
3353 * edge cases first; no work needs to be done for either
3354 */
3355 if (length == 0 || length == 1)
3356 return length;
3357 /* src and dest walk down the list; dest counts unique elements */
3358 for (src = 1; src < length; src++) {
3359 /* find next unique element */
3360 while (list[src] == list[src-1]) {
3361 src++;
3362 if (src == length)
3363 goto after;
3364 }
3365 /* dest always points to where the next unique element goes */
3366 list[dest] = list[src];
3367 dest++;
3368 }
3369after:
3370 /*
3371 * if the length difference is large enough, we want to allocate a
3372 * smaller buffer to save memory. if this fails due to out of memory,
3373 * we'll just stay with what we've got.
3374 */
3375 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003376 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003377 if (newlist)
3378 *p = newlist;
3379 }
3380 return dest;
3381}
3382
3383static int cmppid(const void *a, const void *b)
3384{
3385 return *(pid_t *)a - *(pid_t *)b;
3386}
3387
3388/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003389 * find the appropriate pidlist for our purpose (given procs vs tasks)
3390 * returns with the lock on that pidlist already held, and takes care
3391 * of the use count, or returns NULL with no locks held if we're out of
3392 * memory.
3393 */
3394static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3395 enum cgroup_filetype type)
3396{
3397 struct cgroup_pidlist *l;
3398 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08003399 struct pid_namespace *ns = current->nsproxy->pid_ns;
3400
Ben Blum72a8cb32009-09-23 15:56:27 -07003401 /*
3402 * We can't drop the pidlist_mutex before taking the l->mutex in case
3403 * the last ref-holder is trying to remove l from the list at the same
3404 * time. Holding the pidlist_mutex precludes somebody taking whichever
3405 * list we find out from under us - compare release_pid_array().
3406 */
3407 mutex_lock(&cgrp->pidlist_mutex);
3408 list_for_each_entry(l, &cgrp->pidlists, links) {
3409 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003410 /* make sure l doesn't vanish out from under us */
3411 down_write(&l->mutex);
3412 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003413 return l;
3414 }
3415 }
3416 /* entry not found; create a new one */
3417 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3418 if (!l) {
3419 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003420 return l;
3421 }
3422 init_rwsem(&l->mutex);
3423 down_write(&l->mutex);
3424 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003425 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003426 l->use_count = 0; /* don't increment here */
3427 l->list = NULL;
3428 l->owner = cgrp;
3429 list_add(&l->links, &cgrp->pidlists);
3430 mutex_unlock(&cgrp->pidlist_mutex);
3431 return l;
3432}
3433
3434/*
Ben Blum102a7752009-09-23 15:56:26 -07003435 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3436 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003437static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3438 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003439{
3440 pid_t *array;
3441 int length;
3442 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003443 struct cgroup_iter it;
3444 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003445 struct cgroup_pidlist *l;
3446
3447 /*
3448 * If cgroup gets more users after we read count, we won't have
3449 * enough space - tough. This race is indistinguishable to the
3450 * caller from the case that the additional cgroup users didn't
3451 * show up until sometime later on.
3452 */
3453 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003454 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003455 if (!array)
3456 return -ENOMEM;
3457 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003458 cgroup_iter_start(cgrp, &it);
3459 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003460 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003461 break;
Ben Blum102a7752009-09-23 15:56:26 -07003462 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003463 if (type == CGROUP_FILE_PROCS)
3464 pid = task_tgid_vnr(tsk);
3465 else
3466 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003467 if (pid > 0) /* make sure to only use valid results */
3468 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003469 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003470 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003471 length = n;
3472 /* now sort & (if procs) strip out duplicates */
3473 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003474 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003475 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003476 l = cgroup_pidlist_find(cgrp, type);
3477 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003478 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003479 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003480 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003481 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003482 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003483 l->list = array;
3484 l->length = length;
3485 l->use_count++;
3486 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003487 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003488 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003489}
3490
Balbir Singh846c7bb2007-10-18 23:39:44 -07003491/**
Li Zefana043e3b2008-02-23 15:24:09 -08003492 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003493 * @stats: cgroupstats to fill information into
3494 * @dentry: A dentry entry belonging to the cgroup for which stats have
3495 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003496 *
3497 * Build and fill cgroupstats so that taskstats can export it to user
3498 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003499 */
3500int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3501{
3502 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003503 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003504 struct cgroup_iter it;
3505 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003506
Balbir Singh846c7bb2007-10-18 23:39:44 -07003507 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003508 * Validate dentry by checking the superblock operations,
3509 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003510 */
Li Zefan33d283b2008-11-19 15:36:48 -08003511 if (dentry->d_sb->s_op != &cgroup_ops ||
3512 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003513 goto err;
3514
3515 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003516 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003517
Paul Menagebd89aab2007-10-18 23:40:44 -07003518 cgroup_iter_start(cgrp, &it);
3519 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003520 switch (tsk->state) {
3521 case TASK_RUNNING:
3522 stats->nr_running++;
3523 break;
3524 case TASK_INTERRUPTIBLE:
3525 stats->nr_sleeping++;
3526 break;
3527 case TASK_UNINTERRUPTIBLE:
3528 stats->nr_uninterruptible++;
3529 break;
3530 case TASK_STOPPED:
3531 stats->nr_stopped++;
3532 break;
3533 default:
3534 if (delayacct_is_task_waiting_on_io(tsk))
3535 stats->nr_io_wait++;
3536 break;
3537 }
3538 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003539 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003540
Balbir Singh846c7bb2007-10-18 23:39:44 -07003541err:
3542 return ret;
3543}
3544
Paul Menage8f3ff202009-09-23 15:56:25 -07003545
Paul Menagecc31edc2008-10-18 20:28:04 -07003546/*
Ben Blum102a7752009-09-23 15:56:26 -07003547 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003548 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003549 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003550 */
3551
Ben Blum102a7752009-09-23 15:56:26 -07003552static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003553{
3554 /*
3555 * Initially we receive a position value that corresponds to
3556 * one more than the last pid shown (or 0 on the first call or
3557 * after a seek to the start). Use a binary-search to find the
3558 * next pid to display, if any
3559 */
Ben Blum102a7752009-09-23 15:56:26 -07003560 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003561 int index = 0, pid = *pos;
3562 int *iter;
3563
Ben Blum102a7752009-09-23 15:56:26 -07003564 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003565 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003566 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003567
Paul Menagecc31edc2008-10-18 20:28:04 -07003568 while (index < end) {
3569 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003570 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003571 index = mid;
3572 break;
Ben Blum102a7752009-09-23 15:56:26 -07003573 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003574 index = mid + 1;
3575 else
3576 end = mid;
3577 }
3578 }
3579 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003580 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003581 return NULL;
3582 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003583 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003584 *pos = *iter;
3585 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003586}
3587
Ben Blum102a7752009-09-23 15:56:26 -07003588static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003589{
Ben Blum102a7752009-09-23 15:56:26 -07003590 struct cgroup_pidlist *l = s->private;
3591 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003592}
3593
Ben Blum102a7752009-09-23 15:56:26 -07003594static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003595{
Ben Blum102a7752009-09-23 15:56:26 -07003596 struct cgroup_pidlist *l = s->private;
3597 pid_t *p = v;
3598 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003599 /*
3600 * Advance to the next pid in the array. If this goes off the
3601 * end, we're done
3602 */
3603 p++;
3604 if (p >= end) {
3605 return NULL;
3606 } else {
3607 *pos = *p;
3608 return p;
3609 }
3610}
3611
Ben Blum102a7752009-09-23 15:56:26 -07003612static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003613{
3614 return seq_printf(s, "%d\n", *(int *)v);
3615}
3616
Ben Blum102a7752009-09-23 15:56:26 -07003617/*
3618 * seq_operations functions for iterating on pidlists through seq_file -
3619 * independent of whether it's tasks or procs
3620 */
3621static const struct seq_operations cgroup_pidlist_seq_operations = {
3622 .start = cgroup_pidlist_start,
3623 .stop = cgroup_pidlist_stop,
3624 .next = cgroup_pidlist_next,
3625 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003626};
3627
Ben Blum102a7752009-09-23 15:56:26 -07003628static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003629{
Ben Blum72a8cb32009-09-23 15:56:27 -07003630 /*
3631 * the case where we're the last user of this particular pidlist will
3632 * have us remove it from the cgroup's list, which entails taking the
3633 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3634 * pidlist_mutex, we have to take pidlist_mutex first.
3635 */
3636 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003637 down_write(&l->mutex);
3638 BUG_ON(!l->use_count);
3639 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003640 /* we're the last user if refcount is 0; remove and free */
3641 list_del(&l->links);
3642 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003643 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003644 put_pid_ns(l->key.ns);
3645 up_write(&l->mutex);
3646 kfree(l);
3647 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003648 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003649 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003650 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003651}
3652
Ben Blum102a7752009-09-23 15:56:26 -07003653static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003654{
Ben Blum102a7752009-09-23 15:56:26 -07003655 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003656 if (!(file->f_mode & FMODE_READ))
3657 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003658 /*
3659 * the seq_file will only be initialized if the file was opened for
3660 * reading; hence we check if it's not null only in that case.
3661 */
3662 l = ((struct seq_file *)file->private_data)->private;
3663 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003664 return seq_release(inode, file);
3665}
3666
Ben Blum102a7752009-09-23 15:56:26 -07003667static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003668 .read = seq_read,
3669 .llseek = seq_lseek,
3670 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003671 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003672};
3673
3674/*
Ben Blum102a7752009-09-23 15:56:26 -07003675 * The following functions handle opens on a file that displays a pidlist
3676 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3677 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003678 */
Ben Blum102a7752009-09-23 15:56:26 -07003679/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003680static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003681{
3682 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003683 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003684 int retval;
3685
3686 /* Nothing to do for write-only files */
3687 if (!(file->f_mode & FMODE_READ))
3688 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003689
Ben Blum102a7752009-09-23 15:56:26 -07003690 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003691 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003692 if (retval)
3693 return retval;
3694 /* configure file information */
3695 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003696
Ben Blum102a7752009-09-23 15:56:26 -07003697 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003698 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003699 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003700 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003701 }
Ben Blum102a7752009-09-23 15:56:26 -07003702 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003703 return 0;
3704}
Ben Blum102a7752009-09-23 15:56:26 -07003705static int cgroup_tasks_open(struct inode *unused, struct file *file)
3706{
Ben Blum72a8cb32009-09-23 15:56:27 -07003707 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003708}
3709static int cgroup_procs_open(struct inode *unused, struct file *file)
3710{
Ben Blum72a8cb32009-09-23 15:56:27 -07003711 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003712}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003713
Paul Menagebd89aab2007-10-18 23:40:44 -07003714static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003715 struct cftype *cft)
3716{
Paul Menagebd89aab2007-10-18 23:40:44 -07003717 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003718}
3719
Paul Menage6379c102008-07-25 01:47:01 -07003720static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3721 struct cftype *cft,
3722 u64 val)
3723{
3724 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3725 if (val)
3726 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3727 else
3728 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3729 return 0;
3730}
3731
Paul Menagebbcb81d2007-10-18 23:39:32 -07003732/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003733 * Unregister event and free resources.
3734 *
3735 * Gets called from workqueue.
3736 */
3737static void cgroup_event_remove(struct work_struct *work)
3738{
3739 struct cgroup_event *event = container_of(work, struct cgroup_event,
3740 remove);
3741 struct cgroup *cgrp = event->cgrp;
3742
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003743 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3744
3745 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003746 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003747 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003748}
3749
3750/*
3751 * Gets called on POLLHUP on eventfd when user closes it.
3752 *
3753 * Called with wqh->lock held and interrupts disabled.
3754 */
3755static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3756 int sync, void *key)
3757{
3758 struct cgroup_event *event = container_of(wait,
3759 struct cgroup_event, wait);
3760 struct cgroup *cgrp = event->cgrp;
3761 unsigned long flags = (unsigned long)key;
3762
3763 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003764 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003765 spin_lock(&cgrp->event_list_lock);
3766 list_del(&event->list);
3767 spin_unlock(&cgrp->event_list_lock);
3768 /*
3769 * We are in atomic context, but cgroup_event_remove() may
3770 * sleep, so we have to call it in workqueue.
3771 */
3772 schedule_work(&event->remove);
3773 }
3774
3775 return 0;
3776}
3777
3778static void cgroup_event_ptable_queue_proc(struct file *file,
3779 wait_queue_head_t *wqh, poll_table *pt)
3780{
3781 struct cgroup_event *event = container_of(pt,
3782 struct cgroup_event, pt);
3783
3784 event->wqh = wqh;
3785 add_wait_queue(wqh, &event->wait);
3786}
3787
3788/*
3789 * Parse input and register new cgroup event handler.
3790 *
3791 * Input must be in format '<event_fd> <control_fd> <args>'.
3792 * Interpretation of args is defined by control file implementation.
3793 */
3794static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3795 const char *buffer)
3796{
3797 struct cgroup_event *event = NULL;
3798 unsigned int efd, cfd;
3799 struct file *efile = NULL;
3800 struct file *cfile = NULL;
3801 char *endp;
3802 int ret;
3803
3804 efd = simple_strtoul(buffer, &endp, 10);
3805 if (*endp != ' ')
3806 return -EINVAL;
3807 buffer = endp + 1;
3808
3809 cfd = simple_strtoul(buffer, &endp, 10);
3810 if ((*endp != ' ') && (*endp != '\0'))
3811 return -EINVAL;
3812 buffer = endp + 1;
3813
3814 event = kzalloc(sizeof(*event), GFP_KERNEL);
3815 if (!event)
3816 return -ENOMEM;
3817 event->cgrp = cgrp;
3818 INIT_LIST_HEAD(&event->list);
3819 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3820 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3821 INIT_WORK(&event->remove, cgroup_event_remove);
3822
3823 efile = eventfd_fget(efd);
3824 if (IS_ERR(efile)) {
3825 ret = PTR_ERR(efile);
3826 goto fail;
3827 }
3828
3829 event->eventfd = eventfd_ctx_fileget(efile);
3830 if (IS_ERR(event->eventfd)) {
3831 ret = PTR_ERR(event->eventfd);
3832 goto fail;
3833 }
3834
3835 cfile = fget(cfd);
3836 if (!cfile) {
3837 ret = -EBADF;
3838 goto fail;
3839 }
3840
3841 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003842 /* AV: shouldn't we check that it's been opened for read instead? */
3843 ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003844 if (ret < 0)
3845 goto fail;
3846
3847 event->cft = __file_cft(cfile);
3848 if (IS_ERR(event->cft)) {
3849 ret = PTR_ERR(event->cft);
3850 goto fail;
3851 }
3852
3853 if (!event->cft->register_event || !event->cft->unregister_event) {
3854 ret = -EINVAL;
3855 goto fail;
3856 }
3857
3858 ret = event->cft->register_event(cgrp, event->cft,
3859 event->eventfd, buffer);
3860 if (ret)
3861 goto fail;
3862
3863 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3864 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3865 ret = 0;
3866 goto fail;
3867 }
3868
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003869 /*
3870 * Events should be removed after rmdir of cgroup directory, but before
3871 * destroying subsystem state objects. Let's take reference to cgroup
3872 * directory dentry to do that.
3873 */
3874 dget(cgrp->dentry);
3875
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003876 spin_lock(&cgrp->event_list_lock);
3877 list_add(&event->list, &cgrp->event_list);
3878 spin_unlock(&cgrp->event_list_lock);
3879
3880 fput(cfile);
3881 fput(efile);
3882
3883 return 0;
3884
3885fail:
3886 if (cfile)
3887 fput(cfile);
3888
3889 if (event && event->eventfd && !IS_ERR(event->eventfd))
3890 eventfd_ctx_put(event->eventfd);
3891
3892 if (!IS_ERR_OR_NULL(efile))
3893 fput(efile);
3894
3895 kfree(event);
3896
3897 return ret;
3898}
3899
Daniel Lezcano97978e62010-10-27 15:33:35 -07003900static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3901 struct cftype *cft)
3902{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003903 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003904}
3905
3906static int cgroup_clone_children_write(struct cgroup *cgrp,
3907 struct cftype *cft,
3908 u64 val)
3909{
3910 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003911 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003912 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003913 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003914 return 0;
3915}
3916
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003917/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003918 * for the common functions, 'private' gives the type of file
3919 */
Ben Blum102a7752009-09-23 15:56:26 -07003920/* for hysterical raisins, we can't put this on the older files */
3921#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003922static struct cftype files[] = {
3923 {
3924 .name = "tasks",
3925 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003926 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003927 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003928 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003929 },
Ben Blum102a7752009-09-23 15:56:26 -07003930 {
3931 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3932 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003933 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003934 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003935 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003936 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003937 {
3938 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003939 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003940 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003941 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003942 {
3943 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3944 .write_string = cgroup_write_event_control,
3945 .mode = S_IWUGO,
3946 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003947 {
3948 .name = "cgroup.clone_children",
3949 .read_u64 = cgroup_clone_children_read,
3950 .write_u64 = cgroup_clone_children_write,
3951 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003952 {
3953 .name = "release_agent",
3954 .flags = CFTYPE_ONLY_ON_ROOT,
3955 .read_seq_string = cgroup_release_agent_show,
3956 .write_string = cgroup_release_agent_write,
3957 .max_write_len = PATH_MAX,
3958 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003959 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003960};
3961
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003962/**
3963 * cgroup_populate_dir - selectively creation of files in a directory
3964 * @cgrp: target cgroup
3965 * @base_files: true if the base files should be added
3966 * @subsys_mask: mask of the subsystem ids whose files should be added
3967 */
3968static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3969 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003970{
3971 int err;
3972 struct cgroup_subsys *ss;
3973
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003974 if (base_files) {
3975 err = cgroup_addrm_files(cgrp, NULL, files, true);
3976 if (err < 0)
3977 return err;
3978 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07003979
Tejun Heo8e3f6542012-04-01 12:09:55 -07003980 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07003981 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003982 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003983 if (!test_bit(ss->subsys_id, &subsys_mask))
3984 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003985
Tejun Heodb0416b2012-04-01 12:09:55 -07003986 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07003987 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003988 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07003989
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003990 /* This cgroup is ready now */
3991 for_each_subsys(cgrp->root, ss) {
3992 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3993 /*
3994 * Update id->css pointer and make this css visible from
3995 * CSS ID functions. This pointer will be dereferened
3996 * from RCU-read-side without locks.
3997 */
3998 if (css->id)
3999 rcu_assign_pointer(css->id->css, css);
4000 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004001
4002 return 0;
4003}
4004
Tejun Heo48ddbe12012-04-01 12:09:56 -07004005static void css_dput_fn(struct work_struct *work)
4006{
4007 struct cgroup_subsys_state *css =
4008 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004009 struct dentry *dentry = css->cgroup->dentry;
4010 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004011
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004012 atomic_inc(&sb->s_active);
4013 dput(dentry);
4014 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004015}
4016
Paul Menageddbcc7e2007-10-18 23:39:30 -07004017static void init_cgroup_css(struct cgroup_subsys_state *css,
4018 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004019 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004020{
Paul Menagebd89aab2007-10-18 23:40:44 -07004021 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004022 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004023 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004024 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004025 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004026 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004027 BUG_ON(cgrp->subsys[ss->subsys_id]);
4028 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004029
4030 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004031 * css holds an extra ref to @cgrp->dentry which is put on the last
4032 * css_put(). dput() requires process context, which css_put() may
4033 * be called without. @css->dput_work will be used to invoke
4034 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004035 */
4036 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004037}
4038
Tejun Heob1929db2012-11-19 08:13:38 -08004039/* invoke ->post_create() on a new CSS and mark it online if successful */
4040static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004041{
Tejun Heob1929db2012-11-19 08:13:38 -08004042 int ret = 0;
4043
Tejun Heoa31f2d32012-11-19 08:13:37 -08004044 lockdep_assert_held(&cgroup_mutex);
4045
Tejun Heo92fb9742012-11-19 08:13:38 -08004046 if (ss->css_online)
4047 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004048 if (!ret)
4049 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4050 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004051}
4052
4053/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4054static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4055 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4056{
4057 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4058
4059 lockdep_assert_held(&cgroup_mutex);
4060
4061 if (!(css->flags & CSS_ONLINE))
4062 return;
4063
4064 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004065 * css_offline() should be called with cgroup_mutex unlocked. See
Tejun Heoa31f2d32012-11-19 08:13:37 -08004066 * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for
4067 * details. This temporary unlocking should go away once
4068 * cgroup_mutex is unexported from controllers.
4069 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004070 if (ss->css_offline) {
Tejun Heoa31f2d32012-11-19 08:13:37 -08004071 mutex_unlock(&cgroup_mutex);
Tejun Heo92fb9742012-11-19 08:13:38 -08004072 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004073 mutex_lock(&cgroup_mutex);
4074 }
4075
4076 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4077}
4078
Paul Menageddbcc7e2007-10-18 23:39:30 -07004079/*
Li Zefana043e3b2008-02-23 15:24:09 -08004080 * cgroup_create - create a cgroup
4081 * @parent: cgroup that will be parent of the new cgroup
4082 * @dentry: dentry of the new cgroup
4083 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004084 *
Li Zefana043e3b2008-02-23 15:24:09 -08004085 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004086 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004087static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004088 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004089{
Paul Menagebd89aab2007-10-18 23:40:44 -07004090 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004091 struct cgroupfs_root *root = parent->root;
4092 int err = 0;
4093 struct cgroup_subsys *ss;
4094 struct super_block *sb = root->sb;
4095
Paul Menagebd89aab2007-10-18 23:40:44 -07004096 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4097 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004098 return -ENOMEM;
4099
Tejun Heo976c06b2012-11-05 09:16:59 -08004100 /*
4101 * Only live parents can have children. Note that the liveliness
4102 * check isn't strictly necessary because cgroup_mkdir() and
4103 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4104 * anyway so that locking is contained inside cgroup proper and we
4105 * don't get nasty surprises if we ever grow another caller.
4106 */
4107 if (!cgroup_lock_live_group(parent)) {
4108 err = -ENODEV;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004109 goto err_free_cgrp;
Tejun Heo976c06b2012-11-05 09:16:59 -08004110 }
4111
Paul Menageddbcc7e2007-10-18 23:39:30 -07004112 /* Grab a reference on the superblock so the hierarchy doesn't
4113 * get deleted on unmount if there are child cgroups. This
4114 * can be done outside cgroup_mutex, since the sb can't
4115 * disappear while someone has an open control file on the
4116 * fs */
4117 atomic_inc(&sb->s_active);
4118
Paul Menagecc31edc2008-10-18 20:28:04 -07004119 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004120
Paul Menagebd89aab2007-10-18 23:40:44 -07004121 cgrp->parent = parent;
4122 cgrp->root = parent->root;
4123 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004124
Li Zefanb6abdb02008-03-04 14:28:19 -08004125 if (notify_on_release(parent))
4126 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4127
Tejun Heo2260e7f2012-11-19 08:13:38 -08004128 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4129 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004130
Paul Menageddbcc7e2007-10-18 23:39:30 -07004131 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004132 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004133
Tejun Heo92fb9742012-11-19 08:13:38 -08004134 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004135 if (IS_ERR(css)) {
4136 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004137 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004138 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004139 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004140 if (ss->use_id) {
4141 err = alloc_css_id(ss, parent, cgrp);
4142 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004143 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004144 }
Tejun Heo92fb9742012-11-19 08:13:38 -08004145 /* At error, ->css_free() callback has to free assigned ID. */
Tejun Heo2260e7f2012-11-19 08:13:38 -08004146 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags) &&
4147 ss->post_clone)
Li Zefan761b3ef2012-01-31 13:47:36 +08004148 ss->post_clone(cgrp);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004149
4150 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4151 parent->parent) {
4152 pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4153 current->comm, current->pid, ss->name);
4154 if (!strcmp(ss->name, "memory"))
4155 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4156 ss->warned_broken_hierarchy = true;
4157 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004158 }
4159
Tejun Heo4e139af2012-11-19 08:13:36 -08004160 /*
4161 * Create directory. cgroup_create_file() returns with the new
4162 * directory locked on success so that it can be populated without
4163 * dropping cgroup_mutex.
4164 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004165 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004166 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004167 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004168 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004169
Tejun Heo4e139af2012-11-19 08:13:36 -08004170 /* allocation complete, commit to creation */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004171 dentry->d_fsdata = cgrp;
Tejun Heofebfcef2012-11-19 08:13:36 -08004172 cgrp->dentry = dentry;
Tejun Heo4e139af2012-11-19 08:13:36 -08004173 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4174 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4175 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004176
Tejun Heob1929db2012-11-19 08:13:38 -08004177 /* each css holds a ref to the cgroup's dentry */
4178 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004179 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004180
Tejun Heob1929db2012-11-19 08:13:38 -08004181 /* creation succeeded, notify subsystems */
4182 for_each_subsys(root, ss) {
4183 err = online_css(ss, cgrp);
4184 if (err)
4185 goto err_destroy;
Tejun Heoa8638032012-11-09 09:12:29 -08004186 }
4187
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004188 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004189 if (err)
4190 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004191
4192 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004193 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004194
4195 return 0;
4196
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004197err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004198 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004199 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004200 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004201 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004202 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004203 /* Release the reference count that we took on the superblock */
4204 deactivate_super(sb);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004205err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004206 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004207 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004208
4209err_destroy:
4210 cgroup_destroy_locked(cgrp);
4211 mutex_unlock(&cgroup_mutex);
4212 mutex_unlock(&dentry->d_inode->i_mutex);
4213 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004214}
4215
Al Viro18bb1db2011-07-26 01:41:39 -04004216static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004217{
4218 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4219
4220 /* the vfs holds inode->i_mutex already */
4221 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4222}
4223
Tejun Heo28b4c272012-04-01 12:09:56 -07004224/*
4225 * Check the reference count on each subsystem. Since we already
4226 * established that there are no tasks in the cgroup, if the css refcount
4227 * is also 1, then there should be no outstanding references, so the
4228 * subsystem is safe to destroy. We scan across all subsystems rather than
4229 * using the per-hierarchy linked list of mounted subsystems since we can
4230 * be called via check_for_release() with no synchronization other than
4231 * RCU, and the subsystem linked list isn't RCU-safe.
4232 */
Li Zefan55b6fd02008-07-29 22:33:20 -07004233static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004234{
Paul Menage81a6a5c2007-10-18 23:39:38 -07004235 int i;
Tejun Heo28b4c272012-04-01 12:09:56 -07004236
Ben Blumaae8aab2010-03-10 15:22:07 -08004237 /*
4238 * We won't need to lock the subsys array, because the subsystems
4239 * we're concerned about aren't going anywhere since our cgroup root
4240 * has a reference on them.
4241 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004242 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4243 struct cgroup_subsys *ss = subsys[i];
4244 struct cgroup_subsys_state *css;
Tejun Heo28b4c272012-04-01 12:09:56 -07004245
Ben Blumaae8aab2010-03-10 15:22:07 -08004246 /* Skip subsystems not present or not in this hierarchy */
4247 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004248 continue;
Tejun Heo28b4c272012-04-01 12:09:56 -07004249
Paul Menagebd89aab2007-10-18 23:40:44 -07004250 css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004251 /*
4252 * When called from check_for_release() it's possible
Paul Menage81a6a5c2007-10-18 23:39:38 -07004253 * that by this point the cgroup has been removed
4254 * and the css deleted. But a false-positive doesn't
4255 * matter, since it can only happen if the cgroup
4256 * has been deleted and hence no longer needs the
Tejun Heo28b4c272012-04-01 12:09:56 -07004257 * release agent to be called anyway.
4258 */
4259 if (css && css_refcnt(css) > 1)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004260 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004261 }
4262 return 0;
4263}
4264
Tejun Heo42809dd2012-11-19 08:13:37 -08004265static int cgroup_destroy_locked(struct cgroup *cgrp)
4266 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004267{
Tejun Heo42809dd2012-11-19 08:13:37 -08004268 struct dentry *d = cgrp->dentry;
4269 struct cgroup *parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004270 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004271 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004272 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004273
Tejun Heo42809dd2012-11-19 08:13:37 -08004274 lockdep_assert_held(&d->d_inode->i_mutex);
4275 lockdep_assert_held(&cgroup_mutex);
4276
4277 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004278 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004279
Tejun Heo1a90dd52012-11-05 09:16:59 -08004280 /*
4281 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4282 * removed. This makes future css_tryget() and child creation
4283 * attempts fail thus maintaining the removal conditions verified
4284 * above.
4285 */
Tejun Heoed9577932012-11-05 09:16:58 -08004286 for_each_subsys(cgrp->root, ss) {
4287 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4288
4289 WARN_ON(atomic_read(&css->refcnt) < 0);
4290 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004291 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004292 set_bit(CGRP_REMOVED, &cgrp->flags);
4293
Tejun Heoa31f2d32012-11-19 08:13:37 -08004294 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004295 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004296 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004297
4298 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004299 * Put all the base refs. Each css holds an extra reference to the
4300 * cgroup's dentry and cgroup removal proceeds regardless of css
4301 * refs. On the last put of each css, whenever that may be, the
4302 * extra dentry ref is put so that dentry destruction happens only
4303 * after all css's are released.
4304 */
Tejun Heoe9316082012-11-05 09:16:58 -08004305 for_each_subsys(cgrp->root, ss)
4306 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004307
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004308 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004309 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004310 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004311 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004312
Paul Menage999cd8a2009-01-07 18:08:36 -08004313 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004314 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004315 list_del_init(&cgrp->allcg_node);
4316
Tejun Heo42809dd2012-11-19 08:13:37 -08004317 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004318 cgroup_d_remove_dir(d);
4319 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004320
Paul Menagebd89aab2007-10-18 23:40:44 -07004321 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004322 check_for_release(parent);
4323
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004324 /*
4325 * Unregister events and notify userspace.
4326 * Notify userspace about cgroup removing only after rmdir of cgroup
4327 * directory to avoid race between userspace and kernelspace
4328 */
4329 spin_lock(&cgrp->event_list_lock);
4330 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4331 list_del(&event->list);
4332 remove_wait_queue(event->wqh, &event->wait);
4333 eventfd_signal(event->eventfd, 1);
4334 schedule_work(&event->remove);
4335 }
4336 spin_unlock(&cgrp->event_list_lock);
4337
Paul Menageddbcc7e2007-10-18 23:39:30 -07004338 return 0;
4339}
4340
Tejun Heo42809dd2012-11-19 08:13:37 -08004341static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4342{
4343 int ret;
4344
4345 mutex_lock(&cgroup_mutex);
4346 ret = cgroup_destroy_locked(dentry->d_fsdata);
4347 mutex_unlock(&cgroup_mutex);
4348
4349 return ret;
4350}
4351
Tejun Heo8e3f6542012-04-01 12:09:55 -07004352static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4353{
4354 INIT_LIST_HEAD(&ss->cftsets);
4355
4356 /*
4357 * base_cftset is embedded in subsys itself, no need to worry about
4358 * deregistration.
4359 */
4360 if (ss->base_cftypes) {
4361 ss->base_cftset.cfts = ss->base_cftypes;
4362 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4363 }
4364}
4365
Li Zefan06a11922008-04-29 01:00:07 -07004366static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004367{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004368 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004369
4370 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004371
Tejun Heo648bb562012-11-19 08:13:36 -08004372 mutex_lock(&cgroup_mutex);
4373
Tejun Heo8e3f6542012-04-01 12:09:55 -07004374 /* init base cftset */
4375 cgroup_init_cftsets(ss);
4376
Paul Menageddbcc7e2007-10-18 23:39:30 -07004377 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004378 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004379 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004380 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004381 /* We don't handle early failures gracefully */
4382 BUG_ON(IS_ERR(css));
4383 init_cgroup_css(css, ss, dummytop);
4384
Li Zefane8d55fd2008-04-29 01:00:13 -07004385 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004386 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004387 * newly registered, all tasks and hence the
4388 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004389 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004390
4391 need_forkexit_callback |= ss->fork || ss->exit;
4392
Li Zefane8d55fd2008-04-29 01:00:13 -07004393 /* At system boot, before all subsystems have been
4394 * registered, no tasks have been forked, so we don't
4395 * need to invoke fork callbacks here. */
4396 BUG_ON(!list_empty(&init_task.tasks));
4397
Paul Menageddbcc7e2007-10-18 23:39:30 -07004398 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004399 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004400
Tejun Heo648bb562012-11-19 08:13:36 -08004401 mutex_unlock(&cgroup_mutex);
4402
Ben Blume6a11052010-03-10 15:22:09 -08004403 /* this function shouldn't be used with modular subsystems, since they
4404 * need to register a subsys_id, among other things */
4405 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004406}
4407
4408/**
Ben Blume6a11052010-03-10 15:22:09 -08004409 * cgroup_load_subsys: load and register a modular subsystem at runtime
4410 * @ss: the subsystem to load
4411 *
4412 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004413 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004414 * up for use. If the subsystem is built-in anyway, work is delegated to the
4415 * simpler cgroup_init_subsys.
4416 */
4417int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4418{
Ben Blume6a11052010-03-10 15:22:09 -08004419 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004420 int i, ret;
Ben Blume6a11052010-03-10 15:22:09 -08004421
4422 /* check name and function validity */
4423 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004424 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004425 return -EINVAL;
4426
4427 /*
4428 * we don't support callbacks in modular subsystems. this check is
4429 * before the ss->module check for consistency; a subsystem that could
4430 * be a module should still have no callbacks even if the user isn't
4431 * compiling it as one.
4432 */
4433 if (ss->fork || ss->exit)
4434 return -EINVAL;
4435
4436 /*
4437 * an optionally modular subsystem is built-in: we want to do nothing,
4438 * since cgroup_init_subsys will have already taken care of it.
4439 */
4440 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004441 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004442 BUG_ON(subsys[ss->subsys_id] != ss);
4443 return 0;
4444 }
4445
Tejun Heo8e3f6542012-04-01 12:09:55 -07004446 /* init base cftset */
4447 cgroup_init_cftsets(ss);
4448
Ben Blume6a11052010-03-10 15:22:09 -08004449 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004450 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004451
4452 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004453 * no ss->css_alloc seems to need anything important in the ss
4454 * struct, so this can happen first (i.e. before the rootnode
4455 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004456 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004457 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004458 if (IS_ERR(css)) {
4459 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004460 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004461 mutex_unlock(&cgroup_mutex);
4462 return PTR_ERR(css);
4463 }
4464
4465 list_add(&ss->sibling, &rootnode.subsys_list);
4466 ss->root = &rootnode;
4467
4468 /* our new subsystem will be attached to the dummy hierarchy. */
4469 init_cgroup_css(css, ss, dummytop);
4470 /* init_idr must be after init_cgroup_css because it sets css->id. */
4471 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004472 ret = cgroup_init_idr(ss, css);
4473 if (ret)
4474 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004475 }
4476
4477 /*
4478 * Now we need to entangle the css into the existing css_sets. unlike
4479 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4480 * will need a new pointer to it; done by iterating the css_set_table.
4481 * furthermore, modifying the existing css_sets will corrupt the hash
4482 * table state, so each changed css_set will need its hash recomputed.
4483 * this is all done under the css_set_lock.
4484 */
4485 write_lock(&css_set_lock);
4486 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4487 struct css_set *cg;
4488 struct hlist_node *node, *tmp;
4489 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4490
4491 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4492 /* skip entries that we already rehashed */
4493 if (cg->subsys[ss->subsys_id])
4494 continue;
4495 /* remove existing entry */
4496 hlist_del(&cg->hlist);
4497 /* set new value */
4498 cg->subsys[ss->subsys_id] = css;
4499 /* recompute hash and restore entry */
4500 new_bucket = css_set_hash(cg->subsys);
4501 hlist_add_head(&cg->hlist, new_bucket);
4502 }
4503 }
4504 write_unlock(&css_set_lock);
4505
Ben Blume6a11052010-03-10 15:22:09 -08004506 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004507 ret = online_css(ss, dummytop);
4508 if (ret)
4509 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004510
Ben Blume6a11052010-03-10 15:22:09 -08004511 /* success! */
4512 mutex_unlock(&cgroup_mutex);
4513 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004514
4515err_unload:
4516 mutex_unlock(&cgroup_mutex);
4517 /* @ss can't be mounted here as try_module_get() would fail */
4518 cgroup_unload_subsys(ss);
4519 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004520}
4521EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4522
4523/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004524 * cgroup_unload_subsys: unload a modular subsystem
4525 * @ss: the subsystem to unload
4526 *
4527 * This function should be called in a modular subsystem's exitcall. When this
4528 * function is invoked, the refcount on the subsystem's module will be 0, so
4529 * the subsystem will not be attached to any hierarchy.
4530 */
4531void cgroup_unload_subsys(struct cgroup_subsys *ss)
4532{
4533 struct cg_cgroup_link *link;
4534 struct hlist_head *hhead;
4535
4536 BUG_ON(ss->module == NULL);
4537
4538 /*
4539 * we shouldn't be called if the subsystem is in use, and the use of
4540 * try_module_get in parse_cgroupfs_options should ensure that it
4541 * doesn't start being used while we're killing it off.
4542 */
4543 BUG_ON(ss->root != &rootnode);
4544
4545 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004546
Tejun Heoa31f2d32012-11-19 08:13:37 -08004547 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004548 ss->active = 0;
4549
4550 if (ss->use_id) {
4551 idr_remove_all(&ss->idr);
4552 idr_destroy(&ss->idr);
4553 }
4554
Ben Blumcf5d5942010-03-10 15:22:09 -08004555 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004556 subsys[ss->subsys_id] = NULL;
4557
4558 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004559 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004560
4561 /*
4562 * disentangle the css from all css_sets attached to the dummytop. as
4563 * in loading, we need to pay our respects to the hashtable gods.
4564 */
4565 write_lock(&css_set_lock);
4566 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4567 struct css_set *cg = link->cg;
4568
4569 hlist_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004570 cg->subsys[ss->subsys_id] = NULL;
4571 hhead = css_set_hash(cg->subsys);
4572 hlist_add_head(&cg->hlist, hhead);
4573 }
4574 write_unlock(&css_set_lock);
4575
4576 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004577 * remove subsystem's css from the dummytop and free it - need to
4578 * free before marking as null because ss->css_free needs the
4579 * cgrp->subsys pointer to find their state. note that this also
4580 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004581 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004582 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004583 dummytop->subsys[ss->subsys_id] = NULL;
4584
4585 mutex_unlock(&cgroup_mutex);
4586}
4587EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4588
4589/**
Li Zefana043e3b2008-02-23 15:24:09 -08004590 * cgroup_init_early - cgroup initialization at system boot
4591 *
4592 * Initialize cgroups at system boot, and initialize any
4593 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004594 */
4595int __init cgroup_init_early(void)
4596{
4597 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004598 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004599 INIT_LIST_HEAD(&init_css_set.cg_links);
4600 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004601 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004602 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004603 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004604 root_count = 1;
4605 init_task.cgroups = &init_css_set;
4606
4607 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004608 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004609 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004610 &rootnode.top_cgroup.css_sets);
4611 list_add(&init_css_set_link.cg_link_list,
4612 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004613
Li Zefan472b1052008-04-29 01:00:11 -07004614 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4615 INIT_HLIST_HEAD(&css_set_table[i]);
4616
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004617 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004618 struct cgroup_subsys *ss = subsys[i];
4619
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004620 /* at bootup time, we don't worry about modular subsystems */
4621 if (!ss || ss->module)
4622 continue;
4623
Paul Menageddbcc7e2007-10-18 23:39:30 -07004624 BUG_ON(!ss->name);
4625 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004626 BUG_ON(!ss->css_alloc);
4627 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004628 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004629 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004630 ss->name, ss->subsys_id);
4631 BUG();
4632 }
4633
4634 if (ss->early_init)
4635 cgroup_init_subsys(ss);
4636 }
4637 return 0;
4638}
4639
4640/**
Li Zefana043e3b2008-02-23 15:24:09 -08004641 * cgroup_init - cgroup initialization
4642 *
4643 * Register cgroup filesystem and /proc file, and initialize
4644 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004645 */
4646int __init cgroup_init(void)
4647{
4648 int err;
4649 int i;
Li Zefan472b1052008-04-29 01:00:11 -07004650 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07004651
4652 err = bdi_init(&cgroup_backing_dev_info);
4653 if (err)
4654 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004655
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004656 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004657 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004658
4659 /* at bootup time, we don't worry about modular subsystems */
4660 if (!ss || ss->module)
4661 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004662 if (!ss->early_init)
4663 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004664 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004665 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004666 }
4667
Li Zefan472b1052008-04-29 01:00:11 -07004668 /* Add init_css_set to the hash table */
4669 hhead = css_set_hash(init_css_set.subsys);
4670 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004671 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004672
4673 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4674 if (!cgroup_kobj) {
4675 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004676 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004677 }
4678
4679 err = register_filesystem(&cgroup_fs_type);
4680 if (err < 0) {
4681 kobject_put(cgroup_kobj);
4682 goto out;
4683 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004684
Li Zefan46ae2202008-04-29 01:00:08 -07004685 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004686
Paul Menageddbcc7e2007-10-18 23:39:30 -07004687out:
Paul Menagea4243162007-10-18 23:39:35 -07004688 if (err)
4689 bdi_destroy(&cgroup_backing_dev_info);
4690
Paul Menageddbcc7e2007-10-18 23:39:30 -07004691 return err;
4692}
Paul Menageb4f48b62007-10-18 23:39:33 -07004693
Paul Menagea4243162007-10-18 23:39:35 -07004694/*
4695 * proc_cgroup_show()
4696 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4697 * - Used for /proc/<pid>/cgroup.
4698 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4699 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004700 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004701 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4702 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4703 * cgroup to top_cgroup.
4704 */
4705
4706/* TODO: Use a proper seq_file iterator */
4707static int proc_cgroup_show(struct seq_file *m, void *v)
4708{
4709 struct pid *pid;
4710 struct task_struct *tsk;
4711 char *buf;
4712 int retval;
4713 struct cgroupfs_root *root;
4714
4715 retval = -ENOMEM;
4716 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4717 if (!buf)
4718 goto out;
4719
4720 retval = -ESRCH;
4721 pid = m->private;
4722 tsk = get_pid_task(pid, PIDTYPE_PID);
4723 if (!tsk)
4724 goto out_free;
4725
4726 retval = 0;
4727
4728 mutex_lock(&cgroup_mutex);
4729
Li Zefane5f6a862009-01-07 18:07:41 -08004730 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004731 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004732 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004733 int count = 0;
4734
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004735 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004736 for_each_subsys(root, ss)
4737 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004738 if (strlen(root->name))
4739 seq_printf(m, "%sname=%s", count ? "," : "",
4740 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004741 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004742 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004743 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004744 if (retval < 0)
4745 goto out_unlock;
4746 seq_puts(m, buf);
4747 seq_putc(m, '\n');
4748 }
4749
4750out_unlock:
4751 mutex_unlock(&cgroup_mutex);
4752 put_task_struct(tsk);
4753out_free:
4754 kfree(buf);
4755out:
4756 return retval;
4757}
4758
4759static int cgroup_open(struct inode *inode, struct file *file)
4760{
4761 struct pid *pid = PROC_I(inode)->pid;
4762 return single_open(file, proc_cgroup_show, pid);
4763}
4764
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004765const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004766 .open = cgroup_open,
4767 .read = seq_read,
4768 .llseek = seq_lseek,
4769 .release = single_release,
4770};
4771
4772/* Display information about each subsystem and each hierarchy */
4773static int proc_cgroupstats_show(struct seq_file *m, void *v)
4774{
4775 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004776
Paul Menage8bab8dd2008-04-04 14:29:57 -07004777 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004778 /*
4779 * ideally we don't want subsystems moving around while we do this.
4780 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4781 * subsys/hierarchy state.
4782 */
Paul Menagea4243162007-10-18 23:39:35 -07004783 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004784 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4785 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004786 if (ss == NULL)
4787 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004788 seq_printf(m, "%s\t%d\t%d\t%d\n",
4789 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004790 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004791 }
4792 mutex_unlock(&cgroup_mutex);
4793 return 0;
4794}
4795
4796static int cgroupstats_open(struct inode *inode, struct file *file)
4797{
Al Viro9dce07f2008-03-29 03:07:28 +00004798 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004799}
4800
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004801static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004802 .open = cgroupstats_open,
4803 .read = seq_read,
4804 .llseek = seq_lseek,
4805 .release = single_release,
4806};
4807
Paul Menageb4f48b62007-10-18 23:39:33 -07004808/**
4809 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004810 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004811 *
4812 * Description: A task inherits its parent's cgroup at fork().
4813 *
4814 * A pointer to the shared css_set was automatically copied in
4815 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004816 * it was not made under the protection of RCU or cgroup_mutex, so
4817 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4818 * have already changed current->cgroups, allowing the previously
4819 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004820 *
4821 * At the point that cgroup_fork() is called, 'current' is the parent
4822 * task, and the passed argument 'child' points to the child task.
4823 */
4824void cgroup_fork(struct task_struct *child)
4825{
Tejun Heo9bb71302012-10-18 17:52:07 -07004826 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004827 child->cgroups = current->cgroups;
4828 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004829 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004830 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004831}
4832
4833/**
Li Zefana043e3b2008-02-23 15:24:09 -08004834 * cgroup_post_fork - called on a new task after adding it to the task list
4835 * @child: the task in question
4836 *
Tejun Heo5edee612012-10-16 15:03:14 -07004837 * Adds the task to the list running through its css_set if necessary and
4838 * call the subsystem fork() callbacks. Has to be after the task is
4839 * visible on the task list in case we race with the first call to
4840 * cgroup_iter_start() - to guarantee that the new task ends up on its
4841 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004842 */
Paul Menage817929e2007-10-18 23:39:36 -07004843void cgroup_post_fork(struct task_struct *child)
4844{
Tejun Heo5edee612012-10-16 15:03:14 -07004845 int i;
4846
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004847 /*
4848 * use_task_css_set_links is set to 1 before we walk the tasklist
4849 * under the tasklist_lock and we read it here after we added the child
4850 * to the tasklist under the tasklist_lock as well. If the child wasn't
4851 * yet in the tasklist when we walked through it from
4852 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4853 * should be visible now due to the paired locking and barriers implied
4854 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4855 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4856 * lock on fork.
4857 */
Paul Menage817929e2007-10-18 23:39:36 -07004858 if (use_task_css_set_links) {
4859 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004860 task_lock(child);
4861 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004862 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004863 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004864 write_unlock(&css_set_lock);
4865 }
Tejun Heo5edee612012-10-16 15:03:14 -07004866
4867 /*
4868 * Call ss->fork(). This must happen after @child is linked on
4869 * css_set; otherwise, @child might change state between ->fork()
4870 * and addition to css_set.
4871 */
4872 if (need_forkexit_callback) {
4873 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4874 struct cgroup_subsys *ss = subsys[i];
4875
4876 /*
4877 * fork/exit callbacks are supported only for
4878 * builtin subsystems and we don't need further
4879 * synchronization as they never go away.
4880 */
4881 if (!ss || ss->module)
4882 continue;
4883
4884 if (ss->fork)
4885 ss->fork(child);
4886 }
4887 }
Paul Menage817929e2007-10-18 23:39:36 -07004888}
Tejun Heo5edee612012-10-16 15:03:14 -07004889
Paul Menage817929e2007-10-18 23:39:36 -07004890/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004891 * cgroup_exit - detach cgroup from exiting task
4892 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004893 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004894 *
4895 * Description: Detach cgroup from @tsk and release it.
4896 *
4897 * Note that cgroups marked notify_on_release force every task in
4898 * them to take the global cgroup_mutex mutex when exiting.
4899 * This could impact scaling on very large systems. Be reluctant to
4900 * use notify_on_release cgroups where very high task exit scaling
4901 * is required on large systems.
4902 *
4903 * the_top_cgroup_hack:
4904 *
4905 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4906 *
4907 * We call cgroup_exit() while the task is still competent to
4908 * handle notify_on_release(), then leave the task attached to the
4909 * root cgroup in each hierarchy for the remainder of its exit.
4910 *
4911 * To do this properly, we would increment the reference count on
4912 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4913 * code we would add a second cgroup function call, to drop that
4914 * reference. This would just create an unnecessary hot spot on
4915 * the top_cgroup reference count, to no avail.
4916 *
4917 * Normally, holding a reference to a cgroup without bumping its
4918 * count is unsafe. The cgroup could go away, or someone could
4919 * attach us to a different cgroup, decrementing the count on
4920 * the first cgroup that we never incremented. But in this case,
4921 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004922 * which wards off any cgroup_attach_task() attempts, or task is a failed
4923 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004924 */
4925void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4926{
Paul Menage817929e2007-10-18 23:39:36 -07004927 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004928 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004929
4930 /*
4931 * Unlink from the css_set task list if necessary.
4932 * Optimistically check cg_list before taking
4933 * css_set_lock
4934 */
4935 if (!list_empty(&tsk->cg_list)) {
4936 write_lock(&css_set_lock);
4937 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004938 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004939 write_unlock(&css_set_lock);
4940 }
4941
Paul Menageb4f48b62007-10-18 23:39:33 -07004942 /* Reassign the task to the init_css_set. */
4943 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004944 cg = tsk->cgroups;
4945 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004946
4947 if (run_callbacks && need_forkexit_callback) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004948 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004949 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004950
4951 /* modular subsystems can't use callbacks */
4952 if (!ss || ss->module)
4953 continue;
4954
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004955 if (ss->exit) {
4956 struct cgroup *old_cgrp =
4957 rcu_dereference_raw(cg->subsys[i])->cgroup;
4958 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004959 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004960 }
4961 }
4962 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004963 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004964
Paul Menage817929e2007-10-18 23:39:36 -07004965 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004966 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004967}
Paul Menage697f4162007-10-18 23:39:34 -07004968
4969/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004970 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08004971 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004972 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08004973 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004974 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4975 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07004976 *
4977 * If we are sending in dummytop, then presumably we are creating
4978 * the top cgroup in the subsystem.
4979 *
4980 * Called only by the ns (nsproxy) cgroup.
4981 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004982int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07004983{
4984 int ret;
4985 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07004986
Paul Menagebd89aab2007-10-18 23:40:44 -07004987 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07004988 return 1;
4989
Paul Menage7717f7b2009-09-23 15:56:22 -07004990 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004991 while (cgrp != target && cgrp!= cgrp->top_cgroup)
4992 cgrp = cgrp->parent;
4993 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07004994 return ret;
4995}
Paul Menage81a6a5c2007-10-18 23:39:38 -07004996
Paul Menagebd89aab2007-10-18 23:40:44 -07004997static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004998{
4999 /* All of these checks rely on RCU to keep the cgroup
5000 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07005001 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
5002 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07005003 /* Control Group is currently removeable. If it's not
5004 * already queued for a userspace notification, queue
5005 * it now */
5006 int need_schedule_work = 0;
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005007 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07005008 if (!cgroup_is_removed(cgrp) &&
5009 list_empty(&cgrp->release_list)) {
5010 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005011 need_schedule_work = 1;
5012 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005013 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005014 if (need_schedule_work)
5015 schedule_work(&release_agent_work);
5016 }
5017}
5018
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005019/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005020bool __css_tryget(struct cgroup_subsys_state *css)
5021{
Tejun Heoe9316082012-11-05 09:16:58 -08005022 while (true) {
5023 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005024
Tejun Heoe9316082012-11-05 09:16:58 -08005025 v = css_refcnt(css);
5026 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5027 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005028 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005029 else if (t < 0)
5030 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005031 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005032 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005033}
5034EXPORT_SYMBOL_GPL(__css_tryget);
5035
5036/* Caller must verify that the css is not for root cgroup */
5037void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005038{
Paul Menagebd89aab2007-10-18 23:40:44 -07005039 struct cgroup *cgrp = css->cgroup;
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005040 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005041
Paul Menage81a6a5c2007-10-18 23:39:38 -07005042 rcu_read_lock();
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005043 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
5044
5045 switch (v) {
Tejun Heo48ddbe12012-04-01 12:09:56 -07005046 case 1:
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005047 if (notify_on_release(cgrp)) {
5048 set_bit(CGRP_RELEASABLE, &cgrp->flags);
5049 check_for_release(cgrp);
5050 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07005051 break;
5052 case 0:
Tejun Heoed9577932012-11-05 09:16:58 -08005053 schedule_work(&css->dput_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005054 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005055 }
5056 rcu_read_unlock();
5057}
Ben Blum67523c42010-03-10 15:22:11 -08005058EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005059
5060/*
5061 * Notify userspace when a cgroup is released, by running the
5062 * configured release agent with the name of the cgroup (path
5063 * relative to the root of cgroup file system) as the argument.
5064 *
5065 * Most likely, this user command will try to rmdir this cgroup.
5066 *
5067 * This races with the possibility that some other task will be
5068 * attached to this cgroup before it is removed, or that some other
5069 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5070 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5071 * unused, and this cgroup will be reprieved from its death sentence,
5072 * to continue to serve a useful existence. Next time it's released,
5073 * we will get notified again, if it still has 'notify_on_release' set.
5074 *
5075 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5076 * means only wait until the task is successfully execve()'d. The
5077 * separate release agent task is forked by call_usermodehelper(),
5078 * then control in this thread returns here, without waiting for the
5079 * release agent task. We don't bother to wait because the caller of
5080 * this routine has no use for the exit status of the release agent
5081 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005082 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005083static void cgroup_release_agent(struct work_struct *work)
5084{
5085 BUG_ON(work != &release_agent_work);
5086 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005087 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005088 while (!list_empty(&release_list)) {
5089 char *argv[3], *envp[3];
5090 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005091 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005092 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005093 struct cgroup,
5094 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005095 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005096 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005097 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005098 if (!pathbuf)
5099 goto continue_free;
5100 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5101 goto continue_free;
5102 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5103 if (!agentbuf)
5104 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005105
5106 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005107 argv[i++] = agentbuf;
5108 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005109 argv[i] = NULL;
5110
5111 i = 0;
5112 /* minimal command environment */
5113 envp[i++] = "HOME=/";
5114 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5115 envp[i] = NULL;
5116
5117 /* Drop the lock while we invoke the usermode helper,
5118 * since the exec could involve hitting disk and hence
5119 * be a slow process */
5120 mutex_unlock(&cgroup_mutex);
5121 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005122 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005123 continue_free:
5124 kfree(pathbuf);
5125 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005126 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005127 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005128 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005129 mutex_unlock(&cgroup_mutex);
5130}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005131
5132static int __init cgroup_disable(char *str)
5133{
5134 int i;
5135 char *token;
5136
5137 while ((token = strsep(&str, ",")) != NULL) {
5138 if (!*token)
5139 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005140 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005141 struct cgroup_subsys *ss = subsys[i];
5142
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005143 /*
5144 * cgroup_disable, being at boot time, can't
5145 * know about module subsystems, so we don't
5146 * worry about them.
5147 */
5148 if (!ss || ss->module)
5149 continue;
5150
Paul Menage8bab8dd2008-04-04 14:29:57 -07005151 if (!strcmp(token, ss->name)) {
5152 ss->disabled = 1;
5153 printk(KERN_INFO "Disabling %s control group"
5154 " subsystem\n", ss->name);
5155 break;
5156 }
5157 }
5158 }
5159 return 1;
5160}
5161__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005162
5163/*
5164 * Functons for CSS ID.
5165 */
5166
5167/*
5168 *To get ID other than 0, this should be called when !cgroup_is_removed().
5169 */
5170unsigned short css_id(struct cgroup_subsys_state *css)
5171{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005172 struct css_id *cssid;
5173
5174 /*
5175 * This css_id() can return correct value when somone has refcnt
5176 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5177 * it's unchanged until freed.
5178 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005179 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005180
5181 if (cssid)
5182 return cssid->id;
5183 return 0;
5184}
Ben Blum67523c42010-03-10 15:22:11 -08005185EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005186
5187unsigned short css_depth(struct cgroup_subsys_state *css)
5188{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005189 struct css_id *cssid;
5190
Tejun Heo28b4c272012-04-01 12:09:56 -07005191 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005192
5193 if (cssid)
5194 return cssid->depth;
5195 return 0;
5196}
Ben Blum67523c42010-03-10 15:22:11 -08005197EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005198
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005199/**
5200 * css_is_ancestor - test "root" css is an ancestor of "child"
5201 * @child: the css to be tested.
5202 * @root: the css supporsed to be an ancestor of the child.
5203 *
5204 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005205 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005206 * But, considering usual usage, the csses should be valid objects after test.
5207 * Assuming that the caller will do some action to the child if this returns
5208 * returns true, the caller must take "child";s reference count.
5209 * If "child" is valid object and this returns true, "root" is valid, too.
5210 */
5211
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005212bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005213 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005214{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005215 struct css_id *child_id;
5216 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005217
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005218 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005219 if (!child_id)
5220 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005221 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005222 if (!root_id)
5223 return false;
5224 if (child_id->depth < root_id->depth)
5225 return false;
5226 if (child_id->stack[root_id->depth] != root_id->id)
5227 return false;
5228 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005229}
5230
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005231void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5232{
5233 struct css_id *id = css->id;
5234 /* When this is called before css_id initialization, id can be NULL */
5235 if (!id)
5236 return;
5237
5238 BUG_ON(!ss->use_id);
5239
5240 rcu_assign_pointer(id->css, NULL);
5241 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005242 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005243 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005244 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005245 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005246}
Ben Blum67523c42010-03-10 15:22:11 -08005247EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005248
5249/*
5250 * This is called by init or create(). Then, calls to this function are
5251 * always serialized (By cgroup_mutex() at create()).
5252 */
5253
5254static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5255{
5256 struct css_id *newid;
5257 int myid, error, size;
5258
5259 BUG_ON(!ss->use_id);
5260
5261 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5262 newid = kzalloc(size, GFP_KERNEL);
5263 if (!newid)
5264 return ERR_PTR(-ENOMEM);
5265 /* get id */
5266 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
5267 error = -ENOMEM;
5268 goto err_out;
5269 }
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005270 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005271 /* Don't use 0. allocates an ID of 1-65535 */
5272 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005273 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005274
5275 /* Returns error when there are no free spaces for new ID.*/
5276 if (error) {
5277 error = -ENOSPC;
5278 goto err_out;
5279 }
5280 if (myid > CSS_ID_MAX)
5281 goto remove_idr;
5282
5283 newid->id = myid;
5284 newid->depth = depth;
5285 return newid;
5286remove_idr:
5287 error = -ENOSPC;
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005288 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005289 idr_remove(&ss->idr, myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005290 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005291err_out:
5292 kfree(newid);
5293 return ERR_PTR(error);
5294
5295}
5296
Ben Blume6a11052010-03-10 15:22:09 -08005297static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5298 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005299{
5300 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005301
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005302 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005303 idr_init(&ss->idr);
5304
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005305 newid = get_new_cssid(ss, 0);
5306 if (IS_ERR(newid))
5307 return PTR_ERR(newid);
5308
5309 newid->stack[0] = newid->id;
5310 newid->css = rootcss;
5311 rootcss->id = newid;
5312 return 0;
5313}
5314
5315static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5316 struct cgroup *child)
5317{
5318 int subsys_id, i, depth = 0;
5319 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005320 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005321
5322 subsys_id = ss->subsys_id;
5323 parent_css = parent->subsys[subsys_id];
5324 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005325 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005326 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005327
5328 child_id = get_new_cssid(ss, depth);
5329 if (IS_ERR(child_id))
5330 return PTR_ERR(child_id);
5331
5332 for (i = 0; i < depth; i++)
5333 child_id->stack[i] = parent_id->stack[i];
5334 child_id->stack[depth] = child_id->id;
5335 /*
5336 * child_id->css pointer will be set after this cgroup is available
5337 * see cgroup_populate_dir()
5338 */
5339 rcu_assign_pointer(child_css->id, child_id);
5340
5341 return 0;
5342}
5343
5344/**
5345 * css_lookup - lookup css by id
5346 * @ss: cgroup subsys to be looked into.
5347 * @id: the id
5348 *
5349 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5350 * NULL if not. Should be called under rcu_read_lock()
5351 */
5352struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5353{
5354 struct css_id *cssid = NULL;
5355
5356 BUG_ON(!ss->use_id);
5357 cssid = idr_find(&ss->idr, id);
5358
5359 if (unlikely(!cssid))
5360 return NULL;
5361
5362 return rcu_dereference(cssid->css);
5363}
Ben Blum67523c42010-03-10 15:22:11 -08005364EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005365
5366/**
5367 * css_get_next - lookup next cgroup under specified hierarchy.
5368 * @ss: pointer to subsystem
5369 * @id: current position of iteration.
5370 * @root: pointer to css. search tree under this.
5371 * @foundid: position of found object.
5372 *
5373 * Search next css under the specified hierarchy of rootid. Calling under
5374 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5375 */
5376struct cgroup_subsys_state *
5377css_get_next(struct cgroup_subsys *ss, int id,
5378 struct cgroup_subsys_state *root, int *foundid)
5379{
5380 struct cgroup_subsys_state *ret = NULL;
5381 struct css_id *tmp;
5382 int tmpid;
5383 int rootid = css_id(root);
5384 int depth = css_depth(root);
5385
5386 if (!rootid)
5387 return NULL;
5388
5389 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005390 WARN_ON_ONCE(!rcu_read_lock_held());
5391
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005392 /* fill start point for scan */
5393 tmpid = id;
5394 while (1) {
5395 /*
5396 * scan next entry from bitmap(tree), tmpid is updated after
5397 * idr_get_next().
5398 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005399 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005400 if (!tmp)
5401 break;
5402 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5403 ret = rcu_dereference(tmp->css);
5404 if (ret) {
5405 *foundid = tmpid;
5406 break;
5407 }
5408 }
5409 /* continue to scan from next id */
5410 tmpid = tmpid + 1;
5411 }
5412 return ret;
5413}
5414
Stephane Eraniane5d13672011-02-14 11:20:01 +02005415/*
5416 * get corresponding css from file open on cgroupfs directory
5417 */
5418struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5419{
5420 struct cgroup *cgrp;
5421 struct inode *inode;
5422 struct cgroup_subsys_state *css;
5423
5424 inode = f->f_dentry->d_inode;
5425 /* check in cgroup filesystem dir */
5426 if (inode->i_op != &cgroup_dir_inode_operations)
5427 return ERR_PTR(-EBADF);
5428
5429 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5430 return ERR_PTR(-EINVAL);
5431
5432 /* get cgroup */
5433 cgrp = __d_cgrp(f->f_dentry);
5434 css = cgrp->subsys[id];
5435 return css ? css : ERR_PTR(-ENOENT);
5436}
5437
Paul Menagefe693432009-09-23 15:56:20 -07005438#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005439static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005440{
5441 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5442
5443 if (!css)
5444 return ERR_PTR(-ENOMEM);
5445
5446 return css;
5447}
5448
Tejun Heo92fb9742012-11-19 08:13:38 -08005449static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005450{
5451 kfree(cont->subsys[debug_subsys_id]);
5452}
5453
5454static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5455{
5456 return atomic_read(&cont->count);
5457}
5458
5459static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5460{
5461 return cgroup_task_count(cont);
5462}
5463
5464static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5465{
5466 return (u64)(unsigned long)current->cgroups;
5467}
5468
5469static u64 current_css_set_refcount_read(struct cgroup *cont,
5470 struct cftype *cft)
5471{
5472 u64 count;
5473
5474 rcu_read_lock();
5475 count = atomic_read(&current->cgroups->refcount);
5476 rcu_read_unlock();
5477 return count;
5478}
5479
Paul Menage7717f7b2009-09-23 15:56:22 -07005480static int current_css_set_cg_links_read(struct cgroup *cont,
5481 struct cftype *cft,
5482 struct seq_file *seq)
5483{
5484 struct cg_cgroup_link *link;
5485 struct css_set *cg;
5486
5487 read_lock(&css_set_lock);
5488 rcu_read_lock();
5489 cg = rcu_dereference(current->cgroups);
5490 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5491 struct cgroup *c = link->cgrp;
5492 const char *name;
5493
5494 if (c->dentry)
5495 name = c->dentry->d_name.name;
5496 else
5497 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005498 seq_printf(seq, "Root %d group %s\n",
5499 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005500 }
5501 rcu_read_unlock();
5502 read_unlock(&css_set_lock);
5503 return 0;
5504}
5505
5506#define MAX_TASKS_SHOWN_PER_CSS 25
5507static int cgroup_css_links_read(struct cgroup *cont,
5508 struct cftype *cft,
5509 struct seq_file *seq)
5510{
5511 struct cg_cgroup_link *link;
5512
5513 read_lock(&css_set_lock);
5514 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5515 struct css_set *cg = link->cg;
5516 struct task_struct *task;
5517 int count = 0;
5518 seq_printf(seq, "css_set %p\n", cg);
5519 list_for_each_entry(task, &cg->tasks, cg_list) {
5520 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5521 seq_puts(seq, " ...\n");
5522 break;
5523 } else {
5524 seq_printf(seq, " task %d\n",
5525 task_pid_vnr(task));
5526 }
5527 }
5528 }
5529 read_unlock(&css_set_lock);
5530 return 0;
5531}
5532
Paul Menagefe693432009-09-23 15:56:20 -07005533static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5534{
5535 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5536}
5537
5538static struct cftype debug_files[] = {
5539 {
5540 .name = "cgroup_refcount",
5541 .read_u64 = cgroup_refcount_read,
5542 },
5543 {
5544 .name = "taskcount",
5545 .read_u64 = debug_taskcount_read,
5546 },
5547
5548 {
5549 .name = "current_css_set",
5550 .read_u64 = current_css_set_read,
5551 },
5552
5553 {
5554 .name = "current_css_set_refcount",
5555 .read_u64 = current_css_set_refcount_read,
5556 },
5557
5558 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005559 .name = "current_css_set_cg_links",
5560 .read_seq_string = current_css_set_cg_links_read,
5561 },
5562
5563 {
5564 .name = "cgroup_css_links",
5565 .read_seq_string = cgroup_css_links_read,
5566 },
5567
5568 {
Paul Menagefe693432009-09-23 15:56:20 -07005569 .name = "releasable",
5570 .read_u64 = releasable_read,
5571 },
Paul Menagefe693432009-09-23 15:56:20 -07005572
Tejun Heo4baf6e32012-04-01 12:09:55 -07005573 { } /* terminate */
5574};
Paul Menagefe693432009-09-23 15:56:20 -07005575
5576struct cgroup_subsys debug_subsys = {
5577 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005578 .css_alloc = debug_css_alloc,
5579 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005580 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005581 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005582};
5583#endif /* CONFIG_CGROUP_DEBUG */