blob: 3852d926322c73b18f0e9ecceaac230d9aeee0e5 [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);
Li Zefan71b57072013-01-24 14:43:28 +0800425
426 /*
427 * We may not be holding cgroup_mutex, and if cgrp->count is
428 * dropped to 0 the cgroup can be destroyed at any time, hence
429 * rcu_read_lock is used to keep it alive.
430 */
431 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -0700432 if (atomic_dec_and_test(&cgrp->count) &&
433 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700434 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700435 set_bit(CGRP_RELEASABLE, &cgrp->flags);
436 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700437 }
Li Zefan71b57072013-01-24 14:43:28 +0800438 rcu_read_unlock();
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700439
440 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700441 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700442
443 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800444 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700445}
446
447/*
448 * refcounted get/put for css_set objects
449 */
450static inline void get_css_set(struct css_set *cg)
451{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700452 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700453}
454
455static inline void put_css_set(struct css_set *cg)
456{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700457 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700458}
459
Paul Menage81a6a5c2007-10-18 23:39:38 -0700460static inline void put_css_set_taskexit(struct css_set *cg)
461{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700462 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700463}
464
Paul Menage817929e2007-10-18 23:39:36 -0700465/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700466 * compare_css_sets - helper function for find_existing_css_set().
467 * @cg: candidate css_set being tested
468 * @old_cg: existing css_set for a task
469 * @new_cgrp: cgroup that's being entered by the task
470 * @template: desired set of css pointers in css_set (pre-calculated)
471 *
472 * Returns true if "cg" matches "old_cg" except for the hierarchy
473 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
474 */
475static bool compare_css_sets(struct css_set *cg,
476 struct css_set *old_cg,
477 struct cgroup *new_cgrp,
478 struct cgroup_subsys_state *template[])
479{
480 struct list_head *l1, *l2;
481
482 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
483 /* Not all subsystems matched */
484 return false;
485 }
486
487 /*
488 * Compare cgroup pointers in order to distinguish between
489 * different cgroups in heirarchies with no subsystems. We
490 * could get by with just this check alone (and skip the
491 * memcmp above) but on most setups the memcmp check will
492 * avoid the need for this more expensive check on almost all
493 * candidates.
494 */
495
496 l1 = &cg->cg_links;
497 l2 = &old_cg->cg_links;
498 while (1) {
499 struct cg_cgroup_link *cgl1, *cgl2;
500 struct cgroup *cg1, *cg2;
501
502 l1 = l1->next;
503 l2 = l2->next;
504 /* See if we reached the end - both lists are equal length. */
505 if (l1 == &cg->cg_links) {
506 BUG_ON(l2 != &old_cg->cg_links);
507 break;
508 } else {
509 BUG_ON(l2 == &old_cg->cg_links);
510 }
511 /* Locate the cgroups associated with these links. */
512 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
513 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
514 cg1 = cgl1->cgrp;
515 cg2 = cgl2->cgrp;
516 /* Hierarchies should be linked in the same order. */
517 BUG_ON(cg1->root != cg2->root);
518
519 /*
520 * If this hierarchy is the hierarchy of the cgroup
521 * that's changing, then we need to check that this
522 * css_set points to the new cgroup; if it's any other
523 * hierarchy, then this css_set should point to the
524 * same cgroup as the old css_set.
525 */
526 if (cg1->root == new_cgrp->root) {
527 if (cg1 != new_cgrp)
528 return false;
529 } else {
530 if (cg1 != cg2)
531 return false;
532 }
533 }
534 return true;
535}
536
537/*
Paul Menage817929e2007-10-18 23:39:36 -0700538 * find_existing_css_set() is a helper for
539 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700540 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700541 *
542 * oldcg: the cgroup group that we're using before the cgroup
543 * transition
544 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700545 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700546 *
547 * template: location in which to build the desired set of subsystem
548 * state objects for the new cgroup group
549 */
Paul Menage817929e2007-10-18 23:39:36 -0700550static struct css_set *find_existing_css_set(
551 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700552 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700553 struct cgroup_subsys_state *template[])
554{
555 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700556 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700557 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800558 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700559
Ben Blumaae8aab2010-03-10 15:22:07 -0800560 /*
561 * Build the set of subsystem state objects that we want to see in the
562 * new css_set. while subsystems can change globally, the entries here
563 * won't change, so no need for locking.
564 */
Paul Menage817929e2007-10-18 23:39:36 -0700565 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400566 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700567 /* Subsystem is in this hierarchy. So we want
568 * the subsystem state from the new
569 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700570 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700571 } else {
572 /* Subsystem is not in this hierarchy, so we
573 * don't want to change the subsystem state */
574 template[i] = oldcg->subsys[i];
575 }
576 }
577
Li Zefan0ac801f2013-01-10 11:49:27 +0800578 key = css_set_hash(template);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800579 hash_for_each_possible(css_set_table, cg, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700580 if (!compare_css_sets(cg, oldcg, cgrp, template))
581 continue;
582
583 /* This css_set matches what we need */
584 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700585 }
Paul Menage817929e2007-10-18 23:39:36 -0700586
587 /* No existing cgroup group matched */
588 return NULL;
589}
590
Paul Menage817929e2007-10-18 23:39:36 -0700591static void free_cg_links(struct list_head *tmp)
592{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700593 struct cg_cgroup_link *link;
594 struct cg_cgroup_link *saved_link;
595
596 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700597 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700598 kfree(link);
599 }
600}
601
602/*
Li Zefan36553432008-07-29 22:33:19 -0700603 * allocate_cg_links() allocates "count" cg_cgroup_link structures
604 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
605 * success or a negative error
606 */
607static int allocate_cg_links(int count, struct list_head *tmp)
608{
609 struct cg_cgroup_link *link;
610 int i;
611 INIT_LIST_HEAD(tmp);
612 for (i = 0; i < count; i++) {
613 link = kmalloc(sizeof(*link), GFP_KERNEL);
614 if (!link) {
615 free_cg_links(tmp);
616 return -ENOMEM;
617 }
618 list_add(&link->cgrp_link_list, tmp);
619 }
620 return 0;
621}
622
Li Zefanc12f65d2009-01-07 18:07:42 -0800623/**
624 * link_css_set - a helper function to link a css_set to a cgroup
625 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
626 * @cg: the css_set to be linked
627 * @cgrp: the destination cgroup
628 */
629static void link_css_set(struct list_head *tmp_cg_links,
630 struct css_set *cg, struct cgroup *cgrp)
631{
632 struct cg_cgroup_link *link;
633
634 BUG_ON(list_empty(tmp_cg_links));
635 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
636 cgrp_link_list);
637 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700638 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700639 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800640 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700641 /*
642 * Always add links to the tail of the list so that the list
643 * is sorted by order of hierarchy creation
644 */
645 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800646}
647
Li Zefan36553432008-07-29 22:33:19 -0700648/*
Paul Menage817929e2007-10-18 23:39:36 -0700649 * find_css_set() takes an existing cgroup group and a
650 * cgroup object, and returns a css_set object that's
651 * equivalent to the old group, but with the given cgroup
652 * substituted into the appropriate hierarchy. Must be called with
653 * cgroup_mutex held
654 */
Paul Menage817929e2007-10-18 23:39:36 -0700655static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700656 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700657{
658 struct css_set *res;
659 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700660
661 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700662
Paul Menage7717f7b2009-09-23 15:56:22 -0700663 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800664 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700665
Paul Menage817929e2007-10-18 23:39:36 -0700666 /* First see if we already have a cgroup group that matches
667 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700668 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700669 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700670 if (res)
671 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700672 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700673
674 if (res)
675 return res;
676
677 res = kmalloc(sizeof(*res), GFP_KERNEL);
678 if (!res)
679 return NULL;
680
681 /* Allocate all the cg_cgroup_link objects that we'll need */
682 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
683 kfree(res);
684 return NULL;
685 }
686
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700687 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700688 INIT_LIST_HEAD(&res->cg_links);
689 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700690 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700691
692 /* Copy the set of subsystem state objects generated in
693 * find_existing_css_set() */
694 memcpy(res->subsys, template, sizeof(res->subsys));
695
696 write_lock(&css_set_lock);
697 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700698 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
699 struct cgroup *c = link->cgrp;
700 if (c->root == cgrp->root)
701 c = cgrp;
702 link_css_set(&tmp_cg_links, res, c);
703 }
Paul Menage817929e2007-10-18 23:39:36 -0700704
705 BUG_ON(!list_empty(&tmp_cg_links));
706
Paul Menage817929e2007-10-18 23:39:36 -0700707 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700708
709 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800710 key = css_set_hash(res->subsys);
711 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700712
Paul Menage817929e2007-10-18 23:39:36 -0700713 write_unlock(&css_set_lock);
714
715 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700716}
717
Paul Menageddbcc7e2007-10-18 23:39:30 -0700718/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700719 * Return the cgroup for "task" from the given hierarchy. Must be
720 * called with cgroup_mutex held.
721 */
722static struct cgroup *task_cgroup_from_root(struct task_struct *task,
723 struct cgroupfs_root *root)
724{
725 struct css_set *css;
726 struct cgroup *res = NULL;
727
728 BUG_ON(!mutex_is_locked(&cgroup_mutex));
729 read_lock(&css_set_lock);
730 /*
731 * No need to lock the task - since we hold cgroup_mutex the
732 * task can't change groups, so the only thing that can happen
733 * is that it exits and its css is set back to init_css_set.
734 */
735 css = task->cgroups;
736 if (css == &init_css_set) {
737 res = &root->top_cgroup;
738 } else {
739 struct cg_cgroup_link *link;
740 list_for_each_entry(link, &css->cg_links, cg_link_list) {
741 struct cgroup *c = link->cgrp;
742 if (c->root == root) {
743 res = c;
744 break;
745 }
746 }
747 }
748 read_unlock(&css_set_lock);
749 BUG_ON(!res);
750 return res;
751}
752
753/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700754 * There is one global cgroup mutex. We also require taking
755 * task_lock() when dereferencing a task's cgroup subsys pointers.
756 * See "The task_lock() exception", at the end of this comment.
757 *
758 * A task must hold cgroup_mutex to modify cgroups.
759 *
760 * Any task can increment and decrement the count field without lock.
761 * So in general, code holding cgroup_mutex can't rely on the count
762 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800763 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700764 * means that no tasks are currently attached, therefore there is no
765 * way a task attached to that cgroup can fork (the other way to
766 * increment the count). So code holding cgroup_mutex can safely
767 * assume that if the count is zero, it will stay zero. Similarly, if
768 * a task holds cgroup_mutex on a cgroup with zero count, it
769 * knows that the cgroup won't be removed, as cgroup_rmdir()
770 * needs that mutex.
771 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700772 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
773 * (usually) take cgroup_mutex. These are the two most performance
774 * critical pieces of code here. The exception occurs on cgroup_exit(),
775 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
776 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800777 * to the release agent with the name of the cgroup (path relative to
778 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700779 *
780 * A cgroup can only be deleted if both its 'count' of using tasks
781 * is zero, and its list of 'children' cgroups is empty. Since all
782 * tasks in the system use _some_ cgroup, and since there is always at
783 * least one task in the system (init, pid == 1), therefore, top_cgroup
784 * always has either children cgroups and/or using tasks. So we don't
785 * need a special hack to ensure that top_cgroup cannot be deleted.
786 *
787 * The task_lock() exception
788 *
789 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800790 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800791 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792 * several performance critical places that need to reference
793 * task->cgroup without the expense of grabbing a system global
794 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800795 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
797 * the task_struct routinely used for such matters.
798 *
799 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800800 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700801 */
802
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803/**
804 * cgroup_lock - lock out any changes to cgroup structures
805 *
806 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700807void cgroup_lock(void)
808{
809 mutex_lock(&cgroup_mutex);
810}
Ben Blum67523c42010-03-10 15:22:11 -0800811EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700812
813/**
814 * cgroup_unlock - release lock on cgroup changes
815 *
816 * Undo the lock taken in a previous cgroup_lock() call.
817 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700818void cgroup_unlock(void)
819{
820 mutex_unlock(&cgroup_mutex);
821}
Ben Blum67523c42010-03-10 15:22:11 -0800822EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700823
824/*
825 * A couple of forward declarations required, due to cyclic reference loop:
826 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
827 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
828 * -> cgroup_mkdir.
829 */
830
Al Viro18bb1db2011-07-26 01:41:39 -0400831static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400832static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700833static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400834static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
835 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700836static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700837static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700838
839static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200840 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700841 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700842};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700843
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700844static int alloc_css_id(struct cgroup_subsys *ss,
845 struct cgroup *parent, struct cgroup *child);
846
Al Viroa5e7ed32011-07-26 01:55:55 -0400847static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700848{
849 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700850
851 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400852 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700853 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100854 inode->i_uid = current_fsuid();
855 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700856 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
857 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
858 }
859 return inode;
860}
861
Li Zefanbe445622013-01-24 14:31:42 +0800862static void cgroup_free_fn(struct work_struct *work)
863{
864 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
865 struct cgroup_subsys *ss;
866
867 mutex_lock(&cgroup_mutex);
868 /*
869 * Release the subsystem state objects.
870 */
871 for_each_subsys(cgrp->root, ss)
872 ss->css_free(cgrp);
873
874 cgrp->root->number_of_cgroups--;
875 mutex_unlock(&cgroup_mutex);
876
877 /*
878 * Drop the active superblock reference that we took when we
879 * created the cgroup
880 */
881 deactivate_super(cgrp->root->sb);
882
883 /*
884 * if we're getting rid of the cgroup, refcount should ensure
885 * that there are no pidlists left.
886 */
887 BUG_ON(!list_empty(&cgrp->pidlists));
888
889 simple_xattrs_free(&cgrp->xattrs);
890
891 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
892 kfree(cgrp);
893}
894
895static void cgroup_free_rcu(struct rcu_head *head)
896{
897 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
898
899 schedule_work(&cgrp->free_work);
900}
901
Paul Menageddbcc7e2007-10-18 23:39:30 -0700902static void cgroup_diput(struct dentry *dentry, struct inode *inode)
903{
904 /* is dentry a directory ? if so, kfree() associated cgroup */
905 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700906 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800907
Paul Menagebd89aab2007-10-18 23:40:44 -0700908 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800909 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700910 } else {
911 struct cfent *cfe = __d_cfe(dentry);
912 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400913 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700914
915 WARN_ONCE(!list_empty(&cfe->node) &&
916 cgrp != &cgrp->root->top_cgroup,
917 "cfe still linked for %s\n", cfe->type->name);
918 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400919 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700920 }
921 iput(inode);
922}
923
Al Viroc72a04e2011-01-14 05:31:45 +0000924static int cgroup_delete(const struct dentry *d)
925{
926 return 1;
927}
928
Paul Menageddbcc7e2007-10-18 23:39:30 -0700929static void remove_dir(struct dentry *d)
930{
931 struct dentry *parent = dget(d->d_parent);
932
933 d_delete(d);
934 simple_rmdir(parent->d_inode, d);
935 dput(parent);
936}
937
Li Zefan2739d3c2013-01-21 18:18:33 +0800938static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700939{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700940 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700941
Tejun Heo05ef1d72012-04-01 12:09:56 -0700942 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
943 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100944
Li Zefan2739d3c2013-01-21 18:18:33 +0800945 /*
946 * If we're doing cleanup due to failure of cgroup_create(),
947 * the corresponding @cfe may not exist.
948 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700949 list_for_each_entry(cfe, &cgrp->files, node) {
950 struct dentry *d = cfe->dentry;
951
952 if (cft && cfe->type != cft)
953 continue;
954
955 dget(d);
956 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700957 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700958 list_del_init(&cfe->node);
959 dput(d);
960
Li Zefan2739d3c2013-01-21 18:18:33 +0800961 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700962 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700963}
964
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400965/**
966 * cgroup_clear_directory - selective removal of base and subsystem files
967 * @dir: directory containing the files
968 * @base_files: true if the base files should be removed
969 * @subsys_mask: mask of the subsystem ids whose files should be removed
970 */
971static void cgroup_clear_directory(struct dentry *dir, bool base_files,
972 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700973{
974 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400975 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700976
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400977 for_each_subsys(cgrp->root, ss) {
978 struct cftype_set *set;
979 if (!test_bit(ss->subsys_id, &subsys_mask))
980 continue;
981 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800982 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400983 }
984 if (base_files) {
985 while (!list_empty(&cgrp->files))
986 cgroup_rm_file(cgrp, NULL);
987 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700988}
989
990/*
991 * NOTE : the dentry must have been dget()'ed
992 */
993static void cgroup_d_remove_dir(struct dentry *dentry)
994{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100995 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400996 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100997
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400998 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001000 parent = dentry->d_parent;
1001 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001002 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001004 spin_unlock(&dentry->d_lock);
1005 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006 remove_dir(dentry);
1007}
1008
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001009/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001010 * Call with cgroup_mutex held. Drops reference counts on modules, including
1011 * any duplicate ones that parse_cgroupfs_options took. If this function
1012 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001013 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001014static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001015 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001017 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001018 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001019 int i;
1020
Ben Blumaae8aab2010-03-10 15:22:07 -08001021 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001022 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001023
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001024 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1025 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026 /* Check that any added subsystems are currently free */
1027 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001028 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001030 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001032 /*
1033 * Nobody should tell us to do a subsys that doesn't exist:
1034 * parse_cgroupfs_options should catch that case and refcounts
1035 * ensure that subsystems won't disappear once selected.
1036 */
1037 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001038 if (ss->root != &rootnode) {
1039 /* Subsystem isn't free */
1040 return -EBUSY;
1041 }
1042 }
1043
1044 /* Currently we don't handle adding/removing subsystems when
1045 * any child cgroups exist. This is theoretically supportable
1046 * but involves complex error handling, so it's being left until
1047 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001048 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049 return -EBUSY;
1050
1051 /* Process each subsystem */
1052 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1053 struct cgroup_subsys *ss = subsys[i];
1054 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001055 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001057 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001058 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 BUG_ON(!dummytop->subsys[i]);
1060 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001061 cgrp->subsys[i] = dummytop->subsys[i];
1062 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001063 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001064 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001066 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001067 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001068 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001069 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001070 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001071 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1072 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001074 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001076 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001077 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001078 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001079 /* subsystem is now free - drop reference on module */
1080 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001081 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001083 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001084 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001085 /*
1086 * a refcount was taken, but we already had one, so
1087 * drop the extra reference.
1088 */
1089 module_put(ss->module);
1090#ifdef CONFIG_MODULE_UNLOAD
1091 BUG_ON(ss->module && !module_refcount(ss->module));
1092#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093 } else {
1094 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001095 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096 }
1097 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001098 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099
1100 return 0;
1101}
1102
Al Viro34c80b12011-12-08 21:32:45 -05001103static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104{
Al Viro34c80b12011-12-08 21:32:45 -05001105 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106 struct cgroup_subsys *ss;
1107
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001108 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109 for_each_subsys(root, ss)
1110 seq_printf(seq, ",%s", ss->name);
1111 if (test_bit(ROOT_NOPREFIX, &root->flags))
1112 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001113 if (test_bit(ROOT_XATTR, &root->flags))
1114 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001115 if (strlen(root->release_agent_path))
1116 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001117 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001118 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001119 if (strlen(root->name))
1120 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001121 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001122 return 0;
1123}
1124
1125struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001126 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001128 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001129 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001130 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001131 /* User explicitly requested empty subsystem */
1132 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001133
1134 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001135
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136};
1137
Ben Blumaae8aab2010-03-10 15:22:07 -08001138/*
1139 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001140 * with cgroup_mutex held to protect the subsys[] array. This function takes
1141 * refcounts on subsystems to be used, unless it returns error, in which case
1142 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001143 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001144static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001145{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001146 char *token, *o = data;
1147 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001148 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001149 int i;
1150 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001151
Ben Blumaae8aab2010-03-10 15:22:07 -08001152 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1153
Li Zefanf9ab5b52009-06-17 16:26:33 -07001154#ifdef CONFIG_CPUSETS
1155 mask = ~(1UL << cpuset_subsys_id);
1156#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001157
Paul Menagec6d57f32009-09-23 15:56:19 -07001158 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001159
1160 while ((token = strsep(&o, ",")) != NULL) {
1161 if (!*token)
1162 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001163 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001164 /* Explicitly have no subsystems */
1165 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001166 continue;
1167 }
1168 if (!strcmp(token, "all")) {
1169 /* Mutually exclusive option 'all' + subsystem name */
1170 if (one_ss)
1171 return -EINVAL;
1172 all_ss = true;
1173 continue;
1174 }
1175 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001176 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001177 continue;
1178 }
1179 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001180 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001181 continue;
1182 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001183 if (!strcmp(token, "xattr")) {
1184 set_bit(ROOT_XATTR, &opts->flags);
1185 continue;
1186 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001187 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001188 /* Specifying two release agents is forbidden */
1189 if (opts->release_agent)
1190 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001191 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001192 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001193 if (!opts->release_agent)
1194 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001195 continue;
1196 }
1197 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001198 const char *name = token + 5;
1199 /* Can't specify an empty name */
1200 if (!strlen(name))
1201 return -EINVAL;
1202 /* Must match [\w.-]+ */
1203 for (i = 0; i < strlen(name); i++) {
1204 char c = name[i];
1205 if (isalnum(c))
1206 continue;
1207 if ((c == '.') || (c == '-') || (c == '_'))
1208 continue;
1209 return -EINVAL;
1210 }
1211 /* Specifying two names is forbidden */
1212 if (opts->name)
1213 return -EINVAL;
1214 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001215 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001216 GFP_KERNEL);
1217 if (!opts->name)
1218 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001219
1220 continue;
1221 }
1222
1223 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1224 struct cgroup_subsys *ss = subsys[i];
1225 if (ss == NULL)
1226 continue;
1227 if (strcmp(token, ss->name))
1228 continue;
1229 if (ss->disabled)
1230 continue;
1231
1232 /* Mutually exclusive option 'all' + subsystem name */
1233 if (all_ss)
1234 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001235 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001236 one_ss = true;
1237
1238 break;
1239 }
1240 if (i == CGROUP_SUBSYS_COUNT)
1241 return -ENOENT;
1242 }
1243
1244 /*
1245 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001246 * otherwise if 'none', 'name=' and a subsystem name options
1247 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001248 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001249 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001250 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1251 struct cgroup_subsys *ss = subsys[i];
1252 if (ss == NULL)
1253 continue;
1254 if (ss->disabled)
1255 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001256 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001257 }
1258 }
1259
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001260 /* Consistency checks */
1261
Li Zefanf9ab5b52009-06-17 16:26:33 -07001262 /*
1263 * Option noprefix was introduced just for backward compatibility
1264 * with the old cpuset, so we allow noprefix only if mounting just
1265 * the cpuset subsystem.
1266 */
1267 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001268 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001269 return -EINVAL;
1270
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001271
1272 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001273 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001274 return -EINVAL;
1275
1276 /*
1277 * We either have to specify by name or by subsystems. (So all
1278 * empty hierarchies must have a name).
1279 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001280 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001281 return -EINVAL;
1282
Ben Blumcf5d5942010-03-10 15:22:09 -08001283 /*
1284 * Grab references on all the modules we'll need, so the subsystems
1285 * don't dance around before rebind_subsystems attaches them. This may
1286 * take duplicate reference counts on a subsystem that's already used,
1287 * but rebind_subsystems handles this case.
1288 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001289 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001290 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 if (!try_module_get(subsys[i]->module)) {
1295 module_pin_failed = true;
1296 break;
1297 }
1298 }
1299 if (module_pin_failed) {
1300 /*
1301 * oops, one of the modules was going away. this means that we
1302 * raced with a module_delete call, and to the user this is
1303 * essentially a "subsystem doesn't exist" case.
1304 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001305 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001306 /* drop refcounts only on the ones we took */
1307 unsigned long bit = 1UL << i;
1308
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001309 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001310 continue;
1311 module_put(subsys[i]->module);
1312 }
1313 return -ENOENT;
1314 }
1315
Paul Menageddbcc7e2007-10-18 23:39:30 -07001316 return 0;
1317}
1318
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001319static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001320{
1321 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001322 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001323 unsigned long bit = 1UL << i;
1324
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001325 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001326 continue;
1327 module_put(subsys[i]->module);
1328 }
1329}
1330
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1332{
1333 int ret = 0;
1334 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001335 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001336 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001337 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001338
Paul Menagebd89aab2007-10-18 23:40:44 -07001339 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001341 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342
1343 /* See what subsystems are wanted */
1344 ret = parse_cgroupfs_options(data, &opts);
1345 if (ret)
1346 goto out_unlock;
1347
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001348 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001349 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1350 task_tgid_nr(current), current->comm);
1351
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001352 added_mask = opts.subsys_mask & ~root->subsys_mask;
1353 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001354
Ben Blumcf5d5942010-03-10 15:22:09 -08001355 /* Don't allow flags or name to change at remount */
1356 if (opts.flags != root->flags ||
1357 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001358 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001359 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001360 goto out_unlock;
1361 }
1362
Gao feng7083d032012-12-03 09:28:18 +08001363 /*
1364 * Clear out the files of subsystems that should be removed, do
1365 * this before rebind_subsystems, since rebind_subsystems may
1366 * change this hierarchy's subsys_list.
1367 */
1368 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1369
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001370 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001371 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001372 /* rebind_subsystems failed, re-populate the removed files */
1373 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001374 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001375 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001376 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001377
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001378 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001379 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380
Paul Menage81a6a5c2007-10-18 23:39:38 -07001381 if (opts.release_agent)
1382 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001384 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001385 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001386 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001387 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001388 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389 return ret;
1390}
1391
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001392static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001393 .statfs = simple_statfs,
1394 .drop_inode = generic_delete_inode,
1395 .show_options = cgroup_show_options,
1396 .remount_fs = cgroup_remount,
1397};
1398
Paul Menagecc31edc2008-10-18 20:28:04 -07001399static void init_cgroup_housekeeping(struct cgroup *cgrp)
1400{
1401 INIT_LIST_HEAD(&cgrp->sibling);
1402 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001403 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001404 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001405 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001406 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001407 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001408 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001409 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001410 INIT_LIST_HEAD(&cgrp->event_list);
1411 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001412 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001413}
Paul Menagec6d57f32009-09-23 15:56:19 -07001414
Paul Menageddbcc7e2007-10-18 23:39:30 -07001415static void init_cgroup_root(struct cgroupfs_root *root)
1416{
Paul Menagebd89aab2007-10-18 23:40:44 -07001417 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001418
Paul Menageddbcc7e2007-10-18 23:39:30 -07001419 INIT_LIST_HEAD(&root->subsys_list);
1420 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001421 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001422 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001423 cgrp->root = root;
1424 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001425 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001426 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001427}
1428
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001429static bool init_root_id(struct cgroupfs_root *root)
1430{
1431 int ret = 0;
1432
1433 do {
1434 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1435 return false;
1436 spin_lock(&hierarchy_id_lock);
1437 /* Try to allocate the next unused ID */
1438 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1439 &root->hierarchy_id);
1440 if (ret == -ENOSPC)
1441 /* Try again starting from 0 */
1442 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1443 if (!ret) {
1444 next_hierarchy_id = root->hierarchy_id + 1;
1445 } else if (ret != -EAGAIN) {
1446 /* Can only get here if the 31-bit IDR is full ... */
1447 BUG_ON(ret);
1448 }
1449 spin_unlock(&hierarchy_id_lock);
1450 } while (ret);
1451 return true;
1452}
1453
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454static int cgroup_test_super(struct super_block *sb, void *data)
1455{
Paul Menagec6d57f32009-09-23 15:56:19 -07001456 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001457 struct cgroupfs_root *root = sb->s_fs_info;
1458
Paul Menagec6d57f32009-09-23 15:56:19 -07001459 /* If we asked for a name then it must match */
1460 if (opts->name && strcmp(opts->name, root->name))
1461 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001462
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001463 /*
1464 * If we asked for subsystems (or explicitly for no
1465 * subsystems) then they must match
1466 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001467 if ((opts->subsys_mask || opts->none)
1468 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001469 return 0;
1470
1471 return 1;
1472}
1473
Paul Menagec6d57f32009-09-23 15:56:19 -07001474static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1475{
1476 struct cgroupfs_root *root;
1477
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001478 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001479 return NULL;
1480
1481 root = kzalloc(sizeof(*root), GFP_KERNEL);
1482 if (!root)
1483 return ERR_PTR(-ENOMEM);
1484
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001485 if (!init_root_id(root)) {
1486 kfree(root);
1487 return ERR_PTR(-ENOMEM);
1488 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001489 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001490
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001491 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001492 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001493 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001494 if (opts->release_agent)
1495 strcpy(root->release_agent_path, opts->release_agent);
1496 if (opts->name)
1497 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001498 if (opts->cpuset_clone_children)
1499 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001500 return root;
1501}
1502
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001503static void cgroup_drop_root(struct cgroupfs_root *root)
1504{
1505 if (!root)
1506 return;
1507
1508 BUG_ON(!root->hierarchy_id);
1509 spin_lock(&hierarchy_id_lock);
1510 ida_remove(&hierarchy_ida, root->hierarchy_id);
1511 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001512 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001513 kfree(root);
1514}
1515
Paul Menageddbcc7e2007-10-18 23:39:30 -07001516static int cgroup_set_super(struct super_block *sb, void *data)
1517{
1518 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001519 struct cgroup_sb_opts *opts = data;
1520
1521 /* If we don't have a new root, we can't set up a new sb */
1522 if (!opts->new_root)
1523 return -EINVAL;
1524
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001525 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526
1527 ret = set_anon_super(sb, NULL);
1528 if (ret)
1529 return ret;
1530
Paul Menagec6d57f32009-09-23 15:56:19 -07001531 sb->s_fs_info = opts->new_root;
1532 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533
1534 sb->s_blocksize = PAGE_CACHE_SIZE;
1535 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1536 sb->s_magic = CGROUP_SUPER_MAGIC;
1537 sb->s_op = &cgroup_ops;
1538
1539 return 0;
1540}
1541
1542static int cgroup_get_rootdir(struct super_block *sb)
1543{
Al Viro0df6a632010-12-21 13:29:29 -05001544 static const struct dentry_operations cgroup_dops = {
1545 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001546 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001547 };
1548
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549 struct inode *inode =
1550 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551
1552 if (!inode)
1553 return -ENOMEM;
1554
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555 inode->i_fop = &simple_dir_operations;
1556 inode->i_op = &cgroup_dir_inode_operations;
1557 /* directories start off with i_nlink == 2 (for "." entry) */
1558 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001559 sb->s_root = d_make_root(inode);
1560 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001562 /* for everything else we want ->d_op set */
1563 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 return 0;
1565}
1566
Al Virof7e83572010-07-26 13:23:11 +04001567static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001569 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001570{
1571 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001572 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573 int ret = 0;
1574 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001575 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001576 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577
1578 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001579 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001581 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001582 if (ret)
1583 goto out_err;
1584
1585 /*
1586 * Allocate a new cgroup root. We may not need it if we're
1587 * reusing an existing hierarchy.
1588 */
1589 new_root = cgroup_root_from_opts(&opts);
1590 if (IS_ERR(new_root)) {
1591 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001592 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001593 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001594 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595
Paul Menagec6d57f32009-09-23 15:56:19 -07001596 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001597 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001598 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001599 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001600 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001601 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001602 }
1603
Paul Menagec6d57f32009-09-23 15:56:19 -07001604 root = sb->s_fs_info;
1605 BUG_ON(!root);
1606 if (root == opts.new_root) {
1607 /* We used the new root structure, so this is a new hierarchy */
1608 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001609 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001610 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001611 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001612 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001613 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614
1615 BUG_ON(sb->s_root != NULL);
1616
1617 ret = cgroup_get_rootdir(sb);
1618 if (ret)
1619 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001620 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621
Paul Menage817929e2007-10-18 23:39:36 -07001622 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001624 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001625
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001626 /* Check for name clashes with existing mounts */
1627 ret = -EBUSY;
1628 if (strlen(root->name))
1629 for_each_active_root(existing_root)
1630 if (!strcmp(existing_root->name, root->name))
1631 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001632
Paul Menage817929e2007-10-18 23:39:36 -07001633 /*
1634 * We're accessing css_set_count without locking
1635 * css_set_lock here, but that's OK - it can only be
1636 * increased by someone holding cgroup_lock, and
1637 * that's us. The worst that can happen is that we
1638 * have some link structures left over
1639 */
1640 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001641 if (ret)
1642 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001643
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001644 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001645 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001646 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001647 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001648 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001649 /*
1650 * There must be no failure case after here, since rebinding
1651 * takes care of subsystems' refcounts, which are explicitly
1652 * dropped in the failure exit path.
1653 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654
1655 /* EBUSY should be the only error here */
1656 BUG_ON(ret);
1657
1658 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001659 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660
Li Zefanc12f65d2009-01-07 18:07:42 -08001661 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662 root->top_cgroup.dentry = sb->s_root;
1663
Paul Menage817929e2007-10-18 23:39:36 -07001664 /* Link the top cgroup in this hierarchy into all
1665 * the css_set objects */
1666 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001667 hash_for_each(css_set_table, i, cg, hlist)
Li Zefan0ac801f2013-01-10 11:49:27 +08001668 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001669 write_unlock(&css_set_lock);
1670
1671 free_cg_links(&tmp_cg_links);
1672
Li Zefanc12f65d2009-01-07 18:07:42 -08001673 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674 BUG_ON(root->number_of_cgroups != 1);
1675
eparis@redhat2ce97382011-06-02 21:20:51 +10001676 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001677 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001678 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001679 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001680 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001681 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001682 } else {
1683 /*
1684 * We re-used an existing hierarchy - the new root (if
1685 * any) is not needed
1686 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001687 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001688 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001689 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001690 }
1691
Paul Menagec6d57f32009-09-23 15:56:19 -07001692 kfree(opts.release_agent);
1693 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001694 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001695
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001696 unlock_drop:
1697 mutex_unlock(&cgroup_root_mutex);
1698 mutex_unlock(&cgroup_mutex);
1699 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001701 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001702 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001703 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001704 out_err:
1705 kfree(opts.release_agent);
1706 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001707 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708}
1709
1710static void cgroup_kill_sb(struct super_block *sb) {
1711 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001712 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001713 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001714 struct cg_cgroup_link *link;
1715 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001716
1717 BUG_ON(!root);
1718
1719 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001720 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001721
1722 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001723 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724
1725 /* Rebind all subsystems back to the default hierarchy */
1726 ret = rebind_subsystems(root, 0);
1727 /* Shouldn't be able to fail ... */
1728 BUG_ON(ret);
1729
Paul Menage817929e2007-10-18 23:39:36 -07001730 /*
1731 * Release all the links from css_sets to this hierarchy's
1732 * root cgroup
1733 */
1734 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001735
1736 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1737 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001738 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001739 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001740 kfree(link);
1741 }
1742 write_unlock(&css_set_lock);
1743
Paul Menage839ec542009-01-29 14:25:22 -08001744 if (!list_empty(&root->root_list)) {
1745 list_del(&root->root_list);
1746 root_count--;
1747 }
Li Zefane5f6a862009-01-07 18:07:41 -08001748
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001749 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001750 mutex_unlock(&cgroup_mutex);
1751
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001752 simple_xattrs_free(&cgrp->xattrs);
1753
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001755 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001756}
1757
1758static struct file_system_type cgroup_fs_type = {
1759 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001760 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 .kill_sb = cgroup_kill_sb,
1762};
1763
Greg KH676db4a2010-08-05 13:53:35 -07001764static struct kobject *cgroup_kobj;
1765
Li Zefana043e3b2008-02-23 15:24:09 -08001766/**
1767 * cgroup_path - generate the path of a cgroup
1768 * @cgrp: the cgroup in question
1769 * @buf: the buffer to write the path into
1770 * @buflen: the length of the buffer
1771 *
Paul Menagea47295e2009-01-07 18:07:44 -08001772 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1773 * reference. Writes path of cgroup into buf. Returns 0 on success,
1774 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001775 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001776int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777{
Tejun Heofebfcef2012-11-19 08:13:36 -08001778 struct dentry *dentry = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001780
1781 rcu_lockdep_assert(rcu_read_lock_held() || cgroup_lock_is_held(),
1782 "cgroup_path() called without proper locking");
Paul Menageddbcc7e2007-10-18 23:39:30 -07001783
Li Zefanfe1c06c2013-01-24 14:30:22 +08001784 if (cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001785 /*
1786 * Inactive subsystems have no dentry for their root
1787 * cgroup
1788 */
1789 strcpy(buf, "/");
1790 return 0;
1791 }
1792
Tao Ma316eb662012-11-08 21:36:38 +08001793 start = buf + buflen - 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001794
Tao Ma316eb662012-11-08 21:36:38 +08001795 *start = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07001796 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001797 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001798
Paul Menageddbcc7e2007-10-18 23:39:30 -07001799 if ((start -= len) < buf)
1800 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001801 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001802 cgrp = cgrp->parent;
1803 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001804 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001805
Tejun Heofebfcef2012-11-19 08:13:36 -08001806 dentry = cgrp->dentry;
Paul Menagebd89aab2007-10-18 23:40:44 -07001807 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001808 continue;
1809 if (--start < buf)
1810 return -ENAMETOOLONG;
1811 *start = '/';
1812 }
1813 memmove(buf, start, buf + buflen - start);
1814 return 0;
1815}
Ben Blum67523c42010-03-10 15:22:11 -08001816EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001817
Ben Blum74a11662011-05-26 16:25:20 -07001818/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001819 * Control Group taskset
1820 */
Tejun Heo134d3372011-12-12 18:12:21 -08001821struct task_and_cgroup {
1822 struct task_struct *task;
1823 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001824 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001825};
1826
Tejun Heo2f7ee562011-12-12 18:12:21 -08001827struct cgroup_taskset {
1828 struct task_and_cgroup single;
1829 struct flex_array *tc_array;
1830 int tc_array_len;
1831 int idx;
1832 struct cgroup *cur_cgrp;
1833};
1834
1835/**
1836 * cgroup_taskset_first - reset taskset and return the first task
1837 * @tset: taskset of interest
1838 *
1839 * @tset iteration is initialized and the first task is returned.
1840 */
1841struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1842{
1843 if (tset->tc_array) {
1844 tset->idx = 0;
1845 return cgroup_taskset_next(tset);
1846 } else {
1847 tset->cur_cgrp = tset->single.cgrp;
1848 return tset->single.task;
1849 }
1850}
1851EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1852
1853/**
1854 * cgroup_taskset_next - iterate to the next task in taskset
1855 * @tset: taskset of interest
1856 *
1857 * Return the next task in @tset. Iteration must have been initialized
1858 * with cgroup_taskset_first().
1859 */
1860struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1861{
1862 struct task_and_cgroup *tc;
1863
1864 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1865 return NULL;
1866
1867 tc = flex_array_get(tset->tc_array, tset->idx++);
1868 tset->cur_cgrp = tc->cgrp;
1869 return tc->task;
1870}
1871EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1872
1873/**
1874 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1875 * @tset: taskset of interest
1876 *
1877 * Return the cgroup for the current (last returned) task of @tset. This
1878 * function must be preceded by either cgroup_taskset_first() or
1879 * cgroup_taskset_next().
1880 */
1881struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1882{
1883 return tset->cur_cgrp;
1884}
1885EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1886
1887/**
1888 * cgroup_taskset_size - return the number of tasks in taskset
1889 * @tset: taskset of interest
1890 */
1891int cgroup_taskset_size(struct cgroup_taskset *tset)
1892{
1893 return tset->tc_array ? tset->tc_array_len : 1;
1894}
1895EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1896
1897
Ben Blum74a11662011-05-26 16:25:20 -07001898/*
1899 * cgroup_task_migrate - move a task from one cgroup to another.
1900 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001901 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001902 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001903static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1904 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001905{
1906 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001907
1908 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001909 * We are synchronized through threadgroup_lock() against PF_EXITING
1910 * setting such that we can't race against cgroup_exit() changing the
1911 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001912 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001913 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001914 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001915
Ben Blum74a11662011-05-26 16:25:20 -07001916 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001917 rcu_assign_pointer(tsk->cgroups, newcg);
1918 task_unlock(tsk);
1919
1920 /* Update the css_set linked lists if we're using them */
1921 write_lock(&css_set_lock);
1922 if (!list_empty(&tsk->cg_list))
1923 list_move(&tsk->cg_list, &newcg->tasks);
1924 write_unlock(&css_set_lock);
1925
1926 /*
1927 * We just gained a reference on oldcg by taking it from the task. As
1928 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1929 * it here; it will be freed under RCU.
1930 */
Ben Blum74a11662011-05-26 16:25:20 -07001931 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001932 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001933}
1934
Li Zefana043e3b2008-02-23 15:24:09 -08001935/**
1936 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1937 * @cgrp: the cgroup the task is attaching to
1938 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001939 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001940 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1941 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001942 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001943int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001944{
Tejun Heo8f121912012-03-29 22:03:33 -07001945 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001946 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001947 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001948 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001949 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001950 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001951
Tejun Heocd3d0952011-12-12 18:12:21 -08001952 /* @tsk either already exited or can't exit until the end */
1953 if (tsk->flags & PF_EXITING)
1954 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001955
1956 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001957 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001958 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001959 return 0;
1960
Tejun Heo2f7ee562011-12-12 18:12:21 -08001961 tset.single.task = tsk;
1962 tset.single.cgrp = oldcgrp;
1963
Paul Menagebbcb81d2007-10-18 23:39:32 -07001964 for_each_subsys(root, ss) {
1965 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08001966 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001967 if (retval) {
1968 /*
1969 * Remember on which subsystem the can_attach()
1970 * failed, so that we only call cancel_attach()
1971 * against the subsystems whose can_attach()
1972 * succeeded. (See below)
1973 */
1974 failed_ss = ss;
1975 goto out;
1976 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001977 }
1978 }
1979
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001980 newcg = find_css_set(tsk->cgroups, cgrp);
1981 if (!newcg) {
1982 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001983 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001984 }
1985
1986 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001987
Paul Menagebbcb81d2007-10-18 23:39:32 -07001988 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001989 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001990 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001991 }
Ben Blum74a11662011-05-26 16:25:20 -07001992
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001993out:
1994 if (retval) {
1995 for_each_subsys(root, ss) {
1996 if (ss == failed_ss)
1997 /*
1998 * This subsystem was the one that failed the
1999 * can_attach() check earlier, so we don't need
2000 * to call cancel_attach() against it or any
2001 * remaining subsystems.
2002 */
2003 break;
2004 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002005 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08002006 }
2007 }
2008 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002009}
2010
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002011/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002012 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2013 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002014 * @tsk: the task to be attached
2015 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002016int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002017{
2018 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002019 int retval = 0;
2020
2021 cgroup_lock();
2022 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002023 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2024
2025 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002026 if (retval)
2027 break;
2028 }
2029 cgroup_unlock();
2030
2031 return retval;
2032}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002033EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002034
Ben Blum74a11662011-05-26 16:25:20 -07002035/**
2036 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2037 * @cgrp: the cgroup to attach to
2038 * @leader: the threadgroup leader task_struct of the group to be attached
2039 *
Tejun Heo257058a2011-12-12 18:12:21 -08002040 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2041 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002042 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002043static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002044{
2045 int retval, i, group_size;
2046 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002047 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002048 struct cgroupfs_root *root = cgrp->root;
2049 /* threadgroup list cursor and array */
2050 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002051 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002052 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002053 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002054
2055 /*
2056 * step 0: in order to do expensive, possibly blocking operations for
2057 * every thread, we cannot iterate the thread group list, since it needs
2058 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002059 * group - group_rwsem prevents new threads from appearing, and if
2060 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002061 */
2062 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002063 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002064 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002065 if (!group)
2066 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002067 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2068 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2069 if (retval)
2070 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002071
Ben Blum74a11662011-05-26 16:25:20 -07002072 tsk = leader;
2073 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002074 /*
2075 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2076 * already PF_EXITING could be freed from underneath us unless we
2077 * take an rcu_read_lock.
2078 */
2079 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002080 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002081 struct task_and_cgroup ent;
2082
Tejun Heocd3d0952011-12-12 18:12:21 -08002083 /* @tsk either already exited or can't exit until the end */
2084 if (tsk->flags & PF_EXITING)
2085 continue;
2086
Ben Blum74a11662011-05-26 16:25:20 -07002087 /* as per above, nr_threads may decrease, but not increase. */
2088 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002089 ent.task = tsk;
2090 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002091 /* nothing to do if this task is already in the cgroup */
2092 if (ent.cgrp == cgrp)
2093 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002094 /*
2095 * saying GFP_ATOMIC has no effect here because we did prealloc
2096 * earlier, but it's good form to communicate our expectations.
2097 */
Tejun Heo134d3372011-12-12 18:12:21 -08002098 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002099 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002100 i++;
2101 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002102 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002103 /* remember the number of threads in the array for later. */
2104 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002105 tset.tc_array = group;
2106 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002107
Tejun Heo134d3372011-12-12 18:12:21 -08002108 /* methods shouldn't be called if no task is actually migrating */
2109 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002110 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002111 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002112
Ben Blum74a11662011-05-26 16:25:20 -07002113 /*
2114 * step 1: check that we can legitimately attach to the cgroup.
2115 */
2116 for_each_subsys(root, ss) {
2117 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002118 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002119 if (retval) {
2120 failed_ss = ss;
2121 goto out_cancel_attach;
2122 }
2123 }
Ben Blum74a11662011-05-26 16:25:20 -07002124 }
2125
2126 /*
2127 * step 2: make sure css_sets exist for all threads to be migrated.
2128 * we use find_css_set, which allocates a new one if necessary.
2129 */
Ben Blum74a11662011-05-26 16:25:20 -07002130 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002131 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002132 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2133 if (!tc->cg) {
2134 retval = -ENOMEM;
2135 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002136 }
2137 }
2138
2139 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002140 * step 3: now that we're guaranteed success wrt the css_sets,
2141 * proceed to move all tasks to the new cgroup. There are no
2142 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002143 */
Ben Blum74a11662011-05-26 16:25:20 -07002144 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002145 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002146 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002147 }
2148 /* nothing is sensitive to fork() after this point. */
2149
2150 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002151 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002152 */
2153 for_each_subsys(root, ss) {
2154 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002155 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002156 }
2157
2158 /*
2159 * step 5: success! and cleanup
2160 */
Ben Blum74a11662011-05-26 16:25:20 -07002161 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002162out_put_css_set_refs:
2163 if (retval) {
2164 for (i = 0; i < group_size; i++) {
2165 tc = flex_array_get(group, i);
2166 if (!tc->cg)
2167 break;
2168 put_css_set(tc->cg);
2169 }
Ben Blum74a11662011-05-26 16:25:20 -07002170 }
2171out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002172 if (retval) {
2173 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002174 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002175 break;
Ben Blum74a11662011-05-26 16:25:20 -07002176 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002177 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002178 }
2179 }
Ben Blum74a11662011-05-26 16:25:20 -07002180out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002181 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002182 return retval;
2183}
2184
2185/*
2186 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002187 * function to attach either it or all tasks in its threadgroup. Will lock
2188 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002189 */
2190static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002191{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002192 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002193 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002194 int ret;
2195
Ben Blum74a11662011-05-26 16:25:20 -07002196 if (!cgroup_lock_live_group(cgrp))
2197 return -ENODEV;
2198
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002199retry_find_task:
2200 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002201 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002202 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002203 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002204 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002205 ret= -ESRCH;
2206 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002207 }
Ben Blum74a11662011-05-26 16:25:20 -07002208 /*
2209 * even if we're attaching all tasks in the thread group, we
2210 * only need to check permissions on one of them.
2211 */
David Howellsc69e8d92008-11-14 10:39:19 +11002212 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002213 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2214 !uid_eq(cred->euid, tcred->uid) &&
2215 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002216 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002217 ret = -EACCES;
2218 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002219 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002220 } else
2221 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002222
2223 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002224 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002225
2226 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002227 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002228 * trapped in a cpuset, or RT worker may be born in a cgroup
2229 * with no rt_runtime allocated. Just say no.
2230 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002231 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002232 ret = -EINVAL;
2233 rcu_read_unlock();
2234 goto out_unlock_cgroup;
2235 }
2236
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002237 get_task_struct(tsk);
2238 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002239
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002240 threadgroup_lock(tsk);
2241 if (threadgroup) {
2242 if (!thread_group_leader(tsk)) {
2243 /*
2244 * a race with de_thread from another thread's exec()
2245 * may strip us of our leadership, if this happens,
2246 * there is no choice but to throw this task away and
2247 * try again; this is
2248 * "double-double-toil-and-trouble-check locking".
2249 */
2250 threadgroup_unlock(tsk);
2251 put_task_struct(tsk);
2252 goto retry_find_task;
2253 }
2254 ret = cgroup_attach_proc(cgrp, tsk);
2255 } else
2256 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002257 threadgroup_unlock(tsk);
2258
Paul Menagebbcb81d2007-10-18 23:39:32 -07002259 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002260out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002261 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002262 return ret;
2263}
2264
Paul Menageaf351022008-07-25 01:47:01 -07002265static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2266{
Ben Blum74a11662011-05-26 16:25:20 -07002267 return attach_task_by_pid(cgrp, pid, false);
2268}
2269
2270static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2271{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002272 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002273}
2274
Paul Menagee788e062008-07-25 01:46:59 -07002275/**
2276 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2277 * @cgrp: the cgroup to be checked for liveness
2278 *
Paul Menage84eea842008-07-25 01:47:00 -07002279 * On success, returns true; the lock should be later released with
2280 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002281 */
Paul Menage84eea842008-07-25 01:47:00 -07002282bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002283{
2284 mutex_lock(&cgroup_mutex);
2285 if (cgroup_is_removed(cgrp)) {
2286 mutex_unlock(&cgroup_mutex);
2287 return false;
2288 }
2289 return true;
2290}
Ben Blum67523c42010-03-10 15:22:11 -08002291EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002292
2293static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2294 const char *buffer)
2295{
2296 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002297 if (strlen(buffer) >= PATH_MAX)
2298 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002299 if (!cgroup_lock_live_group(cgrp))
2300 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002301 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002302 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002303 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002304 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002305 return 0;
2306}
2307
2308static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2309 struct seq_file *seq)
2310{
2311 if (!cgroup_lock_live_group(cgrp))
2312 return -ENODEV;
2313 seq_puts(seq, cgrp->root->release_agent_path);
2314 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002315 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002316 return 0;
2317}
2318
Paul Menage84eea842008-07-25 01:47:00 -07002319/* A buffer size big enough for numbers or short strings */
2320#define CGROUP_LOCAL_BUFFER_SIZE 64
2321
Paul Menagee73d2c62008-04-29 01:00:06 -07002322static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002323 struct file *file,
2324 const char __user *userbuf,
2325 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002326{
Paul Menage84eea842008-07-25 01:47:00 -07002327 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002328 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002329 char *end;
2330
2331 if (!nbytes)
2332 return -EINVAL;
2333 if (nbytes >= sizeof(buffer))
2334 return -E2BIG;
2335 if (copy_from_user(buffer, userbuf, nbytes))
2336 return -EFAULT;
2337
2338 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002339 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002340 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002341 if (*end)
2342 return -EINVAL;
2343 retval = cft->write_u64(cgrp, cft, val);
2344 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002345 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002346 if (*end)
2347 return -EINVAL;
2348 retval = cft->write_s64(cgrp, cft, val);
2349 }
Paul Menage355e0c42007-10-18 23:39:33 -07002350 if (!retval)
2351 retval = nbytes;
2352 return retval;
2353}
2354
Paul Menagedb3b1492008-07-25 01:46:58 -07002355static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2356 struct file *file,
2357 const char __user *userbuf,
2358 size_t nbytes, loff_t *unused_ppos)
2359{
Paul Menage84eea842008-07-25 01:47:00 -07002360 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002361 int retval = 0;
2362 size_t max_bytes = cft->max_write_len;
2363 char *buffer = local_buffer;
2364
2365 if (!max_bytes)
2366 max_bytes = sizeof(local_buffer) - 1;
2367 if (nbytes >= max_bytes)
2368 return -E2BIG;
2369 /* Allocate a dynamic buffer if we need one */
2370 if (nbytes >= sizeof(local_buffer)) {
2371 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2372 if (buffer == NULL)
2373 return -ENOMEM;
2374 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002375 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2376 retval = -EFAULT;
2377 goto out;
2378 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002379
2380 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002381 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002382 if (!retval)
2383 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002384out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002385 if (buffer != local_buffer)
2386 kfree(buffer);
2387 return retval;
2388}
2389
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2391 size_t nbytes, loff_t *ppos)
2392{
2393 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002394 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002395
Li Zefan75139b82009-01-07 18:07:33 -08002396 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002397 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002398 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002399 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002400 if (cft->write_u64 || cft->write_s64)
2401 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002402 if (cft->write_string)
2403 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002404 if (cft->trigger) {
2405 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2406 return ret ? ret : nbytes;
2407 }
Paul Menage355e0c42007-10-18 23:39:33 -07002408 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002409}
2410
Paul Menagef4c753b2008-04-29 00:59:56 -07002411static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2412 struct file *file,
2413 char __user *buf, size_t nbytes,
2414 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002415{
Paul Menage84eea842008-07-25 01:47:00 -07002416 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002417 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002418 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2419
2420 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2421}
2422
Paul Menagee73d2c62008-04-29 01:00:06 -07002423static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2424 struct file *file,
2425 char __user *buf, size_t nbytes,
2426 loff_t *ppos)
2427{
Paul Menage84eea842008-07-25 01:47:00 -07002428 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002429 s64 val = cft->read_s64(cgrp, cft);
2430 int len = sprintf(tmp, "%lld\n", (long long) val);
2431
2432 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2433}
2434
Paul Menageddbcc7e2007-10-18 23:39:30 -07002435static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2436 size_t nbytes, loff_t *ppos)
2437{
2438 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002439 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002440
Li Zefan75139b82009-01-07 18:07:33 -08002441 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002442 return -ENODEV;
2443
2444 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002445 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002446 if (cft->read_u64)
2447 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002448 if (cft->read_s64)
2449 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002450 return -EINVAL;
2451}
2452
Paul Menage91796562008-04-29 01:00:01 -07002453/*
2454 * seqfile ops/methods for returning structured data. Currently just
2455 * supports string->u64 maps, but can be extended in future.
2456 */
2457
2458struct cgroup_seqfile_state {
2459 struct cftype *cft;
2460 struct cgroup *cgroup;
2461};
2462
2463static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2464{
2465 struct seq_file *sf = cb->state;
2466 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2467}
2468
2469static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2470{
2471 struct cgroup_seqfile_state *state = m->private;
2472 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002473 if (cft->read_map) {
2474 struct cgroup_map_cb cb = {
2475 .fill = cgroup_map_add,
2476 .state = m,
2477 };
2478 return cft->read_map(state->cgroup, cft, &cb);
2479 }
2480 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002481}
2482
Adrian Bunk96930a62008-07-25 19:46:21 -07002483static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002484{
2485 struct seq_file *seq = file->private_data;
2486 kfree(seq->private);
2487 return single_release(inode, file);
2488}
2489
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002490static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002491 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002492 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002493 .llseek = seq_lseek,
2494 .release = cgroup_seqfile_release,
2495};
2496
Paul Menageddbcc7e2007-10-18 23:39:30 -07002497static int cgroup_file_open(struct inode *inode, struct file *file)
2498{
2499 int err;
2500 struct cftype *cft;
2501
2502 err = generic_file_open(inode, file);
2503 if (err)
2504 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002505 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002506
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002507 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002508 struct cgroup_seqfile_state *state =
2509 kzalloc(sizeof(*state), GFP_USER);
2510 if (!state)
2511 return -ENOMEM;
2512 state->cft = cft;
2513 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2514 file->f_op = &cgroup_seqfile_operations;
2515 err = single_open(file, cgroup_seqfile_show, state);
2516 if (err < 0)
2517 kfree(state);
2518 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002519 err = cft->open(inode, file);
2520 else
2521 err = 0;
2522
2523 return err;
2524}
2525
2526static int cgroup_file_release(struct inode *inode, struct file *file)
2527{
2528 struct cftype *cft = __d_cft(file->f_dentry);
2529 if (cft->release)
2530 return cft->release(inode, file);
2531 return 0;
2532}
2533
2534/*
2535 * cgroup_rename - Only allow simple rename of directories in place.
2536 */
2537static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2538 struct inode *new_dir, struct dentry *new_dentry)
2539{
2540 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2541 return -ENOTDIR;
2542 if (new_dentry->d_inode)
2543 return -EEXIST;
2544 if (old_dir != new_dir)
2545 return -EIO;
2546 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2547}
2548
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002549static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2550{
2551 if (S_ISDIR(dentry->d_inode->i_mode))
2552 return &__d_cgrp(dentry)->xattrs;
2553 else
2554 return &__d_cft(dentry)->xattrs;
2555}
2556
2557static inline int xattr_enabled(struct dentry *dentry)
2558{
2559 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2560 return test_bit(ROOT_XATTR, &root->flags);
2561}
2562
2563static bool is_valid_xattr(const char *name)
2564{
2565 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2566 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2567 return true;
2568 return false;
2569}
2570
2571static int cgroup_setxattr(struct dentry *dentry, const char *name,
2572 const void *val, size_t size, int flags)
2573{
2574 if (!xattr_enabled(dentry))
2575 return -EOPNOTSUPP;
2576 if (!is_valid_xattr(name))
2577 return -EINVAL;
2578 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2579}
2580
2581static int cgroup_removexattr(struct dentry *dentry, const char *name)
2582{
2583 if (!xattr_enabled(dentry))
2584 return -EOPNOTSUPP;
2585 if (!is_valid_xattr(name))
2586 return -EINVAL;
2587 return simple_xattr_remove(__d_xattrs(dentry), name);
2588}
2589
2590static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2591 void *buf, size_t size)
2592{
2593 if (!xattr_enabled(dentry))
2594 return -EOPNOTSUPP;
2595 if (!is_valid_xattr(name))
2596 return -EINVAL;
2597 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2598}
2599
2600static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2601{
2602 if (!xattr_enabled(dentry))
2603 return -EOPNOTSUPP;
2604 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2605}
2606
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002607static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002608 .read = cgroup_file_read,
2609 .write = cgroup_file_write,
2610 .llseek = generic_file_llseek,
2611 .open = cgroup_file_open,
2612 .release = cgroup_file_release,
2613};
2614
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002615static const struct inode_operations cgroup_file_inode_operations = {
2616 .setxattr = cgroup_setxattr,
2617 .getxattr = cgroup_getxattr,
2618 .listxattr = cgroup_listxattr,
2619 .removexattr = cgroup_removexattr,
2620};
2621
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002622static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002623 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002624 .mkdir = cgroup_mkdir,
2625 .rmdir = cgroup_rmdir,
2626 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002627 .setxattr = cgroup_setxattr,
2628 .getxattr = cgroup_getxattr,
2629 .listxattr = cgroup_listxattr,
2630 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002631};
2632
Al Viro00cd8dd2012-06-10 17:13:09 -04002633static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002634{
2635 if (dentry->d_name.len > NAME_MAX)
2636 return ERR_PTR(-ENAMETOOLONG);
2637 d_add(dentry, NULL);
2638 return NULL;
2639}
2640
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002641/*
2642 * Check if a file is a control file
2643 */
2644static inline struct cftype *__file_cft(struct file *file)
2645{
Al Viro496ad9a2013-01-23 17:07:38 -05002646 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002647 return ERR_PTR(-EINVAL);
2648 return __d_cft(file->f_dentry);
2649}
2650
Al Viroa5e7ed32011-07-26 01:55:55 -04002651static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002652 struct super_block *sb)
2653{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002654 struct inode *inode;
2655
2656 if (!dentry)
2657 return -ENOENT;
2658 if (dentry->d_inode)
2659 return -EEXIST;
2660
2661 inode = cgroup_new_inode(mode, sb);
2662 if (!inode)
2663 return -ENOMEM;
2664
2665 if (S_ISDIR(mode)) {
2666 inode->i_op = &cgroup_dir_inode_operations;
2667 inode->i_fop = &simple_dir_operations;
2668
2669 /* start off with i_nlink == 2 (for "." entry) */
2670 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002671 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002672
Tejun Heob8a2df62012-11-19 08:13:37 -08002673 /*
2674 * Control reaches here with cgroup_mutex held.
2675 * @inode->i_mutex should nest outside cgroup_mutex but we
2676 * want to populate it immediately without releasing
2677 * cgroup_mutex. As @inode isn't visible to anyone else
2678 * yet, trylock will always succeed without affecting
2679 * lockdep checks.
2680 */
2681 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002682 } else if (S_ISREG(mode)) {
2683 inode->i_size = 0;
2684 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002685 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002686 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002687 d_instantiate(dentry, inode);
2688 dget(dentry); /* Extra count - pin the dentry in core */
2689 return 0;
2690}
2691
Li Zefan099fca32009-04-02 16:57:29 -07002692/**
2693 * cgroup_file_mode - deduce file mode of a control file
2694 * @cft: the control file in question
2695 *
2696 * returns cft->mode if ->mode is not 0
2697 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2698 * returns S_IRUGO if it has only a read handler
2699 * returns S_IWUSR if it has only a write hander
2700 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002701static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002702{
Al Viroa5e7ed32011-07-26 01:55:55 -04002703 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002704
2705 if (cft->mode)
2706 return cft->mode;
2707
2708 if (cft->read || cft->read_u64 || cft->read_s64 ||
2709 cft->read_map || cft->read_seq_string)
2710 mode |= S_IRUGO;
2711
2712 if (cft->write || cft->write_u64 || cft->write_s64 ||
2713 cft->write_string || cft->trigger)
2714 mode |= S_IWUSR;
2715
2716 return mode;
2717}
2718
Tejun Heodb0416b2012-04-01 12:09:55 -07002719static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002720 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721{
Paul Menagebd89aab2007-10-18 23:40:44 -07002722 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002723 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002725 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002726 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002727 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002728 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002729
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002730 simple_xattrs_init(&cft->xattrs);
2731
Paul Menagebd89aab2007-10-18 23:40:44 -07002732 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002733 strcpy(name, subsys->name);
2734 strcat(name, ".");
2735 }
2736 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002737
Paul Menageddbcc7e2007-10-18 23:39:30 -07002738 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002739
2740 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2741 if (!cfe)
2742 return -ENOMEM;
2743
Paul Menageddbcc7e2007-10-18 23:39:30 -07002744 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002745 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002746 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002747 goto out;
2748 }
2749
2750 mode = cgroup_file_mode(cft);
2751 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2752 if (!error) {
2753 cfe->type = (void *)cft;
2754 cfe->dentry = dentry;
2755 dentry->d_fsdata = cfe;
2756 list_add_tail(&cfe->node, &parent->files);
2757 cfe = NULL;
2758 }
2759 dput(dentry);
2760out:
2761 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002762 return error;
2763}
2764
Tejun Heo79578622012-04-01 12:09:56 -07002765static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002766 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002767{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002768 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002769 int err, ret = 0;
2770
2771 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002772 /* does cft->flags tell us to skip this file on @cgrp? */
2773 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2774 continue;
2775 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2776 continue;
2777
Li Zefan2739d3c2013-01-21 18:18:33 +08002778 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002779 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002780 if (err)
2781 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2782 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002783 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002784 } else {
2785 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002786 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002787 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002788 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002789}
2790
Tejun Heo8e3f6542012-04-01 12:09:55 -07002791static DEFINE_MUTEX(cgroup_cft_mutex);
2792
2793static void cgroup_cfts_prepare(void)
2794 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2795{
2796 /*
2797 * Thanks to the entanglement with vfs inode locking, we can't walk
2798 * the existing cgroups under cgroup_mutex and create files.
2799 * Instead, we increment reference on all cgroups and build list of
2800 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2801 * exclusive access to the field.
2802 */
2803 mutex_lock(&cgroup_cft_mutex);
2804 mutex_lock(&cgroup_mutex);
2805}
2806
2807static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002808 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002809 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2810{
2811 LIST_HEAD(pending);
2812 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002813
2814 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2815 if (cfts && ss->root != &rootnode) {
2816 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2817 dget(cgrp->dentry);
2818 list_add_tail(&cgrp->cft_q_node, &pending);
2819 }
2820 }
2821
2822 mutex_unlock(&cgroup_mutex);
2823
2824 /*
2825 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2826 * files for all cgroups which were created before.
2827 */
2828 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2829 struct inode *inode = cgrp->dentry->d_inode;
2830
2831 mutex_lock(&inode->i_mutex);
2832 mutex_lock(&cgroup_mutex);
2833 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002834 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002835 mutex_unlock(&cgroup_mutex);
2836 mutex_unlock(&inode->i_mutex);
2837
2838 list_del_init(&cgrp->cft_q_node);
2839 dput(cgrp->dentry);
2840 }
2841
2842 mutex_unlock(&cgroup_cft_mutex);
2843}
2844
2845/**
2846 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2847 * @ss: target cgroup subsystem
2848 * @cfts: zero-length name terminated array of cftypes
2849 *
2850 * Register @cfts to @ss. Files described by @cfts are created for all
2851 * existing cgroups to which @ss is attached and all future cgroups will
2852 * have them too. This function can be called anytime whether @ss is
2853 * attached or not.
2854 *
2855 * Returns 0 on successful registration, -errno on failure. Note that this
2856 * function currently returns 0 as long as @cfts registration is successful
2857 * even if some file creation attempts on existing cgroups fail.
2858 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002859int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002860{
2861 struct cftype_set *set;
2862
2863 set = kzalloc(sizeof(*set), GFP_KERNEL);
2864 if (!set)
2865 return -ENOMEM;
2866
2867 cgroup_cfts_prepare();
2868 set->cfts = cfts;
2869 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002870 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002871
2872 return 0;
2873}
2874EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2875
Li Zefana043e3b2008-02-23 15:24:09 -08002876/**
Tejun Heo79578622012-04-01 12:09:56 -07002877 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2878 * @ss: target cgroup subsystem
2879 * @cfts: zero-length name terminated array of cftypes
2880 *
2881 * Unregister @cfts from @ss. Files described by @cfts are removed from
2882 * all existing cgroups to which @ss is attached and all future cgroups
2883 * won't have them either. This function can be called anytime whether @ss
2884 * is attached or not.
2885 *
2886 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2887 * registered with @ss.
2888 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002889int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002890{
2891 struct cftype_set *set;
2892
2893 cgroup_cfts_prepare();
2894
2895 list_for_each_entry(set, &ss->cftsets, node) {
2896 if (set->cfts == cfts) {
2897 list_del_init(&set->node);
2898 cgroup_cfts_commit(ss, cfts, false);
2899 return 0;
2900 }
2901 }
2902
2903 cgroup_cfts_commit(ss, NULL, false);
2904 return -ENOENT;
2905}
2906
2907/**
Li Zefana043e3b2008-02-23 15:24:09 -08002908 * cgroup_task_count - count the number of tasks in a cgroup.
2909 * @cgrp: the cgroup in question
2910 *
2911 * Return the number of tasks in the cgroup.
2912 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002913int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002914{
2915 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002916 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002917
Paul Menage817929e2007-10-18 23:39:36 -07002918 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002919 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002920 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002921 }
2922 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002923 return count;
2924}
2925
2926/*
Paul Menage817929e2007-10-18 23:39:36 -07002927 * Advance a list_head iterator. The iterator should be positioned at
2928 * the start of a css_set
2929 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002930static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002931 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002932{
2933 struct list_head *l = it->cg_link;
2934 struct cg_cgroup_link *link;
2935 struct css_set *cg;
2936
2937 /* Advance to the next non-empty css_set */
2938 do {
2939 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002940 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002941 it->cg_link = NULL;
2942 return;
2943 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002944 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002945 cg = link->cg;
2946 } while (list_empty(&cg->tasks));
2947 it->cg_link = l;
2948 it->task = cg->tasks.next;
2949}
2950
Cliff Wickman31a7df02008-02-07 00:14:42 -08002951/*
2952 * To reduce the fork() overhead for systems that are not actually
2953 * using their cgroups capability, we don't maintain the lists running
2954 * through each css_set to its tasks until we see the list actually
2955 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002956 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002957static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002958{
2959 struct task_struct *p, *g;
2960 write_lock(&css_set_lock);
2961 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002962 /*
2963 * We need tasklist_lock because RCU is not safe against
2964 * while_each_thread(). Besides, a forking task that has passed
2965 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2966 * is not guaranteed to have its child immediately visible in the
2967 * tasklist if we walk through it with RCU.
2968 */
2969 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002970 do_each_thread(g, p) {
2971 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002972 /*
2973 * We should check if the process is exiting, otherwise
2974 * it will race with cgroup_exit() in that the list
2975 * entry won't be deleted though the process has exited.
2976 */
2977 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002978 list_add(&p->cg_list, &p->cgroups->tasks);
2979 task_unlock(p);
2980 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002981 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002982 write_unlock(&css_set_lock);
2983}
2984
Tejun Heo574bd9f2012-11-09 09:12:29 -08002985/**
2986 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2987 * @pos: the current position (%NULL to initiate traversal)
2988 * @cgroup: cgroup whose descendants to walk
2989 *
2990 * To be used by cgroup_for_each_descendant_pre(). Find the next
2991 * descendant to visit for pre-order traversal of @cgroup's descendants.
2992 */
2993struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2994 struct cgroup *cgroup)
2995{
2996 struct cgroup *next;
2997
2998 WARN_ON_ONCE(!rcu_read_lock_held());
2999
3000 /* if first iteration, pretend we just visited @cgroup */
3001 if (!pos) {
3002 if (list_empty(&cgroup->children))
3003 return NULL;
3004 pos = cgroup;
3005 }
3006
3007 /* visit the first child if exists */
3008 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3009 if (next)
3010 return next;
3011
3012 /* no child, visit my or the closest ancestor's next sibling */
3013 do {
3014 next = list_entry_rcu(pos->sibling.next, struct cgroup,
3015 sibling);
3016 if (&next->sibling != &pos->parent->children)
3017 return next;
3018
3019 pos = pos->parent;
3020 } while (pos != cgroup);
3021
3022 return NULL;
3023}
3024EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3025
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003026/**
3027 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3028 * @pos: cgroup of interest
3029 *
3030 * Return the rightmost descendant of @pos. If there's no descendant,
3031 * @pos is returned. This can be used during pre-order traversal to skip
3032 * subtree of @pos.
3033 */
3034struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3035{
3036 struct cgroup *last, *tmp;
3037
3038 WARN_ON_ONCE(!rcu_read_lock_held());
3039
3040 do {
3041 last = pos;
3042 /* ->prev isn't RCU safe, walk ->next till the end */
3043 pos = NULL;
3044 list_for_each_entry_rcu(tmp, &last->children, sibling)
3045 pos = tmp;
3046 } while (pos);
3047
3048 return last;
3049}
3050EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3051
Tejun Heo574bd9f2012-11-09 09:12:29 -08003052static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3053{
3054 struct cgroup *last;
3055
3056 do {
3057 last = pos;
3058 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3059 sibling);
3060 } while (pos);
3061
3062 return last;
3063}
3064
3065/**
3066 * cgroup_next_descendant_post - find the next descendant for post-order walk
3067 * @pos: the current position (%NULL to initiate traversal)
3068 * @cgroup: cgroup whose descendants to walk
3069 *
3070 * To be used by cgroup_for_each_descendant_post(). Find the next
3071 * descendant to visit for post-order traversal of @cgroup's descendants.
3072 */
3073struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3074 struct cgroup *cgroup)
3075{
3076 struct cgroup *next;
3077
3078 WARN_ON_ONCE(!rcu_read_lock_held());
3079
3080 /* if first iteration, visit the leftmost descendant */
3081 if (!pos) {
3082 next = cgroup_leftmost_descendant(cgroup);
3083 return next != cgroup ? next : NULL;
3084 }
3085
3086 /* if there's an unvisited sibling, visit its leftmost descendant */
3087 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3088 if (&next->sibling != &pos->parent->children)
3089 return cgroup_leftmost_descendant(next);
3090
3091 /* no sibling left, visit parent */
3092 next = pos->parent;
3093 return next != cgroup ? next : NULL;
3094}
3095EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3096
Paul Menagebd89aab2007-10-18 23:40:44 -07003097void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003098 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003099{
3100 /*
3101 * The first time anyone tries to iterate across a cgroup,
3102 * we need to enable the list linking each css_set to its
3103 * tasks, and fix up all existing tasks.
3104 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003105 if (!use_task_css_set_links)
3106 cgroup_enable_task_cg_lists();
3107
Paul Menage817929e2007-10-18 23:39:36 -07003108 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003109 it->cg_link = &cgrp->css_sets;
3110 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003111}
3112
Paul Menagebd89aab2007-10-18 23:40:44 -07003113struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003114 struct cgroup_iter *it)
3115{
3116 struct task_struct *res;
3117 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003118 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003119
3120 /* If the iterator cg is NULL, we have no tasks */
3121 if (!it->cg_link)
3122 return NULL;
3123 res = list_entry(l, struct task_struct, cg_list);
3124 /* Advance iterator to find next entry */
3125 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003126 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3127 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003128 /* We reached the end of this task list - move on to
3129 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003130 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003131 } else {
3132 it->task = l;
3133 }
3134 return res;
3135}
3136
Paul Menagebd89aab2007-10-18 23:40:44 -07003137void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003138 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003139{
3140 read_unlock(&css_set_lock);
3141}
3142
Cliff Wickman31a7df02008-02-07 00:14:42 -08003143static inline int started_after_time(struct task_struct *t1,
3144 struct timespec *time,
3145 struct task_struct *t2)
3146{
3147 int start_diff = timespec_compare(&t1->start_time, time);
3148 if (start_diff > 0) {
3149 return 1;
3150 } else if (start_diff < 0) {
3151 return 0;
3152 } else {
3153 /*
3154 * Arbitrarily, if two processes started at the same
3155 * time, we'll say that the lower pointer value
3156 * started first. Note that t2 may have exited by now
3157 * so this may not be a valid pointer any longer, but
3158 * that's fine - it still serves to distinguish
3159 * between two tasks started (effectively) simultaneously.
3160 */
3161 return t1 > t2;
3162 }
3163}
3164
3165/*
3166 * This function is a callback from heap_insert() and is used to order
3167 * the heap.
3168 * In this case we order the heap in descending task start time.
3169 */
3170static inline int started_after(void *p1, void *p2)
3171{
3172 struct task_struct *t1 = p1;
3173 struct task_struct *t2 = p2;
3174 return started_after_time(t1, &t2->start_time, t2);
3175}
3176
3177/**
3178 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3179 * @scan: struct cgroup_scanner containing arguments for the scan
3180 *
3181 * Arguments include pointers to callback functions test_task() and
3182 * process_task().
3183 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3184 * and if it returns true, call process_task() for it also.
3185 * The test_task pointer may be NULL, meaning always true (select all tasks).
3186 * Effectively duplicates cgroup_iter_{start,next,end}()
3187 * but does not lock css_set_lock for the call to process_task().
3188 * The struct cgroup_scanner may be embedded in any structure of the caller's
3189 * creation.
3190 * It is guaranteed that process_task() will act on every task that
3191 * is a member of the cgroup for the duration of this call. This
3192 * function may or may not call process_task() for tasks that exit
3193 * or move to a different cgroup during the call, or are forked or
3194 * move into the cgroup during the call.
3195 *
3196 * Note that test_task() may be called with locks held, and may in some
3197 * situations be called multiple times for the same task, so it should
3198 * be cheap.
3199 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3200 * pre-allocated and will be used for heap operations (and its "gt" member will
3201 * be overwritten), else a temporary heap will be used (allocation of which
3202 * may cause this function to fail).
3203 */
3204int cgroup_scan_tasks(struct cgroup_scanner *scan)
3205{
3206 int retval, i;
3207 struct cgroup_iter it;
3208 struct task_struct *p, *dropped;
3209 /* Never dereference latest_task, since it's not refcounted */
3210 struct task_struct *latest_task = NULL;
3211 struct ptr_heap tmp_heap;
3212 struct ptr_heap *heap;
3213 struct timespec latest_time = { 0, 0 };
3214
3215 if (scan->heap) {
3216 /* The caller supplied our heap and pre-allocated its memory */
3217 heap = scan->heap;
3218 heap->gt = &started_after;
3219 } else {
3220 /* We need to allocate our own heap memory */
3221 heap = &tmp_heap;
3222 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3223 if (retval)
3224 /* cannot allocate the heap */
3225 return retval;
3226 }
3227
3228 again:
3229 /*
3230 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3231 * to determine which are of interest, and using the scanner's
3232 * "process_task" callback to process any of them that need an update.
3233 * Since we don't want to hold any locks during the task updates,
3234 * gather tasks to be processed in a heap structure.
3235 * The heap is sorted by descending task start time.
3236 * If the statically-sized heap fills up, we overflow tasks that
3237 * started later, and in future iterations only consider tasks that
3238 * started after the latest task in the previous pass. This
3239 * guarantees forward progress and that we don't miss any tasks.
3240 */
3241 heap->size = 0;
3242 cgroup_iter_start(scan->cg, &it);
3243 while ((p = cgroup_iter_next(scan->cg, &it))) {
3244 /*
3245 * Only affect tasks that qualify per the caller's callback,
3246 * if he provided one
3247 */
3248 if (scan->test_task && !scan->test_task(p, scan))
3249 continue;
3250 /*
3251 * Only process tasks that started after the last task
3252 * we processed
3253 */
3254 if (!started_after_time(p, &latest_time, latest_task))
3255 continue;
3256 dropped = heap_insert(heap, p);
3257 if (dropped == NULL) {
3258 /*
3259 * The new task was inserted; the heap wasn't
3260 * previously full
3261 */
3262 get_task_struct(p);
3263 } else if (dropped != p) {
3264 /*
3265 * The new task was inserted, and pushed out a
3266 * different task
3267 */
3268 get_task_struct(p);
3269 put_task_struct(dropped);
3270 }
3271 /*
3272 * Else the new task was newer than anything already in
3273 * the heap and wasn't inserted
3274 */
3275 }
3276 cgroup_iter_end(scan->cg, &it);
3277
3278 if (heap->size) {
3279 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003280 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003281 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003282 latest_time = q->start_time;
3283 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003284 }
3285 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003286 scan->process_task(q, scan);
3287 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003288 }
3289 /*
3290 * If we had to process any tasks at all, scan again
3291 * in case some of them were in the middle of forking
3292 * children that didn't get processed.
3293 * Not the most efficient way to do it, but it avoids
3294 * having to take callback_mutex in the fork path
3295 */
3296 goto again;
3297 }
3298 if (heap == &tmp_heap)
3299 heap_free(&tmp_heap);
3300 return 0;
3301}
3302
Paul Menage817929e2007-10-18 23:39:36 -07003303/*
Ben Blum102a7752009-09-23 15:56:26 -07003304 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003305 *
3306 * Reading this file can return large amounts of data if a cgroup has
3307 * *lots* of attached tasks. So it may need several calls to read(),
3308 * but we cannot guarantee that the information we produce is correct
3309 * unless we produce it entirely atomically.
3310 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003311 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003312
Li Zefan24528252012-01-20 11:58:43 +08003313/* which pidlist file are we talking about? */
3314enum cgroup_filetype {
3315 CGROUP_FILE_PROCS,
3316 CGROUP_FILE_TASKS,
3317};
3318
3319/*
3320 * A pidlist is a list of pids that virtually represents the contents of one
3321 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3322 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3323 * to the cgroup.
3324 */
3325struct cgroup_pidlist {
3326 /*
3327 * used to find which pidlist is wanted. doesn't change as long as
3328 * this particular list stays in the list.
3329 */
3330 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3331 /* array of xids */
3332 pid_t *list;
3333 /* how many elements the above list has */
3334 int length;
3335 /* how many files are using the current array */
3336 int use_count;
3337 /* each of these stored in a list by its cgroup */
3338 struct list_head links;
3339 /* pointer to the cgroup we belong to, for list removal purposes */
3340 struct cgroup *owner;
3341 /* protects the other fields */
3342 struct rw_semaphore mutex;
3343};
3344
Paul Menagebbcb81d2007-10-18 23:39:32 -07003345/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003346 * The following two functions "fix" the issue where there are more pids
3347 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3348 * TODO: replace with a kernel-wide solution to this problem
3349 */
3350#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3351static void *pidlist_allocate(int count)
3352{
3353 if (PIDLIST_TOO_LARGE(count))
3354 return vmalloc(count * sizeof(pid_t));
3355 else
3356 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3357}
3358static void pidlist_free(void *p)
3359{
3360 if (is_vmalloc_addr(p))
3361 vfree(p);
3362 else
3363 kfree(p);
3364}
3365static void *pidlist_resize(void *p, int newcount)
3366{
3367 void *newlist;
3368 /* note: if new alloc fails, old p will still be valid either way */
3369 if (is_vmalloc_addr(p)) {
3370 newlist = vmalloc(newcount * sizeof(pid_t));
3371 if (!newlist)
3372 return NULL;
3373 memcpy(newlist, p, newcount * sizeof(pid_t));
3374 vfree(p);
3375 } else {
3376 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3377 }
3378 return newlist;
3379}
3380
3381/*
Ben Blum102a7752009-09-23 15:56:26 -07003382 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3383 * If the new stripped list is sufficiently smaller and there's enough memory
3384 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3385 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003386 */
Ben Blum102a7752009-09-23 15:56:26 -07003387/* is the size difference enough that we should re-allocate the array? */
3388#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3389static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003390{
Ben Blum102a7752009-09-23 15:56:26 -07003391 int src, dest = 1;
3392 pid_t *list = *p;
3393 pid_t *newlist;
3394
3395 /*
3396 * we presume the 0th element is unique, so i starts at 1. trivial
3397 * edge cases first; no work needs to be done for either
3398 */
3399 if (length == 0 || length == 1)
3400 return length;
3401 /* src and dest walk down the list; dest counts unique elements */
3402 for (src = 1; src < length; src++) {
3403 /* find next unique element */
3404 while (list[src] == list[src-1]) {
3405 src++;
3406 if (src == length)
3407 goto after;
3408 }
3409 /* dest always points to where the next unique element goes */
3410 list[dest] = list[src];
3411 dest++;
3412 }
3413after:
3414 /*
3415 * if the length difference is large enough, we want to allocate a
3416 * smaller buffer to save memory. if this fails due to out of memory,
3417 * we'll just stay with what we've got.
3418 */
3419 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003420 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003421 if (newlist)
3422 *p = newlist;
3423 }
3424 return dest;
3425}
3426
3427static int cmppid(const void *a, const void *b)
3428{
3429 return *(pid_t *)a - *(pid_t *)b;
3430}
3431
3432/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003433 * find the appropriate pidlist for our purpose (given procs vs tasks)
3434 * returns with the lock on that pidlist already held, and takes care
3435 * of the use count, or returns NULL with no locks held if we're out of
3436 * memory.
3437 */
3438static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3439 enum cgroup_filetype type)
3440{
3441 struct cgroup_pidlist *l;
3442 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003443 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003444
Ben Blum72a8cb32009-09-23 15:56:27 -07003445 /*
3446 * We can't drop the pidlist_mutex before taking the l->mutex in case
3447 * the last ref-holder is trying to remove l from the list at the same
3448 * time. Holding the pidlist_mutex precludes somebody taking whichever
3449 * list we find out from under us - compare release_pid_array().
3450 */
3451 mutex_lock(&cgrp->pidlist_mutex);
3452 list_for_each_entry(l, &cgrp->pidlists, links) {
3453 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003454 /* make sure l doesn't vanish out from under us */
3455 down_write(&l->mutex);
3456 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003457 return l;
3458 }
3459 }
3460 /* entry not found; create a new one */
3461 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3462 if (!l) {
3463 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003464 return l;
3465 }
3466 init_rwsem(&l->mutex);
3467 down_write(&l->mutex);
3468 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003469 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003470 l->use_count = 0; /* don't increment here */
3471 l->list = NULL;
3472 l->owner = cgrp;
3473 list_add(&l->links, &cgrp->pidlists);
3474 mutex_unlock(&cgrp->pidlist_mutex);
3475 return l;
3476}
3477
3478/*
Ben Blum102a7752009-09-23 15:56:26 -07003479 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3480 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003481static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3482 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003483{
3484 pid_t *array;
3485 int length;
3486 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003487 struct cgroup_iter it;
3488 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003489 struct cgroup_pidlist *l;
3490
3491 /*
3492 * If cgroup gets more users after we read count, we won't have
3493 * enough space - tough. This race is indistinguishable to the
3494 * caller from the case that the additional cgroup users didn't
3495 * show up until sometime later on.
3496 */
3497 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003498 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003499 if (!array)
3500 return -ENOMEM;
3501 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003502 cgroup_iter_start(cgrp, &it);
3503 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003504 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003505 break;
Ben Blum102a7752009-09-23 15:56:26 -07003506 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003507 if (type == CGROUP_FILE_PROCS)
3508 pid = task_tgid_vnr(tsk);
3509 else
3510 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003511 if (pid > 0) /* make sure to only use valid results */
3512 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003513 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003514 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003515 length = n;
3516 /* now sort & (if procs) strip out duplicates */
3517 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003518 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003519 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003520 l = cgroup_pidlist_find(cgrp, type);
3521 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003522 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003523 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003524 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003525 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003526 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003527 l->list = array;
3528 l->length = length;
3529 l->use_count++;
3530 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003531 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003532 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003533}
3534
Balbir Singh846c7bb2007-10-18 23:39:44 -07003535/**
Li Zefana043e3b2008-02-23 15:24:09 -08003536 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003537 * @stats: cgroupstats to fill information into
3538 * @dentry: A dentry entry belonging to the cgroup for which stats have
3539 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003540 *
3541 * Build and fill cgroupstats so that taskstats can export it to user
3542 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003543 */
3544int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3545{
3546 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003547 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003548 struct cgroup_iter it;
3549 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003550
Balbir Singh846c7bb2007-10-18 23:39:44 -07003551 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003552 * Validate dentry by checking the superblock operations,
3553 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003554 */
Li Zefan33d283b2008-11-19 15:36:48 -08003555 if (dentry->d_sb->s_op != &cgroup_ops ||
3556 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003557 goto err;
3558
3559 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003560 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003561
Paul Menagebd89aab2007-10-18 23:40:44 -07003562 cgroup_iter_start(cgrp, &it);
3563 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003564 switch (tsk->state) {
3565 case TASK_RUNNING:
3566 stats->nr_running++;
3567 break;
3568 case TASK_INTERRUPTIBLE:
3569 stats->nr_sleeping++;
3570 break;
3571 case TASK_UNINTERRUPTIBLE:
3572 stats->nr_uninterruptible++;
3573 break;
3574 case TASK_STOPPED:
3575 stats->nr_stopped++;
3576 break;
3577 default:
3578 if (delayacct_is_task_waiting_on_io(tsk))
3579 stats->nr_io_wait++;
3580 break;
3581 }
3582 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003583 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003584
Balbir Singh846c7bb2007-10-18 23:39:44 -07003585err:
3586 return ret;
3587}
3588
Paul Menage8f3ff202009-09-23 15:56:25 -07003589
Paul Menagecc31edc2008-10-18 20:28:04 -07003590/*
Ben Blum102a7752009-09-23 15:56:26 -07003591 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003592 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003593 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003594 */
3595
Ben Blum102a7752009-09-23 15:56:26 -07003596static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003597{
3598 /*
3599 * Initially we receive a position value that corresponds to
3600 * one more than the last pid shown (or 0 on the first call or
3601 * after a seek to the start). Use a binary-search to find the
3602 * next pid to display, if any
3603 */
Ben Blum102a7752009-09-23 15:56:26 -07003604 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003605 int index = 0, pid = *pos;
3606 int *iter;
3607
Ben Blum102a7752009-09-23 15:56:26 -07003608 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003609 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003610 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003611
Paul Menagecc31edc2008-10-18 20:28:04 -07003612 while (index < end) {
3613 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003614 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003615 index = mid;
3616 break;
Ben Blum102a7752009-09-23 15:56:26 -07003617 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003618 index = mid + 1;
3619 else
3620 end = mid;
3621 }
3622 }
3623 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003624 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003625 return NULL;
3626 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003627 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003628 *pos = *iter;
3629 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003630}
3631
Ben Blum102a7752009-09-23 15:56:26 -07003632static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003633{
Ben Blum102a7752009-09-23 15:56:26 -07003634 struct cgroup_pidlist *l = s->private;
3635 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003636}
3637
Ben Blum102a7752009-09-23 15:56:26 -07003638static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003639{
Ben Blum102a7752009-09-23 15:56:26 -07003640 struct cgroup_pidlist *l = s->private;
3641 pid_t *p = v;
3642 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003643 /*
3644 * Advance to the next pid in the array. If this goes off the
3645 * end, we're done
3646 */
3647 p++;
3648 if (p >= end) {
3649 return NULL;
3650 } else {
3651 *pos = *p;
3652 return p;
3653 }
3654}
3655
Ben Blum102a7752009-09-23 15:56:26 -07003656static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003657{
3658 return seq_printf(s, "%d\n", *(int *)v);
3659}
3660
Ben Blum102a7752009-09-23 15:56:26 -07003661/*
3662 * seq_operations functions for iterating on pidlists through seq_file -
3663 * independent of whether it's tasks or procs
3664 */
3665static const struct seq_operations cgroup_pidlist_seq_operations = {
3666 .start = cgroup_pidlist_start,
3667 .stop = cgroup_pidlist_stop,
3668 .next = cgroup_pidlist_next,
3669 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003670};
3671
Ben Blum102a7752009-09-23 15:56:26 -07003672static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003673{
Ben Blum72a8cb32009-09-23 15:56:27 -07003674 /*
3675 * the case where we're the last user of this particular pidlist will
3676 * have us remove it from the cgroup's list, which entails taking the
3677 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3678 * pidlist_mutex, we have to take pidlist_mutex first.
3679 */
3680 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003681 down_write(&l->mutex);
3682 BUG_ON(!l->use_count);
3683 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003684 /* we're the last user if refcount is 0; remove and free */
3685 list_del(&l->links);
3686 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003687 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003688 put_pid_ns(l->key.ns);
3689 up_write(&l->mutex);
3690 kfree(l);
3691 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003692 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003693 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003694 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003695}
3696
Ben Blum102a7752009-09-23 15:56:26 -07003697static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003698{
Ben Blum102a7752009-09-23 15:56:26 -07003699 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003700 if (!(file->f_mode & FMODE_READ))
3701 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003702 /*
3703 * the seq_file will only be initialized if the file was opened for
3704 * reading; hence we check if it's not null only in that case.
3705 */
3706 l = ((struct seq_file *)file->private_data)->private;
3707 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003708 return seq_release(inode, file);
3709}
3710
Ben Blum102a7752009-09-23 15:56:26 -07003711static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003712 .read = seq_read,
3713 .llseek = seq_lseek,
3714 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003715 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003716};
3717
3718/*
Ben Blum102a7752009-09-23 15:56:26 -07003719 * The following functions handle opens on a file that displays a pidlist
3720 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3721 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003722 */
Ben Blum102a7752009-09-23 15:56:26 -07003723/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003724static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003725{
3726 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003727 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003728 int retval;
3729
3730 /* Nothing to do for write-only files */
3731 if (!(file->f_mode & FMODE_READ))
3732 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003733
Ben Blum102a7752009-09-23 15:56:26 -07003734 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003735 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003736 if (retval)
3737 return retval;
3738 /* configure file information */
3739 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003740
Ben Blum102a7752009-09-23 15:56:26 -07003741 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003742 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003743 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003744 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003745 }
Ben Blum102a7752009-09-23 15:56:26 -07003746 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003747 return 0;
3748}
Ben Blum102a7752009-09-23 15:56:26 -07003749static int cgroup_tasks_open(struct inode *unused, struct file *file)
3750{
Ben Blum72a8cb32009-09-23 15:56:27 -07003751 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003752}
3753static int cgroup_procs_open(struct inode *unused, struct file *file)
3754{
Ben Blum72a8cb32009-09-23 15:56:27 -07003755 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003756}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003757
Paul Menagebd89aab2007-10-18 23:40:44 -07003758static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003759 struct cftype *cft)
3760{
Paul Menagebd89aab2007-10-18 23:40:44 -07003761 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003762}
3763
Paul Menage6379c102008-07-25 01:47:01 -07003764static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3765 struct cftype *cft,
3766 u64 val)
3767{
3768 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3769 if (val)
3770 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3771 else
3772 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3773 return 0;
3774}
3775
Paul Menagebbcb81d2007-10-18 23:39:32 -07003776/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003777 * Unregister event and free resources.
3778 *
3779 * Gets called from workqueue.
3780 */
3781static void cgroup_event_remove(struct work_struct *work)
3782{
3783 struct cgroup_event *event = container_of(work, struct cgroup_event,
3784 remove);
3785 struct cgroup *cgrp = event->cgrp;
3786
Li Zefan810cbee2013-02-18 18:56:14 +08003787 remove_wait_queue(event->wqh, &event->wait);
3788
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003789 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3790
Li Zefan810cbee2013-02-18 18:56:14 +08003791 /* Notify userspace the event is going away. */
3792 eventfd_signal(event->eventfd, 1);
3793
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003794 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003795 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003796 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003797}
3798
3799/*
3800 * Gets called on POLLHUP on eventfd when user closes it.
3801 *
3802 * Called with wqh->lock held and interrupts disabled.
3803 */
3804static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3805 int sync, void *key)
3806{
3807 struct cgroup_event *event = container_of(wait,
3808 struct cgroup_event, wait);
3809 struct cgroup *cgrp = event->cgrp;
3810 unsigned long flags = (unsigned long)key;
3811
3812 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003813 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003814 * If the event has been detached at cgroup removal, we
3815 * can simply return knowing the other side will cleanup
3816 * for us.
3817 *
3818 * We can't race against event freeing since the other
3819 * side will require wqh->lock via remove_wait_queue(),
3820 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003821 */
Li Zefan810cbee2013-02-18 18:56:14 +08003822 spin_lock(&cgrp->event_list_lock);
3823 if (!list_empty(&event->list)) {
3824 list_del_init(&event->list);
3825 /*
3826 * We are in atomic context, but cgroup_event_remove()
3827 * may sleep, so we have to call it in workqueue.
3828 */
3829 schedule_work(&event->remove);
3830 }
3831 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003832 }
3833
3834 return 0;
3835}
3836
3837static void cgroup_event_ptable_queue_proc(struct file *file,
3838 wait_queue_head_t *wqh, poll_table *pt)
3839{
3840 struct cgroup_event *event = container_of(pt,
3841 struct cgroup_event, pt);
3842
3843 event->wqh = wqh;
3844 add_wait_queue(wqh, &event->wait);
3845}
3846
3847/*
3848 * Parse input and register new cgroup event handler.
3849 *
3850 * Input must be in format '<event_fd> <control_fd> <args>'.
3851 * Interpretation of args is defined by control file implementation.
3852 */
3853static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3854 const char *buffer)
3855{
3856 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003857 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003858 unsigned int efd, cfd;
3859 struct file *efile = NULL;
3860 struct file *cfile = NULL;
3861 char *endp;
3862 int ret;
3863
3864 efd = simple_strtoul(buffer, &endp, 10);
3865 if (*endp != ' ')
3866 return -EINVAL;
3867 buffer = endp + 1;
3868
3869 cfd = simple_strtoul(buffer, &endp, 10);
3870 if ((*endp != ' ') && (*endp != '\0'))
3871 return -EINVAL;
3872 buffer = endp + 1;
3873
3874 event = kzalloc(sizeof(*event), GFP_KERNEL);
3875 if (!event)
3876 return -ENOMEM;
3877 event->cgrp = cgrp;
3878 INIT_LIST_HEAD(&event->list);
3879 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3880 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3881 INIT_WORK(&event->remove, cgroup_event_remove);
3882
3883 efile = eventfd_fget(efd);
3884 if (IS_ERR(efile)) {
3885 ret = PTR_ERR(efile);
3886 goto fail;
3887 }
3888
3889 event->eventfd = eventfd_ctx_fileget(efile);
3890 if (IS_ERR(event->eventfd)) {
3891 ret = PTR_ERR(event->eventfd);
3892 goto fail;
3893 }
3894
3895 cfile = fget(cfd);
3896 if (!cfile) {
3897 ret = -EBADF;
3898 goto fail;
3899 }
3900
3901 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003902 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003903 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003904 if (ret < 0)
3905 goto fail;
3906
3907 event->cft = __file_cft(cfile);
3908 if (IS_ERR(event->cft)) {
3909 ret = PTR_ERR(event->cft);
3910 goto fail;
3911 }
3912
Li Zefanf1690072013-02-18 14:13:35 +08003913 /*
3914 * The file to be monitored must be in the same cgroup as
3915 * cgroup.event_control is.
3916 */
3917 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3918 if (cgrp_cfile != cgrp) {
3919 ret = -EINVAL;
3920 goto fail;
3921 }
3922
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003923 if (!event->cft->register_event || !event->cft->unregister_event) {
3924 ret = -EINVAL;
3925 goto fail;
3926 }
3927
3928 ret = event->cft->register_event(cgrp, event->cft,
3929 event->eventfd, buffer);
3930 if (ret)
3931 goto fail;
3932
3933 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3934 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3935 ret = 0;
3936 goto fail;
3937 }
3938
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003939 /*
3940 * Events should be removed after rmdir of cgroup directory, but before
3941 * destroying subsystem state objects. Let's take reference to cgroup
3942 * directory dentry to do that.
3943 */
3944 dget(cgrp->dentry);
3945
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003946 spin_lock(&cgrp->event_list_lock);
3947 list_add(&event->list, &cgrp->event_list);
3948 spin_unlock(&cgrp->event_list_lock);
3949
3950 fput(cfile);
3951 fput(efile);
3952
3953 return 0;
3954
3955fail:
3956 if (cfile)
3957 fput(cfile);
3958
3959 if (event && event->eventfd && !IS_ERR(event->eventfd))
3960 eventfd_ctx_put(event->eventfd);
3961
3962 if (!IS_ERR_OR_NULL(efile))
3963 fput(efile);
3964
3965 kfree(event);
3966
3967 return ret;
3968}
3969
Daniel Lezcano97978e62010-10-27 15:33:35 -07003970static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3971 struct cftype *cft)
3972{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003973 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003974}
3975
3976static int cgroup_clone_children_write(struct cgroup *cgrp,
3977 struct cftype *cft,
3978 u64 val)
3979{
3980 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003981 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003982 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003983 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003984 return 0;
3985}
3986
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003987/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003988 * for the common functions, 'private' gives the type of file
3989 */
Ben Blum102a7752009-09-23 15:56:26 -07003990/* for hysterical raisins, we can't put this on the older files */
3991#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003992static struct cftype files[] = {
3993 {
3994 .name = "tasks",
3995 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003996 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003997 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003998 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003999 },
Ben Blum102a7752009-09-23 15:56:26 -07004000 {
4001 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
4002 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004003 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004004 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004005 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004006 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004007 {
4008 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07004009 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07004010 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004011 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004012 {
4013 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
4014 .write_string = cgroup_write_event_control,
4015 .mode = S_IWUGO,
4016 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004017 {
4018 .name = "cgroup.clone_children",
4019 .read_u64 = cgroup_clone_children_read,
4020 .write_u64 = cgroup_clone_children_write,
4021 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004022 {
4023 .name = "release_agent",
4024 .flags = CFTYPE_ONLY_ON_ROOT,
4025 .read_seq_string = cgroup_release_agent_show,
4026 .write_string = cgroup_release_agent_write,
4027 .max_write_len = PATH_MAX,
4028 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004029 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004030};
4031
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004032/**
4033 * cgroup_populate_dir - selectively creation of files in a directory
4034 * @cgrp: target cgroup
4035 * @base_files: true if the base files should be added
4036 * @subsys_mask: mask of the subsystem ids whose files should be added
4037 */
4038static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4039 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004040{
4041 int err;
4042 struct cgroup_subsys *ss;
4043
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004044 if (base_files) {
4045 err = cgroup_addrm_files(cgrp, NULL, files, true);
4046 if (err < 0)
4047 return err;
4048 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004049
Tejun Heo8e3f6542012-04-01 12:09:55 -07004050 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004051 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004052 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004053 if (!test_bit(ss->subsys_id, &subsys_mask))
4054 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004055
Tejun Heodb0416b2012-04-01 12:09:55 -07004056 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004057 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004058 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004059
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004060 /* This cgroup is ready now */
4061 for_each_subsys(cgrp->root, ss) {
4062 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4063 /*
4064 * Update id->css pointer and make this css visible from
4065 * CSS ID functions. This pointer will be dereferened
4066 * from RCU-read-side without locks.
4067 */
4068 if (css->id)
4069 rcu_assign_pointer(css->id->css, css);
4070 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004071
4072 return 0;
4073}
4074
Tejun Heo48ddbe12012-04-01 12:09:56 -07004075static void css_dput_fn(struct work_struct *work)
4076{
4077 struct cgroup_subsys_state *css =
4078 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004079 struct dentry *dentry = css->cgroup->dentry;
4080 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004081
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004082 atomic_inc(&sb->s_active);
4083 dput(dentry);
4084 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004085}
4086
Paul Menageddbcc7e2007-10-18 23:39:30 -07004087static void init_cgroup_css(struct cgroup_subsys_state *css,
4088 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004089 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004090{
Paul Menagebd89aab2007-10-18 23:40:44 -07004091 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004092 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004093 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004094 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004095 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004096 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004097 BUG_ON(cgrp->subsys[ss->subsys_id]);
4098 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004099
4100 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004101 * css holds an extra ref to @cgrp->dentry which is put on the last
4102 * css_put(). dput() requires process context, which css_put() may
4103 * be called without. @css->dput_work will be used to invoke
4104 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004105 */
4106 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004107}
4108
Tejun Heob1929db2012-11-19 08:13:38 -08004109/* invoke ->post_create() on a new CSS and mark it online if successful */
4110static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004111{
Tejun Heob1929db2012-11-19 08:13:38 -08004112 int ret = 0;
4113
Tejun Heoa31f2d32012-11-19 08:13:37 -08004114 lockdep_assert_held(&cgroup_mutex);
4115
Tejun Heo92fb9742012-11-19 08:13:38 -08004116 if (ss->css_online)
4117 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004118 if (!ret)
4119 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4120 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004121}
4122
4123/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4124static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4125 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4126{
4127 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4128
4129 lockdep_assert_held(&cgroup_mutex);
4130
4131 if (!(css->flags & CSS_ONLINE))
4132 return;
4133
4134 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004135 * css_offline() should be called with cgroup_mutex unlocked. See
Tejun Heoa31f2d32012-11-19 08:13:37 -08004136 * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for
4137 * details. This temporary unlocking should go away once
4138 * cgroup_mutex is unexported from controllers.
4139 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004140 if (ss->css_offline) {
Tejun Heoa31f2d32012-11-19 08:13:37 -08004141 mutex_unlock(&cgroup_mutex);
Tejun Heo92fb9742012-11-19 08:13:38 -08004142 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004143 mutex_lock(&cgroup_mutex);
4144 }
4145
4146 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4147}
4148
Paul Menageddbcc7e2007-10-18 23:39:30 -07004149/*
Li Zefana043e3b2008-02-23 15:24:09 -08004150 * cgroup_create - create a cgroup
4151 * @parent: cgroup that will be parent of the new cgroup
4152 * @dentry: dentry of the new cgroup
4153 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004154 *
Li Zefana043e3b2008-02-23 15:24:09 -08004155 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004156 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004157static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004158 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004159{
Paul Menagebd89aab2007-10-18 23:40:44 -07004160 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004161 struct cgroupfs_root *root = parent->root;
4162 int err = 0;
4163 struct cgroup_subsys *ss;
4164 struct super_block *sb = root->sb;
4165
Tejun Heo0a950f62012-11-19 09:02:12 -08004166 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004167 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4168 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004169 return -ENOMEM;
4170
Tejun Heo0a950f62012-11-19 09:02:12 -08004171 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4172 if (cgrp->id < 0)
4173 goto err_free_cgrp;
4174
Tejun Heo976c06b2012-11-05 09:16:59 -08004175 /*
4176 * Only live parents can have children. Note that the liveliness
4177 * check isn't strictly necessary because cgroup_mkdir() and
4178 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4179 * anyway so that locking is contained inside cgroup proper and we
4180 * don't get nasty surprises if we ever grow another caller.
4181 */
4182 if (!cgroup_lock_live_group(parent)) {
4183 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004184 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004185 }
4186
Paul Menageddbcc7e2007-10-18 23:39:30 -07004187 /* Grab a reference on the superblock so the hierarchy doesn't
4188 * get deleted on unmount if there are child cgroups. This
4189 * can be done outside cgroup_mutex, since the sb can't
4190 * disappear while someone has an open control file on the
4191 * fs */
4192 atomic_inc(&sb->s_active);
4193
Paul Menagecc31edc2008-10-18 20:28:04 -07004194 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004195
Li Zefanfe1c06c2013-01-24 14:30:22 +08004196 dentry->d_fsdata = cgrp;
4197 cgrp->dentry = dentry;
4198
Paul Menagebd89aab2007-10-18 23:40:44 -07004199 cgrp->parent = parent;
4200 cgrp->root = parent->root;
4201 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004202
Li Zefanb6abdb02008-03-04 14:28:19 -08004203 if (notify_on_release(parent))
4204 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4205
Tejun Heo2260e7f2012-11-19 08:13:38 -08004206 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4207 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004208
Paul Menageddbcc7e2007-10-18 23:39:30 -07004209 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004210 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004211
Tejun Heo92fb9742012-11-19 08:13:38 -08004212 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004213 if (IS_ERR(css)) {
4214 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004215 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004216 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004217 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004218 if (ss->use_id) {
4219 err = alloc_css_id(ss, parent, cgrp);
4220 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004221 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004222 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004223 }
4224
Tejun Heo4e139af2012-11-19 08:13:36 -08004225 /*
4226 * Create directory. cgroup_create_file() returns with the new
4227 * directory locked on success so that it can be populated without
4228 * dropping cgroup_mutex.
4229 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004230 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004231 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004232 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004233 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004234
Tejun Heo4e139af2012-11-19 08:13:36 -08004235 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004236 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4237 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4238 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004239
Tejun Heob1929db2012-11-19 08:13:38 -08004240 /* each css holds a ref to the cgroup's dentry */
4241 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004242 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004243
Tejun Heob1929db2012-11-19 08:13:38 -08004244 /* creation succeeded, notify subsystems */
4245 for_each_subsys(root, ss) {
4246 err = online_css(ss, cgrp);
4247 if (err)
4248 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004249
4250 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4251 parent->parent) {
4252 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",
4253 current->comm, current->pid, ss->name);
4254 if (!strcmp(ss->name, "memory"))
4255 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4256 ss->warned_broken_hierarchy = true;
4257 }
Tejun Heoa8638032012-11-09 09:12:29 -08004258 }
4259
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004260 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004261 if (err)
4262 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004263
4264 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004265 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004266
4267 return 0;
4268
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004269err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004270 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004271 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004272 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004273 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004274 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004275 /* Release the reference count that we took on the superblock */
4276 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004277err_free_id:
4278 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004279err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004280 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004281 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004282
4283err_destroy:
4284 cgroup_destroy_locked(cgrp);
4285 mutex_unlock(&cgroup_mutex);
4286 mutex_unlock(&dentry->d_inode->i_mutex);
4287 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004288}
4289
Al Viro18bb1db2011-07-26 01:41:39 -04004290static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004291{
4292 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4293
4294 /* the vfs holds inode->i_mutex already */
4295 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4296}
4297
Tejun Heo28b4c272012-04-01 12:09:56 -07004298/*
4299 * Check the reference count on each subsystem. Since we already
4300 * established that there are no tasks in the cgroup, if the css refcount
4301 * is also 1, then there should be no outstanding references, so the
4302 * subsystem is safe to destroy. We scan across all subsystems rather than
4303 * using the per-hierarchy linked list of mounted subsystems since we can
4304 * be called via check_for_release() with no synchronization other than
4305 * RCU, and the subsystem linked list isn't RCU-safe.
4306 */
Li Zefan55b6fd02008-07-29 22:33:20 -07004307static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004308{
Paul Menage81a6a5c2007-10-18 23:39:38 -07004309 int i;
Tejun Heo28b4c272012-04-01 12:09:56 -07004310
Ben Blumaae8aab2010-03-10 15:22:07 -08004311 /*
4312 * We won't need to lock the subsys array, because the subsystems
4313 * we're concerned about aren't going anywhere since our cgroup root
4314 * has a reference on them.
4315 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004316 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4317 struct cgroup_subsys *ss = subsys[i];
4318 struct cgroup_subsys_state *css;
Tejun Heo28b4c272012-04-01 12:09:56 -07004319
Ben Blumaae8aab2010-03-10 15:22:07 -08004320 /* Skip subsystems not present or not in this hierarchy */
4321 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004322 continue;
Tejun Heo28b4c272012-04-01 12:09:56 -07004323
Paul Menagebd89aab2007-10-18 23:40:44 -07004324 css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004325 /*
4326 * When called from check_for_release() it's possible
Paul Menage81a6a5c2007-10-18 23:39:38 -07004327 * that by this point the cgroup has been removed
4328 * and the css deleted. But a false-positive doesn't
4329 * matter, since it can only happen if the cgroup
4330 * has been deleted and hence no longer needs the
Tejun Heo28b4c272012-04-01 12:09:56 -07004331 * release agent to be called anyway.
4332 */
4333 if (css && css_refcnt(css) > 1)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004334 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004335 }
4336 return 0;
4337}
4338
Tejun Heo42809dd2012-11-19 08:13:37 -08004339static int cgroup_destroy_locked(struct cgroup *cgrp)
4340 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004341{
Tejun Heo42809dd2012-11-19 08:13:37 -08004342 struct dentry *d = cgrp->dentry;
4343 struct cgroup *parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004344 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004345 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004346 struct cgroup_subsys *ss;
Greg Thelen205a8722012-11-28 13:50:44 -08004347 LIST_HEAD(tmp_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004348
Tejun Heo42809dd2012-11-19 08:13:37 -08004349 lockdep_assert_held(&d->d_inode->i_mutex);
4350 lockdep_assert_held(&cgroup_mutex);
4351
4352 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004353 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004354
Tejun Heo1a90dd52012-11-05 09:16:59 -08004355 /*
4356 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4357 * removed. This makes future css_tryget() and child creation
4358 * attempts fail thus maintaining the removal conditions verified
4359 * above.
4360 */
Tejun Heoed9577932012-11-05 09:16:58 -08004361 for_each_subsys(cgrp->root, ss) {
4362 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4363
4364 WARN_ON(atomic_read(&css->refcnt) < 0);
4365 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004366 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004367 set_bit(CGRP_REMOVED, &cgrp->flags);
4368
Tejun Heoa31f2d32012-11-19 08:13:37 -08004369 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004370 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004371 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004372
4373 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004374 * Put all the base refs. Each css holds an extra reference to the
4375 * cgroup's dentry and cgroup removal proceeds regardless of css
4376 * refs. On the last put of each css, whenever that may be, the
4377 * extra dentry ref is put so that dentry destruction happens only
4378 * after all css's are released.
4379 */
Tejun Heoe9316082012-11-05 09:16:58 -08004380 for_each_subsys(cgrp->root, ss)
4381 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004382
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004383 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004384 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004385 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004386 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004387
Paul Menage999cd8a2009-01-07 18:08:36 -08004388 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004389 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004390 list_del_init(&cgrp->allcg_node);
4391
Tejun Heo42809dd2012-11-19 08:13:37 -08004392 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004393 cgroup_d_remove_dir(d);
4394 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004395
Paul Menagebd89aab2007-10-18 23:40:44 -07004396 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004397 check_for_release(parent);
4398
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004399 /*
4400 * Unregister events and notify userspace.
4401 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004402 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004403 */
4404 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004405 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004406 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004407 schedule_work(&event->remove);
4408 }
Li Zefan810cbee2013-02-18 18:56:14 +08004409 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004410
Paul Menageddbcc7e2007-10-18 23:39:30 -07004411 return 0;
4412}
4413
Tejun Heo42809dd2012-11-19 08:13:37 -08004414static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4415{
4416 int ret;
4417
4418 mutex_lock(&cgroup_mutex);
4419 ret = cgroup_destroy_locked(dentry->d_fsdata);
4420 mutex_unlock(&cgroup_mutex);
4421
4422 return ret;
4423}
4424
Tejun Heo8e3f6542012-04-01 12:09:55 -07004425static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4426{
4427 INIT_LIST_HEAD(&ss->cftsets);
4428
4429 /*
4430 * base_cftset is embedded in subsys itself, no need to worry about
4431 * deregistration.
4432 */
4433 if (ss->base_cftypes) {
4434 ss->base_cftset.cfts = ss->base_cftypes;
4435 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4436 }
4437}
4438
Li Zefan06a11922008-04-29 01:00:07 -07004439static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004440{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004441 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004442
4443 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004444
Tejun Heo648bb562012-11-19 08:13:36 -08004445 mutex_lock(&cgroup_mutex);
4446
Tejun Heo8e3f6542012-04-01 12:09:55 -07004447 /* init base cftset */
4448 cgroup_init_cftsets(ss);
4449
Paul Menageddbcc7e2007-10-18 23:39:30 -07004450 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004451 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004452 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004453 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004454 /* We don't handle early failures gracefully */
4455 BUG_ON(IS_ERR(css));
4456 init_cgroup_css(css, ss, dummytop);
4457
Li Zefane8d55fd2008-04-29 01:00:13 -07004458 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004459 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004460 * newly registered, all tasks and hence the
4461 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004462 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004463
4464 need_forkexit_callback |= ss->fork || ss->exit;
4465
Li Zefane8d55fd2008-04-29 01:00:13 -07004466 /* At system boot, before all subsystems have been
4467 * registered, no tasks have been forked, so we don't
4468 * need to invoke fork callbacks here. */
4469 BUG_ON(!list_empty(&init_task.tasks));
4470
Paul Menageddbcc7e2007-10-18 23:39:30 -07004471 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004472 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004473
Tejun Heo648bb562012-11-19 08:13:36 -08004474 mutex_unlock(&cgroup_mutex);
4475
Ben Blume6a11052010-03-10 15:22:09 -08004476 /* this function shouldn't be used with modular subsystems, since they
4477 * need to register a subsys_id, among other things */
4478 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004479}
4480
4481/**
Ben Blume6a11052010-03-10 15:22:09 -08004482 * cgroup_load_subsys: load and register a modular subsystem at runtime
4483 * @ss: the subsystem to load
4484 *
4485 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004486 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004487 * up for use. If the subsystem is built-in anyway, work is delegated to the
4488 * simpler cgroup_init_subsys.
4489 */
4490int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4491{
Ben Blume6a11052010-03-10 15:22:09 -08004492 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004493 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004494 struct hlist_node *tmp;
Li Zefan0ac801f2013-01-10 11:49:27 +08004495 struct css_set *cg;
4496 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004497
4498 /* check name and function validity */
4499 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004500 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004501 return -EINVAL;
4502
4503 /*
4504 * we don't support callbacks in modular subsystems. this check is
4505 * before the ss->module check for consistency; a subsystem that could
4506 * be a module should still have no callbacks even if the user isn't
4507 * compiling it as one.
4508 */
4509 if (ss->fork || ss->exit)
4510 return -EINVAL;
4511
4512 /*
4513 * an optionally modular subsystem is built-in: we want to do nothing,
4514 * since cgroup_init_subsys will have already taken care of it.
4515 */
4516 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004517 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004518 BUG_ON(subsys[ss->subsys_id] != ss);
4519 return 0;
4520 }
4521
Tejun Heo8e3f6542012-04-01 12:09:55 -07004522 /* init base cftset */
4523 cgroup_init_cftsets(ss);
4524
Ben Blume6a11052010-03-10 15:22:09 -08004525 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004526 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004527
4528 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004529 * no ss->css_alloc seems to need anything important in the ss
4530 * struct, so this can happen first (i.e. before the rootnode
4531 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004532 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004533 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004534 if (IS_ERR(css)) {
4535 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004536 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004537 mutex_unlock(&cgroup_mutex);
4538 return PTR_ERR(css);
4539 }
4540
4541 list_add(&ss->sibling, &rootnode.subsys_list);
4542 ss->root = &rootnode;
4543
4544 /* our new subsystem will be attached to the dummy hierarchy. */
4545 init_cgroup_css(css, ss, dummytop);
4546 /* init_idr must be after init_cgroup_css because it sets css->id. */
4547 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004548 ret = cgroup_init_idr(ss, css);
4549 if (ret)
4550 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004551 }
4552
4553 /*
4554 * Now we need to entangle the css into the existing css_sets. unlike
4555 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4556 * will need a new pointer to it; done by iterating the css_set_table.
4557 * furthermore, modifying the existing css_sets will corrupt the hash
4558 * table state, so each changed css_set will need its hash recomputed.
4559 * this is all done under the css_set_lock.
4560 */
4561 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004562 hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004563 /* skip entries that we already rehashed */
4564 if (cg->subsys[ss->subsys_id])
4565 continue;
4566 /* remove existing entry */
4567 hash_del(&cg->hlist);
4568 /* set new value */
4569 cg->subsys[ss->subsys_id] = css;
4570 /* recompute hash and restore entry */
4571 key = css_set_hash(cg->subsys);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004572 hash_add(css_set_table, &cg->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004573 }
4574 write_unlock(&css_set_lock);
4575
Ben Blume6a11052010-03-10 15:22:09 -08004576 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004577 ret = online_css(ss, dummytop);
4578 if (ret)
4579 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004580
Ben Blume6a11052010-03-10 15:22:09 -08004581 /* success! */
4582 mutex_unlock(&cgroup_mutex);
4583 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004584
4585err_unload:
4586 mutex_unlock(&cgroup_mutex);
4587 /* @ss can't be mounted here as try_module_get() would fail */
4588 cgroup_unload_subsys(ss);
4589 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004590}
4591EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4592
4593/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004594 * cgroup_unload_subsys: unload a modular subsystem
4595 * @ss: the subsystem to unload
4596 *
4597 * This function should be called in a modular subsystem's exitcall. When this
4598 * function is invoked, the refcount on the subsystem's module will be 0, so
4599 * the subsystem will not be attached to any hierarchy.
4600 */
4601void cgroup_unload_subsys(struct cgroup_subsys *ss)
4602{
4603 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004604
4605 BUG_ON(ss->module == NULL);
4606
4607 /*
4608 * we shouldn't be called if the subsystem is in use, and the use of
4609 * try_module_get in parse_cgroupfs_options should ensure that it
4610 * doesn't start being used while we're killing it off.
4611 */
4612 BUG_ON(ss->root != &rootnode);
4613
4614 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004615
Tejun Heoa31f2d32012-11-19 08:13:37 -08004616 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004617 ss->active = 0;
4618
Tejun Heoc897ff62013-02-27 17:03:49 -08004619 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004620 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004621
Ben Blumcf5d5942010-03-10 15:22:09 -08004622 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004623 subsys[ss->subsys_id] = NULL;
4624
4625 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004626 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004627
4628 /*
4629 * disentangle the css from all css_sets attached to the dummytop. as
4630 * in loading, we need to pay our respects to the hashtable gods.
4631 */
4632 write_lock(&css_set_lock);
4633 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4634 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004635 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004636
Li Zefan0ac801f2013-01-10 11:49:27 +08004637 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004638 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004639 key = css_set_hash(cg->subsys);
4640 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004641 }
4642 write_unlock(&css_set_lock);
4643
4644 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004645 * remove subsystem's css from the dummytop and free it - need to
4646 * free before marking as null because ss->css_free needs the
4647 * cgrp->subsys pointer to find their state. note that this also
4648 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004649 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004650 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004651 dummytop->subsys[ss->subsys_id] = NULL;
4652
4653 mutex_unlock(&cgroup_mutex);
4654}
4655EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4656
4657/**
Li Zefana043e3b2008-02-23 15:24:09 -08004658 * cgroup_init_early - cgroup initialization at system boot
4659 *
4660 * Initialize cgroups at system boot, and initialize any
4661 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004662 */
4663int __init cgroup_init_early(void)
4664{
4665 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004666 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004667 INIT_LIST_HEAD(&init_css_set.cg_links);
4668 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004669 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004670 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004671 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004672 root_count = 1;
4673 init_task.cgroups = &init_css_set;
4674
4675 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004676 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004677 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004678 &rootnode.top_cgroup.css_sets);
4679 list_add(&init_css_set_link.cg_link_list,
4680 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004681
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004682 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004683 struct cgroup_subsys *ss = subsys[i];
4684
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004685 /* at bootup time, we don't worry about modular subsystems */
4686 if (!ss || ss->module)
4687 continue;
4688
Paul Menageddbcc7e2007-10-18 23:39:30 -07004689 BUG_ON(!ss->name);
4690 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004691 BUG_ON(!ss->css_alloc);
4692 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004693 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004694 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004695 ss->name, ss->subsys_id);
4696 BUG();
4697 }
4698
4699 if (ss->early_init)
4700 cgroup_init_subsys(ss);
4701 }
4702 return 0;
4703}
4704
4705/**
Li Zefana043e3b2008-02-23 15:24:09 -08004706 * cgroup_init - cgroup initialization
4707 *
4708 * Register cgroup filesystem and /proc file, and initialize
4709 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004710 */
4711int __init cgroup_init(void)
4712{
4713 int err;
4714 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004715 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004716
4717 err = bdi_init(&cgroup_backing_dev_info);
4718 if (err)
4719 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004720
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004721 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004722 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004723
4724 /* at bootup time, we don't worry about modular subsystems */
4725 if (!ss || ss->module)
4726 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004727 if (!ss->early_init)
4728 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004729 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004730 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004731 }
4732
Li Zefan472b1052008-04-29 01:00:11 -07004733 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004734 key = css_set_hash(init_css_set.subsys);
4735 hash_add(css_set_table, &init_css_set.hlist, key);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004736 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004737
4738 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4739 if (!cgroup_kobj) {
4740 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004741 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004742 }
4743
4744 err = register_filesystem(&cgroup_fs_type);
4745 if (err < 0) {
4746 kobject_put(cgroup_kobj);
4747 goto out;
4748 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004749
Li Zefan46ae2202008-04-29 01:00:08 -07004750 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004751
Paul Menageddbcc7e2007-10-18 23:39:30 -07004752out:
Paul Menagea4243162007-10-18 23:39:35 -07004753 if (err)
4754 bdi_destroy(&cgroup_backing_dev_info);
4755
Paul Menageddbcc7e2007-10-18 23:39:30 -07004756 return err;
4757}
Paul Menageb4f48b62007-10-18 23:39:33 -07004758
Paul Menagea4243162007-10-18 23:39:35 -07004759/*
4760 * proc_cgroup_show()
4761 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4762 * - Used for /proc/<pid>/cgroup.
4763 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4764 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004765 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004766 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4767 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4768 * cgroup to top_cgroup.
4769 */
4770
4771/* TODO: Use a proper seq_file iterator */
4772static int proc_cgroup_show(struct seq_file *m, void *v)
4773{
4774 struct pid *pid;
4775 struct task_struct *tsk;
4776 char *buf;
4777 int retval;
4778 struct cgroupfs_root *root;
4779
4780 retval = -ENOMEM;
4781 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4782 if (!buf)
4783 goto out;
4784
4785 retval = -ESRCH;
4786 pid = m->private;
4787 tsk = get_pid_task(pid, PIDTYPE_PID);
4788 if (!tsk)
4789 goto out_free;
4790
4791 retval = 0;
4792
4793 mutex_lock(&cgroup_mutex);
4794
Li Zefane5f6a862009-01-07 18:07:41 -08004795 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004796 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004797 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004798 int count = 0;
4799
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004800 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004801 for_each_subsys(root, ss)
4802 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004803 if (strlen(root->name))
4804 seq_printf(m, "%sname=%s", count ? "," : "",
4805 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004806 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004807 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004808 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004809 if (retval < 0)
4810 goto out_unlock;
4811 seq_puts(m, buf);
4812 seq_putc(m, '\n');
4813 }
4814
4815out_unlock:
4816 mutex_unlock(&cgroup_mutex);
4817 put_task_struct(tsk);
4818out_free:
4819 kfree(buf);
4820out:
4821 return retval;
4822}
4823
4824static int cgroup_open(struct inode *inode, struct file *file)
4825{
4826 struct pid *pid = PROC_I(inode)->pid;
4827 return single_open(file, proc_cgroup_show, pid);
4828}
4829
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004830const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004831 .open = cgroup_open,
4832 .read = seq_read,
4833 .llseek = seq_lseek,
4834 .release = single_release,
4835};
4836
4837/* Display information about each subsystem and each hierarchy */
4838static int proc_cgroupstats_show(struct seq_file *m, void *v)
4839{
4840 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004841
Paul Menage8bab8dd2008-04-04 14:29:57 -07004842 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004843 /*
4844 * ideally we don't want subsystems moving around while we do this.
4845 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4846 * subsys/hierarchy state.
4847 */
Paul Menagea4243162007-10-18 23:39:35 -07004848 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004849 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4850 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004851 if (ss == NULL)
4852 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004853 seq_printf(m, "%s\t%d\t%d\t%d\n",
4854 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004855 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004856 }
4857 mutex_unlock(&cgroup_mutex);
4858 return 0;
4859}
4860
4861static int cgroupstats_open(struct inode *inode, struct file *file)
4862{
Al Viro9dce07f2008-03-29 03:07:28 +00004863 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004864}
4865
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004866static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004867 .open = cgroupstats_open,
4868 .read = seq_read,
4869 .llseek = seq_lseek,
4870 .release = single_release,
4871};
4872
Paul Menageb4f48b62007-10-18 23:39:33 -07004873/**
4874 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004875 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004876 *
4877 * Description: A task inherits its parent's cgroup at fork().
4878 *
4879 * A pointer to the shared css_set was automatically copied in
4880 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004881 * it was not made under the protection of RCU or cgroup_mutex, so
4882 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4883 * have already changed current->cgroups, allowing the previously
4884 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004885 *
4886 * At the point that cgroup_fork() is called, 'current' is the parent
4887 * task, and the passed argument 'child' points to the child task.
4888 */
4889void cgroup_fork(struct task_struct *child)
4890{
Tejun Heo9bb71302012-10-18 17:52:07 -07004891 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004892 child->cgroups = current->cgroups;
4893 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004894 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004895 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004896}
4897
4898/**
Li Zefana043e3b2008-02-23 15:24:09 -08004899 * cgroup_post_fork - called on a new task after adding it to the task list
4900 * @child: the task in question
4901 *
Tejun Heo5edee612012-10-16 15:03:14 -07004902 * Adds the task to the list running through its css_set if necessary and
4903 * call the subsystem fork() callbacks. Has to be after the task is
4904 * visible on the task list in case we race with the first call to
4905 * cgroup_iter_start() - to guarantee that the new task ends up on its
4906 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004907 */
Paul Menage817929e2007-10-18 23:39:36 -07004908void cgroup_post_fork(struct task_struct *child)
4909{
Tejun Heo5edee612012-10-16 15:03:14 -07004910 int i;
4911
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004912 /*
4913 * use_task_css_set_links is set to 1 before we walk the tasklist
4914 * under the tasklist_lock and we read it here after we added the child
4915 * to the tasklist under the tasklist_lock as well. If the child wasn't
4916 * yet in the tasklist when we walked through it from
4917 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4918 * should be visible now due to the paired locking and barriers implied
4919 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4920 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4921 * lock on fork.
4922 */
Paul Menage817929e2007-10-18 23:39:36 -07004923 if (use_task_css_set_links) {
4924 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004925 task_lock(child);
4926 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004927 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004928 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004929 write_unlock(&css_set_lock);
4930 }
Tejun Heo5edee612012-10-16 15:03:14 -07004931
4932 /*
4933 * Call ss->fork(). This must happen after @child is linked on
4934 * css_set; otherwise, @child might change state between ->fork()
4935 * and addition to css_set.
4936 */
4937 if (need_forkexit_callback) {
4938 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4939 struct cgroup_subsys *ss = subsys[i];
4940
4941 /*
4942 * fork/exit callbacks are supported only for
4943 * builtin subsystems and we don't need further
4944 * synchronization as they never go away.
4945 */
4946 if (!ss || ss->module)
4947 continue;
4948
4949 if (ss->fork)
4950 ss->fork(child);
4951 }
4952 }
Paul Menage817929e2007-10-18 23:39:36 -07004953}
Tejun Heo5edee612012-10-16 15:03:14 -07004954
Paul Menage817929e2007-10-18 23:39:36 -07004955/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004956 * cgroup_exit - detach cgroup from exiting task
4957 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004958 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004959 *
4960 * Description: Detach cgroup from @tsk and release it.
4961 *
4962 * Note that cgroups marked notify_on_release force every task in
4963 * them to take the global cgroup_mutex mutex when exiting.
4964 * This could impact scaling on very large systems. Be reluctant to
4965 * use notify_on_release cgroups where very high task exit scaling
4966 * is required on large systems.
4967 *
4968 * the_top_cgroup_hack:
4969 *
4970 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4971 *
4972 * We call cgroup_exit() while the task is still competent to
4973 * handle notify_on_release(), then leave the task attached to the
4974 * root cgroup in each hierarchy for the remainder of its exit.
4975 *
4976 * To do this properly, we would increment the reference count on
4977 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4978 * code we would add a second cgroup function call, to drop that
4979 * reference. This would just create an unnecessary hot spot on
4980 * the top_cgroup reference count, to no avail.
4981 *
4982 * Normally, holding a reference to a cgroup without bumping its
4983 * count is unsafe. The cgroup could go away, or someone could
4984 * attach us to a different cgroup, decrementing the count on
4985 * the first cgroup that we never incremented. But in this case,
4986 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004987 * which wards off any cgroup_attach_task() attempts, or task is a failed
4988 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004989 */
4990void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4991{
Paul Menage817929e2007-10-18 23:39:36 -07004992 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004993 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004994
4995 /*
4996 * Unlink from the css_set task list if necessary.
4997 * Optimistically check cg_list before taking
4998 * css_set_lock
4999 */
5000 if (!list_empty(&tsk->cg_list)) {
5001 write_lock(&css_set_lock);
5002 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005003 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005004 write_unlock(&css_set_lock);
5005 }
5006
Paul Menageb4f48b62007-10-18 23:39:33 -07005007 /* Reassign the task to the init_css_set. */
5008 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07005009 cg = tsk->cgroups;
5010 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005011
5012 if (run_callbacks && need_forkexit_callback) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005013 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005014 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005015
5016 /* modular subsystems can't use callbacks */
5017 if (!ss || ss->module)
5018 continue;
5019
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005020 if (ss->exit) {
5021 struct cgroup *old_cgrp =
5022 rcu_dereference_raw(cg->subsys[i])->cgroup;
5023 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08005024 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005025 }
5026 }
5027 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005028 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005029
Li Zefanb5d646f2013-01-24 14:43:51 +08005030 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07005031}
Paul Menage697f4162007-10-18 23:39:34 -07005032
5033/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005034 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08005035 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005036 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08005037 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005038 * See if @cgrp is a descendant of @task's cgroup in the appropriate
5039 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07005040 *
5041 * If we are sending in dummytop, then presumably we are creating
5042 * the top cgroup in the subsystem.
5043 *
5044 * Called only by the ns (nsproxy) cgroup.
5045 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005046int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07005047{
5048 int ret;
5049 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07005050
Paul Menagebd89aab2007-10-18 23:40:44 -07005051 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07005052 return 1;
5053
Paul Menage7717f7b2009-09-23 15:56:22 -07005054 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005055 while (cgrp != target && cgrp!= cgrp->top_cgroup)
5056 cgrp = cgrp->parent;
5057 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07005058 return ret;
5059}
Paul Menage81a6a5c2007-10-18 23:39:38 -07005060
Paul Menagebd89aab2007-10-18 23:40:44 -07005061static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005062{
5063 /* All of these checks rely on RCU to keep the cgroup
5064 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07005065 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
5066 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07005067 /* Control Group is currently removeable. If it's not
5068 * already queued for a userspace notification, queue
5069 * it now */
5070 int need_schedule_work = 0;
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005071 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07005072 if (!cgroup_is_removed(cgrp) &&
5073 list_empty(&cgrp->release_list)) {
5074 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005075 need_schedule_work = 1;
5076 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005077 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005078 if (need_schedule_work)
5079 schedule_work(&release_agent_work);
5080 }
5081}
5082
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005083/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005084bool __css_tryget(struct cgroup_subsys_state *css)
5085{
Tejun Heoe9316082012-11-05 09:16:58 -08005086 while (true) {
5087 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005088
Tejun Heoe9316082012-11-05 09:16:58 -08005089 v = css_refcnt(css);
5090 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5091 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005092 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005093 else if (t < 0)
5094 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005095 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005096 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005097}
5098EXPORT_SYMBOL_GPL(__css_tryget);
5099
5100/* Caller must verify that the css is not for root cgroup */
5101void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005102{
Paul Menagebd89aab2007-10-18 23:40:44 -07005103 struct cgroup *cgrp = css->cgroup;
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005104 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005105
Paul Menage81a6a5c2007-10-18 23:39:38 -07005106 rcu_read_lock();
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005107 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
5108
5109 switch (v) {
Tejun Heo48ddbe12012-04-01 12:09:56 -07005110 case 1:
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005111 if (notify_on_release(cgrp)) {
5112 set_bit(CGRP_RELEASABLE, &cgrp->flags);
5113 check_for_release(cgrp);
5114 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07005115 break;
5116 case 0:
Tejun Heoed9577932012-11-05 09:16:58 -08005117 schedule_work(&css->dput_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005118 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005119 }
5120 rcu_read_unlock();
5121}
Ben Blum67523c42010-03-10 15:22:11 -08005122EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005123
5124/*
5125 * Notify userspace when a cgroup is released, by running the
5126 * configured release agent with the name of the cgroup (path
5127 * relative to the root of cgroup file system) as the argument.
5128 *
5129 * Most likely, this user command will try to rmdir this cgroup.
5130 *
5131 * This races with the possibility that some other task will be
5132 * attached to this cgroup before it is removed, or that some other
5133 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5134 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5135 * unused, and this cgroup will be reprieved from its death sentence,
5136 * to continue to serve a useful existence. Next time it's released,
5137 * we will get notified again, if it still has 'notify_on_release' set.
5138 *
5139 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5140 * means only wait until the task is successfully execve()'d. The
5141 * separate release agent task is forked by call_usermodehelper(),
5142 * then control in this thread returns here, without waiting for the
5143 * release agent task. We don't bother to wait because the caller of
5144 * this routine has no use for the exit status of the release agent
5145 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005146 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005147static void cgroup_release_agent(struct work_struct *work)
5148{
5149 BUG_ON(work != &release_agent_work);
5150 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005151 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005152 while (!list_empty(&release_list)) {
5153 char *argv[3], *envp[3];
5154 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005155 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005156 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005157 struct cgroup,
5158 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005159 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005160 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005161 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005162 if (!pathbuf)
5163 goto continue_free;
5164 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5165 goto continue_free;
5166 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5167 if (!agentbuf)
5168 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005169
5170 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005171 argv[i++] = agentbuf;
5172 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005173 argv[i] = NULL;
5174
5175 i = 0;
5176 /* minimal command environment */
5177 envp[i++] = "HOME=/";
5178 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5179 envp[i] = NULL;
5180
5181 /* Drop the lock while we invoke the usermode helper,
5182 * since the exec could involve hitting disk and hence
5183 * be a slow process */
5184 mutex_unlock(&cgroup_mutex);
5185 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005186 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005187 continue_free:
5188 kfree(pathbuf);
5189 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005190 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005191 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005192 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005193 mutex_unlock(&cgroup_mutex);
5194}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005195
5196static int __init cgroup_disable(char *str)
5197{
5198 int i;
5199 char *token;
5200
5201 while ((token = strsep(&str, ",")) != NULL) {
5202 if (!*token)
5203 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005204 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005205 struct cgroup_subsys *ss = subsys[i];
5206
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005207 /*
5208 * cgroup_disable, being at boot time, can't
5209 * know about module subsystems, so we don't
5210 * worry about them.
5211 */
5212 if (!ss || ss->module)
5213 continue;
5214
Paul Menage8bab8dd2008-04-04 14:29:57 -07005215 if (!strcmp(token, ss->name)) {
5216 ss->disabled = 1;
5217 printk(KERN_INFO "Disabling %s control group"
5218 " subsystem\n", ss->name);
5219 break;
5220 }
5221 }
5222 }
5223 return 1;
5224}
5225__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005226
5227/*
5228 * Functons for CSS ID.
5229 */
5230
5231/*
5232 *To get ID other than 0, this should be called when !cgroup_is_removed().
5233 */
5234unsigned short css_id(struct cgroup_subsys_state *css)
5235{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005236 struct css_id *cssid;
5237
5238 /*
5239 * This css_id() can return correct value when somone has refcnt
5240 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5241 * it's unchanged until freed.
5242 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005243 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005244
5245 if (cssid)
5246 return cssid->id;
5247 return 0;
5248}
Ben Blum67523c42010-03-10 15:22:11 -08005249EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005250
5251unsigned short css_depth(struct cgroup_subsys_state *css)
5252{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005253 struct css_id *cssid;
5254
Tejun Heo28b4c272012-04-01 12:09:56 -07005255 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005256
5257 if (cssid)
5258 return cssid->depth;
5259 return 0;
5260}
Ben Blum67523c42010-03-10 15:22:11 -08005261EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005262
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005263/**
5264 * css_is_ancestor - test "root" css is an ancestor of "child"
5265 * @child: the css to be tested.
5266 * @root: the css supporsed to be an ancestor of the child.
5267 *
5268 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005269 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005270 * But, considering usual usage, the csses should be valid objects after test.
5271 * Assuming that the caller will do some action to the child if this returns
5272 * returns true, the caller must take "child";s reference count.
5273 * If "child" is valid object and this returns true, "root" is valid, too.
5274 */
5275
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005276bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005277 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005278{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005279 struct css_id *child_id;
5280 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005281
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005282 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005283 if (!child_id)
5284 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005285 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005286 if (!root_id)
5287 return false;
5288 if (child_id->depth < root_id->depth)
5289 return false;
5290 if (child_id->stack[root_id->depth] != root_id->id)
5291 return false;
5292 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005293}
5294
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005295void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5296{
5297 struct css_id *id = css->id;
5298 /* When this is called before css_id initialization, id can be NULL */
5299 if (!id)
5300 return;
5301
5302 BUG_ON(!ss->use_id);
5303
5304 rcu_assign_pointer(id->css, NULL);
5305 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005306 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005307 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005308 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005309 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005310}
Ben Blum67523c42010-03-10 15:22:11 -08005311EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005312
5313/*
5314 * This is called by init or create(). Then, calls to this function are
5315 * always serialized (By cgroup_mutex() at create()).
5316 */
5317
5318static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5319{
5320 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005321 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005322
5323 BUG_ON(!ss->use_id);
5324
5325 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5326 newid = kzalloc(size, GFP_KERNEL);
5327 if (!newid)
5328 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005329
5330 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005331 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005332 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005333 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005334 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005335 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005336
5337 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005338 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005339 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005340
Tejun Heod228d9e2013-02-27 17:04:54 -08005341 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005342 newid->depth = depth;
5343 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005344err_out:
5345 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005346 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005347
5348}
5349
Ben Blume6a11052010-03-10 15:22:09 -08005350static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5351 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005352{
5353 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005354
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005355 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005356 idr_init(&ss->idr);
5357
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005358 newid = get_new_cssid(ss, 0);
5359 if (IS_ERR(newid))
5360 return PTR_ERR(newid);
5361
5362 newid->stack[0] = newid->id;
5363 newid->css = rootcss;
5364 rootcss->id = newid;
5365 return 0;
5366}
5367
5368static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5369 struct cgroup *child)
5370{
5371 int subsys_id, i, depth = 0;
5372 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005373 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005374
5375 subsys_id = ss->subsys_id;
5376 parent_css = parent->subsys[subsys_id];
5377 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005378 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005379 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005380
5381 child_id = get_new_cssid(ss, depth);
5382 if (IS_ERR(child_id))
5383 return PTR_ERR(child_id);
5384
5385 for (i = 0; i < depth; i++)
5386 child_id->stack[i] = parent_id->stack[i];
5387 child_id->stack[depth] = child_id->id;
5388 /*
5389 * child_id->css pointer will be set after this cgroup is available
5390 * see cgroup_populate_dir()
5391 */
5392 rcu_assign_pointer(child_css->id, child_id);
5393
5394 return 0;
5395}
5396
5397/**
5398 * css_lookup - lookup css by id
5399 * @ss: cgroup subsys to be looked into.
5400 * @id: the id
5401 *
5402 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5403 * NULL if not. Should be called under rcu_read_lock()
5404 */
5405struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5406{
5407 struct css_id *cssid = NULL;
5408
5409 BUG_ON(!ss->use_id);
5410 cssid = idr_find(&ss->idr, id);
5411
5412 if (unlikely(!cssid))
5413 return NULL;
5414
5415 return rcu_dereference(cssid->css);
5416}
Ben Blum67523c42010-03-10 15:22:11 -08005417EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005418
5419/**
5420 * css_get_next - lookup next cgroup under specified hierarchy.
5421 * @ss: pointer to subsystem
5422 * @id: current position of iteration.
5423 * @root: pointer to css. search tree under this.
5424 * @foundid: position of found object.
5425 *
5426 * Search next css under the specified hierarchy of rootid. Calling under
5427 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5428 */
5429struct cgroup_subsys_state *
5430css_get_next(struct cgroup_subsys *ss, int id,
5431 struct cgroup_subsys_state *root, int *foundid)
5432{
5433 struct cgroup_subsys_state *ret = NULL;
5434 struct css_id *tmp;
5435 int tmpid;
5436 int rootid = css_id(root);
5437 int depth = css_depth(root);
5438
5439 if (!rootid)
5440 return NULL;
5441
5442 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005443 WARN_ON_ONCE(!rcu_read_lock_held());
5444
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005445 /* fill start point for scan */
5446 tmpid = id;
5447 while (1) {
5448 /*
5449 * scan next entry from bitmap(tree), tmpid is updated after
5450 * idr_get_next().
5451 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005452 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005453 if (!tmp)
5454 break;
5455 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5456 ret = rcu_dereference(tmp->css);
5457 if (ret) {
5458 *foundid = tmpid;
5459 break;
5460 }
5461 }
5462 /* continue to scan from next id */
5463 tmpid = tmpid + 1;
5464 }
5465 return ret;
5466}
5467
Stephane Eraniane5d13672011-02-14 11:20:01 +02005468/*
5469 * get corresponding css from file open on cgroupfs directory
5470 */
5471struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5472{
5473 struct cgroup *cgrp;
5474 struct inode *inode;
5475 struct cgroup_subsys_state *css;
5476
Al Viro496ad9a2013-01-23 17:07:38 -05005477 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005478 /* check in cgroup filesystem dir */
5479 if (inode->i_op != &cgroup_dir_inode_operations)
5480 return ERR_PTR(-EBADF);
5481
5482 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5483 return ERR_PTR(-EINVAL);
5484
5485 /* get cgroup */
5486 cgrp = __d_cgrp(f->f_dentry);
5487 css = cgrp->subsys[id];
5488 return css ? css : ERR_PTR(-ENOENT);
5489}
5490
Paul Menagefe693432009-09-23 15:56:20 -07005491#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005492static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005493{
5494 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5495
5496 if (!css)
5497 return ERR_PTR(-ENOMEM);
5498
5499 return css;
5500}
5501
Tejun Heo92fb9742012-11-19 08:13:38 -08005502static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005503{
5504 kfree(cont->subsys[debug_subsys_id]);
5505}
5506
5507static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5508{
5509 return atomic_read(&cont->count);
5510}
5511
5512static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5513{
5514 return cgroup_task_count(cont);
5515}
5516
5517static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5518{
5519 return (u64)(unsigned long)current->cgroups;
5520}
5521
5522static u64 current_css_set_refcount_read(struct cgroup *cont,
5523 struct cftype *cft)
5524{
5525 u64 count;
5526
5527 rcu_read_lock();
5528 count = atomic_read(&current->cgroups->refcount);
5529 rcu_read_unlock();
5530 return count;
5531}
5532
Paul Menage7717f7b2009-09-23 15:56:22 -07005533static int current_css_set_cg_links_read(struct cgroup *cont,
5534 struct cftype *cft,
5535 struct seq_file *seq)
5536{
5537 struct cg_cgroup_link *link;
5538 struct css_set *cg;
5539
5540 read_lock(&css_set_lock);
5541 rcu_read_lock();
5542 cg = rcu_dereference(current->cgroups);
5543 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5544 struct cgroup *c = link->cgrp;
5545 const char *name;
5546
5547 if (c->dentry)
5548 name = c->dentry->d_name.name;
5549 else
5550 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005551 seq_printf(seq, "Root %d group %s\n",
5552 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005553 }
5554 rcu_read_unlock();
5555 read_unlock(&css_set_lock);
5556 return 0;
5557}
5558
5559#define MAX_TASKS_SHOWN_PER_CSS 25
5560static int cgroup_css_links_read(struct cgroup *cont,
5561 struct cftype *cft,
5562 struct seq_file *seq)
5563{
5564 struct cg_cgroup_link *link;
5565
5566 read_lock(&css_set_lock);
5567 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5568 struct css_set *cg = link->cg;
5569 struct task_struct *task;
5570 int count = 0;
5571 seq_printf(seq, "css_set %p\n", cg);
5572 list_for_each_entry(task, &cg->tasks, cg_list) {
5573 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5574 seq_puts(seq, " ...\n");
5575 break;
5576 } else {
5577 seq_printf(seq, " task %d\n",
5578 task_pid_vnr(task));
5579 }
5580 }
5581 }
5582 read_unlock(&css_set_lock);
5583 return 0;
5584}
5585
Paul Menagefe693432009-09-23 15:56:20 -07005586static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5587{
5588 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5589}
5590
5591static struct cftype debug_files[] = {
5592 {
5593 .name = "cgroup_refcount",
5594 .read_u64 = cgroup_refcount_read,
5595 },
5596 {
5597 .name = "taskcount",
5598 .read_u64 = debug_taskcount_read,
5599 },
5600
5601 {
5602 .name = "current_css_set",
5603 .read_u64 = current_css_set_read,
5604 },
5605
5606 {
5607 .name = "current_css_set_refcount",
5608 .read_u64 = current_css_set_refcount_read,
5609 },
5610
5611 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005612 .name = "current_css_set_cg_links",
5613 .read_seq_string = current_css_set_cg_links_read,
5614 },
5615
5616 {
5617 .name = "cgroup_css_links",
5618 .read_seq_string = cgroup_css_links_read,
5619 },
5620
5621 {
Paul Menagefe693432009-09-23 15:56:20 -07005622 .name = "releasable",
5623 .read_u64 = releasable_read,
5624 },
Paul Menagefe693432009-09-23 15:56:20 -07005625
Tejun Heo4baf6e32012-04-01 12:09:55 -07005626 { } /* terminate */
5627};
Paul Menagefe693432009-09-23 15:56:20 -07005628
5629struct cgroup_subsys debug_subsys = {
5630 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005631 .css_alloc = debug_css_alloc,
5632 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005633 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005634 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005635};
5636#endif /* CONFIG_CGROUP_DEBUG */