blob: a893985601e974c075ce4474077b79ff9ab9d238 [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 Zefan0ac801f2013-01-10 11:49:27 +080055#include <linux/hashtable.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
Li Zefan0ac801f2013-01-10 11:49:27 +0800379static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700380
Li Zefan0ac801f2013-01-10 11:49:27 +0800381static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700382{
383 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800384 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700385
386 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800387 key += (unsigned long)css[i];
388 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700389
Li Zefan0ac801f2013-01-10 11:49:27 +0800390 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700391}
392
Paul Menage817929e2007-10-18 23:39:36 -0700393/* We don't maintain the lists running through each css_set to its
394 * task until after the first call to cgroup_iter_start(). This
395 * reduces the fork()/exit() overhead for people who have cgroups
396 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700397static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700398
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700399static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700400{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700401 struct cg_cgroup_link *link;
402 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700403 /*
404 * Ensure that the refcount doesn't hit zero while any readers
405 * can see it. Similar to atomic_dec_and_lock(), but for an
406 * rwlock
407 */
408 if (atomic_add_unless(&cg->refcount, -1, 1))
409 return;
410 write_lock(&css_set_lock);
411 if (!atomic_dec_and_test(&cg->refcount)) {
412 write_unlock(&css_set_lock);
413 return;
414 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700415
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700416 /* This css_set is dead. unlink it and release cgroup refcounts */
Li Zefan0ac801f2013-01-10 11:49:27 +0800417 hash_del(&cg->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700418 css_set_count--;
419
420 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
421 cg_link_list) {
422 struct cgroup *cgrp = link->cgrp;
423 list_del(&link->cg_link_list);
424 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700425 if (atomic_dec_and_test(&cgrp->count) &&
426 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700427 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700428 set_bit(CGRP_RELEASABLE, &cgrp->flags);
429 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700430 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700431
432 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700433 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700434
435 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800436 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700437}
438
439/*
440 * refcounted get/put for css_set objects
441 */
442static inline void get_css_set(struct css_set *cg)
443{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700444 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700445}
446
447static inline void put_css_set(struct css_set *cg)
448{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700449 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700450}
451
Paul Menage81a6a5c2007-10-18 23:39:38 -0700452static inline void put_css_set_taskexit(struct css_set *cg)
453{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700454 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700455}
456
Paul Menage817929e2007-10-18 23:39:36 -0700457/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700458 * compare_css_sets - helper function for find_existing_css_set().
459 * @cg: candidate css_set being tested
460 * @old_cg: existing css_set for a task
461 * @new_cgrp: cgroup that's being entered by the task
462 * @template: desired set of css pointers in css_set (pre-calculated)
463 *
464 * Returns true if "cg" matches "old_cg" except for the hierarchy
465 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
466 */
467static bool compare_css_sets(struct css_set *cg,
468 struct css_set *old_cg,
469 struct cgroup *new_cgrp,
470 struct cgroup_subsys_state *template[])
471{
472 struct list_head *l1, *l2;
473
474 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
475 /* Not all subsystems matched */
476 return false;
477 }
478
479 /*
480 * Compare cgroup pointers in order to distinguish between
481 * different cgroups in heirarchies with no subsystems. We
482 * could get by with just this check alone (and skip the
483 * memcmp above) but on most setups the memcmp check will
484 * avoid the need for this more expensive check on almost all
485 * candidates.
486 */
487
488 l1 = &cg->cg_links;
489 l2 = &old_cg->cg_links;
490 while (1) {
491 struct cg_cgroup_link *cgl1, *cgl2;
492 struct cgroup *cg1, *cg2;
493
494 l1 = l1->next;
495 l2 = l2->next;
496 /* See if we reached the end - both lists are equal length. */
497 if (l1 == &cg->cg_links) {
498 BUG_ON(l2 != &old_cg->cg_links);
499 break;
500 } else {
501 BUG_ON(l2 == &old_cg->cg_links);
502 }
503 /* Locate the cgroups associated with these links. */
504 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
505 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
506 cg1 = cgl1->cgrp;
507 cg2 = cgl2->cgrp;
508 /* Hierarchies should be linked in the same order. */
509 BUG_ON(cg1->root != cg2->root);
510
511 /*
512 * If this hierarchy is the hierarchy of the cgroup
513 * that's changing, then we need to check that this
514 * css_set points to the new cgroup; if it's any other
515 * hierarchy, then this css_set should point to the
516 * same cgroup as the old css_set.
517 */
518 if (cg1->root == new_cgrp->root) {
519 if (cg1 != new_cgrp)
520 return false;
521 } else {
522 if (cg1 != cg2)
523 return false;
524 }
525 }
526 return true;
527}
528
529/*
Paul Menage817929e2007-10-18 23:39:36 -0700530 * find_existing_css_set() is a helper for
531 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700532 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700533 *
534 * oldcg: the cgroup group that we're using before the cgroup
535 * transition
536 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700537 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700538 *
539 * template: location in which to build the desired set of subsystem
540 * state objects for the new cgroup group
541 */
Paul Menage817929e2007-10-18 23:39:36 -0700542static struct css_set *find_existing_css_set(
543 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700544 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700545 struct cgroup_subsys_state *template[])
546{
547 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700548 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700549 struct hlist_node *node;
550 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800551 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700552
Ben Blumaae8aab2010-03-10 15:22:07 -0800553 /*
554 * Build the set of subsystem state objects that we want to see in the
555 * new css_set. while subsystems can change globally, the entries here
556 * won't change, so no need for locking.
557 */
Paul Menage817929e2007-10-18 23:39:36 -0700558 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400559 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700560 /* Subsystem is in this hierarchy. So we want
561 * the subsystem state from the new
562 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700563 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700564 } else {
565 /* Subsystem is not in this hierarchy, so we
566 * don't want to change the subsystem state */
567 template[i] = oldcg->subsys[i];
568 }
569 }
570
Li Zefan0ac801f2013-01-10 11:49:27 +0800571 key = css_set_hash(template);
572 hash_for_each_possible(css_set_table, cg, node, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700573 if (!compare_css_sets(cg, oldcg, cgrp, template))
574 continue;
575
576 /* This css_set matches what we need */
577 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700578 }
Paul Menage817929e2007-10-18 23:39:36 -0700579
580 /* No existing cgroup group matched */
581 return NULL;
582}
583
Paul Menage817929e2007-10-18 23:39:36 -0700584static void free_cg_links(struct list_head *tmp)
585{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700586 struct cg_cgroup_link *link;
587 struct cg_cgroup_link *saved_link;
588
589 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700590 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700591 kfree(link);
592 }
593}
594
595/*
Li Zefan36553432008-07-29 22:33:19 -0700596 * allocate_cg_links() allocates "count" cg_cgroup_link structures
597 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
598 * success or a negative error
599 */
600static int allocate_cg_links(int count, struct list_head *tmp)
601{
602 struct cg_cgroup_link *link;
603 int i;
604 INIT_LIST_HEAD(tmp);
605 for (i = 0; i < count; i++) {
606 link = kmalloc(sizeof(*link), GFP_KERNEL);
607 if (!link) {
608 free_cg_links(tmp);
609 return -ENOMEM;
610 }
611 list_add(&link->cgrp_link_list, tmp);
612 }
613 return 0;
614}
615
Li Zefanc12f65d2009-01-07 18:07:42 -0800616/**
617 * link_css_set - a helper function to link a css_set to a cgroup
618 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
619 * @cg: the css_set to be linked
620 * @cgrp: the destination cgroup
621 */
622static void link_css_set(struct list_head *tmp_cg_links,
623 struct css_set *cg, struct cgroup *cgrp)
624{
625 struct cg_cgroup_link *link;
626
627 BUG_ON(list_empty(tmp_cg_links));
628 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
629 cgrp_link_list);
630 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700631 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700632 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800633 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700634 /*
635 * Always add links to the tail of the list so that the list
636 * is sorted by order of hierarchy creation
637 */
638 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800639}
640
Li Zefan36553432008-07-29 22:33:19 -0700641/*
Paul Menage817929e2007-10-18 23:39:36 -0700642 * find_css_set() takes an existing cgroup group and a
643 * cgroup object, and returns a css_set object that's
644 * equivalent to the old group, but with the given cgroup
645 * substituted into the appropriate hierarchy. Must be called with
646 * cgroup_mutex held
647 */
Paul Menage817929e2007-10-18 23:39:36 -0700648static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700649 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700650{
651 struct css_set *res;
652 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700653
654 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700655
Paul Menage7717f7b2009-09-23 15:56:22 -0700656 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800657 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700658
Paul Menage817929e2007-10-18 23:39:36 -0700659 /* First see if we already have a cgroup group that matches
660 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700661 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700662 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700663 if (res)
664 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700665 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700666
667 if (res)
668 return res;
669
670 res = kmalloc(sizeof(*res), GFP_KERNEL);
671 if (!res)
672 return NULL;
673
674 /* Allocate all the cg_cgroup_link objects that we'll need */
675 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
676 kfree(res);
677 return NULL;
678 }
679
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700680 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700681 INIT_LIST_HEAD(&res->cg_links);
682 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700683 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700684
685 /* Copy the set of subsystem state objects generated in
686 * find_existing_css_set() */
687 memcpy(res->subsys, template, sizeof(res->subsys));
688
689 write_lock(&css_set_lock);
690 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700691 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
692 struct cgroup *c = link->cgrp;
693 if (c->root == cgrp->root)
694 c = cgrp;
695 link_css_set(&tmp_cg_links, res, c);
696 }
Paul Menage817929e2007-10-18 23:39:36 -0700697
698 BUG_ON(!list_empty(&tmp_cg_links));
699
Paul Menage817929e2007-10-18 23:39:36 -0700700 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700701
702 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800703 key = css_set_hash(res->subsys);
704 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700705
Paul Menage817929e2007-10-18 23:39:36 -0700706 write_unlock(&css_set_lock);
707
708 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700709}
710
Paul Menageddbcc7e2007-10-18 23:39:30 -0700711/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700712 * Return the cgroup for "task" from the given hierarchy. Must be
713 * called with cgroup_mutex held.
714 */
715static struct cgroup *task_cgroup_from_root(struct task_struct *task,
716 struct cgroupfs_root *root)
717{
718 struct css_set *css;
719 struct cgroup *res = NULL;
720
721 BUG_ON(!mutex_is_locked(&cgroup_mutex));
722 read_lock(&css_set_lock);
723 /*
724 * No need to lock the task - since we hold cgroup_mutex the
725 * task can't change groups, so the only thing that can happen
726 * is that it exits and its css is set back to init_css_set.
727 */
728 css = task->cgroups;
729 if (css == &init_css_set) {
730 res = &root->top_cgroup;
731 } else {
732 struct cg_cgroup_link *link;
733 list_for_each_entry(link, &css->cg_links, cg_link_list) {
734 struct cgroup *c = link->cgrp;
735 if (c->root == root) {
736 res = c;
737 break;
738 }
739 }
740 }
741 read_unlock(&css_set_lock);
742 BUG_ON(!res);
743 return res;
744}
745
746/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700747 * There is one global cgroup mutex. We also require taking
748 * task_lock() when dereferencing a task's cgroup subsys pointers.
749 * See "The task_lock() exception", at the end of this comment.
750 *
751 * A task must hold cgroup_mutex to modify cgroups.
752 *
753 * Any task can increment and decrement the count field without lock.
754 * So in general, code holding cgroup_mutex can't rely on the count
755 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800756 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700757 * means that no tasks are currently attached, therefore there is no
758 * way a task attached to that cgroup can fork (the other way to
759 * increment the count). So code holding cgroup_mutex can safely
760 * assume that if the count is zero, it will stay zero. Similarly, if
761 * a task holds cgroup_mutex on a cgroup with zero count, it
762 * knows that the cgroup won't be removed, as cgroup_rmdir()
763 * needs that mutex.
764 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700765 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
766 * (usually) take cgroup_mutex. These are the two most performance
767 * critical pieces of code here. The exception occurs on cgroup_exit(),
768 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
769 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800770 * to the release agent with the name of the cgroup (path relative to
771 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700772 *
773 * A cgroup can only be deleted if both its 'count' of using tasks
774 * is zero, and its list of 'children' cgroups is empty. Since all
775 * tasks in the system use _some_ cgroup, and since there is always at
776 * least one task in the system (init, pid == 1), therefore, top_cgroup
777 * always has either children cgroups and/or using tasks. So we don't
778 * need a special hack to ensure that top_cgroup cannot be deleted.
779 *
780 * The task_lock() exception
781 *
782 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800783 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800784 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700785 * several performance critical places that need to reference
786 * task->cgroup without the expense of grabbing a system global
787 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800788 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700789 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
790 * the task_struct routinely used for such matters.
791 *
792 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800793 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700794 */
795
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796/**
797 * cgroup_lock - lock out any changes to cgroup structures
798 *
799 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700800void cgroup_lock(void)
801{
802 mutex_lock(&cgroup_mutex);
803}
Ben Blum67523c42010-03-10 15:22:11 -0800804EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700805
806/**
807 * cgroup_unlock - release lock on cgroup changes
808 *
809 * Undo the lock taken in a previous cgroup_lock() call.
810 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700811void cgroup_unlock(void)
812{
813 mutex_unlock(&cgroup_mutex);
814}
Ben Blum67523c42010-03-10 15:22:11 -0800815EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700816
817/*
818 * A couple of forward declarations required, due to cyclic reference loop:
819 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
820 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
821 * -> cgroup_mkdir.
822 */
823
Al Viro18bb1db2011-07-26 01:41:39 -0400824static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400825static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700826static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400827static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
828 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700829static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700830static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700831
832static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200833 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700834 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700835};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700836
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700837static int alloc_css_id(struct cgroup_subsys *ss,
838 struct cgroup *parent, struct cgroup *child);
839
Al Viroa5e7ed32011-07-26 01:55:55 -0400840static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700841{
842 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700843
844 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400845 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700846 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100847 inode->i_uid = current_fsuid();
848 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700849 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
850 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
851 }
852 return inode;
853}
854
855static void cgroup_diput(struct dentry *dentry, struct inode *inode)
856{
857 /* is dentry a directory ? if so, kfree() associated cgroup */
858 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700859 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800860 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700861 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700862 /* It's possible for external users to be holding css
863 * reference counts on a cgroup; css_put() needs to
864 * be able to access the cgroup after decrementing
865 * the reference count in order to know if it needs to
866 * queue the cgroup to be handled by the release
867 * agent */
868 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800869
870 mutex_lock(&cgroup_mutex);
871 /*
872 * Release the subsystem state objects.
873 */
Li Zefan75139b82009-01-07 18:07:33 -0800874 for_each_subsys(cgrp->root, ss)
Tejun Heo92fb9742012-11-19 08:13:38 -0800875 ss->css_free(cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800876
877 cgrp->root->number_of_cgroups--;
878 mutex_unlock(&cgroup_mutex);
879
Paul Menagea47295e2009-01-07 18:07:44 -0800880 /*
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700881 * Drop the active superblock reference that we took when we
882 * created the cgroup
Paul Menagea47295e2009-01-07 18:07:44 -0800883 */
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700884 deactivate_super(cgrp->root->sb);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800885
Ben Blum72a8cb32009-09-23 15:56:27 -0700886 /*
887 * if we're getting rid of the cgroup, refcount should ensure
888 * that there are no pidlists left.
889 */
890 BUG_ON(!list_empty(&cgrp->pidlists));
891
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400892 simple_xattrs_free(&cgrp->xattrs);
893
Tejun Heo0a950f62012-11-19 09:02:12 -0800894 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
Lai Jiangshanf2da1c42011-03-15 17:55:16 +0800895 kfree_rcu(cgrp, rcu_head);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700896 } else {
897 struct cfent *cfe = __d_cfe(dentry);
898 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400899 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700900
901 WARN_ONCE(!list_empty(&cfe->node) &&
902 cgrp != &cgrp->root->top_cgroup,
903 "cfe still linked for %s\n", cfe->type->name);
904 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400905 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700906 }
907 iput(inode);
908}
909
Al Viroc72a04e2011-01-14 05:31:45 +0000910static int cgroup_delete(const struct dentry *d)
911{
912 return 1;
913}
914
Paul Menageddbcc7e2007-10-18 23:39:30 -0700915static void remove_dir(struct dentry *d)
916{
917 struct dentry *parent = dget(d->d_parent);
918
919 d_delete(d);
920 simple_rmdir(parent->d_inode, d);
921 dput(parent);
922}
923
Tejun Heo05ef1d72012-04-01 12:09:56 -0700924static int cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700925{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700926 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700927
Tejun Heo05ef1d72012-04-01 12:09:56 -0700928 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
929 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100930
Tejun Heo05ef1d72012-04-01 12:09:56 -0700931 list_for_each_entry(cfe, &cgrp->files, node) {
932 struct dentry *d = cfe->dentry;
933
934 if (cft && cfe->type != cft)
935 continue;
936
937 dget(d);
938 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700939 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700940 list_del_init(&cfe->node);
941 dput(d);
942
943 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700944 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700945 return -ENOENT;
946}
947
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400948/**
949 * cgroup_clear_directory - selective removal of base and subsystem files
950 * @dir: directory containing the files
951 * @base_files: true if the base files should be removed
952 * @subsys_mask: mask of the subsystem ids whose files should be removed
953 */
954static void cgroup_clear_directory(struct dentry *dir, bool base_files,
955 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700956{
957 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400958 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700959
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400960 for_each_subsys(cgrp->root, ss) {
961 struct cftype_set *set;
962 if (!test_bit(ss->subsys_id, &subsys_mask))
963 continue;
964 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800965 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400966 }
967 if (base_files) {
968 while (!list_empty(&cgrp->files))
969 cgroup_rm_file(cgrp, NULL);
970 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700971}
972
973/*
974 * NOTE : the dentry must have been dget()'ed
975 */
976static void cgroup_d_remove_dir(struct dentry *dentry)
977{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100978 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400979 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100980
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400981 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700982
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100983 parent = dentry->d_parent;
984 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800985 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100987 spin_unlock(&dentry->d_lock);
988 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700989 remove_dir(dentry);
990}
991
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700992/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800993 * Call with cgroup_mutex held. Drops reference counts on modules, including
994 * any duplicate ones that parse_cgroupfs_options took. If this function
995 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800996 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400998 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001000 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001001 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002 int i;
1003
Ben Blumaae8aab2010-03-10 15:22:07 -08001004 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001005 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001006
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001007 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1008 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009 /* Check that any added subsystems are currently free */
1010 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001011 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001013 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001014 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001015 /*
1016 * Nobody should tell us to do a subsys that doesn't exist:
1017 * parse_cgroupfs_options should catch that case and refcounts
1018 * ensure that subsystems won't disappear once selected.
1019 */
1020 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001021 if (ss->root != &rootnode) {
1022 /* Subsystem isn't free */
1023 return -EBUSY;
1024 }
1025 }
1026
1027 /* Currently we don't handle adding/removing subsystems when
1028 * any child cgroups exist. This is theoretically supportable
1029 * but involves complex error handling, so it's being left until
1030 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001031 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032 return -EBUSY;
1033
1034 /* Process each subsystem */
1035 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1036 struct cgroup_subsys *ss = subsys[i];
1037 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001038 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001040 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001041 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001042 BUG_ON(!dummytop->subsys[i]);
1043 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001044 cgrp->subsys[i] = dummytop->subsys[i];
1045 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001046 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001047 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001049 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001050 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001051 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001053 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001054 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1055 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001057 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001059 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001060 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001061 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001062 /* subsystem is now free - drop reference on module */
1063 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001064 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001066 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001067 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001068 /*
1069 * a refcount was taken, but we already had one, so
1070 * drop the extra reference.
1071 */
1072 module_put(ss->module);
1073#ifdef CONFIG_MODULE_UNLOAD
1074 BUG_ON(ss->module && !module_refcount(ss->module));
1075#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076 } else {
1077 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001078 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079 }
1080 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001081 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082
1083 return 0;
1084}
1085
Al Viro34c80b12011-12-08 21:32:45 -05001086static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087{
Al Viro34c80b12011-12-08 21:32:45 -05001088 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089 struct cgroup_subsys *ss;
1090
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001091 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092 for_each_subsys(root, ss)
1093 seq_printf(seq, ",%s", ss->name);
1094 if (test_bit(ROOT_NOPREFIX, &root->flags))
1095 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001096 if (test_bit(ROOT_XATTR, &root->flags))
1097 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001098 if (strlen(root->release_agent_path))
1099 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001100 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001101 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001102 if (strlen(root->name))
1103 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001104 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001105 return 0;
1106}
1107
1108struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001109 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001111 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001112 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001113 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001114 /* User explicitly requested empty subsystem */
1115 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001116
1117 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001118
Paul Menageddbcc7e2007-10-18 23:39:30 -07001119};
1120
Ben Blumaae8aab2010-03-10 15:22:07 -08001121/*
1122 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001123 * with cgroup_mutex held to protect the subsys[] array. This function takes
1124 * refcounts on subsystems to be used, unless it returns error, in which case
1125 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001126 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001127static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001129 char *token, *o = data;
1130 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001131 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001132 int i;
1133 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001134
Ben Blumaae8aab2010-03-10 15:22:07 -08001135 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1136
Li Zefanf9ab5b52009-06-17 16:26:33 -07001137#ifdef CONFIG_CPUSETS
1138 mask = ~(1UL << cpuset_subsys_id);
1139#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001140
Paul Menagec6d57f32009-09-23 15:56:19 -07001141 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001142
1143 while ((token = strsep(&o, ",")) != NULL) {
1144 if (!*token)
1145 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001146 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001147 /* Explicitly have no subsystems */
1148 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001149 continue;
1150 }
1151 if (!strcmp(token, "all")) {
1152 /* Mutually exclusive option 'all' + subsystem name */
1153 if (one_ss)
1154 return -EINVAL;
1155 all_ss = true;
1156 continue;
1157 }
1158 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001159 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001160 continue;
1161 }
1162 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001163 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001164 continue;
1165 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001166 if (!strcmp(token, "xattr")) {
1167 set_bit(ROOT_XATTR, &opts->flags);
1168 continue;
1169 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001170 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001171 /* Specifying two release agents is forbidden */
1172 if (opts->release_agent)
1173 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001174 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001175 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001176 if (!opts->release_agent)
1177 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001178 continue;
1179 }
1180 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001181 const char *name = token + 5;
1182 /* Can't specify an empty name */
1183 if (!strlen(name))
1184 return -EINVAL;
1185 /* Must match [\w.-]+ */
1186 for (i = 0; i < strlen(name); i++) {
1187 char c = name[i];
1188 if (isalnum(c))
1189 continue;
1190 if ((c == '.') || (c == '-') || (c == '_'))
1191 continue;
1192 return -EINVAL;
1193 }
1194 /* Specifying two names is forbidden */
1195 if (opts->name)
1196 return -EINVAL;
1197 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001198 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001199 GFP_KERNEL);
1200 if (!opts->name)
1201 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001202
1203 continue;
1204 }
1205
1206 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1207 struct cgroup_subsys *ss = subsys[i];
1208 if (ss == NULL)
1209 continue;
1210 if (strcmp(token, ss->name))
1211 continue;
1212 if (ss->disabled)
1213 continue;
1214
1215 /* Mutually exclusive option 'all' + subsystem name */
1216 if (all_ss)
1217 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001218 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001219 one_ss = true;
1220
1221 break;
1222 }
1223 if (i == CGROUP_SUBSYS_COUNT)
1224 return -ENOENT;
1225 }
1226
1227 /*
1228 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001229 * otherwise if 'none', 'name=' and a subsystem name options
1230 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001231 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001232 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001233 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1234 struct cgroup_subsys *ss = subsys[i];
1235 if (ss == NULL)
1236 continue;
1237 if (ss->disabled)
1238 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001239 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001240 }
1241 }
1242
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001243 /* Consistency checks */
1244
Li Zefanf9ab5b52009-06-17 16:26:33 -07001245 /*
1246 * Option noprefix was introduced just for backward compatibility
1247 * with the old cpuset, so we allow noprefix only if mounting just
1248 * the cpuset subsystem.
1249 */
1250 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001251 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001252 return -EINVAL;
1253
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001254
1255 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001256 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001257 return -EINVAL;
1258
1259 /*
1260 * We either have to specify by name or by subsystems. (So all
1261 * empty hierarchies must have a name).
1262 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001263 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001264 return -EINVAL;
1265
Ben Blumcf5d5942010-03-10 15:22:09 -08001266 /*
1267 * Grab references on all the modules we'll need, so the subsystems
1268 * don't dance around before rebind_subsystems attaches them. This may
1269 * take duplicate reference counts on a subsystem that's already used,
1270 * but rebind_subsystems handles this case.
1271 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001272 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001273 unsigned long bit = 1UL << i;
1274
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001275 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001276 continue;
1277 if (!try_module_get(subsys[i]->module)) {
1278 module_pin_failed = true;
1279 break;
1280 }
1281 }
1282 if (module_pin_failed) {
1283 /*
1284 * oops, one of the modules was going away. this means that we
1285 * raced with a module_delete call, and to the user this is
1286 * essentially a "subsystem doesn't exist" case.
1287 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001288 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001289 /* drop refcounts only on the ones we took */
1290 unsigned long bit = 1UL << i;
1291
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001292 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001293 continue;
1294 module_put(subsys[i]->module);
1295 }
1296 return -ENOENT;
1297 }
1298
Paul Menageddbcc7e2007-10-18 23:39:30 -07001299 return 0;
1300}
1301
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001302static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001303{
1304 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001305 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001306 unsigned long bit = 1UL << i;
1307
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001308 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001309 continue;
1310 module_put(subsys[i]->module);
1311 }
1312}
1313
Paul Menageddbcc7e2007-10-18 23:39:30 -07001314static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1315{
1316 int ret = 0;
1317 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001318 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001319 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001320 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001321
Paul Menagebd89aab2007-10-18 23:40:44 -07001322 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001324 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325
1326 /* See what subsystems are wanted */
1327 ret = parse_cgroupfs_options(data, &opts);
1328 if (ret)
1329 goto out_unlock;
1330
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001331 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001332 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1333 task_tgid_nr(current), current->comm);
1334
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001335 added_mask = opts.subsys_mask & ~root->subsys_mask;
1336 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001337
Ben Blumcf5d5942010-03-10 15:22:09 -08001338 /* Don't allow flags or name to change at remount */
1339 if (opts.flags != root->flags ||
1340 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001341 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001342 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001343 goto out_unlock;
1344 }
1345
Gao feng7083d032012-12-03 09:28:18 +08001346 /*
1347 * Clear out the files of subsystems that should be removed, do
1348 * this before rebind_subsystems, since rebind_subsystems may
1349 * change this hierarchy's subsys_list.
1350 */
1351 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1352
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001353 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001354 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001355 /* rebind_subsystems failed, re-populate the removed files */
1356 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001357 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001358 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001359 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001360
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001361 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001362 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001363
Paul Menage81a6a5c2007-10-18 23:39:38 -07001364 if (opts.release_agent)
1365 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001366 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001367 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001368 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001369 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001370 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001371 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001372 return ret;
1373}
1374
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001375static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001376 .statfs = simple_statfs,
1377 .drop_inode = generic_delete_inode,
1378 .show_options = cgroup_show_options,
1379 .remount_fs = cgroup_remount,
1380};
1381
Paul Menagecc31edc2008-10-18 20:28:04 -07001382static void init_cgroup_housekeeping(struct cgroup *cgrp)
1383{
1384 INIT_LIST_HEAD(&cgrp->sibling);
1385 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001386 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001387 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001388 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001389 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001390 INIT_LIST_HEAD(&cgrp->pidlists);
1391 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001392 INIT_LIST_HEAD(&cgrp->event_list);
1393 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001394 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001395}
Paul Menagec6d57f32009-09-23 15:56:19 -07001396
Paul Menageddbcc7e2007-10-18 23:39:30 -07001397static void init_cgroup_root(struct cgroupfs_root *root)
1398{
Paul Menagebd89aab2007-10-18 23:40:44 -07001399 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001400
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401 INIT_LIST_HEAD(&root->subsys_list);
1402 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001403 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001404 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001405 cgrp->root = root;
1406 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001407 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001408 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001409}
1410
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001411static bool init_root_id(struct cgroupfs_root *root)
1412{
1413 int ret = 0;
1414
1415 do {
1416 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1417 return false;
1418 spin_lock(&hierarchy_id_lock);
1419 /* Try to allocate the next unused ID */
1420 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1421 &root->hierarchy_id);
1422 if (ret == -ENOSPC)
1423 /* Try again starting from 0 */
1424 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1425 if (!ret) {
1426 next_hierarchy_id = root->hierarchy_id + 1;
1427 } else if (ret != -EAGAIN) {
1428 /* Can only get here if the 31-bit IDR is full ... */
1429 BUG_ON(ret);
1430 }
1431 spin_unlock(&hierarchy_id_lock);
1432 } while (ret);
1433 return true;
1434}
1435
Paul Menageddbcc7e2007-10-18 23:39:30 -07001436static int cgroup_test_super(struct super_block *sb, void *data)
1437{
Paul Menagec6d57f32009-09-23 15:56:19 -07001438 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001439 struct cgroupfs_root *root = sb->s_fs_info;
1440
Paul Menagec6d57f32009-09-23 15:56:19 -07001441 /* If we asked for a name then it must match */
1442 if (opts->name && strcmp(opts->name, root->name))
1443 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001444
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001445 /*
1446 * If we asked for subsystems (or explicitly for no
1447 * subsystems) then they must match
1448 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001449 if ((opts->subsys_mask || opts->none)
1450 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001451 return 0;
1452
1453 return 1;
1454}
1455
Paul Menagec6d57f32009-09-23 15:56:19 -07001456static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1457{
1458 struct cgroupfs_root *root;
1459
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001460 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001461 return NULL;
1462
1463 root = kzalloc(sizeof(*root), GFP_KERNEL);
1464 if (!root)
1465 return ERR_PTR(-ENOMEM);
1466
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001467 if (!init_root_id(root)) {
1468 kfree(root);
1469 return ERR_PTR(-ENOMEM);
1470 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001471 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001472
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001473 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001474 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001475 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001476 if (opts->release_agent)
1477 strcpy(root->release_agent_path, opts->release_agent);
1478 if (opts->name)
1479 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001480 if (opts->cpuset_clone_children)
1481 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 return root;
1483}
1484
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001485static void cgroup_drop_root(struct cgroupfs_root *root)
1486{
1487 if (!root)
1488 return;
1489
1490 BUG_ON(!root->hierarchy_id);
1491 spin_lock(&hierarchy_id_lock);
1492 ida_remove(&hierarchy_ida, root->hierarchy_id);
1493 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001494 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001495 kfree(root);
1496}
1497
Paul Menageddbcc7e2007-10-18 23:39:30 -07001498static int cgroup_set_super(struct super_block *sb, void *data)
1499{
1500 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001501 struct cgroup_sb_opts *opts = data;
1502
1503 /* If we don't have a new root, we can't set up a new sb */
1504 if (!opts->new_root)
1505 return -EINVAL;
1506
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001507 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508
1509 ret = set_anon_super(sb, NULL);
1510 if (ret)
1511 return ret;
1512
Paul Menagec6d57f32009-09-23 15:56:19 -07001513 sb->s_fs_info = opts->new_root;
1514 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001515
1516 sb->s_blocksize = PAGE_CACHE_SIZE;
1517 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1518 sb->s_magic = CGROUP_SUPER_MAGIC;
1519 sb->s_op = &cgroup_ops;
1520
1521 return 0;
1522}
1523
1524static int cgroup_get_rootdir(struct super_block *sb)
1525{
Al Viro0df6a632010-12-21 13:29:29 -05001526 static const struct dentry_operations cgroup_dops = {
1527 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001528 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001529 };
1530
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531 struct inode *inode =
1532 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533
1534 if (!inode)
1535 return -ENOMEM;
1536
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537 inode->i_fop = &simple_dir_operations;
1538 inode->i_op = &cgroup_dir_inode_operations;
1539 /* directories start off with i_nlink == 2 (for "." entry) */
1540 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001541 sb->s_root = d_make_root(inode);
1542 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001544 /* for everything else we want ->d_op set */
1545 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 return 0;
1547}
1548
Al Virof7e83572010-07-26 13:23:11 +04001549static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001550 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001551 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552{
1553 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001554 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555 int ret = 0;
1556 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001557 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001558 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559
1560 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001561 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001563 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001564 if (ret)
1565 goto out_err;
1566
1567 /*
1568 * Allocate a new cgroup root. We may not need it if we're
1569 * reusing an existing hierarchy.
1570 */
1571 new_root = cgroup_root_from_opts(&opts);
1572 if (IS_ERR(new_root)) {
1573 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001574 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001575 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001576 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577
Paul Menagec6d57f32009-09-23 15:56:19 -07001578 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001579 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001581 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001582 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001583 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584 }
1585
Paul Menagec6d57f32009-09-23 15:56:19 -07001586 root = sb->s_fs_info;
1587 BUG_ON(!root);
1588 if (root == opts.new_root) {
1589 /* We used the new root structure, so this is a new hierarchy */
1590 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001591 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001592 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001593 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001594 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001595 struct hlist_node *node;
1596 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001597
1598 BUG_ON(sb->s_root != NULL);
1599
1600 ret = cgroup_get_rootdir(sb);
1601 if (ret)
1602 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001603 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001604
Paul Menage817929e2007-10-18 23:39:36 -07001605 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001606 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001607 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001608
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001609 /* Check for name clashes with existing mounts */
1610 ret = -EBUSY;
1611 if (strlen(root->name))
1612 for_each_active_root(existing_root)
1613 if (!strcmp(existing_root->name, root->name))
1614 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001615
Paul Menage817929e2007-10-18 23:39:36 -07001616 /*
1617 * We're accessing css_set_count without locking
1618 * css_set_lock here, but that's OK - it can only be
1619 * increased by someone holding cgroup_lock, and
1620 * that's us. The worst that can happen is that we
1621 * have some link structures left over
1622 */
1623 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001624 if (ret)
1625 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001626
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001627 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001629 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001630 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001632 /*
1633 * There must be no failure case after here, since rebinding
1634 * takes care of subsystems' refcounts, which are explicitly
1635 * dropped in the failure exit path.
1636 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001637
1638 /* EBUSY should be the only error here */
1639 BUG_ON(ret);
1640
1641 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001642 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001643
Li Zefanc12f65d2009-01-07 18:07:42 -08001644 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001645 root->top_cgroup.dentry = sb->s_root;
1646
Paul Menage817929e2007-10-18 23:39:36 -07001647 /* Link the top cgroup in this hierarchy into all
1648 * the css_set objects */
1649 write_lock(&css_set_lock);
Li Zefan0ac801f2013-01-10 11:49:27 +08001650 hash_for_each(css_set_table, i, node, cg, hlist)
1651 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001652 write_unlock(&css_set_lock);
1653
1654 free_cg_links(&tmp_cg_links);
1655
Li Zefanc12f65d2009-01-07 18:07:42 -08001656 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657 BUG_ON(root->number_of_cgroups != 1);
1658
eparis@redhat2ce97382011-06-02 21:20:51 +10001659 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001660 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001661 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001662 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001664 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001665 } else {
1666 /*
1667 * We re-used an existing hierarchy - the new root (if
1668 * any) is not needed
1669 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001670 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001671 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001672 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673 }
1674
Paul Menagec6d57f32009-09-23 15:56:19 -07001675 kfree(opts.release_agent);
1676 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001677 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001678
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001679 unlock_drop:
1680 mutex_unlock(&cgroup_root_mutex);
1681 mutex_unlock(&cgroup_mutex);
1682 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001684 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001685 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001686 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001687 out_err:
1688 kfree(opts.release_agent);
1689 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001690 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001691}
1692
1693static void cgroup_kill_sb(struct super_block *sb) {
1694 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001695 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001697 struct cg_cgroup_link *link;
1698 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001699
1700 BUG_ON(!root);
1701
1702 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001703 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001704
1705 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001706 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001707
1708 /* Rebind all subsystems back to the default hierarchy */
1709 ret = rebind_subsystems(root, 0);
1710 /* Shouldn't be able to fail ... */
1711 BUG_ON(ret);
1712
Paul Menage817929e2007-10-18 23:39:36 -07001713 /*
1714 * Release all the links from css_sets to this hierarchy's
1715 * root cgroup
1716 */
1717 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001718
1719 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1720 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001721 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001722 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001723 kfree(link);
1724 }
1725 write_unlock(&css_set_lock);
1726
Paul Menage839ec542009-01-29 14:25:22 -08001727 if (!list_empty(&root->root_list)) {
1728 list_del(&root->root_list);
1729 root_count--;
1730 }
Li Zefane5f6a862009-01-07 18:07:41 -08001731
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001732 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733 mutex_unlock(&cgroup_mutex);
1734
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001735 simple_xattrs_free(&cgrp->xattrs);
1736
Paul Menageddbcc7e2007-10-18 23:39:30 -07001737 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001738 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739}
1740
1741static struct file_system_type cgroup_fs_type = {
1742 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001743 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744 .kill_sb = cgroup_kill_sb,
1745};
1746
Greg KH676db4a2010-08-05 13:53:35 -07001747static struct kobject *cgroup_kobj;
1748
Li Zefana043e3b2008-02-23 15:24:09 -08001749/**
1750 * cgroup_path - generate the path of a cgroup
1751 * @cgrp: the cgroup in question
1752 * @buf: the buffer to write the path into
1753 * @buflen: the length of the buffer
1754 *
Paul Menagea47295e2009-01-07 18:07:44 -08001755 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1756 * reference. Writes path of cgroup into buf. Returns 0 on success,
1757 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001759int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760{
Tejun Heofebfcef2012-11-19 08:13:36 -08001761 struct dentry *dentry = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001762 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001763
1764 rcu_lockdep_assert(rcu_read_lock_held() || cgroup_lock_is_held(),
1765 "cgroup_path() called without proper locking");
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766
Paul Menagea47295e2009-01-07 18:07:44 -08001767 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001768 /*
1769 * Inactive subsystems have no dentry for their root
1770 * cgroup
1771 */
1772 strcpy(buf, "/");
1773 return 0;
1774 }
1775
Tao Ma316eb662012-11-08 21:36:38 +08001776 start = buf + buflen - 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777
Tao Ma316eb662012-11-08 21:36:38 +08001778 *start = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001780 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001781
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 if ((start -= len) < buf)
1783 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001784 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001785 cgrp = cgrp->parent;
1786 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001788
Tejun Heofebfcef2012-11-19 08:13:36 -08001789 dentry = cgrp->dentry;
Paul Menagebd89aab2007-10-18 23:40:44 -07001790 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791 continue;
1792 if (--start < buf)
1793 return -ENAMETOOLONG;
1794 *start = '/';
1795 }
1796 memmove(buf, start, buf + buflen - start);
1797 return 0;
1798}
Ben Blum67523c42010-03-10 15:22:11 -08001799EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001800
Ben Blum74a11662011-05-26 16:25:20 -07001801/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001802 * Control Group taskset
1803 */
Tejun Heo134d3372011-12-12 18:12:21 -08001804struct task_and_cgroup {
1805 struct task_struct *task;
1806 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001807 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001808};
1809
Tejun Heo2f7ee562011-12-12 18:12:21 -08001810struct cgroup_taskset {
1811 struct task_and_cgroup single;
1812 struct flex_array *tc_array;
1813 int tc_array_len;
1814 int idx;
1815 struct cgroup *cur_cgrp;
1816};
1817
1818/**
1819 * cgroup_taskset_first - reset taskset and return the first task
1820 * @tset: taskset of interest
1821 *
1822 * @tset iteration is initialized and the first task is returned.
1823 */
1824struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1825{
1826 if (tset->tc_array) {
1827 tset->idx = 0;
1828 return cgroup_taskset_next(tset);
1829 } else {
1830 tset->cur_cgrp = tset->single.cgrp;
1831 return tset->single.task;
1832 }
1833}
1834EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1835
1836/**
1837 * cgroup_taskset_next - iterate to the next task in taskset
1838 * @tset: taskset of interest
1839 *
1840 * Return the next task in @tset. Iteration must have been initialized
1841 * with cgroup_taskset_first().
1842 */
1843struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1844{
1845 struct task_and_cgroup *tc;
1846
1847 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1848 return NULL;
1849
1850 tc = flex_array_get(tset->tc_array, tset->idx++);
1851 tset->cur_cgrp = tc->cgrp;
1852 return tc->task;
1853}
1854EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1855
1856/**
1857 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1858 * @tset: taskset of interest
1859 *
1860 * Return the cgroup for the current (last returned) task of @tset. This
1861 * function must be preceded by either cgroup_taskset_first() or
1862 * cgroup_taskset_next().
1863 */
1864struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1865{
1866 return tset->cur_cgrp;
1867}
1868EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1869
1870/**
1871 * cgroup_taskset_size - return the number of tasks in taskset
1872 * @tset: taskset of interest
1873 */
1874int cgroup_taskset_size(struct cgroup_taskset *tset)
1875{
1876 return tset->tc_array ? tset->tc_array_len : 1;
1877}
1878EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1879
1880
Ben Blum74a11662011-05-26 16:25:20 -07001881/*
1882 * cgroup_task_migrate - move a task from one cgroup to another.
1883 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001884 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001885 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001886static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1887 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001888{
1889 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001890
1891 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001892 * We are synchronized through threadgroup_lock() against PF_EXITING
1893 * setting such that we can't race against cgroup_exit() changing the
1894 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001895 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001896 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001897 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001898
Ben Blum74a11662011-05-26 16:25:20 -07001899 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001900 rcu_assign_pointer(tsk->cgroups, newcg);
1901 task_unlock(tsk);
1902
1903 /* Update the css_set linked lists if we're using them */
1904 write_lock(&css_set_lock);
1905 if (!list_empty(&tsk->cg_list))
1906 list_move(&tsk->cg_list, &newcg->tasks);
1907 write_unlock(&css_set_lock);
1908
1909 /*
1910 * We just gained a reference on oldcg by taking it from the task. As
1911 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1912 * it here; it will be freed under RCU.
1913 */
Ben Blum74a11662011-05-26 16:25:20 -07001914 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001915 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001916}
1917
Li Zefana043e3b2008-02-23 15:24:09 -08001918/**
1919 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1920 * @cgrp: the cgroup the task is attaching to
1921 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001922 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001923 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1924 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001925 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001926int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001927{
Tejun Heo8f121912012-03-29 22:03:33 -07001928 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001929 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001930 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001931 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001932 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001933 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001934
Tejun Heocd3d0952011-12-12 18:12:21 -08001935 /* @tsk either already exited or can't exit until the end */
1936 if (tsk->flags & PF_EXITING)
1937 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001938
1939 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001940 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001941 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001942 return 0;
1943
Tejun Heo2f7ee562011-12-12 18:12:21 -08001944 tset.single.task = tsk;
1945 tset.single.cgrp = oldcgrp;
1946
Paul Menagebbcb81d2007-10-18 23:39:32 -07001947 for_each_subsys(root, ss) {
1948 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08001949 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001950 if (retval) {
1951 /*
1952 * Remember on which subsystem the can_attach()
1953 * failed, so that we only call cancel_attach()
1954 * against the subsystems whose can_attach()
1955 * succeeded. (See below)
1956 */
1957 failed_ss = ss;
1958 goto out;
1959 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001960 }
1961 }
1962
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001963 newcg = find_css_set(tsk->cgroups, cgrp);
1964 if (!newcg) {
1965 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001966 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001967 }
1968
1969 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001970
Paul Menagebbcb81d2007-10-18 23:39:32 -07001971 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001972 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001973 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001974 }
Ben Blum74a11662011-05-26 16:25:20 -07001975
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001976out:
1977 if (retval) {
1978 for_each_subsys(root, ss) {
1979 if (ss == failed_ss)
1980 /*
1981 * This subsystem was the one that failed the
1982 * can_attach() check earlier, so we don't need
1983 * to call cancel_attach() against it or any
1984 * remaining subsystems.
1985 */
1986 break;
1987 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001988 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001989 }
1990 }
1991 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001992}
1993
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001994/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001995 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1996 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001997 * @tsk: the task to be attached
1998 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001999int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002000{
2001 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002002 int retval = 0;
2003
2004 cgroup_lock();
2005 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002006 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2007
2008 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002009 if (retval)
2010 break;
2011 }
2012 cgroup_unlock();
2013
2014 return retval;
2015}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002016EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002017
Ben Blum74a11662011-05-26 16:25:20 -07002018/**
2019 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2020 * @cgrp: the cgroup to attach to
2021 * @leader: the threadgroup leader task_struct of the group to be attached
2022 *
Tejun Heo257058a2011-12-12 18:12:21 -08002023 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2024 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002025 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002026static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002027{
2028 int retval, i, group_size;
2029 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002030 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002031 struct cgroupfs_root *root = cgrp->root;
2032 /* threadgroup list cursor and array */
2033 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002034 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002035 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002036 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002037
2038 /*
2039 * step 0: in order to do expensive, possibly blocking operations for
2040 * every thread, we cannot iterate the thread group list, since it needs
2041 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002042 * group - group_rwsem prevents new threads from appearing, and if
2043 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002044 */
2045 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002046 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002047 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002048 if (!group)
2049 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002050 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2051 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2052 if (retval)
2053 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002054
Ben Blum74a11662011-05-26 16:25:20 -07002055 tsk = leader;
2056 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002057 /*
2058 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2059 * already PF_EXITING could be freed from underneath us unless we
2060 * take an rcu_read_lock.
2061 */
2062 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002063 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002064 struct task_and_cgroup ent;
2065
Tejun Heocd3d0952011-12-12 18:12:21 -08002066 /* @tsk either already exited or can't exit until the end */
2067 if (tsk->flags & PF_EXITING)
2068 continue;
2069
Ben Blum74a11662011-05-26 16:25:20 -07002070 /* as per above, nr_threads may decrease, but not increase. */
2071 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002072 ent.task = tsk;
2073 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002074 /* nothing to do if this task is already in the cgroup */
2075 if (ent.cgrp == cgrp)
2076 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002077 /*
2078 * saying GFP_ATOMIC has no effect here because we did prealloc
2079 * earlier, but it's good form to communicate our expectations.
2080 */
Tejun Heo134d3372011-12-12 18:12:21 -08002081 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002082 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002083 i++;
2084 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002085 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002086 /* remember the number of threads in the array for later. */
2087 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002088 tset.tc_array = group;
2089 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002090
Tejun Heo134d3372011-12-12 18:12:21 -08002091 /* methods shouldn't be called if no task is actually migrating */
2092 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002093 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002094 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002095
Ben Blum74a11662011-05-26 16:25:20 -07002096 /*
2097 * step 1: check that we can legitimately attach to the cgroup.
2098 */
2099 for_each_subsys(root, ss) {
2100 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002101 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002102 if (retval) {
2103 failed_ss = ss;
2104 goto out_cancel_attach;
2105 }
2106 }
Ben Blum74a11662011-05-26 16:25:20 -07002107 }
2108
2109 /*
2110 * step 2: make sure css_sets exist for all threads to be migrated.
2111 * we use find_css_set, which allocates a new one if necessary.
2112 */
Ben Blum74a11662011-05-26 16:25:20 -07002113 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002114 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002115 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2116 if (!tc->cg) {
2117 retval = -ENOMEM;
2118 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002119 }
2120 }
2121
2122 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002123 * step 3: now that we're guaranteed success wrt the css_sets,
2124 * proceed to move all tasks to the new cgroup. There are no
2125 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002126 */
Ben Blum74a11662011-05-26 16:25:20 -07002127 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002128 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002129 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002130 }
2131 /* nothing is sensitive to fork() after this point. */
2132
2133 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002134 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002135 */
2136 for_each_subsys(root, ss) {
2137 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002138 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002139 }
2140
2141 /*
2142 * step 5: success! and cleanup
2143 */
Ben Blum74a11662011-05-26 16:25:20 -07002144 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002145out_put_css_set_refs:
2146 if (retval) {
2147 for (i = 0; i < group_size; i++) {
2148 tc = flex_array_get(group, i);
2149 if (!tc->cg)
2150 break;
2151 put_css_set(tc->cg);
2152 }
Ben Blum74a11662011-05-26 16:25:20 -07002153 }
2154out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002155 if (retval) {
2156 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002157 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002158 break;
Ben Blum74a11662011-05-26 16:25:20 -07002159 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002160 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002161 }
2162 }
Ben Blum74a11662011-05-26 16:25:20 -07002163out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002164 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002165 return retval;
2166}
2167
2168/*
2169 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002170 * function to attach either it or all tasks in its threadgroup. Will lock
2171 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002172 */
2173static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002174{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002175 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002176 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002177 int ret;
2178
Ben Blum74a11662011-05-26 16:25:20 -07002179 if (!cgroup_lock_live_group(cgrp))
2180 return -ENODEV;
2181
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002182retry_find_task:
2183 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002184 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002185 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002186 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002187 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002188 ret= -ESRCH;
2189 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002190 }
Ben Blum74a11662011-05-26 16:25:20 -07002191 /*
2192 * even if we're attaching all tasks in the thread group, we
2193 * only need to check permissions on one of them.
2194 */
David Howellsc69e8d92008-11-14 10:39:19 +11002195 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002196 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2197 !uid_eq(cred->euid, tcred->uid) &&
2198 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002199 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002200 ret = -EACCES;
2201 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002202 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002203 } else
2204 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002205
2206 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002207 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002208
2209 /*
2210 * Workqueue threads may acquire PF_THREAD_BOUND and become
2211 * trapped in a cpuset, or RT worker may be born in a cgroup
2212 * with no rt_runtime allocated. Just say no.
2213 */
2214 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2215 ret = -EINVAL;
2216 rcu_read_unlock();
2217 goto out_unlock_cgroup;
2218 }
2219
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002220 get_task_struct(tsk);
2221 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002222
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002223 threadgroup_lock(tsk);
2224 if (threadgroup) {
2225 if (!thread_group_leader(tsk)) {
2226 /*
2227 * a race with de_thread from another thread's exec()
2228 * may strip us of our leadership, if this happens,
2229 * there is no choice but to throw this task away and
2230 * try again; this is
2231 * "double-double-toil-and-trouble-check locking".
2232 */
2233 threadgroup_unlock(tsk);
2234 put_task_struct(tsk);
2235 goto retry_find_task;
2236 }
2237 ret = cgroup_attach_proc(cgrp, tsk);
2238 } else
2239 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002240 threadgroup_unlock(tsk);
2241
Paul Menagebbcb81d2007-10-18 23:39:32 -07002242 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002243out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002244 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002245 return ret;
2246}
2247
Paul Menageaf351022008-07-25 01:47:01 -07002248static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2249{
Ben Blum74a11662011-05-26 16:25:20 -07002250 return attach_task_by_pid(cgrp, pid, false);
2251}
2252
2253static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2254{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002255 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002256}
2257
Paul Menagee788e062008-07-25 01:46:59 -07002258/**
2259 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2260 * @cgrp: the cgroup to be checked for liveness
2261 *
Paul Menage84eea842008-07-25 01:47:00 -07002262 * On success, returns true; the lock should be later released with
2263 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002264 */
Paul Menage84eea842008-07-25 01:47:00 -07002265bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002266{
2267 mutex_lock(&cgroup_mutex);
2268 if (cgroup_is_removed(cgrp)) {
2269 mutex_unlock(&cgroup_mutex);
2270 return false;
2271 }
2272 return true;
2273}
Ben Blum67523c42010-03-10 15:22:11 -08002274EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002275
2276static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2277 const char *buffer)
2278{
2279 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002280 if (strlen(buffer) >= PATH_MAX)
2281 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002282 if (!cgroup_lock_live_group(cgrp))
2283 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002284 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002285 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002286 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002287 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002288 return 0;
2289}
2290
2291static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2292 struct seq_file *seq)
2293{
2294 if (!cgroup_lock_live_group(cgrp))
2295 return -ENODEV;
2296 seq_puts(seq, cgrp->root->release_agent_path);
2297 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002298 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002299 return 0;
2300}
2301
Paul Menage84eea842008-07-25 01:47:00 -07002302/* A buffer size big enough for numbers or short strings */
2303#define CGROUP_LOCAL_BUFFER_SIZE 64
2304
Paul Menagee73d2c62008-04-29 01:00:06 -07002305static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002306 struct file *file,
2307 const char __user *userbuf,
2308 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002309{
Paul Menage84eea842008-07-25 01:47:00 -07002310 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002311 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002312 char *end;
2313
2314 if (!nbytes)
2315 return -EINVAL;
2316 if (nbytes >= sizeof(buffer))
2317 return -E2BIG;
2318 if (copy_from_user(buffer, userbuf, nbytes))
2319 return -EFAULT;
2320
2321 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002322 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002323 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002324 if (*end)
2325 return -EINVAL;
2326 retval = cft->write_u64(cgrp, cft, val);
2327 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002328 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002329 if (*end)
2330 return -EINVAL;
2331 retval = cft->write_s64(cgrp, cft, val);
2332 }
Paul Menage355e0c42007-10-18 23:39:33 -07002333 if (!retval)
2334 retval = nbytes;
2335 return retval;
2336}
2337
Paul Menagedb3b1492008-07-25 01:46:58 -07002338static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2339 struct file *file,
2340 const char __user *userbuf,
2341 size_t nbytes, loff_t *unused_ppos)
2342{
Paul Menage84eea842008-07-25 01:47:00 -07002343 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002344 int retval = 0;
2345 size_t max_bytes = cft->max_write_len;
2346 char *buffer = local_buffer;
2347
2348 if (!max_bytes)
2349 max_bytes = sizeof(local_buffer) - 1;
2350 if (nbytes >= max_bytes)
2351 return -E2BIG;
2352 /* Allocate a dynamic buffer if we need one */
2353 if (nbytes >= sizeof(local_buffer)) {
2354 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2355 if (buffer == NULL)
2356 return -ENOMEM;
2357 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002358 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2359 retval = -EFAULT;
2360 goto out;
2361 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002362
2363 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002364 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002365 if (!retval)
2366 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002367out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002368 if (buffer != local_buffer)
2369 kfree(buffer);
2370 return retval;
2371}
2372
Paul Menageddbcc7e2007-10-18 23:39:30 -07002373static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2374 size_t nbytes, loff_t *ppos)
2375{
2376 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002377 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002378
Li Zefan75139b82009-01-07 18:07:33 -08002379 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002380 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002381 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002382 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002383 if (cft->write_u64 || cft->write_s64)
2384 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002385 if (cft->write_string)
2386 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002387 if (cft->trigger) {
2388 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2389 return ret ? ret : nbytes;
2390 }
Paul Menage355e0c42007-10-18 23:39:33 -07002391 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392}
2393
Paul Menagef4c753b2008-04-29 00:59:56 -07002394static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2395 struct file *file,
2396 char __user *buf, size_t nbytes,
2397 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002398{
Paul Menage84eea842008-07-25 01:47:00 -07002399 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002400 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002401 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2402
2403 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2404}
2405
Paul Menagee73d2c62008-04-29 01:00:06 -07002406static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2407 struct file *file,
2408 char __user *buf, size_t nbytes,
2409 loff_t *ppos)
2410{
Paul Menage84eea842008-07-25 01:47:00 -07002411 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002412 s64 val = cft->read_s64(cgrp, cft);
2413 int len = sprintf(tmp, "%lld\n", (long long) val);
2414
2415 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2416}
2417
Paul Menageddbcc7e2007-10-18 23:39:30 -07002418static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2419 size_t nbytes, loff_t *ppos)
2420{
2421 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002422 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002423
Li Zefan75139b82009-01-07 18:07:33 -08002424 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002425 return -ENODEV;
2426
2427 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002428 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002429 if (cft->read_u64)
2430 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002431 if (cft->read_s64)
2432 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002433 return -EINVAL;
2434}
2435
Paul Menage91796562008-04-29 01:00:01 -07002436/*
2437 * seqfile ops/methods for returning structured data. Currently just
2438 * supports string->u64 maps, but can be extended in future.
2439 */
2440
2441struct cgroup_seqfile_state {
2442 struct cftype *cft;
2443 struct cgroup *cgroup;
2444};
2445
2446static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2447{
2448 struct seq_file *sf = cb->state;
2449 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2450}
2451
2452static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2453{
2454 struct cgroup_seqfile_state *state = m->private;
2455 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002456 if (cft->read_map) {
2457 struct cgroup_map_cb cb = {
2458 .fill = cgroup_map_add,
2459 .state = m,
2460 };
2461 return cft->read_map(state->cgroup, cft, &cb);
2462 }
2463 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002464}
2465
Adrian Bunk96930a62008-07-25 19:46:21 -07002466static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002467{
2468 struct seq_file *seq = file->private_data;
2469 kfree(seq->private);
2470 return single_release(inode, file);
2471}
2472
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002473static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002474 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002475 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002476 .llseek = seq_lseek,
2477 .release = cgroup_seqfile_release,
2478};
2479
Paul Menageddbcc7e2007-10-18 23:39:30 -07002480static int cgroup_file_open(struct inode *inode, struct file *file)
2481{
2482 int err;
2483 struct cftype *cft;
2484
2485 err = generic_file_open(inode, file);
2486 if (err)
2487 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002488 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002489
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002490 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002491 struct cgroup_seqfile_state *state =
2492 kzalloc(sizeof(*state), GFP_USER);
2493 if (!state)
2494 return -ENOMEM;
2495 state->cft = cft;
2496 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2497 file->f_op = &cgroup_seqfile_operations;
2498 err = single_open(file, cgroup_seqfile_show, state);
2499 if (err < 0)
2500 kfree(state);
2501 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002502 err = cft->open(inode, file);
2503 else
2504 err = 0;
2505
2506 return err;
2507}
2508
2509static int cgroup_file_release(struct inode *inode, struct file *file)
2510{
2511 struct cftype *cft = __d_cft(file->f_dentry);
2512 if (cft->release)
2513 return cft->release(inode, file);
2514 return 0;
2515}
2516
2517/*
2518 * cgroup_rename - Only allow simple rename of directories in place.
2519 */
2520static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2521 struct inode *new_dir, struct dentry *new_dentry)
2522{
2523 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2524 return -ENOTDIR;
2525 if (new_dentry->d_inode)
2526 return -EEXIST;
2527 if (old_dir != new_dir)
2528 return -EIO;
2529 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2530}
2531
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002532static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2533{
2534 if (S_ISDIR(dentry->d_inode->i_mode))
2535 return &__d_cgrp(dentry)->xattrs;
2536 else
2537 return &__d_cft(dentry)->xattrs;
2538}
2539
2540static inline int xattr_enabled(struct dentry *dentry)
2541{
2542 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2543 return test_bit(ROOT_XATTR, &root->flags);
2544}
2545
2546static bool is_valid_xattr(const char *name)
2547{
2548 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2549 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2550 return true;
2551 return false;
2552}
2553
2554static int cgroup_setxattr(struct dentry *dentry, const char *name,
2555 const void *val, size_t size, int flags)
2556{
2557 if (!xattr_enabled(dentry))
2558 return -EOPNOTSUPP;
2559 if (!is_valid_xattr(name))
2560 return -EINVAL;
2561 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2562}
2563
2564static int cgroup_removexattr(struct dentry *dentry, const char *name)
2565{
2566 if (!xattr_enabled(dentry))
2567 return -EOPNOTSUPP;
2568 if (!is_valid_xattr(name))
2569 return -EINVAL;
2570 return simple_xattr_remove(__d_xattrs(dentry), name);
2571}
2572
2573static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2574 void *buf, size_t size)
2575{
2576 if (!xattr_enabled(dentry))
2577 return -EOPNOTSUPP;
2578 if (!is_valid_xattr(name))
2579 return -EINVAL;
2580 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2581}
2582
2583static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2584{
2585 if (!xattr_enabled(dentry))
2586 return -EOPNOTSUPP;
2587 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2588}
2589
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002590static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002591 .read = cgroup_file_read,
2592 .write = cgroup_file_write,
2593 .llseek = generic_file_llseek,
2594 .open = cgroup_file_open,
2595 .release = cgroup_file_release,
2596};
2597
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002598static const struct inode_operations cgroup_file_inode_operations = {
2599 .setxattr = cgroup_setxattr,
2600 .getxattr = cgroup_getxattr,
2601 .listxattr = cgroup_listxattr,
2602 .removexattr = cgroup_removexattr,
2603};
2604
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002605static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002606 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002607 .mkdir = cgroup_mkdir,
2608 .rmdir = cgroup_rmdir,
2609 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002610 .setxattr = cgroup_setxattr,
2611 .getxattr = cgroup_getxattr,
2612 .listxattr = cgroup_listxattr,
2613 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002614};
2615
Al Viro00cd8dd2012-06-10 17:13:09 -04002616static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002617{
2618 if (dentry->d_name.len > NAME_MAX)
2619 return ERR_PTR(-ENAMETOOLONG);
2620 d_add(dentry, NULL);
2621 return NULL;
2622}
2623
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002624/*
2625 * Check if a file is a control file
2626 */
2627static inline struct cftype *__file_cft(struct file *file)
2628{
2629 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2630 return ERR_PTR(-EINVAL);
2631 return __d_cft(file->f_dentry);
2632}
2633
Al Viroa5e7ed32011-07-26 01:55:55 -04002634static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002635 struct super_block *sb)
2636{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002637 struct inode *inode;
2638
2639 if (!dentry)
2640 return -ENOENT;
2641 if (dentry->d_inode)
2642 return -EEXIST;
2643
2644 inode = cgroup_new_inode(mode, sb);
2645 if (!inode)
2646 return -ENOMEM;
2647
2648 if (S_ISDIR(mode)) {
2649 inode->i_op = &cgroup_dir_inode_operations;
2650 inode->i_fop = &simple_dir_operations;
2651
2652 /* start off with i_nlink == 2 (for "." entry) */
2653 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002654 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002655
Tejun Heob8a2df62012-11-19 08:13:37 -08002656 /*
2657 * Control reaches here with cgroup_mutex held.
2658 * @inode->i_mutex should nest outside cgroup_mutex but we
2659 * want to populate it immediately without releasing
2660 * cgroup_mutex. As @inode isn't visible to anyone else
2661 * yet, trylock will always succeed without affecting
2662 * lockdep checks.
2663 */
2664 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002665 } else if (S_ISREG(mode)) {
2666 inode->i_size = 0;
2667 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002668 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002669 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002670 d_instantiate(dentry, inode);
2671 dget(dentry); /* Extra count - pin the dentry in core */
2672 return 0;
2673}
2674
Li Zefan099fca32009-04-02 16:57:29 -07002675/**
2676 * cgroup_file_mode - deduce file mode of a control file
2677 * @cft: the control file in question
2678 *
2679 * returns cft->mode if ->mode is not 0
2680 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2681 * returns S_IRUGO if it has only a read handler
2682 * returns S_IWUSR if it has only a write hander
2683 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002684static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002685{
Al Viroa5e7ed32011-07-26 01:55:55 -04002686 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002687
2688 if (cft->mode)
2689 return cft->mode;
2690
2691 if (cft->read || cft->read_u64 || cft->read_s64 ||
2692 cft->read_map || cft->read_seq_string)
2693 mode |= S_IRUGO;
2694
2695 if (cft->write || cft->write_u64 || cft->write_s64 ||
2696 cft->write_string || cft->trigger)
2697 mode |= S_IWUSR;
2698
2699 return mode;
2700}
2701
Tejun Heodb0416b2012-04-01 12:09:55 -07002702static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002703 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002704{
Paul Menagebd89aab2007-10-18 23:40:44 -07002705 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002706 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002707 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002708 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002709 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002710 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002711 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002712
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002713 simple_xattrs_init(&cft->xattrs);
2714
Paul Menagebd89aab2007-10-18 23:40:44 -07002715 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002716 strcpy(name, subsys->name);
2717 strcat(name, ".");
2718 }
2719 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002720
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002722
2723 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2724 if (!cfe)
2725 return -ENOMEM;
2726
Paul Menageddbcc7e2007-10-18 23:39:30 -07002727 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002728 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002729 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002730 goto out;
2731 }
2732
2733 mode = cgroup_file_mode(cft);
2734 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2735 if (!error) {
2736 cfe->type = (void *)cft;
2737 cfe->dentry = dentry;
2738 dentry->d_fsdata = cfe;
2739 list_add_tail(&cfe->node, &parent->files);
2740 cfe = NULL;
2741 }
2742 dput(dentry);
2743out:
2744 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002745 return error;
2746}
2747
Tejun Heo79578622012-04-01 12:09:56 -07002748static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002749 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002750{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002751 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002752 int err, ret = 0;
2753
2754 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002755 /* does cft->flags tell us to skip this file on @cgrp? */
2756 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2757 continue;
2758 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2759 continue;
2760
Tejun Heo79578622012-04-01 12:09:56 -07002761 if (is_add)
2762 err = cgroup_add_file(cgrp, subsys, cft);
2763 else
2764 err = cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002765 if (err) {
Tejun Heo79578622012-04-01 12:09:56 -07002766 pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n",
2767 is_add ? "add" : "remove", cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002768 ret = err;
2769 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002770 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002771 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002772}
2773
Tejun Heo8e3f6542012-04-01 12:09:55 -07002774static DEFINE_MUTEX(cgroup_cft_mutex);
2775
2776static void cgroup_cfts_prepare(void)
2777 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2778{
2779 /*
2780 * Thanks to the entanglement with vfs inode locking, we can't walk
2781 * the existing cgroups under cgroup_mutex and create files.
2782 * Instead, we increment reference on all cgroups and build list of
2783 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2784 * exclusive access to the field.
2785 */
2786 mutex_lock(&cgroup_cft_mutex);
2787 mutex_lock(&cgroup_mutex);
2788}
2789
2790static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002791 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002792 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2793{
2794 LIST_HEAD(pending);
2795 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002796
2797 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2798 if (cfts && ss->root != &rootnode) {
2799 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2800 dget(cgrp->dentry);
2801 list_add_tail(&cgrp->cft_q_node, &pending);
2802 }
2803 }
2804
2805 mutex_unlock(&cgroup_mutex);
2806
2807 /*
2808 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2809 * files for all cgroups which were created before.
2810 */
2811 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2812 struct inode *inode = cgrp->dentry->d_inode;
2813
2814 mutex_lock(&inode->i_mutex);
2815 mutex_lock(&cgroup_mutex);
2816 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002817 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002818 mutex_unlock(&cgroup_mutex);
2819 mutex_unlock(&inode->i_mutex);
2820
2821 list_del_init(&cgrp->cft_q_node);
2822 dput(cgrp->dentry);
2823 }
2824
2825 mutex_unlock(&cgroup_cft_mutex);
2826}
2827
2828/**
2829 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2830 * @ss: target cgroup subsystem
2831 * @cfts: zero-length name terminated array of cftypes
2832 *
2833 * Register @cfts to @ss. Files described by @cfts are created for all
2834 * existing cgroups to which @ss is attached and all future cgroups will
2835 * have them too. This function can be called anytime whether @ss is
2836 * attached or not.
2837 *
2838 * Returns 0 on successful registration, -errno on failure. Note that this
2839 * function currently returns 0 as long as @cfts registration is successful
2840 * even if some file creation attempts on existing cgroups fail.
2841 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002842int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002843{
2844 struct cftype_set *set;
2845
2846 set = kzalloc(sizeof(*set), GFP_KERNEL);
2847 if (!set)
2848 return -ENOMEM;
2849
2850 cgroup_cfts_prepare();
2851 set->cfts = cfts;
2852 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002853 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002854
2855 return 0;
2856}
2857EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2858
Li Zefana043e3b2008-02-23 15:24:09 -08002859/**
Tejun Heo79578622012-04-01 12:09:56 -07002860 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2861 * @ss: target cgroup subsystem
2862 * @cfts: zero-length name terminated array of cftypes
2863 *
2864 * Unregister @cfts from @ss. Files described by @cfts are removed from
2865 * all existing cgroups to which @ss is attached and all future cgroups
2866 * won't have them either. This function can be called anytime whether @ss
2867 * is attached or not.
2868 *
2869 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2870 * registered with @ss.
2871 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002872int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002873{
2874 struct cftype_set *set;
2875
2876 cgroup_cfts_prepare();
2877
2878 list_for_each_entry(set, &ss->cftsets, node) {
2879 if (set->cfts == cfts) {
2880 list_del_init(&set->node);
2881 cgroup_cfts_commit(ss, cfts, false);
2882 return 0;
2883 }
2884 }
2885
2886 cgroup_cfts_commit(ss, NULL, false);
2887 return -ENOENT;
2888}
2889
2890/**
Li Zefana043e3b2008-02-23 15:24:09 -08002891 * cgroup_task_count - count the number of tasks in a cgroup.
2892 * @cgrp: the cgroup in question
2893 *
2894 * Return the number of tasks in the cgroup.
2895 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002896int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002897{
2898 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002899 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002900
Paul Menage817929e2007-10-18 23:39:36 -07002901 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002902 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002903 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002904 }
2905 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002906 return count;
2907}
2908
2909/*
Paul Menage817929e2007-10-18 23:39:36 -07002910 * Advance a list_head iterator. The iterator should be positioned at
2911 * the start of a css_set
2912 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002913static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002914 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002915{
2916 struct list_head *l = it->cg_link;
2917 struct cg_cgroup_link *link;
2918 struct css_set *cg;
2919
2920 /* Advance to the next non-empty css_set */
2921 do {
2922 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002923 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002924 it->cg_link = NULL;
2925 return;
2926 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002927 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002928 cg = link->cg;
2929 } while (list_empty(&cg->tasks));
2930 it->cg_link = l;
2931 it->task = cg->tasks.next;
2932}
2933
Cliff Wickman31a7df02008-02-07 00:14:42 -08002934/*
2935 * To reduce the fork() overhead for systems that are not actually
2936 * using their cgroups capability, we don't maintain the lists running
2937 * through each css_set to its tasks until we see the list actually
2938 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002939 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002940static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002941{
2942 struct task_struct *p, *g;
2943 write_lock(&css_set_lock);
2944 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002945 /*
2946 * We need tasklist_lock because RCU is not safe against
2947 * while_each_thread(). Besides, a forking task that has passed
2948 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2949 * is not guaranteed to have its child immediately visible in the
2950 * tasklist if we walk through it with RCU.
2951 */
2952 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002953 do_each_thread(g, p) {
2954 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002955 /*
2956 * We should check if the process is exiting, otherwise
2957 * it will race with cgroup_exit() in that the list
2958 * entry won't be deleted though the process has exited.
2959 */
2960 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002961 list_add(&p->cg_list, &p->cgroups->tasks);
2962 task_unlock(p);
2963 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002964 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002965 write_unlock(&css_set_lock);
2966}
2967
Tejun Heo574bd9f2012-11-09 09:12:29 -08002968/**
2969 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2970 * @pos: the current position (%NULL to initiate traversal)
2971 * @cgroup: cgroup whose descendants to walk
2972 *
2973 * To be used by cgroup_for_each_descendant_pre(). Find the next
2974 * descendant to visit for pre-order traversal of @cgroup's descendants.
2975 */
2976struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2977 struct cgroup *cgroup)
2978{
2979 struct cgroup *next;
2980
2981 WARN_ON_ONCE(!rcu_read_lock_held());
2982
2983 /* if first iteration, pretend we just visited @cgroup */
2984 if (!pos) {
2985 if (list_empty(&cgroup->children))
2986 return NULL;
2987 pos = cgroup;
2988 }
2989
2990 /* visit the first child if exists */
2991 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2992 if (next)
2993 return next;
2994
2995 /* no child, visit my or the closest ancestor's next sibling */
2996 do {
2997 next = list_entry_rcu(pos->sibling.next, struct cgroup,
2998 sibling);
2999 if (&next->sibling != &pos->parent->children)
3000 return next;
3001
3002 pos = pos->parent;
3003 } while (pos != cgroup);
3004
3005 return NULL;
3006}
3007EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3008
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003009/**
3010 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3011 * @pos: cgroup of interest
3012 *
3013 * Return the rightmost descendant of @pos. If there's no descendant,
3014 * @pos is returned. This can be used during pre-order traversal to skip
3015 * subtree of @pos.
3016 */
3017struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3018{
3019 struct cgroup *last, *tmp;
3020
3021 WARN_ON_ONCE(!rcu_read_lock_held());
3022
3023 do {
3024 last = pos;
3025 /* ->prev isn't RCU safe, walk ->next till the end */
3026 pos = NULL;
3027 list_for_each_entry_rcu(tmp, &last->children, sibling)
3028 pos = tmp;
3029 } while (pos);
3030
3031 return last;
3032}
3033EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3034
Tejun Heo574bd9f2012-11-09 09:12:29 -08003035static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3036{
3037 struct cgroup *last;
3038
3039 do {
3040 last = pos;
3041 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3042 sibling);
3043 } while (pos);
3044
3045 return last;
3046}
3047
3048/**
3049 * cgroup_next_descendant_post - find the next descendant for post-order walk
3050 * @pos: the current position (%NULL to initiate traversal)
3051 * @cgroup: cgroup whose descendants to walk
3052 *
3053 * To be used by cgroup_for_each_descendant_post(). Find the next
3054 * descendant to visit for post-order traversal of @cgroup's descendants.
3055 */
3056struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3057 struct cgroup *cgroup)
3058{
3059 struct cgroup *next;
3060
3061 WARN_ON_ONCE(!rcu_read_lock_held());
3062
3063 /* if first iteration, visit the leftmost descendant */
3064 if (!pos) {
3065 next = cgroup_leftmost_descendant(cgroup);
3066 return next != cgroup ? next : NULL;
3067 }
3068
3069 /* if there's an unvisited sibling, visit its leftmost descendant */
3070 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3071 if (&next->sibling != &pos->parent->children)
3072 return cgroup_leftmost_descendant(next);
3073
3074 /* no sibling left, visit parent */
3075 next = pos->parent;
3076 return next != cgroup ? next : NULL;
3077}
3078EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3079
Paul Menagebd89aab2007-10-18 23:40:44 -07003080void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003081 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003082{
3083 /*
3084 * The first time anyone tries to iterate across a cgroup,
3085 * we need to enable the list linking each css_set to its
3086 * tasks, and fix up all existing tasks.
3087 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003088 if (!use_task_css_set_links)
3089 cgroup_enable_task_cg_lists();
3090
Paul Menage817929e2007-10-18 23:39:36 -07003091 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003092 it->cg_link = &cgrp->css_sets;
3093 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003094}
3095
Paul Menagebd89aab2007-10-18 23:40:44 -07003096struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003097 struct cgroup_iter *it)
3098{
3099 struct task_struct *res;
3100 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003101 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003102
3103 /* If the iterator cg is NULL, we have no tasks */
3104 if (!it->cg_link)
3105 return NULL;
3106 res = list_entry(l, struct task_struct, cg_list);
3107 /* Advance iterator to find next entry */
3108 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003109 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3110 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003111 /* We reached the end of this task list - move on to
3112 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003113 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003114 } else {
3115 it->task = l;
3116 }
3117 return res;
3118}
3119
Paul Menagebd89aab2007-10-18 23:40:44 -07003120void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003121 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003122{
3123 read_unlock(&css_set_lock);
3124}
3125
Cliff Wickman31a7df02008-02-07 00:14:42 -08003126static inline int started_after_time(struct task_struct *t1,
3127 struct timespec *time,
3128 struct task_struct *t2)
3129{
3130 int start_diff = timespec_compare(&t1->start_time, time);
3131 if (start_diff > 0) {
3132 return 1;
3133 } else if (start_diff < 0) {
3134 return 0;
3135 } else {
3136 /*
3137 * Arbitrarily, if two processes started at the same
3138 * time, we'll say that the lower pointer value
3139 * started first. Note that t2 may have exited by now
3140 * so this may not be a valid pointer any longer, but
3141 * that's fine - it still serves to distinguish
3142 * between two tasks started (effectively) simultaneously.
3143 */
3144 return t1 > t2;
3145 }
3146}
3147
3148/*
3149 * This function is a callback from heap_insert() and is used to order
3150 * the heap.
3151 * In this case we order the heap in descending task start time.
3152 */
3153static inline int started_after(void *p1, void *p2)
3154{
3155 struct task_struct *t1 = p1;
3156 struct task_struct *t2 = p2;
3157 return started_after_time(t1, &t2->start_time, t2);
3158}
3159
3160/**
3161 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3162 * @scan: struct cgroup_scanner containing arguments for the scan
3163 *
3164 * Arguments include pointers to callback functions test_task() and
3165 * process_task().
3166 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3167 * and if it returns true, call process_task() for it also.
3168 * The test_task pointer may be NULL, meaning always true (select all tasks).
3169 * Effectively duplicates cgroup_iter_{start,next,end}()
3170 * but does not lock css_set_lock for the call to process_task().
3171 * The struct cgroup_scanner may be embedded in any structure of the caller's
3172 * creation.
3173 * It is guaranteed that process_task() will act on every task that
3174 * is a member of the cgroup for the duration of this call. This
3175 * function may or may not call process_task() for tasks that exit
3176 * or move to a different cgroup during the call, or are forked or
3177 * move into the cgroup during the call.
3178 *
3179 * Note that test_task() may be called with locks held, and may in some
3180 * situations be called multiple times for the same task, so it should
3181 * be cheap.
3182 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3183 * pre-allocated and will be used for heap operations (and its "gt" member will
3184 * be overwritten), else a temporary heap will be used (allocation of which
3185 * may cause this function to fail).
3186 */
3187int cgroup_scan_tasks(struct cgroup_scanner *scan)
3188{
3189 int retval, i;
3190 struct cgroup_iter it;
3191 struct task_struct *p, *dropped;
3192 /* Never dereference latest_task, since it's not refcounted */
3193 struct task_struct *latest_task = NULL;
3194 struct ptr_heap tmp_heap;
3195 struct ptr_heap *heap;
3196 struct timespec latest_time = { 0, 0 };
3197
3198 if (scan->heap) {
3199 /* The caller supplied our heap and pre-allocated its memory */
3200 heap = scan->heap;
3201 heap->gt = &started_after;
3202 } else {
3203 /* We need to allocate our own heap memory */
3204 heap = &tmp_heap;
3205 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3206 if (retval)
3207 /* cannot allocate the heap */
3208 return retval;
3209 }
3210
3211 again:
3212 /*
3213 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3214 * to determine which are of interest, and using the scanner's
3215 * "process_task" callback to process any of them that need an update.
3216 * Since we don't want to hold any locks during the task updates,
3217 * gather tasks to be processed in a heap structure.
3218 * The heap is sorted by descending task start time.
3219 * If the statically-sized heap fills up, we overflow tasks that
3220 * started later, and in future iterations only consider tasks that
3221 * started after the latest task in the previous pass. This
3222 * guarantees forward progress and that we don't miss any tasks.
3223 */
3224 heap->size = 0;
3225 cgroup_iter_start(scan->cg, &it);
3226 while ((p = cgroup_iter_next(scan->cg, &it))) {
3227 /*
3228 * Only affect tasks that qualify per the caller's callback,
3229 * if he provided one
3230 */
3231 if (scan->test_task && !scan->test_task(p, scan))
3232 continue;
3233 /*
3234 * Only process tasks that started after the last task
3235 * we processed
3236 */
3237 if (!started_after_time(p, &latest_time, latest_task))
3238 continue;
3239 dropped = heap_insert(heap, p);
3240 if (dropped == NULL) {
3241 /*
3242 * The new task was inserted; the heap wasn't
3243 * previously full
3244 */
3245 get_task_struct(p);
3246 } else if (dropped != p) {
3247 /*
3248 * The new task was inserted, and pushed out a
3249 * different task
3250 */
3251 get_task_struct(p);
3252 put_task_struct(dropped);
3253 }
3254 /*
3255 * Else the new task was newer than anything already in
3256 * the heap and wasn't inserted
3257 */
3258 }
3259 cgroup_iter_end(scan->cg, &it);
3260
3261 if (heap->size) {
3262 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003263 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003264 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003265 latest_time = q->start_time;
3266 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003267 }
3268 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003269 scan->process_task(q, scan);
3270 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003271 }
3272 /*
3273 * If we had to process any tasks at all, scan again
3274 * in case some of them were in the middle of forking
3275 * children that didn't get processed.
3276 * Not the most efficient way to do it, but it avoids
3277 * having to take callback_mutex in the fork path
3278 */
3279 goto again;
3280 }
3281 if (heap == &tmp_heap)
3282 heap_free(&tmp_heap);
3283 return 0;
3284}
3285
Paul Menage817929e2007-10-18 23:39:36 -07003286/*
Ben Blum102a7752009-09-23 15:56:26 -07003287 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003288 *
3289 * Reading this file can return large amounts of data if a cgroup has
3290 * *lots* of attached tasks. So it may need several calls to read(),
3291 * but we cannot guarantee that the information we produce is correct
3292 * unless we produce it entirely atomically.
3293 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003294 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003295
Li Zefan24528252012-01-20 11:58:43 +08003296/* which pidlist file are we talking about? */
3297enum cgroup_filetype {
3298 CGROUP_FILE_PROCS,
3299 CGROUP_FILE_TASKS,
3300};
3301
3302/*
3303 * A pidlist is a list of pids that virtually represents the contents of one
3304 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3305 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3306 * to the cgroup.
3307 */
3308struct cgroup_pidlist {
3309 /*
3310 * used to find which pidlist is wanted. doesn't change as long as
3311 * this particular list stays in the list.
3312 */
3313 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3314 /* array of xids */
3315 pid_t *list;
3316 /* how many elements the above list has */
3317 int length;
3318 /* how many files are using the current array */
3319 int use_count;
3320 /* each of these stored in a list by its cgroup */
3321 struct list_head links;
3322 /* pointer to the cgroup we belong to, for list removal purposes */
3323 struct cgroup *owner;
3324 /* protects the other fields */
3325 struct rw_semaphore mutex;
3326};
3327
Paul Menagebbcb81d2007-10-18 23:39:32 -07003328/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003329 * The following two functions "fix" the issue where there are more pids
3330 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3331 * TODO: replace with a kernel-wide solution to this problem
3332 */
3333#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3334static void *pidlist_allocate(int count)
3335{
3336 if (PIDLIST_TOO_LARGE(count))
3337 return vmalloc(count * sizeof(pid_t));
3338 else
3339 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3340}
3341static void pidlist_free(void *p)
3342{
3343 if (is_vmalloc_addr(p))
3344 vfree(p);
3345 else
3346 kfree(p);
3347}
3348static void *pidlist_resize(void *p, int newcount)
3349{
3350 void *newlist;
3351 /* note: if new alloc fails, old p will still be valid either way */
3352 if (is_vmalloc_addr(p)) {
3353 newlist = vmalloc(newcount * sizeof(pid_t));
3354 if (!newlist)
3355 return NULL;
3356 memcpy(newlist, p, newcount * sizeof(pid_t));
3357 vfree(p);
3358 } else {
3359 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3360 }
3361 return newlist;
3362}
3363
3364/*
Ben Blum102a7752009-09-23 15:56:26 -07003365 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3366 * If the new stripped list is sufficiently smaller and there's enough memory
3367 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3368 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003369 */
Ben Blum102a7752009-09-23 15:56:26 -07003370/* is the size difference enough that we should re-allocate the array? */
3371#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3372static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003373{
Ben Blum102a7752009-09-23 15:56:26 -07003374 int src, dest = 1;
3375 pid_t *list = *p;
3376 pid_t *newlist;
3377
3378 /*
3379 * we presume the 0th element is unique, so i starts at 1. trivial
3380 * edge cases first; no work needs to be done for either
3381 */
3382 if (length == 0 || length == 1)
3383 return length;
3384 /* src and dest walk down the list; dest counts unique elements */
3385 for (src = 1; src < length; src++) {
3386 /* find next unique element */
3387 while (list[src] == list[src-1]) {
3388 src++;
3389 if (src == length)
3390 goto after;
3391 }
3392 /* dest always points to where the next unique element goes */
3393 list[dest] = list[src];
3394 dest++;
3395 }
3396after:
3397 /*
3398 * if the length difference is large enough, we want to allocate a
3399 * smaller buffer to save memory. if this fails due to out of memory,
3400 * we'll just stay with what we've got.
3401 */
3402 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003403 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003404 if (newlist)
3405 *p = newlist;
3406 }
3407 return dest;
3408}
3409
3410static int cmppid(const void *a, const void *b)
3411{
3412 return *(pid_t *)a - *(pid_t *)b;
3413}
3414
3415/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003416 * find the appropriate pidlist for our purpose (given procs vs tasks)
3417 * returns with the lock on that pidlist already held, and takes care
3418 * of the use count, or returns NULL with no locks held if we're out of
3419 * memory.
3420 */
3421static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3422 enum cgroup_filetype type)
3423{
3424 struct cgroup_pidlist *l;
3425 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003426 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003427
Ben Blum72a8cb32009-09-23 15:56:27 -07003428 /*
3429 * We can't drop the pidlist_mutex before taking the l->mutex in case
3430 * the last ref-holder is trying to remove l from the list at the same
3431 * time. Holding the pidlist_mutex precludes somebody taking whichever
3432 * list we find out from under us - compare release_pid_array().
3433 */
3434 mutex_lock(&cgrp->pidlist_mutex);
3435 list_for_each_entry(l, &cgrp->pidlists, links) {
3436 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003437 /* make sure l doesn't vanish out from under us */
3438 down_write(&l->mutex);
3439 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003440 return l;
3441 }
3442 }
3443 /* entry not found; create a new one */
3444 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3445 if (!l) {
3446 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003447 return l;
3448 }
3449 init_rwsem(&l->mutex);
3450 down_write(&l->mutex);
3451 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003452 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003453 l->use_count = 0; /* don't increment here */
3454 l->list = NULL;
3455 l->owner = cgrp;
3456 list_add(&l->links, &cgrp->pidlists);
3457 mutex_unlock(&cgrp->pidlist_mutex);
3458 return l;
3459}
3460
3461/*
Ben Blum102a7752009-09-23 15:56:26 -07003462 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3463 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003464static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3465 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003466{
3467 pid_t *array;
3468 int length;
3469 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003470 struct cgroup_iter it;
3471 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003472 struct cgroup_pidlist *l;
3473
3474 /*
3475 * If cgroup gets more users after we read count, we won't have
3476 * enough space - tough. This race is indistinguishable to the
3477 * caller from the case that the additional cgroup users didn't
3478 * show up until sometime later on.
3479 */
3480 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003481 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003482 if (!array)
3483 return -ENOMEM;
3484 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003485 cgroup_iter_start(cgrp, &it);
3486 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003487 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003488 break;
Ben Blum102a7752009-09-23 15:56:26 -07003489 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003490 if (type == CGROUP_FILE_PROCS)
3491 pid = task_tgid_vnr(tsk);
3492 else
3493 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003494 if (pid > 0) /* make sure to only use valid results */
3495 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003496 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003497 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003498 length = n;
3499 /* now sort & (if procs) strip out duplicates */
3500 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003501 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003502 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003503 l = cgroup_pidlist_find(cgrp, type);
3504 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003505 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003506 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003507 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003508 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003509 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003510 l->list = array;
3511 l->length = length;
3512 l->use_count++;
3513 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003514 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003515 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003516}
3517
Balbir Singh846c7bb2007-10-18 23:39:44 -07003518/**
Li Zefana043e3b2008-02-23 15:24:09 -08003519 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003520 * @stats: cgroupstats to fill information into
3521 * @dentry: A dentry entry belonging to the cgroup for which stats have
3522 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003523 *
3524 * Build and fill cgroupstats so that taskstats can export it to user
3525 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003526 */
3527int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3528{
3529 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003530 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003531 struct cgroup_iter it;
3532 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003533
Balbir Singh846c7bb2007-10-18 23:39:44 -07003534 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003535 * Validate dentry by checking the superblock operations,
3536 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003537 */
Li Zefan33d283b2008-11-19 15:36:48 -08003538 if (dentry->d_sb->s_op != &cgroup_ops ||
3539 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003540 goto err;
3541
3542 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003543 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003544
Paul Menagebd89aab2007-10-18 23:40:44 -07003545 cgroup_iter_start(cgrp, &it);
3546 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003547 switch (tsk->state) {
3548 case TASK_RUNNING:
3549 stats->nr_running++;
3550 break;
3551 case TASK_INTERRUPTIBLE:
3552 stats->nr_sleeping++;
3553 break;
3554 case TASK_UNINTERRUPTIBLE:
3555 stats->nr_uninterruptible++;
3556 break;
3557 case TASK_STOPPED:
3558 stats->nr_stopped++;
3559 break;
3560 default:
3561 if (delayacct_is_task_waiting_on_io(tsk))
3562 stats->nr_io_wait++;
3563 break;
3564 }
3565 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003566 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003567
Balbir Singh846c7bb2007-10-18 23:39:44 -07003568err:
3569 return ret;
3570}
3571
Paul Menage8f3ff202009-09-23 15:56:25 -07003572
Paul Menagecc31edc2008-10-18 20:28:04 -07003573/*
Ben Blum102a7752009-09-23 15:56:26 -07003574 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003575 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003576 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003577 */
3578
Ben Blum102a7752009-09-23 15:56:26 -07003579static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003580{
3581 /*
3582 * Initially we receive a position value that corresponds to
3583 * one more than the last pid shown (or 0 on the first call or
3584 * after a seek to the start). Use a binary-search to find the
3585 * next pid to display, if any
3586 */
Ben Blum102a7752009-09-23 15:56:26 -07003587 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003588 int index = 0, pid = *pos;
3589 int *iter;
3590
Ben Blum102a7752009-09-23 15:56:26 -07003591 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003592 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003593 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003594
Paul Menagecc31edc2008-10-18 20:28:04 -07003595 while (index < end) {
3596 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003597 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003598 index = mid;
3599 break;
Ben Blum102a7752009-09-23 15:56:26 -07003600 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003601 index = mid + 1;
3602 else
3603 end = mid;
3604 }
3605 }
3606 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003607 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003608 return NULL;
3609 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003610 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003611 *pos = *iter;
3612 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003613}
3614
Ben Blum102a7752009-09-23 15:56:26 -07003615static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003616{
Ben Blum102a7752009-09-23 15:56:26 -07003617 struct cgroup_pidlist *l = s->private;
3618 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003619}
3620
Ben Blum102a7752009-09-23 15:56:26 -07003621static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003622{
Ben Blum102a7752009-09-23 15:56:26 -07003623 struct cgroup_pidlist *l = s->private;
3624 pid_t *p = v;
3625 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003626 /*
3627 * Advance to the next pid in the array. If this goes off the
3628 * end, we're done
3629 */
3630 p++;
3631 if (p >= end) {
3632 return NULL;
3633 } else {
3634 *pos = *p;
3635 return p;
3636 }
3637}
3638
Ben Blum102a7752009-09-23 15:56:26 -07003639static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003640{
3641 return seq_printf(s, "%d\n", *(int *)v);
3642}
3643
Ben Blum102a7752009-09-23 15:56:26 -07003644/*
3645 * seq_operations functions for iterating on pidlists through seq_file -
3646 * independent of whether it's tasks or procs
3647 */
3648static const struct seq_operations cgroup_pidlist_seq_operations = {
3649 .start = cgroup_pidlist_start,
3650 .stop = cgroup_pidlist_stop,
3651 .next = cgroup_pidlist_next,
3652 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003653};
3654
Ben Blum102a7752009-09-23 15:56:26 -07003655static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003656{
Ben Blum72a8cb32009-09-23 15:56:27 -07003657 /*
3658 * the case where we're the last user of this particular pidlist will
3659 * have us remove it from the cgroup's list, which entails taking the
3660 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3661 * pidlist_mutex, we have to take pidlist_mutex first.
3662 */
3663 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003664 down_write(&l->mutex);
3665 BUG_ON(!l->use_count);
3666 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003667 /* we're the last user if refcount is 0; remove and free */
3668 list_del(&l->links);
3669 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003670 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003671 put_pid_ns(l->key.ns);
3672 up_write(&l->mutex);
3673 kfree(l);
3674 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003675 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003676 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003677 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003678}
3679
Ben Blum102a7752009-09-23 15:56:26 -07003680static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003681{
Ben Blum102a7752009-09-23 15:56:26 -07003682 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003683 if (!(file->f_mode & FMODE_READ))
3684 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003685 /*
3686 * the seq_file will only be initialized if the file was opened for
3687 * reading; hence we check if it's not null only in that case.
3688 */
3689 l = ((struct seq_file *)file->private_data)->private;
3690 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003691 return seq_release(inode, file);
3692}
3693
Ben Blum102a7752009-09-23 15:56:26 -07003694static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003695 .read = seq_read,
3696 .llseek = seq_lseek,
3697 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003698 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003699};
3700
3701/*
Ben Blum102a7752009-09-23 15:56:26 -07003702 * The following functions handle opens on a file that displays a pidlist
3703 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3704 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003705 */
Ben Blum102a7752009-09-23 15:56:26 -07003706/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003707static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003708{
3709 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003710 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003711 int retval;
3712
3713 /* Nothing to do for write-only files */
3714 if (!(file->f_mode & FMODE_READ))
3715 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003716
Ben Blum102a7752009-09-23 15:56:26 -07003717 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003718 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003719 if (retval)
3720 return retval;
3721 /* configure file information */
3722 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003723
Ben Blum102a7752009-09-23 15:56:26 -07003724 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003725 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003726 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003727 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003728 }
Ben Blum102a7752009-09-23 15:56:26 -07003729 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003730 return 0;
3731}
Ben Blum102a7752009-09-23 15:56:26 -07003732static int cgroup_tasks_open(struct inode *unused, struct file *file)
3733{
Ben Blum72a8cb32009-09-23 15:56:27 -07003734 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003735}
3736static int cgroup_procs_open(struct inode *unused, struct file *file)
3737{
Ben Blum72a8cb32009-09-23 15:56:27 -07003738 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003739}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003740
Paul Menagebd89aab2007-10-18 23:40:44 -07003741static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003742 struct cftype *cft)
3743{
Paul Menagebd89aab2007-10-18 23:40:44 -07003744 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003745}
3746
Paul Menage6379c102008-07-25 01:47:01 -07003747static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3748 struct cftype *cft,
3749 u64 val)
3750{
3751 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3752 if (val)
3753 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3754 else
3755 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3756 return 0;
3757}
3758
Paul Menagebbcb81d2007-10-18 23:39:32 -07003759/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003760 * Unregister event and free resources.
3761 *
3762 * Gets called from workqueue.
3763 */
3764static void cgroup_event_remove(struct work_struct *work)
3765{
3766 struct cgroup_event *event = container_of(work, struct cgroup_event,
3767 remove);
3768 struct cgroup *cgrp = event->cgrp;
3769
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003770 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3771
3772 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003773 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003774 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003775}
3776
3777/*
3778 * Gets called on POLLHUP on eventfd when user closes it.
3779 *
3780 * Called with wqh->lock held and interrupts disabled.
3781 */
3782static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3783 int sync, void *key)
3784{
3785 struct cgroup_event *event = container_of(wait,
3786 struct cgroup_event, wait);
3787 struct cgroup *cgrp = event->cgrp;
3788 unsigned long flags = (unsigned long)key;
3789
3790 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003791 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003792 spin_lock(&cgrp->event_list_lock);
Greg Thelen9718ceb2012-11-28 13:50:45 -08003793 list_del_init(&event->list);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003794 spin_unlock(&cgrp->event_list_lock);
3795 /*
3796 * We are in atomic context, but cgroup_event_remove() may
3797 * sleep, so we have to call it in workqueue.
3798 */
3799 schedule_work(&event->remove);
3800 }
3801
3802 return 0;
3803}
3804
3805static void cgroup_event_ptable_queue_proc(struct file *file,
3806 wait_queue_head_t *wqh, poll_table *pt)
3807{
3808 struct cgroup_event *event = container_of(pt,
3809 struct cgroup_event, pt);
3810
3811 event->wqh = wqh;
3812 add_wait_queue(wqh, &event->wait);
3813}
3814
3815/*
3816 * Parse input and register new cgroup event handler.
3817 *
3818 * Input must be in format '<event_fd> <control_fd> <args>'.
3819 * Interpretation of args is defined by control file implementation.
3820 */
3821static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3822 const char *buffer)
3823{
3824 struct cgroup_event *event = NULL;
3825 unsigned int efd, cfd;
3826 struct file *efile = NULL;
3827 struct file *cfile = NULL;
3828 char *endp;
3829 int ret;
3830
3831 efd = simple_strtoul(buffer, &endp, 10);
3832 if (*endp != ' ')
3833 return -EINVAL;
3834 buffer = endp + 1;
3835
3836 cfd = simple_strtoul(buffer, &endp, 10);
3837 if ((*endp != ' ') && (*endp != '\0'))
3838 return -EINVAL;
3839 buffer = endp + 1;
3840
3841 event = kzalloc(sizeof(*event), GFP_KERNEL);
3842 if (!event)
3843 return -ENOMEM;
3844 event->cgrp = cgrp;
3845 INIT_LIST_HEAD(&event->list);
3846 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3847 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3848 INIT_WORK(&event->remove, cgroup_event_remove);
3849
3850 efile = eventfd_fget(efd);
3851 if (IS_ERR(efile)) {
3852 ret = PTR_ERR(efile);
3853 goto fail;
3854 }
3855
3856 event->eventfd = eventfd_ctx_fileget(efile);
3857 if (IS_ERR(event->eventfd)) {
3858 ret = PTR_ERR(event->eventfd);
3859 goto fail;
3860 }
3861
3862 cfile = fget(cfd);
3863 if (!cfile) {
3864 ret = -EBADF;
3865 goto fail;
3866 }
3867
3868 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003869 /* AV: shouldn't we check that it's been opened for read instead? */
3870 ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003871 if (ret < 0)
3872 goto fail;
3873
3874 event->cft = __file_cft(cfile);
3875 if (IS_ERR(event->cft)) {
3876 ret = PTR_ERR(event->cft);
3877 goto fail;
3878 }
3879
3880 if (!event->cft->register_event || !event->cft->unregister_event) {
3881 ret = -EINVAL;
3882 goto fail;
3883 }
3884
3885 ret = event->cft->register_event(cgrp, event->cft,
3886 event->eventfd, buffer);
3887 if (ret)
3888 goto fail;
3889
3890 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3891 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3892 ret = 0;
3893 goto fail;
3894 }
3895
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003896 /*
3897 * Events should be removed after rmdir of cgroup directory, but before
3898 * destroying subsystem state objects. Let's take reference to cgroup
3899 * directory dentry to do that.
3900 */
3901 dget(cgrp->dentry);
3902
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003903 spin_lock(&cgrp->event_list_lock);
3904 list_add(&event->list, &cgrp->event_list);
3905 spin_unlock(&cgrp->event_list_lock);
3906
3907 fput(cfile);
3908 fput(efile);
3909
3910 return 0;
3911
3912fail:
3913 if (cfile)
3914 fput(cfile);
3915
3916 if (event && event->eventfd && !IS_ERR(event->eventfd))
3917 eventfd_ctx_put(event->eventfd);
3918
3919 if (!IS_ERR_OR_NULL(efile))
3920 fput(efile);
3921
3922 kfree(event);
3923
3924 return ret;
3925}
3926
Daniel Lezcano97978e62010-10-27 15:33:35 -07003927static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3928 struct cftype *cft)
3929{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003930 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003931}
3932
3933static int cgroup_clone_children_write(struct cgroup *cgrp,
3934 struct cftype *cft,
3935 u64 val)
3936{
3937 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003938 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003939 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003940 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003941 return 0;
3942}
3943
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003944/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003945 * for the common functions, 'private' gives the type of file
3946 */
Ben Blum102a7752009-09-23 15:56:26 -07003947/* for hysterical raisins, we can't put this on the older files */
3948#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003949static struct cftype files[] = {
3950 {
3951 .name = "tasks",
3952 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003953 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003954 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003955 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003956 },
Ben Blum102a7752009-09-23 15:56:26 -07003957 {
3958 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3959 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003960 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003961 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003962 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003963 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003964 {
3965 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003966 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003967 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003968 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003969 {
3970 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3971 .write_string = cgroup_write_event_control,
3972 .mode = S_IWUGO,
3973 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003974 {
3975 .name = "cgroup.clone_children",
3976 .read_u64 = cgroup_clone_children_read,
3977 .write_u64 = cgroup_clone_children_write,
3978 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003979 {
3980 .name = "release_agent",
3981 .flags = CFTYPE_ONLY_ON_ROOT,
3982 .read_seq_string = cgroup_release_agent_show,
3983 .write_string = cgroup_release_agent_write,
3984 .max_write_len = PATH_MAX,
3985 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003986 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003987};
3988
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003989/**
3990 * cgroup_populate_dir - selectively creation of files in a directory
3991 * @cgrp: target cgroup
3992 * @base_files: true if the base files should be added
3993 * @subsys_mask: mask of the subsystem ids whose files should be added
3994 */
3995static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3996 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003997{
3998 int err;
3999 struct cgroup_subsys *ss;
4000
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004001 if (base_files) {
4002 err = cgroup_addrm_files(cgrp, NULL, files, true);
4003 if (err < 0)
4004 return err;
4005 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004006
Tejun Heo8e3f6542012-04-01 12:09:55 -07004007 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004008 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004009 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004010 if (!test_bit(ss->subsys_id, &subsys_mask))
4011 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004012
Tejun Heodb0416b2012-04-01 12:09:55 -07004013 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004014 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004015 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004016
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004017 /* This cgroup is ready now */
4018 for_each_subsys(cgrp->root, ss) {
4019 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4020 /*
4021 * Update id->css pointer and make this css visible from
4022 * CSS ID functions. This pointer will be dereferened
4023 * from RCU-read-side without locks.
4024 */
4025 if (css->id)
4026 rcu_assign_pointer(css->id->css, css);
4027 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004028
4029 return 0;
4030}
4031
Tejun Heo48ddbe12012-04-01 12:09:56 -07004032static void css_dput_fn(struct work_struct *work)
4033{
4034 struct cgroup_subsys_state *css =
4035 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004036 struct dentry *dentry = css->cgroup->dentry;
4037 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004038
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004039 atomic_inc(&sb->s_active);
4040 dput(dentry);
4041 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004042}
4043
Paul Menageddbcc7e2007-10-18 23:39:30 -07004044static void init_cgroup_css(struct cgroup_subsys_state *css,
4045 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004046 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004047{
Paul Menagebd89aab2007-10-18 23:40:44 -07004048 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004049 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004050 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004051 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004052 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004053 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004054 BUG_ON(cgrp->subsys[ss->subsys_id]);
4055 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004056
4057 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004058 * css holds an extra ref to @cgrp->dentry which is put on the last
4059 * css_put(). dput() requires process context, which css_put() may
4060 * be called without. @css->dput_work will be used to invoke
4061 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004062 */
4063 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004064}
4065
Tejun Heob1929db2012-11-19 08:13:38 -08004066/* invoke ->post_create() on a new CSS and mark it online if successful */
4067static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004068{
Tejun Heob1929db2012-11-19 08:13:38 -08004069 int ret = 0;
4070
Tejun Heoa31f2d32012-11-19 08:13:37 -08004071 lockdep_assert_held(&cgroup_mutex);
4072
Tejun Heo92fb9742012-11-19 08:13:38 -08004073 if (ss->css_online)
4074 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004075 if (!ret)
4076 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4077 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004078}
4079
4080/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4081static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4082 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4083{
4084 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4085
4086 lockdep_assert_held(&cgroup_mutex);
4087
4088 if (!(css->flags & CSS_ONLINE))
4089 return;
4090
4091 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004092 * css_offline() should be called with cgroup_mutex unlocked. See
Tejun Heoa31f2d32012-11-19 08:13:37 -08004093 * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for
4094 * details. This temporary unlocking should go away once
4095 * cgroup_mutex is unexported from controllers.
4096 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004097 if (ss->css_offline) {
Tejun Heoa31f2d32012-11-19 08:13:37 -08004098 mutex_unlock(&cgroup_mutex);
Tejun Heo92fb9742012-11-19 08:13:38 -08004099 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004100 mutex_lock(&cgroup_mutex);
4101 }
4102
4103 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4104}
4105
Paul Menageddbcc7e2007-10-18 23:39:30 -07004106/*
Li Zefana043e3b2008-02-23 15:24:09 -08004107 * cgroup_create - create a cgroup
4108 * @parent: cgroup that will be parent of the new cgroup
4109 * @dentry: dentry of the new cgroup
4110 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004111 *
Li Zefana043e3b2008-02-23 15:24:09 -08004112 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004113 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004114static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004115 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004116{
Paul Menagebd89aab2007-10-18 23:40:44 -07004117 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004118 struct cgroupfs_root *root = parent->root;
4119 int err = 0;
4120 struct cgroup_subsys *ss;
4121 struct super_block *sb = root->sb;
4122
Tejun Heo0a950f62012-11-19 09:02:12 -08004123 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004124 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4125 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004126 return -ENOMEM;
4127
Tejun Heo0a950f62012-11-19 09:02:12 -08004128 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4129 if (cgrp->id < 0)
4130 goto err_free_cgrp;
4131
Tejun Heo976c06b2012-11-05 09:16:59 -08004132 /*
4133 * Only live parents can have children. Note that the liveliness
4134 * check isn't strictly necessary because cgroup_mkdir() and
4135 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4136 * anyway so that locking is contained inside cgroup proper and we
4137 * don't get nasty surprises if we ever grow another caller.
4138 */
4139 if (!cgroup_lock_live_group(parent)) {
4140 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004141 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004142 }
4143
Paul Menageddbcc7e2007-10-18 23:39:30 -07004144 /* Grab a reference on the superblock so the hierarchy doesn't
4145 * get deleted on unmount if there are child cgroups. This
4146 * can be done outside cgroup_mutex, since the sb can't
4147 * disappear while someone has an open control file on the
4148 * fs */
4149 atomic_inc(&sb->s_active);
4150
Paul Menagecc31edc2008-10-18 20:28:04 -07004151 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004152
Paul Menagebd89aab2007-10-18 23:40:44 -07004153 cgrp->parent = parent;
4154 cgrp->root = parent->root;
4155 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004156
Li Zefanb6abdb02008-03-04 14:28:19 -08004157 if (notify_on_release(parent))
4158 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4159
Tejun Heo2260e7f2012-11-19 08:13:38 -08004160 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4161 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004162
Paul Menageddbcc7e2007-10-18 23:39:30 -07004163 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004164 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004165
Tejun Heo92fb9742012-11-19 08:13:38 -08004166 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004167 if (IS_ERR(css)) {
4168 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004169 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004170 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004171 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004172 if (ss->use_id) {
4173 err = alloc_css_id(ss, parent, cgrp);
4174 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004175 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004176 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004177 }
4178
Tejun Heo4e139af2012-11-19 08:13:36 -08004179 /*
4180 * Create directory. cgroup_create_file() returns with the new
4181 * directory locked on success so that it can be populated without
4182 * dropping cgroup_mutex.
4183 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004184 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004185 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004186 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004187 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004188
Tejun Heo4e139af2012-11-19 08:13:36 -08004189 /* allocation complete, commit to creation */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004190 dentry->d_fsdata = cgrp;
Tejun Heofebfcef2012-11-19 08:13:36 -08004191 cgrp->dentry = dentry;
Tejun Heo4e139af2012-11-19 08:13:36 -08004192 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4193 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4194 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004195
Tejun Heob1929db2012-11-19 08:13:38 -08004196 /* each css holds a ref to the cgroup's dentry */
4197 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004198 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004199
Tejun Heob1929db2012-11-19 08:13:38 -08004200 /* creation succeeded, notify subsystems */
4201 for_each_subsys(root, ss) {
4202 err = online_css(ss, cgrp);
4203 if (err)
4204 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004205
4206 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4207 parent->parent) {
4208 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",
4209 current->comm, current->pid, ss->name);
4210 if (!strcmp(ss->name, "memory"))
4211 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4212 ss->warned_broken_hierarchy = true;
4213 }
Tejun Heoa8638032012-11-09 09:12:29 -08004214 }
4215
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004216 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004217 if (err)
4218 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004219
4220 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004221 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004222
4223 return 0;
4224
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004225err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004226 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004227 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004228 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004229 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004230 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004231 /* Release the reference count that we took on the superblock */
4232 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004233err_free_id:
4234 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004235err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004236 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004237 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004238
4239err_destroy:
4240 cgroup_destroy_locked(cgrp);
4241 mutex_unlock(&cgroup_mutex);
4242 mutex_unlock(&dentry->d_inode->i_mutex);
4243 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004244}
4245
Al Viro18bb1db2011-07-26 01:41:39 -04004246static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004247{
4248 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4249
4250 /* the vfs holds inode->i_mutex already */
4251 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4252}
4253
Tejun Heo28b4c272012-04-01 12:09:56 -07004254/*
4255 * Check the reference count on each subsystem. Since we already
4256 * established that there are no tasks in the cgroup, if the css refcount
4257 * is also 1, then there should be no outstanding references, so the
4258 * subsystem is safe to destroy. We scan across all subsystems rather than
4259 * using the per-hierarchy linked list of mounted subsystems since we can
4260 * be called via check_for_release() with no synchronization other than
4261 * RCU, and the subsystem linked list isn't RCU-safe.
4262 */
Li Zefan55b6fd02008-07-29 22:33:20 -07004263static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004264{
Paul Menage81a6a5c2007-10-18 23:39:38 -07004265 int i;
Tejun Heo28b4c272012-04-01 12:09:56 -07004266
Ben Blumaae8aab2010-03-10 15:22:07 -08004267 /*
4268 * We won't need to lock the subsys array, because the subsystems
4269 * we're concerned about aren't going anywhere since our cgroup root
4270 * has a reference on them.
4271 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004272 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4273 struct cgroup_subsys *ss = subsys[i];
4274 struct cgroup_subsys_state *css;
Tejun Heo28b4c272012-04-01 12:09:56 -07004275
Ben Blumaae8aab2010-03-10 15:22:07 -08004276 /* Skip subsystems not present or not in this hierarchy */
4277 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004278 continue;
Tejun Heo28b4c272012-04-01 12:09:56 -07004279
Paul Menagebd89aab2007-10-18 23:40:44 -07004280 css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004281 /*
4282 * When called from check_for_release() it's possible
Paul Menage81a6a5c2007-10-18 23:39:38 -07004283 * that by this point the cgroup has been removed
4284 * and the css deleted. But a false-positive doesn't
4285 * matter, since it can only happen if the cgroup
4286 * has been deleted and hence no longer needs the
Tejun Heo28b4c272012-04-01 12:09:56 -07004287 * release agent to be called anyway.
4288 */
4289 if (css && css_refcnt(css) > 1)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004290 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004291 }
4292 return 0;
4293}
4294
Tejun Heo42809dd2012-11-19 08:13:37 -08004295static int cgroup_destroy_locked(struct cgroup *cgrp)
4296 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004297{
Tejun Heo42809dd2012-11-19 08:13:37 -08004298 struct dentry *d = cgrp->dentry;
4299 struct cgroup *parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004300 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004301 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004302 struct cgroup_subsys *ss;
Greg Thelen205a8722012-11-28 13:50:44 -08004303 LIST_HEAD(tmp_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004304
Tejun Heo42809dd2012-11-19 08:13:37 -08004305 lockdep_assert_held(&d->d_inode->i_mutex);
4306 lockdep_assert_held(&cgroup_mutex);
4307
4308 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004309 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004310
Tejun Heo1a90dd52012-11-05 09:16:59 -08004311 /*
4312 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4313 * removed. This makes future css_tryget() and child creation
4314 * attempts fail thus maintaining the removal conditions verified
4315 * above.
4316 */
Tejun Heoed9577932012-11-05 09:16:58 -08004317 for_each_subsys(cgrp->root, ss) {
4318 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4319
4320 WARN_ON(atomic_read(&css->refcnt) < 0);
4321 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004322 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004323 set_bit(CGRP_REMOVED, &cgrp->flags);
4324
Tejun Heoa31f2d32012-11-19 08:13:37 -08004325 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004326 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004327 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004328
4329 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004330 * Put all the base refs. Each css holds an extra reference to the
4331 * cgroup's dentry and cgroup removal proceeds regardless of css
4332 * refs. On the last put of each css, whenever that may be, the
4333 * extra dentry ref is put so that dentry destruction happens only
4334 * after all css's are released.
4335 */
Tejun Heoe9316082012-11-05 09:16:58 -08004336 for_each_subsys(cgrp->root, ss)
4337 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004338
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004339 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004340 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004341 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004342 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004343
Paul Menage999cd8a2009-01-07 18:08:36 -08004344 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004345 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004346 list_del_init(&cgrp->allcg_node);
4347
Tejun Heo42809dd2012-11-19 08:13:37 -08004348 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004349 cgroup_d_remove_dir(d);
4350 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004351
Paul Menagebd89aab2007-10-18 23:40:44 -07004352 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004353 check_for_release(parent);
4354
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004355 /*
4356 * Unregister events and notify userspace.
4357 * Notify userspace about cgroup removing only after rmdir of cgroup
Greg Thelen205a8722012-11-28 13:50:44 -08004358 * directory to avoid race between userspace and kernelspace. Use
4359 * a temporary list to avoid a deadlock with cgroup_event_wake(). Since
4360 * cgroup_event_wake() is called with the wait queue head locked,
4361 * remove_wait_queue() cannot be called while holding event_list_lock.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004362 */
4363 spin_lock(&cgrp->event_list_lock);
Greg Thelen205a8722012-11-28 13:50:44 -08004364 list_splice_init(&cgrp->event_list, &tmp_list);
4365 spin_unlock(&cgrp->event_list_lock);
4366 list_for_each_entry_safe(event, tmp, &tmp_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004367 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004368 remove_wait_queue(event->wqh, &event->wait);
4369 eventfd_signal(event->eventfd, 1);
4370 schedule_work(&event->remove);
4371 }
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004372
Paul Menageddbcc7e2007-10-18 23:39:30 -07004373 return 0;
4374}
4375
Tejun Heo42809dd2012-11-19 08:13:37 -08004376static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4377{
4378 int ret;
4379
4380 mutex_lock(&cgroup_mutex);
4381 ret = cgroup_destroy_locked(dentry->d_fsdata);
4382 mutex_unlock(&cgroup_mutex);
4383
4384 return ret;
4385}
4386
Tejun Heo8e3f6542012-04-01 12:09:55 -07004387static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4388{
4389 INIT_LIST_HEAD(&ss->cftsets);
4390
4391 /*
4392 * base_cftset is embedded in subsys itself, no need to worry about
4393 * deregistration.
4394 */
4395 if (ss->base_cftypes) {
4396 ss->base_cftset.cfts = ss->base_cftypes;
4397 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4398 }
4399}
4400
Li Zefan06a11922008-04-29 01:00:07 -07004401static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004402{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004403 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004404
4405 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004406
Tejun Heo648bb562012-11-19 08:13:36 -08004407 mutex_lock(&cgroup_mutex);
4408
Tejun Heo8e3f6542012-04-01 12:09:55 -07004409 /* init base cftset */
4410 cgroup_init_cftsets(ss);
4411
Paul Menageddbcc7e2007-10-18 23:39:30 -07004412 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004413 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004414 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004415 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004416 /* We don't handle early failures gracefully */
4417 BUG_ON(IS_ERR(css));
4418 init_cgroup_css(css, ss, dummytop);
4419
Li Zefane8d55fd2008-04-29 01:00:13 -07004420 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004421 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004422 * newly registered, all tasks and hence the
4423 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004424 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004425
4426 need_forkexit_callback |= ss->fork || ss->exit;
4427
Li Zefane8d55fd2008-04-29 01:00:13 -07004428 /* At system boot, before all subsystems have been
4429 * registered, no tasks have been forked, so we don't
4430 * need to invoke fork callbacks here. */
4431 BUG_ON(!list_empty(&init_task.tasks));
4432
Paul Menageddbcc7e2007-10-18 23:39:30 -07004433 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004434 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004435
Tejun Heo648bb562012-11-19 08:13:36 -08004436 mutex_unlock(&cgroup_mutex);
4437
Ben Blume6a11052010-03-10 15:22:09 -08004438 /* this function shouldn't be used with modular subsystems, since they
4439 * need to register a subsys_id, among other things */
4440 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004441}
4442
4443/**
Ben Blume6a11052010-03-10 15:22:09 -08004444 * cgroup_load_subsys: load and register a modular subsystem at runtime
4445 * @ss: the subsystem to load
4446 *
4447 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004448 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004449 * up for use. If the subsystem is built-in anyway, work is delegated to the
4450 * simpler cgroup_init_subsys.
4451 */
4452int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4453{
Ben Blume6a11052010-03-10 15:22:09 -08004454 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004455 int i, ret;
Li Zefan0ac801f2013-01-10 11:49:27 +08004456 struct hlist_node *node, *tmp;
4457 struct css_set *cg;
4458 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004459
4460 /* check name and function validity */
4461 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004462 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004463 return -EINVAL;
4464
4465 /*
4466 * we don't support callbacks in modular subsystems. this check is
4467 * before the ss->module check for consistency; a subsystem that could
4468 * be a module should still have no callbacks even if the user isn't
4469 * compiling it as one.
4470 */
4471 if (ss->fork || ss->exit)
4472 return -EINVAL;
4473
4474 /*
4475 * an optionally modular subsystem is built-in: we want to do nothing,
4476 * since cgroup_init_subsys will have already taken care of it.
4477 */
4478 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004479 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004480 BUG_ON(subsys[ss->subsys_id] != ss);
4481 return 0;
4482 }
4483
Tejun Heo8e3f6542012-04-01 12:09:55 -07004484 /* init base cftset */
4485 cgroup_init_cftsets(ss);
4486
Ben Blume6a11052010-03-10 15:22:09 -08004487 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004488 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004489
4490 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004491 * no ss->css_alloc seems to need anything important in the ss
4492 * struct, so this can happen first (i.e. before the rootnode
4493 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004494 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004495 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004496 if (IS_ERR(css)) {
4497 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004498 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004499 mutex_unlock(&cgroup_mutex);
4500 return PTR_ERR(css);
4501 }
4502
4503 list_add(&ss->sibling, &rootnode.subsys_list);
4504 ss->root = &rootnode;
4505
4506 /* our new subsystem will be attached to the dummy hierarchy. */
4507 init_cgroup_css(css, ss, dummytop);
4508 /* init_idr must be after init_cgroup_css because it sets css->id. */
4509 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004510 ret = cgroup_init_idr(ss, css);
4511 if (ret)
4512 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004513 }
4514
4515 /*
4516 * Now we need to entangle the css into the existing css_sets. unlike
4517 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4518 * will need a new pointer to it; done by iterating the css_set_table.
4519 * furthermore, modifying the existing css_sets will corrupt the hash
4520 * table state, so each changed css_set will need its hash recomputed.
4521 * this is all done under the css_set_lock.
4522 */
4523 write_lock(&css_set_lock);
Li Zefan0ac801f2013-01-10 11:49:27 +08004524 hash_for_each_safe(css_set_table, i, node, tmp, cg, hlist) {
4525 /* skip entries that we already rehashed */
4526 if (cg->subsys[ss->subsys_id])
4527 continue;
4528 /* remove existing entry */
4529 hash_del(&cg->hlist);
4530 /* set new value */
4531 cg->subsys[ss->subsys_id] = css;
4532 /* recompute hash and restore entry */
4533 key = css_set_hash(cg->subsys);
4534 hash_add(css_set_table, node, key);
Ben Blume6a11052010-03-10 15:22:09 -08004535 }
4536 write_unlock(&css_set_lock);
4537
Ben Blume6a11052010-03-10 15:22:09 -08004538 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004539 ret = online_css(ss, dummytop);
4540 if (ret)
4541 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004542
Ben Blume6a11052010-03-10 15:22:09 -08004543 /* success! */
4544 mutex_unlock(&cgroup_mutex);
4545 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004546
4547err_unload:
4548 mutex_unlock(&cgroup_mutex);
4549 /* @ss can't be mounted here as try_module_get() would fail */
4550 cgroup_unload_subsys(ss);
4551 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004552}
4553EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4554
4555/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004556 * cgroup_unload_subsys: unload a modular subsystem
4557 * @ss: the subsystem to unload
4558 *
4559 * This function should be called in a modular subsystem's exitcall. When this
4560 * function is invoked, the refcount on the subsystem's module will be 0, so
4561 * the subsystem will not be attached to any hierarchy.
4562 */
4563void cgroup_unload_subsys(struct cgroup_subsys *ss)
4564{
4565 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004566
4567 BUG_ON(ss->module == NULL);
4568
4569 /*
4570 * we shouldn't be called if the subsystem is in use, and the use of
4571 * try_module_get in parse_cgroupfs_options should ensure that it
4572 * doesn't start being used while we're killing it off.
4573 */
4574 BUG_ON(ss->root != &rootnode);
4575
4576 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004577
Tejun Heoa31f2d32012-11-19 08:13:37 -08004578 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004579 ss->active = 0;
4580
4581 if (ss->use_id) {
4582 idr_remove_all(&ss->idr);
4583 idr_destroy(&ss->idr);
4584 }
4585
Ben Blumcf5d5942010-03-10 15:22:09 -08004586 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004587 subsys[ss->subsys_id] = NULL;
4588
4589 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004590 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004591
4592 /*
4593 * disentangle the css from all css_sets attached to the dummytop. as
4594 * in loading, we need to pay our respects to the hashtable gods.
4595 */
4596 write_lock(&css_set_lock);
4597 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4598 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004599 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004600
Li Zefan0ac801f2013-01-10 11:49:27 +08004601 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004602 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004603 key = css_set_hash(cg->subsys);
4604 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004605 }
4606 write_unlock(&css_set_lock);
4607
4608 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004609 * remove subsystem's css from the dummytop and free it - need to
4610 * free before marking as null because ss->css_free needs the
4611 * cgrp->subsys pointer to find their state. note that this also
4612 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004613 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004614 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004615 dummytop->subsys[ss->subsys_id] = NULL;
4616
4617 mutex_unlock(&cgroup_mutex);
4618}
4619EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4620
4621/**
Li Zefana043e3b2008-02-23 15:24:09 -08004622 * cgroup_init_early - cgroup initialization at system boot
4623 *
4624 * Initialize cgroups at system boot, and initialize any
4625 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004626 */
4627int __init cgroup_init_early(void)
4628{
4629 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004630 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004631 INIT_LIST_HEAD(&init_css_set.cg_links);
4632 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004633 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004634 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004635 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004636 root_count = 1;
4637 init_task.cgroups = &init_css_set;
4638
4639 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004640 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004641 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004642 &rootnode.top_cgroup.css_sets);
4643 list_add(&init_css_set_link.cg_link_list,
4644 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004645
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004646 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004647 struct cgroup_subsys *ss = subsys[i];
4648
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004649 /* at bootup time, we don't worry about modular subsystems */
4650 if (!ss || ss->module)
4651 continue;
4652
Paul Menageddbcc7e2007-10-18 23:39:30 -07004653 BUG_ON(!ss->name);
4654 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004655 BUG_ON(!ss->css_alloc);
4656 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004657 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004658 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004659 ss->name, ss->subsys_id);
4660 BUG();
4661 }
4662
4663 if (ss->early_init)
4664 cgroup_init_subsys(ss);
4665 }
4666 return 0;
4667}
4668
4669/**
Li Zefana043e3b2008-02-23 15:24:09 -08004670 * cgroup_init - cgroup initialization
4671 *
4672 * Register cgroup filesystem and /proc file, and initialize
4673 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004674 */
4675int __init cgroup_init(void)
4676{
4677 int err;
4678 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004679 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004680
4681 err = bdi_init(&cgroup_backing_dev_info);
4682 if (err)
4683 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004684
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004685 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004686 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004687
4688 /* at bootup time, we don't worry about modular subsystems */
4689 if (!ss || ss->module)
4690 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004691 if (!ss->early_init)
4692 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004693 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004694 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004695 }
4696
Li Zefan472b1052008-04-29 01:00:11 -07004697 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004698 key = css_set_hash(init_css_set.subsys);
4699 hash_add(css_set_table, &init_css_set.hlist, key);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004700 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004701
4702 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4703 if (!cgroup_kobj) {
4704 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004705 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004706 }
4707
4708 err = register_filesystem(&cgroup_fs_type);
4709 if (err < 0) {
4710 kobject_put(cgroup_kobj);
4711 goto out;
4712 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004713
Li Zefan46ae2202008-04-29 01:00:08 -07004714 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004715
Paul Menageddbcc7e2007-10-18 23:39:30 -07004716out:
Paul Menagea4243162007-10-18 23:39:35 -07004717 if (err)
4718 bdi_destroy(&cgroup_backing_dev_info);
4719
Paul Menageddbcc7e2007-10-18 23:39:30 -07004720 return err;
4721}
Paul Menageb4f48b62007-10-18 23:39:33 -07004722
Paul Menagea4243162007-10-18 23:39:35 -07004723/*
4724 * proc_cgroup_show()
4725 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4726 * - Used for /proc/<pid>/cgroup.
4727 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4728 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004729 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004730 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4731 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4732 * cgroup to top_cgroup.
4733 */
4734
4735/* TODO: Use a proper seq_file iterator */
4736static int proc_cgroup_show(struct seq_file *m, void *v)
4737{
4738 struct pid *pid;
4739 struct task_struct *tsk;
4740 char *buf;
4741 int retval;
4742 struct cgroupfs_root *root;
4743
4744 retval = -ENOMEM;
4745 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4746 if (!buf)
4747 goto out;
4748
4749 retval = -ESRCH;
4750 pid = m->private;
4751 tsk = get_pid_task(pid, PIDTYPE_PID);
4752 if (!tsk)
4753 goto out_free;
4754
4755 retval = 0;
4756
4757 mutex_lock(&cgroup_mutex);
4758
Li Zefane5f6a862009-01-07 18:07:41 -08004759 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004760 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004761 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004762 int count = 0;
4763
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004764 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004765 for_each_subsys(root, ss)
4766 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004767 if (strlen(root->name))
4768 seq_printf(m, "%sname=%s", count ? "," : "",
4769 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004770 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004771 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004772 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004773 if (retval < 0)
4774 goto out_unlock;
4775 seq_puts(m, buf);
4776 seq_putc(m, '\n');
4777 }
4778
4779out_unlock:
4780 mutex_unlock(&cgroup_mutex);
4781 put_task_struct(tsk);
4782out_free:
4783 kfree(buf);
4784out:
4785 return retval;
4786}
4787
4788static int cgroup_open(struct inode *inode, struct file *file)
4789{
4790 struct pid *pid = PROC_I(inode)->pid;
4791 return single_open(file, proc_cgroup_show, pid);
4792}
4793
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004794const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004795 .open = cgroup_open,
4796 .read = seq_read,
4797 .llseek = seq_lseek,
4798 .release = single_release,
4799};
4800
4801/* Display information about each subsystem and each hierarchy */
4802static int proc_cgroupstats_show(struct seq_file *m, void *v)
4803{
4804 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004805
Paul Menage8bab8dd2008-04-04 14:29:57 -07004806 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004807 /*
4808 * ideally we don't want subsystems moving around while we do this.
4809 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4810 * subsys/hierarchy state.
4811 */
Paul Menagea4243162007-10-18 23:39:35 -07004812 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004813 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4814 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004815 if (ss == NULL)
4816 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004817 seq_printf(m, "%s\t%d\t%d\t%d\n",
4818 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004819 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004820 }
4821 mutex_unlock(&cgroup_mutex);
4822 return 0;
4823}
4824
4825static int cgroupstats_open(struct inode *inode, struct file *file)
4826{
Al Viro9dce07f2008-03-29 03:07:28 +00004827 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004828}
4829
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004830static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004831 .open = cgroupstats_open,
4832 .read = seq_read,
4833 .llseek = seq_lseek,
4834 .release = single_release,
4835};
4836
Paul Menageb4f48b62007-10-18 23:39:33 -07004837/**
4838 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004839 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004840 *
4841 * Description: A task inherits its parent's cgroup at fork().
4842 *
4843 * A pointer to the shared css_set was automatically copied in
4844 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004845 * it was not made under the protection of RCU or cgroup_mutex, so
4846 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4847 * have already changed current->cgroups, allowing the previously
4848 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004849 *
4850 * At the point that cgroup_fork() is called, 'current' is the parent
4851 * task, and the passed argument 'child' points to the child task.
4852 */
4853void cgroup_fork(struct task_struct *child)
4854{
Tejun Heo9bb71302012-10-18 17:52:07 -07004855 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004856 child->cgroups = current->cgroups;
4857 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004858 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004859 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004860}
4861
4862/**
Li Zefana043e3b2008-02-23 15:24:09 -08004863 * cgroup_post_fork - called on a new task after adding it to the task list
4864 * @child: the task in question
4865 *
Tejun Heo5edee612012-10-16 15:03:14 -07004866 * Adds the task to the list running through its css_set if necessary and
4867 * call the subsystem fork() callbacks. Has to be after the task is
4868 * visible on the task list in case we race with the first call to
4869 * cgroup_iter_start() - to guarantee that the new task ends up on its
4870 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004871 */
Paul Menage817929e2007-10-18 23:39:36 -07004872void cgroup_post_fork(struct task_struct *child)
4873{
Tejun Heo5edee612012-10-16 15:03:14 -07004874 int i;
4875
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004876 /*
4877 * use_task_css_set_links is set to 1 before we walk the tasklist
4878 * under the tasklist_lock and we read it here after we added the child
4879 * to the tasklist under the tasklist_lock as well. If the child wasn't
4880 * yet in the tasklist when we walked through it from
4881 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4882 * should be visible now due to the paired locking and barriers implied
4883 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4884 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4885 * lock on fork.
4886 */
Paul Menage817929e2007-10-18 23:39:36 -07004887 if (use_task_css_set_links) {
4888 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004889 task_lock(child);
4890 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004891 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004892 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004893 write_unlock(&css_set_lock);
4894 }
Tejun Heo5edee612012-10-16 15:03:14 -07004895
4896 /*
4897 * Call ss->fork(). This must happen after @child is linked on
4898 * css_set; otherwise, @child might change state between ->fork()
4899 * and addition to css_set.
4900 */
4901 if (need_forkexit_callback) {
4902 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4903 struct cgroup_subsys *ss = subsys[i];
4904
4905 /*
4906 * fork/exit callbacks are supported only for
4907 * builtin subsystems and we don't need further
4908 * synchronization as they never go away.
4909 */
4910 if (!ss || ss->module)
4911 continue;
4912
4913 if (ss->fork)
4914 ss->fork(child);
4915 }
4916 }
Paul Menage817929e2007-10-18 23:39:36 -07004917}
Tejun Heo5edee612012-10-16 15:03:14 -07004918
Paul Menage817929e2007-10-18 23:39:36 -07004919/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004920 * cgroup_exit - detach cgroup from exiting task
4921 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004922 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004923 *
4924 * Description: Detach cgroup from @tsk and release it.
4925 *
4926 * Note that cgroups marked notify_on_release force every task in
4927 * them to take the global cgroup_mutex mutex when exiting.
4928 * This could impact scaling on very large systems. Be reluctant to
4929 * use notify_on_release cgroups where very high task exit scaling
4930 * is required on large systems.
4931 *
4932 * the_top_cgroup_hack:
4933 *
4934 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4935 *
4936 * We call cgroup_exit() while the task is still competent to
4937 * handle notify_on_release(), then leave the task attached to the
4938 * root cgroup in each hierarchy for the remainder of its exit.
4939 *
4940 * To do this properly, we would increment the reference count on
4941 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4942 * code we would add a second cgroup function call, to drop that
4943 * reference. This would just create an unnecessary hot spot on
4944 * the top_cgroup reference count, to no avail.
4945 *
4946 * Normally, holding a reference to a cgroup without bumping its
4947 * count is unsafe. The cgroup could go away, or someone could
4948 * attach us to a different cgroup, decrementing the count on
4949 * the first cgroup that we never incremented. But in this case,
4950 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004951 * which wards off any cgroup_attach_task() attempts, or task is a failed
4952 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004953 */
4954void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4955{
Paul Menage817929e2007-10-18 23:39:36 -07004956 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004957 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004958
4959 /*
4960 * Unlink from the css_set task list if necessary.
4961 * Optimistically check cg_list before taking
4962 * css_set_lock
4963 */
4964 if (!list_empty(&tsk->cg_list)) {
4965 write_lock(&css_set_lock);
4966 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004967 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004968 write_unlock(&css_set_lock);
4969 }
4970
Paul Menageb4f48b62007-10-18 23:39:33 -07004971 /* Reassign the task to the init_css_set. */
4972 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004973 cg = tsk->cgroups;
4974 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004975
4976 if (run_callbacks && need_forkexit_callback) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004977 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004978 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004979
4980 /* modular subsystems can't use callbacks */
4981 if (!ss || ss->module)
4982 continue;
4983
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004984 if (ss->exit) {
4985 struct cgroup *old_cgrp =
4986 rcu_dereference_raw(cg->subsys[i])->cgroup;
4987 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004988 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004989 }
4990 }
4991 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004992 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004993
Paul Menage817929e2007-10-18 23:39:36 -07004994 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004995 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004996}
Paul Menage697f4162007-10-18 23:39:34 -07004997
4998/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004999 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08005000 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005001 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08005002 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005003 * See if @cgrp is a descendant of @task's cgroup in the appropriate
5004 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07005005 *
5006 * If we are sending in dummytop, then presumably we are creating
5007 * the top cgroup in the subsystem.
5008 *
5009 * Called only by the ns (nsproxy) cgroup.
5010 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005011int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07005012{
5013 int ret;
5014 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07005015
Paul Menagebd89aab2007-10-18 23:40:44 -07005016 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07005017 return 1;
5018
Paul Menage7717f7b2009-09-23 15:56:22 -07005019 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005020 while (cgrp != target && cgrp!= cgrp->top_cgroup)
5021 cgrp = cgrp->parent;
5022 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07005023 return ret;
5024}
Paul Menage81a6a5c2007-10-18 23:39:38 -07005025
Paul Menagebd89aab2007-10-18 23:40:44 -07005026static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005027{
5028 /* All of these checks rely on RCU to keep the cgroup
5029 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07005030 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
5031 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07005032 /* Control Group is currently removeable. If it's not
5033 * already queued for a userspace notification, queue
5034 * it now */
5035 int need_schedule_work = 0;
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005036 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07005037 if (!cgroup_is_removed(cgrp) &&
5038 list_empty(&cgrp->release_list)) {
5039 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005040 need_schedule_work = 1;
5041 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005042 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005043 if (need_schedule_work)
5044 schedule_work(&release_agent_work);
5045 }
5046}
5047
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005048/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005049bool __css_tryget(struct cgroup_subsys_state *css)
5050{
Tejun Heoe9316082012-11-05 09:16:58 -08005051 while (true) {
5052 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005053
Tejun Heoe9316082012-11-05 09:16:58 -08005054 v = css_refcnt(css);
5055 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5056 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005057 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005058 else if (t < 0)
5059 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005060 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005061 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005062}
5063EXPORT_SYMBOL_GPL(__css_tryget);
5064
5065/* Caller must verify that the css is not for root cgroup */
5066void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005067{
Paul Menagebd89aab2007-10-18 23:40:44 -07005068 struct cgroup *cgrp = css->cgroup;
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005069 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005070
Paul Menage81a6a5c2007-10-18 23:39:38 -07005071 rcu_read_lock();
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005072 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
5073
5074 switch (v) {
Tejun Heo48ddbe12012-04-01 12:09:56 -07005075 case 1:
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005076 if (notify_on_release(cgrp)) {
5077 set_bit(CGRP_RELEASABLE, &cgrp->flags);
5078 check_for_release(cgrp);
5079 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07005080 break;
5081 case 0:
Tejun Heoed9577932012-11-05 09:16:58 -08005082 schedule_work(&css->dput_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005083 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005084 }
5085 rcu_read_unlock();
5086}
Ben Blum67523c42010-03-10 15:22:11 -08005087EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005088
5089/*
5090 * Notify userspace when a cgroup is released, by running the
5091 * configured release agent with the name of the cgroup (path
5092 * relative to the root of cgroup file system) as the argument.
5093 *
5094 * Most likely, this user command will try to rmdir this cgroup.
5095 *
5096 * This races with the possibility that some other task will be
5097 * attached to this cgroup before it is removed, or that some other
5098 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5099 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5100 * unused, and this cgroup will be reprieved from its death sentence,
5101 * to continue to serve a useful existence. Next time it's released,
5102 * we will get notified again, if it still has 'notify_on_release' set.
5103 *
5104 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5105 * means only wait until the task is successfully execve()'d. The
5106 * separate release agent task is forked by call_usermodehelper(),
5107 * then control in this thread returns here, without waiting for the
5108 * release agent task. We don't bother to wait because the caller of
5109 * this routine has no use for the exit status of the release agent
5110 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005111 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005112static void cgroup_release_agent(struct work_struct *work)
5113{
5114 BUG_ON(work != &release_agent_work);
5115 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005116 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005117 while (!list_empty(&release_list)) {
5118 char *argv[3], *envp[3];
5119 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005120 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005121 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005122 struct cgroup,
5123 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005124 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005125 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005126 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005127 if (!pathbuf)
5128 goto continue_free;
5129 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5130 goto continue_free;
5131 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5132 if (!agentbuf)
5133 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005134
5135 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005136 argv[i++] = agentbuf;
5137 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005138 argv[i] = NULL;
5139
5140 i = 0;
5141 /* minimal command environment */
5142 envp[i++] = "HOME=/";
5143 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5144 envp[i] = NULL;
5145
5146 /* Drop the lock while we invoke the usermode helper,
5147 * since the exec could involve hitting disk and hence
5148 * be a slow process */
5149 mutex_unlock(&cgroup_mutex);
5150 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005151 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005152 continue_free:
5153 kfree(pathbuf);
5154 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005155 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005156 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005157 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005158 mutex_unlock(&cgroup_mutex);
5159}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005160
5161static int __init cgroup_disable(char *str)
5162{
5163 int i;
5164 char *token;
5165
5166 while ((token = strsep(&str, ",")) != NULL) {
5167 if (!*token)
5168 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005169 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005170 struct cgroup_subsys *ss = subsys[i];
5171
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005172 /*
5173 * cgroup_disable, being at boot time, can't
5174 * know about module subsystems, so we don't
5175 * worry about them.
5176 */
5177 if (!ss || ss->module)
5178 continue;
5179
Paul Menage8bab8dd2008-04-04 14:29:57 -07005180 if (!strcmp(token, ss->name)) {
5181 ss->disabled = 1;
5182 printk(KERN_INFO "Disabling %s control group"
5183 " subsystem\n", ss->name);
5184 break;
5185 }
5186 }
5187 }
5188 return 1;
5189}
5190__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005191
5192/*
5193 * Functons for CSS ID.
5194 */
5195
5196/*
5197 *To get ID other than 0, this should be called when !cgroup_is_removed().
5198 */
5199unsigned short css_id(struct cgroup_subsys_state *css)
5200{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005201 struct css_id *cssid;
5202
5203 /*
5204 * This css_id() can return correct value when somone has refcnt
5205 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5206 * it's unchanged until freed.
5207 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005208 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005209
5210 if (cssid)
5211 return cssid->id;
5212 return 0;
5213}
Ben Blum67523c42010-03-10 15:22:11 -08005214EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005215
5216unsigned short css_depth(struct cgroup_subsys_state *css)
5217{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005218 struct css_id *cssid;
5219
Tejun Heo28b4c272012-04-01 12:09:56 -07005220 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005221
5222 if (cssid)
5223 return cssid->depth;
5224 return 0;
5225}
Ben Blum67523c42010-03-10 15:22:11 -08005226EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005227
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005228/**
5229 * css_is_ancestor - test "root" css is an ancestor of "child"
5230 * @child: the css to be tested.
5231 * @root: the css supporsed to be an ancestor of the child.
5232 *
5233 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005234 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005235 * But, considering usual usage, the csses should be valid objects after test.
5236 * Assuming that the caller will do some action to the child if this returns
5237 * returns true, the caller must take "child";s reference count.
5238 * If "child" is valid object and this returns true, "root" is valid, too.
5239 */
5240
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005241bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005242 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005243{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005244 struct css_id *child_id;
5245 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005246
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005247 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005248 if (!child_id)
5249 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005250 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005251 if (!root_id)
5252 return false;
5253 if (child_id->depth < root_id->depth)
5254 return false;
5255 if (child_id->stack[root_id->depth] != root_id->id)
5256 return false;
5257 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005258}
5259
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005260void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5261{
5262 struct css_id *id = css->id;
5263 /* When this is called before css_id initialization, id can be NULL */
5264 if (!id)
5265 return;
5266
5267 BUG_ON(!ss->use_id);
5268
5269 rcu_assign_pointer(id->css, NULL);
5270 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005271 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005272 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005273 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005274 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005275}
Ben Blum67523c42010-03-10 15:22:11 -08005276EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005277
5278/*
5279 * This is called by init or create(). Then, calls to this function are
5280 * always serialized (By cgroup_mutex() at create()).
5281 */
5282
5283static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5284{
5285 struct css_id *newid;
5286 int myid, error, size;
5287
5288 BUG_ON(!ss->use_id);
5289
5290 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5291 newid = kzalloc(size, GFP_KERNEL);
5292 if (!newid)
5293 return ERR_PTR(-ENOMEM);
5294 /* get id */
5295 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
5296 error = -ENOMEM;
5297 goto err_out;
5298 }
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005299 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005300 /* Don't use 0. allocates an ID of 1-65535 */
5301 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005302 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005303
5304 /* Returns error when there are no free spaces for new ID.*/
5305 if (error) {
5306 error = -ENOSPC;
5307 goto err_out;
5308 }
5309 if (myid > CSS_ID_MAX)
5310 goto remove_idr;
5311
5312 newid->id = myid;
5313 newid->depth = depth;
5314 return newid;
5315remove_idr:
5316 error = -ENOSPC;
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005317 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005318 idr_remove(&ss->idr, myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005319 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005320err_out:
5321 kfree(newid);
5322 return ERR_PTR(error);
5323
5324}
5325
Ben Blume6a11052010-03-10 15:22:09 -08005326static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5327 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005328{
5329 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005330
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005331 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005332 idr_init(&ss->idr);
5333
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005334 newid = get_new_cssid(ss, 0);
5335 if (IS_ERR(newid))
5336 return PTR_ERR(newid);
5337
5338 newid->stack[0] = newid->id;
5339 newid->css = rootcss;
5340 rootcss->id = newid;
5341 return 0;
5342}
5343
5344static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5345 struct cgroup *child)
5346{
5347 int subsys_id, i, depth = 0;
5348 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005349 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005350
5351 subsys_id = ss->subsys_id;
5352 parent_css = parent->subsys[subsys_id];
5353 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005354 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005355 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005356
5357 child_id = get_new_cssid(ss, depth);
5358 if (IS_ERR(child_id))
5359 return PTR_ERR(child_id);
5360
5361 for (i = 0; i < depth; i++)
5362 child_id->stack[i] = parent_id->stack[i];
5363 child_id->stack[depth] = child_id->id;
5364 /*
5365 * child_id->css pointer will be set after this cgroup is available
5366 * see cgroup_populate_dir()
5367 */
5368 rcu_assign_pointer(child_css->id, child_id);
5369
5370 return 0;
5371}
5372
5373/**
5374 * css_lookup - lookup css by id
5375 * @ss: cgroup subsys to be looked into.
5376 * @id: the id
5377 *
5378 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5379 * NULL if not. Should be called under rcu_read_lock()
5380 */
5381struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5382{
5383 struct css_id *cssid = NULL;
5384
5385 BUG_ON(!ss->use_id);
5386 cssid = idr_find(&ss->idr, id);
5387
5388 if (unlikely(!cssid))
5389 return NULL;
5390
5391 return rcu_dereference(cssid->css);
5392}
Ben Blum67523c42010-03-10 15:22:11 -08005393EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005394
5395/**
5396 * css_get_next - lookup next cgroup under specified hierarchy.
5397 * @ss: pointer to subsystem
5398 * @id: current position of iteration.
5399 * @root: pointer to css. search tree under this.
5400 * @foundid: position of found object.
5401 *
5402 * Search next css under the specified hierarchy of rootid. Calling under
5403 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5404 */
5405struct cgroup_subsys_state *
5406css_get_next(struct cgroup_subsys *ss, int id,
5407 struct cgroup_subsys_state *root, int *foundid)
5408{
5409 struct cgroup_subsys_state *ret = NULL;
5410 struct css_id *tmp;
5411 int tmpid;
5412 int rootid = css_id(root);
5413 int depth = css_depth(root);
5414
5415 if (!rootid)
5416 return NULL;
5417
5418 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005419 WARN_ON_ONCE(!rcu_read_lock_held());
5420
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005421 /* fill start point for scan */
5422 tmpid = id;
5423 while (1) {
5424 /*
5425 * scan next entry from bitmap(tree), tmpid is updated after
5426 * idr_get_next().
5427 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005428 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005429 if (!tmp)
5430 break;
5431 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5432 ret = rcu_dereference(tmp->css);
5433 if (ret) {
5434 *foundid = tmpid;
5435 break;
5436 }
5437 }
5438 /* continue to scan from next id */
5439 tmpid = tmpid + 1;
5440 }
5441 return ret;
5442}
5443
Stephane Eraniane5d13672011-02-14 11:20:01 +02005444/*
5445 * get corresponding css from file open on cgroupfs directory
5446 */
5447struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5448{
5449 struct cgroup *cgrp;
5450 struct inode *inode;
5451 struct cgroup_subsys_state *css;
5452
5453 inode = f->f_dentry->d_inode;
5454 /* check in cgroup filesystem dir */
5455 if (inode->i_op != &cgroup_dir_inode_operations)
5456 return ERR_PTR(-EBADF);
5457
5458 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5459 return ERR_PTR(-EINVAL);
5460
5461 /* get cgroup */
5462 cgrp = __d_cgrp(f->f_dentry);
5463 css = cgrp->subsys[id];
5464 return css ? css : ERR_PTR(-ENOENT);
5465}
5466
Paul Menagefe693432009-09-23 15:56:20 -07005467#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005468static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005469{
5470 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5471
5472 if (!css)
5473 return ERR_PTR(-ENOMEM);
5474
5475 return css;
5476}
5477
Tejun Heo92fb9742012-11-19 08:13:38 -08005478static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005479{
5480 kfree(cont->subsys[debug_subsys_id]);
5481}
5482
5483static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5484{
5485 return atomic_read(&cont->count);
5486}
5487
5488static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5489{
5490 return cgroup_task_count(cont);
5491}
5492
5493static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5494{
5495 return (u64)(unsigned long)current->cgroups;
5496}
5497
5498static u64 current_css_set_refcount_read(struct cgroup *cont,
5499 struct cftype *cft)
5500{
5501 u64 count;
5502
5503 rcu_read_lock();
5504 count = atomic_read(&current->cgroups->refcount);
5505 rcu_read_unlock();
5506 return count;
5507}
5508
Paul Menage7717f7b2009-09-23 15:56:22 -07005509static int current_css_set_cg_links_read(struct cgroup *cont,
5510 struct cftype *cft,
5511 struct seq_file *seq)
5512{
5513 struct cg_cgroup_link *link;
5514 struct css_set *cg;
5515
5516 read_lock(&css_set_lock);
5517 rcu_read_lock();
5518 cg = rcu_dereference(current->cgroups);
5519 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5520 struct cgroup *c = link->cgrp;
5521 const char *name;
5522
5523 if (c->dentry)
5524 name = c->dentry->d_name.name;
5525 else
5526 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005527 seq_printf(seq, "Root %d group %s\n",
5528 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005529 }
5530 rcu_read_unlock();
5531 read_unlock(&css_set_lock);
5532 return 0;
5533}
5534
5535#define MAX_TASKS_SHOWN_PER_CSS 25
5536static int cgroup_css_links_read(struct cgroup *cont,
5537 struct cftype *cft,
5538 struct seq_file *seq)
5539{
5540 struct cg_cgroup_link *link;
5541
5542 read_lock(&css_set_lock);
5543 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5544 struct css_set *cg = link->cg;
5545 struct task_struct *task;
5546 int count = 0;
5547 seq_printf(seq, "css_set %p\n", cg);
5548 list_for_each_entry(task, &cg->tasks, cg_list) {
5549 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5550 seq_puts(seq, " ...\n");
5551 break;
5552 } else {
5553 seq_printf(seq, " task %d\n",
5554 task_pid_vnr(task));
5555 }
5556 }
5557 }
5558 read_unlock(&css_set_lock);
5559 return 0;
5560}
5561
Paul Menagefe693432009-09-23 15:56:20 -07005562static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5563{
5564 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5565}
5566
5567static struct cftype debug_files[] = {
5568 {
5569 .name = "cgroup_refcount",
5570 .read_u64 = cgroup_refcount_read,
5571 },
5572 {
5573 .name = "taskcount",
5574 .read_u64 = debug_taskcount_read,
5575 },
5576
5577 {
5578 .name = "current_css_set",
5579 .read_u64 = current_css_set_read,
5580 },
5581
5582 {
5583 .name = "current_css_set_refcount",
5584 .read_u64 = current_css_set_refcount_read,
5585 },
5586
5587 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005588 .name = "current_css_set_cg_links",
5589 .read_seq_string = current_css_set_cg_links_read,
5590 },
5591
5592 {
5593 .name = "cgroup_css_links",
5594 .read_seq_string = cgroup_css_links_read,
5595 },
5596
5597 {
Paul Menagefe693432009-09-23 15:56:20 -07005598 .name = "releasable",
5599 .read_u64 = releasable_read,
5600 },
Paul Menagefe693432009-09-23 15:56:20 -07005601
Tejun Heo4baf6e32012-04-01 12:09:55 -07005602 { } /* terminate */
5603};
Paul Menagefe693432009-09-23 15:56:20 -07005604
5605struct cgroup_subsys debug_subsys = {
5606 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005607 .css_alloc = debug_css_alloc,
5608 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005609 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005610 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005611};
5612#endif /* CONFIG_CGROUP_DEBUG */