blob: 5d4c92ead691fa52e0e820ce1e50b36f5151cbc8 [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 Rozanskia1a71b42012-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 Rozanskia1a71b42012-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 hlist_node *node;
558 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800559 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700560
Ben Blumaae8aab2010-03-10 15:22:07 -0800561 /*
562 * Build the set of subsystem state objects that we want to see in the
563 * new css_set. while subsystems can change globally, the entries here
564 * won't change, so no need for locking.
565 */
Paul Menage817929e2007-10-18 23:39:36 -0700566 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400567 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700568 /* Subsystem is in this hierarchy. So we want
569 * the subsystem state from the new
570 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700571 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700572 } else {
573 /* Subsystem is not in this hierarchy, so we
574 * don't want to change the subsystem state */
575 template[i] = oldcg->subsys[i];
576 }
577 }
578
Li Zefan0ac801f2013-01-10 11:49:27 +0800579 key = css_set_hash(template);
580 hash_for_each_possible(css_set_table, cg, node, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700581 if (!compare_css_sets(cg, oldcg, cgrp, template))
582 continue;
583
584 /* This css_set matches what we need */
585 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700586 }
Paul Menage817929e2007-10-18 23:39:36 -0700587
588 /* No existing cgroup group matched */
589 return NULL;
590}
591
Paul Menage817929e2007-10-18 23:39:36 -0700592static void free_cg_links(struct list_head *tmp)
593{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700594 struct cg_cgroup_link *link;
595 struct cg_cgroup_link *saved_link;
596
597 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700598 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700599 kfree(link);
600 }
601}
602
603/*
Li Zefan36553432008-07-29 22:33:19 -0700604 * allocate_cg_links() allocates "count" cg_cgroup_link structures
605 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
606 * success or a negative error
607 */
608static int allocate_cg_links(int count, struct list_head *tmp)
609{
610 struct cg_cgroup_link *link;
611 int i;
612 INIT_LIST_HEAD(tmp);
613 for (i = 0; i < count; i++) {
614 link = kmalloc(sizeof(*link), GFP_KERNEL);
615 if (!link) {
616 free_cg_links(tmp);
617 return -ENOMEM;
618 }
619 list_add(&link->cgrp_link_list, tmp);
620 }
621 return 0;
622}
623
Li Zefanc12f65d2009-01-07 18:07:42 -0800624/**
625 * link_css_set - a helper function to link a css_set to a cgroup
626 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
627 * @cg: the css_set to be linked
628 * @cgrp: the destination cgroup
629 */
630static void link_css_set(struct list_head *tmp_cg_links,
631 struct css_set *cg, struct cgroup *cgrp)
632{
633 struct cg_cgroup_link *link;
634
635 BUG_ON(list_empty(tmp_cg_links));
636 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
637 cgrp_link_list);
638 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700639 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700640 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800641 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700642 /*
643 * Always add links to the tail of the list so that the list
644 * is sorted by order of hierarchy creation
645 */
646 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800647}
648
Li Zefan36553432008-07-29 22:33:19 -0700649/*
Paul Menage817929e2007-10-18 23:39:36 -0700650 * find_css_set() takes an existing cgroup group and a
651 * cgroup object, and returns a css_set object that's
652 * equivalent to the old group, but with the given cgroup
653 * substituted into the appropriate hierarchy. Must be called with
654 * cgroup_mutex held
655 */
Paul Menage817929e2007-10-18 23:39:36 -0700656static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700657 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700658{
659 struct css_set *res;
660 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700661
662 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700663
Paul Menage7717f7b2009-09-23 15:56:22 -0700664 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800665 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700666
Paul Menage817929e2007-10-18 23:39:36 -0700667 /* First see if we already have a cgroup group that matches
668 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700669 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700670 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700671 if (res)
672 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700673 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700674
675 if (res)
676 return res;
677
678 res = kmalloc(sizeof(*res), GFP_KERNEL);
679 if (!res)
680 return NULL;
681
682 /* Allocate all the cg_cgroup_link objects that we'll need */
683 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
684 kfree(res);
685 return NULL;
686 }
687
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700688 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700689 INIT_LIST_HEAD(&res->cg_links);
690 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700691 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700692
693 /* Copy the set of subsystem state objects generated in
694 * find_existing_css_set() */
695 memcpy(res->subsys, template, sizeof(res->subsys));
696
697 write_lock(&css_set_lock);
698 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700699 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
700 struct cgroup *c = link->cgrp;
701 if (c->root == cgrp->root)
702 c = cgrp;
703 link_css_set(&tmp_cg_links, res, c);
704 }
Paul Menage817929e2007-10-18 23:39:36 -0700705
706 BUG_ON(!list_empty(&tmp_cg_links));
707
Paul Menage817929e2007-10-18 23:39:36 -0700708 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700709
710 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800711 key = css_set_hash(res->subsys);
712 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700713
Paul Menage817929e2007-10-18 23:39:36 -0700714 write_unlock(&css_set_lock);
715
716 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700717}
718
Paul Menageddbcc7e2007-10-18 23:39:30 -0700719/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700720 * Return the cgroup for "task" from the given hierarchy. Must be
721 * called with cgroup_mutex held.
722 */
723static struct cgroup *task_cgroup_from_root(struct task_struct *task,
724 struct cgroupfs_root *root)
725{
726 struct css_set *css;
727 struct cgroup *res = NULL;
728
729 BUG_ON(!mutex_is_locked(&cgroup_mutex));
730 read_lock(&css_set_lock);
731 /*
732 * No need to lock the task - since we hold cgroup_mutex the
733 * task can't change groups, so the only thing that can happen
734 * is that it exits and its css is set back to init_css_set.
735 */
736 css = task->cgroups;
737 if (css == &init_css_set) {
738 res = &root->top_cgroup;
739 } else {
740 struct cg_cgroup_link *link;
741 list_for_each_entry(link, &css->cg_links, cg_link_list) {
742 struct cgroup *c = link->cgrp;
743 if (c->root == root) {
744 res = c;
745 break;
746 }
747 }
748 }
749 read_unlock(&css_set_lock);
750 BUG_ON(!res);
751 return res;
752}
753
754/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700755 * There is one global cgroup mutex. We also require taking
756 * task_lock() when dereferencing a task's cgroup subsys pointers.
757 * See "The task_lock() exception", at the end of this comment.
758 *
759 * A task must hold cgroup_mutex to modify cgroups.
760 *
761 * Any task can increment and decrement the count field without lock.
762 * So in general, code holding cgroup_mutex can't rely on the count
763 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800764 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700765 * means that no tasks are currently attached, therefore there is no
766 * way a task attached to that cgroup can fork (the other way to
767 * increment the count). So code holding cgroup_mutex can safely
768 * assume that if the count is zero, it will stay zero. Similarly, if
769 * a task holds cgroup_mutex on a cgroup with zero count, it
770 * knows that the cgroup won't be removed, as cgroup_rmdir()
771 * needs that mutex.
772 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
774 * (usually) take cgroup_mutex. These are the two most performance
775 * critical pieces of code here. The exception occurs on cgroup_exit(),
776 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
777 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800778 * to the release agent with the name of the cgroup (path relative to
779 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700780 *
781 * A cgroup can only be deleted if both its 'count' of using tasks
782 * is zero, and its list of 'children' cgroups is empty. Since all
783 * tasks in the system use _some_ cgroup, and since there is always at
784 * least one task in the system (init, pid == 1), therefore, top_cgroup
785 * always has either children cgroups and/or using tasks. So we don't
786 * need a special hack to ensure that top_cgroup cannot be deleted.
787 *
788 * The task_lock() exception
789 *
790 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800791 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800792 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793 * several performance critical places that need to reference
794 * task->cgroup without the expense of grabbing a system global
795 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800796 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700797 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
798 * the task_struct routinely used for such matters.
799 *
800 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800801 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700802 */
803
Paul Menageddbcc7e2007-10-18 23:39:30 -0700804/**
805 * cgroup_lock - lock out any changes to cgroup structures
806 *
807 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808void cgroup_lock(void)
809{
810 mutex_lock(&cgroup_mutex);
811}
Ben Blum67523c42010-03-10 15:22:11 -0800812EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700813
814/**
815 * cgroup_unlock - release lock on cgroup changes
816 *
817 * Undo the lock taken in a previous cgroup_lock() call.
818 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700819void cgroup_unlock(void)
820{
821 mutex_unlock(&cgroup_mutex);
822}
Ben Blum67523c42010-03-10 15:22:11 -0800823EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700824
825/*
826 * A couple of forward declarations required, due to cyclic reference loop:
827 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
828 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
829 * -> cgroup_mkdir.
830 */
831
Al Viro18bb1db2011-07-26 01:41:39 -0400832static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400833static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700834static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400835static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
836 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700837static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700838static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700839
840static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200841 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700842 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700843};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700844
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700845static int alloc_css_id(struct cgroup_subsys *ss,
846 struct cgroup *parent, struct cgroup *child);
847
Al Viroa5e7ed32011-07-26 01:55:55 -0400848static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700849{
850 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700851
852 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400853 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700854 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100855 inode->i_uid = current_fsuid();
856 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700857 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
858 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
859 }
860 return inode;
861}
862
Li Zefanbe445622013-01-24 14:31:42 +0800863static void cgroup_free_fn(struct work_struct *work)
864{
865 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
866 struct cgroup_subsys *ss;
867
868 mutex_lock(&cgroup_mutex);
869 /*
870 * Release the subsystem state objects.
871 */
872 for_each_subsys(cgrp->root, ss)
873 ss->css_free(cgrp);
874
875 cgrp->root->number_of_cgroups--;
876 mutex_unlock(&cgroup_mutex);
877
878 /*
879 * Drop the active superblock reference that we took when we
880 * created the cgroup
881 */
882 deactivate_super(cgrp->root->sb);
883
884 /*
885 * if we're getting rid of the cgroup, refcount should ensure
886 * that there are no pidlists left.
887 */
888 BUG_ON(!list_empty(&cgrp->pidlists));
889
890 simple_xattrs_free(&cgrp->xattrs);
891
892 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
893 kfree(cgrp);
894}
895
896static void cgroup_free_rcu(struct rcu_head *head)
897{
898 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
899
900 schedule_work(&cgrp->free_work);
901}
902
Paul Menageddbcc7e2007-10-18 23:39:30 -0700903static void cgroup_diput(struct dentry *dentry, struct inode *inode)
904{
905 /* is dentry a directory ? if so, kfree() associated cgroup */
906 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700907 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800908
Paul Menagebd89aab2007-10-18 23:40:44 -0700909 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800910 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700911 } else {
912 struct cfent *cfe = __d_cfe(dentry);
913 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400914 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700915
916 WARN_ONCE(!list_empty(&cfe->node) &&
917 cgrp != &cgrp->root->top_cgroup,
918 "cfe still linked for %s\n", cfe->type->name);
919 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400920 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700921 }
922 iput(inode);
923}
924
Al Viroc72a04e2011-01-14 05:31:45 +0000925static int cgroup_delete(const struct dentry *d)
926{
927 return 1;
928}
929
Paul Menageddbcc7e2007-10-18 23:39:30 -0700930static void remove_dir(struct dentry *d)
931{
932 struct dentry *parent = dget(d->d_parent);
933
934 d_delete(d);
935 simple_rmdir(parent->d_inode, d);
936 dput(parent);
937}
938
Li Zefan2739d3c2013-01-21 18:18:33 +0800939static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700940{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700941 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700942
Tejun Heo05ef1d72012-04-01 12:09:56 -0700943 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
944 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100945
Li Zefan2739d3c2013-01-21 18:18:33 +0800946 /*
947 * If we're doing cleanup due to failure of cgroup_create(),
948 * the corresponding @cfe may not exist.
949 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700950 list_for_each_entry(cfe, &cgrp->files, node) {
951 struct dentry *d = cfe->dentry;
952
953 if (cft && cfe->type != cft)
954 continue;
955
956 dget(d);
957 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700958 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700959 list_del_init(&cfe->node);
960 dput(d);
961
Li Zefan2739d3c2013-01-21 18:18:33 +0800962 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700963 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700964}
965
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400966/**
967 * cgroup_clear_directory - selective removal of base and subsystem files
968 * @dir: directory containing the files
969 * @base_files: true if the base files should be removed
970 * @subsys_mask: mask of the subsystem ids whose files should be removed
971 */
972static void cgroup_clear_directory(struct dentry *dir, bool base_files,
973 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700974{
975 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400976 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700977
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400978 for_each_subsys(cgrp->root, ss) {
979 struct cftype_set *set;
980 if (!test_bit(ss->subsys_id, &subsys_mask))
981 continue;
982 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800983 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400984 }
985 if (base_files) {
986 while (!list_empty(&cgrp->files))
987 cgroup_rm_file(cgrp, NULL);
988 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700989}
990
991/*
992 * NOTE : the dentry must have been dget()'ed
993 */
994static void cgroup_d_remove_dir(struct dentry *dentry)
995{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100996 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400997 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100998
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400999 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001001 parent = dentry->d_parent;
1002 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001003 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001004 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001005 spin_unlock(&dentry->d_lock);
1006 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007 remove_dir(dentry);
1008}
1009
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001010/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001011 * Call with cgroup_mutex held. Drops reference counts on modules, including
1012 * any duplicate ones that parse_cgroupfs_options took. If this function
1013 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001014 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001016 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017{
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001018 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001019 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001020 int i;
1021
Ben Blumaae8aab2010-03-10 15:22:07 -08001022 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001023 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001024
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001025 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1026 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001027 /* Check that any added subsystems are currently free */
1028 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001029 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001031 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001033 /*
1034 * Nobody should tell us to do a subsys that doesn't exist:
1035 * parse_cgroupfs_options should catch that case and refcounts
1036 * ensure that subsystems won't disappear once selected.
1037 */
1038 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 if (ss->root != &rootnode) {
1040 /* Subsystem isn't free */
1041 return -EBUSY;
1042 }
1043 }
1044
1045 /* Currently we don't handle adding/removing subsystems when
1046 * any child cgroups exist. This is theoretically supportable
1047 * but involves complex error handling, so it's being left until
1048 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001049 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001050 return -EBUSY;
1051
1052 /* Process each subsystem */
1053 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1054 struct cgroup_subsys *ss = subsys[i];
1055 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001056 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001058 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001059 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060 BUG_ON(!dummytop->subsys[i]);
1061 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001062 cgrp->subsys[i] = dummytop->subsys[i];
1063 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001064 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001065 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001067 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001068 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001069 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001071 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001072 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1073 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001075 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001077 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001078 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001079 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001080 /* subsystem is now free - drop reference on module */
1081 module_put(ss->module);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001082 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001084 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001085 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001086 /*
1087 * a refcount was taken, but we already had one, so
1088 * drop the extra reference.
1089 */
1090 module_put(ss->module);
1091#ifdef CONFIG_MODULE_UNLOAD
1092 BUG_ON(ss->module && !module_refcount(ss->module));
1093#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 } else {
1095 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001096 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097 }
1098 }
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001099 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100
1101 return 0;
1102}
1103
Al Viro34c80b12011-12-08 21:32:45 -05001104static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001105{
Al Viro34c80b12011-12-08 21:32:45 -05001106 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 struct cgroup_subsys *ss;
1108
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001109 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110 for_each_subsys(root, ss)
1111 seq_printf(seq, ",%s", ss->name);
1112 if (test_bit(ROOT_NOPREFIX, &root->flags))
1113 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001114 if (test_bit(ROOT_XATTR, &root->flags))
1115 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001116 if (strlen(root->release_agent_path))
1117 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001118 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001119 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001120 if (strlen(root->name))
1121 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001122 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001123 return 0;
1124}
1125
1126struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001127 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001129 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001130 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001131 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001132 /* User explicitly requested empty subsystem */
1133 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001134
1135 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001136
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137};
1138
Ben Blumaae8aab2010-03-10 15:22:07 -08001139/*
1140 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001141 * with cgroup_mutex held to protect the subsys[] array. This function takes
1142 * refcounts on subsystems to be used, unless it returns error, in which case
1143 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001144 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001145static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001146{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001147 char *token, *o = data;
1148 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001149 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001150 int i;
1151 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001152
Ben Blumaae8aab2010-03-10 15:22:07 -08001153 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1154
Li Zefanf9ab5b52009-06-17 16:26:33 -07001155#ifdef CONFIG_CPUSETS
1156 mask = ~(1UL << cpuset_subsys_id);
1157#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001158
Paul Menagec6d57f32009-09-23 15:56:19 -07001159 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001160
1161 while ((token = strsep(&o, ",")) != NULL) {
1162 if (!*token)
1163 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001164 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001165 /* Explicitly have no subsystems */
1166 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001167 continue;
1168 }
1169 if (!strcmp(token, "all")) {
1170 /* Mutually exclusive option 'all' + subsystem name */
1171 if (one_ss)
1172 return -EINVAL;
1173 all_ss = true;
1174 continue;
1175 }
1176 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001177 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001178 continue;
1179 }
1180 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001181 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001182 continue;
1183 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001184 if (!strcmp(token, "xattr")) {
1185 set_bit(ROOT_XATTR, &opts->flags);
1186 continue;
1187 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001188 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001189 /* Specifying two release agents is forbidden */
1190 if (opts->release_agent)
1191 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001192 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001193 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001194 if (!opts->release_agent)
1195 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001196 continue;
1197 }
1198 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001199 const char *name = token + 5;
1200 /* Can't specify an empty name */
1201 if (!strlen(name))
1202 return -EINVAL;
1203 /* Must match [\w.-]+ */
1204 for (i = 0; i < strlen(name); i++) {
1205 char c = name[i];
1206 if (isalnum(c))
1207 continue;
1208 if ((c == '.') || (c == '-') || (c == '_'))
1209 continue;
1210 return -EINVAL;
1211 }
1212 /* Specifying two names is forbidden */
1213 if (opts->name)
1214 return -EINVAL;
1215 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001216 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001217 GFP_KERNEL);
1218 if (!opts->name)
1219 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001220
1221 continue;
1222 }
1223
1224 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1225 struct cgroup_subsys *ss = subsys[i];
1226 if (ss == NULL)
1227 continue;
1228 if (strcmp(token, ss->name))
1229 continue;
1230 if (ss->disabled)
1231 continue;
1232
1233 /* Mutually exclusive option 'all' + subsystem name */
1234 if (all_ss)
1235 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001236 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001237 one_ss = true;
1238
1239 break;
1240 }
1241 if (i == CGROUP_SUBSYS_COUNT)
1242 return -ENOENT;
1243 }
1244
1245 /*
1246 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001247 * otherwise if 'none', 'name=' and a subsystem name options
1248 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001249 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001250 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001251 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1252 struct cgroup_subsys *ss = subsys[i];
1253 if (ss == NULL)
1254 continue;
1255 if (ss->disabled)
1256 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001257 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001258 }
1259 }
1260
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001261 /* Consistency checks */
1262
Li Zefanf9ab5b52009-06-17 16:26:33 -07001263 /*
1264 * Option noprefix was introduced just for backward compatibility
1265 * with the old cpuset, so we allow noprefix only if mounting just
1266 * the cpuset subsystem.
1267 */
1268 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001269 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001270 return -EINVAL;
1271
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001272
1273 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001274 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001275 return -EINVAL;
1276
1277 /*
1278 * We either have to specify by name or by subsystems. (So all
1279 * empty hierarchies must have a name).
1280 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001281 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001282 return -EINVAL;
1283
Ben Blumcf5d5942010-03-10 15:22:09 -08001284 /*
1285 * Grab references on all the modules we'll need, so the subsystems
1286 * don't dance around before rebind_subsystems attaches them. This may
1287 * take duplicate reference counts on a subsystem that's already used,
1288 * but rebind_subsystems handles this case.
1289 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001290 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001291 unsigned long bit = 1UL << i;
1292
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001293 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001294 continue;
1295 if (!try_module_get(subsys[i]->module)) {
1296 module_pin_failed = true;
1297 break;
1298 }
1299 }
1300 if (module_pin_failed) {
1301 /*
1302 * oops, one of the modules was going away. this means that we
1303 * raced with a module_delete call, and to the user this is
1304 * essentially a "subsystem doesn't exist" case.
1305 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001306 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001307 /* drop refcounts only on the ones we took */
1308 unsigned long bit = 1UL << i;
1309
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001310 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001311 continue;
1312 module_put(subsys[i]->module);
1313 }
1314 return -ENOENT;
1315 }
1316
Paul Menageddbcc7e2007-10-18 23:39:30 -07001317 return 0;
1318}
1319
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001320static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001321{
1322 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001323 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001324 unsigned long bit = 1UL << i;
1325
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001326 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001327 continue;
1328 module_put(subsys[i]->module);
1329 }
1330}
1331
Paul Menageddbcc7e2007-10-18 23:39:30 -07001332static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1333{
1334 int ret = 0;
1335 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001336 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001337 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001338 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001339
Paul Menagebd89aab2007-10-18 23:40:44 -07001340 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001341 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001342 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001343
1344 /* See what subsystems are wanted */
1345 ret = parse_cgroupfs_options(data, &opts);
1346 if (ret)
1347 goto out_unlock;
1348
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001349 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001350 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1351 task_tgid_nr(current), current->comm);
1352
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001353 added_mask = opts.subsys_mask & ~root->subsys_mask;
1354 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001355
Ben Blumcf5d5942010-03-10 15:22:09 -08001356 /* Don't allow flags or name to change at remount */
1357 if (opts.flags != root->flags ||
1358 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001359 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001360 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001361 goto out_unlock;
1362 }
1363
Gao feng7083d032012-12-03 09:28:18 +08001364 /*
1365 * Clear out the files of subsystems that should be removed, do
1366 * this before rebind_subsystems, since rebind_subsystems may
1367 * change this hierarchy's subsys_list.
1368 */
1369 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1370
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001371 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001372 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001373 /* rebind_subsystems failed, re-populate the removed files */
1374 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001375 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001376 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001377 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001379 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001380 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001381
Paul Menage81a6a5c2007-10-18 23:39:38 -07001382 if (opts.release_agent)
1383 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001384 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001385 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001386 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001387 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001388 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001389 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390 return ret;
1391}
1392
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001393static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394 .statfs = simple_statfs,
1395 .drop_inode = generic_delete_inode,
1396 .show_options = cgroup_show_options,
1397 .remount_fs = cgroup_remount,
1398};
1399
Paul Menagecc31edc2008-10-18 20:28:04 -07001400static void init_cgroup_housekeeping(struct cgroup *cgrp)
1401{
1402 INIT_LIST_HEAD(&cgrp->sibling);
1403 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001404 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001405 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001406 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001407 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001408 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001409 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001410 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001411 INIT_LIST_HEAD(&cgrp->event_list);
1412 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001413 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001414}
Paul Menagec6d57f32009-09-23 15:56:19 -07001415
Paul Menageddbcc7e2007-10-18 23:39:30 -07001416static void init_cgroup_root(struct cgroupfs_root *root)
1417{
Paul Menagebd89aab2007-10-18 23:40:44 -07001418 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001419
Paul Menageddbcc7e2007-10-18 23:39:30 -07001420 INIT_LIST_HEAD(&root->subsys_list);
1421 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001422 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001423 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001424 cgrp->root = root;
1425 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001426 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001427 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001428}
1429
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001430static bool init_root_id(struct cgroupfs_root *root)
1431{
1432 int ret = 0;
1433
1434 do {
1435 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1436 return false;
1437 spin_lock(&hierarchy_id_lock);
1438 /* Try to allocate the next unused ID */
1439 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1440 &root->hierarchy_id);
1441 if (ret == -ENOSPC)
1442 /* Try again starting from 0 */
1443 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1444 if (!ret) {
1445 next_hierarchy_id = root->hierarchy_id + 1;
1446 } else if (ret != -EAGAIN) {
1447 /* Can only get here if the 31-bit IDR is full ... */
1448 BUG_ON(ret);
1449 }
1450 spin_unlock(&hierarchy_id_lock);
1451 } while (ret);
1452 return true;
1453}
1454
Paul Menageddbcc7e2007-10-18 23:39:30 -07001455static int cgroup_test_super(struct super_block *sb, void *data)
1456{
Paul Menagec6d57f32009-09-23 15:56:19 -07001457 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001458 struct cgroupfs_root *root = sb->s_fs_info;
1459
Paul Menagec6d57f32009-09-23 15:56:19 -07001460 /* If we asked for a name then it must match */
1461 if (opts->name && strcmp(opts->name, root->name))
1462 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001463
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001464 /*
1465 * If we asked for subsystems (or explicitly for no
1466 * subsystems) then they must match
1467 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001468 if ((opts->subsys_mask || opts->none)
1469 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001470 return 0;
1471
1472 return 1;
1473}
1474
Paul Menagec6d57f32009-09-23 15:56:19 -07001475static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1476{
1477 struct cgroupfs_root *root;
1478
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001479 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001480 return NULL;
1481
1482 root = kzalloc(sizeof(*root), GFP_KERNEL);
1483 if (!root)
1484 return ERR_PTR(-ENOMEM);
1485
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001486 if (!init_root_id(root)) {
1487 kfree(root);
1488 return ERR_PTR(-ENOMEM);
1489 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001490 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001491
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001492 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001493 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001494 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001495 if (opts->release_agent)
1496 strcpy(root->release_agent_path, opts->release_agent);
1497 if (opts->name)
1498 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001499 if (opts->cpuset_clone_children)
1500 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001501 return root;
1502}
1503
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001504static void cgroup_drop_root(struct cgroupfs_root *root)
1505{
1506 if (!root)
1507 return;
1508
1509 BUG_ON(!root->hierarchy_id);
1510 spin_lock(&hierarchy_id_lock);
1511 ida_remove(&hierarchy_ida, root->hierarchy_id);
1512 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001513 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001514 kfree(root);
1515}
1516
Paul Menageddbcc7e2007-10-18 23:39:30 -07001517static int cgroup_set_super(struct super_block *sb, void *data)
1518{
1519 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001520 struct cgroup_sb_opts *opts = data;
1521
1522 /* If we don't have a new root, we can't set up a new sb */
1523 if (!opts->new_root)
1524 return -EINVAL;
1525
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001526 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001527
1528 ret = set_anon_super(sb, NULL);
1529 if (ret)
1530 return ret;
1531
Paul Menagec6d57f32009-09-23 15:56:19 -07001532 sb->s_fs_info = opts->new_root;
1533 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001534
1535 sb->s_blocksize = PAGE_CACHE_SIZE;
1536 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1537 sb->s_magic = CGROUP_SUPER_MAGIC;
1538 sb->s_op = &cgroup_ops;
1539
1540 return 0;
1541}
1542
1543static int cgroup_get_rootdir(struct super_block *sb)
1544{
Al Viro0df6a632010-12-21 13:29:29 -05001545 static const struct dentry_operations cgroup_dops = {
1546 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001547 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001548 };
1549
Paul Menageddbcc7e2007-10-18 23:39:30 -07001550 struct inode *inode =
1551 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552
1553 if (!inode)
1554 return -ENOMEM;
1555
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556 inode->i_fop = &simple_dir_operations;
1557 inode->i_op = &cgroup_dir_inode_operations;
1558 /* directories start off with i_nlink == 2 (for "." entry) */
1559 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001560 sb->s_root = d_make_root(inode);
1561 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001563 /* for everything else we want ->d_op set */
1564 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565 return 0;
1566}
1567
Al Virof7e83572010-07-26 13:23:11 +04001568static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001570 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571{
1572 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001573 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574 int ret = 0;
1575 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001576 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001577 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001578
1579 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001580 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001581 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001582 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001583 if (ret)
1584 goto out_err;
1585
1586 /*
1587 * Allocate a new cgroup root. We may not need it if we're
1588 * reusing an existing hierarchy.
1589 */
1590 new_root = cgroup_root_from_opts(&opts);
1591 if (IS_ERR(new_root)) {
1592 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001593 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001594 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001595 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001596
Paul Menagec6d57f32009-09-23 15:56:19 -07001597 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001598 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001599 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001600 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001601 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001602 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001603 }
1604
Paul Menagec6d57f32009-09-23 15:56:19 -07001605 root = sb->s_fs_info;
1606 BUG_ON(!root);
1607 if (root == opts.new_root) {
1608 /* We used the new root structure, so this is a new hierarchy */
1609 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001610 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001611 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001612 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001613 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001614 struct hlist_node *node;
1615 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001616
1617 BUG_ON(sb->s_root != NULL);
1618
1619 ret = cgroup_get_rootdir(sb);
1620 if (ret)
1621 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001622 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623
Paul Menage817929e2007-10-18 23:39:36 -07001624 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001625 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001626 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001627
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001628 /* Check for name clashes with existing mounts */
1629 ret = -EBUSY;
1630 if (strlen(root->name))
1631 for_each_active_root(existing_root)
1632 if (!strcmp(existing_root->name, root->name))
1633 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001634
Paul Menage817929e2007-10-18 23:39:36 -07001635 /*
1636 * We're accessing css_set_count without locking
1637 * css_set_lock here, but that's OK - it can only be
1638 * increased by someone holding cgroup_lock, and
1639 * that's us. The worst that can happen is that we
1640 * have some link structures left over
1641 */
1642 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001643 if (ret)
1644 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001645
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001646 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001648 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001649 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001650 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001651 /*
1652 * There must be no failure case after here, since rebinding
1653 * takes care of subsystems' refcounts, which are explicitly
1654 * dropped in the failure exit path.
1655 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001656
1657 /* EBUSY should be the only error here */
1658 BUG_ON(ret);
1659
1660 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001661 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662
Li Zefanc12f65d2009-01-07 18:07:42 -08001663 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664 root->top_cgroup.dentry = sb->s_root;
1665
Paul Menage817929e2007-10-18 23:39:36 -07001666 /* Link the top cgroup in this hierarchy into all
1667 * the css_set objects */
1668 write_lock(&css_set_lock);
Li Zefan0ac801f2013-01-10 11:49:27 +08001669 hash_for_each(css_set_table, i, node, cg, hlist)
1670 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001671 write_unlock(&css_set_lock);
1672
1673 free_cg_links(&tmp_cg_links);
1674
Li Zefanc12f65d2009-01-07 18:07:42 -08001675 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 BUG_ON(root->number_of_cgroups != 1);
1677
eparis@redhat2ce97382011-06-02 21:20:51 +10001678 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001679 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001680 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001681 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001683 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001684 } else {
1685 /*
1686 * We re-used an existing hierarchy - the new root (if
1687 * any) is not needed
1688 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001689 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001690 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001691 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001692 }
1693
Paul Menagec6d57f32009-09-23 15:56:19 -07001694 kfree(opts.release_agent);
1695 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001696 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001697
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001698 unlock_drop:
1699 mutex_unlock(&cgroup_root_mutex);
1700 mutex_unlock(&cgroup_mutex);
1701 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001702 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001703 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001704 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001705 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001706 out_err:
1707 kfree(opts.release_agent);
1708 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001709 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710}
1711
1712static void cgroup_kill_sb(struct super_block *sb) {
1713 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001714 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001715 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001716 struct cg_cgroup_link *link;
1717 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718
1719 BUG_ON(!root);
1720
1721 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001722 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723
1724 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001725 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001726
1727 /* Rebind all subsystems back to the default hierarchy */
1728 ret = rebind_subsystems(root, 0);
1729 /* Shouldn't be able to fail ... */
1730 BUG_ON(ret);
1731
Paul Menage817929e2007-10-18 23:39:36 -07001732 /*
1733 * Release all the links from css_sets to this hierarchy's
1734 * root cgroup
1735 */
1736 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001737
1738 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1739 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001740 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001741 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001742 kfree(link);
1743 }
1744 write_unlock(&css_set_lock);
1745
Paul Menage839ec542009-01-29 14:25:22 -08001746 if (!list_empty(&root->root_list)) {
1747 list_del(&root->root_list);
1748 root_count--;
1749 }
Li Zefane5f6a862009-01-07 18:07:41 -08001750
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001751 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001752 mutex_unlock(&cgroup_mutex);
1753
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001754 simple_xattrs_free(&cgrp->xattrs);
1755
Paul Menageddbcc7e2007-10-18 23:39:30 -07001756 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001757 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758}
1759
1760static struct file_system_type cgroup_fs_type = {
1761 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001762 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001763 .kill_sb = cgroup_kill_sb,
1764};
1765
Greg KH676db4a2010-08-05 13:53:35 -07001766static struct kobject *cgroup_kobj;
1767
Li Zefana043e3b2008-02-23 15:24:09 -08001768/**
1769 * cgroup_path - generate the path of a cgroup
1770 * @cgrp: the cgroup in question
1771 * @buf: the buffer to write the path into
1772 * @buflen: the length of the buffer
1773 *
Paul Menagea47295e2009-01-07 18:07:44 -08001774 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1775 * reference. Writes path of cgroup into buf. Returns 0 on success,
1776 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001778int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779{
Tejun Heofebfcef2012-11-19 08:13:36 -08001780 struct dentry *dentry = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001782
1783 rcu_lockdep_assert(rcu_read_lock_held() || cgroup_lock_is_held(),
1784 "cgroup_path() called without proper locking");
Paul Menageddbcc7e2007-10-18 23:39:30 -07001785
Li Zefanfe1c06c2013-01-24 14:30:22 +08001786 if (cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787 /*
1788 * Inactive subsystems have no dentry for their root
1789 * cgroup
1790 */
1791 strcpy(buf, "/");
1792 return 0;
1793 }
1794
Tao Ma316eb662012-11-08 21:36:38 +08001795 start = buf + buflen - 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001796
Tao Ma316eb662012-11-08 21:36:38 +08001797 *start = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07001798 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001799 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001800
Paul Menageddbcc7e2007-10-18 23:39:30 -07001801 if ((start -= len) < buf)
1802 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001803 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001804 cgrp = cgrp->parent;
1805 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001806 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001807
Tejun Heofebfcef2012-11-19 08:13:36 -08001808 dentry = cgrp->dentry;
Paul Menagebd89aab2007-10-18 23:40:44 -07001809 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810 continue;
1811 if (--start < buf)
1812 return -ENAMETOOLONG;
1813 *start = '/';
1814 }
1815 memmove(buf, start, buf + buflen - start);
1816 return 0;
1817}
Ben Blum67523c42010-03-10 15:22:11 -08001818EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001819
Ben Blum74a11662011-05-26 16:25:20 -07001820/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001821 * Control Group taskset
1822 */
Tejun Heo134d3372011-12-12 18:12:21 -08001823struct task_and_cgroup {
1824 struct task_struct *task;
1825 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001826 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001827};
1828
Tejun Heo2f7ee562011-12-12 18:12:21 -08001829struct cgroup_taskset {
1830 struct task_and_cgroup single;
1831 struct flex_array *tc_array;
1832 int tc_array_len;
1833 int idx;
1834 struct cgroup *cur_cgrp;
1835};
1836
1837/**
1838 * cgroup_taskset_first - reset taskset and return the first task
1839 * @tset: taskset of interest
1840 *
1841 * @tset iteration is initialized and the first task is returned.
1842 */
1843struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1844{
1845 if (tset->tc_array) {
1846 tset->idx = 0;
1847 return cgroup_taskset_next(tset);
1848 } else {
1849 tset->cur_cgrp = tset->single.cgrp;
1850 return tset->single.task;
1851 }
1852}
1853EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1854
1855/**
1856 * cgroup_taskset_next - iterate to the next task in taskset
1857 * @tset: taskset of interest
1858 *
1859 * Return the next task in @tset. Iteration must have been initialized
1860 * with cgroup_taskset_first().
1861 */
1862struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1863{
1864 struct task_and_cgroup *tc;
1865
1866 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1867 return NULL;
1868
1869 tc = flex_array_get(tset->tc_array, tset->idx++);
1870 tset->cur_cgrp = tc->cgrp;
1871 return tc->task;
1872}
1873EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1874
1875/**
1876 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1877 * @tset: taskset of interest
1878 *
1879 * Return the cgroup for the current (last returned) task of @tset. This
1880 * function must be preceded by either cgroup_taskset_first() or
1881 * cgroup_taskset_next().
1882 */
1883struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1884{
1885 return tset->cur_cgrp;
1886}
1887EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1888
1889/**
1890 * cgroup_taskset_size - return the number of tasks in taskset
1891 * @tset: taskset of interest
1892 */
1893int cgroup_taskset_size(struct cgroup_taskset *tset)
1894{
1895 return tset->tc_array ? tset->tc_array_len : 1;
1896}
1897EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1898
1899
Ben Blum74a11662011-05-26 16:25:20 -07001900/*
1901 * cgroup_task_migrate - move a task from one cgroup to another.
1902 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001903 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001904 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001905static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1906 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001907{
1908 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001909
1910 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001911 * We are synchronized through threadgroup_lock() against PF_EXITING
1912 * setting such that we can't race against cgroup_exit() changing the
1913 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001914 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001915 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001916 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001917
Ben Blum74a11662011-05-26 16:25:20 -07001918 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001919 rcu_assign_pointer(tsk->cgroups, newcg);
1920 task_unlock(tsk);
1921
1922 /* Update the css_set linked lists if we're using them */
1923 write_lock(&css_set_lock);
1924 if (!list_empty(&tsk->cg_list))
1925 list_move(&tsk->cg_list, &newcg->tasks);
1926 write_unlock(&css_set_lock);
1927
1928 /*
1929 * We just gained a reference on oldcg by taking it from the task. As
1930 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1931 * it here; it will be freed under RCU.
1932 */
Ben Blum74a11662011-05-26 16:25:20 -07001933 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001934 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001935}
1936
Li Zefana043e3b2008-02-23 15:24:09 -08001937/**
1938 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1939 * @cgrp: the cgroup the task is attaching to
1940 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001941 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001942 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1943 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001944 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001945int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001946{
Tejun Heo8f121912012-03-29 22:03:33 -07001947 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001948 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001949 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001950 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001951 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001952 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001953
Tejun Heocd3d0952011-12-12 18:12:21 -08001954 /* @tsk either already exited or can't exit until the end */
1955 if (tsk->flags & PF_EXITING)
1956 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001957
1958 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001959 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001960 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001961 return 0;
1962
Tejun Heo2f7ee562011-12-12 18:12:21 -08001963 tset.single.task = tsk;
1964 tset.single.cgrp = oldcgrp;
1965
Paul Menagebbcb81d2007-10-18 23:39:32 -07001966 for_each_subsys(root, ss) {
1967 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08001968 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001969 if (retval) {
1970 /*
1971 * Remember on which subsystem the can_attach()
1972 * failed, so that we only call cancel_attach()
1973 * against the subsystems whose can_attach()
1974 * succeeded. (See below)
1975 */
1976 failed_ss = ss;
1977 goto out;
1978 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001979 }
1980 }
1981
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001982 newcg = find_css_set(tsk->cgroups, cgrp);
1983 if (!newcg) {
1984 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001985 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001986 }
1987
1988 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001989
Paul Menagebbcb81d2007-10-18 23:39:32 -07001990 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001991 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001992 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001993 }
Ben Blum74a11662011-05-26 16:25:20 -07001994
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001995out:
1996 if (retval) {
1997 for_each_subsys(root, ss) {
1998 if (ss == failed_ss)
1999 /*
2000 * This subsystem was the one that failed the
2001 * can_attach() check earlier, so we don't need
2002 * to call cancel_attach() against it or any
2003 * remaining subsystems.
2004 */
2005 break;
2006 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002007 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08002008 }
2009 }
2010 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002011}
2012
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002013/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002014 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2015 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002016 * @tsk: the task to be attached
2017 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002018int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002019{
2020 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002021 int retval = 0;
2022
2023 cgroup_lock();
2024 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002025 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2026
2027 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002028 if (retval)
2029 break;
2030 }
2031 cgroup_unlock();
2032
2033 return retval;
2034}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002035EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002036
Ben Blum74a11662011-05-26 16:25:20 -07002037/**
2038 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2039 * @cgrp: the cgroup to attach to
2040 * @leader: the threadgroup leader task_struct of the group to be attached
2041 *
Tejun Heo257058a2011-12-12 18:12:21 -08002042 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2043 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002044 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002045static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002046{
2047 int retval, i, group_size;
2048 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002049 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002050 struct cgroupfs_root *root = cgrp->root;
2051 /* threadgroup list cursor and array */
2052 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002053 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002054 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002055 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002056
2057 /*
2058 * step 0: in order to do expensive, possibly blocking operations for
2059 * every thread, we cannot iterate the thread group list, since it needs
2060 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002061 * group - group_rwsem prevents new threads from appearing, and if
2062 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002063 */
2064 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002065 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002066 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002067 if (!group)
2068 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002069 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2070 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2071 if (retval)
2072 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002073
Ben Blum74a11662011-05-26 16:25:20 -07002074 tsk = leader;
2075 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002076 /*
2077 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2078 * already PF_EXITING could be freed from underneath us unless we
2079 * take an rcu_read_lock.
2080 */
2081 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002082 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002083 struct task_and_cgroup ent;
2084
Tejun Heocd3d0952011-12-12 18:12:21 -08002085 /* @tsk either already exited or can't exit until the end */
2086 if (tsk->flags & PF_EXITING)
2087 continue;
2088
Ben Blum74a11662011-05-26 16:25:20 -07002089 /* as per above, nr_threads may decrease, but not increase. */
2090 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002091 ent.task = tsk;
2092 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002093 /* nothing to do if this task is already in the cgroup */
2094 if (ent.cgrp == cgrp)
2095 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002096 /*
2097 * saying GFP_ATOMIC has no effect here because we did prealloc
2098 * earlier, but it's good form to communicate our expectations.
2099 */
Tejun Heo134d3372011-12-12 18:12:21 -08002100 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002101 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002102 i++;
2103 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002104 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002105 /* remember the number of threads in the array for later. */
2106 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002107 tset.tc_array = group;
2108 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002109
Tejun Heo134d3372011-12-12 18:12:21 -08002110 /* methods shouldn't be called if no task is actually migrating */
2111 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002112 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002113 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002114
Ben Blum74a11662011-05-26 16:25:20 -07002115 /*
2116 * step 1: check that we can legitimately attach to the cgroup.
2117 */
2118 for_each_subsys(root, ss) {
2119 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002120 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002121 if (retval) {
2122 failed_ss = ss;
2123 goto out_cancel_attach;
2124 }
2125 }
Ben Blum74a11662011-05-26 16:25:20 -07002126 }
2127
2128 /*
2129 * step 2: make sure css_sets exist for all threads to be migrated.
2130 * we use find_css_set, which allocates a new one if necessary.
2131 */
Ben Blum74a11662011-05-26 16:25:20 -07002132 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002133 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002134 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2135 if (!tc->cg) {
2136 retval = -ENOMEM;
2137 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002138 }
2139 }
2140
2141 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002142 * step 3: now that we're guaranteed success wrt the css_sets,
2143 * proceed to move all tasks to the new cgroup. There are no
2144 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002145 */
Ben Blum74a11662011-05-26 16:25:20 -07002146 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002147 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002148 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002149 }
2150 /* nothing is sensitive to fork() after this point. */
2151
2152 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002153 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002154 */
2155 for_each_subsys(root, ss) {
2156 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002157 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002158 }
2159
2160 /*
2161 * step 5: success! and cleanup
2162 */
Ben Blum74a11662011-05-26 16:25:20 -07002163 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002164out_put_css_set_refs:
2165 if (retval) {
2166 for (i = 0; i < group_size; i++) {
2167 tc = flex_array_get(group, i);
2168 if (!tc->cg)
2169 break;
2170 put_css_set(tc->cg);
2171 }
Ben Blum74a11662011-05-26 16:25:20 -07002172 }
2173out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002174 if (retval) {
2175 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002176 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002177 break;
Ben Blum74a11662011-05-26 16:25:20 -07002178 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002179 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002180 }
2181 }
Ben Blum74a11662011-05-26 16:25:20 -07002182out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002183 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002184 return retval;
2185}
2186
2187/*
2188 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002189 * function to attach either it or all tasks in its threadgroup. Will lock
2190 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002191 */
2192static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002193{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002194 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002195 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002196 int ret;
2197
Ben Blum74a11662011-05-26 16:25:20 -07002198 if (!cgroup_lock_live_group(cgrp))
2199 return -ENODEV;
2200
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002201retry_find_task:
2202 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002203 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002204 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002205 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002206 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002207 ret= -ESRCH;
2208 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002209 }
Ben Blum74a11662011-05-26 16:25:20 -07002210 /*
2211 * even if we're attaching all tasks in the thread group, we
2212 * only need to check permissions on one of them.
2213 */
David Howellsc69e8d92008-11-14 10:39:19 +11002214 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002215 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2216 !uid_eq(cred->euid, tcred->uid) &&
2217 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002218 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002219 ret = -EACCES;
2220 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002221 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002222 } else
2223 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002224
2225 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002226 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002227
2228 /*
2229 * Workqueue threads may acquire PF_THREAD_BOUND and become
2230 * trapped in a cpuset, or RT worker may be born in a cgroup
2231 * with no rt_runtime allocated. Just say no.
2232 */
2233 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2234 ret = -EINVAL;
2235 rcu_read_unlock();
2236 goto out_unlock_cgroup;
2237 }
2238
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002239 get_task_struct(tsk);
2240 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002241
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002242 threadgroup_lock(tsk);
2243 if (threadgroup) {
2244 if (!thread_group_leader(tsk)) {
2245 /*
2246 * a race with de_thread from another thread's exec()
2247 * may strip us of our leadership, if this happens,
2248 * there is no choice but to throw this task away and
2249 * try again; this is
2250 * "double-double-toil-and-trouble-check locking".
2251 */
2252 threadgroup_unlock(tsk);
2253 put_task_struct(tsk);
2254 goto retry_find_task;
2255 }
2256 ret = cgroup_attach_proc(cgrp, tsk);
2257 } else
2258 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002259 threadgroup_unlock(tsk);
2260
Paul Menagebbcb81d2007-10-18 23:39:32 -07002261 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002262out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002263 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002264 return ret;
2265}
2266
Paul Menageaf351022008-07-25 01:47:01 -07002267static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2268{
Ben Blum74a11662011-05-26 16:25:20 -07002269 return attach_task_by_pid(cgrp, pid, false);
2270}
2271
2272static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2273{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002274 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002275}
2276
Paul Menagee788e062008-07-25 01:46:59 -07002277/**
2278 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2279 * @cgrp: the cgroup to be checked for liveness
2280 *
Paul Menage84eea842008-07-25 01:47:00 -07002281 * On success, returns true; the lock should be later released with
2282 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002283 */
Paul Menage84eea842008-07-25 01:47:00 -07002284bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002285{
2286 mutex_lock(&cgroup_mutex);
2287 if (cgroup_is_removed(cgrp)) {
2288 mutex_unlock(&cgroup_mutex);
2289 return false;
2290 }
2291 return true;
2292}
Ben Blum67523c42010-03-10 15:22:11 -08002293EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002294
2295static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2296 const char *buffer)
2297{
2298 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002299 if (strlen(buffer) >= PATH_MAX)
2300 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002301 if (!cgroup_lock_live_group(cgrp))
2302 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002303 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002304 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002305 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002306 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002307 return 0;
2308}
2309
2310static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2311 struct seq_file *seq)
2312{
2313 if (!cgroup_lock_live_group(cgrp))
2314 return -ENODEV;
2315 seq_puts(seq, cgrp->root->release_agent_path);
2316 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002317 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002318 return 0;
2319}
2320
Paul Menage84eea842008-07-25 01:47:00 -07002321/* A buffer size big enough for numbers or short strings */
2322#define CGROUP_LOCAL_BUFFER_SIZE 64
2323
Paul Menagee73d2c62008-04-29 01:00:06 -07002324static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002325 struct file *file,
2326 const char __user *userbuf,
2327 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002328{
Paul Menage84eea842008-07-25 01:47:00 -07002329 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002330 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002331 char *end;
2332
2333 if (!nbytes)
2334 return -EINVAL;
2335 if (nbytes >= sizeof(buffer))
2336 return -E2BIG;
2337 if (copy_from_user(buffer, userbuf, nbytes))
2338 return -EFAULT;
2339
2340 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002341 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002342 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002343 if (*end)
2344 return -EINVAL;
2345 retval = cft->write_u64(cgrp, cft, val);
2346 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002347 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002348 if (*end)
2349 return -EINVAL;
2350 retval = cft->write_s64(cgrp, cft, val);
2351 }
Paul Menage355e0c42007-10-18 23:39:33 -07002352 if (!retval)
2353 retval = nbytes;
2354 return retval;
2355}
2356
Paul Menagedb3b1492008-07-25 01:46:58 -07002357static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2358 struct file *file,
2359 const char __user *userbuf,
2360 size_t nbytes, loff_t *unused_ppos)
2361{
Paul Menage84eea842008-07-25 01:47:00 -07002362 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002363 int retval = 0;
2364 size_t max_bytes = cft->max_write_len;
2365 char *buffer = local_buffer;
2366
2367 if (!max_bytes)
2368 max_bytes = sizeof(local_buffer) - 1;
2369 if (nbytes >= max_bytes)
2370 return -E2BIG;
2371 /* Allocate a dynamic buffer if we need one */
2372 if (nbytes >= sizeof(local_buffer)) {
2373 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2374 if (buffer == NULL)
2375 return -ENOMEM;
2376 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002377 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2378 retval = -EFAULT;
2379 goto out;
2380 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002381
2382 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002383 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002384 if (!retval)
2385 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002386out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002387 if (buffer != local_buffer)
2388 kfree(buffer);
2389 return retval;
2390}
2391
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2393 size_t nbytes, loff_t *ppos)
2394{
2395 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002396 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002397
Li Zefan75139b82009-01-07 18:07:33 -08002398 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002399 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002400 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002401 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002402 if (cft->write_u64 || cft->write_s64)
2403 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002404 if (cft->write_string)
2405 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002406 if (cft->trigger) {
2407 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2408 return ret ? ret : nbytes;
2409 }
Paul Menage355e0c42007-10-18 23:39:33 -07002410 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002411}
2412
Paul Menagef4c753b2008-04-29 00:59:56 -07002413static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2414 struct file *file,
2415 char __user *buf, size_t nbytes,
2416 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002417{
Paul Menage84eea842008-07-25 01:47:00 -07002418 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002419 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002420 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2421
2422 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2423}
2424
Paul Menagee73d2c62008-04-29 01:00:06 -07002425static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2426 struct file *file,
2427 char __user *buf, size_t nbytes,
2428 loff_t *ppos)
2429{
Paul Menage84eea842008-07-25 01:47:00 -07002430 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002431 s64 val = cft->read_s64(cgrp, cft);
2432 int len = sprintf(tmp, "%lld\n", (long long) val);
2433
2434 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2435}
2436
Paul Menageddbcc7e2007-10-18 23:39:30 -07002437static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2438 size_t nbytes, loff_t *ppos)
2439{
2440 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002441 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002442
Li Zefan75139b82009-01-07 18:07:33 -08002443 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002444 return -ENODEV;
2445
2446 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002447 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002448 if (cft->read_u64)
2449 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002450 if (cft->read_s64)
2451 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002452 return -EINVAL;
2453}
2454
Paul Menage91796562008-04-29 01:00:01 -07002455/*
2456 * seqfile ops/methods for returning structured data. Currently just
2457 * supports string->u64 maps, but can be extended in future.
2458 */
2459
2460struct cgroup_seqfile_state {
2461 struct cftype *cft;
2462 struct cgroup *cgroup;
2463};
2464
2465static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2466{
2467 struct seq_file *sf = cb->state;
2468 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2469}
2470
2471static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2472{
2473 struct cgroup_seqfile_state *state = m->private;
2474 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002475 if (cft->read_map) {
2476 struct cgroup_map_cb cb = {
2477 .fill = cgroup_map_add,
2478 .state = m,
2479 };
2480 return cft->read_map(state->cgroup, cft, &cb);
2481 }
2482 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002483}
2484
Adrian Bunk96930a62008-07-25 19:46:21 -07002485static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002486{
2487 struct seq_file *seq = file->private_data;
2488 kfree(seq->private);
2489 return single_release(inode, file);
2490}
2491
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002492static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002493 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002494 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002495 .llseek = seq_lseek,
2496 .release = cgroup_seqfile_release,
2497};
2498
Paul Menageddbcc7e2007-10-18 23:39:30 -07002499static int cgroup_file_open(struct inode *inode, struct file *file)
2500{
2501 int err;
2502 struct cftype *cft;
2503
2504 err = generic_file_open(inode, file);
2505 if (err)
2506 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002507 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002508
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002509 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002510 struct cgroup_seqfile_state *state =
2511 kzalloc(sizeof(*state), GFP_USER);
2512 if (!state)
2513 return -ENOMEM;
2514 state->cft = cft;
2515 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2516 file->f_op = &cgroup_seqfile_operations;
2517 err = single_open(file, cgroup_seqfile_show, state);
2518 if (err < 0)
2519 kfree(state);
2520 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002521 err = cft->open(inode, file);
2522 else
2523 err = 0;
2524
2525 return err;
2526}
2527
2528static int cgroup_file_release(struct inode *inode, struct file *file)
2529{
2530 struct cftype *cft = __d_cft(file->f_dentry);
2531 if (cft->release)
2532 return cft->release(inode, file);
2533 return 0;
2534}
2535
2536/*
2537 * cgroup_rename - Only allow simple rename of directories in place.
2538 */
2539static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2540 struct inode *new_dir, struct dentry *new_dentry)
2541{
2542 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2543 return -ENOTDIR;
2544 if (new_dentry->d_inode)
2545 return -EEXIST;
2546 if (old_dir != new_dir)
2547 return -EIO;
2548 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2549}
2550
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002551static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2552{
2553 if (S_ISDIR(dentry->d_inode->i_mode))
2554 return &__d_cgrp(dentry)->xattrs;
2555 else
2556 return &__d_cft(dentry)->xattrs;
2557}
2558
2559static inline int xattr_enabled(struct dentry *dentry)
2560{
2561 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2562 return test_bit(ROOT_XATTR, &root->flags);
2563}
2564
2565static bool is_valid_xattr(const char *name)
2566{
2567 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2568 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2569 return true;
2570 return false;
2571}
2572
2573static int cgroup_setxattr(struct dentry *dentry, const char *name,
2574 const void *val, size_t size, int flags)
2575{
2576 if (!xattr_enabled(dentry))
2577 return -EOPNOTSUPP;
2578 if (!is_valid_xattr(name))
2579 return -EINVAL;
2580 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2581}
2582
2583static int cgroup_removexattr(struct dentry *dentry, const char *name)
2584{
2585 if (!xattr_enabled(dentry))
2586 return -EOPNOTSUPP;
2587 if (!is_valid_xattr(name))
2588 return -EINVAL;
2589 return simple_xattr_remove(__d_xattrs(dentry), name);
2590}
2591
2592static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2593 void *buf, size_t size)
2594{
2595 if (!xattr_enabled(dentry))
2596 return -EOPNOTSUPP;
2597 if (!is_valid_xattr(name))
2598 return -EINVAL;
2599 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2600}
2601
2602static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2603{
2604 if (!xattr_enabled(dentry))
2605 return -EOPNOTSUPP;
2606 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2607}
2608
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002609static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002610 .read = cgroup_file_read,
2611 .write = cgroup_file_write,
2612 .llseek = generic_file_llseek,
2613 .open = cgroup_file_open,
2614 .release = cgroup_file_release,
2615};
2616
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002617static const struct inode_operations cgroup_file_inode_operations = {
2618 .setxattr = cgroup_setxattr,
2619 .getxattr = cgroup_getxattr,
2620 .listxattr = cgroup_listxattr,
2621 .removexattr = cgroup_removexattr,
2622};
2623
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002624static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002625 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002626 .mkdir = cgroup_mkdir,
2627 .rmdir = cgroup_rmdir,
2628 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002629 .setxattr = cgroup_setxattr,
2630 .getxattr = cgroup_getxattr,
2631 .listxattr = cgroup_listxattr,
2632 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002633};
2634
Al Viro00cd8dd2012-06-10 17:13:09 -04002635static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002636{
2637 if (dentry->d_name.len > NAME_MAX)
2638 return ERR_PTR(-ENAMETOOLONG);
2639 d_add(dentry, NULL);
2640 return NULL;
2641}
2642
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002643/*
2644 * Check if a file is a control file
2645 */
2646static inline struct cftype *__file_cft(struct file *file)
2647{
2648 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2649 return ERR_PTR(-EINVAL);
2650 return __d_cft(file->f_dentry);
2651}
2652
Al Viroa5e7ed32011-07-26 01:55:55 -04002653static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002654 struct super_block *sb)
2655{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002656 struct inode *inode;
2657
2658 if (!dentry)
2659 return -ENOENT;
2660 if (dentry->d_inode)
2661 return -EEXIST;
2662
2663 inode = cgroup_new_inode(mode, sb);
2664 if (!inode)
2665 return -ENOMEM;
2666
2667 if (S_ISDIR(mode)) {
2668 inode->i_op = &cgroup_dir_inode_operations;
2669 inode->i_fop = &simple_dir_operations;
2670
2671 /* start off with i_nlink == 2 (for "." entry) */
2672 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002673 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674
Tejun Heob8a2df62012-11-19 08:13:37 -08002675 /*
2676 * Control reaches here with cgroup_mutex held.
2677 * @inode->i_mutex should nest outside cgroup_mutex but we
2678 * want to populate it immediately without releasing
2679 * cgroup_mutex. As @inode isn't visible to anyone else
2680 * yet, trylock will always succeed without affecting
2681 * lockdep checks.
2682 */
2683 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002684 } else if (S_ISREG(mode)) {
2685 inode->i_size = 0;
2686 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002687 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002688 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002689 d_instantiate(dentry, inode);
2690 dget(dentry); /* Extra count - pin the dentry in core */
2691 return 0;
2692}
2693
Li Zefan099fca32009-04-02 16:57:29 -07002694/**
2695 * cgroup_file_mode - deduce file mode of a control file
2696 * @cft: the control file in question
2697 *
2698 * returns cft->mode if ->mode is not 0
2699 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2700 * returns S_IRUGO if it has only a read handler
2701 * returns S_IWUSR if it has only a write hander
2702 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002703static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002704{
Al Viroa5e7ed32011-07-26 01:55:55 -04002705 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002706
2707 if (cft->mode)
2708 return cft->mode;
2709
2710 if (cft->read || cft->read_u64 || cft->read_s64 ||
2711 cft->read_map || cft->read_seq_string)
2712 mode |= S_IRUGO;
2713
2714 if (cft->write || cft->write_u64 || cft->write_s64 ||
2715 cft->write_string || cft->trigger)
2716 mode |= S_IWUSR;
2717
2718 return mode;
2719}
2720
Tejun Heodb0416b2012-04-01 12:09:55 -07002721static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002722 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002723{
Paul Menagebd89aab2007-10-18 23:40:44 -07002724 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002725 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002726 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002727 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002728 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002729 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002730 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002731
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002732 simple_xattrs_init(&cft->xattrs);
2733
Paul Menagebd89aab2007-10-18 23:40:44 -07002734 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002735 strcpy(name, subsys->name);
2736 strcat(name, ".");
2737 }
2738 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002739
Paul Menageddbcc7e2007-10-18 23:39:30 -07002740 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002741
2742 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2743 if (!cfe)
2744 return -ENOMEM;
2745
Paul Menageddbcc7e2007-10-18 23:39:30 -07002746 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002747 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002748 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002749 goto out;
2750 }
2751
2752 mode = cgroup_file_mode(cft);
2753 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2754 if (!error) {
2755 cfe->type = (void *)cft;
2756 cfe->dentry = dentry;
2757 dentry->d_fsdata = cfe;
2758 list_add_tail(&cfe->node, &parent->files);
2759 cfe = NULL;
2760 }
2761 dput(dentry);
2762out:
2763 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002764 return error;
2765}
2766
Tejun Heo79578622012-04-01 12:09:56 -07002767static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002768 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002769{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002770 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002771 int err, ret = 0;
2772
2773 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002774 /* does cft->flags tell us to skip this file on @cgrp? */
2775 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2776 continue;
2777 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2778 continue;
2779
Li Zefan2739d3c2013-01-21 18:18:33 +08002780 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002781 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002782 if (err)
2783 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2784 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002785 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002786 } else {
2787 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002788 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002789 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002790 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002791}
2792
Tejun Heo8e3f6542012-04-01 12:09:55 -07002793static DEFINE_MUTEX(cgroup_cft_mutex);
2794
2795static void cgroup_cfts_prepare(void)
2796 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2797{
2798 /*
2799 * Thanks to the entanglement with vfs inode locking, we can't walk
2800 * the existing cgroups under cgroup_mutex and create files.
2801 * Instead, we increment reference on all cgroups and build list of
2802 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2803 * exclusive access to the field.
2804 */
2805 mutex_lock(&cgroup_cft_mutex);
2806 mutex_lock(&cgroup_mutex);
2807}
2808
2809static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002810 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002811 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2812{
2813 LIST_HEAD(pending);
2814 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002815
2816 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2817 if (cfts && ss->root != &rootnode) {
2818 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2819 dget(cgrp->dentry);
2820 list_add_tail(&cgrp->cft_q_node, &pending);
2821 }
2822 }
2823
2824 mutex_unlock(&cgroup_mutex);
2825
2826 /*
2827 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2828 * files for all cgroups which were created before.
2829 */
2830 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2831 struct inode *inode = cgrp->dentry->d_inode;
2832
2833 mutex_lock(&inode->i_mutex);
2834 mutex_lock(&cgroup_mutex);
2835 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002836 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002837 mutex_unlock(&cgroup_mutex);
2838 mutex_unlock(&inode->i_mutex);
2839
2840 list_del_init(&cgrp->cft_q_node);
2841 dput(cgrp->dentry);
2842 }
2843
2844 mutex_unlock(&cgroup_cft_mutex);
2845}
2846
2847/**
2848 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2849 * @ss: target cgroup subsystem
2850 * @cfts: zero-length name terminated array of cftypes
2851 *
2852 * Register @cfts to @ss. Files described by @cfts are created for all
2853 * existing cgroups to which @ss is attached and all future cgroups will
2854 * have them too. This function can be called anytime whether @ss is
2855 * attached or not.
2856 *
2857 * Returns 0 on successful registration, -errno on failure. Note that this
2858 * function currently returns 0 as long as @cfts registration is successful
2859 * even if some file creation attempts on existing cgroups fail.
2860 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002861int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002862{
2863 struct cftype_set *set;
2864
2865 set = kzalloc(sizeof(*set), GFP_KERNEL);
2866 if (!set)
2867 return -ENOMEM;
2868
2869 cgroup_cfts_prepare();
2870 set->cfts = cfts;
2871 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002872 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002873
2874 return 0;
2875}
2876EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2877
Li Zefana043e3b2008-02-23 15:24:09 -08002878/**
Tejun Heo79578622012-04-01 12:09:56 -07002879 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2880 * @ss: target cgroup subsystem
2881 * @cfts: zero-length name terminated array of cftypes
2882 *
2883 * Unregister @cfts from @ss. Files described by @cfts are removed from
2884 * all existing cgroups to which @ss is attached and all future cgroups
2885 * won't have them either. This function can be called anytime whether @ss
2886 * is attached or not.
2887 *
2888 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2889 * registered with @ss.
2890 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002891int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002892{
2893 struct cftype_set *set;
2894
2895 cgroup_cfts_prepare();
2896
2897 list_for_each_entry(set, &ss->cftsets, node) {
2898 if (set->cfts == cfts) {
2899 list_del_init(&set->node);
2900 cgroup_cfts_commit(ss, cfts, false);
2901 return 0;
2902 }
2903 }
2904
2905 cgroup_cfts_commit(ss, NULL, false);
2906 return -ENOENT;
2907}
2908
2909/**
Li Zefana043e3b2008-02-23 15:24:09 -08002910 * cgroup_task_count - count the number of tasks in a cgroup.
2911 * @cgrp: the cgroup in question
2912 *
2913 * Return the number of tasks in the cgroup.
2914 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002915int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002916{
2917 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002918 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002919
Paul Menage817929e2007-10-18 23:39:36 -07002920 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002921 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002922 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002923 }
2924 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002925 return count;
2926}
2927
2928/*
Paul Menage817929e2007-10-18 23:39:36 -07002929 * Advance a list_head iterator. The iterator should be positioned at
2930 * the start of a css_set
2931 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002932static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002933 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002934{
2935 struct list_head *l = it->cg_link;
2936 struct cg_cgroup_link *link;
2937 struct css_set *cg;
2938
2939 /* Advance to the next non-empty css_set */
2940 do {
2941 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002942 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002943 it->cg_link = NULL;
2944 return;
2945 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002946 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002947 cg = link->cg;
2948 } while (list_empty(&cg->tasks));
2949 it->cg_link = l;
2950 it->task = cg->tasks.next;
2951}
2952
Cliff Wickman31a7df02008-02-07 00:14:42 -08002953/*
2954 * To reduce the fork() overhead for systems that are not actually
2955 * using their cgroups capability, we don't maintain the lists running
2956 * through each css_set to its tasks until we see the list actually
2957 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002958 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002959static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002960{
2961 struct task_struct *p, *g;
2962 write_lock(&css_set_lock);
2963 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002964 /*
2965 * We need tasklist_lock because RCU is not safe against
2966 * while_each_thread(). Besides, a forking task that has passed
2967 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2968 * is not guaranteed to have its child immediately visible in the
2969 * tasklist if we walk through it with RCU.
2970 */
2971 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002972 do_each_thread(g, p) {
2973 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002974 /*
2975 * We should check if the process is exiting, otherwise
2976 * it will race with cgroup_exit() in that the list
2977 * entry won't be deleted though the process has exited.
2978 */
2979 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002980 list_add(&p->cg_list, &p->cgroups->tasks);
2981 task_unlock(p);
2982 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002983 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002984 write_unlock(&css_set_lock);
2985}
2986
Tejun Heo574bd9f2012-11-09 09:12:29 -08002987/**
2988 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2989 * @pos: the current position (%NULL to initiate traversal)
2990 * @cgroup: cgroup whose descendants to walk
2991 *
2992 * To be used by cgroup_for_each_descendant_pre(). Find the next
2993 * descendant to visit for pre-order traversal of @cgroup's descendants.
2994 */
2995struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2996 struct cgroup *cgroup)
2997{
2998 struct cgroup *next;
2999
3000 WARN_ON_ONCE(!rcu_read_lock_held());
3001
3002 /* if first iteration, pretend we just visited @cgroup */
3003 if (!pos) {
3004 if (list_empty(&cgroup->children))
3005 return NULL;
3006 pos = cgroup;
3007 }
3008
3009 /* visit the first child if exists */
3010 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3011 if (next)
3012 return next;
3013
3014 /* no child, visit my or the closest ancestor's next sibling */
3015 do {
3016 next = list_entry_rcu(pos->sibling.next, struct cgroup,
3017 sibling);
3018 if (&next->sibling != &pos->parent->children)
3019 return next;
3020
3021 pos = pos->parent;
3022 } while (pos != cgroup);
3023
3024 return NULL;
3025}
3026EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3027
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003028/**
3029 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3030 * @pos: cgroup of interest
3031 *
3032 * Return the rightmost descendant of @pos. If there's no descendant,
3033 * @pos is returned. This can be used during pre-order traversal to skip
3034 * subtree of @pos.
3035 */
3036struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3037{
3038 struct cgroup *last, *tmp;
3039
3040 WARN_ON_ONCE(!rcu_read_lock_held());
3041
3042 do {
3043 last = pos;
3044 /* ->prev isn't RCU safe, walk ->next till the end */
3045 pos = NULL;
3046 list_for_each_entry_rcu(tmp, &last->children, sibling)
3047 pos = tmp;
3048 } while (pos);
3049
3050 return last;
3051}
3052EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3053
Tejun Heo574bd9f2012-11-09 09:12:29 -08003054static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3055{
3056 struct cgroup *last;
3057
3058 do {
3059 last = pos;
3060 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3061 sibling);
3062 } while (pos);
3063
3064 return last;
3065}
3066
3067/**
3068 * cgroup_next_descendant_post - find the next descendant for post-order walk
3069 * @pos: the current position (%NULL to initiate traversal)
3070 * @cgroup: cgroup whose descendants to walk
3071 *
3072 * To be used by cgroup_for_each_descendant_post(). Find the next
3073 * descendant to visit for post-order traversal of @cgroup's descendants.
3074 */
3075struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3076 struct cgroup *cgroup)
3077{
3078 struct cgroup *next;
3079
3080 WARN_ON_ONCE(!rcu_read_lock_held());
3081
3082 /* if first iteration, visit the leftmost descendant */
3083 if (!pos) {
3084 next = cgroup_leftmost_descendant(cgroup);
3085 return next != cgroup ? next : NULL;
3086 }
3087
3088 /* if there's an unvisited sibling, visit its leftmost descendant */
3089 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3090 if (&next->sibling != &pos->parent->children)
3091 return cgroup_leftmost_descendant(next);
3092
3093 /* no sibling left, visit parent */
3094 next = pos->parent;
3095 return next != cgroup ? next : NULL;
3096}
3097EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3098
Paul Menagebd89aab2007-10-18 23:40:44 -07003099void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003100 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003101{
3102 /*
3103 * The first time anyone tries to iterate across a cgroup,
3104 * we need to enable the list linking each css_set to its
3105 * tasks, and fix up all existing tasks.
3106 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003107 if (!use_task_css_set_links)
3108 cgroup_enable_task_cg_lists();
3109
Paul Menage817929e2007-10-18 23:39:36 -07003110 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003111 it->cg_link = &cgrp->css_sets;
3112 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003113}
3114
Paul Menagebd89aab2007-10-18 23:40:44 -07003115struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003116 struct cgroup_iter *it)
3117{
3118 struct task_struct *res;
3119 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003120 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003121
3122 /* If the iterator cg is NULL, we have no tasks */
3123 if (!it->cg_link)
3124 return NULL;
3125 res = list_entry(l, struct task_struct, cg_list);
3126 /* Advance iterator to find next entry */
3127 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003128 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3129 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003130 /* We reached the end of this task list - move on to
3131 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003132 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003133 } else {
3134 it->task = l;
3135 }
3136 return res;
3137}
3138
Paul Menagebd89aab2007-10-18 23:40:44 -07003139void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003140 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003141{
3142 read_unlock(&css_set_lock);
3143}
3144
Cliff Wickman31a7df02008-02-07 00:14:42 -08003145static inline int started_after_time(struct task_struct *t1,
3146 struct timespec *time,
3147 struct task_struct *t2)
3148{
3149 int start_diff = timespec_compare(&t1->start_time, time);
3150 if (start_diff > 0) {
3151 return 1;
3152 } else if (start_diff < 0) {
3153 return 0;
3154 } else {
3155 /*
3156 * Arbitrarily, if two processes started at the same
3157 * time, we'll say that the lower pointer value
3158 * started first. Note that t2 may have exited by now
3159 * so this may not be a valid pointer any longer, but
3160 * that's fine - it still serves to distinguish
3161 * between two tasks started (effectively) simultaneously.
3162 */
3163 return t1 > t2;
3164 }
3165}
3166
3167/*
3168 * This function is a callback from heap_insert() and is used to order
3169 * the heap.
3170 * In this case we order the heap in descending task start time.
3171 */
3172static inline int started_after(void *p1, void *p2)
3173{
3174 struct task_struct *t1 = p1;
3175 struct task_struct *t2 = p2;
3176 return started_after_time(t1, &t2->start_time, t2);
3177}
3178
3179/**
3180 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3181 * @scan: struct cgroup_scanner containing arguments for the scan
3182 *
3183 * Arguments include pointers to callback functions test_task() and
3184 * process_task().
3185 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3186 * and if it returns true, call process_task() for it also.
3187 * The test_task pointer may be NULL, meaning always true (select all tasks).
3188 * Effectively duplicates cgroup_iter_{start,next,end}()
3189 * but does not lock css_set_lock for the call to process_task().
3190 * The struct cgroup_scanner may be embedded in any structure of the caller's
3191 * creation.
3192 * It is guaranteed that process_task() will act on every task that
3193 * is a member of the cgroup for the duration of this call. This
3194 * function may or may not call process_task() for tasks that exit
3195 * or move to a different cgroup during the call, or are forked or
3196 * move into the cgroup during the call.
3197 *
3198 * Note that test_task() may be called with locks held, and may in some
3199 * situations be called multiple times for the same task, so it should
3200 * be cheap.
3201 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3202 * pre-allocated and will be used for heap operations (and its "gt" member will
3203 * be overwritten), else a temporary heap will be used (allocation of which
3204 * may cause this function to fail).
3205 */
3206int cgroup_scan_tasks(struct cgroup_scanner *scan)
3207{
3208 int retval, i;
3209 struct cgroup_iter it;
3210 struct task_struct *p, *dropped;
3211 /* Never dereference latest_task, since it's not refcounted */
3212 struct task_struct *latest_task = NULL;
3213 struct ptr_heap tmp_heap;
3214 struct ptr_heap *heap;
3215 struct timespec latest_time = { 0, 0 };
3216
3217 if (scan->heap) {
3218 /* The caller supplied our heap and pre-allocated its memory */
3219 heap = scan->heap;
3220 heap->gt = &started_after;
3221 } else {
3222 /* We need to allocate our own heap memory */
3223 heap = &tmp_heap;
3224 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3225 if (retval)
3226 /* cannot allocate the heap */
3227 return retval;
3228 }
3229
3230 again:
3231 /*
3232 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3233 * to determine which are of interest, and using the scanner's
3234 * "process_task" callback to process any of them that need an update.
3235 * Since we don't want to hold any locks during the task updates,
3236 * gather tasks to be processed in a heap structure.
3237 * The heap is sorted by descending task start time.
3238 * If the statically-sized heap fills up, we overflow tasks that
3239 * started later, and in future iterations only consider tasks that
3240 * started after the latest task in the previous pass. This
3241 * guarantees forward progress and that we don't miss any tasks.
3242 */
3243 heap->size = 0;
3244 cgroup_iter_start(scan->cg, &it);
3245 while ((p = cgroup_iter_next(scan->cg, &it))) {
3246 /*
3247 * Only affect tasks that qualify per the caller's callback,
3248 * if he provided one
3249 */
3250 if (scan->test_task && !scan->test_task(p, scan))
3251 continue;
3252 /*
3253 * Only process tasks that started after the last task
3254 * we processed
3255 */
3256 if (!started_after_time(p, &latest_time, latest_task))
3257 continue;
3258 dropped = heap_insert(heap, p);
3259 if (dropped == NULL) {
3260 /*
3261 * The new task was inserted; the heap wasn't
3262 * previously full
3263 */
3264 get_task_struct(p);
3265 } else if (dropped != p) {
3266 /*
3267 * The new task was inserted, and pushed out a
3268 * different task
3269 */
3270 get_task_struct(p);
3271 put_task_struct(dropped);
3272 }
3273 /*
3274 * Else the new task was newer than anything already in
3275 * the heap and wasn't inserted
3276 */
3277 }
3278 cgroup_iter_end(scan->cg, &it);
3279
3280 if (heap->size) {
3281 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003282 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003283 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003284 latest_time = q->start_time;
3285 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003286 }
3287 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003288 scan->process_task(q, scan);
3289 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003290 }
3291 /*
3292 * If we had to process any tasks at all, scan again
3293 * in case some of them were in the middle of forking
3294 * children that didn't get processed.
3295 * Not the most efficient way to do it, but it avoids
3296 * having to take callback_mutex in the fork path
3297 */
3298 goto again;
3299 }
3300 if (heap == &tmp_heap)
3301 heap_free(&tmp_heap);
3302 return 0;
3303}
3304
Paul Menage817929e2007-10-18 23:39:36 -07003305/*
Ben Blum102a7752009-09-23 15:56:26 -07003306 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003307 *
3308 * Reading this file can return large amounts of data if a cgroup has
3309 * *lots* of attached tasks. So it may need several calls to read(),
3310 * but we cannot guarantee that the information we produce is correct
3311 * unless we produce it entirely atomically.
3312 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003313 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003314
Li Zefan24528252012-01-20 11:58:43 +08003315/* which pidlist file are we talking about? */
3316enum cgroup_filetype {
3317 CGROUP_FILE_PROCS,
3318 CGROUP_FILE_TASKS,
3319};
3320
3321/*
3322 * A pidlist is a list of pids that virtually represents the contents of one
3323 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3324 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3325 * to the cgroup.
3326 */
3327struct cgroup_pidlist {
3328 /*
3329 * used to find which pidlist is wanted. doesn't change as long as
3330 * this particular list stays in the list.
3331 */
3332 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3333 /* array of xids */
3334 pid_t *list;
3335 /* how many elements the above list has */
3336 int length;
3337 /* how many files are using the current array */
3338 int use_count;
3339 /* each of these stored in a list by its cgroup */
3340 struct list_head links;
3341 /* pointer to the cgroup we belong to, for list removal purposes */
3342 struct cgroup *owner;
3343 /* protects the other fields */
3344 struct rw_semaphore mutex;
3345};
3346
Paul Menagebbcb81d2007-10-18 23:39:32 -07003347/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003348 * The following two functions "fix" the issue where there are more pids
3349 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3350 * TODO: replace with a kernel-wide solution to this problem
3351 */
3352#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3353static void *pidlist_allocate(int count)
3354{
3355 if (PIDLIST_TOO_LARGE(count))
3356 return vmalloc(count * sizeof(pid_t));
3357 else
3358 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3359}
3360static void pidlist_free(void *p)
3361{
3362 if (is_vmalloc_addr(p))
3363 vfree(p);
3364 else
3365 kfree(p);
3366}
3367static void *pidlist_resize(void *p, int newcount)
3368{
3369 void *newlist;
3370 /* note: if new alloc fails, old p will still be valid either way */
3371 if (is_vmalloc_addr(p)) {
3372 newlist = vmalloc(newcount * sizeof(pid_t));
3373 if (!newlist)
3374 return NULL;
3375 memcpy(newlist, p, newcount * sizeof(pid_t));
3376 vfree(p);
3377 } else {
3378 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3379 }
3380 return newlist;
3381}
3382
3383/*
Ben Blum102a7752009-09-23 15:56:26 -07003384 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3385 * If the new stripped list is sufficiently smaller and there's enough memory
3386 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3387 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003388 */
Ben Blum102a7752009-09-23 15:56:26 -07003389/* is the size difference enough that we should re-allocate the array? */
3390#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3391static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003392{
Ben Blum102a7752009-09-23 15:56:26 -07003393 int src, dest = 1;
3394 pid_t *list = *p;
3395 pid_t *newlist;
3396
3397 /*
3398 * we presume the 0th element is unique, so i starts at 1. trivial
3399 * edge cases first; no work needs to be done for either
3400 */
3401 if (length == 0 || length == 1)
3402 return length;
3403 /* src and dest walk down the list; dest counts unique elements */
3404 for (src = 1; src < length; src++) {
3405 /* find next unique element */
3406 while (list[src] == list[src-1]) {
3407 src++;
3408 if (src == length)
3409 goto after;
3410 }
3411 /* dest always points to where the next unique element goes */
3412 list[dest] = list[src];
3413 dest++;
3414 }
3415after:
3416 /*
3417 * if the length difference is large enough, we want to allocate a
3418 * smaller buffer to save memory. if this fails due to out of memory,
3419 * we'll just stay with what we've got.
3420 */
3421 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003422 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003423 if (newlist)
3424 *p = newlist;
3425 }
3426 return dest;
3427}
3428
3429static int cmppid(const void *a, const void *b)
3430{
3431 return *(pid_t *)a - *(pid_t *)b;
3432}
3433
3434/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003435 * find the appropriate pidlist for our purpose (given procs vs tasks)
3436 * returns with the lock on that pidlist already held, and takes care
3437 * of the use count, or returns NULL with no locks held if we're out of
3438 * memory.
3439 */
3440static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3441 enum cgroup_filetype type)
3442{
3443 struct cgroup_pidlist *l;
3444 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003445 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003446
Ben Blum72a8cb32009-09-23 15:56:27 -07003447 /*
3448 * We can't drop the pidlist_mutex before taking the l->mutex in case
3449 * the last ref-holder is trying to remove l from the list at the same
3450 * time. Holding the pidlist_mutex precludes somebody taking whichever
3451 * list we find out from under us - compare release_pid_array().
3452 */
3453 mutex_lock(&cgrp->pidlist_mutex);
3454 list_for_each_entry(l, &cgrp->pidlists, links) {
3455 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003456 /* make sure l doesn't vanish out from under us */
3457 down_write(&l->mutex);
3458 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003459 return l;
3460 }
3461 }
3462 /* entry not found; create a new one */
3463 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3464 if (!l) {
3465 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003466 return l;
3467 }
3468 init_rwsem(&l->mutex);
3469 down_write(&l->mutex);
3470 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003471 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003472 l->use_count = 0; /* don't increment here */
3473 l->list = NULL;
3474 l->owner = cgrp;
3475 list_add(&l->links, &cgrp->pidlists);
3476 mutex_unlock(&cgrp->pidlist_mutex);
3477 return l;
3478}
3479
3480/*
Ben Blum102a7752009-09-23 15:56:26 -07003481 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3482 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003483static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3484 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003485{
3486 pid_t *array;
3487 int length;
3488 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003489 struct cgroup_iter it;
3490 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003491 struct cgroup_pidlist *l;
3492
3493 /*
3494 * If cgroup gets more users after we read count, we won't have
3495 * enough space - tough. This race is indistinguishable to the
3496 * caller from the case that the additional cgroup users didn't
3497 * show up until sometime later on.
3498 */
3499 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003500 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003501 if (!array)
3502 return -ENOMEM;
3503 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003504 cgroup_iter_start(cgrp, &it);
3505 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003506 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003507 break;
Ben Blum102a7752009-09-23 15:56:26 -07003508 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003509 if (type == CGROUP_FILE_PROCS)
3510 pid = task_tgid_vnr(tsk);
3511 else
3512 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003513 if (pid > 0) /* make sure to only use valid results */
3514 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003515 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003516 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003517 length = n;
3518 /* now sort & (if procs) strip out duplicates */
3519 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003520 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003521 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003522 l = cgroup_pidlist_find(cgrp, type);
3523 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003524 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003525 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003526 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003527 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003528 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003529 l->list = array;
3530 l->length = length;
3531 l->use_count++;
3532 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003533 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003534 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003535}
3536
Balbir Singh846c7bb2007-10-18 23:39:44 -07003537/**
Li Zefana043e3b2008-02-23 15:24:09 -08003538 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003539 * @stats: cgroupstats to fill information into
3540 * @dentry: A dentry entry belonging to the cgroup for which stats have
3541 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003542 *
3543 * Build and fill cgroupstats so that taskstats can export it to user
3544 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003545 */
3546int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3547{
3548 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003549 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003550 struct cgroup_iter it;
3551 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003552
Balbir Singh846c7bb2007-10-18 23:39:44 -07003553 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003554 * Validate dentry by checking the superblock operations,
3555 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003556 */
Li Zefan33d283b2008-11-19 15:36:48 -08003557 if (dentry->d_sb->s_op != &cgroup_ops ||
3558 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003559 goto err;
3560
3561 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003562 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003563
Paul Menagebd89aab2007-10-18 23:40:44 -07003564 cgroup_iter_start(cgrp, &it);
3565 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003566 switch (tsk->state) {
3567 case TASK_RUNNING:
3568 stats->nr_running++;
3569 break;
3570 case TASK_INTERRUPTIBLE:
3571 stats->nr_sleeping++;
3572 break;
3573 case TASK_UNINTERRUPTIBLE:
3574 stats->nr_uninterruptible++;
3575 break;
3576 case TASK_STOPPED:
3577 stats->nr_stopped++;
3578 break;
3579 default:
3580 if (delayacct_is_task_waiting_on_io(tsk))
3581 stats->nr_io_wait++;
3582 break;
3583 }
3584 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003585 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003586
Balbir Singh846c7bb2007-10-18 23:39:44 -07003587err:
3588 return ret;
3589}
3590
Paul Menage8f3ff202009-09-23 15:56:25 -07003591
Paul Menagecc31edc2008-10-18 20:28:04 -07003592/*
Ben Blum102a7752009-09-23 15:56:26 -07003593 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003594 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003595 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003596 */
3597
Ben Blum102a7752009-09-23 15:56:26 -07003598static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003599{
3600 /*
3601 * Initially we receive a position value that corresponds to
3602 * one more than the last pid shown (or 0 on the first call or
3603 * after a seek to the start). Use a binary-search to find the
3604 * next pid to display, if any
3605 */
Ben Blum102a7752009-09-23 15:56:26 -07003606 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003607 int index = 0, pid = *pos;
3608 int *iter;
3609
Ben Blum102a7752009-09-23 15:56:26 -07003610 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003611 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003612 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003613
Paul Menagecc31edc2008-10-18 20:28:04 -07003614 while (index < end) {
3615 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003616 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003617 index = mid;
3618 break;
Ben Blum102a7752009-09-23 15:56:26 -07003619 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003620 index = mid + 1;
3621 else
3622 end = mid;
3623 }
3624 }
3625 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003626 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003627 return NULL;
3628 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003629 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003630 *pos = *iter;
3631 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003632}
3633
Ben Blum102a7752009-09-23 15:56:26 -07003634static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003635{
Ben Blum102a7752009-09-23 15:56:26 -07003636 struct cgroup_pidlist *l = s->private;
3637 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003638}
3639
Ben Blum102a7752009-09-23 15:56:26 -07003640static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003641{
Ben Blum102a7752009-09-23 15:56:26 -07003642 struct cgroup_pidlist *l = s->private;
3643 pid_t *p = v;
3644 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003645 /*
3646 * Advance to the next pid in the array. If this goes off the
3647 * end, we're done
3648 */
3649 p++;
3650 if (p >= end) {
3651 return NULL;
3652 } else {
3653 *pos = *p;
3654 return p;
3655 }
3656}
3657
Ben Blum102a7752009-09-23 15:56:26 -07003658static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003659{
3660 return seq_printf(s, "%d\n", *(int *)v);
3661}
3662
Ben Blum102a7752009-09-23 15:56:26 -07003663/*
3664 * seq_operations functions for iterating on pidlists through seq_file -
3665 * independent of whether it's tasks or procs
3666 */
3667static const struct seq_operations cgroup_pidlist_seq_operations = {
3668 .start = cgroup_pidlist_start,
3669 .stop = cgroup_pidlist_stop,
3670 .next = cgroup_pidlist_next,
3671 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003672};
3673
Ben Blum102a7752009-09-23 15:56:26 -07003674static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003675{
Ben Blum72a8cb32009-09-23 15:56:27 -07003676 /*
3677 * the case where we're the last user of this particular pidlist will
3678 * have us remove it from the cgroup's list, which entails taking the
3679 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3680 * pidlist_mutex, we have to take pidlist_mutex first.
3681 */
3682 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003683 down_write(&l->mutex);
3684 BUG_ON(!l->use_count);
3685 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003686 /* we're the last user if refcount is 0; remove and free */
3687 list_del(&l->links);
3688 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003689 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003690 put_pid_ns(l->key.ns);
3691 up_write(&l->mutex);
3692 kfree(l);
3693 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003694 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003695 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003696 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003697}
3698
Ben Blum102a7752009-09-23 15:56:26 -07003699static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003700{
Ben Blum102a7752009-09-23 15:56:26 -07003701 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003702 if (!(file->f_mode & FMODE_READ))
3703 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003704 /*
3705 * the seq_file will only be initialized if the file was opened for
3706 * reading; hence we check if it's not null only in that case.
3707 */
3708 l = ((struct seq_file *)file->private_data)->private;
3709 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003710 return seq_release(inode, file);
3711}
3712
Ben Blum102a7752009-09-23 15:56:26 -07003713static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003714 .read = seq_read,
3715 .llseek = seq_lseek,
3716 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003717 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003718};
3719
3720/*
Ben Blum102a7752009-09-23 15:56:26 -07003721 * The following functions handle opens on a file that displays a pidlist
3722 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3723 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003724 */
Ben Blum102a7752009-09-23 15:56:26 -07003725/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003726static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003727{
3728 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003729 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003730 int retval;
3731
3732 /* Nothing to do for write-only files */
3733 if (!(file->f_mode & FMODE_READ))
3734 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003735
Ben Blum102a7752009-09-23 15:56:26 -07003736 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003737 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003738 if (retval)
3739 return retval;
3740 /* configure file information */
3741 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003742
Ben Blum102a7752009-09-23 15:56:26 -07003743 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003744 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003745 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003746 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003747 }
Ben Blum102a7752009-09-23 15:56:26 -07003748 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003749 return 0;
3750}
Ben Blum102a7752009-09-23 15:56:26 -07003751static int cgroup_tasks_open(struct inode *unused, struct file *file)
3752{
Ben Blum72a8cb32009-09-23 15:56:27 -07003753 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003754}
3755static int cgroup_procs_open(struct inode *unused, struct file *file)
3756{
Ben Blum72a8cb32009-09-23 15:56:27 -07003757 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003758}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003759
Paul Menagebd89aab2007-10-18 23:40:44 -07003760static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003761 struct cftype *cft)
3762{
Paul Menagebd89aab2007-10-18 23:40:44 -07003763 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003764}
3765
Paul Menage6379c102008-07-25 01:47:01 -07003766static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3767 struct cftype *cft,
3768 u64 val)
3769{
3770 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3771 if (val)
3772 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3773 else
3774 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3775 return 0;
3776}
3777
Paul Menagebbcb81d2007-10-18 23:39:32 -07003778/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003779 * Unregister event and free resources.
3780 *
3781 * Gets called from workqueue.
3782 */
3783static void cgroup_event_remove(struct work_struct *work)
3784{
3785 struct cgroup_event *event = container_of(work, struct cgroup_event,
3786 remove);
3787 struct cgroup *cgrp = event->cgrp;
3788
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003789 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3790
3791 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003792 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003793 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003794}
3795
3796/*
3797 * Gets called on POLLHUP on eventfd when user closes it.
3798 *
3799 * Called with wqh->lock held and interrupts disabled.
3800 */
3801static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3802 int sync, void *key)
3803{
3804 struct cgroup_event *event = container_of(wait,
3805 struct cgroup_event, wait);
3806 struct cgroup *cgrp = event->cgrp;
3807 unsigned long flags = (unsigned long)key;
3808
3809 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003810 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003811 spin_lock(&cgrp->event_list_lock);
Greg Thelen9718ceb2012-11-28 13:50:45 -08003812 list_del_init(&event->list);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003813 spin_unlock(&cgrp->event_list_lock);
3814 /*
3815 * We are in atomic context, but cgroup_event_remove() may
3816 * sleep, so we have to call it in workqueue.
3817 */
3818 schedule_work(&event->remove);
3819 }
3820
3821 return 0;
3822}
3823
3824static void cgroup_event_ptable_queue_proc(struct file *file,
3825 wait_queue_head_t *wqh, poll_table *pt)
3826{
3827 struct cgroup_event *event = container_of(pt,
3828 struct cgroup_event, pt);
3829
3830 event->wqh = wqh;
3831 add_wait_queue(wqh, &event->wait);
3832}
3833
3834/*
3835 * Parse input and register new cgroup event handler.
3836 *
3837 * Input must be in format '<event_fd> <control_fd> <args>'.
3838 * Interpretation of args is defined by control file implementation.
3839 */
3840static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3841 const char *buffer)
3842{
3843 struct cgroup_event *event = NULL;
3844 unsigned int efd, cfd;
3845 struct file *efile = NULL;
3846 struct file *cfile = NULL;
3847 char *endp;
3848 int ret;
3849
3850 efd = simple_strtoul(buffer, &endp, 10);
3851 if (*endp != ' ')
3852 return -EINVAL;
3853 buffer = endp + 1;
3854
3855 cfd = simple_strtoul(buffer, &endp, 10);
3856 if ((*endp != ' ') && (*endp != '\0'))
3857 return -EINVAL;
3858 buffer = endp + 1;
3859
3860 event = kzalloc(sizeof(*event), GFP_KERNEL);
3861 if (!event)
3862 return -ENOMEM;
3863 event->cgrp = cgrp;
3864 INIT_LIST_HEAD(&event->list);
3865 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3866 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3867 INIT_WORK(&event->remove, cgroup_event_remove);
3868
3869 efile = eventfd_fget(efd);
3870 if (IS_ERR(efile)) {
3871 ret = PTR_ERR(efile);
3872 goto fail;
3873 }
3874
3875 event->eventfd = eventfd_ctx_fileget(efile);
3876 if (IS_ERR(event->eventfd)) {
3877 ret = PTR_ERR(event->eventfd);
3878 goto fail;
3879 }
3880
3881 cfile = fget(cfd);
3882 if (!cfile) {
3883 ret = -EBADF;
3884 goto fail;
3885 }
3886
3887 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003888 /* AV: shouldn't we check that it's been opened for read instead? */
3889 ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003890 if (ret < 0)
3891 goto fail;
3892
3893 event->cft = __file_cft(cfile);
3894 if (IS_ERR(event->cft)) {
3895 ret = PTR_ERR(event->cft);
3896 goto fail;
3897 }
3898
3899 if (!event->cft->register_event || !event->cft->unregister_event) {
3900 ret = -EINVAL;
3901 goto fail;
3902 }
3903
3904 ret = event->cft->register_event(cgrp, event->cft,
3905 event->eventfd, buffer);
3906 if (ret)
3907 goto fail;
3908
3909 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3910 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3911 ret = 0;
3912 goto fail;
3913 }
3914
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003915 /*
3916 * Events should be removed after rmdir of cgroup directory, but before
3917 * destroying subsystem state objects. Let's take reference to cgroup
3918 * directory dentry to do that.
3919 */
3920 dget(cgrp->dentry);
3921
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003922 spin_lock(&cgrp->event_list_lock);
3923 list_add(&event->list, &cgrp->event_list);
3924 spin_unlock(&cgrp->event_list_lock);
3925
3926 fput(cfile);
3927 fput(efile);
3928
3929 return 0;
3930
3931fail:
3932 if (cfile)
3933 fput(cfile);
3934
3935 if (event && event->eventfd && !IS_ERR(event->eventfd))
3936 eventfd_ctx_put(event->eventfd);
3937
3938 if (!IS_ERR_OR_NULL(efile))
3939 fput(efile);
3940
3941 kfree(event);
3942
3943 return ret;
3944}
3945
Daniel Lezcano97978e62010-10-27 15:33:35 -07003946static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3947 struct cftype *cft)
3948{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003949 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003950}
3951
3952static int cgroup_clone_children_write(struct cgroup *cgrp,
3953 struct cftype *cft,
3954 u64 val)
3955{
3956 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003957 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003958 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003959 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003960 return 0;
3961}
3962
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003963/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003964 * for the common functions, 'private' gives the type of file
3965 */
Ben Blum102a7752009-09-23 15:56:26 -07003966/* for hysterical raisins, we can't put this on the older files */
3967#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003968static struct cftype files[] = {
3969 {
3970 .name = "tasks",
3971 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003972 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003973 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003974 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003975 },
Ben Blum102a7752009-09-23 15:56:26 -07003976 {
3977 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3978 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003979 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003980 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003981 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003982 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003983 {
3984 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003985 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003986 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003987 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003988 {
3989 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3990 .write_string = cgroup_write_event_control,
3991 .mode = S_IWUGO,
3992 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003993 {
3994 .name = "cgroup.clone_children",
3995 .read_u64 = cgroup_clone_children_read,
3996 .write_u64 = cgroup_clone_children_write,
3997 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003998 {
3999 .name = "release_agent",
4000 .flags = CFTYPE_ONLY_ON_ROOT,
4001 .read_seq_string = cgroup_release_agent_show,
4002 .write_string = cgroup_release_agent_write,
4003 .max_write_len = PATH_MAX,
4004 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004005 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004006};
4007
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004008/**
4009 * cgroup_populate_dir - selectively creation of files in a directory
4010 * @cgrp: target cgroup
4011 * @base_files: true if the base files should be added
4012 * @subsys_mask: mask of the subsystem ids whose files should be added
4013 */
4014static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4015 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004016{
4017 int err;
4018 struct cgroup_subsys *ss;
4019
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004020 if (base_files) {
4021 err = cgroup_addrm_files(cgrp, NULL, files, true);
4022 if (err < 0)
4023 return err;
4024 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004025
Tejun Heo8e3f6542012-04-01 12:09:55 -07004026 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004027 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004028 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004029 if (!test_bit(ss->subsys_id, &subsys_mask))
4030 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004031
Tejun Heodb0416b2012-04-01 12:09:55 -07004032 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004033 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004034 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004035
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004036 /* This cgroup is ready now */
4037 for_each_subsys(cgrp->root, ss) {
4038 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4039 /*
4040 * Update id->css pointer and make this css visible from
4041 * CSS ID functions. This pointer will be dereferened
4042 * from RCU-read-side without locks.
4043 */
4044 if (css->id)
4045 rcu_assign_pointer(css->id->css, css);
4046 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004047
4048 return 0;
4049}
4050
Tejun Heo48ddbe12012-04-01 12:09:56 -07004051static void css_dput_fn(struct work_struct *work)
4052{
4053 struct cgroup_subsys_state *css =
4054 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004055 struct dentry *dentry = css->cgroup->dentry;
4056 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004057
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004058 atomic_inc(&sb->s_active);
4059 dput(dentry);
4060 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004061}
4062
Paul Menageddbcc7e2007-10-18 23:39:30 -07004063static void init_cgroup_css(struct cgroup_subsys_state *css,
4064 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004065 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004066{
Paul Menagebd89aab2007-10-18 23:40:44 -07004067 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004068 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004069 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004070 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004071 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004072 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004073 BUG_ON(cgrp->subsys[ss->subsys_id]);
4074 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004075
4076 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004077 * css holds an extra ref to @cgrp->dentry which is put on the last
4078 * css_put(). dput() requires process context, which css_put() may
4079 * be called without. @css->dput_work will be used to invoke
4080 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004081 */
4082 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004083}
4084
Tejun Heob1929db2012-11-19 08:13:38 -08004085/* invoke ->post_create() on a new CSS and mark it online if successful */
4086static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004087{
Tejun Heob1929db2012-11-19 08:13:38 -08004088 int ret = 0;
4089
Tejun Heoa31f2d32012-11-19 08:13:37 -08004090 lockdep_assert_held(&cgroup_mutex);
4091
Tejun Heo92fb9742012-11-19 08:13:38 -08004092 if (ss->css_online)
4093 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004094 if (!ret)
4095 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4096 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004097}
4098
4099/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4100static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4101 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4102{
4103 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4104
4105 lockdep_assert_held(&cgroup_mutex);
4106
4107 if (!(css->flags & CSS_ONLINE))
4108 return;
4109
4110 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004111 * css_offline() should be called with cgroup_mutex unlocked. See
Tejun Heoa31f2d32012-11-19 08:13:37 -08004112 * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for
4113 * details. This temporary unlocking should go away once
4114 * cgroup_mutex is unexported from controllers.
4115 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004116 if (ss->css_offline) {
Tejun Heoa31f2d32012-11-19 08:13:37 -08004117 mutex_unlock(&cgroup_mutex);
Tejun Heo92fb9742012-11-19 08:13:38 -08004118 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004119 mutex_lock(&cgroup_mutex);
4120 }
4121
4122 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4123}
4124
Paul Menageddbcc7e2007-10-18 23:39:30 -07004125/*
Li Zefana043e3b2008-02-23 15:24:09 -08004126 * cgroup_create - create a cgroup
4127 * @parent: cgroup that will be parent of the new cgroup
4128 * @dentry: dentry of the new cgroup
4129 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004130 *
Li Zefana043e3b2008-02-23 15:24:09 -08004131 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004132 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004133static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004134 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004135{
Paul Menagebd89aab2007-10-18 23:40:44 -07004136 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004137 struct cgroupfs_root *root = parent->root;
4138 int err = 0;
4139 struct cgroup_subsys *ss;
4140 struct super_block *sb = root->sb;
4141
Tejun Heo0a950f62012-11-19 09:02:12 -08004142 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004143 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4144 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004145 return -ENOMEM;
4146
Tejun Heo0a950f62012-11-19 09:02:12 -08004147 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4148 if (cgrp->id < 0)
4149 goto err_free_cgrp;
4150
Tejun Heo976c06b2012-11-05 09:16:59 -08004151 /*
4152 * Only live parents can have children. Note that the liveliness
4153 * check isn't strictly necessary because cgroup_mkdir() and
4154 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4155 * anyway so that locking is contained inside cgroup proper and we
4156 * don't get nasty surprises if we ever grow another caller.
4157 */
4158 if (!cgroup_lock_live_group(parent)) {
4159 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004160 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004161 }
4162
Paul Menageddbcc7e2007-10-18 23:39:30 -07004163 /* Grab a reference on the superblock so the hierarchy doesn't
4164 * get deleted on unmount if there are child cgroups. This
4165 * can be done outside cgroup_mutex, since the sb can't
4166 * disappear while someone has an open control file on the
4167 * fs */
4168 atomic_inc(&sb->s_active);
4169
Paul Menagecc31edc2008-10-18 20:28:04 -07004170 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004171
Li Zefanfe1c06c2013-01-24 14:30:22 +08004172 dentry->d_fsdata = cgrp;
4173 cgrp->dentry = dentry;
4174
Paul Menagebd89aab2007-10-18 23:40:44 -07004175 cgrp->parent = parent;
4176 cgrp->root = parent->root;
4177 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004178
Li Zefanb6abdb02008-03-04 14:28:19 -08004179 if (notify_on_release(parent))
4180 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4181
Tejun Heo2260e7f2012-11-19 08:13:38 -08004182 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4183 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004184
Paul Menageddbcc7e2007-10-18 23:39:30 -07004185 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004186 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004187
Tejun Heo92fb9742012-11-19 08:13:38 -08004188 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004189 if (IS_ERR(css)) {
4190 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004191 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004192 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004193 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004194 if (ss->use_id) {
4195 err = alloc_css_id(ss, parent, cgrp);
4196 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004197 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004198 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004199 }
4200
Tejun Heo4e139af2012-11-19 08:13:36 -08004201 /*
4202 * Create directory. cgroup_create_file() returns with the new
4203 * directory locked on success so that it can be populated without
4204 * dropping cgroup_mutex.
4205 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004206 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004207 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004208 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004209 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004210
Tejun Heo4e139af2012-11-19 08:13:36 -08004211 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004212 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4213 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4214 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004215
Tejun Heob1929db2012-11-19 08:13:38 -08004216 /* each css holds a ref to the cgroup's dentry */
4217 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004218 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004219
Tejun Heob1929db2012-11-19 08:13:38 -08004220 /* creation succeeded, notify subsystems */
4221 for_each_subsys(root, ss) {
4222 err = online_css(ss, cgrp);
4223 if (err)
4224 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004225
4226 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4227 parent->parent) {
4228 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",
4229 current->comm, current->pid, ss->name);
4230 if (!strcmp(ss->name, "memory"))
4231 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4232 ss->warned_broken_hierarchy = true;
4233 }
Tejun Heoa8638032012-11-09 09:12:29 -08004234 }
4235
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004236 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004237 if (err)
4238 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004239
4240 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004241 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004242
4243 return 0;
4244
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004245err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004246 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004247 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004248 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004250 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004251 /* Release the reference count that we took on the superblock */
4252 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004253err_free_id:
4254 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004255err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004256 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004257 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004258
4259err_destroy:
4260 cgroup_destroy_locked(cgrp);
4261 mutex_unlock(&cgroup_mutex);
4262 mutex_unlock(&dentry->d_inode->i_mutex);
4263 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004264}
4265
Al Viro18bb1db2011-07-26 01:41:39 -04004266static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004267{
4268 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4269
4270 /* the vfs holds inode->i_mutex already */
4271 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4272}
4273
Tejun Heo28b4c272012-04-01 12:09:56 -07004274/*
4275 * Check the reference count on each subsystem. Since we already
4276 * established that there are no tasks in the cgroup, if the css refcount
4277 * is also 1, then there should be no outstanding references, so the
4278 * subsystem is safe to destroy. We scan across all subsystems rather than
4279 * using the per-hierarchy linked list of mounted subsystems since we can
4280 * be called via check_for_release() with no synchronization other than
4281 * RCU, and the subsystem linked list isn't RCU-safe.
4282 */
Li Zefan55b6fd02008-07-29 22:33:20 -07004283static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004284{
Paul Menage81a6a5c2007-10-18 23:39:38 -07004285 int i;
Tejun Heo28b4c272012-04-01 12:09:56 -07004286
Ben Blumaae8aab2010-03-10 15:22:07 -08004287 /*
4288 * We won't need to lock the subsys array, because the subsystems
4289 * we're concerned about aren't going anywhere since our cgroup root
4290 * has a reference on them.
4291 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004292 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4293 struct cgroup_subsys *ss = subsys[i];
4294 struct cgroup_subsys_state *css;
Tejun Heo28b4c272012-04-01 12:09:56 -07004295
Ben Blumaae8aab2010-03-10 15:22:07 -08004296 /* Skip subsystems not present or not in this hierarchy */
4297 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004298 continue;
Tejun Heo28b4c272012-04-01 12:09:56 -07004299
Paul Menagebd89aab2007-10-18 23:40:44 -07004300 css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004301 /*
4302 * When called from check_for_release() it's possible
Paul Menage81a6a5c2007-10-18 23:39:38 -07004303 * that by this point the cgroup has been removed
4304 * and the css deleted. But a false-positive doesn't
4305 * matter, since it can only happen if the cgroup
4306 * has been deleted and hence no longer needs the
Tejun Heo28b4c272012-04-01 12:09:56 -07004307 * release agent to be called anyway.
4308 */
4309 if (css && css_refcnt(css) > 1)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004310 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004311 }
4312 return 0;
4313}
4314
Tejun Heo42809dd2012-11-19 08:13:37 -08004315static int cgroup_destroy_locked(struct cgroup *cgrp)
4316 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004317{
Tejun Heo42809dd2012-11-19 08:13:37 -08004318 struct dentry *d = cgrp->dentry;
4319 struct cgroup *parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004320 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004321 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004322 struct cgroup_subsys *ss;
Greg Thelen205a8722012-11-28 13:50:44 -08004323 LIST_HEAD(tmp_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004324
Tejun Heo42809dd2012-11-19 08:13:37 -08004325 lockdep_assert_held(&d->d_inode->i_mutex);
4326 lockdep_assert_held(&cgroup_mutex);
4327
4328 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004330
Tejun Heo1a90dd52012-11-05 09:16:59 -08004331 /*
4332 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4333 * removed. This makes future css_tryget() and child creation
4334 * attempts fail thus maintaining the removal conditions verified
4335 * above.
4336 */
Tejun Heoed9577932012-11-05 09:16:58 -08004337 for_each_subsys(cgrp->root, ss) {
4338 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4339
4340 WARN_ON(atomic_read(&css->refcnt) < 0);
4341 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004342 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004343 set_bit(CGRP_REMOVED, &cgrp->flags);
4344
Tejun Heoa31f2d32012-11-19 08:13:37 -08004345 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004346 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004347 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004348
4349 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004350 * Put all the base refs. Each css holds an extra reference to the
4351 * cgroup's dentry and cgroup removal proceeds regardless of css
4352 * refs. On the last put of each css, whenever that may be, the
4353 * extra dentry ref is put so that dentry destruction happens only
4354 * after all css's are released.
4355 */
Tejun Heoe9316082012-11-05 09:16:58 -08004356 for_each_subsys(cgrp->root, ss)
4357 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004358
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004359 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004360 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004361 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004362 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004363
Paul Menage999cd8a2009-01-07 18:08:36 -08004364 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004365 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004366 list_del_init(&cgrp->allcg_node);
4367
Tejun Heo42809dd2012-11-19 08:13:37 -08004368 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004369 cgroup_d_remove_dir(d);
4370 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004371
Paul Menagebd89aab2007-10-18 23:40:44 -07004372 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004373 check_for_release(parent);
4374
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004375 /*
4376 * Unregister events and notify userspace.
4377 * Notify userspace about cgroup removing only after rmdir of cgroup
Greg Thelen205a8722012-11-28 13:50:44 -08004378 * directory to avoid race between userspace and kernelspace. Use
4379 * a temporary list to avoid a deadlock with cgroup_event_wake(). Since
4380 * cgroup_event_wake() is called with the wait queue head locked,
4381 * remove_wait_queue() cannot be called while holding event_list_lock.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004382 */
4383 spin_lock(&cgrp->event_list_lock);
Greg Thelen205a8722012-11-28 13:50:44 -08004384 list_splice_init(&cgrp->event_list, &tmp_list);
4385 spin_unlock(&cgrp->event_list_lock);
4386 list_for_each_entry_safe(event, tmp, &tmp_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004387 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004388 remove_wait_queue(event->wqh, &event->wait);
4389 eventfd_signal(event->eventfd, 1);
4390 schedule_work(&event->remove);
4391 }
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004392
Paul Menageddbcc7e2007-10-18 23:39:30 -07004393 return 0;
4394}
4395
Tejun Heo42809dd2012-11-19 08:13:37 -08004396static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4397{
4398 int ret;
4399
4400 mutex_lock(&cgroup_mutex);
4401 ret = cgroup_destroy_locked(dentry->d_fsdata);
4402 mutex_unlock(&cgroup_mutex);
4403
4404 return ret;
4405}
4406
Tejun Heo8e3f6542012-04-01 12:09:55 -07004407static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4408{
4409 INIT_LIST_HEAD(&ss->cftsets);
4410
4411 /*
4412 * base_cftset is embedded in subsys itself, no need to worry about
4413 * deregistration.
4414 */
4415 if (ss->base_cftypes) {
4416 ss->base_cftset.cfts = ss->base_cftypes;
4417 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4418 }
4419}
4420
Li Zefan06a11922008-04-29 01:00:07 -07004421static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004422{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004423 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004424
4425 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004426
Tejun Heo648bb562012-11-19 08:13:36 -08004427 mutex_lock(&cgroup_mutex);
4428
Tejun Heo8e3f6542012-04-01 12:09:55 -07004429 /* init base cftset */
4430 cgroup_init_cftsets(ss);
4431
Paul Menageddbcc7e2007-10-18 23:39:30 -07004432 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004433 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004434 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004435 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004436 /* We don't handle early failures gracefully */
4437 BUG_ON(IS_ERR(css));
4438 init_cgroup_css(css, ss, dummytop);
4439
Li Zefane8d55fd2008-04-29 01:00:13 -07004440 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004441 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004442 * newly registered, all tasks and hence the
4443 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004444 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004445
4446 need_forkexit_callback |= ss->fork || ss->exit;
4447
Li Zefane8d55fd2008-04-29 01:00:13 -07004448 /* At system boot, before all subsystems have been
4449 * registered, no tasks have been forked, so we don't
4450 * need to invoke fork callbacks here. */
4451 BUG_ON(!list_empty(&init_task.tasks));
4452
Paul Menageddbcc7e2007-10-18 23:39:30 -07004453 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004454 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004455
Tejun Heo648bb562012-11-19 08:13:36 -08004456 mutex_unlock(&cgroup_mutex);
4457
Ben Blume6a11052010-03-10 15:22:09 -08004458 /* this function shouldn't be used with modular subsystems, since they
4459 * need to register a subsys_id, among other things */
4460 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004461}
4462
4463/**
Ben Blume6a11052010-03-10 15:22:09 -08004464 * cgroup_load_subsys: load and register a modular subsystem at runtime
4465 * @ss: the subsystem to load
4466 *
4467 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004468 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004469 * up for use. If the subsystem is built-in anyway, work is delegated to the
4470 * simpler cgroup_init_subsys.
4471 */
4472int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4473{
Ben Blume6a11052010-03-10 15:22:09 -08004474 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004475 int i, ret;
Li Zefan0ac801f2013-01-10 11:49:27 +08004476 struct hlist_node *node, *tmp;
4477 struct css_set *cg;
4478 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004479
4480 /* check name and function validity */
4481 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004482 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004483 return -EINVAL;
4484
4485 /*
4486 * we don't support callbacks in modular subsystems. this check is
4487 * before the ss->module check for consistency; a subsystem that could
4488 * be a module should still have no callbacks even if the user isn't
4489 * compiling it as one.
4490 */
4491 if (ss->fork || ss->exit)
4492 return -EINVAL;
4493
4494 /*
4495 * an optionally modular subsystem is built-in: we want to do nothing,
4496 * since cgroup_init_subsys will have already taken care of it.
4497 */
4498 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004499 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004500 BUG_ON(subsys[ss->subsys_id] != ss);
4501 return 0;
4502 }
4503
Tejun Heo8e3f6542012-04-01 12:09:55 -07004504 /* init base cftset */
4505 cgroup_init_cftsets(ss);
4506
Ben Blume6a11052010-03-10 15:22:09 -08004507 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004508 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004509
4510 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004511 * no ss->css_alloc seems to need anything important in the ss
4512 * struct, so this can happen first (i.e. before the rootnode
4513 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004514 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004515 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004516 if (IS_ERR(css)) {
4517 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004518 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004519 mutex_unlock(&cgroup_mutex);
4520 return PTR_ERR(css);
4521 }
4522
4523 list_add(&ss->sibling, &rootnode.subsys_list);
4524 ss->root = &rootnode;
4525
4526 /* our new subsystem will be attached to the dummy hierarchy. */
4527 init_cgroup_css(css, ss, dummytop);
4528 /* init_idr must be after init_cgroup_css because it sets css->id. */
4529 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004530 ret = cgroup_init_idr(ss, css);
4531 if (ret)
4532 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004533 }
4534
4535 /*
4536 * Now we need to entangle the css into the existing css_sets. unlike
4537 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4538 * will need a new pointer to it; done by iterating the css_set_table.
4539 * furthermore, modifying the existing css_sets will corrupt the hash
4540 * table state, so each changed css_set will need its hash recomputed.
4541 * this is all done under the css_set_lock.
4542 */
4543 write_lock(&css_set_lock);
Li Zefan0ac801f2013-01-10 11:49:27 +08004544 hash_for_each_safe(css_set_table, i, node, tmp, cg, hlist) {
4545 /* skip entries that we already rehashed */
4546 if (cg->subsys[ss->subsys_id])
4547 continue;
4548 /* remove existing entry */
4549 hash_del(&cg->hlist);
4550 /* set new value */
4551 cg->subsys[ss->subsys_id] = css;
4552 /* recompute hash and restore entry */
4553 key = css_set_hash(cg->subsys);
4554 hash_add(css_set_table, node, key);
Ben Blume6a11052010-03-10 15:22:09 -08004555 }
4556 write_unlock(&css_set_lock);
4557
Ben Blume6a11052010-03-10 15:22:09 -08004558 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004559 ret = online_css(ss, dummytop);
4560 if (ret)
4561 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004562
Ben Blume6a11052010-03-10 15:22:09 -08004563 /* success! */
4564 mutex_unlock(&cgroup_mutex);
4565 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004566
4567err_unload:
4568 mutex_unlock(&cgroup_mutex);
4569 /* @ss can't be mounted here as try_module_get() would fail */
4570 cgroup_unload_subsys(ss);
4571 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004572}
4573EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4574
4575/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004576 * cgroup_unload_subsys: unload a modular subsystem
4577 * @ss: the subsystem to unload
4578 *
4579 * This function should be called in a modular subsystem's exitcall. When this
4580 * function is invoked, the refcount on the subsystem's module will be 0, so
4581 * the subsystem will not be attached to any hierarchy.
4582 */
4583void cgroup_unload_subsys(struct cgroup_subsys *ss)
4584{
4585 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004586
4587 BUG_ON(ss->module == NULL);
4588
4589 /*
4590 * we shouldn't be called if the subsystem is in use, and the use of
4591 * try_module_get in parse_cgroupfs_options should ensure that it
4592 * doesn't start being used while we're killing it off.
4593 */
4594 BUG_ON(ss->root != &rootnode);
4595
4596 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004597
Tejun Heoa31f2d32012-11-19 08:13:37 -08004598 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004599 ss->active = 0;
4600
4601 if (ss->use_id) {
4602 idr_remove_all(&ss->idr);
4603 idr_destroy(&ss->idr);
4604 }
4605
Ben Blumcf5d5942010-03-10 15:22:09 -08004606 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004607 subsys[ss->subsys_id] = NULL;
4608
4609 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004610 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004611
4612 /*
4613 * disentangle the css from all css_sets attached to the dummytop. as
4614 * in loading, we need to pay our respects to the hashtable gods.
4615 */
4616 write_lock(&css_set_lock);
4617 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4618 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004619 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004620
Li Zefan0ac801f2013-01-10 11:49:27 +08004621 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004622 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004623 key = css_set_hash(cg->subsys);
4624 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004625 }
4626 write_unlock(&css_set_lock);
4627
4628 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004629 * remove subsystem's css from the dummytop and free it - need to
4630 * free before marking as null because ss->css_free needs the
4631 * cgrp->subsys pointer to find their state. note that this also
4632 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004633 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004634 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004635 dummytop->subsys[ss->subsys_id] = NULL;
4636
4637 mutex_unlock(&cgroup_mutex);
4638}
4639EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4640
4641/**
Li Zefana043e3b2008-02-23 15:24:09 -08004642 * cgroup_init_early - cgroup initialization at system boot
4643 *
4644 * Initialize cgroups at system boot, and initialize any
4645 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004646 */
4647int __init cgroup_init_early(void)
4648{
4649 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004650 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004651 INIT_LIST_HEAD(&init_css_set.cg_links);
4652 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004653 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004654 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004655 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004656 root_count = 1;
4657 init_task.cgroups = &init_css_set;
4658
4659 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004660 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004661 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004662 &rootnode.top_cgroup.css_sets);
4663 list_add(&init_css_set_link.cg_link_list,
4664 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004665
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004666 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004667 struct cgroup_subsys *ss = subsys[i];
4668
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004669 /* at bootup time, we don't worry about modular subsystems */
4670 if (!ss || ss->module)
4671 continue;
4672
Paul Menageddbcc7e2007-10-18 23:39:30 -07004673 BUG_ON(!ss->name);
4674 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004675 BUG_ON(!ss->css_alloc);
4676 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004677 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004678 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004679 ss->name, ss->subsys_id);
4680 BUG();
4681 }
4682
4683 if (ss->early_init)
4684 cgroup_init_subsys(ss);
4685 }
4686 return 0;
4687}
4688
4689/**
Li Zefana043e3b2008-02-23 15:24:09 -08004690 * cgroup_init - cgroup initialization
4691 *
4692 * Register cgroup filesystem and /proc file, and initialize
4693 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004694 */
4695int __init cgroup_init(void)
4696{
4697 int err;
4698 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004699 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004700
4701 err = bdi_init(&cgroup_backing_dev_info);
4702 if (err)
4703 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004704
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004705 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004706 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004707
4708 /* at bootup time, we don't worry about modular subsystems */
4709 if (!ss || ss->module)
4710 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004711 if (!ss->early_init)
4712 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004713 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004714 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004715 }
4716
Li Zefan472b1052008-04-29 01:00:11 -07004717 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004718 key = css_set_hash(init_css_set.subsys);
4719 hash_add(css_set_table, &init_css_set.hlist, key);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004720 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004721
4722 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4723 if (!cgroup_kobj) {
4724 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004725 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004726 }
4727
4728 err = register_filesystem(&cgroup_fs_type);
4729 if (err < 0) {
4730 kobject_put(cgroup_kobj);
4731 goto out;
4732 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004733
Li Zefan46ae2202008-04-29 01:00:08 -07004734 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004735
Paul Menageddbcc7e2007-10-18 23:39:30 -07004736out:
Paul Menagea4243162007-10-18 23:39:35 -07004737 if (err)
4738 bdi_destroy(&cgroup_backing_dev_info);
4739
Paul Menageddbcc7e2007-10-18 23:39:30 -07004740 return err;
4741}
Paul Menageb4f48b62007-10-18 23:39:33 -07004742
Paul Menagea4243162007-10-18 23:39:35 -07004743/*
4744 * proc_cgroup_show()
4745 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4746 * - Used for /proc/<pid>/cgroup.
4747 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4748 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004749 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004750 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4751 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4752 * cgroup to top_cgroup.
4753 */
4754
4755/* TODO: Use a proper seq_file iterator */
4756static int proc_cgroup_show(struct seq_file *m, void *v)
4757{
4758 struct pid *pid;
4759 struct task_struct *tsk;
4760 char *buf;
4761 int retval;
4762 struct cgroupfs_root *root;
4763
4764 retval = -ENOMEM;
4765 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4766 if (!buf)
4767 goto out;
4768
4769 retval = -ESRCH;
4770 pid = m->private;
4771 tsk = get_pid_task(pid, PIDTYPE_PID);
4772 if (!tsk)
4773 goto out_free;
4774
4775 retval = 0;
4776
4777 mutex_lock(&cgroup_mutex);
4778
Li Zefane5f6a862009-01-07 18:07:41 -08004779 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004780 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004781 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004782 int count = 0;
4783
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004784 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004785 for_each_subsys(root, ss)
4786 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004787 if (strlen(root->name))
4788 seq_printf(m, "%sname=%s", count ? "," : "",
4789 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004790 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004791 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004792 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004793 if (retval < 0)
4794 goto out_unlock;
4795 seq_puts(m, buf);
4796 seq_putc(m, '\n');
4797 }
4798
4799out_unlock:
4800 mutex_unlock(&cgroup_mutex);
4801 put_task_struct(tsk);
4802out_free:
4803 kfree(buf);
4804out:
4805 return retval;
4806}
4807
4808static int cgroup_open(struct inode *inode, struct file *file)
4809{
4810 struct pid *pid = PROC_I(inode)->pid;
4811 return single_open(file, proc_cgroup_show, pid);
4812}
4813
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004814const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004815 .open = cgroup_open,
4816 .read = seq_read,
4817 .llseek = seq_lseek,
4818 .release = single_release,
4819};
4820
4821/* Display information about each subsystem and each hierarchy */
4822static int proc_cgroupstats_show(struct seq_file *m, void *v)
4823{
4824 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004825
Paul Menage8bab8dd2008-04-04 14:29:57 -07004826 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004827 /*
4828 * ideally we don't want subsystems moving around while we do this.
4829 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4830 * subsys/hierarchy state.
4831 */
Paul Menagea4243162007-10-18 23:39:35 -07004832 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004833 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4834 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004835 if (ss == NULL)
4836 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004837 seq_printf(m, "%s\t%d\t%d\t%d\n",
4838 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004839 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004840 }
4841 mutex_unlock(&cgroup_mutex);
4842 return 0;
4843}
4844
4845static int cgroupstats_open(struct inode *inode, struct file *file)
4846{
Al Viro9dce07f2008-03-29 03:07:28 +00004847 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004848}
4849
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004850static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004851 .open = cgroupstats_open,
4852 .read = seq_read,
4853 .llseek = seq_lseek,
4854 .release = single_release,
4855};
4856
Paul Menageb4f48b62007-10-18 23:39:33 -07004857/**
4858 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004859 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004860 *
4861 * Description: A task inherits its parent's cgroup at fork().
4862 *
4863 * A pointer to the shared css_set was automatically copied in
4864 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004865 * it was not made under the protection of RCU or cgroup_mutex, so
4866 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4867 * have already changed current->cgroups, allowing the previously
4868 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004869 *
4870 * At the point that cgroup_fork() is called, 'current' is the parent
4871 * task, and the passed argument 'child' points to the child task.
4872 */
4873void cgroup_fork(struct task_struct *child)
4874{
Tejun Heo9bb71302012-10-18 17:52:07 -07004875 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004876 child->cgroups = current->cgroups;
4877 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004878 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004879 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004880}
4881
4882/**
Li Zefana043e3b2008-02-23 15:24:09 -08004883 * cgroup_post_fork - called on a new task after adding it to the task list
4884 * @child: the task in question
4885 *
Tejun Heo5edee612012-10-16 15:03:14 -07004886 * Adds the task to the list running through its css_set if necessary and
4887 * call the subsystem fork() callbacks. Has to be after the task is
4888 * visible on the task list in case we race with the first call to
4889 * cgroup_iter_start() - to guarantee that the new task ends up on its
4890 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004891 */
Paul Menage817929e2007-10-18 23:39:36 -07004892void cgroup_post_fork(struct task_struct *child)
4893{
Tejun Heo5edee612012-10-16 15:03:14 -07004894 int i;
4895
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004896 /*
4897 * use_task_css_set_links is set to 1 before we walk the tasklist
4898 * under the tasklist_lock and we read it here after we added the child
4899 * to the tasklist under the tasklist_lock as well. If the child wasn't
4900 * yet in the tasklist when we walked through it from
4901 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4902 * should be visible now due to the paired locking and barriers implied
4903 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4904 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4905 * lock on fork.
4906 */
Paul Menage817929e2007-10-18 23:39:36 -07004907 if (use_task_css_set_links) {
4908 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004909 task_lock(child);
4910 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004911 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004912 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004913 write_unlock(&css_set_lock);
4914 }
Tejun Heo5edee612012-10-16 15:03:14 -07004915
4916 /*
4917 * Call ss->fork(). This must happen after @child is linked on
4918 * css_set; otherwise, @child might change state between ->fork()
4919 * and addition to css_set.
4920 */
4921 if (need_forkexit_callback) {
4922 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4923 struct cgroup_subsys *ss = subsys[i];
4924
4925 /*
4926 * fork/exit callbacks are supported only for
4927 * builtin subsystems and we don't need further
4928 * synchronization as they never go away.
4929 */
4930 if (!ss || ss->module)
4931 continue;
4932
4933 if (ss->fork)
4934 ss->fork(child);
4935 }
4936 }
Paul Menage817929e2007-10-18 23:39:36 -07004937}
Tejun Heo5edee612012-10-16 15:03:14 -07004938
Paul Menage817929e2007-10-18 23:39:36 -07004939/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004940 * cgroup_exit - detach cgroup from exiting task
4941 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004942 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004943 *
4944 * Description: Detach cgroup from @tsk and release it.
4945 *
4946 * Note that cgroups marked notify_on_release force every task in
4947 * them to take the global cgroup_mutex mutex when exiting.
4948 * This could impact scaling on very large systems. Be reluctant to
4949 * use notify_on_release cgroups where very high task exit scaling
4950 * is required on large systems.
4951 *
4952 * the_top_cgroup_hack:
4953 *
4954 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4955 *
4956 * We call cgroup_exit() while the task is still competent to
4957 * handle notify_on_release(), then leave the task attached to the
4958 * root cgroup in each hierarchy for the remainder of its exit.
4959 *
4960 * To do this properly, we would increment the reference count on
4961 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4962 * code we would add a second cgroup function call, to drop that
4963 * reference. This would just create an unnecessary hot spot on
4964 * the top_cgroup reference count, to no avail.
4965 *
4966 * Normally, holding a reference to a cgroup without bumping its
4967 * count is unsafe. The cgroup could go away, or someone could
4968 * attach us to a different cgroup, decrementing the count on
4969 * the first cgroup that we never incremented. But in this case,
4970 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004971 * which wards off any cgroup_attach_task() attempts, or task is a failed
4972 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004973 */
4974void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4975{
Paul Menage817929e2007-10-18 23:39:36 -07004976 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004977 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004978
4979 /*
4980 * Unlink from the css_set task list if necessary.
4981 * Optimistically check cg_list before taking
4982 * css_set_lock
4983 */
4984 if (!list_empty(&tsk->cg_list)) {
4985 write_lock(&css_set_lock);
4986 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004987 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004988 write_unlock(&css_set_lock);
4989 }
4990
Paul Menageb4f48b62007-10-18 23:39:33 -07004991 /* Reassign the task to the init_css_set. */
4992 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004993 cg = tsk->cgroups;
4994 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004995
4996 if (run_callbacks && need_forkexit_callback) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004997 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004998 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004999
5000 /* modular subsystems can't use callbacks */
5001 if (!ss || ss->module)
5002 continue;
5003
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005004 if (ss->exit) {
5005 struct cgroup *old_cgrp =
5006 rcu_dereference_raw(cg->subsys[i])->cgroup;
5007 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08005008 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005009 }
5010 }
5011 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005012 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005013
Li Zefanb5d646f2013-01-24 14:43:51 +08005014 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07005015}
Paul Menage697f4162007-10-18 23:39:34 -07005016
5017/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005018 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08005019 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005020 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08005021 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005022 * See if @cgrp is a descendant of @task's cgroup in the appropriate
5023 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07005024 *
5025 * If we are sending in dummytop, then presumably we are creating
5026 * the top cgroup in the subsystem.
5027 *
5028 * Called only by the ns (nsproxy) cgroup.
5029 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005030int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07005031{
5032 int ret;
5033 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07005034
Paul Menagebd89aab2007-10-18 23:40:44 -07005035 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07005036 return 1;
5037
Paul Menage7717f7b2009-09-23 15:56:22 -07005038 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005039 while (cgrp != target && cgrp!= cgrp->top_cgroup)
5040 cgrp = cgrp->parent;
5041 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07005042 return ret;
5043}
Paul Menage81a6a5c2007-10-18 23:39:38 -07005044
Paul Menagebd89aab2007-10-18 23:40:44 -07005045static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005046{
5047 /* All of these checks rely on RCU to keep the cgroup
5048 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07005049 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
5050 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07005051 /* Control Group is currently removeable. If it's not
5052 * already queued for a userspace notification, queue
5053 * it now */
5054 int need_schedule_work = 0;
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005055 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07005056 if (!cgroup_is_removed(cgrp) &&
5057 list_empty(&cgrp->release_list)) {
5058 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005059 need_schedule_work = 1;
5060 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005061 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005062 if (need_schedule_work)
5063 schedule_work(&release_agent_work);
5064 }
5065}
5066
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005067/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005068bool __css_tryget(struct cgroup_subsys_state *css)
5069{
Tejun Heoe9316082012-11-05 09:16:58 -08005070 while (true) {
5071 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005072
Tejun Heoe9316082012-11-05 09:16:58 -08005073 v = css_refcnt(css);
5074 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5075 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005076 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005077 else if (t < 0)
5078 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005079 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005080 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005081}
5082EXPORT_SYMBOL_GPL(__css_tryget);
5083
5084/* Caller must verify that the css is not for root cgroup */
5085void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005086{
Paul Menagebd89aab2007-10-18 23:40:44 -07005087 struct cgroup *cgrp = css->cgroup;
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005088 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005089
Paul Menage81a6a5c2007-10-18 23:39:38 -07005090 rcu_read_lock();
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005091 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
5092
5093 switch (v) {
Tejun Heo48ddbe12012-04-01 12:09:56 -07005094 case 1:
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005095 if (notify_on_release(cgrp)) {
5096 set_bit(CGRP_RELEASABLE, &cgrp->flags);
5097 check_for_release(cgrp);
5098 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07005099 break;
5100 case 0:
Tejun Heoed9577932012-11-05 09:16:58 -08005101 schedule_work(&css->dput_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005102 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005103 }
5104 rcu_read_unlock();
5105}
Ben Blum67523c42010-03-10 15:22:11 -08005106EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005107
5108/*
5109 * Notify userspace when a cgroup is released, by running the
5110 * configured release agent with the name of the cgroup (path
5111 * relative to the root of cgroup file system) as the argument.
5112 *
5113 * Most likely, this user command will try to rmdir this cgroup.
5114 *
5115 * This races with the possibility that some other task will be
5116 * attached to this cgroup before it is removed, or that some other
5117 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5118 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5119 * unused, and this cgroup will be reprieved from its death sentence,
5120 * to continue to serve a useful existence. Next time it's released,
5121 * we will get notified again, if it still has 'notify_on_release' set.
5122 *
5123 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5124 * means only wait until the task is successfully execve()'d. The
5125 * separate release agent task is forked by call_usermodehelper(),
5126 * then control in this thread returns here, without waiting for the
5127 * release agent task. We don't bother to wait because the caller of
5128 * this routine has no use for the exit status of the release agent
5129 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005130 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005131static void cgroup_release_agent(struct work_struct *work)
5132{
5133 BUG_ON(work != &release_agent_work);
5134 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005135 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005136 while (!list_empty(&release_list)) {
5137 char *argv[3], *envp[3];
5138 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005139 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005140 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005141 struct cgroup,
5142 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005143 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005144 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005145 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005146 if (!pathbuf)
5147 goto continue_free;
5148 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5149 goto continue_free;
5150 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5151 if (!agentbuf)
5152 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005153
5154 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005155 argv[i++] = agentbuf;
5156 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005157 argv[i] = NULL;
5158
5159 i = 0;
5160 /* minimal command environment */
5161 envp[i++] = "HOME=/";
5162 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5163 envp[i] = NULL;
5164
5165 /* Drop the lock while we invoke the usermode helper,
5166 * since the exec could involve hitting disk and hence
5167 * be a slow process */
5168 mutex_unlock(&cgroup_mutex);
5169 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005170 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005171 continue_free:
5172 kfree(pathbuf);
5173 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005174 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005175 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005176 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005177 mutex_unlock(&cgroup_mutex);
5178}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005179
5180static int __init cgroup_disable(char *str)
5181{
5182 int i;
5183 char *token;
5184
5185 while ((token = strsep(&str, ",")) != NULL) {
5186 if (!*token)
5187 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005188 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005189 struct cgroup_subsys *ss = subsys[i];
5190
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005191 /*
5192 * cgroup_disable, being at boot time, can't
5193 * know about module subsystems, so we don't
5194 * worry about them.
5195 */
5196 if (!ss || ss->module)
5197 continue;
5198
Paul Menage8bab8dd2008-04-04 14:29:57 -07005199 if (!strcmp(token, ss->name)) {
5200 ss->disabled = 1;
5201 printk(KERN_INFO "Disabling %s control group"
5202 " subsystem\n", ss->name);
5203 break;
5204 }
5205 }
5206 }
5207 return 1;
5208}
5209__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005210
5211/*
5212 * Functons for CSS ID.
5213 */
5214
5215/*
5216 *To get ID other than 0, this should be called when !cgroup_is_removed().
5217 */
5218unsigned short css_id(struct cgroup_subsys_state *css)
5219{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005220 struct css_id *cssid;
5221
5222 /*
5223 * This css_id() can return correct value when somone has refcnt
5224 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5225 * it's unchanged until freed.
5226 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005227 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005228
5229 if (cssid)
5230 return cssid->id;
5231 return 0;
5232}
Ben Blum67523c42010-03-10 15:22:11 -08005233EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005234
5235unsigned short css_depth(struct cgroup_subsys_state *css)
5236{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005237 struct css_id *cssid;
5238
Tejun Heo28b4c272012-04-01 12:09:56 -07005239 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005240
5241 if (cssid)
5242 return cssid->depth;
5243 return 0;
5244}
Ben Blum67523c42010-03-10 15:22:11 -08005245EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005246
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005247/**
5248 * css_is_ancestor - test "root" css is an ancestor of "child"
5249 * @child: the css to be tested.
5250 * @root: the css supporsed to be an ancestor of the child.
5251 *
5252 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005253 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005254 * But, considering usual usage, the csses should be valid objects after test.
5255 * Assuming that the caller will do some action to the child if this returns
5256 * returns true, the caller must take "child";s reference count.
5257 * If "child" is valid object and this returns true, "root" is valid, too.
5258 */
5259
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005260bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005261 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005262{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005263 struct css_id *child_id;
5264 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005265
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005266 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005267 if (!child_id)
5268 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005269 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005270 if (!root_id)
5271 return false;
5272 if (child_id->depth < root_id->depth)
5273 return false;
5274 if (child_id->stack[root_id->depth] != root_id->id)
5275 return false;
5276 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005277}
5278
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005279void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5280{
5281 struct css_id *id = css->id;
5282 /* When this is called before css_id initialization, id can be NULL */
5283 if (!id)
5284 return;
5285
5286 BUG_ON(!ss->use_id);
5287
5288 rcu_assign_pointer(id->css, NULL);
5289 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005290 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005291 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005292 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005293 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005294}
Ben Blum67523c42010-03-10 15:22:11 -08005295EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005296
5297/*
5298 * This is called by init or create(). Then, calls to this function are
5299 * always serialized (By cgroup_mutex() at create()).
5300 */
5301
5302static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5303{
5304 struct css_id *newid;
5305 int myid, error, size;
5306
5307 BUG_ON(!ss->use_id);
5308
5309 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5310 newid = kzalloc(size, GFP_KERNEL);
5311 if (!newid)
5312 return ERR_PTR(-ENOMEM);
5313 /* get id */
5314 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
5315 error = -ENOMEM;
5316 goto err_out;
5317 }
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005318 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005319 /* Don't use 0. allocates an ID of 1-65535 */
5320 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005321 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005322
5323 /* Returns error when there are no free spaces for new ID.*/
5324 if (error) {
5325 error = -ENOSPC;
5326 goto err_out;
5327 }
5328 if (myid > CSS_ID_MAX)
5329 goto remove_idr;
5330
5331 newid->id = myid;
5332 newid->depth = depth;
5333 return newid;
5334remove_idr:
5335 error = -ENOSPC;
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005336 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005337 idr_remove(&ss->idr, myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005338 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005339err_out:
5340 kfree(newid);
5341 return ERR_PTR(error);
5342
5343}
5344
Ben Blume6a11052010-03-10 15:22:09 -08005345static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5346 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005347{
5348 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005349
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005350 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005351 idr_init(&ss->idr);
5352
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005353 newid = get_new_cssid(ss, 0);
5354 if (IS_ERR(newid))
5355 return PTR_ERR(newid);
5356
5357 newid->stack[0] = newid->id;
5358 newid->css = rootcss;
5359 rootcss->id = newid;
5360 return 0;
5361}
5362
5363static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5364 struct cgroup *child)
5365{
5366 int subsys_id, i, depth = 0;
5367 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005368 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005369
5370 subsys_id = ss->subsys_id;
5371 parent_css = parent->subsys[subsys_id];
5372 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005373 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005374 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005375
5376 child_id = get_new_cssid(ss, depth);
5377 if (IS_ERR(child_id))
5378 return PTR_ERR(child_id);
5379
5380 for (i = 0; i < depth; i++)
5381 child_id->stack[i] = parent_id->stack[i];
5382 child_id->stack[depth] = child_id->id;
5383 /*
5384 * child_id->css pointer will be set after this cgroup is available
5385 * see cgroup_populate_dir()
5386 */
5387 rcu_assign_pointer(child_css->id, child_id);
5388
5389 return 0;
5390}
5391
5392/**
5393 * css_lookup - lookup css by id
5394 * @ss: cgroup subsys to be looked into.
5395 * @id: the id
5396 *
5397 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5398 * NULL if not. Should be called under rcu_read_lock()
5399 */
5400struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5401{
5402 struct css_id *cssid = NULL;
5403
5404 BUG_ON(!ss->use_id);
5405 cssid = idr_find(&ss->idr, id);
5406
5407 if (unlikely(!cssid))
5408 return NULL;
5409
5410 return rcu_dereference(cssid->css);
5411}
Ben Blum67523c42010-03-10 15:22:11 -08005412EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005413
5414/**
5415 * css_get_next - lookup next cgroup under specified hierarchy.
5416 * @ss: pointer to subsystem
5417 * @id: current position of iteration.
5418 * @root: pointer to css. search tree under this.
5419 * @foundid: position of found object.
5420 *
5421 * Search next css under the specified hierarchy of rootid. Calling under
5422 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5423 */
5424struct cgroup_subsys_state *
5425css_get_next(struct cgroup_subsys *ss, int id,
5426 struct cgroup_subsys_state *root, int *foundid)
5427{
5428 struct cgroup_subsys_state *ret = NULL;
5429 struct css_id *tmp;
5430 int tmpid;
5431 int rootid = css_id(root);
5432 int depth = css_depth(root);
5433
5434 if (!rootid)
5435 return NULL;
5436
5437 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005438 WARN_ON_ONCE(!rcu_read_lock_held());
5439
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005440 /* fill start point for scan */
5441 tmpid = id;
5442 while (1) {
5443 /*
5444 * scan next entry from bitmap(tree), tmpid is updated after
5445 * idr_get_next().
5446 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005447 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005448 if (!tmp)
5449 break;
5450 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5451 ret = rcu_dereference(tmp->css);
5452 if (ret) {
5453 *foundid = tmpid;
5454 break;
5455 }
5456 }
5457 /* continue to scan from next id */
5458 tmpid = tmpid + 1;
5459 }
5460 return ret;
5461}
5462
Stephane Eraniane5d13672011-02-14 11:20:01 +02005463/*
5464 * get corresponding css from file open on cgroupfs directory
5465 */
5466struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5467{
5468 struct cgroup *cgrp;
5469 struct inode *inode;
5470 struct cgroup_subsys_state *css;
5471
5472 inode = f->f_dentry->d_inode;
5473 /* check in cgroup filesystem dir */
5474 if (inode->i_op != &cgroup_dir_inode_operations)
5475 return ERR_PTR(-EBADF);
5476
5477 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5478 return ERR_PTR(-EINVAL);
5479
5480 /* get cgroup */
5481 cgrp = __d_cgrp(f->f_dentry);
5482 css = cgrp->subsys[id];
5483 return css ? css : ERR_PTR(-ENOENT);
5484}
5485
Paul Menagefe693432009-09-23 15:56:20 -07005486#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005487static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005488{
5489 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5490
5491 if (!css)
5492 return ERR_PTR(-ENOMEM);
5493
5494 return css;
5495}
5496
Tejun Heo92fb9742012-11-19 08:13:38 -08005497static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005498{
5499 kfree(cont->subsys[debug_subsys_id]);
5500}
5501
5502static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5503{
5504 return atomic_read(&cont->count);
5505}
5506
5507static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5508{
5509 return cgroup_task_count(cont);
5510}
5511
5512static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5513{
5514 return (u64)(unsigned long)current->cgroups;
5515}
5516
5517static u64 current_css_set_refcount_read(struct cgroup *cont,
5518 struct cftype *cft)
5519{
5520 u64 count;
5521
5522 rcu_read_lock();
5523 count = atomic_read(&current->cgroups->refcount);
5524 rcu_read_unlock();
5525 return count;
5526}
5527
Paul Menage7717f7b2009-09-23 15:56:22 -07005528static int current_css_set_cg_links_read(struct cgroup *cont,
5529 struct cftype *cft,
5530 struct seq_file *seq)
5531{
5532 struct cg_cgroup_link *link;
5533 struct css_set *cg;
5534
5535 read_lock(&css_set_lock);
5536 rcu_read_lock();
5537 cg = rcu_dereference(current->cgroups);
5538 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5539 struct cgroup *c = link->cgrp;
5540 const char *name;
5541
5542 if (c->dentry)
5543 name = c->dentry->d_name.name;
5544 else
5545 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005546 seq_printf(seq, "Root %d group %s\n",
5547 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005548 }
5549 rcu_read_unlock();
5550 read_unlock(&css_set_lock);
5551 return 0;
5552}
5553
5554#define MAX_TASKS_SHOWN_PER_CSS 25
5555static int cgroup_css_links_read(struct cgroup *cont,
5556 struct cftype *cft,
5557 struct seq_file *seq)
5558{
5559 struct cg_cgroup_link *link;
5560
5561 read_lock(&css_set_lock);
5562 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5563 struct css_set *cg = link->cg;
5564 struct task_struct *task;
5565 int count = 0;
5566 seq_printf(seq, "css_set %p\n", cg);
5567 list_for_each_entry(task, &cg->tasks, cg_list) {
5568 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5569 seq_puts(seq, " ...\n");
5570 break;
5571 } else {
5572 seq_printf(seq, " task %d\n",
5573 task_pid_vnr(task));
5574 }
5575 }
5576 }
5577 read_unlock(&css_set_lock);
5578 return 0;
5579}
5580
Paul Menagefe693432009-09-23 15:56:20 -07005581static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5582{
5583 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5584}
5585
5586static struct cftype debug_files[] = {
5587 {
5588 .name = "cgroup_refcount",
5589 .read_u64 = cgroup_refcount_read,
5590 },
5591 {
5592 .name = "taskcount",
5593 .read_u64 = debug_taskcount_read,
5594 },
5595
5596 {
5597 .name = "current_css_set",
5598 .read_u64 = current_css_set_read,
5599 },
5600
5601 {
5602 .name = "current_css_set_refcount",
5603 .read_u64 = current_css_set_refcount_read,
5604 },
5605
5606 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005607 .name = "current_css_set_cg_links",
5608 .read_seq_string = current_css_set_cg_links_read,
5609 },
5610
5611 {
5612 .name = "cgroup_css_links",
5613 .read_seq_string = cgroup_css_links_read,
5614 },
5615
5616 {
Paul Menagefe693432009-09-23 15:56:20 -07005617 .name = "releasable",
5618 .read_u64 = releasable_read,
5619 },
Paul Menagefe693432009-09-23 15:56:20 -07005620
Tejun Heo4baf6e32012-04-01 12:09:55 -07005621 { } /* terminate */
5622};
Paul Menagefe693432009-09-23 15:56:20 -07005623
5624struct cgroup_subsys debug_subsys = {
5625 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005626 .css_alloc = debug_css_alloc,
5627 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005628 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005629 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005630};
5631#endif /* CONFIG_CGROUP_DEBUG */