blob: ba3e24a76daeccee7dcff9f94de6d1802fcd1014 [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>
Li Zefan081aa452013-03-13 09:17:09 +080062#include <linux/flex_array.h> /* used in cgroup_attach_task */
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 */
Tejun Heo22194492013-04-07 09:29:51 -070086#ifdef CONFIG_PROVE_RCU
87DEFINE_MUTEX(cgroup_mutex);
88EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
89#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070090static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070091#endif
92
Tejun Heoe25e2cb2011-12-12 18:12:21 -080093static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070094
Ben Blumaae8aab2010-03-10 15:22:07 -080095/*
96 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020097 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080098 * registered after that. The mutable section of this array is protected by
99 * cgroup_mutex.
100 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200101#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200102#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -0800103static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700104#include <linux/cgroup_subsys.h>
105};
106
Paul Menagec6d57f32009-09-23 15:56:19 -0700107#define MAX_CGROUP_ROOT_NAMELEN 64
108
Paul Menageddbcc7e2007-10-18 23:39:30 -0700109/*
110 * A cgroupfs_root represents the root of a cgroup hierarchy,
111 * and may be associated with a superblock to form an active
112 * hierarchy
113 */
114struct cgroupfs_root {
115 struct super_block *sb;
116
117 /*
118 * The bitmask of subsystems intended to be attached to this
119 * hierarchy
120 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400121 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700123 /* Unique id for this hierarchy. */
124 int hierarchy_id;
125
Paul Menageddbcc7e2007-10-18 23:39:30 -0700126 /* The bitmask of subsystems currently attached to this hierarchy */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400127 unsigned long actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700128
129 /* A list running through the attached subsystems */
130 struct list_head subsys_list;
131
132 /* The root cgroup for this hierarchy */
133 struct cgroup top_cgroup;
134
135 /* Tracks how many cgroups are currently defined in hierarchy.*/
136 int number_of_cgroups;
137
Li Zefane5f6a862009-01-07 18:07:41 -0800138 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700139 struct list_head root_list;
140
Tejun Heob0ca5a82012-04-01 12:09:54 -0700141 /* All cgroups on this root, cgroup_mutex protected */
142 struct list_head allcg_list;
143
Paul Menageddbcc7e2007-10-18 23:39:30 -0700144 /* Hierarchy-specific flags */
145 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700146
Tejun Heo0a950f62012-11-19 09:02:12 -0800147 /* IDs for cgroups in this hierarchy */
148 struct ida cgroup_ida;
149
Paul Menagee788e062008-07-25 01:46:59 -0700150 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700151 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700152
153 /* The name for this hierarchy - may be empty */
154 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700155};
156
Paul Menageddbcc7e2007-10-18 23:39:30 -0700157/*
158 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
159 * subsystems that are otherwise unattached - it never has more than a
160 * single cgroup, and all tasks are part of that cgroup.
161 */
162static struct cgroupfs_root rootnode;
163
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700164/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700165 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
166 */
167struct cfent {
168 struct list_head node;
169 struct dentry *dentry;
170 struct cftype *type;
171};
172
173/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700174 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
175 * cgroup_subsys->use_id != 0.
176 */
177#define CSS_ID_MAX (65535)
178struct css_id {
179 /*
180 * The css to which this ID points. This pointer is set to valid value
181 * after cgroup is populated. If cgroup is removed, this will be NULL.
182 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800183 * is called after synchronize_rcu(). But for safe use, css_tryget()
184 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700185 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100186 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700187 /*
188 * ID of this css.
189 */
190 unsigned short id;
191 /*
192 * Depth in hierarchy which this ID belongs to.
193 */
194 unsigned short depth;
195 /*
196 * ID is freed by RCU. (and lookup routine is RCU safe.)
197 */
198 struct rcu_head rcu_head;
199 /*
200 * Hierarchy of CSS ID belongs to.
201 */
202 unsigned short stack[0]; /* Array of Length (depth+1) */
203};
204
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800205/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300206 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800207 */
208struct cgroup_event {
209 /*
210 * Cgroup which the event belongs to.
211 */
212 struct cgroup *cgrp;
213 /*
214 * Control file which the event associated.
215 */
216 struct cftype *cft;
217 /*
218 * eventfd to signal userspace about the event.
219 */
220 struct eventfd_ctx *eventfd;
221 /*
222 * Each of these stored in a list by the cgroup.
223 */
224 struct list_head list;
225 /*
226 * All fields below needed to unregister event when
227 * userspace closes eventfd.
228 */
229 poll_table pt;
230 wait_queue_head_t *wqh;
231 wait_queue_t wait;
232 struct work_struct remove;
233};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700234
Paul Menageddbcc7e2007-10-18 23:39:30 -0700235/* The list of hierarchy roots */
236
237static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700238static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700239
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700240static DEFINE_IDA(hierarchy_ida);
241static int next_hierarchy_id;
242static DEFINE_SPINLOCK(hierarchy_id_lock);
243
Paul Menageddbcc7e2007-10-18 23:39:30 -0700244/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
245#define dummytop (&rootnode.top_cgroup)
246
Li Zefan65dff752013-03-01 15:01:56 +0800247static struct cgroup_name root_cgroup_name = { .name = "/" };
248
Paul Menageddbcc7e2007-10-18 23:39:30 -0700249/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800250 * check for fork/exit handlers to call. This avoids us having to do
251 * extra work in the fork/exit path if none of the subsystems need to
252 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700253 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700254static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700255
Tejun Heo42809dd2012-11-19 08:13:37 -0800256static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800257static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
258 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800259
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700260static int css_unbias_refcnt(int refcnt)
261{
262 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
263}
264
Tejun Heo28b4c272012-04-01 12:09:56 -0700265/* the current nr of refs, always >= 0 whether @css is deactivated or not */
266static int css_refcnt(struct cgroup_subsys_state *css)
267{
268 int v = atomic_read(&css->refcnt);
269
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700270 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700271}
272
Paul Menageddbcc7e2007-10-18 23:39:30 -0700273/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700274inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700275{
Paul Menagebd89aab2007-10-18 23:40:44 -0700276 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700277}
278
279/* bits in struct cgroupfs_root flags field */
280enum {
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400281 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
282 ROOT_XATTR, /* supports extended attributes */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700283};
284
Adrian Bunke9685a02008-02-07 00:13:46 -0800285static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700286{
287 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700288 (1 << CGRP_RELEASABLE) |
289 (1 << CGRP_NOTIFY_ON_RELEASE);
290 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700291}
292
Adrian Bunke9685a02008-02-07 00:13:46 -0800293static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700294{
Paul Menagebd89aab2007-10-18 23:40:44 -0700295 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700296}
297
Paul Menageddbcc7e2007-10-18 23:39:30 -0700298/*
299 * for_each_subsys() allows you to iterate on each subsystem attached to
300 * an active hierarchy
301 */
302#define for_each_subsys(_root, _ss) \
303list_for_each_entry(_ss, &_root->subsys_list, sibling)
304
Li Zefane5f6a862009-01-07 18:07:41 -0800305/* for_each_active_root() allows you to iterate across the active hierarchies */
306#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700307list_for_each_entry(_root, &roots, root_list)
308
Tejun Heof6ea9372012-04-01 12:09:55 -0700309static inline struct cgroup *__d_cgrp(struct dentry *dentry)
310{
311 return dentry->d_fsdata;
312}
313
Tejun Heo05ef1d72012-04-01 12:09:56 -0700314static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700315{
316 return dentry->d_fsdata;
317}
318
Tejun Heo05ef1d72012-04-01 12:09:56 -0700319static inline struct cftype *__d_cft(struct dentry *dentry)
320{
321 return __d_cfe(dentry)->type;
322}
323
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700324/**
325 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
326 * @cgrp: the cgroup to be checked for liveness
327 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700328 * On success, returns true; the mutex should be later unlocked. On
329 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700330 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700331static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700332{
333 mutex_lock(&cgroup_mutex);
334 if (cgroup_is_removed(cgrp)) {
335 mutex_unlock(&cgroup_mutex);
336 return false;
337 }
338 return true;
339}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700340
Paul Menage81a6a5c2007-10-18 23:39:38 -0700341/* the list of cgroups eligible for automatic release. Protected by
342 * release_list_lock */
343static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200344static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700345static void cgroup_release_agent(struct work_struct *work);
346static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700347static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700348
Paul Menage817929e2007-10-18 23:39:36 -0700349/* Link structure for associating css_set objects with cgroups */
350struct cg_cgroup_link {
351 /*
352 * List running through cg_cgroup_links associated with a
353 * cgroup, anchored on cgroup->css_sets
354 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700355 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700356 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700357 /*
358 * List running through cg_cgroup_links pointing at a
359 * single css_set object, anchored on css_set->cg_links
360 */
361 struct list_head cg_link_list;
362 struct css_set *cg;
363};
364
365/* The default css_set - used by init and its children prior to any
366 * hierarchies being mounted. It contains a pointer to the root state
367 * for each subsystem. Also used to anchor the list of css_sets. Not
368 * reference-counted, to improve performance when child cgroups
369 * haven't been created.
370 */
371
372static struct css_set init_css_set;
373static struct cg_cgroup_link init_css_set_link;
374
Ben Blume6a11052010-03-10 15:22:09 -0800375static int cgroup_init_idr(struct cgroup_subsys *ss,
376 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700377
Paul Menage817929e2007-10-18 23:39:36 -0700378/* css_set_lock protects the list of css_set objects, and the
379 * chain of tasks off each css_set. Nests outside task->alloc_lock
380 * due to cgroup_iter_start() */
381static DEFINE_RWLOCK(css_set_lock);
382static int css_set_count;
383
Paul Menage7717f7b2009-09-23 15:56:22 -0700384/*
385 * hash table for cgroup groups. This improves the performance to find
386 * an existing css_set. This hash doesn't (currently) take into
387 * account cgroups in empty hierarchies.
388 */
Li Zefan472b1052008-04-29 01:00:11 -0700389#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800390static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700391
Li Zefan0ac801f2013-01-10 11:49:27 +0800392static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700393{
394 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800395 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700396
397 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800398 key += (unsigned long)css[i];
399 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700400
Li Zefan0ac801f2013-01-10 11:49:27 +0800401 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700402}
403
Paul Menage817929e2007-10-18 23:39:36 -0700404/* We don't maintain the lists running through each css_set to its
405 * task until after the first call to cgroup_iter_start(). This
406 * reduces the fork()/exit() overhead for people who have cgroups
407 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700408static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700409
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700410static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700411{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700412 struct cg_cgroup_link *link;
413 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700414 /*
415 * Ensure that the refcount doesn't hit zero while any readers
416 * can see it. Similar to atomic_dec_and_lock(), but for an
417 * rwlock
418 */
419 if (atomic_add_unless(&cg->refcount, -1, 1))
420 return;
421 write_lock(&css_set_lock);
422 if (!atomic_dec_and_test(&cg->refcount)) {
423 write_unlock(&css_set_lock);
424 return;
425 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700426
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700427 /* This css_set is dead. unlink it and release cgroup refcounts */
Li Zefan0ac801f2013-01-10 11:49:27 +0800428 hash_del(&cg->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700429 css_set_count--;
430
431 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
432 cg_link_list) {
433 struct cgroup *cgrp = link->cgrp;
434 list_del(&link->cg_link_list);
435 list_del(&link->cgrp_link_list);
Li Zefan71b57072013-01-24 14:43:28 +0800436
437 /*
438 * We may not be holding cgroup_mutex, and if cgrp->count is
439 * dropped to 0 the cgroup can be destroyed at any time, hence
440 * rcu_read_lock is used to keep it alive.
441 */
442 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -0700443 if (atomic_dec_and_test(&cgrp->count) &&
444 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700445 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700446 set_bit(CGRP_RELEASABLE, &cgrp->flags);
447 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700448 }
Li Zefan71b57072013-01-24 14:43:28 +0800449 rcu_read_unlock();
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700450
451 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700452 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700453
454 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800455 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700456}
457
458/*
459 * refcounted get/put for css_set objects
460 */
461static inline void get_css_set(struct css_set *cg)
462{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700463 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700464}
465
466static inline void put_css_set(struct css_set *cg)
467{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700468 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700469}
470
Paul Menage81a6a5c2007-10-18 23:39:38 -0700471static inline void put_css_set_taskexit(struct css_set *cg)
472{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700473 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700474}
475
Paul Menage817929e2007-10-18 23:39:36 -0700476/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700477 * compare_css_sets - helper function for find_existing_css_set().
478 * @cg: candidate css_set being tested
479 * @old_cg: existing css_set for a task
480 * @new_cgrp: cgroup that's being entered by the task
481 * @template: desired set of css pointers in css_set (pre-calculated)
482 *
483 * Returns true if "cg" matches "old_cg" except for the hierarchy
484 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
485 */
486static bool compare_css_sets(struct css_set *cg,
487 struct css_set *old_cg,
488 struct cgroup *new_cgrp,
489 struct cgroup_subsys_state *template[])
490{
491 struct list_head *l1, *l2;
492
493 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
494 /* Not all subsystems matched */
495 return false;
496 }
497
498 /*
499 * Compare cgroup pointers in order to distinguish between
500 * different cgroups in heirarchies with no subsystems. We
501 * could get by with just this check alone (and skip the
502 * memcmp above) but on most setups the memcmp check will
503 * avoid the need for this more expensive check on almost all
504 * candidates.
505 */
506
507 l1 = &cg->cg_links;
508 l2 = &old_cg->cg_links;
509 while (1) {
510 struct cg_cgroup_link *cgl1, *cgl2;
511 struct cgroup *cg1, *cg2;
512
513 l1 = l1->next;
514 l2 = l2->next;
515 /* See if we reached the end - both lists are equal length. */
516 if (l1 == &cg->cg_links) {
517 BUG_ON(l2 != &old_cg->cg_links);
518 break;
519 } else {
520 BUG_ON(l2 == &old_cg->cg_links);
521 }
522 /* Locate the cgroups associated with these links. */
523 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
524 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
525 cg1 = cgl1->cgrp;
526 cg2 = cgl2->cgrp;
527 /* Hierarchies should be linked in the same order. */
528 BUG_ON(cg1->root != cg2->root);
529
530 /*
531 * If this hierarchy is the hierarchy of the cgroup
532 * that's changing, then we need to check that this
533 * css_set points to the new cgroup; if it's any other
534 * hierarchy, then this css_set should point to the
535 * same cgroup as the old css_set.
536 */
537 if (cg1->root == new_cgrp->root) {
538 if (cg1 != new_cgrp)
539 return false;
540 } else {
541 if (cg1 != cg2)
542 return false;
543 }
544 }
545 return true;
546}
547
548/*
Paul Menage817929e2007-10-18 23:39:36 -0700549 * find_existing_css_set() is a helper for
550 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700551 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700552 *
553 * oldcg: the cgroup group that we're using before the cgroup
554 * transition
555 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700556 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700557 *
558 * template: location in which to build the desired set of subsystem
559 * state objects for the new cgroup group
560 */
Paul Menage817929e2007-10-18 23:39:36 -0700561static struct css_set *find_existing_css_set(
562 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700563 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700564 struct cgroup_subsys_state *template[])
565{
566 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700567 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700568 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800569 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700570
Ben Blumaae8aab2010-03-10 15:22:07 -0800571 /*
572 * Build the set of subsystem state objects that we want to see in the
573 * new css_set. while subsystems can change globally, the entries here
574 * won't change, so no need for locking.
575 */
Paul Menage817929e2007-10-18 23:39:36 -0700576 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400577 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700578 /* Subsystem is in this hierarchy. So we want
579 * the subsystem state from the new
580 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700581 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700582 } else {
583 /* Subsystem is not in this hierarchy, so we
584 * don't want to change the subsystem state */
585 template[i] = oldcg->subsys[i];
586 }
587 }
588
Li Zefan0ac801f2013-01-10 11:49:27 +0800589 key = css_set_hash(template);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800590 hash_for_each_possible(css_set_table, cg, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700591 if (!compare_css_sets(cg, oldcg, cgrp, template))
592 continue;
593
594 /* This css_set matches what we need */
595 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700596 }
Paul Menage817929e2007-10-18 23:39:36 -0700597
598 /* No existing cgroup group matched */
599 return NULL;
600}
601
Paul Menage817929e2007-10-18 23:39:36 -0700602static void free_cg_links(struct list_head *tmp)
603{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700604 struct cg_cgroup_link *link;
605 struct cg_cgroup_link *saved_link;
606
607 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700608 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700609 kfree(link);
610 }
611}
612
613/*
Li Zefan36553432008-07-29 22:33:19 -0700614 * allocate_cg_links() allocates "count" cg_cgroup_link structures
615 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
616 * success or a negative error
617 */
618static int allocate_cg_links(int count, struct list_head *tmp)
619{
620 struct cg_cgroup_link *link;
621 int i;
622 INIT_LIST_HEAD(tmp);
623 for (i = 0; i < count; i++) {
624 link = kmalloc(sizeof(*link), GFP_KERNEL);
625 if (!link) {
626 free_cg_links(tmp);
627 return -ENOMEM;
628 }
629 list_add(&link->cgrp_link_list, tmp);
630 }
631 return 0;
632}
633
Li Zefanc12f65d2009-01-07 18:07:42 -0800634/**
635 * link_css_set - a helper function to link a css_set to a cgroup
636 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
637 * @cg: the css_set to be linked
638 * @cgrp: the destination cgroup
639 */
640static void link_css_set(struct list_head *tmp_cg_links,
641 struct css_set *cg, struct cgroup *cgrp)
642{
643 struct cg_cgroup_link *link;
644
645 BUG_ON(list_empty(tmp_cg_links));
646 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
647 cgrp_link_list);
648 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700649 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700650 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800651 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700652 /*
653 * Always add links to the tail of the list so that the list
654 * is sorted by order of hierarchy creation
655 */
656 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800657}
658
Li Zefan36553432008-07-29 22:33:19 -0700659/*
Paul Menage817929e2007-10-18 23:39:36 -0700660 * find_css_set() takes an existing cgroup group and a
661 * cgroup object, and returns a css_set object that's
662 * equivalent to the old group, but with the given cgroup
663 * substituted into the appropriate hierarchy. Must be called with
664 * cgroup_mutex held
665 */
Paul Menage817929e2007-10-18 23:39:36 -0700666static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700667 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700668{
669 struct css_set *res;
670 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700671
672 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700673
Paul Menage7717f7b2009-09-23 15:56:22 -0700674 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800675 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700676
Paul Menage817929e2007-10-18 23:39:36 -0700677 /* First see if we already have a cgroup group that matches
678 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700679 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700680 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700681 if (res)
682 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700683 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700684
685 if (res)
686 return res;
687
688 res = kmalloc(sizeof(*res), GFP_KERNEL);
689 if (!res)
690 return NULL;
691
692 /* Allocate all the cg_cgroup_link objects that we'll need */
693 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
694 kfree(res);
695 return NULL;
696 }
697
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700698 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700699 INIT_LIST_HEAD(&res->cg_links);
700 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700701 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700702
703 /* Copy the set of subsystem state objects generated in
704 * find_existing_css_set() */
705 memcpy(res->subsys, template, sizeof(res->subsys));
706
707 write_lock(&css_set_lock);
708 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700709 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
710 struct cgroup *c = link->cgrp;
711 if (c->root == cgrp->root)
712 c = cgrp;
713 link_css_set(&tmp_cg_links, res, c);
714 }
Paul Menage817929e2007-10-18 23:39:36 -0700715
716 BUG_ON(!list_empty(&tmp_cg_links));
717
Paul Menage817929e2007-10-18 23:39:36 -0700718 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700719
720 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800721 key = css_set_hash(res->subsys);
722 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700723
Paul Menage817929e2007-10-18 23:39:36 -0700724 write_unlock(&css_set_lock);
725
726 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700727}
728
Paul Menageddbcc7e2007-10-18 23:39:30 -0700729/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700730 * Return the cgroup for "task" from the given hierarchy. Must be
731 * called with cgroup_mutex held.
732 */
733static struct cgroup *task_cgroup_from_root(struct task_struct *task,
734 struct cgroupfs_root *root)
735{
736 struct css_set *css;
737 struct cgroup *res = NULL;
738
739 BUG_ON(!mutex_is_locked(&cgroup_mutex));
740 read_lock(&css_set_lock);
741 /*
742 * No need to lock the task - since we hold cgroup_mutex the
743 * task can't change groups, so the only thing that can happen
744 * is that it exits and its css is set back to init_css_set.
745 */
746 css = task->cgroups;
747 if (css == &init_css_set) {
748 res = &root->top_cgroup;
749 } else {
750 struct cg_cgroup_link *link;
751 list_for_each_entry(link, &css->cg_links, cg_link_list) {
752 struct cgroup *c = link->cgrp;
753 if (c->root == root) {
754 res = c;
755 break;
756 }
757 }
758 }
759 read_unlock(&css_set_lock);
760 BUG_ON(!res);
761 return res;
762}
763
764/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700765 * There is one global cgroup mutex. We also require taking
766 * task_lock() when dereferencing a task's cgroup subsys pointers.
767 * See "The task_lock() exception", at the end of this comment.
768 *
769 * A task must hold cgroup_mutex to modify cgroups.
770 *
771 * Any task can increment and decrement the count field without lock.
772 * So in general, code holding cgroup_mutex can't rely on the count
773 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800774 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700775 * means that no tasks are currently attached, therefore there is no
776 * way a task attached to that cgroup can fork (the other way to
777 * increment the count). So code holding cgroup_mutex can safely
778 * assume that if the count is zero, it will stay zero. Similarly, if
779 * a task holds cgroup_mutex on a cgroup with zero count, it
780 * knows that the cgroup won't be removed, as cgroup_rmdir()
781 * needs that mutex.
782 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700783 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
784 * (usually) take cgroup_mutex. These are the two most performance
785 * critical pieces of code here. The exception occurs on cgroup_exit(),
786 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
787 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800788 * to the release agent with the name of the cgroup (path relative to
789 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700790 *
791 * A cgroup can only be deleted if both its 'count' of using tasks
792 * is zero, and its list of 'children' cgroups is empty. Since all
793 * tasks in the system use _some_ cgroup, and since there is always at
794 * least one task in the system (init, pid == 1), therefore, top_cgroup
795 * always has either children cgroups and/or using tasks. So we don't
796 * need a special hack to ensure that top_cgroup cannot be deleted.
797 *
798 * The task_lock() exception
799 *
800 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800801 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800802 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803 * several performance critical places that need to reference
804 * task->cgroup without the expense of grabbing a system global
805 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800806 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700807 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
808 * the task_struct routinely used for such matters.
809 *
810 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800811 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700812 */
813
Paul Menageddbcc7e2007-10-18 23:39:30 -0700814/*
815 * A couple of forward declarations required, due to cyclic reference loop:
816 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
817 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
818 * -> cgroup_mkdir.
819 */
820
Al Viro18bb1db2011-07-26 01:41:39 -0400821static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400822static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700823static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400824static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
825 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700826static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700827static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700828
829static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200830 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700831 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700832};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700833
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700834static int alloc_css_id(struct cgroup_subsys *ss,
835 struct cgroup *parent, struct cgroup *child);
836
Al Viroa5e7ed32011-07-26 01:55:55 -0400837static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700838{
839 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700840
841 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400842 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700843 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100844 inode->i_uid = current_fsuid();
845 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700846 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
847 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
848 }
849 return inode;
850}
851
Li Zefan65dff752013-03-01 15:01:56 +0800852static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
853{
854 struct cgroup_name *name;
855
856 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
857 if (!name)
858 return NULL;
859 strcpy(name->name, dentry->d_name.name);
860 return name;
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);
Li Zefan65dff752013-03-01 15:01:56 +0800893 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800894 kfree(cgrp);
895}
896
897static void cgroup_free_rcu(struct rcu_head *head)
898{
899 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
900
901 schedule_work(&cgrp->free_work);
902}
903
Paul Menageddbcc7e2007-10-18 23:39:30 -0700904static void cgroup_diput(struct dentry *dentry, struct inode *inode)
905{
906 /* is dentry a directory ? if so, kfree() associated cgroup */
907 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700908 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800909
Paul Menagebd89aab2007-10-18 23:40:44 -0700910 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800911 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700912 } else {
913 struct cfent *cfe = __d_cfe(dentry);
914 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400915 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700916
917 WARN_ONCE(!list_empty(&cfe->node) &&
918 cgrp != &cgrp->root->top_cgroup,
919 "cfe still linked for %s\n", cfe->type->name);
920 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400921 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700922 }
923 iput(inode);
924}
925
Al Viroc72a04e2011-01-14 05:31:45 +0000926static int cgroup_delete(const struct dentry *d)
927{
928 return 1;
929}
930
Paul Menageddbcc7e2007-10-18 23:39:30 -0700931static void remove_dir(struct dentry *d)
932{
933 struct dentry *parent = dget(d->d_parent);
934
935 d_delete(d);
936 simple_rmdir(parent->d_inode, d);
937 dput(parent);
938}
939
Li Zefan2739d3c2013-01-21 18:18:33 +0800940static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700941{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700942 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700943
Tejun Heo05ef1d72012-04-01 12:09:56 -0700944 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
945 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100946
Li Zefan2739d3c2013-01-21 18:18:33 +0800947 /*
948 * If we're doing cleanup due to failure of cgroup_create(),
949 * the corresponding @cfe may not exist.
950 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700951 list_for_each_entry(cfe, &cgrp->files, node) {
952 struct dentry *d = cfe->dentry;
953
954 if (cft && cfe->type != cft)
955 continue;
956
957 dget(d);
958 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700959 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700960 list_del_init(&cfe->node);
961 dput(d);
962
Li Zefan2739d3c2013-01-21 18:18:33 +0800963 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700964 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700965}
966
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400967/**
968 * cgroup_clear_directory - selective removal of base and subsystem files
969 * @dir: directory containing the files
970 * @base_files: true if the base files should be removed
971 * @subsys_mask: mask of the subsystem ids whose files should be removed
972 */
973static void cgroup_clear_directory(struct dentry *dir, bool base_files,
974 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700975{
976 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400977 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700978
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400979 for_each_subsys(cgrp->root, ss) {
980 struct cftype_set *set;
981 if (!test_bit(ss->subsys_id, &subsys_mask))
982 continue;
983 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800984 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400985 }
986 if (base_files) {
987 while (!list_empty(&cgrp->files))
988 cgroup_rm_file(cgrp, NULL);
989 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990}
991
992/*
993 * NOTE : the dentry must have been dget()'ed
994 */
995static void cgroup_d_remove_dir(struct dentry *dentry)
996{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100997 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400998 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100999
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001000 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001002 parent = dentry->d_parent;
1003 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001004 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001005 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001006 spin_unlock(&dentry->d_lock);
1007 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008 remove_dir(dentry);
1009}
1010
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001011/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001012 * Call with cgroup_mutex held. Drops reference counts on modules, including
1013 * any duplicate ones that parse_cgroupfs_options took. If this function
1014 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001015 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001017 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001019 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001020 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001021 int i;
1022
Ben Blumaae8aab2010-03-10 15:22:07 -08001023 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001024 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001025
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001026 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1027 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028 /* Check that any added subsystems are currently free */
1029 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001030 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001032 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001034 /*
1035 * Nobody should tell us to do a subsys that doesn't exist:
1036 * parse_cgroupfs_options should catch that case and refcounts
1037 * ensure that subsystems won't disappear once selected.
1038 */
1039 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001040 if (ss->root != &rootnode) {
1041 /* Subsystem isn't free */
1042 return -EBUSY;
1043 }
1044 }
1045
1046 /* Currently we don't handle adding/removing subsystems when
1047 * any child cgroups exist. This is theoretically supportable
1048 * but involves complex error handling, so it's being left until
1049 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001050 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 return -EBUSY;
1052
1053 /* Process each subsystem */
1054 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1055 struct cgroup_subsys *ss = subsys[i];
1056 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001057 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001059 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001060 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 BUG_ON(!dummytop->subsys[i]);
1062 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001063 cgrp->subsys[i] = dummytop->subsys[i];
1064 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001065 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001066 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001068 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001069 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001070 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001072 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001073 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1074 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001076 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001078 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001079 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001080 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001081 /* subsystem is now free - drop reference on module */
1082 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001083 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001084 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001085 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001086 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001087 /*
1088 * a refcount was taken, but we already had one, so
1089 * drop the extra reference.
1090 */
1091 module_put(ss->module);
1092#ifdef CONFIG_MODULE_UNLOAD
1093 BUG_ON(ss->module && !module_refcount(ss->module));
1094#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095 } else {
1096 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001097 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001098 }
1099 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001100 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101
1102 return 0;
1103}
1104
Al Viro34c80b12011-12-08 21:32:45 -05001105static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106{
Al Viro34c80b12011-12-08 21:32:45 -05001107 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108 struct cgroup_subsys *ss;
1109
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001110 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001111 for_each_subsys(root, ss)
1112 seq_printf(seq, ",%s", ss->name);
1113 if (test_bit(ROOT_NOPREFIX, &root->flags))
1114 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001115 if (test_bit(ROOT_XATTR, &root->flags))
1116 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001117 if (strlen(root->release_agent_path))
1118 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001119 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001120 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001121 if (strlen(root->name))
1122 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001123 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124 return 0;
1125}
1126
1127struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001128 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001129 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001130 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001131 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001132 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001133 /* User explicitly requested empty subsystem */
1134 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001135
1136 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001137
Paul Menageddbcc7e2007-10-18 23:39:30 -07001138};
1139
Ben Blumaae8aab2010-03-10 15:22:07 -08001140/*
1141 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001142 * with cgroup_mutex held to protect the subsys[] array. This function takes
1143 * refcounts on subsystems to be used, unless it returns error, in which case
1144 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001145 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001146static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001147{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001148 char *token, *o = data;
1149 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001150 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001151 int i;
1152 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001153
Ben Blumaae8aab2010-03-10 15:22:07 -08001154 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1155
Li Zefanf9ab5b52009-06-17 16:26:33 -07001156#ifdef CONFIG_CPUSETS
1157 mask = ~(1UL << cpuset_subsys_id);
1158#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001159
Paul Menagec6d57f32009-09-23 15:56:19 -07001160 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001161
1162 while ((token = strsep(&o, ",")) != NULL) {
1163 if (!*token)
1164 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001165 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001166 /* Explicitly have no subsystems */
1167 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001168 continue;
1169 }
1170 if (!strcmp(token, "all")) {
1171 /* Mutually exclusive option 'all' + subsystem name */
1172 if (one_ss)
1173 return -EINVAL;
1174 all_ss = true;
1175 continue;
1176 }
1177 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001178 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001179 continue;
1180 }
1181 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001182 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001183 continue;
1184 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001185 if (!strcmp(token, "xattr")) {
1186 set_bit(ROOT_XATTR, &opts->flags);
1187 continue;
1188 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001189 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001190 /* Specifying two release agents is forbidden */
1191 if (opts->release_agent)
1192 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001193 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001194 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001195 if (!opts->release_agent)
1196 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001197 continue;
1198 }
1199 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001200 const char *name = token + 5;
1201 /* Can't specify an empty name */
1202 if (!strlen(name))
1203 return -EINVAL;
1204 /* Must match [\w.-]+ */
1205 for (i = 0; i < strlen(name); i++) {
1206 char c = name[i];
1207 if (isalnum(c))
1208 continue;
1209 if ((c == '.') || (c == '-') || (c == '_'))
1210 continue;
1211 return -EINVAL;
1212 }
1213 /* Specifying two names is forbidden */
1214 if (opts->name)
1215 return -EINVAL;
1216 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001217 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001218 GFP_KERNEL);
1219 if (!opts->name)
1220 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001221
1222 continue;
1223 }
1224
1225 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1226 struct cgroup_subsys *ss = subsys[i];
1227 if (ss == NULL)
1228 continue;
1229 if (strcmp(token, ss->name))
1230 continue;
1231 if (ss->disabled)
1232 continue;
1233
1234 /* Mutually exclusive option 'all' + subsystem name */
1235 if (all_ss)
1236 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001237 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001238 one_ss = true;
1239
1240 break;
1241 }
1242 if (i == CGROUP_SUBSYS_COUNT)
1243 return -ENOENT;
1244 }
1245
1246 /*
1247 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001248 * otherwise if 'none', 'name=' and a subsystem name options
1249 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001250 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001251 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001252 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1253 struct cgroup_subsys *ss = subsys[i];
1254 if (ss == NULL)
1255 continue;
1256 if (ss->disabled)
1257 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001258 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001259 }
1260 }
1261
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001262 /* Consistency checks */
1263
Li Zefanf9ab5b52009-06-17 16:26:33 -07001264 /*
1265 * Option noprefix was introduced just for backward compatibility
1266 * with the old cpuset, so we allow noprefix only if mounting just
1267 * the cpuset subsystem.
1268 */
1269 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001270 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001271 return -EINVAL;
1272
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001273
1274 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001275 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001276 return -EINVAL;
1277
1278 /*
1279 * We either have to specify by name or by subsystems. (So all
1280 * empty hierarchies must have a name).
1281 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001282 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001283 return -EINVAL;
1284
Ben Blumcf5d5942010-03-10 15:22:09 -08001285 /*
1286 * Grab references on all the modules we'll need, so the subsystems
1287 * don't dance around before rebind_subsystems attaches them. This may
1288 * take duplicate reference counts on a subsystem that's already used,
1289 * but rebind_subsystems handles this case.
1290 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001291 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001292 unsigned long bit = 1UL << i;
1293
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001294 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001295 continue;
1296 if (!try_module_get(subsys[i]->module)) {
1297 module_pin_failed = true;
1298 break;
1299 }
1300 }
1301 if (module_pin_failed) {
1302 /*
1303 * oops, one of the modules was going away. this means that we
1304 * raced with a module_delete call, and to the user this is
1305 * essentially a "subsystem doesn't exist" case.
1306 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001307 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001308 /* drop refcounts only on the ones we took */
1309 unsigned long bit = 1UL << i;
1310
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001311 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001312 continue;
1313 module_put(subsys[i]->module);
1314 }
1315 return -ENOENT;
1316 }
1317
Paul Menageddbcc7e2007-10-18 23:39:30 -07001318 return 0;
1319}
1320
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001321static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001322{
1323 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001324 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001325 unsigned long bit = 1UL << i;
1326
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001327 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001328 continue;
1329 module_put(subsys[i]->module);
1330 }
1331}
1332
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1334{
1335 int ret = 0;
1336 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001337 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001338 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001339 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340
Paul Menagebd89aab2007-10-18 23:40:44 -07001341 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001343 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001344
1345 /* See what subsystems are wanted */
1346 ret = parse_cgroupfs_options(data, &opts);
1347 if (ret)
1348 goto out_unlock;
1349
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001350 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001351 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1352 task_tgid_nr(current), current->comm);
1353
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001354 added_mask = opts.subsys_mask & ~root->subsys_mask;
1355 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001356
Ben Blumcf5d5942010-03-10 15:22:09 -08001357 /* Don't allow flags or name to change at remount */
1358 if (opts.flags != root->flags ||
1359 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001360 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001361 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001362 goto out_unlock;
1363 }
1364
Gao feng7083d032012-12-03 09:28:18 +08001365 /*
1366 * Clear out the files of subsystems that should be removed, do
1367 * this before rebind_subsystems, since rebind_subsystems may
1368 * change this hierarchy's subsys_list.
1369 */
1370 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1371
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001372 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001373 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001374 /* rebind_subsystems failed, re-populate the removed files */
1375 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001376 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001377 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001378 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001379
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001380 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001381 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001382
Paul Menage81a6a5c2007-10-18 23:39:38 -07001383 if (opts.release_agent)
1384 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001385 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001386 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001387 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001388 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001390 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001391 return ret;
1392}
1393
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001394static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001395 .statfs = simple_statfs,
1396 .drop_inode = generic_delete_inode,
1397 .show_options = cgroup_show_options,
1398 .remount_fs = cgroup_remount,
1399};
1400
Paul Menagecc31edc2008-10-18 20:28:04 -07001401static void init_cgroup_housekeeping(struct cgroup *cgrp)
1402{
1403 INIT_LIST_HEAD(&cgrp->sibling);
1404 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001405 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001406 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001407 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001408 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001409 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001410 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001411 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001412 INIT_LIST_HEAD(&cgrp->event_list);
1413 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001414 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001415}
Paul Menagec6d57f32009-09-23 15:56:19 -07001416
Paul Menageddbcc7e2007-10-18 23:39:30 -07001417static void init_cgroup_root(struct cgroupfs_root *root)
1418{
Paul Menagebd89aab2007-10-18 23:40:44 -07001419 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001420
Paul Menageddbcc7e2007-10-18 23:39:30 -07001421 INIT_LIST_HEAD(&root->subsys_list);
1422 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001423 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001424 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001425 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001426 cgrp->name = &root_cgroup_name;
Paul Menagebd89aab2007-10-18 23:40:44 -07001427 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001428 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001429 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001430}
1431
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001432static bool init_root_id(struct cgroupfs_root *root)
1433{
1434 int ret = 0;
1435
1436 do {
1437 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1438 return false;
1439 spin_lock(&hierarchy_id_lock);
1440 /* Try to allocate the next unused ID */
1441 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1442 &root->hierarchy_id);
1443 if (ret == -ENOSPC)
1444 /* Try again starting from 0 */
1445 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1446 if (!ret) {
1447 next_hierarchy_id = root->hierarchy_id + 1;
1448 } else if (ret != -EAGAIN) {
1449 /* Can only get here if the 31-bit IDR is full ... */
1450 BUG_ON(ret);
1451 }
1452 spin_unlock(&hierarchy_id_lock);
1453 } while (ret);
1454 return true;
1455}
1456
Paul Menageddbcc7e2007-10-18 23:39:30 -07001457static int cgroup_test_super(struct super_block *sb, void *data)
1458{
Paul Menagec6d57f32009-09-23 15:56:19 -07001459 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001460 struct cgroupfs_root *root = sb->s_fs_info;
1461
Paul Menagec6d57f32009-09-23 15:56:19 -07001462 /* If we asked for a name then it must match */
1463 if (opts->name && strcmp(opts->name, root->name))
1464 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001465
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001466 /*
1467 * If we asked for subsystems (or explicitly for no
1468 * subsystems) then they must match
1469 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001470 if ((opts->subsys_mask || opts->none)
1471 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001472 return 0;
1473
1474 return 1;
1475}
1476
Paul Menagec6d57f32009-09-23 15:56:19 -07001477static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1478{
1479 struct cgroupfs_root *root;
1480
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001481 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 return NULL;
1483
1484 root = kzalloc(sizeof(*root), GFP_KERNEL);
1485 if (!root)
1486 return ERR_PTR(-ENOMEM);
1487
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001488 if (!init_root_id(root)) {
1489 kfree(root);
1490 return ERR_PTR(-ENOMEM);
1491 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001492 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001493
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001494 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001495 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001496 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001497 if (opts->release_agent)
1498 strcpy(root->release_agent_path, opts->release_agent);
1499 if (opts->name)
1500 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001501 if (opts->cpuset_clone_children)
1502 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001503 return root;
1504}
1505
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001506static void cgroup_drop_root(struct cgroupfs_root *root)
1507{
1508 if (!root)
1509 return;
1510
1511 BUG_ON(!root->hierarchy_id);
1512 spin_lock(&hierarchy_id_lock);
1513 ida_remove(&hierarchy_ida, root->hierarchy_id);
1514 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001515 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001516 kfree(root);
1517}
1518
Paul Menageddbcc7e2007-10-18 23:39:30 -07001519static int cgroup_set_super(struct super_block *sb, void *data)
1520{
1521 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001522 struct cgroup_sb_opts *opts = data;
1523
1524 /* If we don't have a new root, we can't set up a new sb */
1525 if (!opts->new_root)
1526 return -EINVAL;
1527
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001528 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001529
1530 ret = set_anon_super(sb, NULL);
1531 if (ret)
1532 return ret;
1533
Paul Menagec6d57f32009-09-23 15:56:19 -07001534 sb->s_fs_info = opts->new_root;
1535 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001536
1537 sb->s_blocksize = PAGE_CACHE_SIZE;
1538 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1539 sb->s_magic = CGROUP_SUPER_MAGIC;
1540 sb->s_op = &cgroup_ops;
1541
1542 return 0;
1543}
1544
1545static int cgroup_get_rootdir(struct super_block *sb)
1546{
Al Viro0df6a632010-12-21 13:29:29 -05001547 static const struct dentry_operations cgroup_dops = {
1548 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001549 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001550 };
1551
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552 struct inode *inode =
1553 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554
1555 if (!inode)
1556 return -ENOMEM;
1557
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558 inode->i_fop = &simple_dir_operations;
1559 inode->i_op = &cgroup_dir_inode_operations;
1560 /* directories start off with i_nlink == 2 (for "." entry) */
1561 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001562 sb->s_root = d_make_root(inode);
1563 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001565 /* for everything else we want ->d_op set */
1566 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567 return 0;
1568}
1569
Al Virof7e83572010-07-26 13:23:11 +04001570static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001572 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573{
1574 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001575 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576 int ret = 0;
1577 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001578 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001579 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580
1581 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001582 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001584 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001585 if (ret)
1586 goto out_err;
1587
1588 /*
1589 * Allocate a new cgroup root. We may not need it if we're
1590 * reusing an existing hierarchy.
1591 */
1592 new_root = cgroup_root_from_opts(&opts);
1593 if (IS_ERR(new_root)) {
1594 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001595 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001596 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001597 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001598
Paul Menagec6d57f32009-09-23 15:56:19 -07001599 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001600 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001601 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001602 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001603 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001604 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605 }
1606
Paul Menagec6d57f32009-09-23 15:56:19 -07001607 root = sb->s_fs_info;
1608 BUG_ON(!root);
1609 if (root == opts.new_root) {
1610 /* We used the new root structure, so this is a new hierarchy */
1611 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001612 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001613 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001614 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001615 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001616 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001617
1618 BUG_ON(sb->s_root != NULL);
1619
1620 ret = cgroup_get_rootdir(sb);
1621 if (ret)
1622 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001623 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624
Paul Menage817929e2007-10-18 23:39:36 -07001625 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001627 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001629 /* Check for name clashes with existing mounts */
1630 ret = -EBUSY;
1631 if (strlen(root->name))
1632 for_each_active_root(existing_root)
1633 if (!strcmp(existing_root->name, root->name))
1634 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001635
Paul Menage817929e2007-10-18 23:39:36 -07001636 /*
1637 * We're accessing css_set_count without locking
1638 * css_set_lock here, but that's OK - it can only be
1639 * increased by someone holding cgroup_lock, and
1640 * that's us. The worst that can happen is that we
1641 * have some link structures left over
1642 */
1643 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001644 if (ret)
1645 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001646
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001647 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001648 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001649 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001650 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001651 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001652 /*
1653 * There must be no failure case after here, since rebinding
1654 * takes care of subsystems' refcounts, which are explicitly
1655 * dropped in the failure exit path.
1656 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657
1658 /* EBUSY should be the only error here */
1659 BUG_ON(ret);
1660
1661 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001662 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663
Li Zefanc12f65d2009-01-07 18:07:42 -08001664 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665 root->top_cgroup.dentry = sb->s_root;
1666
Paul Menage817929e2007-10-18 23:39:36 -07001667 /* Link the top cgroup in this hierarchy into all
1668 * the css_set objects */
1669 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001670 hash_for_each(css_set_table, i, cg, hlist)
Li Zefan0ac801f2013-01-10 11:49:27 +08001671 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001672 write_unlock(&css_set_lock);
1673
1674 free_cg_links(&tmp_cg_links);
1675
Li Zefanc12f65d2009-01-07 18:07:42 -08001676 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001677 BUG_ON(root->number_of_cgroups != 1);
1678
eparis@redhat2ce97382011-06-02 21:20:51 +10001679 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001680 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001681 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001682 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001684 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001685 } else {
1686 /*
1687 * We re-used an existing hierarchy - the new root (if
1688 * any) is not needed
1689 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001690 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001691 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001692 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693 }
1694
Paul Menagec6d57f32009-09-23 15:56:19 -07001695 kfree(opts.release_agent);
1696 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001697 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001699 unlock_drop:
1700 mutex_unlock(&cgroup_root_mutex);
1701 mutex_unlock(&cgroup_mutex);
1702 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001704 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001705 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001706 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001707 out_err:
1708 kfree(opts.release_agent);
1709 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001710 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001711}
1712
1713static void cgroup_kill_sb(struct super_block *sb) {
1714 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001715 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001716 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001717 struct cg_cgroup_link *link;
1718 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719
1720 BUG_ON(!root);
1721
1722 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001723 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724
1725 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001726 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727
1728 /* Rebind all subsystems back to the default hierarchy */
1729 ret = rebind_subsystems(root, 0);
1730 /* Shouldn't be able to fail ... */
1731 BUG_ON(ret);
1732
Paul Menage817929e2007-10-18 23:39:36 -07001733 /*
1734 * Release all the links from css_sets to this hierarchy's
1735 * root cgroup
1736 */
1737 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001738
1739 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1740 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001741 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001742 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001743 kfree(link);
1744 }
1745 write_unlock(&css_set_lock);
1746
Paul Menage839ec542009-01-29 14:25:22 -08001747 if (!list_empty(&root->root_list)) {
1748 list_del(&root->root_list);
1749 root_count--;
1750 }
Li Zefane5f6a862009-01-07 18:07:41 -08001751
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001752 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753 mutex_unlock(&cgroup_mutex);
1754
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001755 simple_xattrs_free(&cgrp->xattrs);
1756
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001758 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759}
1760
1761static struct file_system_type cgroup_fs_type = {
1762 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001763 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001764 .kill_sb = cgroup_kill_sb,
1765};
1766
Greg KH676db4a2010-08-05 13:53:35 -07001767static struct kobject *cgroup_kobj;
1768
Li Zefana043e3b2008-02-23 15:24:09 -08001769/**
1770 * cgroup_path - generate the path of a cgroup
1771 * @cgrp: the cgroup in question
1772 * @buf: the buffer to write the path into
1773 * @buflen: the length of the buffer
1774 *
Li Zefan65dff752013-03-01 15:01:56 +08001775 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1776 *
1777 * We can't generate cgroup path using dentry->d_name, as accessing
1778 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1779 * inode's i_mutex, while on the other hand cgroup_path() can be called
1780 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001782int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001783{
Li Zefan65dff752013-03-01 15:01:56 +08001784 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001785 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001786
Tao Ma316eb662012-11-08 21:36:38 +08001787 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001788 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001789
Li Zefan65dff752013-03-01 15:01:56 +08001790 rcu_read_lock();
1791 while (cgrp) {
1792 const char *name = cgroup_name(cgrp);
1793 int len;
1794
1795 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001796 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001797 goto out;
1798 memcpy(start, name, len);
1799
1800 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001801 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001802
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001804 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001805 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001806
1807 cgrp = cgrp->parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001808 }
Li Zefan65dff752013-03-01 15:01:56 +08001809 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001811out:
1812 rcu_read_unlock();
1813 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001814}
Ben Blum67523c42010-03-10 15:22:11 -08001815EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001816
Ben Blum74a11662011-05-26 16:25:20 -07001817/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001818 * Control Group taskset
1819 */
Tejun Heo134d3372011-12-12 18:12:21 -08001820struct task_and_cgroup {
1821 struct task_struct *task;
1822 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001823 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001824};
1825
Tejun Heo2f7ee562011-12-12 18:12:21 -08001826struct cgroup_taskset {
1827 struct task_and_cgroup single;
1828 struct flex_array *tc_array;
1829 int tc_array_len;
1830 int idx;
1831 struct cgroup *cur_cgrp;
1832};
1833
1834/**
1835 * cgroup_taskset_first - reset taskset and return the first task
1836 * @tset: taskset of interest
1837 *
1838 * @tset iteration is initialized and the first task is returned.
1839 */
1840struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1841{
1842 if (tset->tc_array) {
1843 tset->idx = 0;
1844 return cgroup_taskset_next(tset);
1845 } else {
1846 tset->cur_cgrp = tset->single.cgrp;
1847 return tset->single.task;
1848 }
1849}
1850EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1851
1852/**
1853 * cgroup_taskset_next - iterate to the next task in taskset
1854 * @tset: taskset of interest
1855 *
1856 * Return the next task in @tset. Iteration must have been initialized
1857 * with cgroup_taskset_first().
1858 */
1859struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1860{
1861 struct task_and_cgroup *tc;
1862
1863 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1864 return NULL;
1865
1866 tc = flex_array_get(tset->tc_array, tset->idx++);
1867 tset->cur_cgrp = tc->cgrp;
1868 return tc->task;
1869}
1870EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1871
1872/**
1873 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1874 * @tset: taskset of interest
1875 *
1876 * Return the cgroup for the current (last returned) task of @tset. This
1877 * function must be preceded by either cgroup_taskset_first() or
1878 * cgroup_taskset_next().
1879 */
1880struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1881{
1882 return tset->cur_cgrp;
1883}
1884EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1885
1886/**
1887 * cgroup_taskset_size - return the number of tasks in taskset
1888 * @tset: taskset of interest
1889 */
1890int cgroup_taskset_size(struct cgroup_taskset *tset)
1891{
1892 return tset->tc_array ? tset->tc_array_len : 1;
1893}
1894EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1895
1896
Ben Blum74a11662011-05-26 16:25:20 -07001897/*
1898 * cgroup_task_migrate - move a task from one cgroup to another.
1899 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001900 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001901 */
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03001902static void cgroup_task_migrate(struct cgroup *oldcgrp,
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001903 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001904{
1905 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001906
1907 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001908 * We are synchronized through threadgroup_lock() against PF_EXITING
1909 * setting such that we can't race against cgroup_exit() changing the
1910 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001911 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001912 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001913 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001914
Ben Blum74a11662011-05-26 16:25:20 -07001915 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001916 rcu_assign_pointer(tsk->cgroups, newcg);
1917 task_unlock(tsk);
1918
1919 /* Update the css_set linked lists if we're using them */
1920 write_lock(&css_set_lock);
1921 if (!list_empty(&tsk->cg_list))
1922 list_move(&tsk->cg_list, &newcg->tasks);
1923 write_unlock(&css_set_lock);
1924
1925 /*
1926 * We just gained a reference on oldcg by taking it from the task. As
1927 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1928 * it here; it will be freed under RCU.
1929 */
Ben Blum74a11662011-05-26 16:25:20 -07001930 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001931 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001932}
1933
Li Zefana043e3b2008-02-23 15:24:09 -08001934/**
Li Zefan081aa452013-03-13 09:17:09 +08001935 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001936 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001937 * @tsk: the task or the leader of the threadgroup to be attached
1938 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001939 *
Tejun Heo257058a2011-12-12 18:12:21 -08001940 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001941 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001942 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001943static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1944 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001945{
1946 int retval, i, group_size;
1947 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001948 struct cgroupfs_root *root = cgrp->root;
1949 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001950 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001951 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001952 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001953 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001954
1955 /*
1956 * step 0: in order to do expensive, possibly blocking operations for
1957 * every thread, we cannot iterate the thread group list, since it needs
1958 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001959 * group - group_rwsem prevents new threads from appearing, and if
1960 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001961 */
Li Zefan081aa452013-03-13 09:17:09 +08001962 if (threadgroup)
1963 group_size = get_nr_threads(tsk);
1964 else
1965 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001966 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001967 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001968 if (!group)
1969 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001970 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001971 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001972 if (retval)
1973 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001974
Ben Blum74a11662011-05-26 16:25:20 -07001975 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001976 /*
1977 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1978 * already PF_EXITING could be freed from underneath us unless we
1979 * take an rcu_read_lock.
1980 */
1981 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001982 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001983 struct task_and_cgroup ent;
1984
Tejun Heocd3d0952011-12-12 18:12:21 -08001985 /* @tsk either already exited or can't exit until the end */
1986 if (tsk->flags & PF_EXITING)
1987 continue;
1988
Ben Blum74a11662011-05-26 16:25:20 -07001989 /* as per above, nr_threads may decrease, but not increase. */
1990 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001991 ent.task = tsk;
1992 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001993 /* nothing to do if this task is already in the cgroup */
1994 if (ent.cgrp == cgrp)
1995 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001996 /*
1997 * saying GFP_ATOMIC has no effect here because we did prealloc
1998 * earlier, but it's good form to communicate our expectations.
1999 */
Tejun Heo134d3372011-12-12 18:12:21 -08002000 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002001 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002002 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002003
2004 if (!threadgroup)
2005 break;
Ben Blum74a11662011-05-26 16:25:20 -07002006 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002007 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002008 /* remember the number of threads in the array for later. */
2009 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002010 tset.tc_array = group;
2011 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002012
Tejun Heo134d3372011-12-12 18:12:21 -08002013 /* methods shouldn't be called if no task is actually migrating */
2014 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002015 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002016 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002017
Ben Blum74a11662011-05-26 16:25:20 -07002018 /*
2019 * step 1: check that we can legitimately attach to the cgroup.
2020 */
2021 for_each_subsys(root, ss) {
2022 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002023 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002024 if (retval) {
2025 failed_ss = ss;
2026 goto out_cancel_attach;
2027 }
2028 }
Ben Blum74a11662011-05-26 16:25:20 -07002029 }
2030
2031 /*
2032 * step 2: make sure css_sets exist for all threads to be migrated.
2033 * we use find_css_set, which allocates a new one if necessary.
2034 */
Ben Blum74a11662011-05-26 16:25:20 -07002035 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002036 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002037 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2038 if (!tc->cg) {
2039 retval = -ENOMEM;
2040 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002041 }
2042 }
2043
2044 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002045 * step 3: now that we're guaranteed success wrt the css_sets,
2046 * proceed to move all tasks to the new cgroup. There are no
2047 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002048 */
Ben Blum74a11662011-05-26 16:25:20 -07002049 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002050 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002051 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002052 }
2053 /* nothing is sensitive to fork() after this point. */
2054
2055 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002056 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002057 */
2058 for_each_subsys(root, ss) {
2059 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002060 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002061 }
2062
2063 /*
2064 * step 5: success! and cleanup
2065 */
Ben Blum74a11662011-05-26 16:25:20 -07002066 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002067out_put_css_set_refs:
2068 if (retval) {
2069 for (i = 0; i < group_size; i++) {
2070 tc = flex_array_get(group, i);
2071 if (!tc->cg)
2072 break;
2073 put_css_set(tc->cg);
2074 }
Ben Blum74a11662011-05-26 16:25:20 -07002075 }
2076out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002077 if (retval) {
2078 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002079 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002080 break;
Ben Blum74a11662011-05-26 16:25:20 -07002081 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002082 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002083 }
2084 }
Ben Blum74a11662011-05-26 16:25:20 -07002085out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002086 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002087 return retval;
2088}
2089
2090/*
2091 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002092 * function to attach either it or all tasks in its threadgroup. Will lock
2093 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002094 */
2095static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002096{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002097 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002098 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002099 int ret;
2100
Ben Blum74a11662011-05-26 16:25:20 -07002101 if (!cgroup_lock_live_group(cgrp))
2102 return -ENODEV;
2103
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002104retry_find_task:
2105 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002106 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002107 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002108 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002109 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002110 ret= -ESRCH;
2111 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002112 }
Ben Blum74a11662011-05-26 16:25:20 -07002113 /*
2114 * even if we're attaching all tasks in the thread group, we
2115 * only need to check permissions on one of them.
2116 */
David Howellsc69e8d92008-11-14 10:39:19 +11002117 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002118 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2119 !uid_eq(cred->euid, tcred->uid) &&
2120 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002121 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002122 ret = -EACCES;
2123 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002124 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002125 } else
2126 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002127
2128 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002129 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002130
2131 /*
2132 * Workqueue threads may acquire PF_THREAD_BOUND and become
2133 * trapped in a cpuset, or RT worker may be born in a cgroup
2134 * with no rt_runtime allocated. Just say no.
2135 */
2136 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2137 ret = -EINVAL;
2138 rcu_read_unlock();
2139 goto out_unlock_cgroup;
2140 }
2141
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002142 get_task_struct(tsk);
2143 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002144
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002145 threadgroup_lock(tsk);
2146 if (threadgroup) {
2147 if (!thread_group_leader(tsk)) {
2148 /*
2149 * a race with de_thread from another thread's exec()
2150 * may strip us of our leadership, if this happens,
2151 * there is no choice but to throw this task away and
2152 * try again; this is
2153 * "double-double-toil-and-trouble-check locking".
2154 */
2155 threadgroup_unlock(tsk);
2156 put_task_struct(tsk);
2157 goto retry_find_task;
2158 }
Li Zefan081aa452013-03-13 09:17:09 +08002159 }
2160
2161 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2162
Tejun Heocd3d0952011-12-12 18:12:21 -08002163 threadgroup_unlock(tsk);
2164
Paul Menagebbcb81d2007-10-18 23:39:32 -07002165 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002166out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002167 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002168 return ret;
2169}
2170
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002171/**
2172 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2173 * @from: attach to all cgroups of a given task
2174 * @tsk: the task to be attached
2175 */
2176int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2177{
2178 struct cgroupfs_root *root;
2179 int retval = 0;
2180
Tejun Heo47cfcd02013-04-07 09:29:51 -07002181 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002182 for_each_active_root(root) {
2183 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2184
2185 retval = cgroup_attach_task(from_cg, tsk, false);
2186 if (retval)
2187 break;
2188 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002189 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002190
2191 return retval;
2192}
2193EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2194
Paul Menageaf351022008-07-25 01:47:01 -07002195static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2196{
Ben Blum74a11662011-05-26 16:25:20 -07002197 return attach_task_by_pid(cgrp, pid, false);
2198}
2199
2200static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2201{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002202 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002203}
2204
Paul Menagee788e062008-07-25 01:46:59 -07002205static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2206 const char *buffer)
2207{
2208 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002209 if (strlen(buffer) >= PATH_MAX)
2210 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002211 if (!cgroup_lock_live_group(cgrp))
2212 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002213 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002214 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002215 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002216 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002217 return 0;
2218}
2219
2220static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2221 struct seq_file *seq)
2222{
2223 if (!cgroup_lock_live_group(cgrp))
2224 return -ENODEV;
2225 seq_puts(seq, cgrp->root->release_agent_path);
2226 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002227 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002228 return 0;
2229}
2230
Paul Menage84eea842008-07-25 01:47:00 -07002231/* A buffer size big enough for numbers or short strings */
2232#define CGROUP_LOCAL_BUFFER_SIZE 64
2233
Paul Menagee73d2c62008-04-29 01:00:06 -07002234static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002235 struct file *file,
2236 const char __user *userbuf,
2237 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002238{
Paul Menage84eea842008-07-25 01:47:00 -07002239 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002240 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002241 char *end;
2242
2243 if (!nbytes)
2244 return -EINVAL;
2245 if (nbytes >= sizeof(buffer))
2246 return -E2BIG;
2247 if (copy_from_user(buffer, userbuf, nbytes))
2248 return -EFAULT;
2249
2250 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002251 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002252 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002253 if (*end)
2254 return -EINVAL;
2255 retval = cft->write_u64(cgrp, cft, val);
2256 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002257 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002258 if (*end)
2259 return -EINVAL;
2260 retval = cft->write_s64(cgrp, cft, val);
2261 }
Paul Menage355e0c42007-10-18 23:39:33 -07002262 if (!retval)
2263 retval = nbytes;
2264 return retval;
2265}
2266
Paul Menagedb3b1492008-07-25 01:46:58 -07002267static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2268 struct file *file,
2269 const char __user *userbuf,
2270 size_t nbytes, loff_t *unused_ppos)
2271{
Paul Menage84eea842008-07-25 01:47:00 -07002272 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002273 int retval = 0;
2274 size_t max_bytes = cft->max_write_len;
2275 char *buffer = local_buffer;
2276
2277 if (!max_bytes)
2278 max_bytes = sizeof(local_buffer) - 1;
2279 if (nbytes >= max_bytes)
2280 return -E2BIG;
2281 /* Allocate a dynamic buffer if we need one */
2282 if (nbytes >= sizeof(local_buffer)) {
2283 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2284 if (buffer == NULL)
2285 return -ENOMEM;
2286 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002287 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2288 retval = -EFAULT;
2289 goto out;
2290 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002291
2292 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002293 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002294 if (!retval)
2295 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002296out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002297 if (buffer != local_buffer)
2298 kfree(buffer);
2299 return retval;
2300}
2301
Paul Menageddbcc7e2007-10-18 23:39:30 -07002302static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2303 size_t nbytes, loff_t *ppos)
2304{
2305 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002306 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002307
Li Zefan75139b82009-01-07 18:07:33 -08002308 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002309 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002310 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002311 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002312 if (cft->write_u64 || cft->write_s64)
2313 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002314 if (cft->write_string)
2315 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002316 if (cft->trigger) {
2317 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2318 return ret ? ret : nbytes;
2319 }
Paul Menage355e0c42007-10-18 23:39:33 -07002320 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002321}
2322
Paul Menagef4c753b2008-04-29 00:59:56 -07002323static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2324 struct file *file,
2325 char __user *buf, size_t nbytes,
2326 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002327{
Paul Menage84eea842008-07-25 01:47:00 -07002328 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002329 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002330 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2331
2332 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2333}
2334
Paul Menagee73d2c62008-04-29 01:00:06 -07002335static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2336 struct file *file,
2337 char __user *buf, size_t nbytes,
2338 loff_t *ppos)
2339{
Paul Menage84eea842008-07-25 01:47:00 -07002340 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002341 s64 val = cft->read_s64(cgrp, cft);
2342 int len = sprintf(tmp, "%lld\n", (long long) val);
2343
2344 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2345}
2346
Paul Menageddbcc7e2007-10-18 23:39:30 -07002347static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2348 size_t nbytes, loff_t *ppos)
2349{
2350 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002351 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002352
Li Zefan75139b82009-01-07 18:07:33 -08002353 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002354 return -ENODEV;
2355
2356 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002357 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002358 if (cft->read_u64)
2359 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002360 if (cft->read_s64)
2361 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002362 return -EINVAL;
2363}
2364
Paul Menage91796562008-04-29 01:00:01 -07002365/*
2366 * seqfile ops/methods for returning structured data. Currently just
2367 * supports string->u64 maps, but can be extended in future.
2368 */
2369
2370struct cgroup_seqfile_state {
2371 struct cftype *cft;
2372 struct cgroup *cgroup;
2373};
2374
2375static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2376{
2377 struct seq_file *sf = cb->state;
2378 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2379}
2380
2381static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2382{
2383 struct cgroup_seqfile_state *state = m->private;
2384 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002385 if (cft->read_map) {
2386 struct cgroup_map_cb cb = {
2387 .fill = cgroup_map_add,
2388 .state = m,
2389 };
2390 return cft->read_map(state->cgroup, cft, &cb);
2391 }
2392 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002393}
2394
Adrian Bunk96930a62008-07-25 19:46:21 -07002395static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002396{
2397 struct seq_file *seq = file->private_data;
2398 kfree(seq->private);
2399 return single_release(inode, file);
2400}
2401
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002402static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002403 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002404 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002405 .llseek = seq_lseek,
2406 .release = cgroup_seqfile_release,
2407};
2408
Paul Menageddbcc7e2007-10-18 23:39:30 -07002409static int cgroup_file_open(struct inode *inode, struct file *file)
2410{
2411 int err;
2412 struct cftype *cft;
2413
2414 err = generic_file_open(inode, file);
2415 if (err)
2416 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002417 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002418
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002419 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002420 struct cgroup_seqfile_state *state =
2421 kzalloc(sizeof(*state), GFP_USER);
2422 if (!state)
2423 return -ENOMEM;
2424 state->cft = cft;
2425 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2426 file->f_op = &cgroup_seqfile_operations;
2427 err = single_open(file, cgroup_seqfile_show, state);
2428 if (err < 0)
2429 kfree(state);
2430 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002431 err = cft->open(inode, file);
2432 else
2433 err = 0;
2434
2435 return err;
2436}
2437
2438static int cgroup_file_release(struct inode *inode, struct file *file)
2439{
2440 struct cftype *cft = __d_cft(file->f_dentry);
2441 if (cft->release)
2442 return cft->release(inode, file);
2443 return 0;
2444}
2445
2446/*
2447 * cgroup_rename - Only allow simple rename of directories in place.
2448 */
2449static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2450 struct inode *new_dir, struct dentry *new_dentry)
2451{
Li Zefan65dff752013-03-01 15:01:56 +08002452 int ret;
2453 struct cgroup_name *name, *old_name;
2454 struct cgroup *cgrp;
2455
2456 /*
2457 * It's convinient to use parent dir's i_mutex to protected
2458 * cgrp->name.
2459 */
2460 lockdep_assert_held(&old_dir->i_mutex);
2461
Paul Menageddbcc7e2007-10-18 23:39:30 -07002462 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2463 return -ENOTDIR;
2464 if (new_dentry->d_inode)
2465 return -EEXIST;
2466 if (old_dir != new_dir)
2467 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002468
2469 cgrp = __d_cgrp(old_dentry);
2470
2471 name = cgroup_alloc_name(new_dentry);
2472 if (!name)
2473 return -ENOMEM;
2474
2475 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2476 if (ret) {
2477 kfree(name);
2478 return ret;
2479 }
2480
2481 old_name = cgrp->name;
2482 rcu_assign_pointer(cgrp->name, name);
2483
2484 kfree_rcu(old_name, rcu_head);
2485 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002486}
2487
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002488static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2489{
2490 if (S_ISDIR(dentry->d_inode->i_mode))
2491 return &__d_cgrp(dentry)->xattrs;
2492 else
2493 return &__d_cft(dentry)->xattrs;
2494}
2495
2496static inline int xattr_enabled(struct dentry *dentry)
2497{
2498 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2499 return test_bit(ROOT_XATTR, &root->flags);
2500}
2501
2502static bool is_valid_xattr(const char *name)
2503{
2504 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2505 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2506 return true;
2507 return false;
2508}
2509
2510static int cgroup_setxattr(struct dentry *dentry, const char *name,
2511 const void *val, size_t size, int flags)
2512{
2513 if (!xattr_enabled(dentry))
2514 return -EOPNOTSUPP;
2515 if (!is_valid_xattr(name))
2516 return -EINVAL;
2517 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2518}
2519
2520static int cgroup_removexattr(struct dentry *dentry, const char *name)
2521{
2522 if (!xattr_enabled(dentry))
2523 return -EOPNOTSUPP;
2524 if (!is_valid_xattr(name))
2525 return -EINVAL;
2526 return simple_xattr_remove(__d_xattrs(dentry), name);
2527}
2528
2529static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2530 void *buf, size_t size)
2531{
2532 if (!xattr_enabled(dentry))
2533 return -EOPNOTSUPP;
2534 if (!is_valid_xattr(name))
2535 return -EINVAL;
2536 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2537}
2538
2539static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2540{
2541 if (!xattr_enabled(dentry))
2542 return -EOPNOTSUPP;
2543 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2544}
2545
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002546static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002547 .read = cgroup_file_read,
2548 .write = cgroup_file_write,
2549 .llseek = generic_file_llseek,
2550 .open = cgroup_file_open,
2551 .release = cgroup_file_release,
2552};
2553
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002554static const struct inode_operations cgroup_file_inode_operations = {
2555 .setxattr = cgroup_setxattr,
2556 .getxattr = cgroup_getxattr,
2557 .listxattr = cgroup_listxattr,
2558 .removexattr = cgroup_removexattr,
2559};
2560
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002561static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002562 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002563 .mkdir = cgroup_mkdir,
2564 .rmdir = cgroup_rmdir,
2565 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002566 .setxattr = cgroup_setxattr,
2567 .getxattr = cgroup_getxattr,
2568 .listxattr = cgroup_listxattr,
2569 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002570};
2571
Al Viro00cd8dd2012-06-10 17:13:09 -04002572static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002573{
2574 if (dentry->d_name.len > NAME_MAX)
2575 return ERR_PTR(-ENAMETOOLONG);
2576 d_add(dentry, NULL);
2577 return NULL;
2578}
2579
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002580/*
2581 * Check if a file is a control file
2582 */
2583static inline struct cftype *__file_cft(struct file *file)
2584{
Al Viro496ad9a2013-01-23 17:07:38 -05002585 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002586 return ERR_PTR(-EINVAL);
2587 return __d_cft(file->f_dentry);
2588}
2589
Al Viroa5e7ed32011-07-26 01:55:55 -04002590static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002591 struct super_block *sb)
2592{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002593 struct inode *inode;
2594
2595 if (!dentry)
2596 return -ENOENT;
2597 if (dentry->d_inode)
2598 return -EEXIST;
2599
2600 inode = cgroup_new_inode(mode, sb);
2601 if (!inode)
2602 return -ENOMEM;
2603
2604 if (S_ISDIR(mode)) {
2605 inode->i_op = &cgroup_dir_inode_operations;
2606 inode->i_fop = &simple_dir_operations;
2607
2608 /* start off with i_nlink == 2 (for "." entry) */
2609 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002610 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002611
Tejun Heob8a2df62012-11-19 08:13:37 -08002612 /*
2613 * Control reaches here with cgroup_mutex held.
2614 * @inode->i_mutex should nest outside cgroup_mutex but we
2615 * want to populate it immediately without releasing
2616 * cgroup_mutex. As @inode isn't visible to anyone else
2617 * yet, trylock will always succeed without affecting
2618 * lockdep checks.
2619 */
2620 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002621 } else if (S_ISREG(mode)) {
2622 inode->i_size = 0;
2623 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002624 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002625 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002626 d_instantiate(dentry, inode);
2627 dget(dentry); /* Extra count - pin the dentry in core */
2628 return 0;
2629}
2630
Li Zefan099fca32009-04-02 16:57:29 -07002631/**
2632 * cgroup_file_mode - deduce file mode of a control file
2633 * @cft: the control file in question
2634 *
2635 * returns cft->mode if ->mode is not 0
2636 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2637 * returns S_IRUGO if it has only a read handler
2638 * returns S_IWUSR if it has only a write hander
2639 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002640static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002641{
Al Viroa5e7ed32011-07-26 01:55:55 -04002642 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002643
2644 if (cft->mode)
2645 return cft->mode;
2646
2647 if (cft->read || cft->read_u64 || cft->read_s64 ||
2648 cft->read_map || cft->read_seq_string)
2649 mode |= S_IRUGO;
2650
2651 if (cft->write || cft->write_u64 || cft->write_s64 ||
2652 cft->write_string || cft->trigger)
2653 mode |= S_IWUSR;
2654
2655 return mode;
2656}
2657
Tejun Heodb0416b2012-04-01 12:09:55 -07002658static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002659 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002660{
Paul Menagebd89aab2007-10-18 23:40:44 -07002661 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002662 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002663 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002664 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002665 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002666 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002667 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002668
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002669 simple_xattrs_init(&cft->xattrs);
2670
Paul Menagebd89aab2007-10-18 23:40:44 -07002671 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002672 strcpy(name, subsys->name);
2673 strcat(name, ".");
2674 }
2675 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002676
Paul Menageddbcc7e2007-10-18 23:39:30 -07002677 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002678
2679 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2680 if (!cfe)
2681 return -ENOMEM;
2682
Paul Menageddbcc7e2007-10-18 23:39:30 -07002683 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002684 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002685 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002686 goto out;
2687 }
2688
2689 mode = cgroup_file_mode(cft);
2690 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2691 if (!error) {
2692 cfe->type = (void *)cft;
2693 cfe->dentry = dentry;
2694 dentry->d_fsdata = cfe;
2695 list_add_tail(&cfe->node, &parent->files);
2696 cfe = NULL;
2697 }
2698 dput(dentry);
2699out:
2700 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002701 return error;
2702}
2703
Tejun Heo79578622012-04-01 12:09:56 -07002704static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002705 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002706{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002707 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002708 int err, ret = 0;
2709
2710 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002711 /* does cft->flags tell us to skip this file on @cgrp? */
2712 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2713 continue;
2714 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2715 continue;
2716
Li Zefan2739d3c2013-01-21 18:18:33 +08002717 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002718 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002719 if (err)
2720 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2721 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002722 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002723 } else {
2724 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002725 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002726 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002727 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002728}
2729
Tejun Heo8e3f6542012-04-01 12:09:55 -07002730static DEFINE_MUTEX(cgroup_cft_mutex);
2731
2732static void cgroup_cfts_prepare(void)
2733 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2734{
2735 /*
2736 * Thanks to the entanglement with vfs inode locking, we can't walk
2737 * the existing cgroups under cgroup_mutex and create files.
2738 * Instead, we increment reference on all cgroups and build list of
2739 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2740 * exclusive access to the field.
2741 */
2742 mutex_lock(&cgroup_cft_mutex);
2743 mutex_lock(&cgroup_mutex);
2744}
2745
2746static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002747 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002748 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2749{
2750 LIST_HEAD(pending);
2751 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002752
2753 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2754 if (cfts && ss->root != &rootnode) {
2755 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2756 dget(cgrp->dentry);
2757 list_add_tail(&cgrp->cft_q_node, &pending);
2758 }
2759 }
2760
2761 mutex_unlock(&cgroup_mutex);
2762
2763 /*
2764 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2765 * files for all cgroups which were created before.
2766 */
2767 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2768 struct inode *inode = cgrp->dentry->d_inode;
2769
2770 mutex_lock(&inode->i_mutex);
2771 mutex_lock(&cgroup_mutex);
2772 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002773 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002774 mutex_unlock(&cgroup_mutex);
2775 mutex_unlock(&inode->i_mutex);
2776
2777 list_del_init(&cgrp->cft_q_node);
2778 dput(cgrp->dentry);
2779 }
2780
2781 mutex_unlock(&cgroup_cft_mutex);
2782}
2783
2784/**
2785 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2786 * @ss: target cgroup subsystem
2787 * @cfts: zero-length name terminated array of cftypes
2788 *
2789 * Register @cfts to @ss. Files described by @cfts are created for all
2790 * existing cgroups to which @ss is attached and all future cgroups will
2791 * have them too. This function can be called anytime whether @ss is
2792 * attached or not.
2793 *
2794 * Returns 0 on successful registration, -errno on failure. Note that this
2795 * function currently returns 0 as long as @cfts registration is successful
2796 * even if some file creation attempts on existing cgroups fail.
2797 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002798int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002799{
2800 struct cftype_set *set;
2801
2802 set = kzalloc(sizeof(*set), GFP_KERNEL);
2803 if (!set)
2804 return -ENOMEM;
2805
2806 cgroup_cfts_prepare();
2807 set->cfts = cfts;
2808 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002809 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002810
2811 return 0;
2812}
2813EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2814
Li Zefana043e3b2008-02-23 15:24:09 -08002815/**
Tejun Heo79578622012-04-01 12:09:56 -07002816 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2817 * @ss: target cgroup subsystem
2818 * @cfts: zero-length name terminated array of cftypes
2819 *
2820 * Unregister @cfts from @ss. Files described by @cfts are removed from
2821 * all existing cgroups to which @ss is attached and all future cgroups
2822 * won't have them either. This function can be called anytime whether @ss
2823 * is attached or not.
2824 *
2825 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2826 * registered with @ss.
2827 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002828int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002829{
2830 struct cftype_set *set;
2831
2832 cgroup_cfts_prepare();
2833
2834 list_for_each_entry(set, &ss->cftsets, node) {
2835 if (set->cfts == cfts) {
2836 list_del_init(&set->node);
2837 cgroup_cfts_commit(ss, cfts, false);
2838 return 0;
2839 }
2840 }
2841
2842 cgroup_cfts_commit(ss, NULL, false);
2843 return -ENOENT;
2844}
2845
2846/**
Li Zefana043e3b2008-02-23 15:24:09 -08002847 * cgroup_task_count - count the number of tasks in a cgroup.
2848 * @cgrp: the cgroup in question
2849 *
2850 * Return the number of tasks in the cgroup.
2851 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002852int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002853{
2854 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002855 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002856
Paul Menage817929e2007-10-18 23:39:36 -07002857 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002858 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002859 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002860 }
2861 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002862 return count;
2863}
2864
2865/*
Paul Menage817929e2007-10-18 23:39:36 -07002866 * Advance a list_head iterator. The iterator should be positioned at
2867 * the start of a css_set
2868 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002869static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002870 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002871{
2872 struct list_head *l = it->cg_link;
2873 struct cg_cgroup_link *link;
2874 struct css_set *cg;
2875
2876 /* Advance to the next non-empty css_set */
2877 do {
2878 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002879 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002880 it->cg_link = NULL;
2881 return;
2882 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002883 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002884 cg = link->cg;
2885 } while (list_empty(&cg->tasks));
2886 it->cg_link = l;
2887 it->task = cg->tasks.next;
2888}
2889
Cliff Wickman31a7df02008-02-07 00:14:42 -08002890/*
2891 * To reduce the fork() overhead for systems that are not actually
2892 * using their cgroups capability, we don't maintain the lists running
2893 * through each css_set to its tasks until we see the list actually
2894 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002895 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002896static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002897{
2898 struct task_struct *p, *g;
2899 write_lock(&css_set_lock);
2900 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002901 /*
2902 * We need tasklist_lock because RCU is not safe against
2903 * while_each_thread(). Besides, a forking task that has passed
2904 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2905 * is not guaranteed to have its child immediately visible in the
2906 * tasklist if we walk through it with RCU.
2907 */
2908 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002909 do_each_thread(g, p) {
2910 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002911 /*
2912 * We should check if the process is exiting, otherwise
2913 * it will race with cgroup_exit() in that the list
2914 * entry won't be deleted though the process has exited.
2915 */
2916 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002917 list_add(&p->cg_list, &p->cgroups->tasks);
2918 task_unlock(p);
2919 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002920 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002921 write_unlock(&css_set_lock);
2922}
2923
Tejun Heo574bd9f2012-11-09 09:12:29 -08002924/**
2925 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2926 * @pos: the current position (%NULL to initiate traversal)
2927 * @cgroup: cgroup whose descendants to walk
2928 *
2929 * To be used by cgroup_for_each_descendant_pre(). Find the next
2930 * descendant to visit for pre-order traversal of @cgroup's descendants.
2931 */
2932struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2933 struct cgroup *cgroup)
2934{
2935 struct cgroup *next;
2936
2937 WARN_ON_ONCE(!rcu_read_lock_held());
2938
2939 /* if first iteration, pretend we just visited @cgroup */
2940 if (!pos) {
2941 if (list_empty(&cgroup->children))
2942 return NULL;
2943 pos = cgroup;
2944 }
2945
2946 /* visit the first child if exists */
2947 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2948 if (next)
2949 return next;
2950
2951 /* no child, visit my or the closest ancestor's next sibling */
2952 do {
2953 next = list_entry_rcu(pos->sibling.next, struct cgroup,
2954 sibling);
2955 if (&next->sibling != &pos->parent->children)
2956 return next;
2957
2958 pos = pos->parent;
2959 } while (pos != cgroup);
2960
2961 return NULL;
2962}
2963EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
2964
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002965/**
2966 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
2967 * @pos: cgroup of interest
2968 *
2969 * Return the rightmost descendant of @pos. If there's no descendant,
2970 * @pos is returned. This can be used during pre-order traversal to skip
2971 * subtree of @pos.
2972 */
2973struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
2974{
2975 struct cgroup *last, *tmp;
2976
2977 WARN_ON_ONCE(!rcu_read_lock_held());
2978
2979 do {
2980 last = pos;
2981 /* ->prev isn't RCU safe, walk ->next till the end */
2982 pos = NULL;
2983 list_for_each_entry_rcu(tmp, &last->children, sibling)
2984 pos = tmp;
2985 } while (pos);
2986
2987 return last;
2988}
2989EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
2990
Tejun Heo574bd9f2012-11-09 09:12:29 -08002991static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
2992{
2993 struct cgroup *last;
2994
2995 do {
2996 last = pos;
2997 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
2998 sibling);
2999 } while (pos);
3000
3001 return last;
3002}
3003
3004/**
3005 * cgroup_next_descendant_post - find the next descendant for post-order walk
3006 * @pos: the current position (%NULL to initiate traversal)
3007 * @cgroup: cgroup whose descendants to walk
3008 *
3009 * To be used by cgroup_for_each_descendant_post(). Find the next
3010 * descendant to visit for post-order traversal of @cgroup's descendants.
3011 */
3012struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3013 struct cgroup *cgroup)
3014{
3015 struct cgroup *next;
3016
3017 WARN_ON_ONCE(!rcu_read_lock_held());
3018
3019 /* if first iteration, visit the leftmost descendant */
3020 if (!pos) {
3021 next = cgroup_leftmost_descendant(cgroup);
3022 return next != cgroup ? next : NULL;
3023 }
3024
3025 /* if there's an unvisited sibling, visit its leftmost descendant */
3026 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3027 if (&next->sibling != &pos->parent->children)
3028 return cgroup_leftmost_descendant(next);
3029
3030 /* no sibling left, visit parent */
3031 next = pos->parent;
3032 return next != cgroup ? next : NULL;
3033}
3034EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3035
Paul Menagebd89aab2007-10-18 23:40:44 -07003036void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003037 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003038{
3039 /*
3040 * The first time anyone tries to iterate across a cgroup,
3041 * we need to enable the list linking each css_set to its
3042 * tasks, and fix up all existing tasks.
3043 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003044 if (!use_task_css_set_links)
3045 cgroup_enable_task_cg_lists();
3046
Paul Menage817929e2007-10-18 23:39:36 -07003047 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003048 it->cg_link = &cgrp->css_sets;
3049 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003050}
3051
Paul Menagebd89aab2007-10-18 23:40:44 -07003052struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003053 struct cgroup_iter *it)
3054{
3055 struct task_struct *res;
3056 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003057 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003058
3059 /* If the iterator cg is NULL, we have no tasks */
3060 if (!it->cg_link)
3061 return NULL;
3062 res = list_entry(l, struct task_struct, cg_list);
3063 /* Advance iterator to find next entry */
3064 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003065 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3066 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003067 /* We reached the end of this task list - move on to
3068 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003069 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003070 } else {
3071 it->task = l;
3072 }
3073 return res;
3074}
3075
Paul Menagebd89aab2007-10-18 23:40:44 -07003076void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003077 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003078{
3079 read_unlock(&css_set_lock);
3080}
3081
Cliff Wickman31a7df02008-02-07 00:14:42 -08003082static inline int started_after_time(struct task_struct *t1,
3083 struct timespec *time,
3084 struct task_struct *t2)
3085{
3086 int start_diff = timespec_compare(&t1->start_time, time);
3087 if (start_diff > 0) {
3088 return 1;
3089 } else if (start_diff < 0) {
3090 return 0;
3091 } else {
3092 /*
3093 * Arbitrarily, if two processes started at the same
3094 * time, we'll say that the lower pointer value
3095 * started first. Note that t2 may have exited by now
3096 * so this may not be a valid pointer any longer, but
3097 * that's fine - it still serves to distinguish
3098 * between two tasks started (effectively) simultaneously.
3099 */
3100 return t1 > t2;
3101 }
3102}
3103
3104/*
3105 * This function is a callback from heap_insert() and is used to order
3106 * the heap.
3107 * In this case we order the heap in descending task start time.
3108 */
3109static inline int started_after(void *p1, void *p2)
3110{
3111 struct task_struct *t1 = p1;
3112 struct task_struct *t2 = p2;
3113 return started_after_time(t1, &t2->start_time, t2);
3114}
3115
3116/**
3117 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3118 * @scan: struct cgroup_scanner containing arguments for the scan
3119 *
3120 * Arguments include pointers to callback functions test_task() and
3121 * process_task().
3122 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3123 * and if it returns true, call process_task() for it also.
3124 * The test_task pointer may be NULL, meaning always true (select all tasks).
3125 * Effectively duplicates cgroup_iter_{start,next,end}()
3126 * but does not lock css_set_lock for the call to process_task().
3127 * The struct cgroup_scanner may be embedded in any structure of the caller's
3128 * creation.
3129 * It is guaranteed that process_task() will act on every task that
3130 * is a member of the cgroup for the duration of this call. This
3131 * function may or may not call process_task() for tasks that exit
3132 * or move to a different cgroup during the call, or are forked or
3133 * move into the cgroup during the call.
3134 *
3135 * Note that test_task() may be called with locks held, and may in some
3136 * situations be called multiple times for the same task, so it should
3137 * be cheap.
3138 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3139 * pre-allocated and will be used for heap operations (and its "gt" member will
3140 * be overwritten), else a temporary heap will be used (allocation of which
3141 * may cause this function to fail).
3142 */
3143int cgroup_scan_tasks(struct cgroup_scanner *scan)
3144{
3145 int retval, i;
3146 struct cgroup_iter it;
3147 struct task_struct *p, *dropped;
3148 /* Never dereference latest_task, since it's not refcounted */
3149 struct task_struct *latest_task = NULL;
3150 struct ptr_heap tmp_heap;
3151 struct ptr_heap *heap;
3152 struct timespec latest_time = { 0, 0 };
3153
3154 if (scan->heap) {
3155 /* The caller supplied our heap and pre-allocated its memory */
3156 heap = scan->heap;
3157 heap->gt = &started_after;
3158 } else {
3159 /* We need to allocate our own heap memory */
3160 heap = &tmp_heap;
3161 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3162 if (retval)
3163 /* cannot allocate the heap */
3164 return retval;
3165 }
3166
3167 again:
3168 /*
3169 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3170 * to determine which are of interest, and using the scanner's
3171 * "process_task" callback to process any of them that need an update.
3172 * Since we don't want to hold any locks during the task updates,
3173 * gather tasks to be processed in a heap structure.
3174 * The heap is sorted by descending task start time.
3175 * If the statically-sized heap fills up, we overflow tasks that
3176 * started later, and in future iterations only consider tasks that
3177 * started after the latest task in the previous pass. This
3178 * guarantees forward progress and that we don't miss any tasks.
3179 */
3180 heap->size = 0;
3181 cgroup_iter_start(scan->cg, &it);
3182 while ((p = cgroup_iter_next(scan->cg, &it))) {
3183 /*
3184 * Only affect tasks that qualify per the caller's callback,
3185 * if he provided one
3186 */
3187 if (scan->test_task && !scan->test_task(p, scan))
3188 continue;
3189 /*
3190 * Only process tasks that started after the last task
3191 * we processed
3192 */
3193 if (!started_after_time(p, &latest_time, latest_task))
3194 continue;
3195 dropped = heap_insert(heap, p);
3196 if (dropped == NULL) {
3197 /*
3198 * The new task was inserted; the heap wasn't
3199 * previously full
3200 */
3201 get_task_struct(p);
3202 } else if (dropped != p) {
3203 /*
3204 * The new task was inserted, and pushed out a
3205 * different task
3206 */
3207 get_task_struct(p);
3208 put_task_struct(dropped);
3209 }
3210 /*
3211 * Else the new task was newer than anything already in
3212 * the heap and wasn't inserted
3213 */
3214 }
3215 cgroup_iter_end(scan->cg, &it);
3216
3217 if (heap->size) {
3218 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003219 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003220 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003221 latest_time = q->start_time;
3222 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003223 }
3224 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003225 scan->process_task(q, scan);
3226 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003227 }
3228 /*
3229 * If we had to process any tasks at all, scan again
3230 * in case some of them were in the middle of forking
3231 * children that didn't get processed.
3232 * Not the most efficient way to do it, but it avoids
3233 * having to take callback_mutex in the fork path
3234 */
3235 goto again;
3236 }
3237 if (heap == &tmp_heap)
3238 heap_free(&tmp_heap);
3239 return 0;
3240}
3241
Tejun Heo8cc99342013-04-07 09:29:50 -07003242static void cgroup_transfer_one_task(struct task_struct *task,
3243 struct cgroup_scanner *scan)
3244{
3245 struct cgroup *new_cgroup = scan->data;
3246
Tejun Heo47cfcd02013-04-07 09:29:51 -07003247 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003248 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003249 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003250}
3251
3252/**
3253 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3254 * @to: cgroup to which the tasks will be moved
3255 * @from: cgroup in which the tasks currently reside
3256 */
3257int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3258{
3259 struct cgroup_scanner scan;
3260
3261 scan.cg = from;
3262 scan.test_task = NULL; /* select all tasks in cgroup */
3263 scan.process_task = cgroup_transfer_one_task;
3264 scan.heap = NULL;
3265 scan.data = to;
3266
3267 return cgroup_scan_tasks(&scan);
3268}
3269
Paul Menage817929e2007-10-18 23:39:36 -07003270/*
Ben Blum102a7752009-09-23 15:56:26 -07003271 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003272 *
3273 * Reading this file can return large amounts of data if a cgroup has
3274 * *lots* of attached tasks. So it may need several calls to read(),
3275 * but we cannot guarantee that the information we produce is correct
3276 * unless we produce it entirely atomically.
3277 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003278 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003279
Li Zefan24528252012-01-20 11:58:43 +08003280/* which pidlist file are we talking about? */
3281enum cgroup_filetype {
3282 CGROUP_FILE_PROCS,
3283 CGROUP_FILE_TASKS,
3284};
3285
3286/*
3287 * A pidlist is a list of pids that virtually represents the contents of one
3288 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3289 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3290 * to the cgroup.
3291 */
3292struct cgroup_pidlist {
3293 /*
3294 * used to find which pidlist is wanted. doesn't change as long as
3295 * this particular list stays in the list.
3296 */
3297 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3298 /* array of xids */
3299 pid_t *list;
3300 /* how many elements the above list has */
3301 int length;
3302 /* how many files are using the current array */
3303 int use_count;
3304 /* each of these stored in a list by its cgroup */
3305 struct list_head links;
3306 /* pointer to the cgroup we belong to, for list removal purposes */
3307 struct cgroup *owner;
3308 /* protects the other fields */
3309 struct rw_semaphore mutex;
3310};
3311
Paul Menagebbcb81d2007-10-18 23:39:32 -07003312/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003313 * The following two functions "fix" the issue where there are more pids
3314 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3315 * TODO: replace with a kernel-wide solution to this problem
3316 */
3317#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3318static void *pidlist_allocate(int count)
3319{
3320 if (PIDLIST_TOO_LARGE(count))
3321 return vmalloc(count * sizeof(pid_t));
3322 else
3323 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3324}
3325static void pidlist_free(void *p)
3326{
3327 if (is_vmalloc_addr(p))
3328 vfree(p);
3329 else
3330 kfree(p);
3331}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003332
3333/*
Ben Blum102a7752009-09-23 15:56:26 -07003334 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003335 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003336 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003337static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003338{
Ben Blum102a7752009-09-23 15:56:26 -07003339 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003340
3341 /*
3342 * we presume the 0th element is unique, so i starts at 1. trivial
3343 * edge cases first; no work needs to be done for either
3344 */
3345 if (length == 0 || length == 1)
3346 return length;
3347 /* src and dest walk down the list; dest counts unique elements */
3348 for (src = 1; src < length; src++) {
3349 /* find next unique element */
3350 while (list[src] == list[src-1]) {
3351 src++;
3352 if (src == length)
3353 goto after;
3354 }
3355 /* dest always points to where the next unique element goes */
3356 list[dest] = list[src];
3357 dest++;
3358 }
3359after:
Ben Blum102a7752009-09-23 15:56:26 -07003360 return dest;
3361}
3362
3363static int cmppid(const void *a, const void *b)
3364{
3365 return *(pid_t *)a - *(pid_t *)b;
3366}
3367
3368/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003369 * find the appropriate pidlist for our purpose (given procs vs tasks)
3370 * returns with the lock on that pidlist already held, and takes care
3371 * of the use count, or returns NULL with no locks held if we're out of
3372 * memory.
3373 */
3374static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3375 enum cgroup_filetype type)
3376{
3377 struct cgroup_pidlist *l;
3378 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003379 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003380
Ben Blum72a8cb32009-09-23 15:56:27 -07003381 /*
3382 * We can't drop the pidlist_mutex before taking the l->mutex in case
3383 * the last ref-holder is trying to remove l from the list at the same
3384 * time. Holding the pidlist_mutex precludes somebody taking whichever
3385 * list we find out from under us - compare release_pid_array().
3386 */
3387 mutex_lock(&cgrp->pidlist_mutex);
3388 list_for_each_entry(l, &cgrp->pidlists, links) {
3389 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003390 /* make sure l doesn't vanish out from under us */
3391 down_write(&l->mutex);
3392 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003393 return l;
3394 }
3395 }
3396 /* entry not found; create a new one */
3397 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3398 if (!l) {
3399 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003400 return l;
3401 }
3402 init_rwsem(&l->mutex);
3403 down_write(&l->mutex);
3404 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003405 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003406 l->use_count = 0; /* don't increment here */
3407 l->list = NULL;
3408 l->owner = cgrp;
3409 list_add(&l->links, &cgrp->pidlists);
3410 mutex_unlock(&cgrp->pidlist_mutex);
3411 return l;
3412}
3413
3414/*
Ben Blum102a7752009-09-23 15:56:26 -07003415 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3416 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003417static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3418 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003419{
3420 pid_t *array;
3421 int length;
3422 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003423 struct cgroup_iter it;
3424 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003425 struct cgroup_pidlist *l;
3426
3427 /*
3428 * If cgroup gets more users after we read count, we won't have
3429 * enough space - tough. This race is indistinguishable to the
3430 * caller from the case that the additional cgroup users didn't
3431 * show up until sometime later on.
3432 */
3433 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003434 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003435 if (!array)
3436 return -ENOMEM;
3437 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003438 cgroup_iter_start(cgrp, &it);
3439 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003440 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003441 break;
Ben Blum102a7752009-09-23 15:56:26 -07003442 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003443 if (type == CGROUP_FILE_PROCS)
3444 pid = task_tgid_vnr(tsk);
3445 else
3446 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003447 if (pid > 0) /* make sure to only use valid results */
3448 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003449 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003450 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003451 length = n;
3452 /* now sort & (if procs) strip out duplicates */
3453 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003454 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003455 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003456 l = cgroup_pidlist_find(cgrp, type);
3457 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003458 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003459 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003460 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003461 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003462 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003463 l->list = array;
3464 l->length = length;
3465 l->use_count++;
3466 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003467 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003468 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003469}
3470
Balbir Singh846c7bb2007-10-18 23:39:44 -07003471/**
Li Zefana043e3b2008-02-23 15:24:09 -08003472 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003473 * @stats: cgroupstats to fill information into
3474 * @dentry: A dentry entry belonging to the cgroup for which stats have
3475 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003476 *
3477 * Build and fill cgroupstats so that taskstats can export it to user
3478 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003479 */
3480int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3481{
3482 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003483 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003484 struct cgroup_iter it;
3485 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003486
Balbir Singh846c7bb2007-10-18 23:39:44 -07003487 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003488 * Validate dentry by checking the superblock operations,
3489 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003490 */
Li Zefan33d283b2008-11-19 15:36:48 -08003491 if (dentry->d_sb->s_op != &cgroup_ops ||
3492 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003493 goto err;
3494
3495 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003496 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003497
Paul Menagebd89aab2007-10-18 23:40:44 -07003498 cgroup_iter_start(cgrp, &it);
3499 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003500 switch (tsk->state) {
3501 case TASK_RUNNING:
3502 stats->nr_running++;
3503 break;
3504 case TASK_INTERRUPTIBLE:
3505 stats->nr_sleeping++;
3506 break;
3507 case TASK_UNINTERRUPTIBLE:
3508 stats->nr_uninterruptible++;
3509 break;
3510 case TASK_STOPPED:
3511 stats->nr_stopped++;
3512 break;
3513 default:
3514 if (delayacct_is_task_waiting_on_io(tsk))
3515 stats->nr_io_wait++;
3516 break;
3517 }
3518 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003519 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003520
Balbir Singh846c7bb2007-10-18 23:39:44 -07003521err:
3522 return ret;
3523}
3524
Paul Menage8f3ff202009-09-23 15:56:25 -07003525
Paul Menagecc31edc2008-10-18 20:28:04 -07003526/*
Ben Blum102a7752009-09-23 15:56:26 -07003527 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003528 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003529 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003530 */
3531
Ben Blum102a7752009-09-23 15:56:26 -07003532static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003533{
3534 /*
3535 * Initially we receive a position value that corresponds to
3536 * one more than the last pid shown (or 0 on the first call or
3537 * after a seek to the start). Use a binary-search to find the
3538 * next pid to display, if any
3539 */
Ben Blum102a7752009-09-23 15:56:26 -07003540 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003541 int index = 0, pid = *pos;
3542 int *iter;
3543
Ben Blum102a7752009-09-23 15:56:26 -07003544 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003545 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003546 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003547
Paul Menagecc31edc2008-10-18 20:28:04 -07003548 while (index < end) {
3549 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003550 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003551 index = mid;
3552 break;
Ben Blum102a7752009-09-23 15:56:26 -07003553 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003554 index = mid + 1;
3555 else
3556 end = mid;
3557 }
3558 }
3559 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003560 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003561 return NULL;
3562 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003563 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003564 *pos = *iter;
3565 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003566}
3567
Ben Blum102a7752009-09-23 15:56:26 -07003568static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003569{
Ben Blum102a7752009-09-23 15:56:26 -07003570 struct cgroup_pidlist *l = s->private;
3571 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003572}
3573
Ben Blum102a7752009-09-23 15:56:26 -07003574static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003575{
Ben Blum102a7752009-09-23 15:56:26 -07003576 struct cgroup_pidlist *l = s->private;
3577 pid_t *p = v;
3578 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003579 /*
3580 * Advance to the next pid in the array. If this goes off the
3581 * end, we're done
3582 */
3583 p++;
3584 if (p >= end) {
3585 return NULL;
3586 } else {
3587 *pos = *p;
3588 return p;
3589 }
3590}
3591
Ben Blum102a7752009-09-23 15:56:26 -07003592static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003593{
3594 return seq_printf(s, "%d\n", *(int *)v);
3595}
3596
Ben Blum102a7752009-09-23 15:56:26 -07003597/*
3598 * seq_operations functions for iterating on pidlists through seq_file -
3599 * independent of whether it's tasks or procs
3600 */
3601static const struct seq_operations cgroup_pidlist_seq_operations = {
3602 .start = cgroup_pidlist_start,
3603 .stop = cgroup_pidlist_stop,
3604 .next = cgroup_pidlist_next,
3605 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003606};
3607
Ben Blum102a7752009-09-23 15:56:26 -07003608static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003609{
Ben Blum72a8cb32009-09-23 15:56:27 -07003610 /*
3611 * the case where we're the last user of this particular pidlist will
3612 * have us remove it from the cgroup's list, which entails taking the
3613 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3614 * pidlist_mutex, we have to take pidlist_mutex first.
3615 */
3616 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003617 down_write(&l->mutex);
3618 BUG_ON(!l->use_count);
3619 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003620 /* we're the last user if refcount is 0; remove and free */
3621 list_del(&l->links);
3622 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003623 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003624 put_pid_ns(l->key.ns);
3625 up_write(&l->mutex);
3626 kfree(l);
3627 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003628 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003629 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003630 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003631}
3632
Ben Blum102a7752009-09-23 15:56:26 -07003633static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003634{
Ben Blum102a7752009-09-23 15:56:26 -07003635 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003636 if (!(file->f_mode & FMODE_READ))
3637 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003638 /*
3639 * the seq_file will only be initialized if the file was opened for
3640 * reading; hence we check if it's not null only in that case.
3641 */
3642 l = ((struct seq_file *)file->private_data)->private;
3643 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003644 return seq_release(inode, file);
3645}
3646
Ben Blum102a7752009-09-23 15:56:26 -07003647static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003648 .read = seq_read,
3649 .llseek = seq_lseek,
3650 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003651 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003652};
3653
3654/*
Ben Blum102a7752009-09-23 15:56:26 -07003655 * The following functions handle opens on a file that displays a pidlist
3656 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3657 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003658 */
Ben Blum102a7752009-09-23 15:56:26 -07003659/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003660static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003661{
3662 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003663 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003664 int retval;
3665
3666 /* Nothing to do for write-only files */
3667 if (!(file->f_mode & FMODE_READ))
3668 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003669
Ben Blum102a7752009-09-23 15:56:26 -07003670 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003671 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003672 if (retval)
3673 return retval;
3674 /* configure file information */
3675 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003676
Ben Blum102a7752009-09-23 15:56:26 -07003677 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003678 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003679 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003680 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003681 }
Ben Blum102a7752009-09-23 15:56:26 -07003682 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003683 return 0;
3684}
Ben Blum102a7752009-09-23 15:56:26 -07003685static int cgroup_tasks_open(struct inode *unused, struct file *file)
3686{
Ben Blum72a8cb32009-09-23 15:56:27 -07003687 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003688}
3689static int cgroup_procs_open(struct inode *unused, struct file *file)
3690{
Ben Blum72a8cb32009-09-23 15:56:27 -07003691 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003692}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003693
Paul Menagebd89aab2007-10-18 23:40:44 -07003694static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003695 struct cftype *cft)
3696{
Paul Menagebd89aab2007-10-18 23:40:44 -07003697 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003698}
3699
Paul Menage6379c102008-07-25 01:47:01 -07003700static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3701 struct cftype *cft,
3702 u64 val)
3703{
3704 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3705 if (val)
3706 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3707 else
3708 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3709 return 0;
3710}
3711
Paul Menagebbcb81d2007-10-18 23:39:32 -07003712/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003713 * Unregister event and free resources.
3714 *
3715 * Gets called from workqueue.
3716 */
3717static void cgroup_event_remove(struct work_struct *work)
3718{
3719 struct cgroup_event *event = container_of(work, struct cgroup_event,
3720 remove);
3721 struct cgroup *cgrp = event->cgrp;
3722
Li Zefan810cbee2013-02-18 18:56:14 +08003723 remove_wait_queue(event->wqh, &event->wait);
3724
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003725 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3726
Li Zefan810cbee2013-02-18 18:56:14 +08003727 /* Notify userspace the event is going away. */
3728 eventfd_signal(event->eventfd, 1);
3729
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003730 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003731 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003732 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003733}
3734
3735/*
3736 * Gets called on POLLHUP on eventfd when user closes it.
3737 *
3738 * Called with wqh->lock held and interrupts disabled.
3739 */
3740static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3741 int sync, void *key)
3742{
3743 struct cgroup_event *event = container_of(wait,
3744 struct cgroup_event, wait);
3745 struct cgroup *cgrp = event->cgrp;
3746 unsigned long flags = (unsigned long)key;
3747
3748 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003749 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003750 * If the event has been detached at cgroup removal, we
3751 * can simply return knowing the other side will cleanup
3752 * for us.
3753 *
3754 * We can't race against event freeing since the other
3755 * side will require wqh->lock via remove_wait_queue(),
3756 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003757 */
Li Zefan810cbee2013-02-18 18:56:14 +08003758 spin_lock(&cgrp->event_list_lock);
3759 if (!list_empty(&event->list)) {
3760 list_del_init(&event->list);
3761 /*
3762 * We are in atomic context, but cgroup_event_remove()
3763 * may sleep, so we have to call it in workqueue.
3764 */
3765 schedule_work(&event->remove);
3766 }
3767 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003768 }
3769
3770 return 0;
3771}
3772
3773static void cgroup_event_ptable_queue_proc(struct file *file,
3774 wait_queue_head_t *wqh, poll_table *pt)
3775{
3776 struct cgroup_event *event = container_of(pt,
3777 struct cgroup_event, pt);
3778
3779 event->wqh = wqh;
3780 add_wait_queue(wqh, &event->wait);
3781}
3782
3783/*
3784 * Parse input and register new cgroup event handler.
3785 *
3786 * Input must be in format '<event_fd> <control_fd> <args>'.
3787 * Interpretation of args is defined by control file implementation.
3788 */
3789static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3790 const char *buffer)
3791{
3792 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003793 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003794 unsigned int efd, cfd;
3795 struct file *efile = NULL;
3796 struct file *cfile = NULL;
3797 char *endp;
3798 int ret;
3799
3800 efd = simple_strtoul(buffer, &endp, 10);
3801 if (*endp != ' ')
3802 return -EINVAL;
3803 buffer = endp + 1;
3804
3805 cfd = simple_strtoul(buffer, &endp, 10);
3806 if ((*endp != ' ') && (*endp != '\0'))
3807 return -EINVAL;
3808 buffer = endp + 1;
3809
3810 event = kzalloc(sizeof(*event), GFP_KERNEL);
3811 if (!event)
3812 return -ENOMEM;
3813 event->cgrp = cgrp;
3814 INIT_LIST_HEAD(&event->list);
3815 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3816 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3817 INIT_WORK(&event->remove, cgroup_event_remove);
3818
3819 efile = eventfd_fget(efd);
3820 if (IS_ERR(efile)) {
3821 ret = PTR_ERR(efile);
3822 goto fail;
3823 }
3824
3825 event->eventfd = eventfd_ctx_fileget(efile);
3826 if (IS_ERR(event->eventfd)) {
3827 ret = PTR_ERR(event->eventfd);
3828 goto fail;
3829 }
3830
3831 cfile = fget(cfd);
3832 if (!cfile) {
3833 ret = -EBADF;
3834 goto fail;
3835 }
3836
3837 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003838 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003839 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003840 if (ret < 0)
3841 goto fail;
3842
3843 event->cft = __file_cft(cfile);
3844 if (IS_ERR(event->cft)) {
3845 ret = PTR_ERR(event->cft);
3846 goto fail;
3847 }
3848
Li Zefanf1690072013-02-18 14:13:35 +08003849 /*
3850 * The file to be monitored must be in the same cgroup as
3851 * cgroup.event_control is.
3852 */
3853 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3854 if (cgrp_cfile != cgrp) {
3855 ret = -EINVAL;
3856 goto fail;
3857 }
3858
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003859 if (!event->cft->register_event || !event->cft->unregister_event) {
3860 ret = -EINVAL;
3861 goto fail;
3862 }
3863
3864 ret = event->cft->register_event(cgrp, event->cft,
3865 event->eventfd, buffer);
3866 if (ret)
3867 goto fail;
3868
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003869 /*
3870 * Events should be removed after rmdir of cgroup directory, but before
3871 * destroying subsystem state objects. Let's take reference to cgroup
3872 * directory dentry to do that.
3873 */
3874 dget(cgrp->dentry);
3875
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003876 spin_lock(&cgrp->event_list_lock);
3877 list_add(&event->list, &cgrp->event_list);
3878 spin_unlock(&cgrp->event_list_lock);
3879
3880 fput(cfile);
3881 fput(efile);
3882
3883 return 0;
3884
3885fail:
3886 if (cfile)
3887 fput(cfile);
3888
3889 if (event && event->eventfd && !IS_ERR(event->eventfd))
3890 eventfd_ctx_put(event->eventfd);
3891
3892 if (!IS_ERR_OR_NULL(efile))
3893 fput(efile);
3894
3895 kfree(event);
3896
3897 return ret;
3898}
3899
Daniel Lezcano97978e62010-10-27 15:33:35 -07003900static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3901 struct cftype *cft)
3902{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003903 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003904}
3905
3906static int cgroup_clone_children_write(struct cgroup *cgrp,
3907 struct cftype *cft,
3908 u64 val)
3909{
3910 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003911 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003912 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003913 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003914 return 0;
3915}
3916
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003917/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003918 * for the common functions, 'private' gives the type of file
3919 */
Ben Blum102a7752009-09-23 15:56:26 -07003920/* for hysterical raisins, we can't put this on the older files */
3921#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003922static struct cftype files[] = {
3923 {
3924 .name = "tasks",
3925 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003926 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003927 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003928 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003929 },
Ben Blum102a7752009-09-23 15:56:26 -07003930 {
3931 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3932 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003933 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003934 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003935 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003936 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003937 {
3938 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003939 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003940 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003941 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003942 {
3943 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3944 .write_string = cgroup_write_event_control,
3945 .mode = S_IWUGO,
3946 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003947 {
3948 .name = "cgroup.clone_children",
3949 .read_u64 = cgroup_clone_children_read,
3950 .write_u64 = cgroup_clone_children_write,
3951 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003952 {
3953 .name = "release_agent",
3954 .flags = CFTYPE_ONLY_ON_ROOT,
3955 .read_seq_string = cgroup_release_agent_show,
3956 .write_string = cgroup_release_agent_write,
3957 .max_write_len = PATH_MAX,
3958 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003959 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003960};
3961
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003962/**
3963 * cgroup_populate_dir - selectively creation of files in a directory
3964 * @cgrp: target cgroup
3965 * @base_files: true if the base files should be added
3966 * @subsys_mask: mask of the subsystem ids whose files should be added
3967 */
3968static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3969 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003970{
3971 int err;
3972 struct cgroup_subsys *ss;
3973
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003974 if (base_files) {
3975 err = cgroup_addrm_files(cgrp, NULL, files, true);
3976 if (err < 0)
3977 return err;
3978 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07003979
Tejun Heo8e3f6542012-04-01 12:09:55 -07003980 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07003981 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003982 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003983 if (!test_bit(ss->subsys_id, &subsys_mask))
3984 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003985
Tejun Heodb0416b2012-04-01 12:09:55 -07003986 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07003987 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003988 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07003989
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003990 /* This cgroup is ready now */
3991 for_each_subsys(cgrp->root, ss) {
3992 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3993 /*
3994 * Update id->css pointer and make this css visible from
3995 * CSS ID functions. This pointer will be dereferened
3996 * from RCU-read-side without locks.
3997 */
3998 if (css->id)
3999 rcu_assign_pointer(css->id->css, css);
4000 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004001
4002 return 0;
4003}
4004
Tejun Heo48ddbe12012-04-01 12:09:56 -07004005static void css_dput_fn(struct work_struct *work)
4006{
4007 struct cgroup_subsys_state *css =
4008 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004009 struct dentry *dentry = css->cgroup->dentry;
4010 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004011
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004012 atomic_inc(&sb->s_active);
4013 dput(dentry);
4014 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004015}
4016
Paul Menageddbcc7e2007-10-18 23:39:30 -07004017static void init_cgroup_css(struct cgroup_subsys_state *css,
4018 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004019 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004020{
Paul Menagebd89aab2007-10-18 23:40:44 -07004021 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004022 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004023 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004024 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004025 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004026 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004027 BUG_ON(cgrp->subsys[ss->subsys_id]);
4028 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004029
4030 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004031 * css holds an extra ref to @cgrp->dentry which is put on the last
4032 * css_put(). dput() requires process context, which css_put() may
4033 * be called without. @css->dput_work will be used to invoke
4034 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004035 */
4036 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004037}
4038
Tejun Heob1929db2012-11-19 08:13:38 -08004039/* invoke ->post_create() on a new CSS and mark it online if successful */
4040static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004041{
Tejun Heob1929db2012-11-19 08:13:38 -08004042 int ret = 0;
4043
Tejun Heoa31f2d32012-11-19 08:13:37 -08004044 lockdep_assert_held(&cgroup_mutex);
4045
Tejun Heo92fb9742012-11-19 08:13:38 -08004046 if (ss->css_online)
4047 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004048 if (!ret)
4049 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4050 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004051}
4052
4053/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4054static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4055 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4056{
4057 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4058
4059 lockdep_assert_held(&cgroup_mutex);
4060
4061 if (!(css->flags & CSS_ONLINE))
4062 return;
4063
Li Zefand7eeac12013-03-12 15:35:59 -07004064 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004065 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004066
4067 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4068}
4069
Paul Menageddbcc7e2007-10-18 23:39:30 -07004070/*
Li Zefana043e3b2008-02-23 15:24:09 -08004071 * cgroup_create - create a cgroup
4072 * @parent: cgroup that will be parent of the new cgroup
4073 * @dentry: dentry of the new cgroup
4074 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004075 *
Li Zefana043e3b2008-02-23 15:24:09 -08004076 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004077 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004078static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004079 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004080{
Paul Menagebd89aab2007-10-18 23:40:44 -07004081 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004082 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004083 struct cgroupfs_root *root = parent->root;
4084 int err = 0;
4085 struct cgroup_subsys *ss;
4086 struct super_block *sb = root->sb;
4087
Tejun Heo0a950f62012-11-19 09:02:12 -08004088 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004089 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4090 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004091 return -ENOMEM;
4092
Li Zefan65dff752013-03-01 15:01:56 +08004093 name = cgroup_alloc_name(dentry);
4094 if (!name)
4095 goto err_free_cgrp;
4096 rcu_assign_pointer(cgrp->name, name);
4097
Tejun Heo0a950f62012-11-19 09:02:12 -08004098 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4099 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004100 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004101
Tejun Heo976c06b2012-11-05 09:16:59 -08004102 /*
4103 * Only live parents can have children. Note that the liveliness
4104 * check isn't strictly necessary because cgroup_mkdir() and
4105 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4106 * anyway so that locking is contained inside cgroup proper and we
4107 * don't get nasty surprises if we ever grow another caller.
4108 */
4109 if (!cgroup_lock_live_group(parent)) {
4110 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004111 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004112 }
4113
Paul Menageddbcc7e2007-10-18 23:39:30 -07004114 /* Grab a reference on the superblock so the hierarchy doesn't
4115 * get deleted on unmount if there are child cgroups. This
4116 * can be done outside cgroup_mutex, since the sb can't
4117 * disappear while someone has an open control file on the
4118 * fs */
4119 atomic_inc(&sb->s_active);
4120
Paul Menagecc31edc2008-10-18 20:28:04 -07004121 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004122
Li Zefanfe1c06c2013-01-24 14:30:22 +08004123 dentry->d_fsdata = cgrp;
4124 cgrp->dentry = dentry;
4125
Paul Menagebd89aab2007-10-18 23:40:44 -07004126 cgrp->parent = parent;
4127 cgrp->root = parent->root;
4128 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004129
Li Zefanb6abdb02008-03-04 14:28:19 -08004130 if (notify_on_release(parent))
4131 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4132
Tejun Heo2260e7f2012-11-19 08:13:38 -08004133 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4134 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004135
Paul Menageddbcc7e2007-10-18 23:39:30 -07004136 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004137 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004138
Tejun Heo92fb9742012-11-19 08:13:38 -08004139 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004140 if (IS_ERR(css)) {
4141 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004142 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004143 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004144 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004145 if (ss->use_id) {
4146 err = alloc_css_id(ss, parent, cgrp);
4147 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004148 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004149 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004150 }
4151
Tejun Heo4e139af2012-11-19 08:13:36 -08004152 /*
4153 * Create directory. cgroup_create_file() returns with the new
4154 * directory locked on success so that it can be populated without
4155 * dropping cgroup_mutex.
4156 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004157 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004158 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004159 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004160 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004161
Tejun Heo4e139af2012-11-19 08:13:36 -08004162 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004163 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4164 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4165 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004166
Tejun Heob1929db2012-11-19 08:13:38 -08004167 /* each css holds a ref to the cgroup's dentry */
4168 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004169 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004170
Tejun Heob1929db2012-11-19 08:13:38 -08004171 /* creation succeeded, notify subsystems */
4172 for_each_subsys(root, ss) {
4173 err = online_css(ss, cgrp);
4174 if (err)
4175 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004176
4177 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4178 parent->parent) {
4179 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",
4180 current->comm, current->pid, ss->name);
4181 if (!strcmp(ss->name, "memory"))
4182 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4183 ss->warned_broken_hierarchy = true;
4184 }
Tejun Heoa8638032012-11-09 09:12:29 -08004185 }
4186
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004187 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004188 if (err)
4189 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004190
4191 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004192 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004193
4194 return 0;
4195
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004196err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004197 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004198 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004199 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004200 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004201 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004202 /* Release the reference count that we took on the superblock */
4203 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004204err_free_id:
4205 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004206err_free_name:
4207 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004208err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004209 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004210 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004211
4212err_destroy:
4213 cgroup_destroy_locked(cgrp);
4214 mutex_unlock(&cgroup_mutex);
4215 mutex_unlock(&dentry->d_inode->i_mutex);
4216 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004217}
4218
Al Viro18bb1db2011-07-26 01:41:39 -04004219static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004220{
4221 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4222
4223 /* the vfs holds inode->i_mutex already */
4224 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4225}
4226
Tejun Heo42809dd2012-11-19 08:13:37 -08004227static int cgroup_destroy_locked(struct cgroup *cgrp)
4228 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004229{
Tejun Heo42809dd2012-11-19 08:13:37 -08004230 struct dentry *d = cgrp->dentry;
4231 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004232 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004233 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004234
Tejun Heo42809dd2012-11-19 08:13:37 -08004235 lockdep_assert_held(&d->d_inode->i_mutex);
4236 lockdep_assert_held(&cgroup_mutex);
4237
4238 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004239 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004240
Tejun Heo1a90dd52012-11-05 09:16:59 -08004241 /*
4242 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4243 * removed. This makes future css_tryget() and child creation
4244 * attempts fail thus maintaining the removal conditions verified
4245 * above.
4246 */
Tejun Heoed9577932012-11-05 09:16:58 -08004247 for_each_subsys(cgrp->root, ss) {
4248 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4249
4250 WARN_ON(atomic_read(&css->refcnt) < 0);
4251 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004252 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004253 set_bit(CGRP_REMOVED, &cgrp->flags);
4254
Tejun Heoa31f2d32012-11-19 08:13:37 -08004255 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004256 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004257 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004258
4259 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004260 * Put all the base refs. Each css holds an extra reference to the
4261 * cgroup's dentry and cgroup removal proceeds regardless of css
4262 * refs. On the last put of each css, whenever that may be, the
4263 * extra dentry ref is put so that dentry destruction happens only
4264 * after all css's are released.
4265 */
Tejun Heoe9316082012-11-05 09:16:58 -08004266 for_each_subsys(cgrp->root, ss)
4267 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004268
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004269 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004270 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004271 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004272 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004273
Paul Menage999cd8a2009-01-07 18:08:36 -08004274 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004275 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004276 list_del_init(&cgrp->allcg_node);
4277
Tejun Heo42809dd2012-11-19 08:13:37 -08004278 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004279 cgroup_d_remove_dir(d);
4280 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004281
Paul Menagebd89aab2007-10-18 23:40:44 -07004282 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004283 check_for_release(parent);
4284
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004285 /*
4286 * Unregister events and notify userspace.
4287 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004288 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004289 */
4290 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004291 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004292 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004293 schedule_work(&event->remove);
4294 }
Li Zefan810cbee2013-02-18 18:56:14 +08004295 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004296
Paul Menageddbcc7e2007-10-18 23:39:30 -07004297 return 0;
4298}
4299
Tejun Heo42809dd2012-11-19 08:13:37 -08004300static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4301{
4302 int ret;
4303
4304 mutex_lock(&cgroup_mutex);
4305 ret = cgroup_destroy_locked(dentry->d_fsdata);
4306 mutex_unlock(&cgroup_mutex);
4307
4308 return ret;
4309}
4310
Tejun Heo8e3f6542012-04-01 12:09:55 -07004311static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4312{
4313 INIT_LIST_HEAD(&ss->cftsets);
4314
4315 /*
4316 * base_cftset is embedded in subsys itself, no need to worry about
4317 * deregistration.
4318 */
4319 if (ss->base_cftypes) {
4320 ss->base_cftset.cfts = ss->base_cftypes;
4321 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4322 }
4323}
4324
Li Zefan06a11922008-04-29 01:00:07 -07004325static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004326{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004327 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004328
4329 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004330
Tejun Heo648bb562012-11-19 08:13:36 -08004331 mutex_lock(&cgroup_mutex);
4332
Tejun Heo8e3f6542012-04-01 12:09:55 -07004333 /* init base cftset */
4334 cgroup_init_cftsets(ss);
4335
Paul Menageddbcc7e2007-10-18 23:39:30 -07004336 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004337 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004338 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004339 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004340 /* We don't handle early failures gracefully */
4341 BUG_ON(IS_ERR(css));
4342 init_cgroup_css(css, ss, dummytop);
4343
Li Zefane8d55fd2008-04-29 01:00:13 -07004344 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004345 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004346 * newly registered, all tasks and hence the
4347 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004348 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004349
4350 need_forkexit_callback |= ss->fork || ss->exit;
4351
Li Zefane8d55fd2008-04-29 01:00:13 -07004352 /* At system boot, before all subsystems have been
4353 * registered, no tasks have been forked, so we don't
4354 * need to invoke fork callbacks here. */
4355 BUG_ON(!list_empty(&init_task.tasks));
4356
Paul Menageddbcc7e2007-10-18 23:39:30 -07004357 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004358 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004359
Tejun Heo648bb562012-11-19 08:13:36 -08004360 mutex_unlock(&cgroup_mutex);
4361
Ben Blume6a11052010-03-10 15:22:09 -08004362 /* this function shouldn't be used with modular subsystems, since they
4363 * need to register a subsys_id, among other things */
4364 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004365}
4366
4367/**
Ben Blume6a11052010-03-10 15:22:09 -08004368 * cgroup_load_subsys: load and register a modular subsystem at runtime
4369 * @ss: the subsystem to load
4370 *
4371 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004372 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004373 * up for use. If the subsystem is built-in anyway, work is delegated to the
4374 * simpler cgroup_init_subsys.
4375 */
4376int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4377{
Ben Blume6a11052010-03-10 15:22:09 -08004378 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004379 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004380 struct hlist_node *tmp;
Li Zefan0ac801f2013-01-10 11:49:27 +08004381 struct css_set *cg;
4382 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004383
4384 /* check name and function validity */
4385 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004386 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004387 return -EINVAL;
4388
4389 /*
4390 * we don't support callbacks in modular subsystems. this check is
4391 * before the ss->module check for consistency; a subsystem that could
4392 * be a module should still have no callbacks even if the user isn't
4393 * compiling it as one.
4394 */
4395 if (ss->fork || ss->exit)
4396 return -EINVAL;
4397
4398 /*
4399 * an optionally modular subsystem is built-in: we want to do nothing,
4400 * since cgroup_init_subsys will have already taken care of it.
4401 */
4402 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004403 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004404 BUG_ON(subsys[ss->subsys_id] != ss);
4405 return 0;
4406 }
4407
Tejun Heo8e3f6542012-04-01 12:09:55 -07004408 /* init base cftset */
4409 cgroup_init_cftsets(ss);
4410
Ben Blume6a11052010-03-10 15:22:09 -08004411 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004412 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004413
4414 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004415 * no ss->css_alloc seems to need anything important in the ss
4416 * struct, so this can happen first (i.e. before the rootnode
4417 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004418 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004419 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004420 if (IS_ERR(css)) {
4421 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004422 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004423 mutex_unlock(&cgroup_mutex);
4424 return PTR_ERR(css);
4425 }
4426
4427 list_add(&ss->sibling, &rootnode.subsys_list);
4428 ss->root = &rootnode;
4429
4430 /* our new subsystem will be attached to the dummy hierarchy. */
4431 init_cgroup_css(css, ss, dummytop);
4432 /* init_idr must be after init_cgroup_css because it sets css->id. */
4433 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004434 ret = cgroup_init_idr(ss, css);
4435 if (ret)
4436 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004437 }
4438
4439 /*
4440 * Now we need to entangle the css into the existing css_sets. unlike
4441 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4442 * will need a new pointer to it; done by iterating the css_set_table.
4443 * furthermore, modifying the existing css_sets will corrupt the hash
4444 * table state, so each changed css_set will need its hash recomputed.
4445 * this is all done under the css_set_lock.
4446 */
4447 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004448 hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004449 /* skip entries that we already rehashed */
4450 if (cg->subsys[ss->subsys_id])
4451 continue;
4452 /* remove existing entry */
4453 hash_del(&cg->hlist);
4454 /* set new value */
4455 cg->subsys[ss->subsys_id] = css;
4456 /* recompute hash and restore entry */
4457 key = css_set_hash(cg->subsys);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004458 hash_add(css_set_table, &cg->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004459 }
4460 write_unlock(&css_set_lock);
4461
Ben Blume6a11052010-03-10 15:22:09 -08004462 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004463 ret = online_css(ss, dummytop);
4464 if (ret)
4465 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004466
Ben Blume6a11052010-03-10 15:22:09 -08004467 /* success! */
4468 mutex_unlock(&cgroup_mutex);
4469 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004470
4471err_unload:
4472 mutex_unlock(&cgroup_mutex);
4473 /* @ss can't be mounted here as try_module_get() would fail */
4474 cgroup_unload_subsys(ss);
4475 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004476}
4477EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4478
4479/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004480 * cgroup_unload_subsys: unload a modular subsystem
4481 * @ss: the subsystem to unload
4482 *
4483 * This function should be called in a modular subsystem's exitcall. When this
4484 * function is invoked, the refcount on the subsystem's module will be 0, so
4485 * the subsystem will not be attached to any hierarchy.
4486 */
4487void cgroup_unload_subsys(struct cgroup_subsys *ss)
4488{
4489 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004490
4491 BUG_ON(ss->module == NULL);
4492
4493 /*
4494 * we shouldn't be called if the subsystem is in use, and the use of
4495 * try_module_get in parse_cgroupfs_options should ensure that it
4496 * doesn't start being used while we're killing it off.
4497 */
4498 BUG_ON(ss->root != &rootnode);
4499
4500 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004501
Tejun Heoa31f2d32012-11-19 08:13:37 -08004502 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004503 ss->active = 0;
4504
Tejun Heoc897ff62013-02-27 17:03:49 -08004505 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004506 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004507
Ben Blumcf5d5942010-03-10 15:22:09 -08004508 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004509 subsys[ss->subsys_id] = NULL;
4510
4511 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004512 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004513
4514 /*
4515 * disentangle the css from all css_sets attached to the dummytop. as
4516 * in loading, we need to pay our respects to the hashtable gods.
4517 */
4518 write_lock(&css_set_lock);
4519 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4520 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004521 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004522
Li Zefan0ac801f2013-01-10 11:49:27 +08004523 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004524 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004525 key = css_set_hash(cg->subsys);
4526 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004527 }
4528 write_unlock(&css_set_lock);
4529
4530 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004531 * remove subsystem's css from the dummytop and free it - need to
4532 * free before marking as null because ss->css_free needs the
4533 * cgrp->subsys pointer to find their state. note that this also
4534 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004535 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004536 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004537 dummytop->subsys[ss->subsys_id] = NULL;
4538
4539 mutex_unlock(&cgroup_mutex);
4540}
4541EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4542
4543/**
Li Zefana043e3b2008-02-23 15:24:09 -08004544 * cgroup_init_early - cgroup initialization at system boot
4545 *
4546 * Initialize cgroups at system boot, and initialize any
4547 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004548 */
4549int __init cgroup_init_early(void)
4550{
4551 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004552 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004553 INIT_LIST_HEAD(&init_css_set.cg_links);
4554 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004555 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004556 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004557 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004558 root_count = 1;
4559 init_task.cgroups = &init_css_set;
4560
4561 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004562 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004563 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004564 &rootnode.top_cgroup.css_sets);
4565 list_add(&init_css_set_link.cg_link_list,
4566 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004567
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004568 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004569 struct cgroup_subsys *ss = subsys[i];
4570
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004571 /* at bootup time, we don't worry about modular subsystems */
4572 if (!ss || ss->module)
4573 continue;
4574
Paul Menageddbcc7e2007-10-18 23:39:30 -07004575 BUG_ON(!ss->name);
4576 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004577 BUG_ON(!ss->css_alloc);
4578 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004579 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004580 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004581 ss->name, ss->subsys_id);
4582 BUG();
4583 }
4584
4585 if (ss->early_init)
4586 cgroup_init_subsys(ss);
4587 }
4588 return 0;
4589}
4590
4591/**
Li Zefana043e3b2008-02-23 15:24:09 -08004592 * cgroup_init - cgroup initialization
4593 *
4594 * Register cgroup filesystem and /proc file, and initialize
4595 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004596 */
4597int __init cgroup_init(void)
4598{
4599 int err;
4600 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004601 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004602
4603 err = bdi_init(&cgroup_backing_dev_info);
4604 if (err)
4605 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004606
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004607 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004608 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004609
4610 /* at bootup time, we don't worry about modular subsystems */
4611 if (!ss || ss->module)
4612 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004613 if (!ss->early_init)
4614 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004615 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004616 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004617 }
4618
Li Zefan472b1052008-04-29 01:00:11 -07004619 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004620 key = css_set_hash(init_css_set.subsys);
4621 hash_add(css_set_table, &init_css_set.hlist, key);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004622 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004623
4624 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4625 if (!cgroup_kobj) {
4626 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004627 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004628 }
4629
4630 err = register_filesystem(&cgroup_fs_type);
4631 if (err < 0) {
4632 kobject_put(cgroup_kobj);
4633 goto out;
4634 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004635
Li Zefan46ae2202008-04-29 01:00:08 -07004636 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004637
Paul Menageddbcc7e2007-10-18 23:39:30 -07004638out:
Paul Menagea4243162007-10-18 23:39:35 -07004639 if (err)
4640 bdi_destroy(&cgroup_backing_dev_info);
4641
Paul Menageddbcc7e2007-10-18 23:39:30 -07004642 return err;
4643}
Paul Menageb4f48b62007-10-18 23:39:33 -07004644
Paul Menagea4243162007-10-18 23:39:35 -07004645/*
4646 * proc_cgroup_show()
4647 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4648 * - Used for /proc/<pid>/cgroup.
4649 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4650 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004651 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004652 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4653 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4654 * cgroup to top_cgroup.
4655 */
4656
4657/* TODO: Use a proper seq_file iterator */
4658static int proc_cgroup_show(struct seq_file *m, void *v)
4659{
4660 struct pid *pid;
4661 struct task_struct *tsk;
4662 char *buf;
4663 int retval;
4664 struct cgroupfs_root *root;
4665
4666 retval = -ENOMEM;
4667 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4668 if (!buf)
4669 goto out;
4670
4671 retval = -ESRCH;
4672 pid = m->private;
4673 tsk = get_pid_task(pid, PIDTYPE_PID);
4674 if (!tsk)
4675 goto out_free;
4676
4677 retval = 0;
4678
4679 mutex_lock(&cgroup_mutex);
4680
Li Zefane5f6a862009-01-07 18:07:41 -08004681 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004682 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004683 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004684 int count = 0;
4685
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004686 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004687 for_each_subsys(root, ss)
4688 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004689 if (strlen(root->name))
4690 seq_printf(m, "%sname=%s", count ? "," : "",
4691 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004692 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004693 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004694 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004695 if (retval < 0)
4696 goto out_unlock;
4697 seq_puts(m, buf);
4698 seq_putc(m, '\n');
4699 }
4700
4701out_unlock:
4702 mutex_unlock(&cgroup_mutex);
4703 put_task_struct(tsk);
4704out_free:
4705 kfree(buf);
4706out:
4707 return retval;
4708}
4709
4710static int cgroup_open(struct inode *inode, struct file *file)
4711{
4712 struct pid *pid = PROC_I(inode)->pid;
4713 return single_open(file, proc_cgroup_show, pid);
4714}
4715
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004716const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004717 .open = cgroup_open,
4718 .read = seq_read,
4719 .llseek = seq_lseek,
4720 .release = single_release,
4721};
4722
4723/* Display information about each subsystem and each hierarchy */
4724static int proc_cgroupstats_show(struct seq_file *m, void *v)
4725{
4726 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004727
Paul Menage8bab8dd2008-04-04 14:29:57 -07004728 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004729 /*
4730 * ideally we don't want subsystems moving around while we do this.
4731 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4732 * subsys/hierarchy state.
4733 */
Paul Menagea4243162007-10-18 23:39:35 -07004734 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004735 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4736 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004737 if (ss == NULL)
4738 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004739 seq_printf(m, "%s\t%d\t%d\t%d\n",
4740 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004741 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004742 }
4743 mutex_unlock(&cgroup_mutex);
4744 return 0;
4745}
4746
4747static int cgroupstats_open(struct inode *inode, struct file *file)
4748{
Al Viro9dce07f2008-03-29 03:07:28 +00004749 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004750}
4751
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004752static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004753 .open = cgroupstats_open,
4754 .read = seq_read,
4755 .llseek = seq_lseek,
4756 .release = single_release,
4757};
4758
Paul Menageb4f48b62007-10-18 23:39:33 -07004759/**
4760 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004761 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004762 *
4763 * Description: A task inherits its parent's cgroup at fork().
4764 *
4765 * A pointer to the shared css_set was automatically copied in
4766 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004767 * it was not made under the protection of RCU or cgroup_mutex, so
4768 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4769 * have already changed current->cgroups, allowing the previously
4770 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004771 *
4772 * At the point that cgroup_fork() is called, 'current' is the parent
4773 * task, and the passed argument 'child' points to the child task.
4774 */
4775void cgroup_fork(struct task_struct *child)
4776{
Tejun Heo9bb71302012-10-18 17:52:07 -07004777 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004778 child->cgroups = current->cgroups;
4779 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004780 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004781 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004782}
4783
4784/**
Li Zefana043e3b2008-02-23 15:24:09 -08004785 * cgroup_post_fork - called on a new task after adding it to the task list
4786 * @child: the task in question
4787 *
Tejun Heo5edee612012-10-16 15:03:14 -07004788 * Adds the task to the list running through its css_set if necessary and
4789 * call the subsystem fork() callbacks. Has to be after the task is
4790 * visible on the task list in case we race with the first call to
4791 * cgroup_iter_start() - to guarantee that the new task ends up on its
4792 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004793 */
Paul Menage817929e2007-10-18 23:39:36 -07004794void cgroup_post_fork(struct task_struct *child)
4795{
Tejun Heo5edee612012-10-16 15:03:14 -07004796 int i;
4797
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004798 /*
4799 * use_task_css_set_links is set to 1 before we walk the tasklist
4800 * under the tasklist_lock and we read it here after we added the child
4801 * to the tasklist under the tasklist_lock as well. If the child wasn't
4802 * yet in the tasklist when we walked through it from
4803 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4804 * should be visible now due to the paired locking and barriers implied
4805 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4806 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4807 * lock on fork.
4808 */
Paul Menage817929e2007-10-18 23:39:36 -07004809 if (use_task_css_set_links) {
4810 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004811 task_lock(child);
4812 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004813 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004814 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004815 write_unlock(&css_set_lock);
4816 }
Tejun Heo5edee612012-10-16 15:03:14 -07004817
4818 /*
4819 * Call ss->fork(). This must happen after @child is linked on
4820 * css_set; otherwise, @child might change state between ->fork()
4821 * and addition to css_set.
4822 */
4823 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004824 /*
4825 * fork/exit callbacks are supported only for builtin
4826 * subsystems, and the builtin section of the subsys
4827 * array is immutable, so we don't need to lock the
4828 * subsys array here. On the other hand, modular section
4829 * of the array can be freed at module unload, so we
4830 * can't touch that.
4831 */
4832 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004833 struct cgroup_subsys *ss = subsys[i];
4834
Tejun Heo5edee612012-10-16 15:03:14 -07004835 if (ss->fork)
4836 ss->fork(child);
4837 }
4838 }
Paul Menage817929e2007-10-18 23:39:36 -07004839}
Tejun Heo5edee612012-10-16 15:03:14 -07004840
Paul Menage817929e2007-10-18 23:39:36 -07004841/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004842 * cgroup_exit - detach cgroup from exiting task
4843 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004844 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004845 *
4846 * Description: Detach cgroup from @tsk and release it.
4847 *
4848 * Note that cgroups marked notify_on_release force every task in
4849 * them to take the global cgroup_mutex mutex when exiting.
4850 * This could impact scaling on very large systems. Be reluctant to
4851 * use notify_on_release cgroups where very high task exit scaling
4852 * is required on large systems.
4853 *
4854 * the_top_cgroup_hack:
4855 *
4856 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4857 *
4858 * We call cgroup_exit() while the task is still competent to
4859 * handle notify_on_release(), then leave the task attached to the
4860 * root cgroup in each hierarchy for the remainder of its exit.
4861 *
4862 * To do this properly, we would increment the reference count on
4863 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4864 * code we would add a second cgroup function call, to drop that
4865 * reference. This would just create an unnecessary hot spot on
4866 * the top_cgroup reference count, to no avail.
4867 *
4868 * Normally, holding a reference to a cgroup without bumping its
4869 * count is unsafe. The cgroup could go away, or someone could
4870 * attach us to a different cgroup, decrementing the count on
4871 * the first cgroup that we never incremented. But in this case,
4872 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004873 * which wards off any cgroup_attach_task() attempts, or task is a failed
4874 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004875 */
4876void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4877{
Paul Menage817929e2007-10-18 23:39:36 -07004878 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004879 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004880
4881 /*
4882 * Unlink from the css_set task list if necessary.
4883 * Optimistically check cg_list before taking
4884 * css_set_lock
4885 */
4886 if (!list_empty(&tsk->cg_list)) {
4887 write_lock(&css_set_lock);
4888 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004889 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004890 write_unlock(&css_set_lock);
4891 }
4892
Paul Menageb4f48b62007-10-18 23:39:33 -07004893 /* Reassign the task to the init_css_set. */
4894 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004895 cg = tsk->cgroups;
4896 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004897
4898 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004899 /*
4900 * fork/exit callbacks are supported only for builtin
4901 * subsystems, see cgroup_post_fork() for details.
4902 */
4903 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004904 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004905
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004906 if (ss->exit) {
4907 struct cgroup *old_cgrp =
4908 rcu_dereference_raw(cg->subsys[i])->cgroup;
4909 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004910 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004911 }
4912 }
4913 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004914 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004915
Li Zefanb5d646f2013-01-24 14:43:51 +08004916 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004917}
Paul Menage697f4162007-10-18 23:39:34 -07004918
Paul Menagebd89aab2007-10-18 23:40:44 -07004919static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004920{
4921 /* All of these checks rely on RCU to keep the cgroup
4922 * structure alive */
Li Zefanf50daa72013-03-01 15:06:07 +08004923 if (cgroup_is_releasable(cgrp) &&
4924 !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) {
4925 /*
4926 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004927 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004928 * it now
4929 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004930 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004931
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004932 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004933 if (!cgroup_is_removed(cgrp) &&
4934 list_empty(&cgrp->release_list)) {
4935 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004936 need_schedule_work = 1;
4937 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004938 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004939 if (need_schedule_work)
4940 schedule_work(&release_agent_work);
4941 }
4942}
4943
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004944/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07004945bool __css_tryget(struct cgroup_subsys_state *css)
4946{
Tejun Heoe9316082012-11-05 09:16:58 -08004947 while (true) {
4948 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004949
Tejun Heoe9316082012-11-05 09:16:58 -08004950 v = css_refcnt(css);
4951 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
4952 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07004953 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08004954 else if (t < 0)
4955 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07004956 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08004957 }
Tejun Heo28b4c272012-04-01 12:09:56 -07004958}
4959EXPORT_SYMBOL_GPL(__css_tryget);
4960
4961/* Caller must verify that the css is not for root cgroup */
4962void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004963{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004964 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004965
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004966 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08004967 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08004968 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004969}
Ben Blum67523c42010-03-10 15:22:11 -08004970EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004971
4972/*
4973 * Notify userspace when a cgroup is released, by running the
4974 * configured release agent with the name of the cgroup (path
4975 * relative to the root of cgroup file system) as the argument.
4976 *
4977 * Most likely, this user command will try to rmdir this cgroup.
4978 *
4979 * This races with the possibility that some other task will be
4980 * attached to this cgroup before it is removed, or that some other
4981 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4982 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4983 * unused, and this cgroup will be reprieved from its death sentence,
4984 * to continue to serve a useful existence. Next time it's released,
4985 * we will get notified again, if it still has 'notify_on_release' set.
4986 *
4987 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4988 * means only wait until the task is successfully execve()'d. The
4989 * separate release agent task is forked by call_usermodehelper(),
4990 * then control in this thread returns here, without waiting for the
4991 * release agent task. We don't bother to wait because the caller of
4992 * this routine has no use for the exit status of the release agent
4993 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004994 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004995static void cgroup_release_agent(struct work_struct *work)
4996{
4997 BUG_ON(work != &release_agent_work);
4998 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004999 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005000 while (!list_empty(&release_list)) {
5001 char *argv[3], *envp[3];
5002 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005003 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005004 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005005 struct cgroup,
5006 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005007 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005008 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005009 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005010 if (!pathbuf)
5011 goto continue_free;
5012 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5013 goto continue_free;
5014 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5015 if (!agentbuf)
5016 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005017
5018 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005019 argv[i++] = agentbuf;
5020 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005021 argv[i] = NULL;
5022
5023 i = 0;
5024 /* minimal command environment */
5025 envp[i++] = "HOME=/";
5026 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5027 envp[i] = NULL;
5028
5029 /* Drop the lock while we invoke the usermode helper,
5030 * since the exec could involve hitting disk and hence
5031 * be a slow process */
5032 mutex_unlock(&cgroup_mutex);
5033 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005034 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005035 continue_free:
5036 kfree(pathbuf);
5037 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005038 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005039 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005040 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005041 mutex_unlock(&cgroup_mutex);
5042}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005043
5044static int __init cgroup_disable(char *str)
5045{
5046 int i;
5047 char *token;
5048
5049 while ((token = strsep(&str, ",")) != NULL) {
5050 if (!*token)
5051 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005052 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005053 struct cgroup_subsys *ss = subsys[i];
5054
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005055 /*
5056 * cgroup_disable, being at boot time, can't
5057 * know about module subsystems, so we don't
5058 * worry about them.
5059 */
5060 if (!ss || ss->module)
5061 continue;
5062
Paul Menage8bab8dd2008-04-04 14:29:57 -07005063 if (!strcmp(token, ss->name)) {
5064 ss->disabled = 1;
5065 printk(KERN_INFO "Disabling %s control group"
5066 " subsystem\n", ss->name);
5067 break;
5068 }
5069 }
5070 }
5071 return 1;
5072}
5073__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005074
5075/*
5076 * Functons for CSS ID.
5077 */
5078
5079/*
5080 *To get ID other than 0, this should be called when !cgroup_is_removed().
5081 */
5082unsigned short css_id(struct cgroup_subsys_state *css)
5083{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005084 struct css_id *cssid;
5085
5086 /*
5087 * This css_id() can return correct value when somone has refcnt
5088 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5089 * it's unchanged until freed.
5090 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005091 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005092
5093 if (cssid)
5094 return cssid->id;
5095 return 0;
5096}
Ben Blum67523c42010-03-10 15:22:11 -08005097EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005098
5099unsigned short css_depth(struct cgroup_subsys_state *css)
5100{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005101 struct css_id *cssid;
5102
Tejun Heo28b4c272012-04-01 12:09:56 -07005103 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005104
5105 if (cssid)
5106 return cssid->depth;
5107 return 0;
5108}
Ben Blum67523c42010-03-10 15:22:11 -08005109EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005110
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005111/**
5112 * css_is_ancestor - test "root" css is an ancestor of "child"
5113 * @child: the css to be tested.
5114 * @root: the css supporsed to be an ancestor of the child.
5115 *
5116 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005117 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005118 * But, considering usual usage, the csses should be valid objects after test.
5119 * Assuming that the caller will do some action to the child if this returns
5120 * returns true, the caller must take "child";s reference count.
5121 * If "child" is valid object and this returns true, "root" is valid, too.
5122 */
5123
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005124bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005125 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005126{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005127 struct css_id *child_id;
5128 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005129
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005130 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005131 if (!child_id)
5132 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005133 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005134 if (!root_id)
5135 return false;
5136 if (child_id->depth < root_id->depth)
5137 return false;
5138 if (child_id->stack[root_id->depth] != root_id->id)
5139 return false;
5140 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005141}
5142
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005143void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5144{
5145 struct css_id *id = css->id;
5146 /* When this is called before css_id initialization, id can be NULL */
5147 if (!id)
5148 return;
5149
5150 BUG_ON(!ss->use_id);
5151
5152 rcu_assign_pointer(id->css, NULL);
5153 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005154 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005155 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005156 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005157 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005158}
Ben Blum67523c42010-03-10 15:22:11 -08005159EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005160
5161/*
5162 * This is called by init or create(). Then, calls to this function are
5163 * always serialized (By cgroup_mutex() at create()).
5164 */
5165
5166static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5167{
5168 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005169 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005170
5171 BUG_ON(!ss->use_id);
5172
5173 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5174 newid = kzalloc(size, GFP_KERNEL);
5175 if (!newid)
5176 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005177
5178 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005179 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005180 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005181 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005182 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005183 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005184
5185 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005186 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005187 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005188
Tejun Heod228d9e2013-02-27 17:04:54 -08005189 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005190 newid->depth = depth;
5191 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005192err_out:
5193 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005194 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005195
5196}
5197
Ben Blume6a11052010-03-10 15:22:09 -08005198static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5199 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005200{
5201 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005202
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005203 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005204 idr_init(&ss->idr);
5205
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005206 newid = get_new_cssid(ss, 0);
5207 if (IS_ERR(newid))
5208 return PTR_ERR(newid);
5209
5210 newid->stack[0] = newid->id;
5211 newid->css = rootcss;
5212 rootcss->id = newid;
5213 return 0;
5214}
5215
5216static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5217 struct cgroup *child)
5218{
5219 int subsys_id, i, depth = 0;
5220 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005221 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005222
5223 subsys_id = ss->subsys_id;
5224 parent_css = parent->subsys[subsys_id];
5225 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005226 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005227 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005228
5229 child_id = get_new_cssid(ss, depth);
5230 if (IS_ERR(child_id))
5231 return PTR_ERR(child_id);
5232
5233 for (i = 0; i < depth; i++)
5234 child_id->stack[i] = parent_id->stack[i];
5235 child_id->stack[depth] = child_id->id;
5236 /*
5237 * child_id->css pointer will be set after this cgroup is available
5238 * see cgroup_populate_dir()
5239 */
5240 rcu_assign_pointer(child_css->id, child_id);
5241
5242 return 0;
5243}
5244
5245/**
5246 * css_lookup - lookup css by id
5247 * @ss: cgroup subsys to be looked into.
5248 * @id: the id
5249 *
5250 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5251 * NULL if not. Should be called under rcu_read_lock()
5252 */
5253struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5254{
5255 struct css_id *cssid = NULL;
5256
5257 BUG_ON(!ss->use_id);
5258 cssid = idr_find(&ss->idr, id);
5259
5260 if (unlikely(!cssid))
5261 return NULL;
5262
5263 return rcu_dereference(cssid->css);
5264}
Ben Blum67523c42010-03-10 15:22:11 -08005265EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005266
5267/**
5268 * css_get_next - lookup next cgroup under specified hierarchy.
5269 * @ss: pointer to subsystem
5270 * @id: current position of iteration.
5271 * @root: pointer to css. search tree under this.
5272 * @foundid: position of found object.
5273 *
5274 * Search next css under the specified hierarchy of rootid. Calling under
5275 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5276 */
5277struct cgroup_subsys_state *
5278css_get_next(struct cgroup_subsys *ss, int id,
5279 struct cgroup_subsys_state *root, int *foundid)
5280{
5281 struct cgroup_subsys_state *ret = NULL;
5282 struct css_id *tmp;
5283 int tmpid;
5284 int rootid = css_id(root);
5285 int depth = css_depth(root);
5286
5287 if (!rootid)
5288 return NULL;
5289
5290 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005291 WARN_ON_ONCE(!rcu_read_lock_held());
5292
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005293 /* fill start point for scan */
5294 tmpid = id;
5295 while (1) {
5296 /*
5297 * scan next entry from bitmap(tree), tmpid is updated after
5298 * idr_get_next().
5299 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005300 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005301 if (!tmp)
5302 break;
5303 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5304 ret = rcu_dereference(tmp->css);
5305 if (ret) {
5306 *foundid = tmpid;
5307 break;
5308 }
5309 }
5310 /* continue to scan from next id */
5311 tmpid = tmpid + 1;
5312 }
5313 return ret;
5314}
5315
Stephane Eraniane5d13672011-02-14 11:20:01 +02005316/*
5317 * get corresponding css from file open on cgroupfs directory
5318 */
5319struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5320{
5321 struct cgroup *cgrp;
5322 struct inode *inode;
5323 struct cgroup_subsys_state *css;
5324
Al Viro496ad9a2013-01-23 17:07:38 -05005325 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005326 /* check in cgroup filesystem dir */
5327 if (inode->i_op != &cgroup_dir_inode_operations)
5328 return ERR_PTR(-EBADF);
5329
5330 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5331 return ERR_PTR(-EINVAL);
5332
5333 /* get cgroup */
5334 cgrp = __d_cgrp(f->f_dentry);
5335 css = cgrp->subsys[id];
5336 return css ? css : ERR_PTR(-ENOENT);
5337}
5338
Paul Menagefe693432009-09-23 15:56:20 -07005339#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005340static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005341{
5342 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5343
5344 if (!css)
5345 return ERR_PTR(-ENOMEM);
5346
5347 return css;
5348}
5349
Tejun Heo92fb9742012-11-19 08:13:38 -08005350static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005351{
5352 kfree(cont->subsys[debug_subsys_id]);
5353}
5354
5355static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5356{
5357 return atomic_read(&cont->count);
5358}
5359
5360static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5361{
5362 return cgroup_task_count(cont);
5363}
5364
5365static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5366{
5367 return (u64)(unsigned long)current->cgroups;
5368}
5369
5370static u64 current_css_set_refcount_read(struct cgroup *cont,
5371 struct cftype *cft)
5372{
5373 u64 count;
5374
5375 rcu_read_lock();
5376 count = atomic_read(&current->cgroups->refcount);
5377 rcu_read_unlock();
5378 return count;
5379}
5380
Paul Menage7717f7b2009-09-23 15:56:22 -07005381static int current_css_set_cg_links_read(struct cgroup *cont,
5382 struct cftype *cft,
5383 struct seq_file *seq)
5384{
5385 struct cg_cgroup_link *link;
5386 struct css_set *cg;
5387
5388 read_lock(&css_set_lock);
5389 rcu_read_lock();
5390 cg = rcu_dereference(current->cgroups);
5391 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5392 struct cgroup *c = link->cgrp;
5393 const char *name;
5394
5395 if (c->dentry)
5396 name = c->dentry->d_name.name;
5397 else
5398 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005399 seq_printf(seq, "Root %d group %s\n",
5400 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005401 }
5402 rcu_read_unlock();
5403 read_unlock(&css_set_lock);
5404 return 0;
5405}
5406
5407#define MAX_TASKS_SHOWN_PER_CSS 25
5408static int cgroup_css_links_read(struct cgroup *cont,
5409 struct cftype *cft,
5410 struct seq_file *seq)
5411{
5412 struct cg_cgroup_link *link;
5413
5414 read_lock(&css_set_lock);
5415 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5416 struct css_set *cg = link->cg;
5417 struct task_struct *task;
5418 int count = 0;
5419 seq_printf(seq, "css_set %p\n", cg);
5420 list_for_each_entry(task, &cg->tasks, cg_list) {
5421 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5422 seq_puts(seq, " ...\n");
5423 break;
5424 } else {
5425 seq_printf(seq, " task %d\n",
5426 task_pid_vnr(task));
5427 }
5428 }
5429 }
5430 read_unlock(&css_set_lock);
5431 return 0;
5432}
5433
Paul Menagefe693432009-09-23 15:56:20 -07005434static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5435{
5436 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5437}
5438
5439static struct cftype debug_files[] = {
5440 {
5441 .name = "cgroup_refcount",
5442 .read_u64 = cgroup_refcount_read,
5443 },
5444 {
5445 .name = "taskcount",
5446 .read_u64 = debug_taskcount_read,
5447 },
5448
5449 {
5450 .name = "current_css_set",
5451 .read_u64 = current_css_set_read,
5452 },
5453
5454 {
5455 .name = "current_css_set_refcount",
5456 .read_u64 = current_css_set_refcount_read,
5457 },
5458
5459 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005460 .name = "current_css_set_cg_links",
5461 .read_seq_string = current_css_set_cg_links_read,
5462 },
5463
5464 {
5465 .name = "cgroup_css_links",
5466 .read_seq_string = cgroup_css_links_read,
5467 },
5468
5469 {
Paul Menagefe693432009-09-23 15:56:20 -07005470 .name = "releasable",
5471 .read_u64 = releasable_read,
5472 },
Paul Menagefe693432009-09-23 15:56:20 -07005473
Tejun Heo4baf6e32012-04-01 12:09:55 -07005474 { } /* terminate */
5475};
Paul Menagefe693432009-09-23 15:56:20 -07005476
5477struct cgroup_subsys debug_subsys = {
5478 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005479 .css_alloc = debug_css_alloc,
5480 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005481 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005482 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005483};
5484#endif /* CONFIG_CGROUP_DEBUG */