blob: 6643f70534544b13729dd41130d92a77c12a81ec [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
33#include <linux/fs.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100034#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/kernel.h>
36#include <linux/list.h>
37#include <linux/mm.h>
38#include <linux/mutex.h>
39#include <linux/mount.h>
40#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070041#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070042#include <linux/rcupdate.h>
43#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070044#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/seq_file.h>
46#include <linux/slab.h>
47#include <linux/magic.h>
48#include <linux/spinlock.h>
49#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080052#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070053#include <linux/delayacct.h>
54#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070055#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040056#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070057#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070058#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070059#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080060#include <linux/eventfd.h>
61#include <linux/poll.h>
Ben Blumd8466872011-05-26 16:25:21 -070062#include <linux/flex_array.h> /* used in cgroup_attach_proc */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020063#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070064
Arun Sharma600634972011-07-26 16:09:06 -070065#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070066
Tejun Heo28b4c272012-04-01 12:09:56 -070067/* css deactivation bias, makes css->refcnt negative to deny new trygets */
68#define CSS_DEACT_BIAS INT_MIN
69
Tejun Heoe25e2cb2011-12-12 18:12:21 -080070/*
71 * cgroup_mutex is the master lock. Any modification to cgroup or its
72 * hierarchy must be performed while holding it.
73 *
74 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
75 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
76 * release_agent_path and so on. Modifying requires both cgroup_mutex and
77 * cgroup_root_mutex. Readers can acquire either of the two. This is to
78 * break the following locking order cycle.
79 *
80 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
81 * B. namespace_sem -> cgroup_mutex
82 *
83 * B happens only through cgroup_show_options() and using cgroup_root_mutex
84 * breaks it.
85 */
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -080087static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070088
Ben Blumaae8aab2010-03-10 15:22:07 -080089/*
90 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020091 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080092 * registered after that. The mutable section of this array is protected by
93 * cgroup_mutex.
94 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020095#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020096#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -080097static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070098#include <linux/cgroup_subsys.h>
99};
100
Paul Menagec6d57f32009-09-23 15:56:19 -0700101#define MAX_CGROUP_ROOT_NAMELEN 64
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
104 * A cgroupfs_root represents the root of a cgroup hierarchy,
105 * and may be associated with a superblock to form an active
106 * hierarchy
107 */
108struct cgroupfs_root {
109 struct super_block *sb;
110
111 /*
112 * The bitmask of subsystems intended to be attached to this
113 * hierarchy
114 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400115 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700116
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700117 /* Unique id for this hierarchy. */
118 int hierarchy_id;
119
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120 /* The bitmask of subsystems currently attached to this hierarchy */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400121 unsigned long actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122
123 /* A list running through the attached subsystems */
124 struct list_head subsys_list;
125
126 /* The root cgroup for this hierarchy */
127 struct cgroup top_cgroup;
128
129 /* Tracks how many cgroups are currently defined in hierarchy.*/
130 int number_of_cgroups;
131
Li Zefane5f6a862009-01-07 18:07:41 -0800132 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700133 struct list_head root_list;
134
Tejun Heob0ca5a82012-04-01 12:09:54 -0700135 /* All cgroups on this root, cgroup_mutex protected */
136 struct list_head allcg_list;
137
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138 /* Hierarchy-specific flags */
139 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700140
Tejun Heo0a950f62012-11-19 09:02:12 -0800141 /* IDs for cgroups in this hierarchy */
142 struct ida cgroup_ida;
143
Paul Menagee788e062008-07-25 01:46:59 -0700144 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700145 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700146
147 /* The name for this hierarchy - may be empty */
148 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700149};
150
Paul Menageddbcc7e2007-10-18 23:39:30 -0700151/*
152 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
153 * subsystems that are otherwise unattached - it never has more than a
154 * single cgroup, and all tasks are part of that cgroup.
155 */
156static struct cgroupfs_root rootnode;
157
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700158/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700159 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
160 */
161struct cfent {
162 struct list_head node;
163 struct dentry *dentry;
164 struct cftype *type;
165};
166
167/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700168 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
169 * cgroup_subsys->use_id != 0.
170 */
171#define CSS_ID_MAX (65535)
172struct css_id {
173 /*
174 * The css to which this ID points. This pointer is set to valid value
175 * after cgroup is populated. If cgroup is removed, this will be NULL.
176 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800177 * is called after synchronize_rcu(). But for safe use, css_tryget()
178 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700179 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100180 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700181 /*
182 * ID of this css.
183 */
184 unsigned short id;
185 /*
186 * Depth in hierarchy which this ID belongs to.
187 */
188 unsigned short depth;
189 /*
190 * ID is freed by RCU. (and lookup routine is RCU safe.)
191 */
192 struct rcu_head rcu_head;
193 /*
194 * Hierarchy of CSS ID belongs to.
195 */
196 unsigned short stack[0]; /* Array of Length (depth+1) */
197};
198
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800199/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300200 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800201 */
202struct cgroup_event {
203 /*
204 * Cgroup which the event belongs to.
205 */
206 struct cgroup *cgrp;
207 /*
208 * Control file which the event associated.
209 */
210 struct cftype *cft;
211 /*
212 * eventfd to signal userspace about the event.
213 */
214 struct eventfd_ctx *eventfd;
215 /*
216 * Each of these stored in a list by the cgroup.
217 */
218 struct list_head list;
219 /*
220 * All fields below needed to unregister event when
221 * userspace closes eventfd.
222 */
223 poll_table pt;
224 wait_queue_head_t *wqh;
225 wait_queue_t wait;
226 struct work_struct remove;
227};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700228
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229/* The list of hierarchy roots */
230
231static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700232static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700233
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700234static DEFINE_IDA(hierarchy_ida);
235static int next_hierarchy_id;
236static DEFINE_SPINLOCK(hierarchy_id_lock);
237
Paul Menageddbcc7e2007-10-18 23:39:30 -0700238/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
239#define dummytop (&rootnode.top_cgroup)
240
241/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800242 * check for fork/exit handlers to call. This avoids us having to do
243 * extra work in the fork/exit path if none of the subsystems need to
244 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700245 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700246static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700247
Tejun Heo42809dd2012-11-19 08:13:37 -0800248static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800249static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
250 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800251
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800252#ifdef CONFIG_PROVE_LOCKING
253int cgroup_lock_is_held(void)
254{
255 return lockdep_is_held(&cgroup_mutex);
256}
257#else /* #ifdef CONFIG_PROVE_LOCKING */
258int cgroup_lock_is_held(void)
259{
260 return mutex_is_locked(&cgroup_mutex);
261}
262#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
263
264EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
265
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700266static int css_unbias_refcnt(int refcnt)
267{
268 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
269}
270
Tejun Heo28b4c272012-04-01 12:09:56 -0700271/* the current nr of refs, always >= 0 whether @css is deactivated or not */
272static int css_refcnt(struct cgroup_subsys_state *css)
273{
274 int v = atomic_read(&css->refcnt);
275
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700276 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700277}
278
Paul Menageddbcc7e2007-10-18 23:39:30 -0700279/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700280inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700281{
Paul Menagebd89aab2007-10-18 23:40:44 -0700282 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700283}
284
285/* bits in struct cgroupfs_root flags field */
286enum {
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400287 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
288 ROOT_XATTR, /* supports extended attributes */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700289};
290
Adrian Bunke9685a02008-02-07 00:13:46 -0800291static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700292{
293 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700294 (1 << CGRP_RELEASABLE) |
295 (1 << CGRP_NOTIFY_ON_RELEASE);
296 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700297}
298
Adrian Bunke9685a02008-02-07 00:13:46 -0800299static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700300{
Paul Menagebd89aab2007-10-18 23:40:44 -0700301 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700302}
303
Paul Menageddbcc7e2007-10-18 23:39:30 -0700304/*
305 * for_each_subsys() allows you to iterate on each subsystem attached to
306 * an active hierarchy
307 */
308#define for_each_subsys(_root, _ss) \
309list_for_each_entry(_ss, &_root->subsys_list, sibling)
310
Li Zefane5f6a862009-01-07 18:07:41 -0800311/* for_each_active_root() allows you to iterate across the active hierarchies */
312#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700313list_for_each_entry(_root, &roots, root_list)
314
Tejun Heof6ea9372012-04-01 12:09:55 -0700315static inline struct cgroup *__d_cgrp(struct dentry *dentry)
316{
317 return dentry->d_fsdata;
318}
319
Tejun Heo05ef1d72012-04-01 12:09:56 -0700320static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700321{
322 return dentry->d_fsdata;
323}
324
Tejun Heo05ef1d72012-04-01 12:09:56 -0700325static inline struct cftype *__d_cft(struct dentry *dentry)
326{
327 return __d_cfe(dentry)->type;
328}
329
Paul Menage81a6a5c2007-10-18 23:39:38 -0700330/* the list of cgroups eligible for automatic release. Protected by
331 * release_list_lock */
332static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200333static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700334static void cgroup_release_agent(struct work_struct *work);
335static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700336static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700337
Paul Menage817929e2007-10-18 23:39:36 -0700338/* Link structure for associating css_set objects with cgroups */
339struct cg_cgroup_link {
340 /*
341 * List running through cg_cgroup_links associated with a
342 * cgroup, anchored on cgroup->css_sets
343 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700344 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700345 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700346 /*
347 * List running through cg_cgroup_links pointing at a
348 * single css_set object, anchored on css_set->cg_links
349 */
350 struct list_head cg_link_list;
351 struct css_set *cg;
352};
353
354/* The default css_set - used by init and its children prior to any
355 * hierarchies being mounted. It contains a pointer to the root state
356 * for each subsystem. Also used to anchor the list of css_sets. Not
357 * reference-counted, to improve performance when child cgroups
358 * haven't been created.
359 */
360
361static struct css_set init_css_set;
362static struct cg_cgroup_link init_css_set_link;
363
Ben Blume6a11052010-03-10 15:22:09 -0800364static int cgroup_init_idr(struct cgroup_subsys *ss,
365 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700366
Paul Menage817929e2007-10-18 23:39:36 -0700367/* css_set_lock protects the list of css_set objects, and the
368 * chain of tasks off each css_set. Nests outside task->alloc_lock
369 * due to cgroup_iter_start() */
370static DEFINE_RWLOCK(css_set_lock);
371static int css_set_count;
372
Paul Menage7717f7b2009-09-23 15:56:22 -0700373/*
374 * hash table for cgroup groups. This improves the performance to find
375 * an existing css_set. This hash doesn't (currently) take into
376 * account cgroups in empty hierarchies.
377 */
Li Zefan472b1052008-04-29 01:00:11 -0700378#define CSS_SET_HASH_BITS 7
379#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
380static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
381
382static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
383{
384 int i;
385 int index;
386 unsigned long tmp = 0UL;
387
388 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
389 tmp += (unsigned long)css[i];
390 tmp = (tmp >> 16) ^ tmp;
391
392 index = hash_long(tmp, CSS_SET_HASH_BITS);
393
394 return &css_set_table[index];
395}
396
Paul Menage817929e2007-10-18 23:39:36 -0700397/* We don't maintain the lists running through each css_set to its
398 * task until after the first call to cgroup_iter_start(). This
399 * reduces the fork()/exit() overhead for people who have cgroups
400 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700401static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700402
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700403static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700404{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700405 struct cg_cgroup_link *link;
406 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700407 /*
408 * Ensure that the refcount doesn't hit zero while any readers
409 * can see it. Similar to atomic_dec_and_lock(), but for an
410 * rwlock
411 */
412 if (atomic_add_unless(&cg->refcount, -1, 1))
413 return;
414 write_lock(&css_set_lock);
415 if (!atomic_dec_and_test(&cg->refcount)) {
416 write_unlock(&css_set_lock);
417 return;
418 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700419
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420 /* This css_set is dead. unlink it and release cgroup refcounts */
421 hlist_del(&cg->hlist);
422 css_set_count--;
423
424 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
425 cg_link_list) {
426 struct cgroup *cgrp = link->cgrp;
427 list_del(&link->cg_link_list);
428 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700429 if (atomic_dec_and_test(&cgrp->count) &&
430 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700431 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700432 set_bit(CGRP_RELEASABLE, &cgrp->flags);
433 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700434 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700435
436 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700437 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700438
439 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800440 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700441}
442
443/*
444 * refcounted get/put for css_set objects
445 */
446static inline void get_css_set(struct css_set *cg)
447{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700448 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700449}
450
451static inline void put_css_set(struct css_set *cg)
452{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700453 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700454}
455
Paul Menage81a6a5c2007-10-18 23:39:38 -0700456static inline void put_css_set_taskexit(struct css_set *cg)
457{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700458 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700459}
460
Paul Menage817929e2007-10-18 23:39:36 -0700461/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700462 * compare_css_sets - helper function for find_existing_css_set().
463 * @cg: candidate css_set being tested
464 * @old_cg: existing css_set for a task
465 * @new_cgrp: cgroup that's being entered by the task
466 * @template: desired set of css pointers in css_set (pre-calculated)
467 *
468 * Returns true if "cg" matches "old_cg" except for the hierarchy
469 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
470 */
471static bool compare_css_sets(struct css_set *cg,
472 struct css_set *old_cg,
473 struct cgroup *new_cgrp,
474 struct cgroup_subsys_state *template[])
475{
476 struct list_head *l1, *l2;
477
478 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
479 /* Not all subsystems matched */
480 return false;
481 }
482
483 /*
484 * Compare cgroup pointers in order to distinguish between
485 * different cgroups in heirarchies with no subsystems. We
486 * could get by with just this check alone (and skip the
487 * memcmp above) but on most setups the memcmp check will
488 * avoid the need for this more expensive check on almost all
489 * candidates.
490 */
491
492 l1 = &cg->cg_links;
493 l2 = &old_cg->cg_links;
494 while (1) {
495 struct cg_cgroup_link *cgl1, *cgl2;
496 struct cgroup *cg1, *cg2;
497
498 l1 = l1->next;
499 l2 = l2->next;
500 /* See if we reached the end - both lists are equal length. */
501 if (l1 == &cg->cg_links) {
502 BUG_ON(l2 != &old_cg->cg_links);
503 break;
504 } else {
505 BUG_ON(l2 == &old_cg->cg_links);
506 }
507 /* Locate the cgroups associated with these links. */
508 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
509 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
510 cg1 = cgl1->cgrp;
511 cg2 = cgl2->cgrp;
512 /* Hierarchies should be linked in the same order. */
513 BUG_ON(cg1->root != cg2->root);
514
515 /*
516 * If this hierarchy is the hierarchy of the cgroup
517 * that's changing, then we need to check that this
518 * css_set points to the new cgroup; if it's any other
519 * hierarchy, then this css_set should point to the
520 * same cgroup as the old css_set.
521 */
522 if (cg1->root == new_cgrp->root) {
523 if (cg1 != new_cgrp)
524 return false;
525 } else {
526 if (cg1 != cg2)
527 return false;
528 }
529 }
530 return true;
531}
532
533/*
Paul Menage817929e2007-10-18 23:39:36 -0700534 * find_existing_css_set() is a helper for
535 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700536 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700537 *
538 * oldcg: the cgroup group that we're using before the cgroup
539 * transition
540 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700541 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700542 *
543 * template: location in which to build the desired set of subsystem
544 * state objects for the new cgroup group
545 */
Paul Menage817929e2007-10-18 23:39:36 -0700546static struct css_set *find_existing_css_set(
547 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700548 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700549 struct cgroup_subsys_state *template[])
550{
551 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700552 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700553 struct hlist_head *hhead;
554 struct hlist_node *node;
555 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700556
Ben Blumaae8aab2010-03-10 15:22:07 -0800557 /*
558 * Build the set of subsystem state objects that we want to see in the
559 * new css_set. while subsystems can change globally, the entries here
560 * won't change, so no need for locking.
561 */
Paul Menage817929e2007-10-18 23:39:36 -0700562 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400563 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700564 /* Subsystem is in this hierarchy. So we want
565 * the subsystem state from the new
566 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700567 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700568 } else {
569 /* Subsystem is not in this hierarchy, so we
570 * don't want to change the subsystem state */
571 template[i] = oldcg->subsys[i];
572 }
573 }
574
Li Zefan472b1052008-04-29 01:00:11 -0700575 hhead = css_set_hash(template);
576 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700577 if (!compare_css_sets(cg, oldcg, cgrp, template))
578 continue;
579
580 /* This css_set matches what we need */
581 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700582 }
Paul Menage817929e2007-10-18 23:39:36 -0700583
584 /* No existing cgroup group matched */
585 return NULL;
586}
587
Paul Menage817929e2007-10-18 23:39:36 -0700588static void free_cg_links(struct list_head *tmp)
589{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700590 struct cg_cgroup_link *link;
591 struct cg_cgroup_link *saved_link;
592
593 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700594 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700595 kfree(link);
596 }
597}
598
599/*
Li Zefan36553432008-07-29 22:33:19 -0700600 * allocate_cg_links() allocates "count" cg_cgroup_link structures
601 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
602 * success or a negative error
603 */
604static int allocate_cg_links(int count, struct list_head *tmp)
605{
606 struct cg_cgroup_link *link;
607 int i;
608 INIT_LIST_HEAD(tmp);
609 for (i = 0; i < count; i++) {
610 link = kmalloc(sizeof(*link), GFP_KERNEL);
611 if (!link) {
612 free_cg_links(tmp);
613 return -ENOMEM;
614 }
615 list_add(&link->cgrp_link_list, tmp);
616 }
617 return 0;
618}
619
Li Zefanc12f65d2009-01-07 18:07:42 -0800620/**
621 * link_css_set - a helper function to link a css_set to a cgroup
622 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
623 * @cg: the css_set to be linked
624 * @cgrp: the destination cgroup
625 */
626static void link_css_set(struct list_head *tmp_cg_links,
627 struct css_set *cg, struct cgroup *cgrp)
628{
629 struct cg_cgroup_link *link;
630
631 BUG_ON(list_empty(tmp_cg_links));
632 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
633 cgrp_link_list);
634 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700635 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700636 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800637 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700638 /*
639 * Always add links to the tail of the list so that the list
640 * is sorted by order of hierarchy creation
641 */
642 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800643}
644
Li Zefan36553432008-07-29 22:33:19 -0700645/*
Paul Menage817929e2007-10-18 23:39:36 -0700646 * find_css_set() takes an existing cgroup group and a
647 * cgroup object, and returns a css_set object that's
648 * equivalent to the old group, but with the given cgroup
649 * substituted into the appropriate hierarchy. Must be called with
650 * cgroup_mutex held
651 */
Paul Menage817929e2007-10-18 23:39:36 -0700652static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700653 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700654{
655 struct css_set *res;
656 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700657
658 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700659
Li Zefan472b1052008-04-29 01:00:11 -0700660 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700661 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700662
Paul Menage817929e2007-10-18 23:39:36 -0700663 /* First see if we already have a cgroup group that matches
664 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700665 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700666 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700667 if (res)
668 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700669 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700670
671 if (res)
672 return res;
673
674 res = kmalloc(sizeof(*res), GFP_KERNEL);
675 if (!res)
676 return NULL;
677
678 /* Allocate all the cg_cgroup_link objects that we'll need */
679 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
680 kfree(res);
681 return NULL;
682 }
683
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700684 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700685 INIT_LIST_HEAD(&res->cg_links);
686 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700687 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700688
689 /* Copy the set of subsystem state objects generated in
690 * find_existing_css_set() */
691 memcpy(res->subsys, template, sizeof(res->subsys));
692
693 write_lock(&css_set_lock);
694 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700695 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
696 struct cgroup *c = link->cgrp;
697 if (c->root == cgrp->root)
698 c = cgrp;
699 link_css_set(&tmp_cg_links, res, c);
700 }
Paul Menage817929e2007-10-18 23:39:36 -0700701
702 BUG_ON(!list_empty(&tmp_cg_links));
703
Paul Menage817929e2007-10-18 23:39:36 -0700704 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700705
706 /* Add this cgroup group to the hash table */
707 hhead = css_set_hash(res->subsys);
708 hlist_add_head(&res->hlist, hhead);
709
Paul Menage817929e2007-10-18 23:39:36 -0700710 write_unlock(&css_set_lock);
711
712 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700713}
714
Paul Menageddbcc7e2007-10-18 23:39:30 -0700715/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700716 * Return the cgroup for "task" from the given hierarchy. Must be
717 * called with cgroup_mutex held.
718 */
719static struct cgroup *task_cgroup_from_root(struct task_struct *task,
720 struct cgroupfs_root *root)
721{
722 struct css_set *css;
723 struct cgroup *res = NULL;
724
725 BUG_ON(!mutex_is_locked(&cgroup_mutex));
726 read_lock(&css_set_lock);
727 /*
728 * No need to lock the task - since we hold cgroup_mutex the
729 * task can't change groups, so the only thing that can happen
730 * is that it exits and its css is set back to init_css_set.
731 */
732 css = task->cgroups;
733 if (css == &init_css_set) {
734 res = &root->top_cgroup;
735 } else {
736 struct cg_cgroup_link *link;
737 list_for_each_entry(link, &css->cg_links, cg_link_list) {
738 struct cgroup *c = link->cgrp;
739 if (c->root == root) {
740 res = c;
741 break;
742 }
743 }
744 }
745 read_unlock(&css_set_lock);
746 BUG_ON(!res);
747 return res;
748}
749
750/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700751 * There is one global cgroup mutex. We also require taking
752 * task_lock() when dereferencing a task's cgroup subsys pointers.
753 * See "The task_lock() exception", at the end of this comment.
754 *
755 * A task must hold cgroup_mutex to modify cgroups.
756 *
757 * Any task can increment and decrement the count field without lock.
758 * So in general, code holding cgroup_mutex can't rely on the count
759 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800760 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700761 * means that no tasks are currently attached, therefore there is no
762 * way a task attached to that cgroup can fork (the other way to
763 * increment the count). So code holding cgroup_mutex can safely
764 * assume that if the count is zero, it will stay zero. Similarly, if
765 * a task holds cgroup_mutex on a cgroup with zero count, it
766 * knows that the cgroup won't be removed, as cgroup_rmdir()
767 * needs that mutex.
768 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700769 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
770 * (usually) take cgroup_mutex. These are the two most performance
771 * critical pieces of code here. The exception occurs on cgroup_exit(),
772 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
773 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800774 * to the release agent with the name of the cgroup (path relative to
775 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700776 *
777 * A cgroup can only be deleted if both its 'count' of using tasks
778 * is zero, and its list of 'children' cgroups is empty. Since all
779 * tasks in the system use _some_ cgroup, and since there is always at
780 * least one task in the system (init, pid == 1), therefore, top_cgroup
781 * always has either children cgroups and/or using tasks. So we don't
782 * need a special hack to ensure that top_cgroup cannot be deleted.
783 *
784 * The task_lock() exception
785 *
786 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800787 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800788 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700789 * several performance critical places that need to reference
790 * task->cgroup without the expense of grabbing a system global
791 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800792 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
794 * the task_struct routinely used for such matters.
795 *
796 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800797 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798 */
799
Paul Menageddbcc7e2007-10-18 23:39:30 -0700800/**
801 * cgroup_lock - lock out any changes to cgroup structures
802 *
803 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700804void cgroup_lock(void)
805{
806 mutex_lock(&cgroup_mutex);
807}
Ben Blum67523c42010-03-10 15:22:11 -0800808EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809
810/**
811 * cgroup_unlock - release lock on cgroup changes
812 *
813 * Undo the lock taken in a previous cgroup_lock() call.
814 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815void cgroup_unlock(void)
816{
817 mutex_unlock(&cgroup_mutex);
818}
Ben Blum67523c42010-03-10 15:22:11 -0800819EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700820
821/*
822 * A couple of forward declarations required, due to cyclic reference loop:
823 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
824 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
825 * -> cgroup_mkdir.
826 */
827
Al Viro18bb1db2011-07-26 01:41:39 -0400828static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400829static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700830static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400831static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
832 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700833static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700834static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700835
836static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200837 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700838 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700839};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700840
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700841static int alloc_css_id(struct cgroup_subsys *ss,
842 struct cgroup *parent, struct cgroup *child);
843
Al Viroa5e7ed32011-07-26 01:55:55 -0400844static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700845{
846 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700847
848 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400849 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700850 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100851 inode->i_uid = current_fsuid();
852 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700853 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
854 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
855 }
856 return inode;
857}
858
859static void cgroup_diput(struct dentry *dentry, struct inode *inode)
860{
861 /* is dentry a directory ? if so, kfree() associated cgroup */
862 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700863 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800864 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700865 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700866 /* It's possible for external users to be holding css
867 * reference counts on a cgroup; css_put() needs to
868 * be able to access the cgroup after decrementing
869 * the reference count in order to know if it needs to
870 * queue the cgroup to be handled by the release
871 * agent */
872 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800873
874 mutex_lock(&cgroup_mutex);
875 /*
876 * Release the subsystem state objects.
877 */
Li Zefan75139b82009-01-07 18:07:33 -0800878 for_each_subsys(cgrp->root, ss)
Tejun Heo92fb9742012-11-19 08:13:38 -0800879 ss->css_free(cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800880
881 cgrp->root->number_of_cgroups--;
882 mutex_unlock(&cgroup_mutex);
883
Paul Menagea47295e2009-01-07 18:07:44 -0800884 /*
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700885 * Drop the active superblock reference that we took when we
886 * created the cgroup
Paul Menagea47295e2009-01-07 18:07:44 -0800887 */
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700888 deactivate_super(cgrp->root->sb);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800889
Ben Blum72a8cb32009-09-23 15:56:27 -0700890 /*
891 * if we're getting rid of the cgroup, refcount should ensure
892 * that there are no pidlists left.
893 */
894 BUG_ON(!list_empty(&cgrp->pidlists));
895
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400896 simple_xattrs_free(&cgrp->xattrs);
897
Tejun Heo0a950f62012-11-19 09:02:12 -0800898 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
Lai Jiangshanf2da1c42011-03-15 17:55:16 +0800899 kfree_rcu(cgrp, rcu_head);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700900 } else {
901 struct cfent *cfe = __d_cfe(dentry);
902 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400903 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700904
905 WARN_ONCE(!list_empty(&cfe->node) &&
906 cgrp != &cgrp->root->top_cgroup,
907 "cfe still linked for %s\n", cfe->type->name);
908 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400909 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700910 }
911 iput(inode);
912}
913
Al Viroc72a04e2011-01-14 05:31:45 +0000914static int cgroup_delete(const struct dentry *d)
915{
916 return 1;
917}
918
Paul Menageddbcc7e2007-10-18 23:39:30 -0700919static void remove_dir(struct dentry *d)
920{
921 struct dentry *parent = dget(d->d_parent);
922
923 d_delete(d);
924 simple_rmdir(parent->d_inode, d);
925 dput(parent);
926}
927
Tejun Heo05ef1d72012-04-01 12:09:56 -0700928static int cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700929{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700930 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700931
Tejun Heo05ef1d72012-04-01 12:09:56 -0700932 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
933 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100934
Tejun Heo05ef1d72012-04-01 12:09:56 -0700935 list_for_each_entry(cfe, &cgrp->files, node) {
936 struct dentry *d = cfe->dentry;
937
938 if (cft && cfe->type != cft)
939 continue;
940
941 dget(d);
942 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700943 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700944 list_del_init(&cfe->node);
945 dput(d);
946
947 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700948 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700949 return -ENOENT;
950}
951
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400952/**
953 * cgroup_clear_directory - selective removal of base and subsystem files
954 * @dir: directory containing the files
955 * @base_files: true if the base files should be removed
956 * @subsys_mask: mask of the subsystem ids whose files should be removed
957 */
958static void cgroup_clear_directory(struct dentry *dir, bool base_files,
959 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700960{
961 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400962 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700963
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400964 for_each_subsys(cgrp->root, ss) {
965 struct cftype_set *set;
966 if (!test_bit(ss->subsys_id, &subsys_mask))
967 continue;
968 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800969 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400970 }
971 if (base_files) {
972 while (!list_empty(&cgrp->files))
973 cgroup_rm_file(cgrp, NULL);
974 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700975}
976
977/*
978 * NOTE : the dentry must have been dget()'ed
979 */
980static void cgroup_d_remove_dir(struct dentry *dentry)
981{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100982 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400983 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100984
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400985 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100987 parent = dentry->d_parent;
988 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800989 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100991 spin_unlock(&dentry->d_lock);
992 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993 remove_dir(dentry);
994}
995
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700996/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800997 * Call with cgroup_mutex held. Drops reference counts on modules, including
998 * any duplicate ones that parse_cgroupfs_options took. If this function
999 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001000 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001002 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001004 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001005 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006 int i;
1007
Ben Blumaae8aab2010-03-10 15:22:07 -08001008 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001009 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001010
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001011 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1012 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 /* Check that any added subsystems are currently free */
1014 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001015 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001017 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001019 /*
1020 * Nobody should tell us to do a subsys that doesn't exist:
1021 * parse_cgroupfs_options should catch that case and refcounts
1022 * ensure that subsystems won't disappear once selected.
1023 */
1024 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025 if (ss->root != &rootnode) {
1026 /* Subsystem isn't free */
1027 return -EBUSY;
1028 }
1029 }
1030
1031 /* Currently we don't handle adding/removing subsystems when
1032 * any child cgroups exist. This is theoretically supportable
1033 * but involves complex error handling, so it's being left until
1034 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001035 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 return -EBUSY;
1037
1038 /* Process each subsystem */
1039 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1040 struct cgroup_subsys *ss = subsys[i];
1041 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001042 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001044 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001045 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046 BUG_ON(!dummytop->subsys[i]);
1047 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001048 cgrp->subsys[i] = dummytop->subsys[i];
1049 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001050 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001051 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001053 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001054 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001055 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001057 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001058 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1059 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001061 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001062 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001063 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001064 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001065 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001066 /* subsystem is now free - drop reference on module */
1067 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001068 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001069 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001070 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001071 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001072 /*
1073 * a refcount was taken, but we already had one, so
1074 * drop the extra reference.
1075 */
1076 module_put(ss->module);
1077#ifdef CONFIG_MODULE_UNLOAD
1078 BUG_ON(ss->module && !module_refcount(ss->module));
1079#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080 } else {
1081 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001082 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 }
1084 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001085 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086 synchronize_rcu();
1087
1088 return 0;
1089}
1090
Al Viro34c80b12011-12-08 21:32:45 -05001091static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092{
Al Viro34c80b12011-12-08 21:32:45 -05001093 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 struct cgroup_subsys *ss;
1095
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001096 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097 for_each_subsys(root, ss)
1098 seq_printf(seq, ",%s", ss->name);
1099 if (test_bit(ROOT_NOPREFIX, &root->flags))
1100 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001101 if (test_bit(ROOT_XATTR, &root->flags))
1102 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001103 if (strlen(root->release_agent_path))
1104 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001105 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001106 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001107 if (strlen(root->name))
1108 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001109 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110 return 0;
1111}
1112
1113struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001114 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001115 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001116 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001117 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001118 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001119 /* User explicitly requested empty subsystem */
1120 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001121
1122 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001123
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124};
1125
Ben Blumaae8aab2010-03-10 15:22:07 -08001126/*
1127 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001128 * with cgroup_mutex held to protect the subsys[] array. This function takes
1129 * refcounts on subsystems to be used, unless it returns error, in which case
1130 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001131 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001132static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001134 char *token, *o = data;
1135 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001136 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001137 int i;
1138 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001139
Ben Blumaae8aab2010-03-10 15:22:07 -08001140 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1141
Li Zefanf9ab5b52009-06-17 16:26:33 -07001142#ifdef CONFIG_CPUSETS
1143 mask = ~(1UL << cpuset_subsys_id);
1144#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001145
Paul Menagec6d57f32009-09-23 15:56:19 -07001146 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001147
1148 while ((token = strsep(&o, ",")) != NULL) {
1149 if (!*token)
1150 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001151 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001152 /* Explicitly have no subsystems */
1153 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001154 continue;
1155 }
1156 if (!strcmp(token, "all")) {
1157 /* Mutually exclusive option 'all' + subsystem name */
1158 if (one_ss)
1159 return -EINVAL;
1160 all_ss = true;
1161 continue;
1162 }
1163 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001164 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001165 continue;
1166 }
1167 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001168 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001169 continue;
1170 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001171 if (!strcmp(token, "xattr")) {
1172 set_bit(ROOT_XATTR, &opts->flags);
1173 continue;
1174 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001175 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001176 /* Specifying two release agents is forbidden */
1177 if (opts->release_agent)
1178 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001179 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001180 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001181 if (!opts->release_agent)
1182 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001183 continue;
1184 }
1185 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001186 const char *name = token + 5;
1187 /* Can't specify an empty name */
1188 if (!strlen(name))
1189 return -EINVAL;
1190 /* Must match [\w.-]+ */
1191 for (i = 0; i < strlen(name); i++) {
1192 char c = name[i];
1193 if (isalnum(c))
1194 continue;
1195 if ((c == '.') || (c == '-') || (c == '_'))
1196 continue;
1197 return -EINVAL;
1198 }
1199 /* Specifying two names is forbidden */
1200 if (opts->name)
1201 return -EINVAL;
1202 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001203 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001204 GFP_KERNEL);
1205 if (!opts->name)
1206 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001207
1208 continue;
1209 }
1210
1211 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1212 struct cgroup_subsys *ss = subsys[i];
1213 if (ss == NULL)
1214 continue;
1215 if (strcmp(token, ss->name))
1216 continue;
1217 if (ss->disabled)
1218 continue;
1219
1220 /* Mutually exclusive option 'all' + subsystem name */
1221 if (all_ss)
1222 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001223 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001224 one_ss = true;
1225
1226 break;
1227 }
1228 if (i == CGROUP_SUBSYS_COUNT)
1229 return -ENOENT;
1230 }
1231
1232 /*
1233 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001234 * otherwise if 'none', 'name=' and a subsystem name options
1235 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001236 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001237 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001238 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1239 struct cgroup_subsys *ss = subsys[i];
1240 if (ss == NULL)
1241 continue;
1242 if (ss->disabled)
1243 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001244 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001245 }
1246 }
1247
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001248 /* Consistency checks */
1249
Li Zefanf9ab5b52009-06-17 16:26:33 -07001250 /*
1251 * Option noprefix was introduced just for backward compatibility
1252 * with the old cpuset, so we allow noprefix only if mounting just
1253 * the cpuset subsystem.
1254 */
1255 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001256 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001257 return -EINVAL;
1258
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001259
1260 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001261 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001262 return -EINVAL;
1263
1264 /*
1265 * We either have to specify by name or by subsystems. (So all
1266 * empty hierarchies must have a name).
1267 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001268 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001269 return -EINVAL;
1270
Ben Blumcf5d5942010-03-10 15:22:09 -08001271 /*
1272 * Grab references on all the modules we'll need, so the subsystems
1273 * don't dance around before rebind_subsystems attaches them. This may
1274 * take duplicate reference counts on a subsystem that's already used,
1275 * but rebind_subsystems handles this case.
1276 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001277 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001278 unsigned long bit = 1UL << i;
1279
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001280 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001281 continue;
1282 if (!try_module_get(subsys[i]->module)) {
1283 module_pin_failed = true;
1284 break;
1285 }
1286 }
1287 if (module_pin_failed) {
1288 /*
1289 * oops, one of the modules was going away. this means that we
1290 * raced with a module_delete call, and to the user this is
1291 * essentially a "subsystem doesn't exist" case.
1292 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001293 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001294 /* drop refcounts only on the ones we took */
1295 unsigned long bit = 1UL << i;
1296
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001297 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001298 continue;
1299 module_put(subsys[i]->module);
1300 }
1301 return -ENOENT;
1302 }
1303
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304 return 0;
1305}
1306
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001307static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001308{
1309 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001310 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001311 unsigned long bit = 1UL << i;
1312
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001313 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001314 continue;
1315 module_put(subsys[i]->module);
1316 }
1317}
1318
Paul Menageddbcc7e2007-10-18 23:39:30 -07001319static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1320{
1321 int ret = 0;
1322 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001323 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001325 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326
Paul Menagebd89aab2007-10-18 23:40:44 -07001327 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001329 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330
1331 /* See what subsystems are wanted */
1332 ret = parse_cgroupfs_options(data, &opts);
1333 if (ret)
1334 goto out_unlock;
1335
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001336 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001337 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1338 task_tgid_nr(current), current->comm);
1339
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001340 added_mask = opts.subsys_mask & ~root->subsys_mask;
1341 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001342
Ben Blumcf5d5942010-03-10 15:22:09 -08001343 /* Don't allow flags or name to change at remount */
1344 if (opts.flags != root->flags ||
1345 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001347 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001348 goto out_unlock;
1349 }
1350
Gao feng7083d032012-12-03 09:28:18 +08001351 /*
1352 * Clear out the files of subsystems that should be removed, do
1353 * this before rebind_subsystems, since rebind_subsystems may
1354 * change this hierarchy's subsys_list.
1355 */
1356 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1357
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001358 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001359 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001360 /* rebind_subsystems failed, re-populate the removed files */
1361 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001362 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001363 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001364 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001366 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001367 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368
Paul Menage81a6a5c2007-10-18 23:39:38 -07001369 if (opts.release_agent)
1370 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001372 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001373 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001374 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001375 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001376 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001377 return ret;
1378}
1379
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001380static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001381 .statfs = simple_statfs,
1382 .drop_inode = generic_delete_inode,
1383 .show_options = cgroup_show_options,
1384 .remount_fs = cgroup_remount,
1385};
1386
Paul Menagecc31edc2008-10-18 20:28:04 -07001387static void init_cgroup_housekeeping(struct cgroup *cgrp)
1388{
1389 INIT_LIST_HEAD(&cgrp->sibling);
1390 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001391 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001392 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001393 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001394 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001395 INIT_LIST_HEAD(&cgrp->pidlists);
1396 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001397 INIT_LIST_HEAD(&cgrp->event_list);
1398 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001399 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001400}
Paul Menagec6d57f32009-09-23 15:56:19 -07001401
Paul Menageddbcc7e2007-10-18 23:39:30 -07001402static void init_cgroup_root(struct cgroupfs_root *root)
1403{
Paul Menagebd89aab2007-10-18 23:40:44 -07001404 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001405
Paul Menageddbcc7e2007-10-18 23:39:30 -07001406 INIT_LIST_HEAD(&root->subsys_list);
1407 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001408 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001409 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001410 cgrp->root = root;
1411 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001412 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001413 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414}
1415
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001416static bool init_root_id(struct cgroupfs_root *root)
1417{
1418 int ret = 0;
1419
1420 do {
1421 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1422 return false;
1423 spin_lock(&hierarchy_id_lock);
1424 /* Try to allocate the next unused ID */
1425 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1426 &root->hierarchy_id);
1427 if (ret == -ENOSPC)
1428 /* Try again starting from 0 */
1429 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1430 if (!ret) {
1431 next_hierarchy_id = root->hierarchy_id + 1;
1432 } else if (ret != -EAGAIN) {
1433 /* Can only get here if the 31-bit IDR is full ... */
1434 BUG_ON(ret);
1435 }
1436 spin_unlock(&hierarchy_id_lock);
1437 } while (ret);
1438 return true;
1439}
1440
Paul Menageddbcc7e2007-10-18 23:39:30 -07001441static int cgroup_test_super(struct super_block *sb, void *data)
1442{
Paul Menagec6d57f32009-09-23 15:56:19 -07001443 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001444 struct cgroupfs_root *root = sb->s_fs_info;
1445
Paul Menagec6d57f32009-09-23 15:56:19 -07001446 /* If we asked for a name then it must match */
1447 if (opts->name && strcmp(opts->name, root->name))
1448 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001449
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001450 /*
1451 * If we asked for subsystems (or explicitly for no
1452 * subsystems) then they must match
1453 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001454 if ((opts->subsys_mask || opts->none)
1455 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001456 return 0;
1457
1458 return 1;
1459}
1460
Paul Menagec6d57f32009-09-23 15:56:19 -07001461static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1462{
1463 struct cgroupfs_root *root;
1464
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001465 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001466 return NULL;
1467
1468 root = kzalloc(sizeof(*root), GFP_KERNEL);
1469 if (!root)
1470 return ERR_PTR(-ENOMEM);
1471
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001472 if (!init_root_id(root)) {
1473 kfree(root);
1474 return ERR_PTR(-ENOMEM);
1475 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001476 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001477
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001478 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001479 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001480 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001481 if (opts->release_agent)
1482 strcpy(root->release_agent_path, opts->release_agent);
1483 if (opts->name)
1484 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001485 if (opts->cpuset_clone_children)
1486 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001487 return root;
1488}
1489
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001490static void cgroup_drop_root(struct cgroupfs_root *root)
1491{
1492 if (!root)
1493 return;
1494
1495 BUG_ON(!root->hierarchy_id);
1496 spin_lock(&hierarchy_id_lock);
1497 ida_remove(&hierarchy_ida, root->hierarchy_id);
1498 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001499 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001500 kfree(root);
1501}
1502
Paul Menageddbcc7e2007-10-18 23:39:30 -07001503static int cgroup_set_super(struct super_block *sb, void *data)
1504{
1505 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001506 struct cgroup_sb_opts *opts = data;
1507
1508 /* If we don't have a new root, we can't set up a new sb */
1509 if (!opts->new_root)
1510 return -EINVAL;
1511
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001512 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513
1514 ret = set_anon_super(sb, NULL);
1515 if (ret)
1516 return ret;
1517
Paul Menagec6d57f32009-09-23 15:56:19 -07001518 sb->s_fs_info = opts->new_root;
1519 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520
1521 sb->s_blocksize = PAGE_CACHE_SIZE;
1522 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1523 sb->s_magic = CGROUP_SUPER_MAGIC;
1524 sb->s_op = &cgroup_ops;
1525
1526 return 0;
1527}
1528
1529static int cgroup_get_rootdir(struct super_block *sb)
1530{
Al Viro0df6a632010-12-21 13:29:29 -05001531 static const struct dentry_operations cgroup_dops = {
1532 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001533 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001534 };
1535
Paul Menageddbcc7e2007-10-18 23:39:30 -07001536 struct inode *inode =
1537 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001538
1539 if (!inode)
1540 return -ENOMEM;
1541
Paul Menageddbcc7e2007-10-18 23:39:30 -07001542 inode->i_fop = &simple_dir_operations;
1543 inode->i_op = &cgroup_dir_inode_operations;
1544 /* directories start off with i_nlink == 2 (for "." entry) */
1545 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001546 sb->s_root = d_make_root(inode);
1547 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001549 /* for everything else we want ->d_op set */
1550 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551 return 0;
1552}
1553
Al Virof7e83572010-07-26 13:23:11 +04001554static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001556 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557{
1558 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001559 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 int ret = 0;
1561 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001562 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001563 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564
1565 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001566 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001568 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001569 if (ret)
1570 goto out_err;
1571
1572 /*
1573 * Allocate a new cgroup root. We may not need it if we're
1574 * reusing an existing hierarchy.
1575 */
1576 new_root = cgroup_root_from_opts(&opts);
1577 if (IS_ERR(new_root)) {
1578 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001579 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001580 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001581 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582
Paul Menagec6d57f32009-09-23 15:56:19 -07001583 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001584 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001585 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001586 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001587 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001588 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001589 }
1590
Paul Menagec6d57f32009-09-23 15:56:19 -07001591 root = sb->s_fs_info;
1592 BUG_ON(!root);
1593 if (root == opts.new_root) {
1594 /* We used the new root structure, so this is a new hierarchy */
1595 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001596 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001597 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001598 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001599 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600
1601 BUG_ON(sb->s_root != NULL);
1602
1603 ret = cgroup_get_rootdir(sb);
1604 if (ret)
1605 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001606 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607
Paul Menage817929e2007-10-18 23:39:36 -07001608 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001610 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001611
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001612 /* Check for name clashes with existing mounts */
1613 ret = -EBUSY;
1614 if (strlen(root->name))
1615 for_each_active_root(existing_root)
1616 if (!strcmp(existing_root->name, root->name))
1617 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001618
Paul Menage817929e2007-10-18 23:39:36 -07001619 /*
1620 * We're accessing css_set_count without locking
1621 * css_set_lock here, but that's OK - it can only be
1622 * increased by someone holding cgroup_lock, and
1623 * that's us. The worst that can happen is that we
1624 * have some link structures left over
1625 */
1626 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001627 if (ret)
1628 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001629
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001630 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001632 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001633 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001634 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001635 /*
1636 * There must be no failure case after here, since rebinding
1637 * takes care of subsystems' refcounts, which are explicitly
1638 * dropped in the failure exit path.
1639 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001640
1641 /* EBUSY should be the only error here */
1642 BUG_ON(ret);
1643
1644 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001645 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001646
Li Zefanc12f65d2009-01-07 18:07:42 -08001647 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001648 root->top_cgroup.dentry = sb->s_root;
1649
Paul Menage817929e2007-10-18 23:39:36 -07001650 /* Link the top cgroup in this hierarchy into all
1651 * the css_set objects */
1652 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001653 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1654 struct hlist_head *hhead = &css_set_table[i];
1655 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001656 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001657
Li Zefanc12f65d2009-01-07 18:07:42 -08001658 hlist_for_each_entry(cg, node, hhead, hlist)
1659 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001660 }
Paul Menage817929e2007-10-18 23:39:36 -07001661 write_unlock(&css_set_lock);
1662
1663 free_cg_links(&tmp_cg_links);
1664
Li Zefanc12f65d2009-01-07 18:07:42 -08001665 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001666 BUG_ON(root->number_of_cgroups != 1);
1667
eparis@redhat2ce97382011-06-02 21:20:51 +10001668 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001669 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001670 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001671 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001673 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001674 } else {
1675 /*
1676 * We re-used an existing hierarchy - the new root (if
1677 * any) is not needed
1678 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001679 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001680 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001681 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682 }
1683
Paul Menagec6d57f32009-09-23 15:56:19 -07001684 kfree(opts.release_agent);
1685 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001686 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001687
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001688 unlock_drop:
1689 mutex_unlock(&cgroup_root_mutex);
1690 mutex_unlock(&cgroup_mutex);
1691 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001692 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001693 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001694 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001695 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001696 out_err:
1697 kfree(opts.release_agent);
1698 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001699 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700}
1701
1702static void cgroup_kill_sb(struct super_block *sb) {
1703 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001704 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001706 struct cg_cgroup_link *link;
1707 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708
1709 BUG_ON(!root);
1710
1711 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001712 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001713
1714 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001715 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001716
1717 /* Rebind all subsystems back to the default hierarchy */
1718 ret = rebind_subsystems(root, 0);
1719 /* Shouldn't be able to fail ... */
1720 BUG_ON(ret);
1721
Paul Menage817929e2007-10-18 23:39:36 -07001722 /*
1723 * Release all the links from css_sets to this hierarchy's
1724 * root cgroup
1725 */
1726 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001727
1728 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1729 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001730 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001731 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001732 kfree(link);
1733 }
1734 write_unlock(&css_set_lock);
1735
Paul Menage839ec542009-01-29 14:25:22 -08001736 if (!list_empty(&root->root_list)) {
1737 list_del(&root->root_list);
1738 root_count--;
1739 }
Li Zefane5f6a862009-01-07 18:07:41 -08001740
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001741 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001742 mutex_unlock(&cgroup_mutex);
1743
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001744 simple_xattrs_free(&cgrp->xattrs);
1745
Paul Menageddbcc7e2007-10-18 23:39:30 -07001746 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001747 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001748}
1749
1750static struct file_system_type cgroup_fs_type = {
1751 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001752 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753 .kill_sb = cgroup_kill_sb,
1754};
1755
Greg KH676db4a2010-08-05 13:53:35 -07001756static struct kobject *cgroup_kobj;
1757
Li Zefana043e3b2008-02-23 15:24:09 -08001758/**
1759 * cgroup_path - generate the path of a cgroup
1760 * @cgrp: the cgroup in question
1761 * @buf: the buffer to write the path into
1762 * @buflen: the length of the buffer
1763 *
Paul Menagea47295e2009-01-07 18:07:44 -08001764 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1765 * reference. Writes path of cgroup into buf. Returns 0 on success,
1766 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001768int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769{
Tejun Heofebfcef2012-11-19 08:13:36 -08001770 struct dentry *dentry = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001771 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001772
1773 rcu_lockdep_assert(rcu_read_lock_held() || cgroup_lock_is_held(),
1774 "cgroup_path() called without proper locking");
Paul Menageddbcc7e2007-10-18 23:39:30 -07001775
Paul Menagea47295e2009-01-07 18:07:44 -08001776 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777 /*
1778 * Inactive subsystems have no dentry for their root
1779 * cgroup
1780 */
1781 strcpy(buf, "/");
1782 return 0;
1783 }
1784
Tao Ma316eb662012-11-08 21:36:38 +08001785 start = buf + buflen - 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786
Tao Ma316eb662012-11-08 21:36:38 +08001787 *start = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07001788 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001789 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001790
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791 if ((start -= len) < buf)
1792 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001793 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001794 cgrp = cgrp->parent;
1795 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001796 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001797
Tejun Heofebfcef2012-11-19 08:13:36 -08001798 dentry = cgrp->dentry;
Paul Menagebd89aab2007-10-18 23:40:44 -07001799 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001800 continue;
1801 if (--start < buf)
1802 return -ENAMETOOLONG;
1803 *start = '/';
1804 }
1805 memmove(buf, start, buf + buflen - start);
1806 return 0;
1807}
Ben Blum67523c42010-03-10 15:22:11 -08001808EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001809
Ben Blum74a11662011-05-26 16:25:20 -07001810/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001811 * Control Group taskset
1812 */
Tejun Heo134d3372011-12-12 18:12:21 -08001813struct task_and_cgroup {
1814 struct task_struct *task;
1815 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001816 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001817};
1818
Tejun Heo2f7ee562011-12-12 18:12:21 -08001819struct cgroup_taskset {
1820 struct task_and_cgroup single;
1821 struct flex_array *tc_array;
1822 int tc_array_len;
1823 int idx;
1824 struct cgroup *cur_cgrp;
1825};
1826
1827/**
1828 * cgroup_taskset_first - reset taskset and return the first task
1829 * @tset: taskset of interest
1830 *
1831 * @tset iteration is initialized and the first task is returned.
1832 */
1833struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1834{
1835 if (tset->tc_array) {
1836 tset->idx = 0;
1837 return cgroup_taskset_next(tset);
1838 } else {
1839 tset->cur_cgrp = tset->single.cgrp;
1840 return tset->single.task;
1841 }
1842}
1843EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1844
1845/**
1846 * cgroup_taskset_next - iterate to the next task in taskset
1847 * @tset: taskset of interest
1848 *
1849 * Return the next task in @tset. Iteration must have been initialized
1850 * with cgroup_taskset_first().
1851 */
1852struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1853{
1854 struct task_and_cgroup *tc;
1855
1856 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1857 return NULL;
1858
1859 tc = flex_array_get(tset->tc_array, tset->idx++);
1860 tset->cur_cgrp = tc->cgrp;
1861 return tc->task;
1862}
1863EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1864
1865/**
1866 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1867 * @tset: taskset of interest
1868 *
1869 * Return the cgroup for the current (last returned) task of @tset. This
1870 * function must be preceded by either cgroup_taskset_first() or
1871 * cgroup_taskset_next().
1872 */
1873struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1874{
1875 return tset->cur_cgrp;
1876}
1877EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1878
1879/**
1880 * cgroup_taskset_size - return the number of tasks in taskset
1881 * @tset: taskset of interest
1882 */
1883int cgroup_taskset_size(struct cgroup_taskset *tset)
1884{
1885 return tset->tc_array ? tset->tc_array_len : 1;
1886}
1887EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1888
1889
Ben Blum74a11662011-05-26 16:25:20 -07001890/*
1891 * cgroup_task_migrate - move a task from one cgroup to another.
1892 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001893 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001894 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001895static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1896 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001897{
1898 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001899
1900 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001901 * We are synchronized through threadgroup_lock() against PF_EXITING
1902 * setting such that we can't race against cgroup_exit() changing the
1903 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001904 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001905 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001906 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001907
Ben Blum74a11662011-05-26 16:25:20 -07001908 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001909 rcu_assign_pointer(tsk->cgroups, newcg);
1910 task_unlock(tsk);
1911
1912 /* Update the css_set linked lists if we're using them */
1913 write_lock(&css_set_lock);
1914 if (!list_empty(&tsk->cg_list))
1915 list_move(&tsk->cg_list, &newcg->tasks);
1916 write_unlock(&css_set_lock);
1917
1918 /*
1919 * We just gained a reference on oldcg by taking it from the task. As
1920 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1921 * it here; it will be freed under RCU.
1922 */
Ben Blum74a11662011-05-26 16:25:20 -07001923 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001924 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001925}
1926
Li Zefana043e3b2008-02-23 15:24:09 -08001927/**
1928 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1929 * @cgrp: the cgroup the task is attaching to
1930 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001931 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001932 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1933 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001934 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001935int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001936{
Tejun Heo8f121912012-03-29 22:03:33 -07001937 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001938 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001939 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001940 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001941 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001942 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001943
Tejun Heocd3d0952011-12-12 18:12:21 -08001944 /* @tsk either already exited or can't exit until the end */
1945 if (tsk->flags & PF_EXITING)
1946 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001947
1948 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001949 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001950 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001951 return 0;
1952
Tejun Heo2f7ee562011-12-12 18:12:21 -08001953 tset.single.task = tsk;
1954 tset.single.cgrp = oldcgrp;
1955
Paul Menagebbcb81d2007-10-18 23:39:32 -07001956 for_each_subsys(root, ss) {
1957 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08001958 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001959 if (retval) {
1960 /*
1961 * Remember on which subsystem the can_attach()
1962 * failed, so that we only call cancel_attach()
1963 * against the subsystems whose can_attach()
1964 * succeeded. (See below)
1965 */
1966 failed_ss = ss;
1967 goto out;
1968 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001969 }
1970 }
1971
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001972 newcg = find_css_set(tsk->cgroups, cgrp);
1973 if (!newcg) {
1974 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001975 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001976 }
1977
1978 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001979
Paul Menagebbcb81d2007-10-18 23:39:32 -07001980 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001981 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001982 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001983 }
Ben Blum74a11662011-05-26 16:25:20 -07001984
Paul Menagebbcb81d2007-10-18 23:39:32 -07001985 synchronize_rcu();
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001986out:
1987 if (retval) {
1988 for_each_subsys(root, ss) {
1989 if (ss == failed_ss)
1990 /*
1991 * This subsystem was the one that failed the
1992 * can_attach() check earlier, so we don't need
1993 * to call cancel_attach() against it or any
1994 * remaining subsystems.
1995 */
1996 break;
1997 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001998 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001999 }
2000 }
2001 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002002}
2003
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002004/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002005 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2006 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002007 * @tsk: the task to be attached
2008 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002009int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002010{
2011 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002012 int retval = 0;
2013
2014 cgroup_lock();
2015 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002016 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2017
2018 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002019 if (retval)
2020 break;
2021 }
2022 cgroup_unlock();
2023
2024 return retval;
2025}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002026EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002027
Ben Blum74a11662011-05-26 16:25:20 -07002028/**
2029 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2030 * @cgrp: the cgroup to attach to
2031 * @leader: the threadgroup leader task_struct of the group to be attached
2032 *
Tejun Heo257058a2011-12-12 18:12:21 -08002033 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2034 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002035 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002036static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002037{
2038 int retval, i, group_size;
2039 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002040 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002041 struct cgroupfs_root *root = cgrp->root;
2042 /* threadgroup list cursor and array */
2043 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002044 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002045 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002046 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002047
2048 /*
2049 * step 0: in order to do expensive, possibly blocking operations for
2050 * every thread, we cannot iterate the thread group list, since it needs
2051 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002052 * group - group_rwsem prevents new threads from appearing, and if
2053 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002054 */
2055 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002056 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002057 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002058 if (!group)
2059 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002060 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2061 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2062 if (retval)
2063 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002064
Ben Blum74a11662011-05-26 16:25:20 -07002065 tsk = leader;
2066 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002067 /*
2068 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2069 * already PF_EXITING could be freed from underneath us unless we
2070 * take an rcu_read_lock.
2071 */
2072 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002073 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002074 struct task_and_cgroup ent;
2075
Tejun Heocd3d0952011-12-12 18:12:21 -08002076 /* @tsk either already exited or can't exit until the end */
2077 if (tsk->flags & PF_EXITING)
2078 continue;
2079
Ben Blum74a11662011-05-26 16:25:20 -07002080 /* as per above, nr_threads may decrease, but not increase. */
2081 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002082 ent.task = tsk;
2083 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002084 /* nothing to do if this task is already in the cgroup */
2085 if (ent.cgrp == cgrp)
2086 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002087 /*
2088 * saying GFP_ATOMIC has no effect here because we did prealloc
2089 * earlier, but it's good form to communicate our expectations.
2090 */
Tejun Heo134d3372011-12-12 18:12:21 -08002091 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002092 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002093 i++;
2094 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002095 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002096 /* remember the number of threads in the array for later. */
2097 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002098 tset.tc_array = group;
2099 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002100
Tejun Heo134d3372011-12-12 18:12:21 -08002101 /* methods shouldn't be called if no task is actually migrating */
2102 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002103 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002104 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002105
Ben Blum74a11662011-05-26 16:25:20 -07002106 /*
2107 * step 1: check that we can legitimately attach to the cgroup.
2108 */
2109 for_each_subsys(root, ss) {
2110 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002111 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002112 if (retval) {
2113 failed_ss = ss;
2114 goto out_cancel_attach;
2115 }
2116 }
Ben Blum74a11662011-05-26 16:25:20 -07002117 }
2118
2119 /*
2120 * step 2: make sure css_sets exist for all threads to be migrated.
2121 * we use find_css_set, which allocates a new one if necessary.
2122 */
Ben Blum74a11662011-05-26 16:25:20 -07002123 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002124 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002125 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2126 if (!tc->cg) {
2127 retval = -ENOMEM;
2128 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002129 }
2130 }
2131
2132 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002133 * step 3: now that we're guaranteed success wrt the css_sets,
2134 * proceed to move all tasks to the new cgroup. There are no
2135 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002136 */
Ben Blum74a11662011-05-26 16:25:20 -07002137 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002138 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002139 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002140 }
2141 /* nothing is sensitive to fork() after this point. */
2142
2143 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002144 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002145 */
2146 for_each_subsys(root, ss) {
2147 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002148 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002149 }
2150
2151 /*
2152 * step 5: success! and cleanup
2153 */
2154 synchronize_rcu();
Ben Blum74a11662011-05-26 16:25:20 -07002155 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002156out_put_css_set_refs:
2157 if (retval) {
2158 for (i = 0; i < group_size; i++) {
2159 tc = flex_array_get(group, i);
2160 if (!tc->cg)
2161 break;
2162 put_css_set(tc->cg);
2163 }
Ben Blum74a11662011-05-26 16:25:20 -07002164 }
2165out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002166 if (retval) {
2167 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002168 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002169 break;
Ben Blum74a11662011-05-26 16:25:20 -07002170 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002171 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002172 }
2173 }
Ben Blum74a11662011-05-26 16:25:20 -07002174out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002175 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002176 return retval;
2177}
2178
2179/*
2180 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002181 * function to attach either it or all tasks in its threadgroup. Will lock
2182 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002183 */
2184static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002185{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002186 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002187 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002188 int ret;
2189
Ben Blum74a11662011-05-26 16:25:20 -07002190 if (!cgroup_lock_live_group(cgrp))
2191 return -ENODEV;
2192
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002193retry_find_task:
2194 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002195 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002196 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002197 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002198 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002199 ret= -ESRCH;
2200 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002201 }
Ben Blum74a11662011-05-26 16:25:20 -07002202 /*
2203 * even if we're attaching all tasks in the thread group, we
2204 * only need to check permissions on one of them.
2205 */
David Howellsc69e8d92008-11-14 10:39:19 +11002206 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002207 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2208 !uid_eq(cred->euid, tcred->uid) &&
2209 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002210 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002211 ret = -EACCES;
2212 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002213 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002214 } else
2215 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002216
2217 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002218 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002219
2220 /*
2221 * Workqueue threads may acquire PF_THREAD_BOUND and become
2222 * trapped in a cpuset, or RT worker may be born in a cgroup
2223 * with no rt_runtime allocated. Just say no.
2224 */
2225 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2226 ret = -EINVAL;
2227 rcu_read_unlock();
2228 goto out_unlock_cgroup;
2229 }
2230
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002231 get_task_struct(tsk);
2232 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002233
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002234 threadgroup_lock(tsk);
2235 if (threadgroup) {
2236 if (!thread_group_leader(tsk)) {
2237 /*
2238 * a race with de_thread from another thread's exec()
2239 * may strip us of our leadership, if this happens,
2240 * there is no choice but to throw this task away and
2241 * try again; this is
2242 * "double-double-toil-and-trouble-check locking".
2243 */
2244 threadgroup_unlock(tsk);
2245 put_task_struct(tsk);
2246 goto retry_find_task;
2247 }
2248 ret = cgroup_attach_proc(cgrp, tsk);
2249 } else
2250 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002251 threadgroup_unlock(tsk);
2252
Paul Menagebbcb81d2007-10-18 23:39:32 -07002253 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002254out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002255 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002256 return ret;
2257}
2258
Paul Menageaf351022008-07-25 01:47:01 -07002259static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2260{
Ben Blum74a11662011-05-26 16:25:20 -07002261 return attach_task_by_pid(cgrp, pid, false);
2262}
2263
2264static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2265{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002266 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002267}
2268
Paul Menagee788e062008-07-25 01:46:59 -07002269/**
2270 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2271 * @cgrp: the cgroup to be checked for liveness
2272 *
Paul Menage84eea842008-07-25 01:47:00 -07002273 * On success, returns true; the lock should be later released with
2274 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002275 */
Paul Menage84eea842008-07-25 01:47:00 -07002276bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002277{
2278 mutex_lock(&cgroup_mutex);
2279 if (cgroup_is_removed(cgrp)) {
2280 mutex_unlock(&cgroup_mutex);
2281 return false;
2282 }
2283 return true;
2284}
Ben Blum67523c42010-03-10 15:22:11 -08002285EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002286
2287static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2288 const char *buffer)
2289{
2290 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002291 if (strlen(buffer) >= PATH_MAX)
2292 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002293 if (!cgroup_lock_live_group(cgrp))
2294 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002295 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002296 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002297 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002298 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002299 return 0;
2300}
2301
2302static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2303 struct seq_file *seq)
2304{
2305 if (!cgroup_lock_live_group(cgrp))
2306 return -ENODEV;
2307 seq_puts(seq, cgrp->root->release_agent_path);
2308 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002309 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002310 return 0;
2311}
2312
Paul Menage84eea842008-07-25 01:47:00 -07002313/* A buffer size big enough for numbers or short strings */
2314#define CGROUP_LOCAL_BUFFER_SIZE 64
2315
Paul Menagee73d2c62008-04-29 01:00:06 -07002316static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002317 struct file *file,
2318 const char __user *userbuf,
2319 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002320{
Paul Menage84eea842008-07-25 01:47:00 -07002321 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002322 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002323 char *end;
2324
2325 if (!nbytes)
2326 return -EINVAL;
2327 if (nbytes >= sizeof(buffer))
2328 return -E2BIG;
2329 if (copy_from_user(buffer, userbuf, nbytes))
2330 return -EFAULT;
2331
2332 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002333 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002334 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002335 if (*end)
2336 return -EINVAL;
2337 retval = cft->write_u64(cgrp, cft, val);
2338 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002339 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002340 if (*end)
2341 return -EINVAL;
2342 retval = cft->write_s64(cgrp, cft, val);
2343 }
Paul Menage355e0c42007-10-18 23:39:33 -07002344 if (!retval)
2345 retval = nbytes;
2346 return retval;
2347}
2348
Paul Menagedb3b1492008-07-25 01:46:58 -07002349static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2350 struct file *file,
2351 const char __user *userbuf,
2352 size_t nbytes, loff_t *unused_ppos)
2353{
Paul Menage84eea842008-07-25 01:47:00 -07002354 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002355 int retval = 0;
2356 size_t max_bytes = cft->max_write_len;
2357 char *buffer = local_buffer;
2358
2359 if (!max_bytes)
2360 max_bytes = sizeof(local_buffer) - 1;
2361 if (nbytes >= max_bytes)
2362 return -E2BIG;
2363 /* Allocate a dynamic buffer if we need one */
2364 if (nbytes >= sizeof(local_buffer)) {
2365 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2366 if (buffer == NULL)
2367 return -ENOMEM;
2368 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002369 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2370 retval = -EFAULT;
2371 goto out;
2372 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002373
2374 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002375 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002376 if (!retval)
2377 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002378out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002379 if (buffer != local_buffer)
2380 kfree(buffer);
2381 return retval;
2382}
2383
Paul Menageddbcc7e2007-10-18 23:39:30 -07002384static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2385 size_t nbytes, loff_t *ppos)
2386{
2387 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002388 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002389
Li Zefan75139b82009-01-07 18:07:33 -08002390 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002391 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002392 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002393 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002394 if (cft->write_u64 || cft->write_s64)
2395 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002396 if (cft->write_string)
2397 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002398 if (cft->trigger) {
2399 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2400 return ret ? ret : nbytes;
2401 }
Paul Menage355e0c42007-10-18 23:39:33 -07002402 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002403}
2404
Paul Menagef4c753b2008-04-29 00:59:56 -07002405static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2406 struct file *file,
2407 char __user *buf, size_t nbytes,
2408 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002409{
Paul Menage84eea842008-07-25 01:47:00 -07002410 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002411 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002412 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2413
2414 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2415}
2416
Paul Menagee73d2c62008-04-29 01:00:06 -07002417static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2418 struct file *file,
2419 char __user *buf, size_t nbytes,
2420 loff_t *ppos)
2421{
Paul Menage84eea842008-07-25 01:47:00 -07002422 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002423 s64 val = cft->read_s64(cgrp, cft);
2424 int len = sprintf(tmp, "%lld\n", (long long) val);
2425
2426 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2427}
2428
Paul Menageddbcc7e2007-10-18 23:39:30 -07002429static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2430 size_t nbytes, loff_t *ppos)
2431{
2432 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002433 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002434
Li Zefan75139b82009-01-07 18:07:33 -08002435 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002436 return -ENODEV;
2437
2438 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002439 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002440 if (cft->read_u64)
2441 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002442 if (cft->read_s64)
2443 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002444 return -EINVAL;
2445}
2446
Paul Menage91796562008-04-29 01:00:01 -07002447/*
2448 * seqfile ops/methods for returning structured data. Currently just
2449 * supports string->u64 maps, but can be extended in future.
2450 */
2451
2452struct cgroup_seqfile_state {
2453 struct cftype *cft;
2454 struct cgroup *cgroup;
2455};
2456
2457static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2458{
2459 struct seq_file *sf = cb->state;
2460 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2461}
2462
2463static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2464{
2465 struct cgroup_seqfile_state *state = m->private;
2466 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002467 if (cft->read_map) {
2468 struct cgroup_map_cb cb = {
2469 .fill = cgroup_map_add,
2470 .state = m,
2471 };
2472 return cft->read_map(state->cgroup, cft, &cb);
2473 }
2474 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002475}
2476
Adrian Bunk96930a62008-07-25 19:46:21 -07002477static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002478{
2479 struct seq_file *seq = file->private_data;
2480 kfree(seq->private);
2481 return single_release(inode, file);
2482}
2483
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002484static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002485 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002486 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002487 .llseek = seq_lseek,
2488 .release = cgroup_seqfile_release,
2489};
2490
Paul Menageddbcc7e2007-10-18 23:39:30 -07002491static int cgroup_file_open(struct inode *inode, struct file *file)
2492{
2493 int err;
2494 struct cftype *cft;
2495
2496 err = generic_file_open(inode, file);
2497 if (err)
2498 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002499 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002500
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002501 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002502 struct cgroup_seqfile_state *state =
2503 kzalloc(sizeof(*state), GFP_USER);
2504 if (!state)
2505 return -ENOMEM;
2506 state->cft = cft;
2507 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2508 file->f_op = &cgroup_seqfile_operations;
2509 err = single_open(file, cgroup_seqfile_show, state);
2510 if (err < 0)
2511 kfree(state);
2512 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002513 err = cft->open(inode, file);
2514 else
2515 err = 0;
2516
2517 return err;
2518}
2519
2520static int cgroup_file_release(struct inode *inode, struct file *file)
2521{
2522 struct cftype *cft = __d_cft(file->f_dentry);
2523 if (cft->release)
2524 return cft->release(inode, file);
2525 return 0;
2526}
2527
2528/*
2529 * cgroup_rename - Only allow simple rename of directories in place.
2530 */
2531static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2532 struct inode *new_dir, struct dentry *new_dentry)
2533{
2534 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2535 return -ENOTDIR;
2536 if (new_dentry->d_inode)
2537 return -EEXIST;
2538 if (old_dir != new_dir)
2539 return -EIO;
2540 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2541}
2542
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002543static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2544{
2545 if (S_ISDIR(dentry->d_inode->i_mode))
2546 return &__d_cgrp(dentry)->xattrs;
2547 else
2548 return &__d_cft(dentry)->xattrs;
2549}
2550
2551static inline int xattr_enabled(struct dentry *dentry)
2552{
2553 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2554 return test_bit(ROOT_XATTR, &root->flags);
2555}
2556
2557static bool is_valid_xattr(const char *name)
2558{
2559 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2560 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2561 return true;
2562 return false;
2563}
2564
2565static int cgroup_setxattr(struct dentry *dentry, const char *name,
2566 const void *val, size_t size, int flags)
2567{
2568 if (!xattr_enabled(dentry))
2569 return -EOPNOTSUPP;
2570 if (!is_valid_xattr(name))
2571 return -EINVAL;
2572 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2573}
2574
2575static int cgroup_removexattr(struct dentry *dentry, const char *name)
2576{
2577 if (!xattr_enabled(dentry))
2578 return -EOPNOTSUPP;
2579 if (!is_valid_xattr(name))
2580 return -EINVAL;
2581 return simple_xattr_remove(__d_xattrs(dentry), name);
2582}
2583
2584static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2585 void *buf, size_t size)
2586{
2587 if (!xattr_enabled(dentry))
2588 return -EOPNOTSUPP;
2589 if (!is_valid_xattr(name))
2590 return -EINVAL;
2591 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2592}
2593
2594static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2595{
2596 if (!xattr_enabled(dentry))
2597 return -EOPNOTSUPP;
2598 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2599}
2600
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002601static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002602 .read = cgroup_file_read,
2603 .write = cgroup_file_write,
2604 .llseek = generic_file_llseek,
2605 .open = cgroup_file_open,
2606 .release = cgroup_file_release,
2607};
2608
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002609static const struct inode_operations cgroup_file_inode_operations = {
2610 .setxattr = cgroup_setxattr,
2611 .getxattr = cgroup_getxattr,
2612 .listxattr = cgroup_listxattr,
2613 .removexattr = cgroup_removexattr,
2614};
2615
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002616static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002617 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002618 .mkdir = cgroup_mkdir,
2619 .rmdir = cgroup_rmdir,
2620 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002621 .setxattr = cgroup_setxattr,
2622 .getxattr = cgroup_getxattr,
2623 .listxattr = cgroup_listxattr,
2624 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002625};
2626
Al Viro00cd8dd2012-06-10 17:13:09 -04002627static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002628{
2629 if (dentry->d_name.len > NAME_MAX)
2630 return ERR_PTR(-ENAMETOOLONG);
2631 d_add(dentry, NULL);
2632 return NULL;
2633}
2634
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002635/*
2636 * Check if a file is a control file
2637 */
2638static inline struct cftype *__file_cft(struct file *file)
2639{
2640 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2641 return ERR_PTR(-EINVAL);
2642 return __d_cft(file->f_dentry);
2643}
2644
Al Viroa5e7ed32011-07-26 01:55:55 -04002645static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002646 struct super_block *sb)
2647{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002648 struct inode *inode;
2649
2650 if (!dentry)
2651 return -ENOENT;
2652 if (dentry->d_inode)
2653 return -EEXIST;
2654
2655 inode = cgroup_new_inode(mode, sb);
2656 if (!inode)
2657 return -ENOMEM;
2658
2659 if (S_ISDIR(mode)) {
2660 inode->i_op = &cgroup_dir_inode_operations;
2661 inode->i_fop = &simple_dir_operations;
2662
2663 /* start off with i_nlink == 2 (for "." entry) */
2664 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002665 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002666
Tejun Heob8a2df62012-11-19 08:13:37 -08002667 /*
2668 * Control reaches here with cgroup_mutex held.
2669 * @inode->i_mutex should nest outside cgroup_mutex but we
2670 * want to populate it immediately without releasing
2671 * cgroup_mutex. As @inode isn't visible to anyone else
2672 * yet, trylock will always succeed without affecting
2673 * lockdep checks.
2674 */
2675 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002676 } else if (S_ISREG(mode)) {
2677 inode->i_size = 0;
2678 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002679 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002680 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002681 d_instantiate(dentry, inode);
2682 dget(dentry); /* Extra count - pin the dentry in core */
2683 return 0;
2684}
2685
Li Zefan099fca32009-04-02 16:57:29 -07002686/**
2687 * cgroup_file_mode - deduce file mode of a control file
2688 * @cft: the control file in question
2689 *
2690 * returns cft->mode if ->mode is not 0
2691 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2692 * returns S_IRUGO if it has only a read handler
2693 * returns S_IWUSR if it has only a write hander
2694 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002695static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002696{
Al Viroa5e7ed32011-07-26 01:55:55 -04002697 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002698
2699 if (cft->mode)
2700 return cft->mode;
2701
2702 if (cft->read || cft->read_u64 || cft->read_s64 ||
2703 cft->read_map || cft->read_seq_string)
2704 mode |= S_IRUGO;
2705
2706 if (cft->write || cft->write_u64 || cft->write_s64 ||
2707 cft->write_string || cft->trigger)
2708 mode |= S_IWUSR;
2709
2710 return mode;
2711}
2712
Tejun Heodb0416b2012-04-01 12:09:55 -07002713static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002714 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002715{
Paul Menagebd89aab2007-10-18 23:40:44 -07002716 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002717 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002718 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002719 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002720 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002721 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002722 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002723
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002724 simple_xattrs_init(&cft->xattrs);
2725
Paul Menagebd89aab2007-10-18 23:40:44 -07002726 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002727 strcpy(name, subsys->name);
2728 strcat(name, ".");
2729 }
2730 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002731
Paul Menageddbcc7e2007-10-18 23:39:30 -07002732 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002733
2734 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2735 if (!cfe)
2736 return -ENOMEM;
2737
Paul Menageddbcc7e2007-10-18 23:39:30 -07002738 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002739 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002740 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002741 goto out;
2742 }
2743
2744 mode = cgroup_file_mode(cft);
2745 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2746 if (!error) {
2747 cfe->type = (void *)cft;
2748 cfe->dentry = dentry;
2749 dentry->d_fsdata = cfe;
2750 list_add_tail(&cfe->node, &parent->files);
2751 cfe = NULL;
2752 }
2753 dput(dentry);
2754out:
2755 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002756 return error;
2757}
2758
Tejun Heo79578622012-04-01 12:09:56 -07002759static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002760 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002761{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002762 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002763 int err, ret = 0;
2764
2765 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002766 /* does cft->flags tell us to skip this file on @cgrp? */
2767 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2768 continue;
2769 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2770 continue;
2771
Tejun Heo79578622012-04-01 12:09:56 -07002772 if (is_add)
2773 err = cgroup_add_file(cgrp, subsys, cft);
2774 else
2775 err = cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002776 if (err) {
Tejun Heo79578622012-04-01 12:09:56 -07002777 pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n",
2778 is_add ? "add" : "remove", cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002779 ret = err;
2780 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002781 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002782 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002783}
2784
Tejun Heo8e3f6542012-04-01 12:09:55 -07002785static DEFINE_MUTEX(cgroup_cft_mutex);
2786
2787static void cgroup_cfts_prepare(void)
2788 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2789{
2790 /*
2791 * Thanks to the entanglement with vfs inode locking, we can't walk
2792 * the existing cgroups under cgroup_mutex and create files.
2793 * Instead, we increment reference on all cgroups and build list of
2794 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2795 * exclusive access to the field.
2796 */
2797 mutex_lock(&cgroup_cft_mutex);
2798 mutex_lock(&cgroup_mutex);
2799}
2800
2801static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002802 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002803 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2804{
2805 LIST_HEAD(pending);
2806 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002807
2808 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2809 if (cfts && ss->root != &rootnode) {
2810 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2811 dget(cgrp->dentry);
2812 list_add_tail(&cgrp->cft_q_node, &pending);
2813 }
2814 }
2815
2816 mutex_unlock(&cgroup_mutex);
2817
2818 /*
2819 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2820 * files for all cgroups which were created before.
2821 */
2822 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2823 struct inode *inode = cgrp->dentry->d_inode;
2824
2825 mutex_lock(&inode->i_mutex);
2826 mutex_lock(&cgroup_mutex);
2827 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002828 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002829 mutex_unlock(&cgroup_mutex);
2830 mutex_unlock(&inode->i_mutex);
2831
2832 list_del_init(&cgrp->cft_q_node);
2833 dput(cgrp->dentry);
2834 }
2835
2836 mutex_unlock(&cgroup_cft_mutex);
2837}
2838
2839/**
2840 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2841 * @ss: target cgroup subsystem
2842 * @cfts: zero-length name terminated array of cftypes
2843 *
2844 * Register @cfts to @ss. Files described by @cfts are created for all
2845 * existing cgroups to which @ss is attached and all future cgroups will
2846 * have them too. This function can be called anytime whether @ss is
2847 * attached or not.
2848 *
2849 * Returns 0 on successful registration, -errno on failure. Note that this
2850 * function currently returns 0 as long as @cfts registration is successful
2851 * even if some file creation attempts on existing cgroups fail.
2852 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002853int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002854{
2855 struct cftype_set *set;
2856
2857 set = kzalloc(sizeof(*set), GFP_KERNEL);
2858 if (!set)
2859 return -ENOMEM;
2860
2861 cgroup_cfts_prepare();
2862 set->cfts = cfts;
2863 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002864 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002865
2866 return 0;
2867}
2868EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2869
Li Zefana043e3b2008-02-23 15:24:09 -08002870/**
Tejun Heo79578622012-04-01 12:09:56 -07002871 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2872 * @ss: target cgroup subsystem
2873 * @cfts: zero-length name terminated array of cftypes
2874 *
2875 * Unregister @cfts from @ss. Files described by @cfts are removed from
2876 * all existing cgroups to which @ss is attached and all future cgroups
2877 * won't have them either. This function can be called anytime whether @ss
2878 * is attached or not.
2879 *
2880 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2881 * registered with @ss.
2882 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002883int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002884{
2885 struct cftype_set *set;
2886
2887 cgroup_cfts_prepare();
2888
2889 list_for_each_entry(set, &ss->cftsets, node) {
2890 if (set->cfts == cfts) {
2891 list_del_init(&set->node);
2892 cgroup_cfts_commit(ss, cfts, false);
2893 return 0;
2894 }
2895 }
2896
2897 cgroup_cfts_commit(ss, NULL, false);
2898 return -ENOENT;
2899}
2900
2901/**
Li Zefana043e3b2008-02-23 15:24:09 -08002902 * cgroup_task_count - count the number of tasks in a cgroup.
2903 * @cgrp: the cgroup in question
2904 *
2905 * Return the number of tasks in the cgroup.
2906 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002907int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002908{
2909 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002910 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002911
Paul Menage817929e2007-10-18 23:39:36 -07002912 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002913 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002914 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002915 }
2916 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002917 return count;
2918}
2919
2920/*
Paul Menage817929e2007-10-18 23:39:36 -07002921 * Advance a list_head iterator. The iterator should be positioned at
2922 * the start of a css_set
2923 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002924static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002925 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002926{
2927 struct list_head *l = it->cg_link;
2928 struct cg_cgroup_link *link;
2929 struct css_set *cg;
2930
2931 /* Advance to the next non-empty css_set */
2932 do {
2933 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002934 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002935 it->cg_link = NULL;
2936 return;
2937 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002938 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002939 cg = link->cg;
2940 } while (list_empty(&cg->tasks));
2941 it->cg_link = l;
2942 it->task = cg->tasks.next;
2943}
2944
Cliff Wickman31a7df02008-02-07 00:14:42 -08002945/*
2946 * To reduce the fork() overhead for systems that are not actually
2947 * using their cgroups capability, we don't maintain the lists running
2948 * through each css_set to its tasks until we see the list actually
2949 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002950 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002951static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002952{
2953 struct task_struct *p, *g;
2954 write_lock(&css_set_lock);
2955 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002956 /*
2957 * We need tasklist_lock because RCU is not safe against
2958 * while_each_thread(). Besides, a forking task that has passed
2959 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2960 * is not guaranteed to have its child immediately visible in the
2961 * tasklist if we walk through it with RCU.
2962 */
2963 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002964 do_each_thread(g, p) {
2965 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002966 /*
2967 * We should check if the process is exiting, otherwise
2968 * it will race with cgroup_exit() in that the list
2969 * entry won't be deleted though the process has exited.
2970 */
2971 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002972 list_add(&p->cg_list, &p->cgroups->tasks);
2973 task_unlock(p);
2974 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002975 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002976 write_unlock(&css_set_lock);
2977}
2978
Tejun Heo574bd9f2012-11-09 09:12:29 -08002979/**
2980 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2981 * @pos: the current position (%NULL to initiate traversal)
2982 * @cgroup: cgroup whose descendants to walk
2983 *
2984 * To be used by cgroup_for_each_descendant_pre(). Find the next
2985 * descendant to visit for pre-order traversal of @cgroup's descendants.
2986 */
2987struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2988 struct cgroup *cgroup)
2989{
2990 struct cgroup *next;
2991
2992 WARN_ON_ONCE(!rcu_read_lock_held());
2993
2994 /* if first iteration, pretend we just visited @cgroup */
2995 if (!pos) {
2996 if (list_empty(&cgroup->children))
2997 return NULL;
2998 pos = cgroup;
2999 }
3000
3001 /* visit the first child if exists */
3002 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3003 if (next)
3004 return next;
3005
3006 /* no child, visit my or the closest ancestor's next sibling */
3007 do {
3008 next = list_entry_rcu(pos->sibling.next, struct cgroup,
3009 sibling);
3010 if (&next->sibling != &pos->parent->children)
3011 return next;
3012
3013 pos = pos->parent;
3014 } while (pos != cgroup);
3015
3016 return NULL;
3017}
3018EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3019
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003020/**
3021 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3022 * @pos: cgroup of interest
3023 *
3024 * Return the rightmost descendant of @pos. If there's no descendant,
3025 * @pos is returned. This can be used during pre-order traversal to skip
3026 * subtree of @pos.
3027 */
3028struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3029{
3030 struct cgroup *last, *tmp;
3031
3032 WARN_ON_ONCE(!rcu_read_lock_held());
3033
3034 do {
3035 last = pos;
3036 /* ->prev isn't RCU safe, walk ->next till the end */
3037 pos = NULL;
3038 list_for_each_entry_rcu(tmp, &last->children, sibling)
3039 pos = tmp;
3040 } while (pos);
3041
3042 return last;
3043}
3044EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3045
Tejun Heo574bd9f2012-11-09 09:12:29 -08003046static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3047{
3048 struct cgroup *last;
3049
3050 do {
3051 last = pos;
3052 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3053 sibling);
3054 } while (pos);
3055
3056 return last;
3057}
3058
3059/**
3060 * cgroup_next_descendant_post - find the next descendant for post-order walk
3061 * @pos: the current position (%NULL to initiate traversal)
3062 * @cgroup: cgroup whose descendants to walk
3063 *
3064 * To be used by cgroup_for_each_descendant_post(). Find the next
3065 * descendant to visit for post-order traversal of @cgroup's descendants.
3066 */
3067struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3068 struct cgroup *cgroup)
3069{
3070 struct cgroup *next;
3071
3072 WARN_ON_ONCE(!rcu_read_lock_held());
3073
3074 /* if first iteration, visit the leftmost descendant */
3075 if (!pos) {
3076 next = cgroup_leftmost_descendant(cgroup);
3077 return next != cgroup ? next : NULL;
3078 }
3079
3080 /* if there's an unvisited sibling, visit its leftmost descendant */
3081 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3082 if (&next->sibling != &pos->parent->children)
3083 return cgroup_leftmost_descendant(next);
3084
3085 /* no sibling left, visit parent */
3086 next = pos->parent;
3087 return next != cgroup ? next : NULL;
3088}
3089EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3090
Paul Menagebd89aab2007-10-18 23:40:44 -07003091void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003092 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003093{
3094 /*
3095 * The first time anyone tries to iterate across a cgroup,
3096 * we need to enable the list linking each css_set to its
3097 * tasks, and fix up all existing tasks.
3098 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003099 if (!use_task_css_set_links)
3100 cgroup_enable_task_cg_lists();
3101
Paul Menage817929e2007-10-18 23:39:36 -07003102 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003103 it->cg_link = &cgrp->css_sets;
3104 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003105}
3106
Paul Menagebd89aab2007-10-18 23:40:44 -07003107struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003108 struct cgroup_iter *it)
3109{
3110 struct task_struct *res;
3111 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003112 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003113
3114 /* If the iterator cg is NULL, we have no tasks */
3115 if (!it->cg_link)
3116 return NULL;
3117 res = list_entry(l, struct task_struct, cg_list);
3118 /* Advance iterator to find next entry */
3119 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003120 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3121 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003122 /* We reached the end of this task list - move on to
3123 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003124 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003125 } else {
3126 it->task = l;
3127 }
3128 return res;
3129}
3130
Paul Menagebd89aab2007-10-18 23:40:44 -07003131void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003132 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003133{
3134 read_unlock(&css_set_lock);
3135}
3136
Cliff Wickman31a7df02008-02-07 00:14:42 -08003137static inline int started_after_time(struct task_struct *t1,
3138 struct timespec *time,
3139 struct task_struct *t2)
3140{
3141 int start_diff = timespec_compare(&t1->start_time, time);
3142 if (start_diff > 0) {
3143 return 1;
3144 } else if (start_diff < 0) {
3145 return 0;
3146 } else {
3147 /*
3148 * Arbitrarily, if two processes started at the same
3149 * time, we'll say that the lower pointer value
3150 * started first. Note that t2 may have exited by now
3151 * so this may not be a valid pointer any longer, but
3152 * that's fine - it still serves to distinguish
3153 * between two tasks started (effectively) simultaneously.
3154 */
3155 return t1 > t2;
3156 }
3157}
3158
3159/*
3160 * This function is a callback from heap_insert() and is used to order
3161 * the heap.
3162 * In this case we order the heap in descending task start time.
3163 */
3164static inline int started_after(void *p1, void *p2)
3165{
3166 struct task_struct *t1 = p1;
3167 struct task_struct *t2 = p2;
3168 return started_after_time(t1, &t2->start_time, t2);
3169}
3170
3171/**
3172 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3173 * @scan: struct cgroup_scanner containing arguments for the scan
3174 *
3175 * Arguments include pointers to callback functions test_task() and
3176 * process_task().
3177 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3178 * and if it returns true, call process_task() for it also.
3179 * The test_task pointer may be NULL, meaning always true (select all tasks).
3180 * Effectively duplicates cgroup_iter_{start,next,end}()
3181 * but does not lock css_set_lock for the call to process_task().
3182 * The struct cgroup_scanner may be embedded in any structure of the caller's
3183 * creation.
3184 * It is guaranteed that process_task() will act on every task that
3185 * is a member of the cgroup for the duration of this call. This
3186 * function may or may not call process_task() for tasks that exit
3187 * or move to a different cgroup during the call, or are forked or
3188 * move into the cgroup during the call.
3189 *
3190 * Note that test_task() may be called with locks held, and may in some
3191 * situations be called multiple times for the same task, so it should
3192 * be cheap.
3193 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3194 * pre-allocated and will be used for heap operations (and its "gt" member will
3195 * be overwritten), else a temporary heap will be used (allocation of which
3196 * may cause this function to fail).
3197 */
3198int cgroup_scan_tasks(struct cgroup_scanner *scan)
3199{
3200 int retval, i;
3201 struct cgroup_iter it;
3202 struct task_struct *p, *dropped;
3203 /* Never dereference latest_task, since it's not refcounted */
3204 struct task_struct *latest_task = NULL;
3205 struct ptr_heap tmp_heap;
3206 struct ptr_heap *heap;
3207 struct timespec latest_time = { 0, 0 };
3208
3209 if (scan->heap) {
3210 /* The caller supplied our heap and pre-allocated its memory */
3211 heap = scan->heap;
3212 heap->gt = &started_after;
3213 } else {
3214 /* We need to allocate our own heap memory */
3215 heap = &tmp_heap;
3216 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3217 if (retval)
3218 /* cannot allocate the heap */
3219 return retval;
3220 }
3221
3222 again:
3223 /*
3224 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3225 * to determine which are of interest, and using the scanner's
3226 * "process_task" callback to process any of them that need an update.
3227 * Since we don't want to hold any locks during the task updates,
3228 * gather tasks to be processed in a heap structure.
3229 * The heap is sorted by descending task start time.
3230 * If the statically-sized heap fills up, we overflow tasks that
3231 * started later, and in future iterations only consider tasks that
3232 * started after the latest task in the previous pass. This
3233 * guarantees forward progress and that we don't miss any tasks.
3234 */
3235 heap->size = 0;
3236 cgroup_iter_start(scan->cg, &it);
3237 while ((p = cgroup_iter_next(scan->cg, &it))) {
3238 /*
3239 * Only affect tasks that qualify per the caller's callback,
3240 * if he provided one
3241 */
3242 if (scan->test_task && !scan->test_task(p, scan))
3243 continue;
3244 /*
3245 * Only process tasks that started after the last task
3246 * we processed
3247 */
3248 if (!started_after_time(p, &latest_time, latest_task))
3249 continue;
3250 dropped = heap_insert(heap, p);
3251 if (dropped == NULL) {
3252 /*
3253 * The new task was inserted; the heap wasn't
3254 * previously full
3255 */
3256 get_task_struct(p);
3257 } else if (dropped != p) {
3258 /*
3259 * The new task was inserted, and pushed out a
3260 * different task
3261 */
3262 get_task_struct(p);
3263 put_task_struct(dropped);
3264 }
3265 /*
3266 * Else the new task was newer than anything already in
3267 * the heap and wasn't inserted
3268 */
3269 }
3270 cgroup_iter_end(scan->cg, &it);
3271
3272 if (heap->size) {
3273 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003274 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003275 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003276 latest_time = q->start_time;
3277 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003278 }
3279 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003280 scan->process_task(q, scan);
3281 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003282 }
3283 /*
3284 * If we had to process any tasks at all, scan again
3285 * in case some of them were in the middle of forking
3286 * children that didn't get processed.
3287 * Not the most efficient way to do it, but it avoids
3288 * having to take callback_mutex in the fork path
3289 */
3290 goto again;
3291 }
3292 if (heap == &tmp_heap)
3293 heap_free(&tmp_heap);
3294 return 0;
3295}
3296
Paul Menage817929e2007-10-18 23:39:36 -07003297/*
Ben Blum102a7752009-09-23 15:56:26 -07003298 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003299 *
3300 * Reading this file can return large amounts of data if a cgroup has
3301 * *lots* of attached tasks. So it may need several calls to read(),
3302 * but we cannot guarantee that the information we produce is correct
3303 * unless we produce it entirely atomically.
3304 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003305 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003306
Li Zefan24528252012-01-20 11:58:43 +08003307/* which pidlist file are we talking about? */
3308enum cgroup_filetype {
3309 CGROUP_FILE_PROCS,
3310 CGROUP_FILE_TASKS,
3311};
3312
3313/*
3314 * A pidlist is a list of pids that virtually represents the contents of one
3315 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3316 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3317 * to the cgroup.
3318 */
3319struct cgroup_pidlist {
3320 /*
3321 * used to find which pidlist is wanted. doesn't change as long as
3322 * this particular list stays in the list.
3323 */
3324 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3325 /* array of xids */
3326 pid_t *list;
3327 /* how many elements the above list has */
3328 int length;
3329 /* how many files are using the current array */
3330 int use_count;
3331 /* each of these stored in a list by its cgroup */
3332 struct list_head links;
3333 /* pointer to the cgroup we belong to, for list removal purposes */
3334 struct cgroup *owner;
3335 /* protects the other fields */
3336 struct rw_semaphore mutex;
3337};
3338
Paul Menagebbcb81d2007-10-18 23:39:32 -07003339/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003340 * The following two functions "fix" the issue where there are more pids
3341 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3342 * TODO: replace with a kernel-wide solution to this problem
3343 */
3344#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3345static void *pidlist_allocate(int count)
3346{
3347 if (PIDLIST_TOO_LARGE(count))
3348 return vmalloc(count * sizeof(pid_t));
3349 else
3350 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3351}
3352static void pidlist_free(void *p)
3353{
3354 if (is_vmalloc_addr(p))
3355 vfree(p);
3356 else
3357 kfree(p);
3358}
3359static void *pidlist_resize(void *p, int newcount)
3360{
3361 void *newlist;
3362 /* note: if new alloc fails, old p will still be valid either way */
3363 if (is_vmalloc_addr(p)) {
3364 newlist = vmalloc(newcount * sizeof(pid_t));
3365 if (!newlist)
3366 return NULL;
3367 memcpy(newlist, p, newcount * sizeof(pid_t));
3368 vfree(p);
3369 } else {
3370 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3371 }
3372 return newlist;
3373}
3374
3375/*
Ben Blum102a7752009-09-23 15:56:26 -07003376 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3377 * If the new stripped list is sufficiently smaller and there's enough memory
3378 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3379 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003380 */
Ben Blum102a7752009-09-23 15:56:26 -07003381/* is the size difference enough that we should re-allocate the array? */
3382#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3383static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003384{
Ben Blum102a7752009-09-23 15:56:26 -07003385 int src, dest = 1;
3386 pid_t *list = *p;
3387 pid_t *newlist;
3388
3389 /*
3390 * we presume the 0th element is unique, so i starts at 1. trivial
3391 * edge cases first; no work needs to be done for either
3392 */
3393 if (length == 0 || length == 1)
3394 return length;
3395 /* src and dest walk down the list; dest counts unique elements */
3396 for (src = 1; src < length; src++) {
3397 /* find next unique element */
3398 while (list[src] == list[src-1]) {
3399 src++;
3400 if (src == length)
3401 goto after;
3402 }
3403 /* dest always points to where the next unique element goes */
3404 list[dest] = list[src];
3405 dest++;
3406 }
3407after:
3408 /*
3409 * if the length difference is large enough, we want to allocate a
3410 * smaller buffer to save memory. if this fails due to out of memory,
3411 * we'll just stay with what we've got.
3412 */
3413 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003414 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003415 if (newlist)
3416 *p = newlist;
3417 }
3418 return dest;
3419}
3420
3421static int cmppid(const void *a, const void *b)
3422{
3423 return *(pid_t *)a - *(pid_t *)b;
3424}
3425
3426/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003427 * find the appropriate pidlist for our purpose (given procs vs tasks)
3428 * returns with the lock on that pidlist already held, and takes care
3429 * of the use count, or returns NULL with no locks held if we're out of
3430 * memory.
3431 */
3432static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3433 enum cgroup_filetype type)
3434{
3435 struct cgroup_pidlist *l;
3436 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003437 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003438
Ben Blum72a8cb32009-09-23 15:56:27 -07003439 /*
3440 * We can't drop the pidlist_mutex before taking the l->mutex in case
3441 * the last ref-holder is trying to remove l from the list at the same
3442 * time. Holding the pidlist_mutex precludes somebody taking whichever
3443 * list we find out from under us - compare release_pid_array().
3444 */
3445 mutex_lock(&cgrp->pidlist_mutex);
3446 list_for_each_entry(l, &cgrp->pidlists, links) {
3447 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003448 /* make sure l doesn't vanish out from under us */
3449 down_write(&l->mutex);
3450 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003451 return l;
3452 }
3453 }
3454 /* entry not found; create a new one */
3455 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3456 if (!l) {
3457 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003458 return l;
3459 }
3460 init_rwsem(&l->mutex);
3461 down_write(&l->mutex);
3462 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003463 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003464 l->use_count = 0; /* don't increment here */
3465 l->list = NULL;
3466 l->owner = cgrp;
3467 list_add(&l->links, &cgrp->pidlists);
3468 mutex_unlock(&cgrp->pidlist_mutex);
3469 return l;
3470}
3471
3472/*
Ben Blum102a7752009-09-23 15:56:26 -07003473 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3474 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003475static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3476 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003477{
3478 pid_t *array;
3479 int length;
3480 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003481 struct cgroup_iter it;
3482 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003483 struct cgroup_pidlist *l;
3484
3485 /*
3486 * If cgroup gets more users after we read count, we won't have
3487 * enough space - tough. This race is indistinguishable to the
3488 * caller from the case that the additional cgroup users didn't
3489 * show up until sometime later on.
3490 */
3491 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003492 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003493 if (!array)
3494 return -ENOMEM;
3495 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003496 cgroup_iter_start(cgrp, &it);
3497 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003498 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003499 break;
Ben Blum102a7752009-09-23 15:56:26 -07003500 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003501 if (type == CGROUP_FILE_PROCS)
3502 pid = task_tgid_vnr(tsk);
3503 else
3504 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003505 if (pid > 0) /* make sure to only use valid results */
3506 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003507 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003508 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003509 length = n;
3510 /* now sort & (if procs) strip out duplicates */
3511 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003512 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003513 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003514 l = cgroup_pidlist_find(cgrp, type);
3515 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003516 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003517 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003518 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003519 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003520 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003521 l->list = array;
3522 l->length = length;
3523 l->use_count++;
3524 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003525 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003526 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003527}
3528
Balbir Singh846c7bb2007-10-18 23:39:44 -07003529/**
Li Zefana043e3b2008-02-23 15:24:09 -08003530 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003531 * @stats: cgroupstats to fill information into
3532 * @dentry: A dentry entry belonging to the cgroup for which stats have
3533 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003534 *
3535 * Build and fill cgroupstats so that taskstats can export it to user
3536 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003537 */
3538int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3539{
3540 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003541 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003542 struct cgroup_iter it;
3543 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003544
Balbir Singh846c7bb2007-10-18 23:39:44 -07003545 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003546 * Validate dentry by checking the superblock operations,
3547 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003548 */
Li Zefan33d283b2008-11-19 15:36:48 -08003549 if (dentry->d_sb->s_op != &cgroup_ops ||
3550 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003551 goto err;
3552
3553 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003554 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003555
Paul Menagebd89aab2007-10-18 23:40:44 -07003556 cgroup_iter_start(cgrp, &it);
3557 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003558 switch (tsk->state) {
3559 case TASK_RUNNING:
3560 stats->nr_running++;
3561 break;
3562 case TASK_INTERRUPTIBLE:
3563 stats->nr_sleeping++;
3564 break;
3565 case TASK_UNINTERRUPTIBLE:
3566 stats->nr_uninterruptible++;
3567 break;
3568 case TASK_STOPPED:
3569 stats->nr_stopped++;
3570 break;
3571 default:
3572 if (delayacct_is_task_waiting_on_io(tsk))
3573 stats->nr_io_wait++;
3574 break;
3575 }
3576 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003577 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003578
Balbir Singh846c7bb2007-10-18 23:39:44 -07003579err:
3580 return ret;
3581}
3582
Paul Menage8f3ff202009-09-23 15:56:25 -07003583
Paul Menagecc31edc2008-10-18 20:28:04 -07003584/*
Ben Blum102a7752009-09-23 15:56:26 -07003585 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003586 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003587 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003588 */
3589
Ben Blum102a7752009-09-23 15:56:26 -07003590static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003591{
3592 /*
3593 * Initially we receive a position value that corresponds to
3594 * one more than the last pid shown (or 0 on the first call or
3595 * after a seek to the start). Use a binary-search to find the
3596 * next pid to display, if any
3597 */
Ben Blum102a7752009-09-23 15:56:26 -07003598 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003599 int index = 0, pid = *pos;
3600 int *iter;
3601
Ben Blum102a7752009-09-23 15:56:26 -07003602 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003603 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003604 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003605
Paul Menagecc31edc2008-10-18 20:28:04 -07003606 while (index < end) {
3607 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003608 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003609 index = mid;
3610 break;
Ben Blum102a7752009-09-23 15:56:26 -07003611 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003612 index = mid + 1;
3613 else
3614 end = mid;
3615 }
3616 }
3617 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003618 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003619 return NULL;
3620 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003621 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003622 *pos = *iter;
3623 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003624}
3625
Ben Blum102a7752009-09-23 15:56:26 -07003626static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003627{
Ben Blum102a7752009-09-23 15:56:26 -07003628 struct cgroup_pidlist *l = s->private;
3629 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003630}
3631
Ben Blum102a7752009-09-23 15:56:26 -07003632static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003633{
Ben Blum102a7752009-09-23 15:56:26 -07003634 struct cgroup_pidlist *l = s->private;
3635 pid_t *p = v;
3636 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003637 /*
3638 * Advance to the next pid in the array. If this goes off the
3639 * end, we're done
3640 */
3641 p++;
3642 if (p >= end) {
3643 return NULL;
3644 } else {
3645 *pos = *p;
3646 return p;
3647 }
3648}
3649
Ben Blum102a7752009-09-23 15:56:26 -07003650static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003651{
3652 return seq_printf(s, "%d\n", *(int *)v);
3653}
3654
Ben Blum102a7752009-09-23 15:56:26 -07003655/*
3656 * seq_operations functions for iterating on pidlists through seq_file -
3657 * independent of whether it's tasks or procs
3658 */
3659static const struct seq_operations cgroup_pidlist_seq_operations = {
3660 .start = cgroup_pidlist_start,
3661 .stop = cgroup_pidlist_stop,
3662 .next = cgroup_pidlist_next,
3663 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003664};
3665
Ben Blum102a7752009-09-23 15:56:26 -07003666static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003667{
Ben Blum72a8cb32009-09-23 15:56:27 -07003668 /*
3669 * the case where we're the last user of this particular pidlist will
3670 * have us remove it from the cgroup's list, which entails taking the
3671 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3672 * pidlist_mutex, we have to take pidlist_mutex first.
3673 */
3674 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003675 down_write(&l->mutex);
3676 BUG_ON(!l->use_count);
3677 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003678 /* we're the last user if refcount is 0; remove and free */
3679 list_del(&l->links);
3680 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003681 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003682 put_pid_ns(l->key.ns);
3683 up_write(&l->mutex);
3684 kfree(l);
3685 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003686 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003687 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003688 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003689}
3690
Ben Blum102a7752009-09-23 15:56:26 -07003691static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003692{
Ben Blum102a7752009-09-23 15:56:26 -07003693 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003694 if (!(file->f_mode & FMODE_READ))
3695 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003696 /*
3697 * the seq_file will only be initialized if the file was opened for
3698 * reading; hence we check if it's not null only in that case.
3699 */
3700 l = ((struct seq_file *)file->private_data)->private;
3701 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003702 return seq_release(inode, file);
3703}
3704
Ben Blum102a7752009-09-23 15:56:26 -07003705static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003706 .read = seq_read,
3707 .llseek = seq_lseek,
3708 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003709 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003710};
3711
3712/*
Ben Blum102a7752009-09-23 15:56:26 -07003713 * The following functions handle opens on a file that displays a pidlist
3714 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3715 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003716 */
Ben Blum102a7752009-09-23 15:56:26 -07003717/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003718static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003719{
3720 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003721 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003722 int retval;
3723
3724 /* Nothing to do for write-only files */
3725 if (!(file->f_mode & FMODE_READ))
3726 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003727
Ben Blum102a7752009-09-23 15:56:26 -07003728 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003729 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003730 if (retval)
3731 return retval;
3732 /* configure file information */
3733 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003734
Ben Blum102a7752009-09-23 15:56:26 -07003735 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003736 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003737 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003738 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003739 }
Ben Blum102a7752009-09-23 15:56:26 -07003740 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003741 return 0;
3742}
Ben Blum102a7752009-09-23 15:56:26 -07003743static int cgroup_tasks_open(struct inode *unused, struct file *file)
3744{
Ben Blum72a8cb32009-09-23 15:56:27 -07003745 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003746}
3747static int cgroup_procs_open(struct inode *unused, struct file *file)
3748{
Ben Blum72a8cb32009-09-23 15:56:27 -07003749 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003750}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003751
Paul Menagebd89aab2007-10-18 23:40:44 -07003752static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003753 struct cftype *cft)
3754{
Paul Menagebd89aab2007-10-18 23:40:44 -07003755 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003756}
3757
Paul Menage6379c102008-07-25 01:47:01 -07003758static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3759 struct cftype *cft,
3760 u64 val)
3761{
3762 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3763 if (val)
3764 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3765 else
3766 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3767 return 0;
3768}
3769
Paul Menagebbcb81d2007-10-18 23:39:32 -07003770/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003771 * Unregister event and free resources.
3772 *
3773 * Gets called from workqueue.
3774 */
3775static void cgroup_event_remove(struct work_struct *work)
3776{
3777 struct cgroup_event *event = container_of(work, struct cgroup_event,
3778 remove);
3779 struct cgroup *cgrp = event->cgrp;
3780
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003781 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3782
3783 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003784 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003785 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003786}
3787
3788/*
3789 * Gets called on POLLHUP on eventfd when user closes it.
3790 *
3791 * Called with wqh->lock held and interrupts disabled.
3792 */
3793static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3794 int sync, void *key)
3795{
3796 struct cgroup_event *event = container_of(wait,
3797 struct cgroup_event, wait);
3798 struct cgroup *cgrp = event->cgrp;
3799 unsigned long flags = (unsigned long)key;
3800
3801 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003802 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003803 spin_lock(&cgrp->event_list_lock);
Greg Thelen9718ceb2012-11-28 13:50:45 -08003804 list_del_init(&event->list);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003805 spin_unlock(&cgrp->event_list_lock);
3806 /*
3807 * We are in atomic context, but cgroup_event_remove() may
3808 * sleep, so we have to call it in workqueue.
3809 */
3810 schedule_work(&event->remove);
3811 }
3812
3813 return 0;
3814}
3815
3816static void cgroup_event_ptable_queue_proc(struct file *file,
3817 wait_queue_head_t *wqh, poll_table *pt)
3818{
3819 struct cgroup_event *event = container_of(pt,
3820 struct cgroup_event, pt);
3821
3822 event->wqh = wqh;
3823 add_wait_queue(wqh, &event->wait);
3824}
3825
3826/*
3827 * Parse input and register new cgroup event handler.
3828 *
3829 * Input must be in format '<event_fd> <control_fd> <args>'.
3830 * Interpretation of args is defined by control file implementation.
3831 */
3832static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3833 const char *buffer)
3834{
3835 struct cgroup_event *event = NULL;
3836 unsigned int efd, cfd;
3837 struct file *efile = NULL;
3838 struct file *cfile = NULL;
3839 char *endp;
3840 int ret;
3841
3842 efd = simple_strtoul(buffer, &endp, 10);
3843 if (*endp != ' ')
3844 return -EINVAL;
3845 buffer = endp + 1;
3846
3847 cfd = simple_strtoul(buffer, &endp, 10);
3848 if ((*endp != ' ') && (*endp != '\0'))
3849 return -EINVAL;
3850 buffer = endp + 1;
3851
3852 event = kzalloc(sizeof(*event), GFP_KERNEL);
3853 if (!event)
3854 return -ENOMEM;
3855 event->cgrp = cgrp;
3856 INIT_LIST_HEAD(&event->list);
3857 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3858 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3859 INIT_WORK(&event->remove, cgroup_event_remove);
3860
3861 efile = eventfd_fget(efd);
3862 if (IS_ERR(efile)) {
3863 ret = PTR_ERR(efile);
3864 goto fail;
3865 }
3866
3867 event->eventfd = eventfd_ctx_fileget(efile);
3868 if (IS_ERR(event->eventfd)) {
3869 ret = PTR_ERR(event->eventfd);
3870 goto fail;
3871 }
3872
3873 cfile = fget(cfd);
3874 if (!cfile) {
3875 ret = -EBADF;
3876 goto fail;
3877 }
3878
3879 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003880 /* AV: shouldn't we check that it's been opened for read instead? */
3881 ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003882 if (ret < 0)
3883 goto fail;
3884
3885 event->cft = __file_cft(cfile);
3886 if (IS_ERR(event->cft)) {
3887 ret = PTR_ERR(event->cft);
3888 goto fail;
3889 }
3890
3891 if (!event->cft->register_event || !event->cft->unregister_event) {
3892 ret = -EINVAL;
3893 goto fail;
3894 }
3895
3896 ret = event->cft->register_event(cgrp, event->cft,
3897 event->eventfd, buffer);
3898 if (ret)
3899 goto fail;
3900
3901 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3902 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3903 ret = 0;
3904 goto fail;
3905 }
3906
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003907 /*
3908 * Events should be removed after rmdir of cgroup directory, but before
3909 * destroying subsystem state objects. Let's take reference to cgroup
3910 * directory dentry to do that.
3911 */
3912 dget(cgrp->dentry);
3913
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003914 spin_lock(&cgrp->event_list_lock);
3915 list_add(&event->list, &cgrp->event_list);
3916 spin_unlock(&cgrp->event_list_lock);
3917
3918 fput(cfile);
3919 fput(efile);
3920
3921 return 0;
3922
3923fail:
3924 if (cfile)
3925 fput(cfile);
3926
3927 if (event && event->eventfd && !IS_ERR(event->eventfd))
3928 eventfd_ctx_put(event->eventfd);
3929
3930 if (!IS_ERR_OR_NULL(efile))
3931 fput(efile);
3932
3933 kfree(event);
3934
3935 return ret;
3936}
3937
Daniel Lezcano97978e62010-10-27 15:33:35 -07003938static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3939 struct cftype *cft)
3940{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003941 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003942}
3943
3944static int cgroup_clone_children_write(struct cgroup *cgrp,
3945 struct cftype *cft,
3946 u64 val)
3947{
3948 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003949 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003950 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003951 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003952 return 0;
3953}
3954
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003955/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003956 * for the common functions, 'private' gives the type of file
3957 */
Ben Blum102a7752009-09-23 15:56:26 -07003958/* for hysterical raisins, we can't put this on the older files */
3959#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003960static struct cftype files[] = {
3961 {
3962 .name = "tasks",
3963 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003964 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003965 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003966 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003967 },
Ben Blum102a7752009-09-23 15:56:26 -07003968 {
3969 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3970 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003971 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003972 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003973 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003974 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003975 {
3976 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003977 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003978 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003979 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003980 {
3981 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3982 .write_string = cgroup_write_event_control,
3983 .mode = S_IWUGO,
3984 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003985 {
3986 .name = "cgroup.clone_children",
3987 .read_u64 = cgroup_clone_children_read,
3988 .write_u64 = cgroup_clone_children_write,
3989 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003990 {
3991 .name = "release_agent",
3992 .flags = CFTYPE_ONLY_ON_ROOT,
3993 .read_seq_string = cgroup_release_agent_show,
3994 .write_string = cgroup_release_agent_write,
3995 .max_write_len = PATH_MAX,
3996 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003997 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003998};
3999
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004000/**
4001 * cgroup_populate_dir - selectively creation of files in a directory
4002 * @cgrp: target cgroup
4003 * @base_files: true if the base files should be added
4004 * @subsys_mask: mask of the subsystem ids whose files should be added
4005 */
4006static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4007 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004008{
4009 int err;
4010 struct cgroup_subsys *ss;
4011
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004012 if (base_files) {
4013 err = cgroup_addrm_files(cgrp, NULL, files, true);
4014 if (err < 0)
4015 return err;
4016 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004017
Tejun Heo8e3f6542012-04-01 12:09:55 -07004018 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004019 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004020 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004021 if (!test_bit(ss->subsys_id, &subsys_mask))
4022 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004023
Tejun Heodb0416b2012-04-01 12:09:55 -07004024 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004025 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004026 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004027
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004028 /* This cgroup is ready now */
4029 for_each_subsys(cgrp->root, ss) {
4030 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4031 /*
4032 * Update id->css pointer and make this css visible from
4033 * CSS ID functions. This pointer will be dereferened
4034 * from RCU-read-side without locks.
4035 */
4036 if (css->id)
4037 rcu_assign_pointer(css->id->css, css);
4038 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004039
4040 return 0;
4041}
4042
Tejun Heo48ddbe12012-04-01 12:09:56 -07004043static void css_dput_fn(struct work_struct *work)
4044{
4045 struct cgroup_subsys_state *css =
4046 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004047 struct dentry *dentry = css->cgroup->dentry;
4048 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004049
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004050 atomic_inc(&sb->s_active);
4051 dput(dentry);
4052 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004053}
4054
Paul Menageddbcc7e2007-10-18 23:39:30 -07004055static void init_cgroup_css(struct cgroup_subsys_state *css,
4056 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004057 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004058{
Paul Menagebd89aab2007-10-18 23:40:44 -07004059 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004060 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004061 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004062 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004063 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004064 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004065 BUG_ON(cgrp->subsys[ss->subsys_id]);
4066 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004067
4068 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004069 * css holds an extra ref to @cgrp->dentry which is put on the last
4070 * css_put(). dput() requires process context, which css_put() may
4071 * be called without. @css->dput_work will be used to invoke
4072 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004073 */
4074 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004075}
4076
Tejun Heob1929db2012-11-19 08:13:38 -08004077/* invoke ->post_create() on a new CSS and mark it online if successful */
4078static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004079{
Tejun Heob1929db2012-11-19 08:13:38 -08004080 int ret = 0;
4081
Tejun Heoa31f2d32012-11-19 08:13:37 -08004082 lockdep_assert_held(&cgroup_mutex);
4083
Tejun Heo92fb9742012-11-19 08:13:38 -08004084 if (ss->css_online)
4085 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004086 if (!ret)
4087 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4088 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004089}
4090
4091/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4092static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4093 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4094{
4095 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4096
4097 lockdep_assert_held(&cgroup_mutex);
4098
4099 if (!(css->flags & CSS_ONLINE))
4100 return;
4101
4102 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004103 * css_offline() should be called with cgroup_mutex unlocked. See
Tejun Heoa31f2d32012-11-19 08:13:37 -08004104 * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for
4105 * details. This temporary unlocking should go away once
4106 * cgroup_mutex is unexported from controllers.
4107 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004108 if (ss->css_offline) {
Tejun Heoa31f2d32012-11-19 08:13:37 -08004109 mutex_unlock(&cgroup_mutex);
Tejun Heo92fb9742012-11-19 08:13:38 -08004110 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004111 mutex_lock(&cgroup_mutex);
4112 }
4113
4114 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4115}
4116
Paul Menageddbcc7e2007-10-18 23:39:30 -07004117/*
Li Zefana043e3b2008-02-23 15:24:09 -08004118 * cgroup_create - create a cgroup
4119 * @parent: cgroup that will be parent of the new cgroup
4120 * @dentry: dentry of the new cgroup
4121 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004122 *
Li Zefana043e3b2008-02-23 15:24:09 -08004123 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004124 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004125static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004126 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004127{
Paul Menagebd89aab2007-10-18 23:40:44 -07004128 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004129 struct cgroupfs_root *root = parent->root;
4130 int err = 0;
4131 struct cgroup_subsys *ss;
4132 struct super_block *sb = root->sb;
4133
Tejun Heo0a950f62012-11-19 09:02:12 -08004134 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004135 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4136 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004137 return -ENOMEM;
4138
Tejun Heo0a950f62012-11-19 09:02:12 -08004139 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4140 if (cgrp->id < 0)
4141 goto err_free_cgrp;
4142
Tejun Heo976c06b2012-11-05 09:16:59 -08004143 /*
4144 * Only live parents can have children. Note that the liveliness
4145 * check isn't strictly necessary because cgroup_mkdir() and
4146 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4147 * anyway so that locking is contained inside cgroup proper and we
4148 * don't get nasty surprises if we ever grow another caller.
4149 */
4150 if (!cgroup_lock_live_group(parent)) {
4151 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004152 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004153 }
4154
Paul Menageddbcc7e2007-10-18 23:39:30 -07004155 /* Grab a reference on the superblock so the hierarchy doesn't
4156 * get deleted on unmount if there are child cgroups. This
4157 * can be done outside cgroup_mutex, since the sb can't
4158 * disappear while someone has an open control file on the
4159 * fs */
4160 atomic_inc(&sb->s_active);
4161
Paul Menagecc31edc2008-10-18 20:28:04 -07004162 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004163
Paul Menagebd89aab2007-10-18 23:40:44 -07004164 cgrp->parent = parent;
4165 cgrp->root = parent->root;
4166 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004167
Li Zefanb6abdb02008-03-04 14:28:19 -08004168 if (notify_on_release(parent))
4169 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4170
Tejun Heo2260e7f2012-11-19 08:13:38 -08004171 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4172 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004173
Paul Menageddbcc7e2007-10-18 23:39:30 -07004174 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004175 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004176
Tejun Heo92fb9742012-11-19 08:13:38 -08004177 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004178 if (IS_ERR(css)) {
4179 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004180 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004181 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004182 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004183 if (ss->use_id) {
4184 err = alloc_css_id(ss, parent, cgrp);
4185 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004186 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004187 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004188 }
4189
Tejun Heo4e139af2012-11-19 08:13:36 -08004190 /*
4191 * Create directory. cgroup_create_file() returns with the new
4192 * directory locked on success so that it can be populated without
4193 * dropping cgroup_mutex.
4194 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004195 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004196 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004197 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004198 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004199
Tejun Heo4e139af2012-11-19 08:13:36 -08004200 /* allocation complete, commit to creation */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004201 dentry->d_fsdata = cgrp;
Tejun Heofebfcef2012-11-19 08:13:36 -08004202 cgrp->dentry = dentry;
Tejun Heo4e139af2012-11-19 08:13:36 -08004203 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4204 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4205 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004206
Tejun Heob1929db2012-11-19 08:13:38 -08004207 /* each css holds a ref to the cgroup's dentry */
4208 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004209 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004210
Tejun Heob1929db2012-11-19 08:13:38 -08004211 /* creation succeeded, notify subsystems */
4212 for_each_subsys(root, ss) {
4213 err = online_css(ss, cgrp);
4214 if (err)
4215 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004216
4217 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4218 parent->parent) {
4219 pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4220 current->comm, current->pid, ss->name);
4221 if (!strcmp(ss->name, "memory"))
4222 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4223 ss->warned_broken_hierarchy = true;
4224 }
Tejun Heoa8638032012-11-09 09:12:29 -08004225 }
4226
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004227 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004228 if (err)
4229 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004230
4231 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004232 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004233
4234 return 0;
4235
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004236err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004237 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004238 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004239 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004240 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004241 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004242 /* Release the reference count that we took on the superblock */
4243 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004244err_free_id:
4245 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004246err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004247 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004248 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004249
4250err_destroy:
4251 cgroup_destroy_locked(cgrp);
4252 mutex_unlock(&cgroup_mutex);
4253 mutex_unlock(&dentry->d_inode->i_mutex);
4254 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004255}
4256
Al Viro18bb1db2011-07-26 01:41:39 -04004257static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004258{
4259 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4260
4261 /* the vfs holds inode->i_mutex already */
4262 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4263}
4264
Tejun Heo28b4c272012-04-01 12:09:56 -07004265/*
4266 * Check the reference count on each subsystem. Since we already
4267 * established that there are no tasks in the cgroup, if the css refcount
4268 * is also 1, then there should be no outstanding references, so the
4269 * subsystem is safe to destroy. We scan across all subsystems rather than
4270 * using the per-hierarchy linked list of mounted subsystems since we can
4271 * be called via check_for_release() with no synchronization other than
4272 * RCU, and the subsystem linked list isn't RCU-safe.
4273 */
Li Zefan55b6fd02008-07-29 22:33:20 -07004274static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004275{
Paul Menage81a6a5c2007-10-18 23:39:38 -07004276 int i;
Tejun Heo28b4c272012-04-01 12:09:56 -07004277
Ben Blumaae8aab2010-03-10 15:22:07 -08004278 /*
4279 * We won't need to lock the subsys array, because the subsystems
4280 * we're concerned about aren't going anywhere since our cgroup root
4281 * has a reference on them.
4282 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004283 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4284 struct cgroup_subsys *ss = subsys[i];
4285 struct cgroup_subsys_state *css;
Tejun Heo28b4c272012-04-01 12:09:56 -07004286
Ben Blumaae8aab2010-03-10 15:22:07 -08004287 /* Skip subsystems not present or not in this hierarchy */
4288 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004289 continue;
Tejun Heo28b4c272012-04-01 12:09:56 -07004290
Paul Menagebd89aab2007-10-18 23:40:44 -07004291 css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004292 /*
4293 * When called from check_for_release() it's possible
Paul Menage81a6a5c2007-10-18 23:39:38 -07004294 * that by this point the cgroup has been removed
4295 * and the css deleted. But a false-positive doesn't
4296 * matter, since it can only happen if the cgroup
4297 * has been deleted and hence no longer needs the
Tejun Heo28b4c272012-04-01 12:09:56 -07004298 * release agent to be called anyway.
4299 */
4300 if (css && css_refcnt(css) > 1)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004301 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004302 }
4303 return 0;
4304}
4305
Tejun Heo42809dd2012-11-19 08:13:37 -08004306static int cgroup_destroy_locked(struct cgroup *cgrp)
4307 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004308{
Tejun Heo42809dd2012-11-19 08:13:37 -08004309 struct dentry *d = cgrp->dentry;
4310 struct cgroup *parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004311 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004312 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004313 struct cgroup_subsys *ss;
Greg Thelen205a8722012-11-28 13:50:44 -08004314 LIST_HEAD(tmp_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004315
Tejun Heo42809dd2012-11-19 08:13:37 -08004316 lockdep_assert_held(&d->d_inode->i_mutex);
4317 lockdep_assert_held(&cgroup_mutex);
4318
4319 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004320 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004321
Tejun Heo1a90dd52012-11-05 09:16:59 -08004322 /*
4323 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4324 * removed. This makes future css_tryget() and child creation
4325 * attempts fail thus maintaining the removal conditions verified
4326 * above.
4327 */
Tejun Heoed9577932012-11-05 09:16:58 -08004328 for_each_subsys(cgrp->root, ss) {
4329 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4330
4331 WARN_ON(atomic_read(&css->refcnt) < 0);
4332 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004333 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004334 set_bit(CGRP_REMOVED, &cgrp->flags);
4335
Tejun Heoa31f2d32012-11-19 08:13:37 -08004336 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004337 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004338 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004339
4340 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004341 * Put all the base refs. Each css holds an extra reference to the
4342 * cgroup's dentry and cgroup removal proceeds regardless of css
4343 * refs. On the last put of each css, whenever that may be, the
4344 * extra dentry ref is put so that dentry destruction happens only
4345 * after all css's are released.
4346 */
Tejun Heoe9316082012-11-05 09:16:58 -08004347 for_each_subsys(cgrp->root, ss)
4348 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004349
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004350 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004351 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004352 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004353 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004354
Paul Menage999cd8a2009-01-07 18:08:36 -08004355 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004356 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004357 list_del_init(&cgrp->allcg_node);
4358
Tejun Heo42809dd2012-11-19 08:13:37 -08004359 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004360 cgroup_d_remove_dir(d);
4361 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004362
Paul Menagebd89aab2007-10-18 23:40:44 -07004363 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004364 check_for_release(parent);
4365
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004366 /*
4367 * Unregister events and notify userspace.
4368 * Notify userspace about cgroup removing only after rmdir of cgroup
Greg Thelen205a8722012-11-28 13:50:44 -08004369 * directory to avoid race between userspace and kernelspace. Use
4370 * a temporary list to avoid a deadlock with cgroup_event_wake(). Since
4371 * cgroup_event_wake() is called with the wait queue head locked,
4372 * remove_wait_queue() cannot be called while holding event_list_lock.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004373 */
4374 spin_lock(&cgrp->event_list_lock);
Greg Thelen205a8722012-11-28 13:50:44 -08004375 list_splice_init(&cgrp->event_list, &tmp_list);
4376 spin_unlock(&cgrp->event_list_lock);
4377 list_for_each_entry_safe(event, tmp, &tmp_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004378 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004379 remove_wait_queue(event->wqh, &event->wait);
4380 eventfd_signal(event->eventfd, 1);
4381 schedule_work(&event->remove);
4382 }
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004383
Paul Menageddbcc7e2007-10-18 23:39:30 -07004384 return 0;
4385}
4386
Tejun Heo42809dd2012-11-19 08:13:37 -08004387static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4388{
4389 int ret;
4390
4391 mutex_lock(&cgroup_mutex);
4392 ret = cgroup_destroy_locked(dentry->d_fsdata);
4393 mutex_unlock(&cgroup_mutex);
4394
4395 return ret;
4396}
4397
Tejun Heo8e3f6542012-04-01 12:09:55 -07004398static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4399{
4400 INIT_LIST_HEAD(&ss->cftsets);
4401
4402 /*
4403 * base_cftset is embedded in subsys itself, no need to worry about
4404 * deregistration.
4405 */
4406 if (ss->base_cftypes) {
4407 ss->base_cftset.cfts = ss->base_cftypes;
4408 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4409 }
4410}
4411
Li Zefan06a11922008-04-29 01:00:07 -07004412static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004413{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004414 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004415
4416 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004417
Tejun Heo648bb562012-11-19 08:13:36 -08004418 mutex_lock(&cgroup_mutex);
4419
Tejun Heo8e3f6542012-04-01 12:09:55 -07004420 /* init base cftset */
4421 cgroup_init_cftsets(ss);
4422
Paul Menageddbcc7e2007-10-18 23:39:30 -07004423 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004424 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004425 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004426 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004427 /* We don't handle early failures gracefully */
4428 BUG_ON(IS_ERR(css));
4429 init_cgroup_css(css, ss, dummytop);
4430
Li Zefane8d55fd2008-04-29 01:00:13 -07004431 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004432 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004433 * newly registered, all tasks and hence the
4434 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004435 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004436
4437 need_forkexit_callback |= ss->fork || ss->exit;
4438
Li Zefane8d55fd2008-04-29 01:00:13 -07004439 /* At system boot, before all subsystems have been
4440 * registered, no tasks have been forked, so we don't
4441 * need to invoke fork callbacks here. */
4442 BUG_ON(!list_empty(&init_task.tasks));
4443
Paul Menageddbcc7e2007-10-18 23:39:30 -07004444 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004445 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004446
Tejun Heo648bb562012-11-19 08:13:36 -08004447 mutex_unlock(&cgroup_mutex);
4448
Ben Blume6a11052010-03-10 15:22:09 -08004449 /* this function shouldn't be used with modular subsystems, since they
4450 * need to register a subsys_id, among other things */
4451 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004452}
4453
4454/**
Ben Blume6a11052010-03-10 15:22:09 -08004455 * cgroup_load_subsys: load and register a modular subsystem at runtime
4456 * @ss: the subsystem to load
4457 *
4458 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004459 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004460 * up for use. If the subsystem is built-in anyway, work is delegated to the
4461 * simpler cgroup_init_subsys.
4462 */
4463int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4464{
Ben Blume6a11052010-03-10 15:22:09 -08004465 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004466 int i, ret;
Ben Blume6a11052010-03-10 15:22:09 -08004467
4468 /* check name and function validity */
4469 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004470 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004471 return -EINVAL;
4472
4473 /*
4474 * we don't support callbacks in modular subsystems. this check is
4475 * before the ss->module check for consistency; a subsystem that could
4476 * be a module should still have no callbacks even if the user isn't
4477 * compiling it as one.
4478 */
4479 if (ss->fork || ss->exit)
4480 return -EINVAL;
4481
4482 /*
4483 * an optionally modular subsystem is built-in: we want to do nothing,
4484 * since cgroup_init_subsys will have already taken care of it.
4485 */
4486 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004487 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004488 BUG_ON(subsys[ss->subsys_id] != ss);
4489 return 0;
4490 }
4491
Tejun Heo8e3f6542012-04-01 12:09:55 -07004492 /* init base cftset */
4493 cgroup_init_cftsets(ss);
4494
Ben Blume6a11052010-03-10 15:22:09 -08004495 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004496 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004497
4498 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004499 * no ss->css_alloc seems to need anything important in the ss
4500 * struct, so this can happen first (i.e. before the rootnode
4501 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004502 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004503 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004504 if (IS_ERR(css)) {
4505 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004506 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004507 mutex_unlock(&cgroup_mutex);
4508 return PTR_ERR(css);
4509 }
4510
4511 list_add(&ss->sibling, &rootnode.subsys_list);
4512 ss->root = &rootnode;
4513
4514 /* our new subsystem will be attached to the dummy hierarchy. */
4515 init_cgroup_css(css, ss, dummytop);
4516 /* init_idr must be after init_cgroup_css because it sets css->id. */
4517 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004518 ret = cgroup_init_idr(ss, css);
4519 if (ret)
4520 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004521 }
4522
4523 /*
4524 * Now we need to entangle the css into the existing css_sets. unlike
4525 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4526 * will need a new pointer to it; done by iterating the css_set_table.
4527 * furthermore, modifying the existing css_sets will corrupt the hash
4528 * table state, so each changed css_set will need its hash recomputed.
4529 * this is all done under the css_set_lock.
4530 */
4531 write_lock(&css_set_lock);
4532 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4533 struct css_set *cg;
4534 struct hlist_node *node, *tmp;
4535 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4536
4537 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4538 /* skip entries that we already rehashed */
4539 if (cg->subsys[ss->subsys_id])
4540 continue;
4541 /* remove existing entry */
4542 hlist_del(&cg->hlist);
4543 /* set new value */
4544 cg->subsys[ss->subsys_id] = css;
4545 /* recompute hash and restore entry */
4546 new_bucket = css_set_hash(cg->subsys);
4547 hlist_add_head(&cg->hlist, new_bucket);
4548 }
4549 }
4550 write_unlock(&css_set_lock);
4551
Ben Blume6a11052010-03-10 15:22:09 -08004552 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004553 ret = online_css(ss, dummytop);
4554 if (ret)
4555 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004556
Ben Blume6a11052010-03-10 15:22:09 -08004557 /* success! */
4558 mutex_unlock(&cgroup_mutex);
4559 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004560
4561err_unload:
4562 mutex_unlock(&cgroup_mutex);
4563 /* @ss can't be mounted here as try_module_get() would fail */
4564 cgroup_unload_subsys(ss);
4565 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004566}
4567EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4568
4569/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004570 * cgroup_unload_subsys: unload a modular subsystem
4571 * @ss: the subsystem to unload
4572 *
4573 * This function should be called in a modular subsystem's exitcall. When this
4574 * function is invoked, the refcount on the subsystem's module will be 0, so
4575 * the subsystem will not be attached to any hierarchy.
4576 */
4577void cgroup_unload_subsys(struct cgroup_subsys *ss)
4578{
4579 struct cg_cgroup_link *link;
4580 struct hlist_head *hhead;
4581
4582 BUG_ON(ss->module == NULL);
4583
4584 /*
4585 * we shouldn't be called if the subsystem is in use, and the use of
4586 * try_module_get in parse_cgroupfs_options should ensure that it
4587 * doesn't start being used while we're killing it off.
4588 */
4589 BUG_ON(ss->root != &rootnode);
4590
4591 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004592
Tejun Heoa31f2d32012-11-19 08:13:37 -08004593 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004594 ss->active = 0;
4595
4596 if (ss->use_id) {
4597 idr_remove_all(&ss->idr);
4598 idr_destroy(&ss->idr);
4599 }
4600
Ben Blumcf5d5942010-03-10 15:22:09 -08004601 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004602 subsys[ss->subsys_id] = NULL;
4603
4604 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004605 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004606
4607 /*
4608 * disentangle the css from all css_sets attached to the dummytop. as
4609 * in loading, we need to pay our respects to the hashtable gods.
4610 */
4611 write_lock(&css_set_lock);
4612 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4613 struct css_set *cg = link->cg;
4614
4615 hlist_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004616 cg->subsys[ss->subsys_id] = NULL;
4617 hhead = css_set_hash(cg->subsys);
4618 hlist_add_head(&cg->hlist, hhead);
4619 }
4620 write_unlock(&css_set_lock);
4621
4622 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004623 * remove subsystem's css from the dummytop and free it - need to
4624 * free before marking as null because ss->css_free needs the
4625 * cgrp->subsys pointer to find their state. note that this also
4626 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004627 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004628 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004629 dummytop->subsys[ss->subsys_id] = NULL;
4630
4631 mutex_unlock(&cgroup_mutex);
4632}
4633EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4634
4635/**
Li Zefana043e3b2008-02-23 15:24:09 -08004636 * cgroup_init_early - cgroup initialization at system boot
4637 *
4638 * Initialize cgroups at system boot, and initialize any
4639 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004640 */
4641int __init cgroup_init_early(void)
4642{
4643 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004644 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004645 INIT_LIST_HEAD(&init_css_set.cg_links);
4646 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004647 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004648 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004649 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004650 root_count = 1;
4651 init_task.cgroups = &init_css_set;
4652
4653 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004654 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004655 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004656 &rootnode.top_cgroup.css_sets);
4657 list_add(&init_css_set_link.cg_link_list,
4658 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004659
Li Zefan472b1052008-04-29 01:00:11 -07004660 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4661 INIT_HLIST_HEAD(&css_set_table[i]);
4662
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];
4665
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004666 /* at bootup time, we don't worry about modular subsystems */
4667 if (!ss || ss->module)
4668 continue;
4669
Paul Menageddbcc7e2007-10-18 23:39:30 -07004670 BUG_ON(!ss->name);
4671 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004672 BUG_ON(!ss->css_alloc);
4673 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004674 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004675 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004676 ss->name, ss->subsys_id);
4677 BUG();
4678 }
4679
4680 if (ss->early_init)
4681 cgroup_init_subsys(ss);
4682 }
4683 return 0;
4684}
4685
4686/**
Li Zefana043e3b2008-02-23 15:24:09 -08004687 * cgroup_init - cgroup initialization
4688 *
4689 * Register cgroup filesystem and /proc file, and initialize
4690 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004691 */
4692int __init cgroup_init(void)
4693{
4694 int err;
4695 int i;
Li Zefan472b1052008-04-29 01:00:11 -07004696 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07004697
4698 err = bdi_init(&cgroup_backing_dev_info);
4699 if (err)
4700 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004701
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004702 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004703 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004704
4705 /* at bootup time, we don't worry about modular subsystems */
4706 if (!ss || ss->module)
4707 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004708 if (!ss->early_init)
4709 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004710 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004711 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004712 }
4713
Li Zefan472b1052008-04-29 01:00:11 -07004714 /* Add init_css_set to the hash table */
4715 hhead = css_set_hash(init_css_set.subsys);
4716 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004717 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004718
4719 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4720 if (!cgroup_kobj) {
4721 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004722 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004723 }
4724
4725 err = register_filesystem(&cgroup_fs_type);
4726 if (err < 0) {
4727 kobject_put(cgroup_kobj);
4728 goto out;
4729 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004730
Li Zefan46ae2202008-04-29 01:00:08 -07004731 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004732
Paul Menageddbcc7e2007-10-18 23:39:30 -07004733out:
Paul Menagea4243162007-10-18 23:39:35 -07004734 if (err)
4735 bdi_destroy(&cgroup_backing_dev_info);
4736
Paul Menageddbcc7e2007-10-18 23:39:30 -07004737 return err;
4738}
Paul Menageb4f48b62007-10-18 23:39:33 -07004739
Paul Menagea4243162007-10-18 23:39:35 -07004740/*
4741 * proc_cgroup_show()
4742 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4743 * - Used for /proc/<pid>/cgroup.
4744 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4745 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004746 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004747 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4748 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4749 * cgroup to top_cgroup.
4750 */
4751
4752/* TODO: Use a proper seq_file iterator */
4753static int proc_cgroup_show(struct seq_file *m, void *v)
4754{
4755 struct pid *pid;
4756 struct task_struct *tsk;
4757 char *buf;
4758 int retval;
4759 struct cgroupfs_root *root;
4760
4761 retval = -ENOMEM;
4762 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4763 if (!buf)
4764 goto out;
4765
4766 retval = -ESRCH;
4767 pid = m->private;
4768 tsk = get_pid_task(pid, PIDTYPE_PID);
4769 if (!tsk)
4770 goto out_free;
4771
4772 retval = 0;
4773
4774 mutex_lock(&cgroup_mutex);
4775
Li Zefane5f6a862009-01-07 18:07:41 -08004776 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004777 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004778 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004779 int count = 0;
4780
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004781 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004782 for_each_subsys(root, ss)
4783 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004784 if (strlen(root->name))
4785 seq_printf(m, "%sname=%s", count ? "," : "",
4786 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004787 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004788 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004789 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004790 if (retval < 0)
4791 goto out_unlock;
4792 seq_puts(m, buf);
4793 seq_putc(m, '\n');
4794 }
4795
4796out_unlock:
4797 mutex_unlock(&cgroup_mutex);
4798 put_task_struct(tsk);
4799out_free:
4800 kfree(buf);
4801out:
4802 return retval;
4803}
4804
4805static int cgroup_open(struct inode *inode, struct file *file)
4806{
4807 struct pid *pid = PROC_I(inode)->pid;
4808 return single_open(file, proc_cgroup_show, pid);
4809}
4810
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004811const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004812 .open = cgroup_open,
4813 .read = seq_read,
4814 .llseek = seq_lseek,
4815 .release = single_release,
4816};
4817
4818/* Display information about each subsystem and each hierarchy */
4819static int proc_cgroupstats_show(struct seq_file *m, void *v)
4820{
4821 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004822
Paul Menage8bab8dd2008-04-04 14:29:57 -07004823 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004824 /*
4825 * ideally we don't want subsystems moving around while we do this.
4826 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4827 * subsys/hierarchy state.
4828 */
Paul Menagea4243162007-10-18 23:39:35 -07004829 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004830 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4831 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004832 if (ss == NULL)
4833 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004834 seq_printf(m, "%s\t%d\t%d\t%d\n",
4835 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004836 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004837 }
4838 mutex_unlock(&cgroup_mutex);
4839 return 0;
4840}
4841
4842static int cgroupstats_open(struct inode *inode, struct file *file)
4843{
Al Viro9dce07f2008-03-29 03:07:28 +00004844 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004845}
4846
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004847static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004848 .open = cgroupstats_open,
4849 .read = seq_read,
4850 .llseek = seq_lseek,
4851 .release = single_release,
4852};
4853
Paul Menageb4f48b62007-10-18 23:39:33 -07004854/**
4855 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004856 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004857 *
4858 * Description: A task inherits its parent's cgroup at fork().
4859 *
4860 * A pointer to the shared css_set was automatically copied in
4861 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004862 * it was not made under the protection of RCU or cgroup_mutex, so
4863 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4864 * have already changed current->cgroups, allowing the previously
4865 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004866 *
4867 * At the point that cgroup_fork() is called, 'current' is the parent
4868 * task, and the passed argument 'child' points to the child task.
4869 */
4870void cgroup_fork(struct task_struct *child)
4871{
Tejun Heo9bb71302012-10-18 17:52:07 -07004872 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004873 child->cgroups = current->cgroups;
4874 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004875 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004876 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004877}
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 *
Tejun Heo5edee612012-10-16 15:03:14 -07004883 * Adds the task to the list running through its css_set if necessary and
4884 * call the subsystem fork() callbacks. Has to be after the task is
4885 * visible on the task list in case we race with the first call to
4886 * cgroup_iter_start() - to guarantee that the new task ends up on its
4887 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004888 */
Paul Menage817929e2007-10-18 23:39:36 -07004889void cgroup_post_fork(struct task_struct *child)
4890{
Tejun Heo5edee612012-10-16 15:03:14 -07004891 int i;
4892
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004893 /*
4894 * use_task_css_set_links is set to 1 before we walk the tasklist
4895 * under the tasklist_lock and we read it here after we added the child
4896 * to the tasklist under the tasklist_lock as well. If the child wasn't
4897 * yet in the tasklist when we walked through it from
4898 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4899 * should be visible now due to the paired locking and barriers implied
4900 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4901 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4902 * lock on fork.
4903 */
Paul Menage817929e2007-10-18 23:39:36 -07004904 if (use_task_css_set_links) {
4905 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004906 task_lock(child);
4907 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004908 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004909 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004910 write_unlock(&css_set_lock);
4911 }
Tejun Heo5edee612012-10-16 15:03:14 -07004912
4913 /*
4914 * Call ss->fork(). This must happen after @child is linked on
4915 * css_set; otherwise, @child might change state between ->fork()
4916 * and addition to css_set.
4917 */
4918 if (need_forkexit_callback) {
4919 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4920 struct cgroup_subsys *ss = subsys[i];
4921
4922 /*
4923 * fork/exit callbacks are supported only for
4924 * builtin subsystems and we don't need further
4925 * synchronization as they never go away.
4926 */
4927 if (!ss || ss->module)
4928 continue;
4929
4930 if (ss->fork)
4931 ss->fork(child);
4932 }
4933 }
Paul Menage817929e2007-10-18 23:39:36 -07004934}
Tejun Heo5edee612012-10-16 15:03:14 -07004935
Paul Menage817929e2007-10-18 23:39:36 -07004936/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004937 * cgroup_exit - detach cgroup from exiting task
4938 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004939 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004940 *
4941 * Description: Detach cgroup from @tsk and release it.
4942 *
4943 * Note that cgroups marked notify_on_release force every task in
4944 * them to take the global cgroup_mutex mutex when exiting.
4945 * This could impact scaling on very large systems. Be reluctant to
4946 * use notify_on_release cgroups where very high task exit scaling
4947 * is required on large systems.
4948 *
4949 * the_top_cgroup_hack:
4950 *
4951 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4952 *
4953 * We call cgroup_exit() while the task is still competent to
4954 * handle notify_on_release(), then leave the task attached to the
4955 * root cgroup in each hierarchy for the remainder of its exit.
4956 *
4957 * To do this properly, we would increment the reference count on
4958 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4959 * code we would add a second cgroup function call, to drop that
4960 * reference. This would just create an unnecessary hot spot on
4961 * the top_cgroup reference count, to no avail.
4962 *
4963 * Normally, holding a reference to a cgroup without bumping its
4964 * count is unsafe. The cgroup could go away, or someone could
4965 * attach us to a different cgroup, decrementing the count on
4966 * the first cgroup that we never incremented. But in this case,
4967 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004968 * which wards off any cgroup_attach_task() attempts, or task is a failed
4969 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004970 */
4971void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4972{
Paul Menage817929e2007-10-18 23:39:36 -07004973 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004974 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004975
4976 /*
4977 * Unlink from the css_set task list if necessary.
4978 * Optimistically check cg_list before taking
4979 * css_set_lock
4980 */
4981 if (!list_empty(&tsk->cg_list)) {
4982 write_lock(&css_set_lock);
4983 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004984 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004985 write_unlock(&css_set_lock);
4986 }
4987
Paul Menageb4f48b62007-10-18 23:39:33 -07004988 /* Reassign the task to the init_css_set. */
4989 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004990 cg = tsk->cgroups;
4991 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004992
4993 if (run_callbacks && need_forkexit_callback) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004994 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004995 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004996
4997 /* modular subsystems can't use callbacks */
4998 if (!ss || ss->module)
4999 continue;
5000
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005001 if (ss->exit) {
5002 struct cgroup *old_cgrp =
5003 rcu_dereference_raw(cg->subsys[i])->cgroup;
5004 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08005005 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005006 }
5007 }
5008 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005009 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005010
Paul Menage817929e2007-10-18 23:39:36 -07005011 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005012 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07005013}
Paul Menage697f4162007-10-18 23:39:34 -07005014
5015/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005016 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08005017 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005018 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08005019 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005020 * See if @cgrp is a descendant of @task's cgroup in the appropriate
5021 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07005022 *
5023 * If we are sending in dummytop, then presumably we are creating
5024 * the top cgroup in the subsystem.
5025 *
5026 * Called only by the ns (nsproxy) cgroup.
5027 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005028int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07005029{
5030 int ret;
5031 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07005032
Paul Menagebd89aab2007-10-18 23:40:44 -07005033 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07005034 return 1;
5035
Paul Menage7717f7b2009-09-23 15:56:22 -07005036 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005037 while (cgrp != target && cgrp!= cgrp->top_cgroup)
5038 cgrp = cgrp->parent;
5039 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07005040 return ret;
5041}
Paul Menage81a6a5c2007-10-18 23:39:38 -07005042
Paul Menagebd89aab2007-10-18 23:40:44 -07005043static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005044{
5045 /* All of these checks rely on RCU to keep the cgroup
5046 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07005047 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
5048 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07005049 /* Control Group is currently removeable. If it's not
5050 * already queued for a userspace notification, queue
5051 * it now */
5052 int need_schedule_work = 0;
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005053 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07005054 if (!cgroup_is_removed(cgrp) &&
5055 list_empty(&cgrp->release_list)) {
5056 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005057 need_schedule_work = 1;
5058 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005059 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005060 if (need_schedule_work)
5061 schedule_work(&release_agent_work);
5062 }
5063}
5064
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005065/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005066bool __css_tryget(struct cgroup_subsys_state *css)
5067{
Tejun Heoe9316082012-11-05 09:16:58 -08005068 while (true) {
5069 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005070
Tejun Heoe9316082012-11-05 09:16:58 -08005071 v = css_refcnt(css);
5072 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5073 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005074 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005075 else if (t < 0)
5076 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005077 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005078 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005079}
5080EXPORT_SYMBOL_GPL(__css_tryget);
5081
5082/* Caller must verify that the css is not for root cgroup */
5083void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005084{
Paul Menagebd89aab2007-10-18 23:40:44 -07005085 struct cgroup *cgrp = css->cgroup;
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005086 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005087
Paul Menage81a6a5c2007-10-18 23:39:38 -07005088 rcu_read_lock();
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005089 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
5090
5091 switch (v) {
Tejun Heo48ddbe12012-04-01 12:09:56 -07005092 case 1:
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005093 if (notify_on_release(cgrp)) {
5094 set_bit(CGRP_RELEASABLE, &cgrp->flags);
5095 check_for_release(cgrp);
5096 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07005097 break;
5098 case 0:
Tejun Heoed9577932012-11-05 09:16:58 -08005099 schedule_work(&css->dput_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005100 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005101 }
5102 rcu_read_unlock();
5103}
Ben Blum67523c42010-03-10 15:22:11 -08005104EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005105
5106/*
5107 * Notify userspace when a cgroup is released, by running the
5108 * configured release agent with the name of the cgroup (path
5109 * relative to the root of cgroup file system) as the argument.
5110 *
5111 * Most likely, this user command will try to rmdir this cgroup.
5112 *
5113 * This races with the possibility that some other task will be
5114 * attached to this cgroup before it is removed, or that some other
5115 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5116 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5117 * unused, and this cgroup will be reprieved from its death sentence,
5118 * to continue to serve a useful existence. Next time it's released,
5119 * we will get notified again, if it still has 'notify_on_release' set.
5120 *
5121 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5122 * means only wait until the task is successfully execve()'d. The
5123 * separate release agent task is forked by call_usermodehelper(),
5124 * then control in this thread returns here, without waiting for the
5125 * release agent task. We don't bother to wait because the caller of
5126 * this routine has no use for the exit status of the release agent
5127 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005128 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005129static void cgroup_release_agent(struct work_struct *work)
5130{
5131 BUG_ON(work != &release_agent_work);
5132 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005133 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005134 while (!list_empty(&release_list)) {
5135 char *argv[3], *envp[3];
5136 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005137 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005138 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005139 struct cgroup,
5140 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005141 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005142 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005143 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005144 if (!pathbuf)
5145 goto continue_free;
5146 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5147 goto continue_free;
5148 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5149 if (!agentbuf)
5150 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005151
5152 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005153 argv[i++] = agentbuf;
5154 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005155 argv[i] = NULL;
5156
5157 i = 0;
5158 /* minimal command environment */
5159 envp[i++] = "HOME=/";
5160 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5161 envp[i] = NULL;
5162
5163 /* Drop the lock while we invoke the usermode helper,
5164 * since the exec could involve hitting disk and hence
5165 * be a slow process */
5166 mutex_unlock(&cgroup_mutex);
5167 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005168 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005169 continue_free:
5170 kfree(pathbuf);
5171 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005172 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005173 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005174 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005175 mutex_unlock(&cgroup_mutex);
5176}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005177
5178static int __init cgroup_disable(char *str)
5179{
5180 int i;
5181 char *token;
5182
5183 while ((token = strsep(&str, ",")) != NULL) {
5184 if (!*token)
5185 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005186 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005187 struct cgroup_subsys *ss = subsys[i];
5188
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005189 /*
5190 * cgroup_disable, being at boot time, can't
5191 * know about module subsystems, so we don't
5192 * worry about them.
5193 */
5194 if (!ss || ss->module)
5195 continue;
5196
Paul Menage8bab8dd2008-04-04 14:29:57 -07005197 if (!strcmp(token, ss->name)) {
5198 ss->disabled = 1;
5199 printk(KERN_INFO "Disabling %s control group"
5200 " subsystem\n", ss->name);
5201 break;
5202 }
5203 }
5204 }
5205 return 1;
5206}
5207__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005208
5209/*
5210 * Functons for CSS ID.
5211 */
5212
5213/*
5214 *To get ID other than 0, this should be called when !cgroup_is_removed().
5215 */
5216unsigned short css_id(struct cgroup_subsys_state *css)
5217{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005218 struct css_id *cssid;
5219
5220 /*
5221 * This css_id() can return correct value when somone has refcnt
5222 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5223 * it's unchanged until freed.
5224 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005225 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005226
5227 if (cssid)
5228 return cssid->id;
5229 return 0;
5230}
Ben Blum67523c42010-03-10 15:22:11 -08005231EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005232
5233unsigned short css_depth(struct cgroup_subsys_state *css)
5234{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005235 struct css_id *cssid;
5236
Tejun Heo28b4c272012-04-01 12:09:56 -07005237 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005238
5239 if (cssid)
5240 return cssid->depth;
5241 return 0;
5242}
Ben Blum67523c42010-03-10 15:22:11 -08005243EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005244
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005245/**
5246 * css_is_ancestor - test "root" css is an ancestor of "child"
5247 * @child: the css to be tested.
5248 * @root: the css supporsed to be an ancestor of the child.
5249 *
5250 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005251 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005252 * But, considering usual usage, the csses should be valid objects after test.
5253 * Assuming that the caller will do some action to the child if this returns
5254 * returns true, the caller must take "child";s reference count.
5255 * If "child" is valid object and this returns true, "root" is valid, too.
5256 */
5257
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005258bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005259 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005260{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005261 struct css_id *child_id;
5262 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005263
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005264 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005265 if (!child_id)
5266 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005267 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005268 if (!root_id)
5269 return false;
5270 if (child_id->depth < root_id->depth)
5271 return false;
5272 if (child_id->stack[root_id->depth] != root_id->id)
5273 return false;
5274 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005275}
5276
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005277void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5278{
5279 struct css_id *id = css->id;
5280 /* When this is called before css_id initialization, id can be NULL */
5281 if (!id)
5282 return;
5283
5284 BUG_ON(!ss->use_id);
5285
5286 rcu_assign_pointer(id->css, NULL);
5287 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005288 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005289 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005290 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005291 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005292}
Ben Blum67523c42010-03-10 15:22:11 -08005293EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005294
5295/*
5296 * This is called by init or create(). Then, calls to this function are
5297 * always serialized (By cgroup_mutex() at create()).
5298 */
5299
5300static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5301{
5302 struct css_id *newid;
5303 int myid, error, size;
5304
5305 BUG_ON(!ss->use_id);
5306
5307 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5308 newid = kzalloc(size, GFP_KERNEL);
5309 if (!newid)
5310 return ERR_PTR(-ENOMEM);
5311 /* get id */
5312 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
5313 error = -ENOMEM;
5314 goto err_out;
5315 }
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005316 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005317 /* Don't use 0. allocates an ID of 1-65535 */
5318 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005319 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005320
5321 /* Returns error when there are no free spaces for new ID.*/
5322 if (error) {
5323 error = -ENOSPC;
5324 goto err_out;
5325 }
5326 if (myid > CSS_ID_MAX)
5327 goto remove_idr;
5328
5329 newid->id = myid;
5330 newid->depth = depth;
5331 return newid;
5332remove_idr:
5333 error = -ENOSPC;
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005334 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005335 idr_remove(&ss->idr, myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005336 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005337err_out:
5338 kfree(newid);
5339 return ERR_PTR(error);
5340
5341}
5342
Ben Blume6a11052010-03-10 15:22:09 -08005343static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5344 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005345{
5346 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005347
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005348 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005349 idr_init(&ss->idr);
5350
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005351 newid = get_new_cssid(ss, 0);
5352 if (IS_ERR(newid))
5353 return PTR_ERR(newid);
5354
5355 newid->stack[0] = newid->id;
5356 newid->css = rootcss;
5357 rootcss->id = newid;
5358 return 0;
5359}
5360
5361static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5362 struct cgroup *child)
5363{
5364 int subsys_id, i, depth = 0;
5365 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005366 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005367
5368 subsys_id = ss->subsys_id;
5369 parent_css = parent->subsys[subsys_id];
5370 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005371 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005372 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005373
5374 child_id = get_new_cssid(ss, depth);
5375 if (IS_ERR(child_id))
5376 return PTR_ERR(child_id);
5377
5378 for (i = 0; i < depth; i++)
5379 child_id->stack[i] = parent_id->stack[i];
5380 child_id->stack[depth] = child_id->id;
5381 /*
5382 * child_id->css pointer will be set after this cgroup is available
5383 * see cgroup_populate_dir()
5384 */
5385 rcu_assign_pointer(child_css->id, child_id);
5386
5387 return 0;
5388}
5389
5390/**
5391 * css_lookup - lookup css by id
5392 * @ss: cgroup subsys to be looked into.
5393 * @id: the id
5394 *
5395 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5396 * NULL if not. Should be called under rcu_read_lock()
5397 */
5398struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5399{
5400 struct css_id *cssid = NULL;
5401
5402 BUG_ON(!ss->use_id);
5403 cssid = idr_find(&ss->idr, id);
5404
5405 if (unlikely(!cssid))
5406 return NULL;
5407
5408 return rcu_dereference(cssid->css);
5409}
Ben Blum67523c42010-03-10 15:22:11 -08005410EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005411
5412/**
5413 * css_get_next - lookup next cgroup under specified hierarchy.
5414 * @ss: pointer to subsystem
5415 * @id: current position of iteration.
5416 * @root: pointer to css. search tree under this.
5417 * @foundid: position of found object.
5418 *
5419 * Search next css under the specified hierarchy of rootid. Calling under
5420 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5421 */
5422struct cgroup_subsys_state *
5423css_get_next(struct cgroup_subsys *ss, int id,
5424 struct cgroup_subsys_state *root, int *foundid)
5425{
5426 struct cgroup_subsys_state *ret = NULL;
5427 struct css_id *tmp;
5428 int tmpid;
5429 int rootid = css_id(root);
5430 int depth = css_depth(root);
5431
5432 if (!rootid)
5433 return NULL;
5434
5435 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005436 WARN_ON_ONCE(!rcu_read_lock_held());
5437
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005438 /* fill start point for scan */
5439 tmpid = id;
5440 while (1) {
5441 /*
5442 * scan next entry from bitmap(tree), tmpid is updated after
5443 * idr_get_next().
5444 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005445 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005446 if (!tmp)
5447 break;
5448 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5449 ret = rcu_dereference(tmp->css);
5450 if (ret) {
5451 *foundid = tmpid;
5452 break;
5453 }
5454 }
5455 /* continue to scan from next id */
5456 tmpid = tmpid + 1;
5457 }
5458 return ret;
5459}
5460
Stephane Eraniane5d13672011-02-14 11:20:01 +02005461/*
5462 * get corresponding css from file open on cgroupfs directory
5463 */
5464struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5465{
5466 struct cgroup *cgrp;
5467 struct inode *inode;
5468 struct cgroup_subsys_state *css;
5469
5470 inode = f->f_dentry->d_inode;
5471 /* check in cgroup filesystem dir */
5472 if (inode->i_op != &cgroup_dir_inode_operations)
5473 return ERR_PTR(-EBADF);
5474
5475 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5476 return ERR_PTR(-EINVAL);
5477
5478 /* get cgroup */
5479 cgrp = __d_cgrp(f->f_dentry);
5480 css = cgrp->subsys[id];
5481 return css ? css : ERR_PTR(-ENOENT);
5482}
5483
Paul Menagefe693432009-09-23 15:56:20 -07005484#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005485static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005486{
5487 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5488
5489 if (!css)
5490 return ERR_PTR(-ENOMEM);
5491
5492 return css;
5493}
5494
Tejun Heo92fb9742012-11-19 08:13:38 -08005495static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005496{
5497 kfree(cont->subsys[debug_subsys_id]);
5498}
5499
5500static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5501{
5502 return atomic_read(&cont->count);
5503}
5504
5505static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5506{
5507 return cgroup_task_count(cont);
5508}
5509
5510static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5511{
5512 return (u64)(unsigned long)current->cgroups;
5513}
5514
5515static u64 current_css_set_refcount_read(struct cgroup *cont,
5516 struct cftype *cft)
5517{
5518 u64 count;
5519
5520 rcu_read_lock();
5521 count = atomic_read(&current->cgroups->refcount);
5522 rcu_read_unlock();
5523 return count;
5524}
5525
Paul Menage7717f7b2009-09-23 15:56:22 -07005526static int current_css_set_cg_links_read(struct cgroup *cont,
5527 struct cftype *cft,
5528 struct seq_file *seq)
5529{
5530 struct cg_cgroup_link *link;
5531 struct css_set *cg;
5532
5533 read_lock(&css_set_lock);
5534 rcu_read_lock();
5535 cg = rcu_dereference(current->cgroups);
5536 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5537 struct cgroup *c = link->cgrp;
5538 const char *name;
5539
5540 if (c->dentry)
5541 name = c->dentry->d_name.name;
5542 else
5543 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005544 seq_printf(seq, "Root %d group %s\n",
5545 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005546 }
5547 rcu_read_unlock();
5548 read_unlock(&css_set_lock);
5549 return 0;
5550}
5551
5552#define MAX_TASKS_SHOWN_PER_CSS 25
5553static int cgroup_css_links_read(struct cgroup *cont,
5554 struct cftype *cft,
5555 struct seq_file *seq)
5556{
5557 struct cg_cgroup_link *link;
5558
5559 read_lock(&css_set_lock);
5560 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5561 struct css_set *cg = link->cg;
5562 struct task_struct *task;
5563 int count = 0;
5564 seq_printf(seq, "css_set %p\n", cg);
5565 list_for_each_entry(task, &cg->tasks, cg_list) {
5566 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5567 seq_puts(seq, " ...\n");
5568 break;
5569 } else {
5570 seq_printf(seq, " task %d\n",
5571 task_pid_vnr(task));
5572 }
5573 }
5574 }
5575 read_unlock(&css_set_lock);
5576 return 0;
5577}
5578
Paul Menagefe693432009-09-23 15:56:20 -07005579static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5580{
5581 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5582}
5583
5584static struct cftype debug_files[] = {
5585 {
5586 .name = "cgroup_refcount",
5587 .read_u64 = cgroup_refcount_read,
5588 },
5589 {
5590 .name = "taskcount",
5591 .read_u64 = debug_taskcount_read,
5592 },
5593
5594 {
5595 .name = "current_css_set",
5596 .read_u64 = current_css_set_read,
5597 },
5598
5599 {
5600 .name = "current_css_set_refcount",
5601 .read_u64 = current_css_set_refcount_read,
5602 },
5603
5604 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005605 .name = "current_css_set_cg_links",
5606 .read_seq_string = current_css_set_cg_links_read,
5607 },
5608
5609 {
5610 .name = "cgroup_css_links",
5611 .read_seq_string = cgroup_css_links_read,
5612 },
5613
5614 {
Paul Menagefe693432009-09-23 15:56:20 -07005615 .name = "releasable",
5616 .read_u64 = releasable_read,
5617 },
Paul Menagefe693432009-09-23 15:56:20 -07005618
Tejun Heo4baf6e32012-04-01 12:09:55 -07005619 { } /* terminate */
5620};
Paul Menagefe693432009-09-23 15:56:20 -07005621
5622struct cgroup_subsys debug_subsys = {
5623 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005624 .css_alloc = debug_css_alloc,
5625 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005626 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005627 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005628};
5629#endif /* CONFIG_CGROUP_DEBUG */