blob: 1b18090269ad1bb3bffe98e63d7ee16202f50d80 [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 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070095#define SUBSYS(_x) &_x ## _subsys,
Ben Blumaae8aab2010-03-10 15:22:07 -080096static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070097#include <linux/cgroup_subsys.h>
98};
99
Paul Menagec6d57f32009-09-23 15:56:19 -0700100#define MAX_CGROUP_ROOT_NAMELEN 64
101
Paul Menageddbcc7e2007-10-18 23:39:30 -0700102/*
103 * A cgroupfs_root represents the root of a cgroup hierarchy,
104 * and may be associated with a superblock to form an active
105 * hierarchy
106 */
107struct cgroupfs_root {
108 struct super_block *sb;
109
110 /*
111 * The bitmask of subsystems intended to be attached to this
112 * hierarchy
113 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400114 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700115
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700116 /* Unique id for this hierarchy. */
117 int hierarchy_id;
118
Paul Menageddbcc7e2007-10-18 23:39:30 -0700119 /* The bitmask of subsystems currently attached to this hierarchy */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400120 unsigned long actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700121
122 /* A list running through the attached subsystems */
123 struct list_head subsys_list;
124
125 /* The root cgroup for this hierarchy */
126 struct cgroup top_cgroup;
127
128 /* Tracks how many cgroups are currently defined in hierarchy.*/
129 int number_of_cgroups;
130
Li Zefane5f6a862009-01-07 18:07:41 -0800131 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700132 struct list_head root_list;
133
Tejun Heob0ca5a82012-04-01 12:09:54 -0700134 /* All cgroups on this root, cgroup_mutex protected */
135 struct list_head allcg_list;
136
Paul Menageddbcc7e2007-10-18 23:39:30 -0700137 /* Hierarchy-specific flags */
138 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700139
Paul Menagee788e062008-07-25 01:46:59 -0700140 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700141 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700142
143 /* The name for this hierarchy - may be empty */
144 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700145};
146
Paul Menageddbcc7e2007-10-18 23:39:30 -0700147/*
148 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
149 * subsystems that are otherwise unattached - it never has more than a
150 * single cgroup, and all tasks are part of that cgroup.
151 */
152static struct cgroupfs_root rootnode;
153
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700154/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700155 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
156 */
157struct cfent {
158 struct list_head node;
159 struct dentry *dentry;
160 struct cftype *type;
161};
162
163/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700164 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
165 * cgroup_subsys->use_id != 0.
166 */
167#define CSS_ID_MAX (65535)
168struct css_id {
169 /*
170 * The css to which this ID points. This pointer is set to valid value
171 * after cgroup is populated. If cgroup is removed, this will be NULL.
172 * This pointer is expected to be RCU-safe because destroy()
173 * is called after synchronize_rcu(). But for safe use, css_is_removed()
174 * css_tryget() should be used for avoiding race.
175 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100176 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700177 /*
178 * ID of this css.
179 */
180 unsigned short id;
181 /*
182 * Depth in hierarchy which this ID belongs to.
183 */
184 unsigned short depth;
185 /*
186 * ID is freed by RCU. (and lookup routine is RCU safe.)
187 */
188 struct rcu_head rcu_head;
189 /*
190 * Hierarchy of CSS ID belongs to.
191 */
192 unsigned short stack[0]; /* Array of Length (depth+1) */
193};
194
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800195/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300196 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800197 */
198struct cgroup_event {
199 /*
200 * Cgroup which the event belongs to.
201 */
202 struct cgroup *cgrp;
203 /*
204 * Control file which the event associated.
205 */
206 struct cftype *cft;
207 /*
208 * eventfd to signal userspace about the event.
209 */
210 struct eventfd_ctx *eventfd;
211 /*
212 * Each of these stored in a list by the cgroup.
213 */
214 struct list_head list;
215 /*
216 * All fields below needed to unregister event when
217 * userspace closes eventfd.
218 */
219 poll_table pt;
220 wait_queue_head_t *wqh;
221 wait_queue_t wait;
222 struct work_struct remove;
223};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700224
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225/* The list of hierarchy roots */
226
227static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700228static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700230static DEFINE_IDA(hierarchy_ida);
231static int next_hierarchy_id;
232static DEFINE_SPINLOCK(hierarchy_id_lock);
233
Paul Menageddbcc7e2007-10-18 23:39:30 -0700234/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
235#define dummytop (&rootnode.top_cgroup)
236
237/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800238 * check for fork/exit handlers to call. This avoids us having to do
239 * extra work in the fork/exit path if none of the subsystems need to
240 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700241 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700242static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700243
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800244#ifdef CONFIG_PROVE_LOCKING
245int cgroup_lock_is_held(void)
246{
247 return lockdep_is_held(&cgroup_mutex);
248}
249#else /* #ifdef CONFIG_PROVE_LOCKING */
250int cgroup_lock_is_held(void)
251{
252 return mutex_is_locked(&cgroup_mutex);
253}
254#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
255
256EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
257
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700258static int css_unbias_refcnt(int refcnt)
259{
260 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
261}
262
Tejun Heo28b4c272012-04-01 12:09:56 -0700263/* the current nr of refs, always >= 0 whether @css is deactivated or not */
264static int css_refcnt(struct cgroup_subsys_state *css)
265{
266 int v = atomic_read(&css->refcnt);
267
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700268 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700269}
270
Paul Menageddbcc7e2007-10-18 23:39:30 -0700271/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700272inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700273{
Paul Menagebd89aab2007-10-18 23:40:44 -0700274 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700275}
276
277/* bits in struct cgroupfs_root flags field */
278enum {
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400279 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
280 ROOT_XATTR, /* supports extended attributes */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700281};
282
Adrian Bunke9685a02008-02-07 00:13:46 -0800283static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700284{
285 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700286 (1 << CGRP_RELEASABLE) |
287 (1 << CGRP_NOTIFY_ON_RELEASE);
288 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700289}
290
Adrian Bunke9685a02008-02-07 00:13:46 -0800291static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700292{
Paul Menagebd89aab2007-10-18 23:40:44 -0700293 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700294}
295
Daniel Lezcano97978e62010-10-27 15:33:35 -0700296static int clone_children(const struct cgroup *cgrp)
297{
298 return test_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
299}
300
Paul Menageddbcc7e2007-10-18 23:39:30 -0700301/*
302 * for_each_subsys() allows you to iterate on each subsystem attached to
303 * an active hierarchy
304 */
305#define for_each_subsys(_root, _ss) \
306list_for_each_entry(_ss, &_root->subsys_list, sibling)
307
Li Zefane5f6a862009-01-07 18:07:41 -0800308/* for_each_active_root() allows you to iterate across the active hierarchies */
309#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700310list_for_each_entry(_root, &roots, root_list)
311
Tejun Heof6ea9372012-04-01 12:09:55 -0700312static inline struct cgroup *__d_cgrp(struct dentry *dentry)
313{
314 return dentry->d_fsdata;
315}
316
Tejun Heo05ef1d72012-04-01 12:09:56 -0700317static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700318{
319 return dentry->d_fsdata;
320}
321
Tejun Heo05ef1d72012-04-01 12:09:56 -0700322static inline struct cftype *__d_cft(struct dentry *dentry)
323{
324 return __d_cfe(dentry)->type;
325}
326
Paul Menage81a6a5c2007-10-18 23:39:38 -0700327/* the list of cgroups eligible for automatic release. Protected by
328 * release_list_lock */
329static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200330static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700331static void cgroup_release_agent(struct work_struct *work);
332static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700333static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700334
Paul Menage817929e2007-10-18 23:39:36 -0700335/* Link structure for associating css_set objects with cgroups */
336struct cg_cgroup_link {
337 /*
338 * List running through cg_cgroup_links associated with a
339 * cgroup, anchored on cgroup->css_sets
340 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700341 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700342 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700343 /*
344 * List running through cg_cgroup_links pointing at a
345 * single css_set object, anchored on css_set->cg_links
346 */
347 struct list_head cg_link_list;
348 struct css_set *cg;
349};
350
351/* The default css_set - used by init and its children prior to any
352 * hierarchies being mounted. It contains a pointer to the root state
353 * for each subsystem. Also used to anchor the list of css_sets. Not
354 * reference-counted, to improve performance when child cgroups
355 * haven't been created.
356 */
357
358static struct css_set init_css_set;
359static struct cg_cgroup_link init_css_set_link;
360
Ben Blume6a11052010-03-10 15:22:09 -0800361static int cgroup_init_idr(struct cgroup_subsys *ss,
362 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700363
Paul Menage817929e2007-10-18 23:39:36 -0700364/* css_set_lock protects the list of css_set objects, and the
365 * chain of tasks off each css_set. Nests outside task->alloc_lock
366 * due to cgroup_iter_start() */
367static DEFINE_RWLOCK(css_set_lock);
368static int css_set_count;
369
Paul Menage7717f7b2009-09-23 15:56:22 -0700370/*
371 * hash table for cgroup groups. This improves the performance to find
372 * an existing css_set. This hash doesn't (currently) take into
373 * account cgroups in empty hierarchies.
374 */
Li Zefan472b1052008-04-29 01:00:11 -0700375#define CSS_SET_HASH_BITS 7
376#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
377static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
378
379static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
380{
381 int i;
382 int index;
383 unsigned long tmp = 0UL;
384
385 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
386 tmp += (unsigned long)css[i];
387 tmp = (tmp >> 16) ^ tmp;
388
389 index = hash_long(tmp, CSS_SET_HASH_BITS);
390
391 return &css_set_table[index];
392}
393
Paul Menage817929e2007-10-18 23:39:36 -0700394/* We don't maintain the lists running through each css_set to its
395 * task until after the first call to cgroup_iter_start(). This
396 * reduces the fork()/exit() overhead for people who have cgroups
397 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700398static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700399
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700400static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700401{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700402 struct cg_cgroup_link *link;
403 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700404 /*
405 * Ensure that the refcount doesn't hit zero while any readers
406 * can see it. Similar to atomic_dec_and_lock(), but for an
407 * rwlock
408 */
409 if (atomic_add_unless(&cg->refcount, -1, 1))
410 return;
411 write_lock(&css_set_lock);
412 if (!atomic_dec_and_test(&cg->refcount)) {
413 write_unlock(&css_set_lock);
414 return;
415 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700416
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700417 /* This css_set is dead. unlink it and release cgroup refcounts */
418 hlist_del(&cg->hlist);
419 css_set_count--;
420
421 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
422 cg_link_list) {
423 struct cgroup *cgrp = link->cgrp;
424 list_del(&link->cg_link_list);
425 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700426 if (atomic_dec_and_test(&cgrp->count) &&
427 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700428 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700429 set_bit(CGRP_RELEASABLE, &cgrp->flags);
430 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700431 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700432
433 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700434 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700435
436 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800437 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700438}
439
440/*
441 * refcounted get/put for css_set objects
442 */
443static inline void get_css_set(struct css_set *cg)
444{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700445 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700446}
447
448static inline void put_css_set(struct css_set *cg)
449{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700450 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700451}
452
Paul Menage81a6a5c2007-10-18 23:39:38 -0700453static inline void put_css_set_taskexit(struct css_set *cg)
454{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700455 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700456}
457
Paul Menage817929e2007-10-18 23:39:36 -0700458/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700459 * compare_css_sets - helper function for find_existing_css_set().
460 * @cg: candidate css_set being tested
461 * @old_cg: existing css_set for a task
462 * @new_cgrp: cgroup that's being entered by the task
463 * @template: desired set of css pointers in css_set (pre-calculated)
464 *
465 * Returns true if "cg" matches "old_cg" except for the hierarchy
466 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
467 */
468static bool compare_css_sets(struct css_set *cg,
469 struct css_set *old_cg,
470 struct cgroup *new_cgrp,
471 struct cgroup_subsys_state *template[])
472{
473 struct list_head *l1, *l2;
474
475 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
476 /* Not all subsystems matched */
477 return false;
478 }
479
480 /*
481 * Compare cgroup pointers in order to distinguish between
482 * different cgroups in heirarchies with no subsystems. We
483 * could get by with just this check alone (and skip the
484 * memcmp above) but on most setups the memcmp check will
485 * avoid the need for this more expensive check on almost all
486 * candidates.
487 */
488
489 l1 = &cg->cg_links;
490 l2 = &old_cg->cg_links;
491 while (1) {
492 struct cg_cgroup_link *cgl1, *cgl2;
493 struct cgroup *cg1, *cg2;
494
495 l1 = l1->next;
496 l2 = l2->next;
497 /* See if we reached the end - both lists are equal length. */
498 if (l1 == &cg->cg_links) {
499 BUG_ON(l2 != &old_cg->cg_links);
500 break;
501 } else {
502 BUG_ON(l2 == &old_cg->cg_links);
503 }
504 /* Locate the cgroups associated with these links. */
505 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
506 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
507 cg1 = cgl1->cgrp;
508 cg2 = cgl2->cgrp;
509 /* Hierarchies should be linked in the same order. */
510 BUG_ON(cg1->root != cg2->root);
511
512 /*
513 * If this hierarchy is the hierarchy of the cgroup
514 * that's changing, then we need to check that this
515 * css_set points to the new cgroup; if it's any other
516 * hierarchy, then this css_set should point to the
517 * same cgroup as the old css_set.
518 */
519 if (cg1->root == new_cgrp->root) {
520 if (cg1 != new_cgrp)
521 return false;
522 } else {
523 if (cg1 != cg2)
524 return false;
525 }
526 }
527 return true;
528}
529
530/*
Paul Menage817929e2007-10-18 23:39:36 -0700531 * find_existing_css_set() is a helper for
532 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700533 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700534 *
535 * oldcg: the cgroup group that we're using before the cgroup
536 * transition
537 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700538 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700539 *
540 * template: location in which to build the desired set of subsystem
541 * state objects for the new cgroup group
542 */
Paul Menage817929e2007-10-18 23:39:36 -0700543static struct css_set *find_existing_css_set(
544 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700545 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700546 struct cgroup_subsys_state *template[])
547{
548 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700549 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700550 struct hlist_head *hhead;
551 struct hlist_node *node;
552 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700553
Ben Blumaae8aab2010-03-10 15:22:07 -0800554 /*
555 * Build the set of subsystem state objects that we want to see in the
556 * new css_set. while subsystems can change globally, the entries here
557 * won't change, so no need for locking.
558 */
Paul Menage817929e2007-10-18 23:39:36 -0700559 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400560 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700561 /* Subsystem is in this hierarchy. So we want
562 * the subsystem state from the new
563 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700564 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700565 } else {
566 /* Subsystem is not in this hierarchy, so we
567 * don't want to change the subsystem state */
568 template[i] = oldcg->subsys[i];
569 }
570 }
571
Li Zefan472b1052008-04-29 01:00:11 -0700572 hhead = css_set_hash(template);
573 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700574 if (!compare_css_sets(cg, oldcg, cgrp, template))
575 continue;
576
577 /* This css_set matches what we need */
578 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700579 }
Paul Menage817929e2007-10-18 23:39:36 -0700580
581 /* No existing cgroup group matched */
582 return NULL;
583}
584
Paul Menage817929e2007-10-18 23:39:36 -0700585static void free_cg_links(struct list_head *tmp)
586{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700587 struct cg_cgroup_link *link;
588 struct cg_cgroup_link *saved_link;
589
590 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700591 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700592 kfree(link);
593 }
594}
595
596/*
Li Zefan36553432008-07-29 22:33:19 -0700597 * allocate_cg_links() allocates "count" cg_cgroup_link structures
598 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
599 * success or a negative error
600 */
601static int allocate_cg_links(int count, struct list_head *tmp)
602{
603 struct cg_cgroup_link *link;
604 int i;
605 INIT_LIST_HEAD(tmp);
606 for (i = 0; i < count; i++) {
607 link = kmalloc(sizeof(*link), GFP_KERNEL);
608 if (!link) {
609 free_cg_links(tmp);
610 return -ENOMEM;
611 }
612 list_add(&link->cgrp_link_list, tmp);
613 }
614 return 0;
615}
616
Li Zefanc12f65d2009-01-07 18:07:42 -0800617/**
618 * link_css_set - a helper function to link a css_set to a cgroup
619 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
620 * @cg: the css_set to be linked
621 * @cgrp: the destination cgroup
622 */
623static void link_css_set(struct list_head *tmp_cg_links,
624 struct css_set *cg, struct cgroup *cgrp)
625{
626 struct cg_cgroup_link *link;
627
628 BUG_ON(list_empty(tmp_cg_links));
629 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
630 cgrp_link_list);
631 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700632 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700633 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800634 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700635 /*
636 * Always add links to the tail of the list so that the list
637 * is sorted by order of hierarchy creation
638 */
639 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800640}
641
Li Zefan36553432008-07-29 22:33:19 -0700642/*
Paul Menage817929e2007-10-18 23:39:36 -0700643 * find_css_set() takes an existing cgroup group and a
644 * cgroup object, and returns a css_set object that's
645 * equivalent to the old group, but with the given cgroup
646 * substituted into the appropriate hierarchy. Must be called with
647 * cgroup_mutex held
648 */
Paul Menage817929e2007-10-18 23:39:36 -0700649static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700650 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700651{
652 struct css_set *res;
653 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700654
655 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700656
Li Zefan472b1052008-04-29 01:00:11 -0700657 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700658 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700659
Paul Menage817929e2007-10-18 23:39:36 -0700660 /* First see if we already have a cgroup group that matches
661 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700662 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700663 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700664 if (res)
665 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700666 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700667
668 if (res)
669 return res;
670
671 res = kmalloc(sizeof(*res), GFP_KERNEL);
672 if (!res)
673 return NULL;
674
675 /* Allocate all the cg_cgroup_link objects that we'll need */
676 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
677 kfree(res);
678 return NULL;
679 }
680
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700681 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700682 INIT_LIST_HEAD(&res->cg_links);
683 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700684 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700685
686 /* Copy the set of subsystem state objects generated in
687 * find_existing_css_set() */
688 memcpy(res->subsys, template, sizeof(res->subsys));
689
690 write_lock(&css_set_lock);
691 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700692 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
693 struct cgroup *c = link->cgrp;
694 if (c->root == cgrp->root)
695 c = cgrp;
696 link_css_set(&tmp_cg_links, res, c);
697 }
Paul Menage817929e2007-10-18 23:39:36 -0700698
699 BUG_ON(!list_empty(&tmp_cg_links));
700
Paul Menage817929e2007-10-18 23:39:36 -0700701 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700702
703 /* Add this cgroup group to the hash table */
704 hhead = css_set_hash(res->subsys);
705 hlist_add_head(&res->hlist, hhead);
706
Paul Menage817929e2007-10-18 23:39:36 -0700707 write_unlock(&css_set_lock);
708
709 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700710}
711
Paul Menageddbcc7e2007-10-18 23:39:30 -0700712/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700713 * Return the cgroup for "task" from the given hierarchy. Must be
714 * called with cgroup_mutex held.
715 */
716static struct cgroup *task_cgroup_from_root(struct task_struct *task,
717 struct cgroupfs_root *root)
718{
719 struct css_set *css;
720 struct cgroup *res = NULL;
721
722 BUG_ON(!mutex_is_locked(&cgroup_mutex));
723 read_lock(&css_set_lock);
724 /*
725 * No need to lock the task - since we hold cgroup_mutex the
726 * task can't change groups, so the only thing that can happen
727 * is that it exits and its css is set back to init_css_set.
728 */
729 css = task->cgroups;
730 if (css == &init_css_set) {
731 res = &root->top_cgroup;
732 } else {
733 struct cg_cgroup_link *link;
734 list_for_each_entry(link, &css->cg_links, cg_link_list) {
735 struct cgroup *c = link->cgrp;
736 if (c->root == root) {
737 res = c;
738 break;
739 }
740 }
741 }
742 read_unlock(&css_set_lock);
743 BUG_ON(!res);
744 return res;
745}
746
747/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700748 * There is one global cgroup mutex. We also require taking
749 * task_lock() when dereferencing a task's cgroup subsys pointers.
750 * See "The task_lock() exception", at the end of this comment.
751 *
752 * A task must hold cgroup_mutex to modify cgroups.
753 *
754 * Any task can increment and decrement the count field without lock.
755 * So in general, code holding cgroup_mutex can't rely on the count
756 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800757 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700758 * means that no tasks are currently attached, therefore there is no
759 * way a task attached to that cgroup can fork (the other way to
760 * increment the count). So code holding cgroup_mutex can safely
761 * assume that if the count is zero, it will stay zero. Similarly, if
762 * a task holds cgroup_mutex on a cgroup with zero count, it
763 * knows that the cgroup won't be removed, as cgroup_rmdir()
764 * needs that mutex.
765 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700766 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
767 * (usually) take cgroup_mutex. These are the two most performance
768 * critical pieces of code here. The exception occurs on cgroup_exit(),
769 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
770 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800771 * to the release agent with the name of the cgroup (path relative to
772 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773 *
774 * A cgroup can only be deleted if both its 'count' of using tasks
775 * is zero, and its list of 'children' cgroups is empty. Since all
776 * tasks in the system use _some_ cgroup, and since there is always at
777 * least one task in the system (init, pid == 1), therefore, top_cgroup
778 * always has either children cgroups and/or using tasks. So we don't
779 * need a special hack to ensure that top_cgroup cannot be deleted.
780 *
781 * The task_lock() exception
782 *
783 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800784 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800785 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700786 * several performance critical places that need to reference
787 * task->cgroup without the expense of grabbing a system global
788 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800789 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700790 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
791 * the task_struct routinely used for such matters.
792 *
793 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800794 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700795 */
796
Paul Menageddbcc7e2007-10-18 23:39:30 -0700797/**
798 * cgroup_lock - lock out any changes to cgroup structures
799 *
800 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700801void cgroup_lock(void)
802{
803 mutex_lock(&cgroup_mutex);
804}
Ben Blum67523c42010-03-10 15:22:11 -0800805EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806
807/**
808 * cgroup_unlock - release lock on cgroup changes
809 *
810 * Undo the lock taken in a previous cgroup_lock() call.
811 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700812void cgroup_unlock(void)
813{
814 mutex_unlock(&cgroup_mutex);
815}
Ben Blum67523c42010-03-10 15:22:11 -0800816EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700817
818/*
819 * A couple of forward declarations required, due to cyclic reference loop:
820 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
821 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
822 * -> cgroup_mkdir.
823 */
824
Al Viro18bb1db2011-07-26 01:41:39 -0400825static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400826static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700827static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400828static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
829 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700830static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700831static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700832
833static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200834 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700835 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700836};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700837
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700838static int alloc_css_id(struct cgroup_subsys *ss,
839 struct cgroup *parent, struct cgroup *child);
840
Al Viroa5e7ed32011-07-26 01:55:55 -0400841static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700842{
843 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700844
845 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400846 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700847 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100848 inode->i_uid = current_fsuid();
849 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700850 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
851 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
852 }
853 return inode;
854}
855
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800856/*
857 * Call subsys's pre_destroy handler.
858 * This is called before css refcnt check.
859 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700860static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800861{
862 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700863 int ret = 0;
864
Tejun Heo48ddbe12012-04-01 12:09:56 -0700865 for_each_subsys(cgrp->root, ss) {
866 if (!ss->pre_destroy)
867 continue;
868
869 ret = ss->pre_destroy(cgrp);
870 if (ret) {
871 /* ->pre_destroy() failure is being deprecated */
872 WARN_ON_ONCE(!ss->__DEPRECATED_clear_css_refs);
873 break;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700874 }
Tejun Heo48ddbe12012-04-01 12:09:56 -0700875 }
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800876
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700877 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800878}
879
Paul Menageddbcc7e2007-10-18 23:39:30 -0700880static void cgroup_diput(struct dentry *dentry, struct inode *inode)
881{
882 /* is dentry a directory ? if so, kfree() associated cgroup */
883 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700884 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800885 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700886 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700887 /* It's possible for external users to be holding css
888 * reference counts on a cgroup; css_put() needs to
889 * be able to access the cgroup after decrementing
890 * the reference count in order to know if it needs to
891 * queue the cgroup to be handled by the release
892 * agent */
893 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800894
895 mutex_lock(&cgroup_mutex);
896 /*
897 * Release the subsystem state objects.
898 */
Li Zefan75139b82009-01-07 18:07:33 -0800899 for_each_subsys(cgrp->root, ss)
Li Zefan761b3ef2012-01-31 13:47:36 +0800900 ss->destroy(cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800901
902 cgrp->root->number_of_cgroups--;
903 mutex_unlock(&cgroup_mutex);
904
Paul Menagea47295e2009-01-07 18:07:44 -0800905 /*
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700906 * Drop the active superblock reference that we took when we
907 * created the cgroup
Paul Menagea47295e2009-01-07 18:07:44 -0800908 */
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700909 deactivate_super(cgrp->root->sb);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800910
Ben Blum72a8cb32009-09-23 15:56:27 -0700911 /*
912 * if we're getting rid of the cgroup, refcount should ensure
913 * that there are no pidlists left.
914 */
915 BUG_ON(!list_empty(&cgrp->pidlists));
916
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400917 simple_xattrs_free(&cgrp->xattrs);
918
Lai Jiangshanf2da1c42011-03-15 17:55:16 +0800919 kfree_rcu(cgrp, rcu_head);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700920 } else {
921 struct cfent *cfe = __d_cfe(dentry);
922 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400923 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700924
925 WARN_ONCE(!list_empty(&cfe->node) &&
926 cgrp != &cgrp->root->top_cgroup,
927 "cfe still linked for %s\n", cfe->type->name);
928 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400929 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700930 }
931 iput(inode);
932}
933
Al Viroc72a04e2011-01-14 05:31:45 +0000934static int cgroup_delete(const struct dentry *d)
935{
936 return 1;
937}
938
Paul Menageddbcc7e2007-10-18 23:39:30 -0700939static void remove_dir(struct dentry *d)
940{
941 struct dentry *parent = dget(d->d_parent);
942
943 d_delete(d);
944 simple_rmdir(parent->d_inode, d);
945 dput(parent);
946}
947
Tejun Heo05ef1d72012-04-01 12:09:56 -0700948static int cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700949{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700950 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700951
Tejun Heo05ef1d72012-04-01 12:09:56 -0700952 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
953 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100954
Tejun Heo05ef1d72012-04-01 12:09:56 -0700955 list_for_each_entry(cfe, &cgrp->files, node) {
956 struct dentry *d = cfe->dentry;
957
958 if (cft && cfe->type != cft)
959 continue;
960
961 dget(d);
962 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700963 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700964 list_del_init(&cfe->node);
965 dput(d);
966
967 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700969 return -ENOENT;
970}
971
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400972/**
973 * cgroup_clear_directory - selective removal of base and subsystem files
974 * @dir: directory containing the files
975 * @base_files: true if the base files should be removed
976 * @subsys_mask: mask of the subsystem ids whose files should be removed
977 */
978static void cgroup_clear_directory(struct dentry *dir, bool base_files,
979 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700980{
981 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400982 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700983
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400984 for_each_subsys(cgrp->root, ss) {
985 struct cftype_set *set;
986 if (!test_bit(ss->subsys_id, &subsys_mask))
987 continue;
988 list_for_each_entry(set, &ss->cftsets, node)
989 cgroup_rm_file(cgrp, set->cfts);
990 }
991 if (base_files) {
992 while (!list_empty(&cgrp->files))
993 cgroup_rm_file(cgrp, NULL);
994 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995}
996
997/*
998 * NOTE : the dentry must have been dget()'ed
999 */
1000static void cgroup_d_remove_dir(struct dentry *dentry)
1001{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001002 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001003 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001004
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001005 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001007 parent = dentry->d_parent;
1008 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001009 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001011 spin_unlock(&dentry->d_lock);
1012 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 remove_dir(dentry);
1014}
1015
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001016/*
1017 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
1018 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
1019 * reference to css->refcnt. In general, this refcnt is expected to goes down
1020 * to zero, soon.
1021 *
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001022 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001023 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02001024static DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001025
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001026static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001027{
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001028 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001029 wake_up_all(&cgroup_rmdir_waitq);
1030}
1031
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001032void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
1033{
1034 css_get(css);
1035}
1036
1037void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
1038{
1039 cgroup_wakeup_rmdir_waiter(css->cgroup);
1040 css_put(css);
1041}
1042
Ben Blumaae8aab2010-03-10 15:22:07 -08001043/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001044 * Call with cgroup_mutex held. Drops reference counts on modules, including
1045 * any duplicate ones that parse_cgroupfs_options took. If this function
1046 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001047 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001049 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001050{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001051 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001052 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053 int i;
1054
Ben Blumaae8aab2010-03-10 15:22:07 -08001055 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001056 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001057
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001058 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1059 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060 /* Check that any added subsystems are currently free */
1061 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001062 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001064 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001066 /*
1067 * Nobody should tell us to do a subsys that doesn't exist:
1068 * parse_cgroupfs_options should catch that case and refcounts
1069 * ensure that subsystems won't disappear once selected.
1070 */
1071 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072 if (ss->root != &rootnode) {
1073 /* Subsystem isn't free */
1074 return -EBUSY;
1075 }
1076 }
1077
1078 /* Currently we don't handle adding/removing subsystems when
1079 * any child cgroups exist. This is theoretically supportable
1080 * but involves complex error handling, so it's being left until
1081 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001082 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 return -EBUSY;
1084
1085 /* Process each subsystem */
1086 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1087 struct cgroup_subsys *ss = subsys[i];
1088 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001089 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001090 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001091 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001092 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093 BUG_ON(!dummytop->subsys[i]);
1094 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001095 cgrp->subsys[i] = dummytop->subsys[i];
1096 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001097 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001098 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001100 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001101 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001102 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001103 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001104 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001105 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1106 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001108 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001110 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001111 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001112 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001113 /* subsystem is now free - drop reference on module */
1114 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001115 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001116 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001117 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001118 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001119 /*
1120 * a refcount was taken, but we already had one, so
1121 * drop the extra reference.
1122 */
1123 module_put(ss->module);
1124#ifdef CONFIG_MODULE_UNLOAD
1125 BUG_ON(ss->module && !module_refcount(ss->module));
1126#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127 } else {
1128 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001129 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001130 }
1131 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001132 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133 synchronize_rcu();
1134
1135 return 0;
1136}
1137
Al Viro34c80b12011-12-08 21:32:45 -05001138static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001139{
Al Viro34c80b12011-12-08 21:32:45 -05001140 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001141 struct cgroup_subsys *ss;
1142
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001143 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001144 for_each_subsys(root, ss)
1145 seq_printf(seq, ",%s", ss->name);
1146 if (test_bit(ROOT_NOPREFIX, &root->flags))
1147 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001148 if (test_bit(ROOT_XATTR, &root->flags))
1149 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001150 if (strlen(root->release_agent_path))
1151 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001152 if (clone_children(&root->top_cgroup))
1153 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001154 if (strlen(root->name))
1155 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001156 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001157 return 0;
1158}
1159
1160struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001161 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001162 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001163 char *release_agent;
Daniel Lezcano97978e62010-10-27 15:33:35 -07001164 bool clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001165 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001166 /* User explicitly requested empty subsystem */
1167 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001168
1169 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001170
Paul Menageddbcc7e2007-10-18 23:39:30 -07001171};
1172
Ben Blumaae8aab2010-03-10 15:22:07 -08001173/*
1174 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001175 * with cgroup_mutex held to protect the subsys[] array. This function takes
1176 * refcounts on subsystems to be used, unless it returns error, in which case
1177 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001178 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001179static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001180{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001181 char *token, *o = data;
1182 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001183 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001184 int i;
1185 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001186
Ben Blumaae8aab2010-03-10 15:22:07 -08001187 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1188
Li Zefanf9ab5b52009-06-17 16:26:33 -07001189#ifdef CONFIG_CPUSETS
1190 mask = ~(1UL << cpuset_subsys_id);
1191#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001192
Paul Menagec6d57f32009-09-23 15:56:19 -07001193 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001194
1195 while ((token = strsep(&o, ",")) != NULL) {
1196 if (!*token)
1197 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001198 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001199 /* Explicitly have no subsystems */
1200 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001201 continue;
1202 }
1203 if (!strcmp(token, "all")) {
1204 /* Mutually exclusive option 'all' + subsystem name */
1205 if (one_ss)
1206 return -EINVAL;
1207 all_ss = true;
1208 continue;
1209 }
1210 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001211 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001212 continue;
1213 }
1214 if (!strcmp(token, "clone_children")) {
Daniel Lezcano97978e62010-10-27 15:33:35 -07001215 opts->clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001216 continue;
1217 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001218 if (!strcmp(token, "xattr")) {
1219 set_bit(ROOT_XATTR, &opts->flags);
1220 continue;
1221 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001222 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001223 /* Specifying two release agents is forbidden */
1224 if (opts->release_agent)
1225 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001226 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001227 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001228 if (!opts->release_agent)
1229 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001230 continue;
1231 }
1232 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001233 const char *name = token + 5;
1234 /* Can't specify an empty name */
1235 if (!strlen(name))
1236 return -EINVAL;
1237 /* Must match [\w.-]+ */
1238 for (i = 0; i < strlen(name); i++) {
1239 char c = name[i];
1240 if (isalnum(c))
1241 continue;
1242 if ((c == '.') || (c == '-') || (c == '_'))
1243 continue;
1244 return -EINVAL;
1245 }
1246 /* Specifying two names is forbidden */
1247 if (opts->name)
1248 return -EINVAL;
1249 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001250 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001251 GFP_KERNEL);
1252 if (!opts->name)
1253 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001254
1255 continue;
1256 }
1257
1258 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1259 struct cgroup_subsys *ss = subsys[i];
1260 if (ss == NULL)
1261 continue;
1262 if (strcmp(token, ss->name))
1263 continue;
1264 if (ss->disabled)
1265 continue;
1266
1267 /* Mutually exclusive option 'all' + subsystem name */
1268 if (all_ss)
1269 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001270 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001271 one_ss = true;
1272
1273 break;
1274 }
1275 if (i == CGROUP_SUBSYS_COUNT)
1276 return -ENOENT;
1277 }
1278
1279 /*
1280 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001281 * otherwise if 'none', 'name=' and a subsystem name options
1282 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001283 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001284 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001285 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1286 struct cgroup_subsys *ss = subsys[i];
1287 if (ss == NULL)
1288 continue;
1289 if (ss->disabled)
1290 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001291 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001292 }
1293 }
1294
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001295 /* Consistency checks */
1296
Li Zefanf9ab5b52009-06-17 16:26:33 -07001297 /*
1298 * Option noprefix was introduced just for backward compatibility
1299 * with the old cpuset, so we allow noprefix only if mounting just
1300 * the cpuset subsystem.
1301 */
1302 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001303 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001304 return -EINVAL;
1305
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001306
1307 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001308 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001309 return -EINVAL;
1310
1311 /*
1312 * We either have to specify by name or by subsystems. (So all
1313 * empty hierarchies must have a name).
1314 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001315 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001316 return -EINVAL;
1317
Ben Blumcf5d5942010-03-10 15:22:09 -08001318 /*
1319 * Grab references on all the modules we'll need, so the subsystems
1320 * don't dance around before rebind_subsystems attaches them. This may
1321 * take duplicate reference counts on a subsystem that's already used,
1322 * but rebind_subsystems handles this case.
1323 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001324 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001325 unsigned long bit = 1UL << i;
1326
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001327 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001328 continue;
1329 if (!try_module_get(subsys[i]->module)) {
1330 module_pin_failed = true;
1331 break;
1332 }
1333 }
1334 if (module_pin_failed) {
1335 /*
1336 * oops, one of the modules was going away. this means that we
1337 * raced with a module_delete call, and to the user this is
1338 * essentially a "subsystem doesn't exist" case.
1339 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001340 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001341 /* drop refcounts only on the ones we took */
1342 unsigned long bit = 1UL << i;
1343
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001344 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001345 continue;
1346 module_put(subsys[i]->module);
1347 }
1348 return -ENOENT;
1349 }
1350
Paul Menageddbcc7e2007-10-18 23:39:30 -07001351 return 0;
1352}
1353
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001354static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001355{
1356 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001357 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001358 unsigned long bit = 1UL << i;
1359
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001360 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001361 continue;
1362 module_put(subsys[i]->module);
1363 }
1364}
1365
Paul Menageddbcc7e2007-10-18 23:39:30 -07001366static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1367{
1368 int ret = 0;
1369 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001370 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001372 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001373
Paul Menagebd89aab2007-10-18 23:40:44 -07001374 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001375 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001376 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001377
1378 /* See what subsystems are wanted */
1379 ret = parse_cgroupfs_options(data, &opts);
1380 if (ret)
1381 goto out_unlock;
1382
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001383 /* See feature-removal-schedule.txt */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001384 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001385 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1386 task_tgid_nr(current), current->comm);
1387
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001388 added_mask = opts.subsys_mask & ~root->subsys_mask;
1389 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001390
Ben Blumcf5d5942010-03-10 15:22:09 -08001391 /* Don't allow flags or name to change at remount */
1392 if (opts.flags != root->flags ||
1393 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001395 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001396 goto out_unlock;
1397 }
1398
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001399 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001400 if (ret) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001401 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001402 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001403 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001404
Tejun Heoff4c8d52012-04-01 12:09:54 -07001405 /* clear out any existing files and repopulate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001406 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001407 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001408 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001409
Paul Menage81a6a5c2007-10-18 23:39:38 -07001410 if (opts.release_agent)
1411 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001412 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001413 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001414 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001415 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001416 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001417 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001418 return ret;
1419}
1420
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001421static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001422 .statfs = simple_statfs,
1423 .drop_inode = generic_delete_inode,
1424 .show_options = cgroup_show_options,
1425 .remount_fs = cgroup_remount,
1426};
1427
Paul Menagecc31edc2008-10-18 20:28:04 -07001428static void init_cgroup_housekeeping(struct cgroup *cgrp)
1429{
1430 INIT_LIST_HEAD(&cgrp->sibling);
1431 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001432 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001433 INIT_LIST_HEAD(&cgrp->css_sets);
1434 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001435 INIT_LIST_HEAD(&cgrp->pidlists);
1436 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001437 INIT_LIST_HEAD(&cgrp->event_list);
1438 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001439 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001440}
Paul Menagec6d57f32009-09-23 15:56:19 -07001441
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442static void init_cgroup_root(struct cgroupfs_root *root)
1443{
Paul Menagebd89aab2007-10-18 23:40:44 -07001444 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001445
Paul Menageddbcc7e2007-10-18 23:39:30 -07001446 INIT_LIST_HEAD(&root->subsys_list);
1447 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001448 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001449 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001450 cgrp->root = root;
1451 cgrp->top_cgroup = cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001452 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menagecc31edc2008-10-18 20:28:04 -07001453 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454}
1455
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001456static bool init_root_id(struct cgroupfs_root *root)
1457{
1458 int ret = 0;
1459
1460 do {
1461 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1462 return false;
1463 spin_lock(&hierarchy_id_lock);
1464 /* Try to allocate the next unused ID */
1465 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1466 &root->hierarchy_id);
1467 if (ret == -ENOSPC)
1468 /* Try again starting from 0 */
1469 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1470 if (!ret) {
1471 next_hierarchy_id = root->hierarchy_id + 1;
1472 } else if (ret != -EAGAIN) {
1473 /* Can only get here if the 31-bit IDR is full ... */
1474 BUG_ON(ret);
1475 }
1476 spin_unlock(&hierarchy_id_lock);
1477 } while (ret);
1478 return true;
1479}
1480
Paul Menageddbcc7e2007-10-18 23:39:30 -07001481static int cgroup_test_super(struct super_block *sb, void *data)
1482{
Paul Menagec6d57f32009-09-23 15:56:19 -07001483 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001484 struct cgroupfs_root *root = sb->s_fs_info;
1485
Paul Menagec6d57f32009-09-23 15:56:19 -07001486 /* If we asked for a name then it must match */
1487 if (opts->name && strcmp(opts->name, root->name))
1488 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001489
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001490 /*
1491 * If we asked for subsystems (or explicitly for no
1492 * subsystems) then they must match
1493 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001494 if ((opts->subsys_mask || opts->none)
1495 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001496 return 0;
1497
1498 return 1;
1499}
1500
Paul Menagec6d57f32009-09-23 15:56:19 -07001501static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1502{
1503 struct cgroupfs_root *root;
1504
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001505 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001506 return NULL;
1507
1508 root = kzalloc(sizeof(*root), GFP_KERNEL);
1509 if (!root)
1510 return ERR_PTR(-ENOMEM);
1511
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001512 if (!init_root_id(root)) {
1513 kfree(root);
1514 return ERR_PTR(-ENOMEM);
1515 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001516 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001517
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001518 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001519 root->flags = opts->flags;
1520 if (opts->release_agent)
1521 strcpy(root->release_agent_path, opts->release_agent);
1522 if (opts->name)
1523 strcpy(root->name, opts->name);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001524 if (opts->clone_children)
1525 set_bit(CGRP_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001526 return root;
1527}
1528
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001529static void cgroup_drop_root(struct cgroupfs_root *root)
1530{
1531 if (!root)
1532 return;
1533
1534 BUG_ON(!root->hierarchy_id);
1535 spin_lock(&hierarchy_id_lock);
1536 ida_remove(&hierarchy_ida, root->hierarchy_id);
1537 spin_unlock(&hierarchy_id_lock);
1538 kfree(root);
1539}
1540
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541static int cgroup_set_super(struct super_block *sb, void *data)
1542{
1543 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001544 struct cgroup_sb_opts *opts = data;
1545
1546 /* If we don't have a new root, we can't set up a new sb */
1547 if (!opts->new_root)
1548 return -EINVAL;
1549
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001550 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551
1552 ret = set_anon_super(sb, NULL);
1553 if (ret)
1554 return ret;
1555
Paul Menagec6d57f32009-09-23 15:56:19 -07001556 sb->s_fs_info = opts->new_root;
1557 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558
1559 sb->s_blocksize = PAGE_CACHE_SIZE;
1560 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1561 sb->s_magic = CGROUP_SUPER_MAGIC;
1562 sb->s_op = &cgroup_ops;
1563
1564 return 0;
1565}
1566
1567static int cgroup_get_rootdir(struct super_block *sb)
1568{
Al Viro0df6a632010-12-21 13:29:29 -05001569 static const struct dentry_operations cgroup_dops = {
1570 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001571 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001572 };
1573
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574 struct inode *inode =
1575 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576
1577 if (!inode)
1578 return -ENOMEM;
1579
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580 inode->i_fop = &simple_dir_operations;
1581 inode->i_op = &cgroup_dir_inode_operations;
1582 /* directories start off with i_nlink == 2 (for "." entry) */
1583 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001584 sb->s_root = d_make_root(inode);
1585 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001587 /* for everything else we want ->d_op set */
1588 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001589 return 0;
1590}
1591
Al Virof7e83572010-07-26 13:23:11 +04001592static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001593 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001594 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595{
1596 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001597 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001598 int ret = 0;
1599 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001600 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001601 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001602
1603 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001604 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001606 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001607 if (ret)
1608 goto out_err;
1609
1610 /*
1611 * Allocate a new cgroup root. We may not need it if we're
1612 * reusing an existing hierarchy.
1613 */
1614 new_root = cgroup_root_from_opts(&opts);
1615 if (IS_ERR(new_root)) {
1616 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001617 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001618 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001619 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001620
Paul Menagec6d57f32009-09-23 15:56:19 -07001621 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001622 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001624 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001625 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001626 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001627 }
1628
Paul Menagec6d57f32009-09-23 15:56:19 -07001629 root = sb->s_fs_info;
1630 BUG_ON(!root);
1631 if (root == opts.new_root) {
1632 /* We used the new root structure, so this is a new hierarchy */
1633 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001634 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001635 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001636 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001637 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001638
1639 BUG_ON(sb->s_root != NULL);
1640
1641 ret = cgroup_get_rootdir(sb);
1642 if (ret)
1643 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001644 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001645
Paul Menage817929e2007-10-18 23:39:36 -07001646 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001648 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001650 /* Check for name clashes with existing mounts */
1651 ret = -EBUSY;
1652 if (strlen(root->name))
1653 for_each_active_root(existing_root)
1654 if (!strcmp(existing_root->name, root->name))
1655 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001656
Paul Menage817929e2007-10-18 23:39:36 -07001657 /*
1658 * We're accessing css_set_count without locking
1659 * css_set_lock here, but that's OK - it can only be
1660 * increased by someone holding cgroup_lock, and
1661 * that's us. The worst that can happen is that we
1662 * have some link structures left over
1663 */
1664 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001665 if (ret)
1666 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001667
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001668 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001669 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001670 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001671 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001673 /*
1674 * There must be no failure case after here, since rebinding
1675 * takes care of subsystems' refcounts, which are explicitly
1676 * dropped in the failure exit path.
1677 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001678
1679 /* EBUSY should be the only error here */
1680 BUG_ON(ret);
1681
1682 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001683 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684
Li Zefanc12f65d2009-01-07 18:07:42 -08001685 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001686 root->top_cgroup.dentry = sb->s_root;
1687
Paul Menage817929e2007-10-18 23:39:36 -07001688 /* Link the top cgroup in this hierarchy into all
1689 * the css_set objects */
1690 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001691 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1692 struct hlist_head *hhead = &css_set_table[i];
1693 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001694 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001695
Li Zefanc12f65d2009-01-07 18:07:42 -08001696 hlist_for_each_entry(cg, node, hhead, hlist)
1697 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001698 }
Paul Menage817929e2007-10-18 23:39:36 -07001699 write_unlock(&css_set_lock);
1700
1701 free_cg_links(&tmp_cg_links);
1702
Li Zefanc12f65d2009-01-07 18:07:42 -08001703 BUG_ON(!list_empty(&root_cgrp->sibling));
1704 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705 BUG_ON(root->number_of_cgroups != 1);
1706
eparis@redhat2ce97382011-06-02 21:20:51 +10001707 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001708 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001709 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001710 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001711 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001712 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001713 } else {
1714 /*
1715 * We re-used an existing hierarchy - the new root (if
1716 * any) is not needed
1717 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001718 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001719 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001720 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001721 }
1722
Paul Menagec6d57f32009-09-23 15:56:19 -07001723 kfree(opts.release_agent);
1724 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001725 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001726
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001727 unlock_drop:
1728 mutex_unlock(&cgroup_root_mutex);
1729 mutex_unlock(&cgroup_mutex);
1730 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001731 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001732 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001733 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001734 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001735 out_err:
1736 kfree(opts.release_agent);
1737 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001738 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739}
1740
1741static void cgroup_kill_sb(struct super_block *sb) {
1742 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001743 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001745 struct cg_cgroup_link *link;
1746 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001747
1748 BUG_ON(!root);
1749
1750 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001751 BUG_ON(!list_empty(&cgrp->children));
1752 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753
1754 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001755 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001756
1757 /* Rebind all subsystems back to the default hierarchy */
1758 ret = rebind_subsystems(root, 0);
1759 /* Shouldn't be able to fail ... */
1760 BUG_ON(ret);
1761
Paul Menage817929e2007-10-18 23:39:36 -07001762 /*
1763 * Release all the links from css_sets to this hierarchy's
1764 * root cgroup
1765 */
1766 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001767
1768 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1769 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001770 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001771 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001772 kfree(link);
1773 }
1774 write_unlock(&css_set_lock);
1775
Paul Menage839ec542009-01-29 14:25:22 -08001776 if (!list_empty(&root->root_list)) {
1777 list_del(&root->root_list);
1778 root_count--;
1779 }
Li Zefane5f6a862009-01-07 18:07:41 -08001780
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001781 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 mutex_unlock(&cgroup_mutex);
1783
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001784 simple_xattrs_free(&cgrp->xattrs);
1785
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001787 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001788}
1789
1790static struct file_system_type cgroup_fs_type = {
1791 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001792 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793 .kill_sb = cgroup_kill_sb,
1794};
1795
Greg KH676db4a2010-08-05 13:53:35 -07001796static struct kobject *cgroup_kobj;
1797
Li Zefana043e3b2008-02-23 15:24:09 -08001798/**
1799 * cgroup_path - generate the path of a cgroup
1800 * @cgrp: the cgroup in question
1801 * @buf: the buffer to write the path into
1802 * @buflen: the length of the buffer
1803 *
Paul Menagea47295e2009-01-07 18:07:44 -08001804 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1805 * reference. Writes path of cgroup into buf. Returns 0 on success,
1806 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001807 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001808int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001809{
1810 char *start;
Li Zefan9a9686b2010-04-22 17:29:24 +08001811 struct dentry *dentry = rcu_dereference_check(cgrp->dentry,
Li Zefan9a9686b2010-04-22 17:29:24 +08001812 cgroup_lock_is_held());
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813
Paul Menagea47295e2009-01-07 18:07:44 -08001814 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001815 /*
1816 * Inactive subsystems have no dentry for their root
1817 * cgroup
1818 */
1819 strcpy(buf, "/");
1820 return 0;
1821 }
1822
1823 start = buf + buflen;
1824
1825 *--start = '\0';
1826 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001827 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001828
Paul Menageddbcc7e2007-10-18 23:39:30 -07001829 if ((start -= len) < buf)
1830 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001831 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001832 cgrp = cgrp->parent;
1833 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001834 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001835
1836 dentry = rcu_dereference_check(cgrp->dentry,
Li Zefan9a9686b2010-04-22 17:29:24 +08001837 cgroup_lock_is_held());
Paul Menagebd89aab2007-10-18 23:40:44 -07001838 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001839 continue;
1840 if (--start < buf)
1841 return -ENAMETOOLONG;
1842 *start = '/';
1843 }
1844 memmove(buf, start, buf + buflen - start);
1845 return 0;
1846}
Ben Blum67523c42010-03-10 15:22:11 -08001847EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001848
Ben Blum74a11662011-05-26 16:25:20 -07001849/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001850 * Control Group taskset
1851 */
Tejun Heo134d3372011-12-12 18:12:21 -08001852struct task_and_cgroup {
1853 struct task_struct *task;
1854 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001855 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001856};
1857
Tejun Heo2f7ee562011-12-12 18:12:21 -08001858struct cgroup_taskset {
1859 struct task_and_cgroup single;
1860 struct flex_array *tc_array;
1861 int tc_array_len;
1862 int idx;
1863 struct cgroup *cur_cgrp;
1864};
1865
1866/**
1867 * cgroup_taskset_first - reset taskset and return the first task
1868 * @tset: taskset of interest
1869 *
1870 * @tset iteration is initialized and the first task is returned.
1871 */
1872struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1873{
1874 if (tset->tc_array) {
1875 tset->idx = 0;
1876 return cgroup_taskset_next(tset);
1877 } else {
1878 tset->cur_cgrp = tset->single.cgrp;
1879 return tset->single.task;
1880 }
1881}
1882EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1883
1884/**
1885 * cgroup_taskset_next - iterate to the next task in taskset
1886 * @tset: taskset of interest
1887 *
1888 * Return the next task in @tset. Iteration must have been initialized
1889 * with cgroup_taskset_first().
1890 */
1891struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1892{
1893 struct task_and_cgroup *tc;
1894
1895 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1896 return NULL;
1897
1898 tc = flex_array_get(tset->tc_array, tset->idx++);
1899 tset->cur_cgrp = tc->cgrp;
1900 return tc->task;
1901}
1902EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1903
1904/**
1905 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1906 * @tset: taskset of interest
1907 *
1908 * Return the cgroup for the current (last returned) task of @tset. This
1909 * function must be preceded by either cgroup_taskset_first() or
1910 * cgroup_taskset_next().
1911 */
1912struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1913{
1914 return tset->cur_cgrp;
1915}
1916EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1917
1918/**
1919 * cgroup_taskset_size - return the number of tasks in taskset
1920 * @tset: taskset of interest
1921 */
1922int cgroup_taskset_size(struct cgroup_taskset *tset)
1923{
1924 return tset->tc_array ? tset->tc_array_len : 1;
1925}
1926EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1927
1928
Ben Blum74a11662011-05-26 16:25:20 -07001929/*
1930 * cgroup_task_migrate - move a task from one cgroup to another.
1931 *
1932 * 'guarantee' is set if the caller promises that a new css_set for the task
1933 * will already exist. If not set, this function might sleep, and can fail with
Tejun Heocd3d0952011-12-12 18:12:21 -08001934 * -ENOMEM. Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001935 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001936static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1937 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001938{
1939 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001940
1941 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001942 * We are synchronized through threadgroup_lock() against PF_EXITING
1943 * setting such that we can't race against cgroup_exit() changing the
1944 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001945 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001946 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001947 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001948
Ben Blum74a11662011-05-26 16:25:20 -07001949 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001950 rcu_assign_pointer(tsk->cgroups, newcg);
1951 task_unlock(tsk);
1952
1953 /* Update the css_set linked lists if we're using them */
1954 write_lock(&css_set_lock);
1955 if (!list_empty(&tsk->cg_list))
1956 list_move(&tsk->cg_list, &newcg->tasks);
1957 write_unlock(&css_set_lock);
1958
1959 /*
1960 * We just gained a reference on oldcg by taking it from the task. As
1961 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1962 * it here; it will be freed under RCU.
1963 */
1964 put_css_set(oldcg);
1965
1966 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Ben Blum74a11662011-05-26 16:25:20 -07001967}
1968
Li Zefana043e3b2008-02-23 15:24:09 -08001969/**
1970 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1971 * @cgrp: the cgroup the task is attaching to
1972 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001973 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001974 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1975 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001976 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001977int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001978{
Tejun Heo8f121912012-03-29 22:03:33 -07001979 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001980 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001981 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001982 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001983 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001984 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001985
Tejun Heocd3d0952011-12-12 18:12:21 -08001986 /* @tsk either already exited or can't exit until the end */
1987 if (tsk->flags & PF_EXITING)
1988 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001989
1990 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001991 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001992 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001993 return 0;
1994
Tejun Heo2f7ee562011-12-12 18:12:21 -08001995 tset.single.task = tsk;
1996 tset.single.cgrp = oldcgrp;
1997
Paul Menagebbcb81d2007-10-18 23:39:32 -07001998 for_each_subsys(root, ss) {
1999 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002000 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08002001 if (retval) {
2002 /*
2003 * Remember on which subsystem the can_attach()
2004 * failed, so that we only call cancel_attach()
2005 * against the subsystems whose can_attach()
2006 * succeeded. (See below)
2007 */
2008 failed_ss = ss;
2009 goto out;
2010 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07002011 }
2012 }
2013
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002014 newcg = find_css_set(tsk->cgroups, cgrp);
2015 if (!newcg) {
2016 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08002017 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002018 }
2019
2020 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07002021
Paul Menagebbcb81d2007-10-18 23:39:32 -07002022 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08002023 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002024 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002025 }
Ben Blum74a11662011-05-26 16:25:20 -07002026
Paul Menagebbcb81d2007-10-18 23:39:32 -07002027 synchronize_rcu();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002028
2029 /*
2030 * wake up rmdir() waiter. the rmdir should fail since the cgroup
2031 * is no longer empty.
2032 */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002033 cgroup_wakeup_rmdir_waiter(cgrp);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08002034out:
2035 if (retval) {
2036 for_each_subsys(root, ss) {
2037 if (ss == failed_ss)
2038 /*
2039 * This subsystem was the one that failed the
2040 * can_attach() check earlier, so we don't need
2041 * to call cancel_attach() against it or any
2042 * remaining subsystems.
2043 */
2044 break;
2045 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002046 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08002047 }
2048 }
2049 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002050}
2051
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002052/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002053 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2054 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002055 * @tsk: the task to be attached
2056 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002057int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002058{
2059 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002060 int retval = 0;
2061
2062 cgroup_lock();
2063 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002064 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2065
2066 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002067 if (retval)
2068 break;
2069 }
2070 cgroup_unlock();
2071
2072 return retval;
2073}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002074EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002075
Ben Blum74a11662011-05-26 16:25:20 -07002076/**
2077 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2078 * @cgrp: the cgroup to attach to
2079 * @leader: the threadgroup leader task_struct of the group to be attached
2080 *
Tejun Heo257058a2011-12-12 18:12:21 -08002081 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2082 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002083 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002084static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002085{
2086 int retval, i, group_size;
2087 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002088 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002089 struct cgroupfs_root *root = cgrp->root;
2090 /* threadgroup list cursor and array */
2091 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002092 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002093 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002094 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002095
2096 /*
2097 * step 0: in order to do expensive, possibly blocking operations for
2098 * every thread, we cannot iterate the thread group list, since it needs
2099 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002100 * group - group_rwsem prevents new threads from appearing, and if
2101 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002102 */
2103 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002104 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002105 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002106 if (!group)
2107 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002108 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2109 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2110 if (retval)
2111 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002112
Ben Blum74a11662011-05-26 16:25:20 -07002113 tsk = leader;
2114 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002115 /*
2116 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2117 * already PF_EXITING could be freed from underneath us unless we
2118 * take an rcu_read_lock.
2119 */
2120 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002121 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002122 struct task_and_cgroup ent;
2123
Tejun Heocd3d0952011-12-12 18:12:21 -08002124 /* @tsk either already exited or can't exit until the end */
2125 if (tsk->flags & PF_EXITING)
2126 continue;
2127
Ben Blum74a11662011-05-26 16:25:20 -07002128 /* as per above, nr_threads may decrease, but not increase. */
2129 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002130 ent.task = tsk;
2131 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002132 /* nothing to do if this task is already in the cgroup */
2133 if (ent.cgrp == cgrp)
2134 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002135 /*
2136 * saying GFP_ATOMIC has no effect here because we did prealloc
2137 * earlier, but it's good form to communicate our expectations.
2138 */
Tejun Heo134d3372011-12-12 18:12:21 -08002139 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002140 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002141 i++;
2142 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002143 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002144 /* remember the number of threads in the array for later. */
2145 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002146 tset.tc_array = group;
2147 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002148
Tejun Heo134d3372011-12-12 18:12:21 -08002149 /* methods shouldn't be called if no task is actually migrating */
2150 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002151 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002152 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002153
Ben Blum74a11662011-05-26 16:25:20 -07002154 /*
2155 * step 1: check that we can legitimately attach to the cgroup.
2156 */
2157 for_each_subsys(root, ss) {
2158 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002159 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002160 if (retval) {
2161 failed_ss = ss;
2162 goto out_cancel_attach;
2163 }
2164 }
Ben Blum74a11662011-05-26 16:25:20 -07002165 }
2166
2167 /*
2168 * step 2: make sure css_sets exist for all threads to be migrated.
2169 * we use find_css_set, which allocates a new one if necessary.
2170 */
Ben Blum74a11662011-05-26 16:25:20 -07002171 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002172 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002173 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2174 if (!tc->cg) {
2175 retval = -ENOMEM;
2176 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002177 }
2178 }
2179
2180 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002181 * step 3: now that we're guaranteed success wrt the css_sets,
2182 * proceed to move all tasks to the new cgroup. There are no
2183 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002184 */
Ben Blum74a11662011-05-26 16:25:20 -07002185 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002186 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002187 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002188 }
2189 /* nothing is sensitive to fork() after this point. */
2190
2191 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002192 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002193 */
2194 for_each_subsys(root, ss) {
2195 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002196 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002197 }
2198
2199 /*
2200 * step 5: success! and cleanup
2201 */
2202 synchronize_rcu();
2203 cgroup_wakeup_rmdir_waiter(cgrp);
2204 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002205out_put_css_set_refs:
2206 if (retval) {
2207 for (i = 0; i < group_size; i++) {
2208 tc = flex_array_get(group, i);
2209 if (!tc->cg)
2210 break;
2211 put_css_set(tc->cg);
2212 }
Ben Blum74a11662011-05-26 16:25:20 -07002213 }
2214out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002215 if (retval) {
2216 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002217 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002218 break;
Ben Blum74a11662011-05-26 16:25:20 -07002219 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002220 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002221 }
2222 }
Ben Blum74a11662011-05-26 16:25:20 -07002223out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002224 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002225 return retval;
2226}
2227
2228/*
2229 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002230 * function to attach either it or all tasks in its threadgroup. Will lock
2231 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002232 */
2233static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002234{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002235 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002236 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002237 int ret;
2238
Ben Blum74a11662011-05-26 16:25:20 -07002239 if (!cgroup_lock_live_group(cgrp))
2240 return -ENODEV;
2241
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002242retry_find_task:
2243 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002244 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002245 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002246 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002247 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002248 ret= -ESRCH;
2249 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002250 }
Ben Blum74a11662011-05-26 16:25:20 -07002251 /*
2252 * even if we're attaching all tasks in the thread group, we
2253 * only need to check permissions on one of them.
2254 */
David Howellsc69e8d92008-11-14 10:39:19 +11002255 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002256 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2257 !uid_eq(cred->euid, tcred->uid) &&
2258 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002259 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002260 ret = -EACCES;
2261 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002262 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002263 } else
2264 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002265
2266 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002267 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002268
2269 /*
2270 * Workqueue threads may acquire PF_THREAD_BOUND and become
2271 * trapped in a cpuset, or RT worker may be born in a cgroup
2272 * with no rt_runtime allocated. Just say no.
2273 */
2274 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2275 ret = -EINVAL;
2276 rcu_read_unlock();
2277 goto out_unlock_cgroup;
2278 }
2279
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002280 get_task_struct(tsk);
2281 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002282
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002283 threadgroup_lock(tsk);
2284 if (threadgroup) {
2285 if (!thread_group_leader(tsk)) {
2286 /*
2287 * a race with de_thread from another thread's exec()
2288 * may strip us of our leadership, if this happens,
2289 * there is no choice but to throw this task away and
2290 * try again; this is
2291 * "double-double-toil-and-trouble-check locking".
2292 */
2293 threadgroup_unlock(tsk);
2294 put_task_struct(tsk);
2295 goto retry_find_task;
2296 }
2297 ret = cgroup_attach_proc(cgrp, tsk);
2298 } else
2299 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002300 threadgroup_unlock(tsk);
2301
Paul Menagebbcb81d2007-10-18 23:39:32 -07002302 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002303out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002304 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002305 return ret;
2306}
2307
Paul Menageaf351022008-07-25 01:47:01 -07002308static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2309{
Ben Blum74a11662011-05-26 16:25:20 -07002310 return attach_task_by_pid(cgrp, pid, false);
2311}
2312
2313static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2314{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002315 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002316}
2317
Paul Menagee788e062008-07-25 01:46:59 -07002318/**
2319 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2320 * @cgrp: the cgroup to be checked for liveness
2321 *
Paul Menage84eea842008-07-25 01:47:00 -07002322 * On success, returns true; the lock should be later released with
2323 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002324 */
Paul Menage84eea842008-07-25 01:47:00 -07002325bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002326{
2327 mutex_lock(&cgroup_mutex);
2328 if (cgroup_is_removed(cgrp)) {
2329 mutex_unlock(&cgroup_mutex);
2330 return false;
2331 }
2332 return true;
2333}
Ben Blum67523c42010-03-10 15:22:11 -08002334EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002335
2336static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2337 const char *buffer)
2338{
2339 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002340 if (strlen(buffer) >= PATH_MAX)
2341 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002342 if (!cgroup_lock_live_group(cgrp))
2343 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002344 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002345 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002346 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002347 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002348 return 0;
2349}
2350
2351static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2352 struct seq_file *seq)
2353{
2354 if (!cgroup_lock_live_group(cgrp))
2355 return -ENODEV;
2356 seq_puts(seq, cgrp->root->release_agent_path);
2357 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002358 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002359 return 0;
2360}
2361
Paul Menage84eea842008-07-25 01:47:00 -07002362/* A buffer size big enough for numbers or short strings */
2363#define CGROUP_LOCAL_BUFFER_SIZE 64
2364
Paul Menagee73d2c62008-04-29 01:00:06 -07002365static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002366 struct file *file,
2367 const char __user *userbuf,
2368 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002369{
Paul Menage84eea842008-07-25 01:47:00 -07002370 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002371 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002372 char *end;
2373
2374 if (!nbytes)
2375 return -EINVAL;
2376 if (nbytes >= sizeof(buffer))
2377 return -E2BIG;
2378 if (copy_from_user(buffer, userbuf, nbytes))
2379 return -EFAULT;
2380
2381 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002382 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002383 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002384 if (*end)
2385 return -EINVAL;
2386 retval = cft->write_u64(cgrp, cft, val);
2387 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002388 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002389 if (*end)
2390 return -EINVAL;
2391 retval = cft->write_s64(cgrp, cft, val);
2392 }
Paul Menage355e0c42007-10-18 23:39:33 -07002393 if (!retval)
2394 retval = nbytes;
2395 return retval;
2396}
2397
Paul Menagedb3b1492008-07-25 01:46:58 -07002398static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2399 struct file *file,
2400 const char __user *userbuf,
2401 size_t nbytes, loff_t *unused_ppos)
2402{
Paul Menage84eea842008-07-25 01:47:00 -07002403 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002404 int retval = 0;
2405 size_t max_bytes = cft->max_write_len;
2406 char *buffer = local_buffer;
2407
2408 if (!max_bytes)
2409 max_bytes = sizeof(local_buffer) - 1;
2410 if (nbytes >= max_bytes)
2411 return -E2BIG;
2412 /* Allocate a dynamic buffer if we need one */
2413 if (nbytes >= sizeof(local_buffer)) {
2414 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2415 if (buffer == NULL)
2416 return -ENOMEM;
2417 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002418 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2419 retval = -EFAULT;
2420 goto out;
2421 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002422
2423 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002424 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002425 if (!retval)
2426 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002427out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002428 if (buffer != local_buffer)
2429 kfree(buffer);
2430 return retval;
2431}
2432
Paul Menageddbcc7e2007-10-18 23:39:30 -07002433static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2434 size_t nbytes, loff_t *ppos)
2435{
2436 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002437 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002438
Li Zefan75139b82009-01-07 18:07:33 -08002439 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002440 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002441 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002442 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002443 if (cft->write_u64 || cft->write_s64)
2444 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002445 if (cft->write_string)
2446 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002447 if (cft->trigger) {
2448 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2449 return ret ? ret : nbytes;
2450 }
Paul Menage355e0c42007-10-18 23:39:33 -07002451 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002452}
2453
Paul Menagef4c753b2008-04-29 00:59:56 -07002454static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2455 struct file *file,
2456 char __user *buf, size_t nbytes,
2457 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002458{
Paul Menage84eea842008-07-25 01:47:00 -07002459 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002460 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002461 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2462
2463 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2464}
2465
Paul Menagee73d2c62008-04-29 01:00:06 -07002466static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2467 struct file *file,
2468 char __user *buf, size_t nbytes,
2469 loff_t *ppos)
2470{
Paul Menage84eea842008-07-25 01:47:00 -07002471 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002472 s64 val = cft->read_s64(cgrp, cft);
2473 int len = sprintf(tmp, "%lld\n", (long long) val);
2474
2475 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2476}
2477
Paul Menageddbcc7e2007-10-18 23:39:30 -07002478static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2479 size_t nbytes, loff_t *ppos)
2480{
2481 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002482 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002483
Li Zefan75139b82009-01-07 18:07:33 -08002484 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002485 return -ENODEV;
2486
2487 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002488 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002489 if (cft->read_u64)
2490 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002491 if (cft->read_s64)
2492 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002493 return -EINVAL;
2494}
2495
Paul Menage91796562008-04-29 01:00:01 -07002496/*
2497 * seqfile ops/methods for returning structured data. Currently just
2498 * supports string->u64 maps, but can be extended in future.
2499 */
2500
2501struct cgroup_seqfile_state {
2502 struct cftype *cft;
2503 struct cgroup *cgroup;
2504};
2505
2506static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2507{
2508 struct seq_file *sf = cb->state;
2509 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2510}
2511
2512static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2513{
2514 struct cgroup_seqfile_state *state = m->private;
2515 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002516 if (cft->read_map) {
2517 struct cgroup_map_cb cb = {
2518 .fill = cgroup_map_add,
2519 .state = m,
2520 };
2521 return cft->read_map(state->cgroup, cft, &cb);
2522 }
2523 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002524}
2525
Adrian Bunk96930a62008-07-25 19:46:21 -07002526static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002527{
2528 struct seq_file *seq = file->private_data;
2529 kfree(seq->private);
2530 return single_release(inode, file);
2531}
2532
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002533static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002534 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002535 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002536 .llseek = seq_lseek,
2537 .release = cgroup_seqfile_release,
2538};
2539
Paul Menageddbcc7e2007-10-18 23:39:30 -07002540static int cgroup_file_open(struct inode *inode, struct file *file)
2541{
2542 int err;
2543 struct cftype *cft;
2544
2545 err = generic_file_open(inode, file);
2546 if (err)
2547 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002548 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002549
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002550 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002551 struct cgroup_seqfile_state *state =
2552 kzalloc(sizeof(*state), GFP_USER);
2553 if (!state)
2554 return -ENOMEM;
2555 state->cft = cft;
2556 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2557 file->f_op = &cgroup_seqfile_operations;
2558 err = single_open(file, cgroup_seqfile_show, state);
2559 if (err < 0)
2560 kfree(state);
2561 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002562 err = cft->open(inode, file);
2563 else
2564 err = 0;
2565
2566 return err;
2567}
2568
2569static int cgroup_file_release(struct inode *inode, struct file *file)
2570{
2571 struct cftype *cft = __d_cft(file->f_dentry);
2572 if (cft->release)
2573 return cft->release(inode, file);
2574 return 0;
2575}
2576
2577/*
2578 * cgroup_rename - Only allow simple rename of directories in place.
2579 */
2580static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2581 struct inode *new_dir, struct dentry *new_dentry)
2582{
2583 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2584 return -ENOTDIR;
2585 if (new_dentry->d_inode)
2586 return -EEXIST;
2587 if (old_dir != new_dir)
2588 return -EIO;
2589 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2590}
2591
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002592static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2593{
2594 if (S_ISDIR(dentry->d_inode->i_mode))
2595 return &__d_cgrp(dentry)->xattrs;
2596 else
2597 return &__d_cft(dentry)->xattrs;
2598}
2599
2600static inline int xattr_enabled(struct dentry *dentry)
2601{
2602 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2603 return test_bit(ROOT_XATTR, &root->flags);
2604}
2605
2606static bool is_valid_xattr(const char *name)
2607{
2608 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2609 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2610 return true;
2611 return false;
2612}
2613
2614static int cgroup_setxattr(struct dentry *dentry, const char *name,
2615 const void *val, size_t size, int flags)
2616{
2617 if (!xattr_enabled(dentry))
2618 return -EOPNOTSUPP;
2619 if (!is_valid_xattr(name))
2620 return -EINVAL;
2621 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2622}
2623
2624static int cgroup_removexattr(struct dentry *dentry, const char *name)
2625{
2626 if (!xattr_enabled(dentry))
2627 return -EOPNOTSUPP;
2628 if (!is_valid_xattr(name))
2629 return -EINVAL;
2630 return simple_xattr_remove(__d_xattrs(dentry), name);
2631}
2632
2633static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2634 void *buf, size_t size)
2635{
2636 if (!xattr_enabled(dentry))
2637 return -EOPNOTSUPP;
2638 if (!is_valid_xattr(name))
2639 return -EINVAL;
2640 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2641}
2642
2643static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2644{
2645 if (!xattr_enabled(dentry))
2646 return -EOPNOTSUPP;
2647 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2648}
2649
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002650static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002651 .read = cgroup_file_read,
2652 .write = cgroup_file_write,
2653 .llseek = generic_file_llseek,
2654 .open = cgroup_file_open,
2655 .release = cgroup_file_release,
2656};
2657
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002658static const struct inode_operations cgroup_file_inode_operations = {
2659 .setxattr = cgroup_setxattr,
2660 .getxattr = cgroup_getxattr,
2661 .listxattr = cgroup_listxattr,
2662 .removexattr = cgroup_removexattr,
2663};
2664
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002665static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002666 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002667 .mkdir = cgroup_mkdir,
2668 .rmdir = cgroup_rmdir,
2669 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002670 .setxattr = cgroup_setxattr,
2671 .getxattr = cgroup_getxattr,
2672 .listxattr = cgroup_listxattr,
2673 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674};
2675
Al Viro00cd8dd2012-06-10 17:13:09 -04002676static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002677{
2678 if (dentry->d_name.len > NAME_MAX)
2679 return ERR_PTR(-ENAMETOOLONG);
2680 d_add(dentry, NULL);
2681 return NULL;
2682}
2683
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002684/*
2685 * Check if a file is a control file
2686 */
2687static inline struct cftype *__file_cft(struct file *file)
2688{
2689 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2690 return ERR_PTR(-EINVAL);
2691 return __d_cft(file->f_dentry);
2692}
2693
Al Viroa5e7ed32011-07-26 01:55:55 -04002694static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002695 struct super_block *sb)
2696{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002697 struct inode *inode;
2698
2699 if (!dentry)
2700 return -ENOENT;
2701 if (dentry->d_inode)
2702 return -EEXIST;
2703
2704 inode = cgroup_new_inode(mode, sb);
2705 if (!inode)
2706 return -ENOMEM;
2707
2708 if (S_ISDIR(mode)) {
2709 inode->i_op = &cgroup_dir_inode_operations;
2710 inode->i_fop = &simple_dir_operations;
2711
2712 /* start off with i_nlink == 2 (for "." entry) */
2713 inc_nlink(inode);
2714
2715 /* start with the directory inode held, so that we can
2716 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07002717 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002718 } else if (S_ISREG(mode)) {
2719 inode->i_size = 0;
2720 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002721 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002722 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002723 d_instantiate(dentry, inode);
2724 dget(dentry); /* Extra count - pin the dentry in core */
2725 return 0;
2726}
2727
2728/*
Li Zefana043e3b2008-02-23 15:24:09 -08002729 * cgroup_create_dir - create a directory for an object.
2730 * @cgrp: the cgroup we create the directory for. It must have a valid
2731 * ->parent field. And we are going to fill its ->dentry field.
2732 * @dentry: dentry of the new cgroup
2733 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002734 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002735static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04002736 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002737{
2738 struct dentry *parent;
2739 int error = 0;
2740
Paul Menagebd89aab2007-10-18 23:40:44 -07002741 parent = cgrp->parent->dentry;
2742 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002743 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002744 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002745 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08002746 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002747 dget(dentry);
2748 }
2749 dput(dentry);
2750
2751 return error;
2752}
2753
Li Zefan099fca32009-04-02 16:57:29 -07002754/**
2755 * cgroup_file_mode - deduce file mode of a control file
2756 * @cft: the control file in question
2757 *
2758 * returns cft->mode if ->mode is not 0
2759 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2760 * returns S_IRUGO if it has only a read handler
2761 * returns S_IWUSR if it has only a write hander
2762 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002763static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002764{
Al Viroa5e7ed32011-07-26 01:55:55 -04002765 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002766
2767 if (cft->mode)
2768 return cft->mode;
2769
2770 if (cft->read || cft->read_u64 || cft->read_s64 ||
2771 cft->read_map || cft->read_seq_string)
2772 mode |= S_IRUGO;
2773
2774 if (cft->write || cft->write_u64 || cft->write_s64 ||
2775 cft->write_string || cft->trigger)
2776 mode |= S_IWUSR;
2777
2778 return mode;
2779}
2780
Tejun Heodb0416b2012-04-01 12:09:55 -07002781static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002782 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002783{
Paul Menagebd89aab2007-10-18 23:40:44 -07002784 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002785 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002786 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002787 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002788 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002789 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002790 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002791
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002792 simple_xattrs_init(&cft->xattrs);
2793
Tejun Heo8e3f6542012-04-01 12:09:55 -07002794 /* does @cft->flags tell us to skip creation on @cgrp? */
2795 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2796 return 0;
2797 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2798 return 0;
2799
Paul Menagebd89aab2007-10-18 23:40:44 -07002800 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002801 strcpy(name, subsys->name);
2802 strcat(name, ".");
2803 }
2804 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002805
Paul Menageddbcc7e2007-10-18 23:39:30 -07002806 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002807
2808 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2809 if (!cfe)
2810 return -ENOMEM;
2811
Paul Menageddbcc7e2007-10-18 23:39:30 -07002812 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002813 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002814 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002815 goto out;
2816 }
2817
2818 mode = cgroup_file_mode(cft);
2819 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2820 if (!error) {
2821 cfe->type = (void *)cft;
2822 cfe->dentry = dentry;
2823 dentry->d_fsdata = cfe;
2824 list_add_tail(&cfe->node, &parent->files);
2825 cfe = NULL;
2826 }
2827 dput(dentry);
2828out:
2829 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002830 return error;
2831}
2832
Tejun Heo79578622012-04-01 12:09:56 -07002833static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002834 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002835{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002836 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002837 int err, ret = 0;
2838
2839 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Tejun Heo79578622012-04-01 12:09:56 -07002840 if (is_add)
2841 err = cgroup_add_file(cgrp, subsys, cft);
2842 else
2843 err = cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002844 if (err) {
Tejun Heo79578622012-04-01 12:09:56 -07002845 pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n",
2846 is_add ? "add" : "remove", cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002847 ret = err;
2848 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002849 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002850 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002851}
2852
Tejun Heo8e3f6542012-04-01 12:09:55 -07002853static DEFINE_MUTEX(cgroup_cft_mutex);
2854
2855static void cgroup_cfts_prepare(void)
2856 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2857{
2858 /*
2859 * Thanks to the entanglement with vfs inode locking, we can't walk
2860 * the existing cgroups under cgroup_mutex and create files.
2861 * Instead, we increment reference on all cgroups and build list of
2862 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2863 * exclusive access to the field.
2864 */
2865 mutex_lock(&cgroup_cft_mutex);
2866 mutex_lock(&cgroup_mutex);
2867}
2868
2869static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002870 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002871 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2872{
2873 LIST_HEAD(pending);
2874 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002875
2876 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2877 if (cfts && ss->root != &rootnode) {
2878 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2879 dget(cgrp->dentry);
2880 list_add_tail(&cgrp->cft_q_node, &pending);
2881 }
2882 }
2883
2884 mutex_unlock(&cgroup_mutex);
2885
2886 /*
2887 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2888 * files for all cgroups which were created before.
2889 */
2890 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2891 struct inode *inode = cgrp->dentry->d_inode;
2892
2893 mutex_lock(&inode->i_mutex);
2894 mutex_lock(&cgroup_mutex);
2895 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002896 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002897 mutex_unlock(&cgroup_mutex);
2898 mutex_unlock(&inode->i_mutex);
2899
2900 list_del_init(&cgrp->cft_q_node);
2901 dput(cgrp->dentry);
2902 }
2903
2904 mutex_unlock(&cgroup_cft_mutex);
2905}
2906
2907/**
2908 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2909 * @ss: target cgroup subsystem
2910 * @cfts: zero-length name terminated array of cftypes
2911 *
2912 * Register @cfts to @ss. Files described by @cfts are created for all
2913 * existing cgroups to which @ss is attached and all future cgroups will
2914 * have them too. This function can be called anytime whether @ss is
2915 * attached or not.
2916 *
2917 * Returns 0 on successful registration, -errno on failure. Note that this
2918 * function currently returns 0 as long as @cfts registration is successful
2919 * even if some file creation attempts on existing cgroups fail.
2920 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002921int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002922{
2923 struct cftype_set *set;
2924
2925 set = kzalloc(sizeof(*set), GFP_KERNEL);
2926 if (!set)
2927 return -ENOMEM;
2928
2929 cgroup_cfts_prepare();
2930 set->cfts = cfts;
2931 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002932 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002933
2934 return 0;
2935}
2936EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2937
Li Zefana043e3b2008-02-23 15:24:09 -08002938/**
Tejun Heo79578622012-04-01 12:09:56 -07002939 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2940 * @ss: target cgroup subsystem
2941 * @cfts: zero-length name terminated array of cftypes
2942 *
2943 * Unregister @cfts from @ss. Files described by @cfts are removed from
2944 * all existing cgroups to which @ss is attached and all future cgroups
2945 * won't have them either. This function can be called anytime whether @ss
2946 * is attached or not.
2947 *
2948 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2949 * registered with @ss.
2950 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002951int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002952{
2953 struct cftype_set *set;
2954
2955 cgroup_cfts_prepare();
2956
2957 list_for_each_entry(set, &ss->cftsets, node) {
2958 if (set->cfts == cfts) {
2959 list_del_init(&set->node);
2960 cgroup_cfts_commit(ss, cfts, false);
2961 return 0;
2962 }
2963 }
2964
2965 cgroup_cfts_commit(ss, NULL, false);
2966 return -ENOENT;
2967}
2968
2969/**
Li Zefana043e3b2008-02-23 15:24:09 -08002970 * cgroup_task_count - count the number of tasks in a cgroup.
2971 * @cgrp: the cgroup in question
2972 *
2973 * Return the number of tasks in the cgroup.
2974 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002975int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002976{
2977 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002978 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002979
Paul Menage817929e2007-10-18 23:39:36 -07002980 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002981 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002982 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002983 }
2984 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002985 return count;
2986}
2987
2988/*
Paul Menage817929e2007-10-18 23:39:36 -07002989 * Advance a list_head iterator. The iterator should be positioned at
2990 * the start of a css_set
2991 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002992static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002993 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002994{
2995 struct list_head *l = it->cg_link;
2996 struct cg_cgroup_link *link;
2997 struct css_set *cg;
2998
2999 /* Advance to the next non-empty css_set */
3000 do {
3001 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07003002 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07003003 it->cg_link = NULL;
3004 return;
3005 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003006 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07003007 cg = link->cg;
3008 } while (list_empty(&cg->tasks));
3009 it->cg_link = l;
3010 it->task = cg->tasks.next;
3011}
3012
Cliff Wickman31a7df02008-02-07 00:14:42 -08003013/*
3014 * To reduce the fork() overhead for systems that are not actually
3015 * using their cgroups capability, we don't maintain the lists running
3016 * through each css_set to its tasks until we see the list actually
3017 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08003018 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07003019static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003020{
3021 struct task_struct *p, *g;
3022 write_lock(&css_set_lock);
3023 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003024 /*
3025 * We need tasklist_lock because RCU is not safe against
3026 * while_each_thread(). Besides, a forking task that has passed
3027 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3028 * is not guaranteed to have its child immediately visible in the
3029 * tasklist if we walk through it with RCU.
3030 */
3031 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003032 do_each_thread(g, p) {
3033 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003034 /*
3035 * We should check if the process is exiting, otherwise
3036 * it will race with cgroup_exit() in that the list
3037 * entry won't be deleted though the process has exited.
3038 */
3039 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003040 list_add(&p->cg_list, &p->cgroups->tasks);
3041 task_unlock(p);
3042 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003043 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003044 write_unlock(&css_set_lock);
3045}
3046
Paul Menagebd89aab2007-10-18 23:40:44 -07003047void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003048 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003049{
3050 /*
3051 * The first time anyone tries to iterate across a cgroup,
3052 * we need to enable the list linking each css_set to its
3053 * tasks, and fix up all existing tasks.
3054 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003055 if (!use_task_css_set_links)
3056 cgroup_enable_task_cg_lists();
3057
Paul Menage817929e2007-10-18 23:39:36 -07003058 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003059 it->cg_link = &cgrp->css_sets;
3060 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003061}
3062
Paul Menagebd89aab2007-10-18 23:40:44 -07003063struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003064 struct cgroup_iter *it)
3065{
3066 struct task_struct *res;
3067 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003068 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003069
3070 /* If the iterator cg is NULL, we have no tasks */
3071 if (!it->cg_link)
3072 return NULL;
3073 res = list_entry(l, struct task_struct, cg_list);
3074 /* Advance iterator to find next entry */
3075 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003076 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3077 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003078 /* We reached the end of this task list - move on to
3079 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003080 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003081 } else {
3082 it->task = l;
3083 }
3084 return res;
3085}
3086
Paul Menagebd89aab2007-10-18 23:40:44 -07003087void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003088 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003089{
3090 read_unlock(&css_set_lock);
3091}
3092
Cliff Wickman31a7df02008-02-07 00:14:42 -08003093static inline int started_after_time(struct task_struct *t1,
3094 struct timespec *time,
3095 struct task_struct *t2)
3096{
3097 int start_diff = timespec_compare(&t1->start_time, time);
3098 if (start_diff > 0) {
3099 return 1;
3100 } else if (start_diff < 0) {
3101 return 0;
3102 } else {
3103 /*
3104 * Arbitrarily, if two processes started at the same
3105 * time, we'll say that the lower pointer value
3106 * started first. Note that t2 may have exited by now
3107 * so this may not be a valid pointer any longer, but
3108 * that's fine - it still serves to distinguish
3109 * between two tasks started (effectively) simultaneously.
3110 */
3111 return t1 > t2;
3112 }
3113}
3114
3115/*
3116 * This function is a callback from heap_insert() and is used to order
3117 * the heap.
3118 * In this case we order the heap in descending task start time.
3119 */
3120static inline int started_after(void *p1, void *p2)
3121{
3122 struct task_struct *t1 = p1;
3123 struct task_struct *t2 = p2;
3124 return started_after_time(t1, &t2->start_time, t2);
3125}
3126
3127/**
3128 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3129 * @scan: struct cgroup_scanner containing arguments for the scan
3130 *
3131 * Arguments include pointers to callback functions test_task() and
3132 * process_task().
3133 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3134 * and if it returns true, call process_task() for it also.
3135 * The test_task pointer may be NULL, meaning always true (select all tasks).
3136 * Effectively duplicates cgroup_iter_{start,next,end}()
3137 * but does not lock css_set_lock for the call to process_task().
3138 * The struct cgroup_scanner may be embedded in any structure of the caller's
3139 * creation.
3140 * It is guaranteed that process_task() will act on every task that
3141 * is a member of the cgroup for the duration of this call. This
3142 * function may or may not call process_task() for tasks that exit
3143 * or move to a different cgroup during the call, or are forked or
3144 * move into the cgroup during the call.
3145 *
3146 * Note that test_task() may be called with locks held, and may in some
3147 * situations be called multiple times for the same task, so it should
3148 * be cheap.
3149 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3150 * pre-allocated and will be used for heap operations (and its "gt" member will
3151 * be overwritten), else a temporary heap will be used (allocation of which
3152 * may cause this function to fail).
3153 */
3154int cgroup_scan_tasks(struct cgroup_scanner *scan)
3155{
3156 int retval, i;
3157 struct cgroup_iter it;
3158 struct task_struct *p, *dropped;
3159 /* Never dereference latest_task, since it's not refcounted */
3160 struct task_struct *latest_task = NULL;
3161 struct ptr_heap tmp_heap;
3162 struct ptr_heap *heap;
3163 struct timespec latest_time = { 0, 0 };
3164
3165 if (scan->heap) {
3166 /* The caller supplied our heap and pre-allocated its memory */
3167 heap = scan->heap;
3168 heap->gt = &started_after;
3169 } else {
3170 /* We need to allocate our own heap memory */
3171 heap = &tmp_heap;
3172 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3173 if (retval)
3174 /* cannot allocate the heap */
3175 return retval;
3176 }
3177
3178 again:
3179 /*
3180 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3181 * to determine which are of interest, and using the scanner's
3182 * "process_task" callback to process any of them that need an update.
3183 * Since we don't want to hold any locks during the task updates,
3184 * gather tasks to be processed in a heap structure.
3185 * The heap is sorted by descending task start time.
3186 * If the statically-sized heap fills up, we overflow tasks that
3187 * started later, and in future iterations only consider tasks that
3188 * started after the latest task in the previous pass. This
3189 * guarantees forward progress and that we don't miss any tasks.
3190 */
3191 heap->size = 0;
3192 cgroup_iter_start(scan->cg, &it);
3193 while ((p = cgroup_iter_next(scan->cg, &it))) {
3194 /*
3195 * Only affect tasks that qualify per the caller's callback,
3196 * if he provided one
3197 */
3198 if (scan->test_task && !scan->test_task(p, scan))
3199 continue;
3200 /*
3201 * Only process tasks that started after the last task
3202 * we processed
3203 */
3204 if (!started_after_time(p, &latest_time, latest_task))
3205 continue;
3206 dropped = heap_insert(heap, p);
3207 if (dropped == NULL) {
3208 /*
3209 * The new task was inserted; the heap wasn't
3210 * previously full
3211 */
3212 get_task_struct(p);
3213 } else if (dropped != p) {
3214 /*
3215 * The new task was inserted, and pushed out a
3216 * different task
3217 */
3218 get_task_struct(p);
3219 put_task_struct(dropped);
3220 }
3221 /*
3222 * Else the new task was newer than anything already in
3223 * the heap and wasn't inserted
3224 */
3225 }
3226 cgroup_iter_end(scan->cg, &it);
3227
3228 if (heap->size) {
3229 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003230 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003231 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003232 latest_time = q->start_time;
3233 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003234 }
3235 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003236 scan->process_task(q, scan);
3237 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003238 }
3239 /*
3240 * If we had to process any tasks at all, scan again
3241 * in case some of them were in the middle of forking
3242 * children that didn't get processed.
3243 * Not the most efficient way to do it, but it avoids
3244 * having to take callback_mutex in the fork path
3245 */
3246 goto again;
3247 }
3248 if (heap == &tmp_heap)
3249 heap_free(&tmp_heap);
3250 return 0;
3251}
3252
Paul Menage817929e2007-10-18 23:39:36 -07003253/*
Ben Blum102a7752009-09-23 15:56:26 -07003254 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003255 *
3256 * Reading this file can return large amounts of data if a cgroup has
3257 * *lots* of attached tasks. So it may need several calls to read(),
3258 * but we cannot guarantee that the information we produce is correct
3259 * unless we produce it entirely atomically.
3260 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003261 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003262
Li Zefan24528252012-01-20 11:58:43 +08003263/* which pidlist file are we talking about? */
3264enum cgroup_filetype {
3265 CGROUP_FILE_PROCS,
3266 CGROUP_FILE_TASKS,
3267};
3268
3269/*
3270 * A pidlist is a list of pids that virtually represents the contents of one
3271 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3272 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3273 * to the cgroup.
3274 */
3275struct cgroup_pidlist {
3276 /*
3277 * used to find which pidlist is wanted. doesn't change as long as
3278 * this particular list stays in the list.
3279 */
3280 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3281 /* array of xids */
3282 pid_t *list;
3283 /* how many elements the above list has */
3284 int length;
3285 /* how many files are using the current array */
3286 int use_count;
3287 /* each of these stored in a list by its cgroup */
3288 struct list_head links;
3289 /* pointer to the cgroup we belong to, for list removal purposes */
3290 struct cgroup *owner;
3291 /* protects the other fields */
3292 struct rw_semaphore mutex;
3293};
3294
Paul Menagebbcb81d2007-10-18 23:39:32 -07003295/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003296 * The following two functions "fix" the issue where there are more pids
3297 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3298 * TODO: replace with a kernel-wide solution to this problem
3299 */
3300#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3301static void *pidlist_allocate(int count)
3302{
3303 if (PIDLIST_TOO_LARGE(count))
3304 return vmalloc(count * sizeof(pid_t));
3305 else
3306 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3307}
3308static void pidlist_free(void *p)
3309{
3310 if (is_vmalloc_addr(p))
3311 vfree(p);
3312 else
3313 kfree(p);
3314}
3315static void *pidlist_resize(void *p, int newcount)
3316{
3317 void *newlist;
3318 /* note: if new alloc fails, old p will still be valid either way */
3319 if (is_vmalloc_addr(p)) {
3320 newlist = vmalloc(newcount * sizeof(pid_t));
3321 if (!newlist)
3322 return NULL;
3323 memcpy(newlist, p, newcount * sizeof(pid_t));
3324 vfree(p);
3325 } else {
3326 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3327 }
3328 return newlist;
3329}
3330
3331/*
Ben Blum102a7752009-09-23 15:56:26 -07003332 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3333 * If the new stripped list is sufficiently smaller and there's enough memory
3334 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3335 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003336 */
Ben Blum102a7752009-09-23 15:56:26 -07003337/* is the size difference enough that we should re-allocate the array? */
3338#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3339static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003340{
Ben Blum102a7752009-09-23 15:56:26 -07003341 int src, dest = 1;
3342 pid_t *list = *p;
3343 pid_t *newlist;
3344
3345 /*
3346 * we presume the 0th element is unique, so i starts at 1. trivial
3347 * edge cases first; no work needs to be done for either
3348 */
3349 if (length == 0 || length == 1)
3350 return length;
3351 /* src and dest walk down the list; dest counts unique elements */
3352 for (src = 1; src < length; src++) {
3353 /* find next unique element */
3354 while (list[src] == list[src-1]) {
3355 src++;
3356 if (src == length)
3357 goto after;
3358 }
3359 /* dest always points to where the next unique element goes */
3360 list[dest] = list[src];
3361 dest++;
3362 }
3363after:
3364 /*
3365 * if the length difference is large enough, we want to allocate a
3366 * smaller buffer to save memory. if this fails due to out of memory,
3367 * we'll just stay with what we've got.
3368 */
3369 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003370 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003371 if (newlist)
3372 *p = newlist;
3373 }
3374 return dest;
3375}
3376
3377static int cmppid(const void *a, const void *b)
3378{
3379 return *(pid_t *)a - *(pid_t *)b;
3380}
3381
3382/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003383 * find the appropriate pidlist for our purpose (given procs vs tasks)
3384 * returns with the lock on that pidlist already held, and takes care
3385 * of the use count, or returns NULL with no locks held if we're out of
3386 * memory.
3387 */
3388static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3389 enum cgroup_filetype type)
3390{
3391 struct cgroup_pidlist *l;
3392 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08003393 struct pid_namespace *ns = current->nsproxy->pid_ns;
3394
Ben Blum72a8cb32009-09-23 15:56:27 -07003395 /*
3396 * We can't drop the pidlist_mutex before taking the l->mutex in case
3397 * the last ref-holder is trying to remove l from the list at the same
3398 * time. Holding the pidlist_mutex precludes somebody taking whichever
3399 * list we find out from under us - compare release_pid_array().
3400 */
3401 mutex_lock(&cgrp->pidlist_mutex);
3402 list_for_each_entry(l, &cgrp->pidlists, links) {
3403 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003404 /* make sure l doesn't vanish out from under us */
3405 down_write(&l->mutex);
3406 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003407 return l;
3408 }
3409 }
3410 /* entry not found; create a new one */
3411 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3412 if (!l) {
3413 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003414 return l;
3415 }
3416 init_rwsem(&l->mutex);
3417 down_write(&l->mutex);
3418 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003419 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003420 l->use_count = 0; /* don't increment here */
3421 l->list = NULL;
3422 l->owner = cgrp;
3423 list_add(&l->links, &cgrp->pidlists);
3424 mutex_unlock(&cgrp->pidlist_mutex);
3425 return l;
3426}
3427
3428/*
Ben Blum102a7752009-09-23 15:56:26 -07003429 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3430 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003431static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3432 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003433{
3434 pid_t *array;
3435 int length;
3436 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003437 struct cgroup_iter it;
3438 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003439 struct cgroup_pidlist *l;
3440
3441 /*
3442 * If cgroup gets more users after we read count, we won't have
3443 * enough space - tough. This race is indistinguishable to the
3444 * caller from the case that the additional cgroup users didn't
3445 * show up until sometime later on.
3446 */
3447 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003448 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003449 if (!array)
3450 return -ENOMEM;
3451 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003452 cgroup_iter_start(cgrp, &it);
3453 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003454 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003455 break;
Ben Blum102a7752009-09-23 15:56:26 -07003456 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003457 if (type == CGROUP_FILE_PROCS)
3458 pid = task_tgid_vnr(tsk);
3459 else
3460 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003461 if (pid > 0) /* make sure to only use valid results */
3462 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003463 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003464 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003465 length = n;
3466 /* now sort & (if procs) strip out duplicates */
3467 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003468 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003469 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003470 l = cgroup_pidlist_find(cgrp, type);
3471 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003472 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003473 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003474 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003475 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003476 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003477 l->list = array;
3478 l->length = length;
3479 l->use_count++;
3480 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003481 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003482 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003483}
3484
Balbir Singh846c7bb2007-10-18 23:39:44 -07003485/**
Li Zefana043e3b2008-02-23 15:24:09 -08003486 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003487 * @stats: cgroupstats to fill information into
3488 * @dentry: A dentry entry belonging to the cgroup for which stats have
3489 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003490 *
3491 * Build and fill cgroupstats so that taskstats can export it to user
3492 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003493 */
3494int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3495{
3496 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003497 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003498 struct cgroup_iter it;
3499 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003500
Balbir Singh846c7bb2007-10-18 23:39:44 -07003501 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003502 * Validate dentry by checking the superblock operations,
3503 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003504 */
Li Zefan33d283b2008-11-19 15:36:48 -08003505 if (dentry->d_sb->s_op != &cgroup_ops ||
3506 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003507 goto err;
3508
3509 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003510 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003511
Paul Menagebd89aab2007-10-18 23:40:44 -07003512 cgroup_iter_start(cgrp, &it);
3513 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003514 switch (tsk->state) {
3515 case TASK_RUNNING:
3516 stats->nr_running++;
3517 break;
3518 case TASK_INTERRUPTIBLE:
3519 stats->nr_sleeping++;
3520 break;
3521 case TASK_UNINTERRUPTIBLE:
3522 stats->nr_uninterruptible++;
3523 break;
3524 case TASK_STOPPED:
3525 stats->nr_stopped++;
3526 break;
3527 default:
3528 if (delayacct_is_task_waiting_on_io(tsk))
3529 stats->nr_io_wait++;
3530 break;
3531 }
3532 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003533 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003534
Balbir Singh846c7bb2007-10-18 23:39:44 -07003535err:
3536 return ret;
3537}
3538
Paul Menage8f3ff202009-09-23 15:56:25 -07003539
Paul Menagecc31edc2008-10-18 20:28:04 -07003540/*
Ben Blum102a7752009-09-23 15:56:26 -07003541 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003542 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003543 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003544 */
3545
Ben Blum102a7752009-09-23 15:56:26 -07003546static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003547{
3548 /*
3549 * Initially we receive a position value that corresponds to
3550 * one more than the last pid shown (or 0 on the first call or
3551 * after a seek to the start). Use a binary-search to find the
3552 * next pid to display, if any
3553 */
Ben Blum102a7752009-09-23 15:56:26 -07003554 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003555 int index = 0, pid = *pos;
3556 int *iter;
3557
Ben Blum102a7752009-09-23 15:56:26 -07003558 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003559 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003560 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003561
Paul Menagecc31edc2008-10-18 20:28:04 -07003562 while (index < end) {
3563 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003564 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003565 index = mid;
3566 break;
Ben Blum102a7752009-09-23 15:56:26 -07003567 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003568 index = mid + 1;
3569 else
3570 end = mid;
3571 }
3572 }
3573 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003574 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003575 return NULL;
3576 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003577 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003578 *pos = *iter;
3579 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003580}
3581
Ben Blum102a7752009-09-23 15:56:26 -07003582static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003583{
Ben Blum102a7752009-09-23 15:56:26 -07003584 struct cgroup_pidlist *l = s->private;
3585 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003586}
3587
Ben Blum102a7752009-09-23 15:56:26 -07003588static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003589{
Ben Blum102a7752009-09-23 15:56:26 -07003590 struct cgroup_pidlist *l = s->private;
3591 pid_t *p = v;
3592 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003593 /*
3594 * Advance to the next pid in the array. If this goes off the
3595 * end, we're done
3596 */
3597 p++;
3598 if (p >= end) {
3599 return NULL;
3600 } else {
3601 *pos = *p;
3602 return p;
3603 }
3604}
3605
Ben Blum102a7752009-09-23 15:56:26 -07003606static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003607{
3608 return seq_printf(s, "%d\n", *(int *)v);
3609}
3610
Ben Blum102a7752009-09-23 15:56:26 -07003611/*
3612 * seq_operations functions for iterating on pidlists through seq_file -
3613 * independent of whether it's tasks or procs
3614 */
3615static const struct seq_operations cgroup_pidlist_seq_operations = {
3616 .start = cgroup_pidlist_start,
3617 .stop = cgroup_pidlist_stop,
3618 .next = cgroup_pidlist_next,
3619 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003620};
3621
Ben Blum102a7752009-09-23 15:56:26 -07003622static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003623{
Ben Blum72a8cb32009-09-23 15:56:27 -07003624 /*
3625 * the case where we're the last user of this particular pidlist will
3626 * have us remove it from the cgroup's list, which entails taking the
3627 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3628 * pidlist_mutex, we have to take pidlist_mutex first.
3629 */
3630 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003631 down_write(&l->mutex);
3632 BUG_ON(!l->use_count);
3633 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003634 /* we're the last user if refcount is 0; remove and free */
3635 list_del(&l->links);
3636 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003637 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003638 put_pid_ns(l->key.ns);
3639 up_write(&l->mutex);
3640 kfree(l);
3641 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003642 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003643 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003644 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003645}
3646
Ben Blum102a7752009-09-23 15:56:26 -07003647static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003648{
Ben Blum102a7752009-09-23 15:56:26 -07003649 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003650 if (!(file->f_mode & FMODE_READ))
3651 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003652 /*
3653 * the seq_file will only be initialized if the file was opened for
3654 * reading; hence we check if it's not null only in that case.
3655 */
3656 l = ((struct seq_file *)file->private_data)->private;
3657 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003658 return seq_release(inode, file);
3659}
3660
Ben Blum102a7752009-09-23 15:56:26 -07003661static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003662 .read = seq_read,
3663 .llseek = seq_lseek,
3664 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003665 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003666};
3667
3668/*
Ben Blum102a7752009-09-23 15:56:26 -07003669 * The following functions handle opens on a file that displays a pidlist
3670 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3671 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003672 */
Ben Blum102a7752009-09-23 15:56:26 -07003673/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003674static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003675{
3676 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003677 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003678 int retval;
3679
3680 /* Nothing to do for write-only files */
3681 if (!(file->f_mode & FMODE_READ))
3682 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003683
Ben Blum102a7752009-09-23 15:56:26 -07003684 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003685 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003686 if (retval)
3687 return retval;
3688 /* configure file information */
3689 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003690
Ben Blum102a7752009-09-23 15:56:26 -07003691 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003692 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003693 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003694 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003695 }
Ben Blum102a7752009-09-23 15:56:26 -07003696 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003697 return 0;
3698}
Ben Blum102a7752009-09-23 15:56:26 -07003699static int cgroup_tasks_open(struct inode *unused, struct file *file)
3700{
Ben Blum72a8cb32009-09-23 15:56:27 -07003701 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003702}
3703static int cgroup_procs_open(struct inode *unused, struct file *file)
3704{
Ben Blum72a8cb32009-09-23 15:56:27 -07003705 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003706}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003707
Paul Menagebd89aab2007-10-18 23:40:44 -07003708static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003709 struct cftype *cft)
3710{
Paul Menagebd89aab2007-10-18 23:40:44 -07003711 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003712}
3713
Paul Menage6379c102008-07-25 01:47:01 -07003714static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3715 struct cftype *cft,
3716 u64 val)
3717{
3718 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3719 if (val)
3720 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3721 else
3722 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3723 return 0;
3724}
3725
Paul Menagebbcb81d2007-10-18 23:39:32 -07003726/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003727 * Unregister event and free resources.
3728 *
3729 * Gets called from workqueue.
3730 */
3731static void cgroup_event_remove(struct work_struct *work)
3732{
3733 struct cgroup_event *event = container_of(work, struct cgroup_event,
3734 remove);
3735 struct cgroup *cgrp = event->cgrp;
3736
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003737 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3738
3739 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003740 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003741 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003742}
3743
3744/*
3745 * Gets called on POLLHUP on eventfd when user closes it.
3746 *
3747 * Called with wqh->lock held and interrupts disabled.
3748 */
3749static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3750 int sync, void *key)
3751{
3752 struct cgroup_event *event = container_of(wait,
3753 struct cgroup_event, wait);
3754 struct cgroup *cgrp = event->cgrp;
3755 unsigned long flags = (unsigned long)key;
3756
3757 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003758 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003759 spin_lock(&cgrp->event_list_lock);
3760 list_del(&event->list);
3761 spin_unlock(&cgrp->event_list_lock);
3762 /*
3763 * We are in atomic context, but cgroup_event_remove() may
3764 * sleep, so we have to call it in workqueue.
3765 */
3766 schedule_work(&event->remove);
3767 }
3768
3769 return 0;
3770}
3771
3772static void cgroup_event_ptable_queue_proc(struct file *file,
3773 wait_queue_head_t *wqh, poll_table *pt)
3774{
3775 struct cgroup_event *event = container_of(pt,
3776 struct cgroup_event, pt);
3777
3778 event->wqh = wqh;
3779 add_wait_queue(wqh, &event->wait);
3780}
3781
3782/*
3783 * Parse input and register new cgroup event handler.
3784 *
3785 * Input must be in format '<event_fd> <control_fd> <args>'.
3786 * Interpretation of args is defined by control file implementation.
3787 */
3788static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3789 const char *buffer)
3790{
3791 struct cgroup_event *event = NULL;
3792 unsigned int efd, cfd;
3793 struct file *efile = NULL;
3794 struct file *cfile = NULL;
3795 char *endp;
3796 int ret;
3797
3798 efd = simple_strtoul(buffer, &endp, 10);
3799 if (*endp != ' ')
3800 return -EINVAL;
3801 buffer = endp + 1;
3802
3803 cfd = simple_strtoul(buffer, &endp, 10);
3804 if ((*endp != ' ') && (*endp != '\0'))
3805 return -EINVAL;
3806 buffer = endp + 1;
3807
3808 event = kzalloc(sizeof(*event), GFP_KERNEL);
3809 if (!event)
3810 return -ENOMEM;
3811 event->cgrp = cgrp;
3812 INIT_LIST_HEAD(&event->list);
3813 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3814 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3815 INIT_WORK(&event->remove, cgroup_event_remove);
3816
3817 efile = eventfd_fget(efd);
3818 if (IS_ERR(efile)) {
3819 ret = PTR_ERR(efile);
3820 goto fail;
3821 }
3822
3823 event->eventfd = eventfd_ctx_fileget(efile);
3824 if (IS_ERR(event->eventfd)) {
3825 ret = PTR_ERR(event->eventfd);
3826 goto fail;
3827 }
3828
3829 cfile = fget(cfd);
3830 if (!cfile) {
3831 ret = -EBADF;
3832 goto fail;
3833 }
3834
3835 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003836 /* AV: shouldn't we check that it's been opened for read instead? */
3837 ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003838 if (ret < 0)
3839 goto fail;
3840
3841 event->cft = __file_cft(cfile);
3842 if (IS_ERR(event->cft)) {
3843 ret = PTR_ERR(event->cft);
3844 goto fail;
3845 }
3846
3847 if (!event->cft->register_event || !event->cft->unregister_event) {
3848 ret = -EINVAL;
3849 goto fail;
3850 }
3851
3852 ret = event->cft->register_event(cgrp, event->cft,
3853 event->eventfd, buffer);
3854 if (ret)
3855 goto fail;
3856
3857 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3858 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3859 ret = 0;
3860 goto fail;
3861 }
3862
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003863 /*
3864 * Events should be removed after rmdir of cgroup directory, but before
3865 * destroying subsystem state objects. Let's take reference to cgroup
3866 * directory dentry to do that.
3867 */
3868 dget(cgrp->dentry);
3869
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003870 spin_lock(&cgrp->event_list_lock);
3871 list_add(&event->list, &cgrp->event_list);
3872 spin_unlock(&cgrp->event_list_lock);
3873
3874 fput(cfile);
3875 fput(efile);
3876
3877 return 0;
3878
3879fail:
3880 if (cfile)
3881 fput(cfile);
3882
3883 if (event && event->eventfd && !IS_ERR(event->eventfd))
3884 eventfd_ctx_put(event->eventfd);
3885
3886 if (!IS_ERR_OR_NULL(efile))
3887 fput(efile);
3888
3889 kfree(event);
3890
3891 return ret;
3892}
3893
Daniel Lezcano97978e62010-10-27 15:33:35 -07003894static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3895 struct cftype *cft)
3896{
3897 return clone_children(cgrp);
3898}
3899
3900static int cgroup_clone_children_write(struct cgroup *cgrp,
3901 struct cftype *cft,
3902 u64 val)
3903{
3904 if (val)
3905 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3906 else
3907 clear_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3908 return 0;
3909}
3910
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003911/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003912 * for the common functions, 'private' gives the type of file
3913 */
Ben Blum102a7752009-09-23 15:56:26 -07003914/* for hysterical raisins, we can't put this on the older files */
3915#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003916static struct cftype files[] = {
3917 {
3918 .name = "tasks",
3919 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003920 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003921 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003922 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003923 },
Ben Blum102a7752009-09-23 15:56:26 -07003924 {
3925 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3926 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003927 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003928 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003929 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003930 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003931 {
3932 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003933 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003934 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003935 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003936 {
3937 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3938 .write_string = cgroup_write_event_control,
3939 .mode = S_IWUGO,
3940 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003941 {
3942 .name = "cgroup.clone_children",
3943 .read_u64 = cgroup_clone_children_read,
3944 .write_u64 = cgroup_clone_children_write,
3945 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003946 {
3947 .name = "release_agent",
3948 .flags = CFTYPE_ONLY_ON_ROOT,
3949 .read_seq_string = cgroup_release_agent_show,
3950 .write_string = cgroup_release_agent_write,
3951 .max_write_len = PATH_MAX,
3952 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003953 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003954};
3955
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003956/**
3957 * cgroup_populate_dir - selectively creation of files in a directory
3958 * @cgrp: target cgroup
3959 * @base_files: true if the base files should be added
3960 * @subsys_mask: mask of the subsystem ids whose files should be added
3961 */
3962static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3963 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003964{
3965 int err;
3966 struct cgroup_subsys *ss;
3967
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003968 if (base_files) {
3969 err = cgroup_addrm_files(cgrp, NULL, files, true);
3970 if (err < 0)
3971 return err;
3972 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07003973
Tejun Heo8e3f6542012-04-01 12:09:55 -07003974 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07003975 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003976 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003977 if (!test_bit(ss->subsys_id, &subsys_mask))
3978 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003979
Tejun Heodb0416b2012-04-01 12:09:55 -07003980 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07003981 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003982 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07003983
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003984 /* This cgroup is ready now */
3985 for_each_subsys(cgrp->root, ss) {
3986 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3987 /*
3988 * Update id->css pointer and make this css visible from
3989 * CSS ID functions. This pointer will be dereferened
3990 * from RCU-read-side without locks.
3991 */
3992 if (css->id)
3993 rcu_assign_pointer(css->id->css, css);
3994 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003995
3996 return 0;
3997}
3998
Tejun Heo48ddbe12012-04-01 12:09:56 -07003999static void css_dput_fn(struct work_struct *work)
4000{
4001 struct cgroup_subsys_state *css =
4002 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004003 struct dentry *dentry = css->cgroup->dentry;
4004 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004005
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004006 atomic_inc(&sb->s_active);
4007 dput(dentry);
4008 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004009}
4010
Paul Menageddbcc7e2007-10-18 23:39:30 -07004011static void init_cgroup_css(struct cgroup_subsys_state *css,
4012 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004013 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004014{
Paul Menagebd89aab2007-10-18 23:40:44 -07004015 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004016 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004017 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004018 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004019 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004020 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07004021 BUG_ON(cgrp->subsys[ss->subsys_id]);
4022 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004023
4024 /*
4025 * If !clear_css_refs, css holds an extra ref to @cgrp->dentry
4026 * which is put on the last css_put(). dput() requires process
4027 * context, which css_put() may be called without. @css->dput_work
4028 * will be used to invoke dput() asynchronously from css_put().
4029 */
4030 INIT_WORK(&css->dput_work, css_dput_fn);
4031 if (ss->__DEPRECATED_clear_css_refs)
4032 set_bit(CSS_CLEAR_CSS_REFS, &css->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004033}
4034
4035/*
Li Zefana043e3b2008-02-23 15:24:09 -08004036 * cgroup_create - create a cgroup
4037 * @parent: cgroup that will be parent of the new cgroup
4038 * @dentry: dentry of the new cgroup
4039 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004040 *
Li Zefana043e3b2008-02-23 15:24:09 -08004041 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004042 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004043static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004044 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004045{
Paul Menagebd89aab2007-10-18 23:40:44 -07004046 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004047 struct cgroupfs_root *root = parent->root;
4048 int err = 0;
4049 struct cgroup_subsys *ss;
4050 struct super_block *sb = root->sb;
4051
Paul Menagebd89aab2007-10-18 23:40:44 -07004052 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4053 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004054 return -ENOMEM;
4055
4056 /* Grab a reference on the superblock so the hierarchy doesn't
4057 * get deleted on unmount if there are child cgroups. This
4058 * can be done outside cgroup_mutex, since the sb can't
4059 * disappear while someone has an open control file on the
4060 * fs */
4061 atomic_inc(&sb->s_active);
4062
4063 mutex_lock(&cgroup_mutex);
4064
Paul Menagecc31edc2008-10-18 20:28:04 -07004065 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004066
Paul Menagebd89aab2007-10-18 23:40:44 -07004067 cgrp->parent = parent;
4068 cgrp->root = parent->root;
4069 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004070
Li Zefanb6abdb02008-03-04 14:28:19 -08004071 if (notify_on_release(parent))
4072 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4073
Daniel Lezcano97978e62010-10-27 15:33:35 -07004074 if (clone_children(parent))
4075 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
4076
Paul Menageddbcc7e2007-10-18 23:39:30 -07004077 for_each_subsys(root, ss) {
Li Zefan761b3ef2012-01-31 13:47:36 +08004078 struct cgroup_subsys_state *css = ss->create(cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004079
Paul Menageddbcc7e2007-10-18 23:39:30 -07004080 if (IS_ERR(css)) {
4081 err = PTR_ERR(css);
4082 goto err_destroy;
4083 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004084 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004085 if (ss->use_id) {
4086 err = alloc_css_id(ss, parent, cgrp);
4087 if (err)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004088 goto err_destroy;
Li Zefan4528fd02010-02-02 13:44:10 -08004089 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004090 /* At error, ->destroy() callback has to free assigned ID. */
Daniel Lezcano97978e62010-10-27 15:33:35 -07004091 if (clone_children(parent) && ss->post_clone)
Li Zefan761b3ef2012-01-31 13:47:36 +08004092 ss->post_clone(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004093 }
4094
Paul Menagebd89aab2007-10-18 23:40:44 -07004095 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004096 root->number_of_cgroups++;
4097
Paul Menagebd89aab2007-10-18 23:40:44 -07004098 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004099 if (err < 0)
4100 goto err_remove;
4101
Tejun Heo48ddbe12012-04-01 12:09:56 -07004102 /* If !clear_css_refs, each css holds a ref to the cgroup's dentry */
4103 for_each_subsys(root, ss)
4104 if (!ss->__DEPRECATED_clear_css_refs)
4105 dget(dentry);
4106
Paul Menageddbcc7e2007-10-18 23:39:30 -07004107 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07004108 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004109
Tejun Heob0ca5a82012-04-01 12:09:54 -07004110 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4111
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004112 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004113 /* If err < 0, we have a half-filled directory - oh well ;) */
4114
4115 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004116 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004117
4118 return 0;
4119
4120 err_remove:
4121
Paul Menagebd89aab2007-10-18 23:40:44 -07004122 list_del(&cgrp->sibling);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004123 root->number_of_cgroups--;
4124
4125 err_destroy:
4126
4127 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004128 if (cgrp->subsys[ss->subsys_id])
Li Zefan761b3ef2012-01-31 13:47:36 +08004129 ss->destroy(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004130 }
4131
4132 mutex_unlock(&cgroup_mutex);
4133
4134 /* Release the reference count that we took on the superblock */
4135 deactivate_super(sb);
4136
Paul Menagebd89aab2007-10-18 23:40:44 -07004137 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004138 return err;
4139}
4140
Al Viro18bb1db2011-07-26 01:41:39 -04004141static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004142{
4143 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4144
4145 /* the vfs holds inode->i_mutex already */
4146 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4147}
4148
Tejun Heo28b4c272012-04-01 12:09:56 -07004149/*
4150 * Check the reference count on each subsystem. Since we already
4151 * established that there are no tasks in the cgroup, if the css refcount
4152 * is also 1, then there should be no outstanding references, so the
4153 * subsystem is safe to destroy. We scan across all subsystems rather than
4154 * using the per-hierarchy linked list of mounted subsystems since we can
4155 * be called via check_for_release() with no synchronization other than
4156 * RCU, and the subsystem linked list isn't RCU-safe.
4157 */
Li Zefan55b6fd02008-07-29 22:33:20 -07004158static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004159{
Paul Menage81a6a5c2007-10-18 23:39:38 -07004160 int i;
Tejun Heo28b4c272012-04-01 12:09:56 -07004161
Ben Blumaae8aab2010-03-10 15:22:07 -08004162 /*
4163 * We won't need to lock the subsys array, because the subsystems
4164 * we're concerned about aren't going anywhere since our cgroup root
4165 * has a reference on them.
4166 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004167 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4168 struct cgroup_subsys *ss = subsys[i];
4169 struct cgroup_subsys_state *css;
Tejun Heo28b4c272012-04-01 12:09:56 -07004170
Ben Blumaae8aab2010-03-10 15:22:07 -08004171 /* Skip subsystems not present or not in this hierarchy */
4172 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004173 continue;
Tejun Heo28b4c272012-04-01 12:09:56 -07004174
Paul Menagebd89aab2007-10-18 23:40:44 -07004175 css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004176 /*
4177 * When called from check_for_release() it's possible
Paul Menage81a6a5c2007-10-18 23:39:38 -07004178 * that by this point the cgroup has been removed
4179 * and the css deleted. But a false-positive doesn't
4180 * matter, since it can only happen if the cgroup
4181 * has been deleted and hence no longer needs the
Tejun Heo28b4c272012-04-01 12:09:56 -07004182 * release agent to be called anyway.
4183 */
4184 if (css && css_refcnt(css) > 1)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004185 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004186 }
4187 return 0;
4188}
4189
Paul Menagee7c5ec92009-01-07 18:08:38 -08004190/*
4191 * Atomically mark all (or else none) of the cgroup's CSS objects as
4192 * CSS_REMOVED. Return true on success, or false if the cgroup has
4193 * busy subsystems. Call with cgroup_mutex held
Tejun Heo48ddbe12012-04-01 12:09:56 -07004194 *
4195 * Depending on whether a subsys has __DEPRECATED_clear_css_refs set or
4196 * not, cgroup removal behaves differently.
4197 *
4198 * If clear is set, css refcnt for the subsystem should be zero before
4199 * cgroup removal can be committed. This is implemented by
4200 * CGRP_WAIT_ON_RMDIR and retry logic around ->pre_destroy(), which may be
4201 * called multiple times until all css refcnts reach zero and is allowed to
4202 * veto removal on any invocation. This behavior is deprecated and will be
4203 * removed as soon as the existing user (memcg) is updated.
4204 *
4205 * If clear is not set, each css holds an extra reference to the cgroup's
4206 * dentry and cgroup removal proceeds regardless of css refs.
4207 * ->pre_destroy() will be called at least once and is not allowed to fail.
4208 * On the last put of each css, whenever that may be, the extra dentry ref
4209 * is put so that dentry destruction happens only after all css's are
4210 * released.
Paul Menagee7c5ec92009-01-07 18:08:38 -08004211 */
Paul Menagee7c5ec92009-01-07 18:08:38 -08004212static int cgroup_clear_css_refs(struct cgroup *cgrp)
4213{
4214 struct cgroup_subsys *ss;
4215 unsigned long flags;
4216 bool failed = false;
Tejun Heo28b4c272012-04-01 12:09:56 -07004217
Paul Menagee7c5ec92009-01-07 18:08:38 -08004218 local_irq_save(flags);
Tejun Heo28b4c272012-04-01 12:09:56 -07004219
4220 /*
4221 * Block new css_tryget() by deactivating refcnt. If all refcnts
Tejun Heo48ddbe12012-04-01 12:09:56 -07004222 * for subsystems w/ clear_css_refs set were 1 at the moment of
4223 * deactivation, we succeeded.
Tejun Heo28b4c272012-04-01 12:09:56 -07004224 */
Paul Menagee7c5ec92009-01-07 18:08:38 -08004225 for_each_subsys(cgrp->root, ss) {
4226 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004227
4228 WARN_ON(atomic_read(&css->refcnt) < 0);
4229 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004230
4231 if (ss->__DEPRECATED_clear_css_refs)
4232 failed |= css_refcnt(css) != 1;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004233 }
Tejun Heo28b4c272012-04-01 12:09:56 -07004234
4235 /*
4236 * If succeeded, set REMOVED and put all the base refs; otherwise,
4237 * restore refcnts to positive values. Either way, all in-progress
4238 * css_tryget() will be released.
4239 */
Paul Menagee7c5ec92009-01-07 18:08:38 -08004240 for_each_subsys(cgrp->root, ss) {
4241 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004242
4243 if (!failed) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08004244 set_bit(CSS_REMOVED, &css->flags);
Tejun Heo28b4c272012-04-01 12:09:56 -07004245 css_put(css);
4246 } else {
4247 atomic_sub(CSS_DEACT_BIAS, &css->refcnt);
Paul Menagee7c5ec92009-01-07 18:08:38 -08004248 }
4249 }
Tejun Heo28b4c272012-04-01 12:09:56 -07004250
Paul Menagee7c5ec92009-01-07 18:08:38 -08004251 local_irq_restore(flags);
4252 return !failed;
4253}
4254
Paul Menageddbcc7e2007-10-18 23:39:30 -07004255static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4256{
Paul Menagebd89aab2007-10-18 23:40:44 -07004257 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004258 struct dentry *d;
4259 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004260 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004261 struct cgroup_event *event, *tmp;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004262 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004263
4264 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004265again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004266 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004267 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004268 mutex_unlock(&cgroup_mutex);
4269 return -EBUSY;
4270 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004271 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004272 mutex_unlock(&cgroup_mutex);
4273 return -EBUSY;
4274 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08004275 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08004276
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08004277 /*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004278 * In general, subsystem has no css->refcnt after pre_destroy(). But
4279 * in racy cases, subsystem may have to get css->refcnt after
4280 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
4281 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
4282 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
4283 * and subsystem's reference count handling. Please see css_get/put
4284 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
4285 */
4286 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4287
4288 /*
Li Zefana043e3b2008-02-23 15:24:09 -08004289 * Call pre_destroy handlers of subsys. Notify subsystems
4290 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08004291 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004292 ret = cgroup_call_pre_destroy(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004293 if (ret) {
4294 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004295 return ret;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004296 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004297
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08004298 mutex_lock(&cgroup_mutex);
4299 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004300 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004301 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004302 mutex_unlock(&cgroup_mutex);
4303 return -EBUSY;
4304 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004305 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004306 if (!cgroup_clear_css_refs(cgrp)) {
4307 mutex_unlock(&cgroup_mutex);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004308 /*
4309 * Because someone may call cgroup_wakeup_rmdir_waiter() before
4310 * prepare_to_wait(), we need to check this flag.
4311 */
4312 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
4313 schedule();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004314 finish_wait(&cgroup_rmdir_waitq, &wait);
4315 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4316 if (signal_pending(current))
4317 return -EINTR;
4318 goto again;
4319 }
4320 /* NO css_tryget() can success after here. */
4321 finish_wait(&cgroup_rmdir_waitq, &wait);
4322 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004323
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004324 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004325 set_bit(CGRP_REMOVED, &cgrp->flags);
4326 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004327 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004328 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004329
Paul Menage999cd8a2009-01-07 18:08:36 -08004330 /* delete this cgroup from parent->children */
Phil Carmody8d258792011-03-22 16:30:13 -07004331 list_del_init(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08004332
Tejun Heob0ca5a82012-04-01 12:09:54 -07004333 list_del_init(&cgrp->allcg_node);
4334
Paul Menagebd89aab2007-10-18 23:40:44 -07004335 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004336
4337 cgroup_d_remove_dir(d);
4338 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004339
Paul Menagebd89aab2007-10-18 23:40:44 -07004340 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004341 check_for_release(parent);
4342
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004343 /*
4344 * Unregister events and notify userspace.
4345 * Notify userspace about cgroup removing only after rmdir of cgroup
4346 * directory to avoid race between userspace and kernelspace
4347 */
4348 spin_lock(&cgrp->event_list_lock);
4349 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4350 list_del(&event->list);
4351 remove_wait_queue(event->wqh, &event->wait);
4352 eventfd_signal(event->eventfd, 1);
4353 schedule_work(&event->remove);
4354 }
4355 spin_unlock(&cgrp->event_list_lock);
4356
Paul Menageddbcc7e2007-10-18 23:39:30 -07004357 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004358 return 0;
4359}
4360
Tejun Heo8e3f6542012-04-01 12:09:55 -07004361static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4362{
4363 INIT_LIST_HEAD(&ss->cftsets);
4364
4365 /*
4366 * base_cftset is embedded in subsys itself, no need to worry about
4367 * deregistration.
4368 */
4369 if (ss->base_cftypes) {
4370 ss->base_cftset.cfts = ss->base_cftypes;
4371 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4372 }
4373}
4374
Li Zefan06a11922008-04-29 01:00:07 -07004375static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004376{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004377 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004378
4379 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004380
Tejun Heo8e3f6542012-04-01 12:09:55 -07004381 /* init base cftset */
4382 cgroup_init_cftsets(ss);
4383
Paul Menageddbcc7e2007-10-18 23:39:30 -07004384 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004385 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004386 ss->root = &rootnode;
Li Zefan761b3ef2012-01-31 13:47:36 +08004387 css = ss->create(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004388 /* We don't handle early failures gracefully */
4389 BUG_ON(IS_ERR(css));
4390 init_cgroup_css(css, ss, dummytop);
4391
Li Zefane8d55fd2008-04-29 01:00:13 -07004392 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004393 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004394 * newly registered, all tasks and hence the
4395 * init_css_set is in the subsystem's top cgroup. */
4396 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004397
4398 need_forkexit_callback |= ss->fork || ss->exit;
4399
Li Zefane8d55fd2008-04-29 01:00:13 -07004400 /* At system boot, before all subsystems have been
4401 * registered, no tasks have been forked, so we don't
4402 * need to invoke fork callbacks here. */
4403 BUG_ON(!list_empty(&init_task.tasks));
4404
Paul Menageddbcc7e2007-10-18 23:39:30 -07004405 ss->active = 1;
Ben Blume6a11052010-03-10 15:22:09 -08004406
4407 /* this function shouldn't be used with modular subsystems, since they
4408 * need to register a subsys_id, among other things */
4409 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004410}
4411
4412/**
Ben Blume6a11052010-03-10 15:22:09 -08004413 * cgroup_load_subsys: load and register a modular subsystem at runtime
4414 * @ss: the subsystem to load
4415 *
4416 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004417 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004418 * up for use. If the subsystem is built-in anyway, work is delegated to the
4419 * simpler cgroup_init_subsys.
4420 */
4421int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4422{
4423 int i;
4424 struct cgroup_subsys_state *css;
4425
4426 /* check name and function validity */
4427 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4428 ss->create == NULL || ss->destroy == NULL)
4429 return -EINVAL;
4430
4431 /*
4432 * we don't support callbacks in modular subsystems. this check is
4433 * before the ss->module check for consistency; a subsystem that could
4434 * be a module should still have no callbacks even if the user isn't
4435 * compiling it as one.
4436 */
4437 if (ss->fork || ss->exit)
4438 return -EINVAL;
4439
4440 /*
4441 * an optionally modular subsystem is built-in: we want to do nothing,
4442 * since cgroup_init_subsys will have already taken care of it.
4443 */
4444 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004445 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004446 BUG_ON(subsys[ss->subsys_id] != ss);
4447 return 0;
4448 }
4449
Tejun Heo8e3f6542012-04-01 12:09:55 -07004450 /* init base cftset */
4451 cgroup_init_cftsets(ss);
4452
Ben Blume6a11052010-03-10 15:22:09 -08004453 /*
4454 * need to register a subsys id before anything else - for example,
4455 * init_cgroup_css needs it.
4456 */
4457 mutex_lock(&cgroup_mutex);
4458 /* find the first empty slot in the array */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004459 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blume6a11052010-03-10 15:22:09 -08004460 if (subsys[i] == NULL)
4461 break;
4462 }
4463 if (i == CGROUP_SUBSYS_COUNT) {
4464 /* maximum number of subsystems already registered! */
4465 mutex_unlock(&cgroup_mutex);
4466 return -EBUSY;
4467 }
4468 /* assign ourselves the subsys_id */
4469 ss->subsys_id = i;
4470 subsys[i] = ss;
4471
4472 /*
4473 * no ss->create seems to need anything important in the ss struct, so
4474 * this can happen first (i.e. before the rootnode attachment).
4475 */
Li Zefan761b3ef2012-01-31 13:47:36 +08004476 css = ss->create(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004477 if (IS_ERR(css)) {
4478 /* failure case - need to deassign the subsys[] slot. */
4479 subsys[i] = NULL;
4480 mutex_unlock(&cgroup_mutex);
4481 return PTR_ERR(css);
4482 }
4483
4484 list_add(&ss->sibling, &rootnode.subsys_list);
4485 ss->root = &rootnode;
4486
4487 /* our new subsystem will be attached to the dummy hierarchy. */
4488 init_cgroup_css(css, ss, dummytop);
4489 /* init_idr must be after init_cgroup_css because it sets css->id. */
4490 if (ss->use_id) {
4491 int ret = cgroup_init_idr(ss, css);
4492 if (ret) {
4493 dummytop->subsys[ss->subsys_id] = NULL;
Li Zefan761b3ef2012-01-31 13:47:36 +08004494 ss->destroy(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004495 subsys[i] = NULL;
4496 mutex_unlock(&cgroup_mutex);
4497 return ret;
4498 }
4499 }
4500
4501 /*
4502 * Now we need to entangle the css into the existing css_sets. unlike
4503 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4504 * will need a new pointer to it; done by iterating the css_set_table.
4505 * furthermore, modifying the existing css_sets will corrupt the hash
4506 * table state, so each changed css_set will need its hash recomputed.
4507 * this is all done under the css_set_lock.
4508 */
4509 write_lock(&css_set_lock);
4510 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4511 struct css_set *cg;
4512 struct hlist_node *node, *tmp;
4513 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4514
4515 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4516 /* skip entries that we already rehashed */
4517 if (cg->subsys[ss->subsys_id])
4518 continue;
4519 /* remove existing entry */
4520 hlist_del(&cg->hlist);
4521 /* set new value */
4522 cg->subsys[ss->subsys_id] = css;
4523 /* recompute hash and restore entry */
4524 new_bucket = css_set_hash(cg->subsys);
4525 hlist_add_head(&cg->hlist, new_bucket);
4526 }
4527 }
4528 write_unlock(&css_set_lock);
4529
Ben Blume6a11052010-03-10 15:22:09 -08004530 ss->active = 1;
4531
Ben Blume6a11052010-03-10 15:22:09 -08004532 /* success! */
4533 mutex_unlock(&cgroup_mutex);
4534 return 0;
4535}
4536EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4537
4538/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004539 * cgroup_unload_subsys: unload a modular subsystem
4540 * @ss: the subsystem to unload
4541 *
4542 * This function should be called in a modular subsystem's exitcall. When this
4543 * function is invoked, the refcount on the subsystem's module will be 0, so
4544 * the subsystem will not be attached to any hierarchy.
4545 */
4546void cgroup_unload_subsys(struct cgroup_subsys *ss)
4547{
4548 struct cg_cgroup_link *link;
4549 struct hlist_head *hhead;
4550
4551 BUG_ON(ss->module == NULL);
4552
4553 /*
4554 * we shouldn't be called if the subsystem is in use, and the use of
4555 * try_module_get in parse_cgroupfs_options should ensure that it
4556 * doesn't start being used while we're killing it off.
4557 */
4558 BUG_ON(ss->root != &rootnode);
4559
4560 mutex_lock(&cgroup_mutex);
4561 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004562 subsys[ss->subsys_id] = NULL;
4563
4564 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004565 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004566
4567 /*
4568 * disentangle the css from all css_sets attached to the dummytop. as
4569 * in loading, we need to pay our respects to the hashtable gods.
4570 */
4571 write_lock(&css_set_lock);
4572 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4573 struct css_set *cg = link->cg;
4574
4575 hlist_del(&cg->hlist);
4576 BUG_ON(!cg->subsys[ss->subsys_id]);
4577 cg->subsys[ss->subsys_id] = NULL;
4578 hhead = css_set_hash(cg->subsys);
4579 hlist_add_head(&cg->hlist, hhead);
4580 }
4581 write_unlock(&css_set_lock);
4582
4583 /*
4584 * remove subsystem's css from the dummytop and free it - need to free
4585 * before marking as null because ss->destroy needs the cgrp->subsys
4586 * pointer to find their state. note that this also takes care of
4587 * freeing the css_id.
4588 */
Li Zefan761b3ef2012-01-31 13:47:36 +08004589 ss->destroy(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004590 dummytop->subsys[ss->subsys_id] = NULL;
4591
4592 mutex_unlock(&cgroup_mutex);
4593}
4594EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4595
4596/**
Li Zefana043e3b2008-02-23 15:24:09 -08004597 * cgroup_init_early - cgroup initialization at system boot
4598 *
4599 * Initialize cgroups at system boot, and initialize any
4600 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004601 */
4602int __init cgroup_init_early(void)
4603{
4604 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004605 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004606 INIT_LIST_HEAD(&init_css_set.cg_links);
4607 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004608 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004609 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004610 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004611 root_count = 1;
4612 init_task.cgroups = &init_css_set;
4613
4614 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004615 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004616 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004617 &rootnode.top_cgroup.css_sets);
4618 list_add(&init_css_set_link.cg_link_list,
4619 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004620
Li Zefan472b1052008-04-29 01:00:11 -07004621 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4622 INIT_HLIST_HEAD(&css_set_table[i]);
4623
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004624 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004625 struct cgroup_subsys *ss = subsys[i];
4626
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004627 /* at bootup time, we don't worry about modular subsystems */
4628 if (!ss || ss->module)
4629 continue;
4630
Paul Menageddbcc7e2007-10-18 23:39:30 -07004631 BUG_ON(!ss->name);
4632 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
4633 BUG_ON(!ss->create);
4634 BUG_ON(!ss->destroy);
4635 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004636 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004637 ss->name, ss->subsys_id);
4638 BUG();
4639 }
4640
4641 if (ss->early_init)
4642 cgroup_init_subsys(ss);
4643 }
4644 return 0;
4645}
4646
4647/**
Li Zefana043e3b2008-02-23 15:24:09 -08004648 * cgroup_init - cgroup initialization
4649 *
4650 * Register cgroup filesystem and /proc file, and initialize
4651 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004652 */
4653int __init cgroup_init(void)
4654{
4655 int err;
4656 int i;
Li Zefan472b1052008-04-29 01:00:11 -07004657 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07004658
4659 err = bdi_init(&cgroup_backing_dev_info);
4660 if (err)
4661 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004662
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004663 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004664 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004665
4666 /* at bootup time, we don't worry about modular subsystems */
4667 if (!ss || ss->module)
4668 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004669 if (!ss->early_init)
4670 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004671 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004672 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004673 }
4674
Li Zefan472b1052008-04-29 01:00:11 -07004675 /* Add init_css_set to the hash table */
4676 hhead = css_set_hash(init_css_set.subsys);
4677 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004678 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004679
4680 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4681 if (!cgroup_kobj) {
4682 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004683 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004684 }
4685
4686 err = register_filesystem(&cgroup_fs_type);
4687 if (err < 0) {
4688 kobject_put(cgroup_kobj);
4689 goto out;
4690 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004691
Li Zefan46ae2202008-04-29 01:00:08 -07004692 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004693
Paul Menageddbcc7e2007-10-18 23:39:30 -07004694out:
Paul Menagea4243162007-10-18 23:39:35 -07004695 if (err)
4696 bdi_destroy(&cgroup_backing_dev_info);
4697
Paul Menageddbcc7e2007-10-18 23:39:30 -07004698 return err;
4699}
Paul Menageb4f48b62007-10-18 23:39:33 -07004700
Paul Menagea4243162007-10-18 23:39:35 -07004701/*
4702 * proc_cgroup_show()
4703 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4704 * - Used for /proc/<pid>/cgroup.
4705 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4706 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004707 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004708 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4709 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4710 * cgroup to top_cgroup.
4711 */
4712
4713/* TODO: Use a proper seq_file iterator */
4714static int proc_cgroup_show(struct seq_file *m, void *v)
4715{
4716 struct pid *pid;
4717 struct task_struct *tsk;
4718 char *buf;
4719 int retval;
4720 struct cgroupfs_root *root;
4721
4722 retval = -ENOMEM;
4723 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4724 if (!buf)
4725 goto out;
4726
4727 retval = -ESRCH;
4728 pid = m->private;
4729 tsk = get_pid_task(pid, PIDTYPE_PID);
4730 if (!tsk)
4731 goto out_free;
4732
4733 retval = 0;
4734
4735 mutex_lock(&cgroup_mutex);
4736
Li Zefane5f6a862009-01-07 18:07:41 -08004737 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004738 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004739 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004740 int count = 0;
4741
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004742 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004743 for_each_subsys(root, ss)
4744 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004745 if (strlen(root->name))
4746 seq_printf(m, "%sname=%s", count ? "," : "",
4747 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004748 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004749 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004750 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004751 if (retval < 0)
4752 goto out_unlock;
4753 seq_puts(m, buf);
4754 seq_putc(m, '\n');
4755 }
4756
4757out_unlock:
4758 mutex_unlock(&cgroup_mutex);
4759 put_task_struct(tsk);
4760out_free:
4761 kfree(buf);
4762out:
4763 return retval;
4764}
4765
4766static int cgroup_open(struct inode *inode, struct file *file)
4767{
4768 struct pid *pid = PROC_I(inode)->pid;
4769 return single_open(file, proc_cgroup_show, pid);
4770}
4771
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004772const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004773 .open = cgroup_open,
4774 .read = seq_read,
4775 .llseek = seq_lseek,
4776 .release = single_release,
4777};
4778
4779/* Display information about each subsystem and each hierarchy */
4780static int proc_cgroupstats_show(struct seq_file *m, void *v)
4781{
4782 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004783
Paul Menage8bab8dd2008-04-04 14:29:57 -07004784 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004785 /*
4786 * ideally we don't want subsystems moving around while we do this.
4787 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4788 * subsys/hierarchy state.
4789 */
Paul Menagea4243162007-10-18 23:39:35 -07004790 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004791 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4792 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004793 if (ss == NULL)
4794 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004795 seq_printf(m, "%s\t%d\t%d\t%d\n",
4796 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004797 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004798 }
4799 mutex_unlock(&cgroup_mutex);
4800 return 0;
4801}
4802
4803static int cgroupstats_open(struct inode *inode, struct file *file)
4804{
Al Viro9dce07f2008-03-29 03:07:28 +00004805 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004806}
4807
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004808static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004809 .open = cgroupstats_open,
4810 .read = seq_read,
4811 .llseek = seq_lseek,
4812 .release = single_release,
4813};
4814
Paul Menageb4f48b62007-10-18 23:39:33 -07004815/**
4816 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004817 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004818 *
4819 * Description: A task inherits its parent's cgroup at fork().
4820 *
4821 * A pointer to the shared css_set was automatically copied in
4822 * fork.c by dup_task_struct(). However, we ignore that copy, since
Frederic Weisbecker7e381b0e2011-12-21 20:03:19 +01004823 * it was not made under the protection of RCU, cgroup_mutex or
4824 * threadgroup_change_begin(), so it might no longer be a valid
4825 * cgroup pointer. cgroup_attach_task() might have already changed
4826 * current->cgroups, allowing the previously referenced cgroup
4827 * group to be removed and freed.
4828 *
4829 * Outside the pointer validity we also need to process the css_set
4830 * inheritance between threadgoup_change_begin() and
4831 * threadgoup_change_end(), this way there is no leak in any process
4832 * wide migration performed by cgroup_attach_proc() that could otherwise
4833 * miss a thread because it is too early or too late in the fork stage.
Paul Menageb4f48b62007-10-18 23:39:33 -07004834 *
4835 * At the point that cgroup_fork() is called, 'current' is the parent
4836 * task, and the passed argument 'child' points to the child task.
4837 */
4838void cgroup_fork(struct task_struct *child)
4839{
Frederic Weisbecker7e381b0e2011-12-21 20:03:19 +01004840 /*
4841 * We don't need to task_lock() current because current->cgroups
4842 * can't be changed concurrently here. The parent obviously hasn't
4843 * exited and called cgroup_exit(), and we are synchronized against
4844 * cgroup migration through threadgroup_change_begin().
4845 */
Paul Menage817929e2007-10-18 23:39:36 -07004846 child->cgroups = current->cgroups;
4847 get_css_set(child->cgroups);
Paul Menage817929e2007-10-18 23:39:36 -07004848 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004849}
4850
4851/**
Li Zefana043e3b2008-02-23 15:24:09 -08004852 * cgroup_fork_callbacks - run fork callbacks
4853 * @child: the new task
4854 *
4855 * Called on a new task very soon before adding it to the
4856 * tasklist. No need to take any locks since no-one can
4857 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004858 */
4859void cgroup_fork_callbacks(struct task_struct *child)
4860{
4861 if (need_forkexit_callback) {
4862 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004863 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageb4f48b62007-10-18 23:39:33 -07004864 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004865
4866 /*
4867 * forkexit callbacks are only supported for
4868 * builtin subsystems.
4869 */
4870 if (!ss || ss->module)
4871 continue;
4872
Paul Menageb4f48b62007-10-18 23:39:33 -07004873 if (ss->fork)
Li Zefan761b3ef2012-01-31 13:47:36 +08004874 ss->fork(child);
Paul Menageb4f48b62007-10-18 23:39:33 -07004875 }
4876 }
4877}
4878
4879/**
Li Zefana043e3b2008-02-23 15:24:09 -08004880 * cgroup_post_fork - called on a new task after adding it to the task list
4881 * @child: the task in question
4882 *
4883 * Adds the task to the list running through its css_set if necessary.
4884 * Has to be after the task is visible on the task list in case we race
4885 * with the first call to cgroup_iter_start() - to guarantee that the
4886 * new task ends up on its list.
4887 */
Paul Menage817929e2007-10-18 23:39:36 -07004888void cgroup_post_fork(struct task_struct *child)
4889{
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004890 /*
4891 * use_task_css_set_links is set to 1 before we walk the tasklist
4892 * under the tasklist_lock and we read it here after we added the child
4893 * to the tasklist under the tasklist_lock as well. If the child wasn't
4894 * yet in the tasklist when we walked through it from
4895 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4896 * should be visible now due to the paired locking and barriers implied
4897 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4898 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4899 * lock on fork.
4900 */
Paul Menage817929e2007-10-18 23:39:36 -07004901 if (use_task_css_set_links) {
4902 write_lock(&css_set_lock);
Frederic Weisbecker7e3aa302011-12-23 04:25:23 +01004903 if (list_empty(&child->cg_list)) {
4904 /*
4905 * It's safe to use child->cgroups without task_lock()
4906 * here because we are protected through
4907 * threadgroup_change_begin() against concurrent
4908 * css_set change in cgroup_task_migrate(). Also
4909 * the task can't exit at that point until
4910 * wake_up_new_task() is called, so we are protected
4911 * against cgroup_exit() setting child->cgroup to
4912 * init_css_set.
4913 */
Paul Menage817929e2007-10-18 23:39:36 -07004914 list_add(&child->cg_list, &child->cgroups->tasks);
Frederic Weisbecker7e3aa302011-12-23 04:25:23 +01004915 }
Paul Menage817929e2007-10-18 23:39:36 -07004916 write_unlock(&css_set_lock);
4917 }
4918}
4919/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004920 * cgroup_exit - detach cgroup from exiting task
4921 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004922 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004923 *
4924 * Description: Detach cgroup from @tsk and release it.
4925 *
4926 * Note that cgroups marked notify_on_release force every task in
4927 * them to take the global cgroup_mutex mutex when exiting.
4928 * This could impact scaling on very large systems. Be reluctant to
4929 * use notify_on_release cgroups where very high task exit scaling
4930 * is required on large systems.
4931 *
4932 * the_top_cgroup_hack:
4933 *
4934 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4935 *
4936 * We call cgroup_exit() while the task is still competent to
4937 * handle notify_on_release(), then leave the task attached to the
4938 * root cgroup in each hierarchy for the remainder of its exit.
4939 *
4940 * To do this properly, we would increment the reference count on
4941 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4942 * code we would add a second cgroup function call, to drop that
4943 * reference. This would just create an unnecessary hot spot on
4944 * the top_cgroup reference count, to no avail.
4945 *
4946 * Normally, holding a reference to a cgroup without bumping its
4947 * count is unsafe. The cgroup could go away, or someone could
4948 * attach us to a different cgroup, decrementing the count on
4949 * the first cgroup that we never incremented. But in this case,
4950 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004951 * which wards off any cgroup_attach_task() attempts, or task is a failed
4952 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004953 */
4954void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4955{
Paul Menage817929e2007-10-18 23:39:36 -07004956 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004957 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004958
4959 /*
4960 * Unlink from the css_set task list if necessary.
4961 * Optimistically check cg_list before taking
4962 * css_set_lock
4963 */
4964 if (!list_empty(&tsk->cg_list)) {
4965 write_lock(&css_set_lock);
4966 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004967 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004968 write_unlock(&css_set_lock);
4969 }
4970
Paul Menageb4f48b62007-10-18 23:39:33 -07004971 /* Reassign the task to the init_css_set. */
4972 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004973 cg = tsk->cgroups;
4974 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004975
4976 if (run_callbacks && need_forkexit_callback) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004977 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004978 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004979
4980 /* modular subsystems can't use callbacks */
4981 if (!ss || ss->module)
4982 continue;
4983
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004984 if (ss->exit) {
4985 struct cgroup *old_cgrp =
4986 rcu_dereference_raw(cg->subsys[i])->cgroup;
4987 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004988 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004989 }
4990 }
4991 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004992 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004993
Paul Menage817929e2007-10-18 23:39:36 -07004994 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004995 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004996}
Paul Menage697f4162007-10-18 23:39:34 -07004997
4998/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004999 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08005000 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005001 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08005002 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005003 * See if @cgrp is a descendant of @task's cgroup in the appropriate
5004 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07005005 *
5006 * If we are sending in dummytop, then presumably we are creating
5007 * the top cgroup in the subsystem.
5008 *
5009 * Called only by the ns (nsproxy) cgroup.
5010 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005011int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07005012{
5013 int ret;
5014 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07005015
Paul Menagebd89aab2007-10-18 23:40:44 -07005016 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07005017 return 1;
5018
Paul Menage7717f7b2009-09-23 15:56:22 -07005019 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005020 while (cgrp != target && cgrp!= cgrp->top_cgroup)
5021 cgrp = cgrp->parent;
5022 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07005023 return ret;
5024}
Paul Menage81a6a5c2007-10-18 23:39:38 -07005025
Paul Menagebd89aab2007-10-18 23:40:44 -07005026static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005027{
5028 /* All of these checks rely on RCU to keep the cgroup
5029 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07005030 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
5031 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07005032 /* Control Group is currently removeable. If it's not
5033 * already queued for a userspace notification, queue
5034 * it now */
5035 int need_schedule_work = 0;
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005036 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07005037 if (!cgroup_is_removed(cgrp) &&
5038 list_empty(&cgrp->release_list)) {
5039 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005040 need_schedule_work = 1;
5041 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005042 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005043 if (need_schedule_work)
5044 schedule_work(&release_agent_work);
5045 }
5046}
5047
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005048/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005049bool __css_tryget(struct cgroup_subsys_state *css)
5050{
5051 do {
5052 int v = css_refcnt(css);
5053
5054 if (atomic_cmpxchg(&css->refcnt, v, v + 1) == v)
5055 return true;
5056 cpu_relax();
5057 } while (!test_bit(CSS_REMOVED, &css->flags));
5058
5059 return false;
5060}
5061EXPORT_SYMBOL_GPL(__css_tryget);
5062
5063/* Caller must verify that the css is not for root cgroup */
5064void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005065{
Paul Menagebd89aab2007-10-18 23:40:44 -07005066 struct cgroup *cgrp = css->cgroup;
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005067 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005068
Paul Menage81a6a5c2007-10-18 23:39:38 -07005069 rcu_read_lock();
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005070 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
5071
5072 switch (v) {
Tejun Heo48ddbe12012-04-01 12:09:56 -07005073 case 1:
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005074 if (notify_on_release(cgrp)) {
5075 set_bit(CGRP_RELEASABLE, &cgrp->flags);
5076 check_for_release(cgrp);
5077 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07005078 cgroup_wakeup_rmdir_waiter(cgrp);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005079 break;
5080 case 0:
5081 if (!test_bit(CSS_CLEAR_CSS_REFS, &css->flags))
5082 schedule_work(&css->dput_work);
5083 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005084 }
5085 rcu_read_unlock();
5086}
Ben Blum67523c42010-03-10 15:22:11 -08005087EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005088
5089/*
5090 * Notify userspace when a cgroup is released, by running the
5091 * configured release agent with the name of the cgroup (path
5092 * relative to the root of cgroup file system) as the argument.
5093 *
5094 * Most likely, this user command will try to rmdir this cgroup.
5095 *
5096 * This races with the possibility that some other task will be
5097 * attached to this cgroup before it is removed, or that some other
5098 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5099 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5100 * unused, and this cgroup will be reprieved from its death sentence,
5101 * to continue to serve a useful existence. Next time it's released,
5102 * we will get notified again, if it still has 'notify_on_release' set.
5103 *
5104 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5105 * means only wait until the task is successfully execve()'d. The
5106 * separate release agent task is forked by call_usermodehelper(),
5107 * then control in this thread returns here, without waiting for the
5108 * release agent task. We don't bother to wait because the caller of
5109 * this routine has no use for the exit status of the release agent
5110 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005111 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005112static void cgroup_release_agent(struct work_struct *work)
5113{
5114 BUG_ON(work != &release_agent_work);
5115 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005116 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005117 while (!list_empty(&release_list)) {
5118 char *argv[3], *envp[3];
5119 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005120 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005121 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005122 struct cgroup,
5123 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005124 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005125 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005126 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005127 if (!pathbuf)
5128 goto continue_free;
5129 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5130 goto continue_free;
5131 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5132 if (!agentbuf)
5133 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005134
5135 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005136 argv[i++] = agentbuf;
5137 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005138 argv[i] = NULL;
5139
5140 i = 0;
5141 /* minimal command environment */
5142 envp[i++] = "HOME=/";
5143 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5144 envp[i] = NULL;
5145
5146 /* Drop the lock while we invoke the usermode helper,
5147 * since the exec could involve hitting disk and hence
5148 * be a slow process */
5149 mutex_unlock(&cgroup_mutex);
5150 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005151 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005152 continue_free:
5153 kfree(pathbuf);
5154 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005155 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005156 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005157 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005158 mutex_unlock(&cgroup_mutex);
5159}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005160
5161static int __init cgroup_disable(char *str)
5162{
5163 int i;
5164 char *token;
5165
5166 while ((token = strsep(&str, ",")) != NULL) {
5167 if (!*token)
5168 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005169 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005170 struct cgroup_subsys *ss = subsys[i];
5171
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005172 /*
5173 * cgroup_disable, being at boot time, can't
5174 * know about module subsystems, so we don't
5175 * worry about them.
5176 */
5177 if (!ss || ss->module)
5178 continue;
5179
Paul Menage8bab8dd2008-04-04 14:29:57 -07005180 if (!strcmp(token, ss->name)) {
5181 ss->disabled = 1;
5182 printk(KERN_INFO "Disabling %s control group"
5183 " subsystem\n", ss->name);
5184 break;
5185 }
5186 }
5187 }
5188 return 1;
5189}
5190__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005191
5192/*
5193 * Functons for CSS ID.
5194 */
5195
5196/*
5197 *To get ID other than 0, this should be called when !cgroup_is_removed().
5198 */
5199unsigned short css_id(struct cgroup_subsys_state *css)
5200{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005201 struct css_id *cssid;
5202
5203 /*
5204 * This css_id() can return correct value when somone has refcnt
5205 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5206 * it's unchanged until freed.
5207 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005208 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005209
5210 if (cssid)
5211 return cssid->id;
5212 return 0;
5213}
Ben Blum67523c42010-03-10 15:22:11 -08005214EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005215
5216unsigned short css_depth(struct cgroup_subsys_state *css)
5217{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005218 struct css_id *cssid;
5219
Tejun Heo28b4c272012-04-01 12:09:56 -07005220 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005221
5222 if (cssid)
5223 return cssid->depth;
5224 return 0;
5225}
Ben Blum67523c42010-03-10 15:22:11 -08005226EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005227
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005228/**
5229 * css_is_ancestor - test "root" css is an ancestor of "child"
5230 * @child: the css to be tested.
5231 * @root: the css supporsed to be an ancestor of the child.
5232 *
5233 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005234 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005235 * But, considering usual usage, the csses should be valid objects after test.
5236 * Assuming that the caller will do some action to the child if this returns
5237 * returns true, the caller must take "child";s reference count.
5238 * If "child" is valid object and this returns true, "root" is valid, too.
5239 */
5240
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005241bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005242 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005243{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005244 struct css_id *child_id;
5245 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005246
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005247 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005248 if (!child_id)
5249 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005250 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005251 if (!root_id)
5252 return false;
5253 if (child_id->depth < root_id->depth)
5254 return false;
5255 if (child_id->stack[root_id->depth] != root_id->id)
5256 return false;
5257 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005258}
5259
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005260void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5261{
5262 struct css_id *id = css->id;
5263 /* When this is called before css_id initialization, id can be NULL */
5264 if (!id)
5265 return;
5266
5267 BUG_ON(!ss->use_id);
5268
5269 rcu_assign_pointer(id->css, NULL);
5270 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005271 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005272 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005273 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005274 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005275}
Ben Blum67523c42010-03-10 15:22:11 -08005276EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005277
5278/*
5279 * This is called by init or create(). Then, calls to this function are
5280 * always serialized (By cgroup_mutex() at create()).
5281 */
5282
5283static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5284{
5285 struct css_id *newid;
5286 int myid, error, size;
5287
5288 BUG_ON(!ss->use_id);
5289
5290 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5291 newid = kzalloc(size, GFP_KERNEL);
5292 if (!newid)
5293 return ERR_PTR(-ENOMEM);
5294 /* get id */
5295 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
5296 error = -ENOMEM;
5297 goto err_out;
5298 }
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005299 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005300 /* Don't use 0. allocates an ID of 1-65535 */
5301 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005302 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005303
5304 /* Returns error when there are no free spaces for new ID.*/
5305 if (error) {
5306 error = -ENOSPC;
5307 goto err_out;
5308 }
5309 if (myid > CSS_ID_MAX)
5310 goto remove_idr;
5311
5312 newid->id = myid;
5313 newid->depth = depth;
5314 return newid;
5315remove_idr:
5316 error = -ENOSPC;
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005317 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005318 idr_remove(&ss->idr, myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005319 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005320err_out:
5321 kfree(newid);
5322 return ERR_PTR(error);
5323
5324}
5325
Ben Blume6a11052010-03-10 15:22:09 -08005326static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5327 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005328{
5329 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005330
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005331 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005332 idr_init(&ss->idr);
5333
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005334 newid = get_new_cssid(ss, 0);
5335 if (IS_ERR(newid))
5336 return PTR_ERR(newid);
5337
5338 newid->stack[0] = newid->id;
5339 newid->css = rootcss;
5340 rootcss->id = newid;
5341 return 0;
5342}
5343
5344static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5345 struct cgroup *child)
5346{
5347 int subsys_id, i, depth = 0;
5348 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005349 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005350
5351 subsys_id = ss->subsys_id;
5352 parent_css = parent->subsys[subsys_id];
5353 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005354 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005355 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005356
5357 child_id = get_new_cssid(ss, depth);
5358 if (IS_ERR(child_id))
5359 return PTR_ERR(child_id);
5360
5361 for (i = 0; i < depth; i++)
5362 child_id->stack[i] = parent_id->stack[i];
5363 child_id->stack[depth] = child_id->id;
5364 /*
5365 * child_id->css pointer will be set after this cgroup is available
5366 * see cgroup_populate_dir()
5367 */
5368 rcu_assign_pointer(child_css->id, child_id);
5369
5370 return 0;
5371}
5372
5373/**
5374 * css_lookup - lookup css by id
5375 * @ss: cgroup subsys to be looked into.
5376 * @id: the id
5377 *
5378 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5379 * NULL if not. Should be called under rcu_read_lock()
5380 */
5381struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5382{
5383 struct css_id *cssid = NULL;
5384
5385 BUG_ON(!ss->use_id);
5386 cssid = idr_find(&ss->idr, id);
5387
5388 if (unlikely(!cssid))
5389 return NULL;
5390
5391 return rcu_dereference(cssid->css);
5392}
Ben Blum67523c42010-03-10 15:22:11 -08005393EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005394
5395/**
5396 * css_get_next - lookup next cgroup under specified hierarchy.
5397 * @ss: pointer to subsystem
5398 * @id: current position of iteration.
5399 * @root: pointer to css. search tree under this.
5400 * @foundid: position of found object.
5401 *
5402 * Search next css under the specified hierarchy of rootid. Calling under
5403 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5404 */
5405struct cgroup_subsys_state *
5406css_get_next(struct cgroup_subsys *ss, int id,
5407 struct cgroup_subsys_state *root, int *foundid)
5408{
5409 struct cgroup_subsys_state *ret = NULL;
5410 struct css_id *tmp;
5411 int tmpid;
5412 int rootid = css_id(root);
5413 int depth = css_depth(root);
5414
5415 if (!rootid)
5416 return NULL;
5417
5418 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005419 WARN_ON_ONCE(!rcu_read_lock_held());
5420
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005421 /* fill start point for scan */
5422 tmpid = id;
5423 while (1) {
5424 /*
5425 * scan next entry from bitmap(tree), tmpid is updated after
5426 * idr_get_next().
5427 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005428 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005429 if (!tmp)
5430 break;
5431 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5432 ret = rcu_dereference(tmp->css);
5433 if (ret) {
5434 *foundid = tmpid;
5435 break;
5436 }
5437 }
5438 /* continue to scan from next id */
5439 tmpid = tmpid + 1;
5440 }
5441 return ret;
5442}
5443
Stephane Eraniane5d13672011-02-14 11:20:01 +02005444/*
5445 * get corresponding css from file open on cgroupfs directory
5446 */
5447struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5448{
5449 struct cgroup *cgrp;
5450 struct inode *inode;
5451 struct cgroup_subsys_state *css;
5452
5453 inode = f->f_dentry->d_inode;
5454 /* check in cgroup filesystem dir */
5455 if (inode->i_op != &cgroup_dir_inode_operations)
5456 return ERR_PTR(-EBADF);
5457
5458 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5459 return ERR_PTR(-EINVAL);
5460
5461 /* get cgroup */
5462 cgrp = __d_cgrp(f->f_dentry);
5463 css = cgrp->subsys[id];
5464 return css ? css : ERR_PTR(-ENOENT);
5465}
5466
Paul Menagefe693432009-09-23 15:56:20 -07005467#ifdef CONFIG_CGROUP_DEBUG
Li Zefan761b3ef2012-01-31 13:47:36 +08005468static struct cgroup_subsys_state *debug_create(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005469{
5470 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5471
5472 if (!css)
5473 return ERR_PTR(-ENOMEM);
5474
5475 return css;
5476}
5477
Li Zefan761b3ef2012-01-31 13:47:36 +08005478static void debug_destroy(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005479{
5480 kfree(cont->subsys[debug_subsys_id]);
5481}
5482
5483static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5484{
5485 return atomic_read(&cont->count);
5486}
5487
5488static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5489{
5490 return cgroup_task_count(cont);
5491}
5492
5493static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5494{
5495 return (u64)(unsigned long)current->cgroups;
5496}
5497
5498static u64 current_css_set_refcount_read(struct cgroup *cont,
5499 struct cftype *cft)
5500{
5501 u64 count;
5502
5503 rcu_read_lock();
5504 count = atomic_read(&current->cgroups->refcount);
5505 rcu_read_unlock();
5506 return count;
5507}
5508
Paul Menage7717f7b2009-09-23 15:56:22 -07005509static int current_css_set_cg_links_read(struct cgroup *cont,
5510 struct cftype *cft,
5511 struct seq_file *seq)
5512{
5513 struct cg_cgroup_link *link;
5514 struct css_set *cg;
5515
5516 read_lock(&css_set_lock);
5517 rcu_read_lock();
5518 cg = rcu_dereference(current->cgroups);
5519 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5520 struct cgroup *c = link->cgrp;
5521 const char *name;
5522
5523 if (c->dentry)
5524 name = c->dentry->d_name.name;
5525 else
5526 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005527 seq_printf(seq, "Root %d group %s\n",
5528 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005529 }
5530 rcu_read_unlock();
5531 read_unlock(&css_set_lock);
5532 return 0;
5533}
5534
5535#define MAX_TASKS_SHOWN_PER_CSS 25
5536static int cgroup_css_links_read(struct cgroup *cont,
5537 struct cftype *cft,
5538 struct seq_file *seq)
5539{
5540 struct cg_cgroup_link *link;
5541
5542 read_lock(&css_set_lock);
5543 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5544 struct css_set *cg = link->cg;
5545 struct task_struct *task;
5546 int count = 0;
5547 seq_printf(seq, "css_set %p\n", cg);
5548 list_for_each_entry(task, &cg->tasks, cg_list) {
5549 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5550 seq_puts(seq, " ...\n");
5551 break;
5552 } else {
5553 seq_printf(seq, " task %d\n",
5554 task_pid_vnr(task));
5555 }
5556 }
5557 }
5558 read_unlock(&css_set_lock);
5559 return 0;
5560}
5561
Paul Menagefe693432009-09-23 15:56:20 -07005562static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5563{
5564 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5565}
5566
5567static struct cftype debug_files[] = {
5568 {
5569 .name = "cgroup_refcount",
5570 .read_u64 = cgroup_refcount_read,
5571 },
5572 {
5573 .name = "taskcount",
5574 .read_u64 = debug_taskcount_read,
5575 },
5576
5577 {
5578 .name = "current_css_set",
5579 .read_u64 = current_css_set_read,
5580 },
5581
5582 {
5583 .name = "current_css_set_refcount",
5584 .read_u64 = current_css_set_refcount_read,
5585 },
5586
5587 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005588 .name = "current_css_set_cg_links",
5589 .read_seq_string = current_css_set_cg_links_read,
5590 },
5591
5592 {
5593 .name = "cgroup_css_links",
5594 .read_seq_string = cgroup_css_links_read,
5595 },
5596
5597 {
Paul Menagefe693432009-09-23 15:56:20 -07005598 .name = "releasable",
5599 .read_u64 = releasable_read,
5600 },
Paul Menagefe693432009-09-23 15:56:20 -07005601
Tejun Heo4baf6e32012-04-01 12:09:55 -07005602 { } /* terminate */
5603};
Paul Menagefe693432009-09-23 15:56:20 -07005604
5605struct cgroup_subsys debug_subsys = {
5606 .name = "debug",
5607 .create = debug_create,
5608 .destroy = debug_destroy,
Paul Menagefe693432009-09-23 15:56:20 -07005609 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005610 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005611};
5612#endif /* CONFIG_CGROUP_DEBUG */